Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 5 | #ifndef COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_BUILDER_H_ |
| 6 | #define COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_BUILDER_H_ |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 7 | |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 8 | #include <map> |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/strings/string_piece.h" |
| 14 | #include "components/cbor/writer.h" |
| 15 | |
Kunihiko Sakamoto | 2495266 | 2020-06-30 03:11:09 | [diff] [blame] | 16 | namespace web_package { |
Miras Myrzakerey | abbfce3 | 2021-08-25 05:25:18 | [diff] [blame] | 17 | |
| 18 | enum class BundleVersion { |
Miras Myrzakerey | abbfce3 | 2021-08-25 05:25:18 | [diff] [blame] | 19 | kB2, |
| 20 | }; |
| 21 | |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 22 | // This class can be used to create a Web Bundle. |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 23 | class WebBundleBuilder { |
| 24 | public: |
| 25 | using Headers = std::vector<std::pair<std::string, std::string>>; |
| 26 | struct ResponseLocation { |
| 27 | // /components/cbor uses int64_t for integer types. |
| 28 | int64_t offset; |
| 29 | int64_t length; |
| 30 | }; |
| 31 | |
Kunihiko Sakamoto | aa3ebfb5 | 2022-04-12 02:04:08 | [diff] [blame^] | 32 | explicit WebBundleBuilder( |
| 33 | BundleVersion version = BundleVersion::kB2, |
| 34 | bool allow_invalid_utf8_strings_for_testing = false); |
Miras Myrzakerey | abbfce3 | 2021-08-25 05:25:18 | [diff] [blame] | 35 | |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 36 | ~WebBundleBuilder(); |
| 37 | |
| 38 | void AddExchange(base::StringPiece url, |
| 39 | const Headers& response_headers, |
| 40 | base::StringPiece payload); |
| 41 | |
| 42 | ResponseLocation AddResponse(const Headers& headers, |
| 43 | base::StringPiece payload); |
| 44 | |
| 45 | void AddIndexEntry(base::StringPiece url, |
Kunihiko Sakamoto | aa3ebfb5 | 2022-04-12 02:04:08 | [diff] [blame^] | 46 | const ResponseLocation& response_location); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 47 | void AddSection(base::StringPiece name, cbor::Value section); |
| 48 | void AddAuthority(cbor::Value::MapValue authority); |
| 49 | void AddVouchedSubset(cbor::Value::MapValue vouched_subset); |
Kunihiko Sakamoto | aa3ebfb5 | 2022-04-12 02:04:08 | [diff] [blame^] | 50 | void AddPrimaryURL(base::StringPiece url); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 51 | |
| 52 | std::vector<uint8_t> CreateBundle(); |
| 53 | |
| 54 | // Creates a signed-subset structure with single subset-hashes entry, |
| 55 | // and returns it as a CBOR bytestring. |
| 56 | cbor::Value CreateEncodedSigned(base::StringPiece validity_url, |
| 57 | base::StringPiece auth_sha256, |
| 58 | int64_t date, |
| 59 | int64_t expires, |
| 60 | base::StringPiece url, |
| 61 | base::StringPiece header_sha256, |
| 62 | base::StringPiece payload_integrity_header); |
| 63 | |
| 64 | private: |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 65 | std::vector<uint8_t> CreateTopLevel(); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 66 | std::vector<uint8_t> Encode(const cbor::Value& value); |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 67 | cbor::Value GetCborValueOfURL(base::StringPiece url); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 68 | |
| 69 | int64_t EncodedLength(const cbor::Value& value); |
| 70 | |
| 71 | cbor::Writer::Config writer_config_; |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 72 | cbor::Value::ArrayValue section_lengths_; |
| 73 | cbor::Value::ArrayValue sections_; |
Kunihiko Sakamoto | aa3ebfb5 | 2022-04-12 02:04:08 | [diff] [blame^] | 74 | std::map<std::string, ResponseLocation> delayed_index_; |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 75 | cbor::Value::ArrayValue responses_; |
| 76 | cbor::Value::ArrayValue authorities_; |
| 77 | cbor::Value::ArrayValue vouched_subsets_; |
Miras Myrzakerey | abbfce3 | 2021-08-25 05:25:18 | [diff] [blame] | 78 | BundleVersion version_; |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 79 | int64_t current_responses_offset_ = 0; |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 80 | }; |
| 81 | |
Kunihiko Sakamoto | 2495266 | 2020-06-30 03:11:09 | [diff] [blame] | 82 | } // namespace web_package |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 83 | |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 84 | #endif // COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_BUILDER_H_ |