blob: b6cbf494d9ae3ace1677c01b89bfd460bc4f5725 [file] [log] [blame]
Tsuyoshi Horo2730fd52020-01-08 22:33:481// 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 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>
10#include <utility>
11#include <vector>
12
13#include "base/strings/string_piece.h"
14#include "components/cbor/writer.h"
15
Kunihiko Sakamoto24952662020-06-30 03:11:0916namespace web_package {
Miras Myrzakereyabbfce32021-08-25 05:25:1817
18enum class BundleVersion {
Miras Myrzakereyabbfce32021-08-25 05:25:1819 kB2,
20};
21
Miras Myrzakereye1402562021-10-12 06:49:5422// This class can be used to create a Web Bundle.
Tsuyoshi Horo2730fd52020-01-08 22:33:4823class 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 Sakamotoaa3ebfb52022-04-12 02:04:0832 explicit WebBundleBuilder(
33 BundleVersion version = BundleVersion::kB2,
34 bool allow_invalid_utf8_strings_for_testing = false);
Miras Myrzakereyabbfce32021-08-25 05:25:1835
Tsuyoshi Horo2730fd52020-01-08 22:33:4836 ~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 Sakamotoaa3ebfb52022-04-12 02:04:0846 const ResponseLocation& response_location);
Tsuyoshi Horo2730fd52020-01-08 22:33:4847 void AddSection(base::StringPiece name, cbor::Value section);
48 void AddAuthority(cbor::Value::MapValue authority);
49 void AddVouchedSubset(cbor::Value::MapValue vouched_subset);
Kunihiko Sakamotoaa3ebfb52022-04-12 02:04:0850 void AddPrimaryURL(base::StringPiece url);
Tsuyoshi Horo2730fd52020-01-08 22:33:4851
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 Myrzakereye1402562021-10-12 06:49:5465 std::vector<uint8_t> CreateTopLevel();
Tsuyoshi Horo2730fd52020-01-08 22:33:4866 std::vector<uint8_t> Encode(const cbor::Value& value);
Miras Myrzakereye1402562021-10-12 06:49:5467 cbor::Value GetCborValueOfURL(base::StringPiece url);
Tsuyoshi Horo2730fd52020-01-08 22:33:4868
69 int64_t EncodedLength(const cbor::Value& value);
70
71 cbor::Writer::Config writer_config_;
Tsuyoshi Horo2730fd52020-01-08 22:33:4872 cbor::Value::ArrayValue section_lengths_;
73 cbor::Value::ArrayValue sections_;
Kunihiko Sakamotoaa3ebfb52022-04-12 02:04:0874 std::map<std::string, ResponseLocation> delayed_index_;
Tsuyoshi Horo2730fd52020-01-08 22:33:4875 cbor::Value::ArrayValue responses_;
76 cbor::Value::ArrayValue authorities_;
77 cbor::Value::ArrayValue vouched_subsets_;
Miras Myrzakereyabbfce32021-08-25 05:25:1878 BundleVersion version_;
Miras Myrzakereye1402562021-10-12 06:49:5479 int64_t current_responses_offset_ = 0;
Tsuyoshi Horo2730fd52020-01-08 22:33:4880};
81
Kunihiko Sakamoto24952662020-06-30 03:11:0982} // namespace web_package
Tsuyoshi Horo2730fd52020-01-08 22:33:4883
Miras Myrzakereye1402562021-10-12 06:49:5484#endif // COMPONENTS_WEB_PACKAGE_WEB_BUNDLE_BUILDER_H_