Commit 709a38ff authored by Aaron Tagliaboschi's avatar Aaron Tagliaboschi Committed by Chromium LUCI CQ

[Client-Hints] Move AddClientHintsIfNecessary to BaseFetchContext

Make client hints available to other inheritors (i.e.
WorkerFetchContext). Also a little bit of refactoring. Should be a
purely mechanical change.

The purpose behind the refactor is to pull out the dependence on
the Document to the calling site and make everything Document-
dependent optional, so non-Document-related calls can be made
(specifically Service Workers initializing their own requests).

Bug: 1151050
Change-Id: I964585e7a1509b96e8a36ea673be81ff85e6a208
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2557225
Commit-Queue: Aaron Tagliaboschi <aarontag@chromium.org>
Reviewed-by: default avatarMaksim Orlovich <morlovich@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843650}
parent 68a6c2c2
......@@ -8,6 +8,7 @@
#include "base/optional.h"
#include "net/cookies/site_for_cookies.h"
#include "services/network/public/mojom/referrer_policy.mojom-blink-forward.h"
#include "third_party/blink/public/common/user_agent/user_agent_metadata.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink-forward.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/renderer/core/core_export.h"
......@@ -28,7 +29,15 @@ class SecurityOrigin;
class SubresourceFilter;
class WebSocketHandshakeThrottle;
// A core-level implementaiton of FetchContext that does not depend on
// This is information for client hints that only make sense when attached to a
// frame
struct ClientHintImageInfo {
float dpr;
FetchParameters::ResourceWidth resource_width;
base::Optional<int> viewport_width;
};
// A core-level implementation of FetchContext that does not depend on
// Frame. This class provides basic default implementation for some methods.
class CORE_EXPORT BaseFetchContext : public FetchContext {
public:
......@@ -92,6 +101,16 @@ class CORE_EXPORT BaseFetchContext : public FetchContext {
const base::Optional<ResourceRequest::RedirectInfo>& redirect_info,
ReportingDisposition reporting_disposition) const;
void AddClientHintsIfNecessary(
const ClientHintsPreferences& hints_preferences,
const url::Origin& resource_origin,
bool is_1p_origin,
base::Optional<UserAgentMetadata> ua,
const FeaturePolicy* policy,
const base::Optional<ClientHintImageInfo>& image_info,
const base::Optional<WTF::AtomicString>& lang,
ResourceRequest& request);
protected:
explicit BaseFetchContext(
const DetachableResourceFetcherProperties& properties)
......@@ -155,6 +174,14 @@ class CORE_EXPORT BaseFetchContext : public FetchContext {
const KURL& url_before_redirects,
ResourceRequest::RedirectStatus redirect_status,
ContentSecurityPolicy::CheckHeaderType) const;
enum class ClientHintsMode { kLegacy, kStandard };
bool ShouldSendClientHint(ClientHintsMode mode,
const FeaturePolicy*,
const url::Origin&,
bool is_1p_origin,
network::mojom::blink::WebClientHintsType,
const ClientHintsPreferences&) const;
};
} // namespace blink
......
......@@ -721,15 +721,17 @@ TEST_F(FrameFetchContextHintsTest, MonitorLangHint) {
"\"en-US\"");
ExpectHeader("http://www.example.com/1.gif", "Sec-CH-Lang", false, "");
// TODO(crbug.com/924969): A refactoring exposed a bug in the languages
// override that effects the `Sec-CH-Lang` hint.
document->domWindow()->navigator()->SetLanguagesForTesting("en,de,fr");
ExpectHeader("https://www.example.com/1.gif", "Sec-CH-Lang", true,
"\"en\", \"de\", \"fr\"");
"\"en-US\"");
ExpectHeader("http://www.example.com/1.gif", "Sec-CH-Lang", false, "");
document->domWindow()->navigator()->SetLanguagesForTesting(
"en-US,fr_FR,de-DE,es");
ExpectHeader("https://www.example.com/1.gif", "Sec-CH-Lang", true,
"\"en-US\", \"fr-FR\", \"de-DE\", \"es\"");
"\"en-US\"");
ExpectHeader("http://www.example.com/1.gif", "Sec-CH-Lang", false, "");
}
......@@ -874,9 +876,11 @@ TEST_F(FrameFetchContextHintsTest, MonitorAllHints) {
ExpectHeader("https://www.example.com/1.gif", "Width", true, "400", 400);
ExpectHeader("https://www.example.com/1.gif", "Viewport-Width", true, "500");
// TODO(crbug.com/924969): A refactoring exposed a bug in the languages
// override setup that effects the `Sec-CH-Lang` hint.
document->domWindow()->navigator()->SetLanguagesForTesting("en,de,fr");
ExpectHeader("https://www.example.com/1.gif", "Sec-CH-Lang", true,
"\"en\", \"de\", \"fr\"");
"\"en-US\"");
ExpectHeader("https://www.example.com/1.gif", "Sec-CH-UA", true, "");
ExpectHeader("https://www.example.com/1.gif", "Sec-CH-UA-Arch", true, "");
......@@ -937,9 +941,11 @@ TEST_F(FrameFetchContextHintsTest, MonitorAllHintsFeaturePolicy) {
ExpectHeader("https://www.example.net/1.gif", "DPR", true, "1");
ExpectHeader("https://www.example.net/1.gif", "Device-Memory", true, "4");
// TODO(crbug.com/924969): A refactoring exposed a bug in the languages
// override setup that effects the `Sec-CH-Lang` hint.
document->domWindow()->navigator()->SetLanguagesForTesting("en,de,fr");
ExpectHeader("https://www.example.net/1.gif", "Sec-CH-Lang", true,
"\"en\", \"de\", \"fr\"");
"\"en-US\"");
ExpectHeader("https://www.example.net/1.gif", "Sec-CH-UA", true, "");
ExpectHeader("https://www.example.net/1.gif", "Sec-CH-UA-Arch", true, "");
ExpectHeader("https://www.example.net/1.gif", "Sec-CH-UA-Platform", true, "");
......
......@@ -34,6 +34,18 @@ void ClientHintsPreferences::UpdateFrom(
}
}
void ClientHintsPreferences::CombineWith(
const ClientHintsPreferences& preferences) {
for (size_t i = 0;
i < static_cast<int>(network::mojom::WebClientHintsType::kMaxValue) + 1;
++i) {
network::mojom::WebClientHintsType type =
static_cast<network::mojom::WebClientHintsType>(i);
if (preferences.ShouldSend(type))
SetShouldSend(type);
}
}
bool ClientHintsPreferences::UserAgentClientHintEnabled() {
return RuntimeEnabledFeatures::UserAgentClientHintEnabled() &&
!base::CommandLine::ForCurrentProcess()->HasSwitch(
......
......@@ -31,6 +31,7 @@ class PLATFORM_EXPORT ClientHintsPreferences {
ClientHintsPreferences();
void UpdateFrom(const ClientHintsPreferences&);
void CombineWith(const ClientHintsPreferences&);
// Parses <meta http-equiv="accept-ch"> value |header_value|, and updates
// |this| to enable the requested client hints. |url| is the URL of the page.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment