blob: dff6ae21f75367fc35d43e954a7ed3773416a13d [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2020 The Chromium Authors
Tsuyoshi Horo2730fd52020-01-08 22:33:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Miras Myrzakereye1402562021-10-12 06:49:545#ifndef COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_BUILDER_H_
6#define COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_BUILDER_H_
Tsuyoshi Horo2730fd52020-01-08 22:33:487
Miras Myrzakereye1402562021-10-12 06:49:548#include <map>
Tsuyoshi Horo2730fd52020-01-08 22:33:489#include <string>
Claudio DeSouza5b2fd76a2024-05-27 19:38:5810#include <string_view>
Tsuyoshi Horo2730fd52020-01-08 22:33:4811#include <utility>
12#include <vector>
13
Tsuyoshi Horo2730fd52020-01-08 22:33:4814#include "components/cbor/writer.h"
Christian Flachfe2d65c2022-11-29 11:58:4515#include "url/gurl.h"
Tsuyoshi Horo2730fd52020-01-08 22:33:4816
Kunihiko Sakamoto24952662020-06-30 03:11:0917namespace web_package {
Miras Myrzakereyabbfce32021-08-25 05:25:1818
19enum class BundleVersion {
Miras Myrzakereyabbfce32021-08-25 05:25:1820 kB2,
21};
22
Miras Myrzakereye1402562021-10-12 06:49:5423// This class can be used to create a Web Bundle.
Tsuyoshi Horo2730fd52020-01-08 22:33:4824class 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 Sakamotoaa3ebfb52022-04-12 02:04:0833 explicit WebBundleBuilder(
34 BundleVersion version = BundleVersion::kB2,
35 bool allow_invalid_utf8_strings_for_testing = false);
Miras Myrzakereyabbfce32021-08-25 05:25:1836
Tsuyoshi Horo2730fd52020-01-08 22:33:4837 ~WebBundleBuilder();
38
Christian Flachfe2d65c2022-11-29 11:58:4539 // Add an exchange to the Web Bundle for a given `GURL`.
40 void AddExchange(const GURL& url,
41 const Headers& response_headers,
Claudio DeSouza5b2fd76a2024-05-27 19:38:5842 std::string_view payload);
Christian Flachfe2d65c2022-11-29 11:58:4543 // 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 DeSouza5b2fd76a2024-05-27 19:38:5846 void AddExchange(std::string_view url,
Tsuyoshi Horo2730fd52020-01-08 22:33:4847 const Headers& response_headers,
Claudio DeSouza5b2fd76a2024-05-27 19:38:5848 std::string_view payload);
Tsuyoshi Horo2730fd52020-01-08 22:33:4849
50 ResponseLocation AddResponse(const Headers& headers,
Claudio DeSouza5b2fd76a2024-05-27 19:38:5851 std::string_view payload);
Tsuyoshi Horo2730fd52020-01-08 22:33:4852
Christian Flachfe2d65c2022-11-29 11:58:4553 // 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 DeSouza5b2fd76a2024-05-27 19:38:5860 void AddIndexEntry(std::string_view url,
Kunihiko Sakamotoaa3ebfb52022-04-12 02:04:0861 const ResponseLocation& response_location);
Christian Flachfe2d65c2022-11-29 11:58:4562
Claudio DeSouza5b2fd76a2024-05-27 19:38:5863 void AddSection(std::string_view name, cbor::Value section);
Christian Flachfe2d65c2022-11-29 11:58:4564
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 DeSouza5b2fd76a2024-05-27 19:38:5870 void AddPrimaryURL(std::string_view url);
Tsuyoshi Horo2730fd52020-01-08 22:33:4871
72 std::vector<uint8_t> CreateBundle();
73
Tsuyoshi Horo2730fd52020-01-08 22:33:4874 private:
Miras Myrzakereye1402562021-10-12 06:49:5475 std::vector<uint8_t> CreateTopLevel();
Tsuyoshi Horo2730fd52020-01-08 22:33:4876 std::vector<uint8_t> Encode(const cbor::Value& value);
Claudio DeSouza5b2fd76a2024-05-27 19:38:5877 cbor::Value GetCborValueOfURL(std::string_view url);
Tsuyoshi Horo2730fd52020-01-08 22:33:4878
79 int64_t EncodedLength(const cbor::Value& value);
80
81 cbor::Writer::Config writer_config_;
Tsuyoshi Horo2730fd52020-01-08 22:33:4882 cbor::Value::ArrayValue section_lengths_;
83 cbor::Value::ArrayValue sections_;
Kunihiko Sakamotoaa3ebfb52022-04-12 02:04:0884 std::map<std::string, ResponseLocation> delayed_index_;
Tsuyoshi Horo2730fd52020-01-08 22:33:4885 cbor::Value::ArrayValue responses_;
Miras Myrzakereyabbfce32021-08-25 05:25:1886 BundleVersion version_;
Miras Myrzakereye1402562021-10-12 06:49:5487 int64_t current_responses_offset_ = 0;
Tsuyoshi Horo2730fd52020-01-08 22:33:4888};
89
Kunihiko Sakamoto24952662020-06-30 03:11:0990} // namespace web_package
Tsuyoshi Horo2730fd52020-01-08 22:33:4891
Miras Myrzakereye1402562021-10-12 06:49:5492#endif // COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_BUILDER_H_