Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 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> |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 10 | #include <string_view> |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 14 | #include "components/cbor/writer.h" |
Christian Flach | fe2d65c | 2022-11-29 11:58:45 | [diff] [blame] | 15 | #include "url/gurl.h" |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 16 | |
Kunihiko Sakamoto | 2495266 | 2020-06-30 03:11:09 | [diff] [blame] | 17 | namespace web_package { |
Miras Myrzakerey | abbfce3 | 2021-08-25 05:25:18 | [diff] [blame] | 18 | |
| 19 | enum class BundleVersion { |
Miras Myrzakerey | abbfce3 | 2021-08-25 05:25:18 | [diff] [blame] | 20 | kB2, |
| 21 | }; |
| 22 | |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 23 | // This class can be used to create a Web Bundle. |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 24 | class WebBundleBuilder { |
| 25 | public: |
| 26 | using Headers = std::vector<std::pair<std::string, std::string>>; |
| 27 | struct ResponseLocation { |
| 28 | // /components/cbor uses int64_t for integer types. |
| 29 | int64_t offset; |
| 30 | int64_t length; |
| 31 | }; |
| 32 | |
Kunihiko Sakamoto | aa3ebfb5 | 2022-04-12 02:04:08 | [diff] [blame] | 33 | explicit WebBundleBuilder( |
| 34 | BundleVersion version = BundleVersion::kB2, |
| 35 | bool allow_invalid_utf8_strings_for_testing = false); |
Miras Myrzakerey | abbfce3 | 2021-08-25 05:25:18 | [diff] [blame] | 36 | |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 37 | ~WebBundleBuilder(); |
| 38 | |
Christian Flach | fe2d65c | 2022-11-29 11:58:45 | [diff] [blame] | 39 | // Add an exchange to the Web Bundle for a given `GURL`. |
| 40 | void AddExchange(const GURL& url, |
| 41 | const Headers& response_headers, |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 42 | std::string_view payload); |
Christian Flach | fe2d65c | 2022-11-29 11:58:45 | [diff] [blame] | 43 | // Add an exchange to the Web Bundle for a given `url` represented as a |
| 44 | // string. In contrast to providing the URL as `GURL`, this allows adding |
| 45 | // relative URLs to the Web Bundle. |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 46 | void AddExchange(std::string_view url, |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 47 | const Headers& response_headers, |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 48 | std::string_view payload); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 49 | |
| 50 | ResponseLocation AddResponse(const Headers& headers, |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 51 | std::string_view payload); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 52 | |
Christian Flach | fe2d65c | 2022-11-29 11:58:45 | [diff] [blame] | 53 | // Adds an entry to the "index" section of the Web Bundle for the given |
| 54 | // `GURL`. |
| 55 | void AddIndexEntry(const GURL& url, |
| 56 | const ResponseLocation& response_location); |
| 57 | // Adds an entry to the "index" section of the Web Bundle for the given `url` |
| 58 | // represented as a string. In contrast to providing the URL as `GURL`, this |
| 59 | // allows adding relative URLs to the Web Bundle. |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 60 | void AddIndexEntry(std::string_view url, |
Kunihiko Sakamoto | aa3ebfb5 | 2022-04-12 02:04:08 | [diff] [blame] | 61 | const ResponseLocation& response_location); |
Christian Flach | fe2d65c | 2022-11-29 11:58:45 | [diff] [blame] | 62 | |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 63 | void AddSection(std::string_view name, cbor::Value section); |
Christian Flach | fe2d65c | 2022-11-29 11:58:45 | [diff] [blame] | 64 | |
| 65 | // Adds a "primary" section to the Web Bundle containing a given `GURL`. |
| 66 | void AddPrimaryURL(const GURL& url); |
| 67 | // Adds a "primary" section to the Web Bundle for a given `url` represented as |
| 68 | // a string. In contrast to providing the URL as `GURL`, this allows setting |
| 69 | // relative URLs as the primary URL of a Web Bundle. |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 70 | void AddPrimaryURL(std::string_view url); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 71 | |
| 72 | std::vector<uint8_t> CreateBundle(); |
| 73 | |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 74 | private: |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 75 | std::vector<uint8_t> CreateTopLevel(); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 76 | std::vector<uint8_t> Encode(const cbor::Value& value); |
Claudio DeSouza | 5b2fd76a | 2024-05-27 19:38:58 | [diff] [blame] | 77 | cbor::Value GetCborValueOfURL(std::string_view url); |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 78 | |
| 79 | int64_t EncodedLength(const cbor::Value& value); |
| 80 | |
| 81 | cbor::Writer::Config writer_config_; |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 82 | cbor::Value::ArrayValue section_lengths_; |
| 83 | cbor::Value::ArrayValue sections_; |
Kunihiko Sakamoto | aa3ebfb5 | 2022-04-12 02:04:08 | [diff] [blame] | 84 | std::map<std::string, ResponseLocation> delayed_index_; |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 85 | cbor::Value::ArrayValue responses_; |
Miras Myrzakerey | abbfce3 | 2021-08-25 05:25:18 | [diff] [blame] | 86 | BundleVersion version_; |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 87 | int64_t current_responses_offset_ = 0; |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 88 | }; |
| 89 | |
Kunihiko Sakamoto | 2495266 | 2020-06-30 03:11:09 | [diff] [blame] | 90 | } // namespace web_package |
Tsuyoshi Horo | 2730fd5 | 2020-01-08 22:33:48 | [diff] [blame] | 91 | |
Miras Myrzakerey | e140256 | 2021-10-12 06:49:54 | [diff] [blame] | 92 | #endif // COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_BUILDER_H_ |