blob: 1fc0c515ffe78314d14516a858668e4b1111adf4 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2018 The Chromium Authors
Tsuyoshi Horocdbb4902018-04-12 06:09:142// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_WEB_PACKAGE_SIGNED_EXCHANGE_UTILS_H_
6#define CONTENT_BROWSER_WEB_PACKAGE_SIGNED_EXCHANGE_UTILS_H_
7
Arthur Sonzognic686e8f2024-01-11 08:36:378#include <optional>
Tsuyoshi Horocdbb4902018-04-12 06:09:149#include <string>
Md Hasibul Hasana963a9342024-04-03 10:15:1410#include <string_view>
Tsuyoshi Horocdbb4902018-04-12 06:09:1411
Kunihiko Sakamotob5c94d902018-09-04 04:09:0212#include "content/browser/web_package/signed_exchange_consts.h"
Tsuyoshi Horob40c7c32018-05-31 07:32:4513#include "content/browser/web_package/signed_exchange_error.h"
Tsuyoshi Horo06eb28f2019-02-21 13:52:2414#include "content/browser/web_package/signed_exchange_signature_verifier.h"
Kunihiko Sakamotob5c94d902018-09-04 04:09:0215#include "content/common/content_export.h"
Tsuyoshi Horod5eb7612019-05-09 08:59:4616#include "net/url_request/redirect_util.h"
Lucas Furukawa Gadani62fcfa92019-12-05 15:38:4117#include "services/network/public/mojom/url_response_head.mojom.h"
Kunihiko Sakamotod405c9d2018-12-19 02:59:2218#include "url/gurl.h"
Tsuyoshi Horo46f5fff2018-05-10 12:33:3519
20namespace network {
Tsuyoshi Horod5eb7612019-05-09 08:59:4621struct ResourceRequest;
Tsuyoshi Horo46f5fff2018-05-10 12:33:3522} // namespace network
23
Tsuyoshi Horocdbb4902018-04-12 06:09:1424namespace content {
Tsuyoshi Horo4801e762018-04-25 07:36:5725
Clark DuVallab63d142019-07-23 04:24:3626class BrowserContext;
Tsuyoshi Horo4801e762018-04-25 07:36:5727class SignedExchangeDevToolsProxy;
28
Tsuyoshi Horocdbb4902018-04-12 06:09:1429namespace signed_exchange_utils {
30
Kunihiko Sakamotod405c9d2018-12-19 02:59:2231// URLWithRawString holds a parsed URL along with its raw bytes.
32struct URLWithRawString {
33 GURL url;
34 std::string raw_string;
35 URLWithRawString() = default;
Md Hasibul Hasana963a9342024-04-03 10:15:1436 URLWithRawString(std::string_view url_string)
Peter Kastingb53b81912021-04-28 19:23:3037 : url(url_string), raw_string(url_string) {}
Kunihiko Sakamotod405c9d2018-12-19 02:59:2238};
39
Kunihiko Sakamoto6405d0af2021-11-18 00:46:3740// Records SignedExchange.LoadResult2 UMA histogram.
41void RecordLoadResultHistogram(SignedExchangeLoadResult result);
42
Tsuyoshi Horob40c7c32018-05-31 07:32:4543// Utility method to call SignedExchangeDevToolsProxy::ReportError() and
Tsuyoshi Horo6361cb02018-06-04 04:36:0244// TRACE_EVENT_INSTANT1 to report the error to both DevTools and about:tracing.
45// If |devtools_proxy| is nullptr, it just calls TRACE_EVENT_INSTANT1().
Arthur Sonzognic686e8f2024-01-11 08:36:3746void ReportErrorAndTraceEvent(SignedExchangeDevToolsProxy* devtools_proxy,
47 const std::string& error_message,
48 std::optional<SignedExchangeError::FieldIndexPair>
49 error_field = std::nullopt);
Tsuyoshi Horocdbb4902018-04-12 06:09:1450
Kunihiko Sakamotof586da62019-03-28 03:03:0451// Returns true when SignedHTTPExchange feature is enabled. This must be called
Clark DuValla1220f72019-08-02 19:00:5752// on the UI thread.
Clark DuVallab63d142019-07-23 04:24:3653CONTENT_EXPORT bool IsSignedExchangeHandlingEnabled(BrowserContext* context);
Tsuyoshi Horo46f5fff2018-05-10 12:33:3554
Tsuyoshi Horo4f5ce9012019-02-27 01:04:4555// Returns true when SignedExchangeReportingForDistributors feature is enabled.
56bool IsSignedExchangeReportingForDistributorsEnabled();
57
Tsuyoshi Horo46f5fff2018-05-10 12:33:3558// Returns true when the response should be handled as a signed exchange by
Kunihiko Sakamotof586da62019-03-28 03:03:0459// checking the url and the response headers. Note that the caller should also
60// check IsSignedExchangeHandlingEnabled() before really enabling the feature.
Tsuyoshi Horo46f5fff2018-05-10 12:33:3561bool ShouldHandleAsSignedHTTPExchange(
62 const GURL& request_url,
Lucas Furukawa Gadanid661c0d2019-12-02 19:58:1663 const network::mojom::URLResponseHead& head);
Tsuyoshi Horo46f5fff2018-05-10 12:33:3564
Kunihiko Sakamotob5c94d902018-09-04 04:09:0265// Extracts the signed exchange version [1] from |content_type|, and converts it
66// to SignedExchanveVersion. Returns nullopt if the mime type is not a variant
67// of application/signed-exchange. Returns SignedExchangeVersion::kUnknown if an
68// unsupported signed exchange version is found.
69// [1] https://wicg.github.io/webpackage/loading.html#signed-exchange-version
Arthur Sonzognic686e8f2024-01-11 08:36:3770CONTENT_EXPORT std::optional<SignedExchangeVersion> GetSignedExchangeVersion(
Matt Menke16ba4572024-10-04 04:24:1871 std::string_view content_type);
Kunihiko Sakamotob5c94d902018-09-04 04:09:0272
Tsuyoshi Horo06eb28f2019-02-21 13:52:2473// Returns the matching SignedExchangeLoadResult for the verifier's result.
74// There is a gap between the logic of SignedExchangeSignatureVerifier and the
75// spec of Loading Signed Exchanges [1]. This method is used to fill the gap
76// and send a correct signed exchange report.
77// [1] https://wicg.github.io/webpackage/loading.html
78SignedExchangeLoadResult GetLoadResultFromSignatureVerifierResult(
79 SignedExchangeSignatureVerifier::Result verify_result);
80
Tsuyoshi Horod5eb7612019-05-09 08:59:4681// Creates a RedirectInfo of synthesized redirect for signed exchange loading.
82net::RedirectInfo CreateRedirectInfo(
83 const GURL& new_url,
84 const network::ResourceRequest& outer_request,
Lucas Furukawa Gadani62fcfa92019-12-05 15:38:4185 const network::mojom::URLResponseHead& outer_response,
Tsuyoshi Horod5eb7612019-05-09 08:59:4686 bool is_fallback_redirect);
87
Lucas Furukawa Gadani62fcfa92019-12-05 15:38:4188// Creates a URLResponseHead of synthesized redirect for signed exchange
Tsuyoshi Horod5eb7612019-05-09 08:59:4689// loading.
Lucas Furukawa Gadani62fcfa92019-12-05 15:38:4190network::mojom::URLResponseHeadPtr CreateRedirectResponseHead(
91 const network::mojom::URLResponseHead& outer_response,
Tsuyoshi Horod5eb7612019-05-09 08:59:4692 bool is_fallback_redirect);
93
John Abd-El-Malekd96edf32019-07-29 22:04:5294// Creates a new request ID for browser initiated requests. Can be called on
95// any thread.
96int MakeRequestID();
97
Tsuyoshi Horoaf9059532019-08-29 15:27:0298// Returns the time to be used for verifying signed exchange. Can be overridden
99// using SetVerificationTimeForTesting().
100base::Time GetVerificationTime();
101
102// Override the time which is used for verifying signed exchange.
103CONTENT_EXPORT void SetVerificationTimeForTesting(
Arthur Sonzognic686e8f2024-01-11 08:36:37104 std::optional<base::Time> verification_time_for_testing);
Tsuyoshi Horoaf9059532019-08-29 15:27:02105
Kunihiko Sakamoto6405d0af2021-11-18 00:46:37106bool IsCookielessOnlyExchange(const net::HttpResponseHeaders& inner_headers);
107
Kunihiko Sakamotob5c94d902018-09-04 04:09:02108} // namespace signed_exchange_utils
Tsuyoshi Horocdbb4902018-04-12 06:09:14109} // namespace content
110
111#endif // CONTENT_BROWSER_WEB_PACKAGE_SIGNED_EXCHANGE_UTILS_H_