forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_url_loader_throttle.cc
165 lines (145 loc) · 5.73 KB
/
google_url_loader_throttle.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/google_url_loader_throttle.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "build/build_config.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/net/safe_search_util.h"
#include "components/google/core/common/google_util.h"
#include "net/base/url_util.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/common/extension_urls.h"
#endif
namespace {
#if defined(OS_ANDROID)
const char kCCTClientDataHeader[] = "X-CCT-Client-Data";
#endif
} // namespace
// static
void GoogleURLLoaderThrottle::UpdateCorsExemptHeader(
network::mojom::NetworkContextParams* params) {
params->cors_exempt_header_list.push_back(
safe_search_util::kGoogleAppsAllowedDomains);
#if defined(OS_ANDROID)
params->cors_exempt_header_list.push_back(kCCTClientDataHeader);
#endif
}
GoogleURLLoaderThrottle::GoogleURLLoaderThrottle(
#if defined(OS_ANDROID)
const std::string& client_data_header,
bool night_mode_enabled,
#endif
chrome::mojom::DynamicParams dynamic_params)
:
#if defined(OS_ANDROID)
client_data_header_(client_data_header),
night_mode_enabled_(night_mode_enabled),
#endif
dynamic_params_(std::move(dynamic_params)) {
}
GoogleURLLoaderThrottle::~GoogleURLLoaderThrottle() = default;
void GoogleURLLoaderThrottle::DetachFromCurrentSequence() {}
void GoogleURLLoaderThrottle::WillStartRequest(
network::ResourceRequest* request,
bool* defer) {
if (dynamic_params_.force_safe_search) {
GURL new_url;
safe_search_util::ForceGoogleSafeSearch(request->url, &new_url);
if (!new_url.is_empty())
request->url = new_url;
}
static_assert(safe_search_util::YOUTUBE_RESTRICT_OFF == 0,
"OFF must be first");
if (dynamic_params_.youtube_restrict >
safe_search_util::YOUTUBE_RESTRICT_OFF &&
dynamic_params_.youtube_restrict <
safe_search_util::YOUTUBE_RESTRICT_COUNT) {
safe_search_util::ForceYouTubeRestrict(
request->url, &request->headers,
static_cast<safe_search_util::YouTubeRestrictMode>(
dynamic_params_.youtube_restrict));
}
if (!dynamic_params_.allowed_domains_for_apps.empty() &&
request->url.DomainIs("google.com")) {
request->cors_exempt_headers.SetHeader(
safe_search_util::kGoogleAppsAllowedDomains,
dynamic_params_.allowed_domains_for_apps);
}
#if defined(OS_ANDROID)
if (!client_data_header_.empty() &&
google_util::IsGoogleAssociatedDomainUrl(request->url)) {
request->cors_exempt_headers.SetHeader(kCCTClientDataHeader,
client_data_header_);
}
bool is_google_homepage_or_search =
google_util::IsGoogleHomePageUrl(request->url) ||
google_util::IsGoogleSearchUrl(request->url);
if (is_google_homepage_or_search) {
// TODO (crbug.com/1081510): Remove this experimental code once a final
// solution is agreed upon.
if (base::FeatureList::IsEnabled(features::kAndroidDarkSearch)) {
request->url = net::AppendOrReplaceQueryParameter(
request->url, "cs", night_mode_enabled_ ? "1" : "0");
}
base::UmaHistogramBoolean("Android.DarkTheme.DarkSearchRequested",
night_mode_enabled_);
}
#endif
}
void GoogleURLLoaderThrottle::WillRedirectRequest(
net::RedirectInfo* redirect_info,
const network::mojom::URLResponseHead& response_head,
bool* /* defer */,
std::vector<std::string>* to_be_removed_headers,
net::HttpRequestHeaders* modified_headers,
net::HttpRequestHeaders* modified_cors_exempt_headers) {
// URLLoaderThrottles can only change the redirect URL when the network
// service is enabled. The non-network service path handles this in
// ChromeNetworkDelegate.
if (dynamic_params_.force_safe_search) {
safe_search_util::ForceGoogleSafeSearch(redirect_info->new_url,
&redirect_info->new_url);
}
if (dynamic_params_.youtube_restrict >
safe_search_util::YOUTUBE_RESTRICT_OFF &&
dynamic_params_.youtube_restrict <
safe_search_util::YOUTUBE_RESTRICT_COUNT) {
safe_search_util::ForceYouTubeRestrict(
redirect_info->new_url, modified_headers,
static_cast<safe_search_util::YouTubeRestrictMode>(
dynamic_params_.youtube_restrict));
}
if (!dynamic_params_.allowed_domains_for_apps.empty() &&
redirect_info->new_url.DomainIs("google.com")) {
modified_cors_exempt_headers->SetHeader(
safe_search_util::kGoogleAppsAllowedDomains,
dynamic_params_.allowed_domains_for_apps);
}
#if defined(OS_ANDROID)
if (!client_data_header_.empty() &&
!google_util::IsGoogleAssociatedDomainUrl(redirect_info->new_url)) {
to_be_removed_headers->push_back(kCCTClientDataHeader);
}
#endif
}
#if BUILDFLAG(ENABLE_EXTENSIONS)
void GoogleURLLoaderThrottle::WillProcessResponse(
const GURL& response_url,
network::mojom::URLResponseHead* response_head,
bool* defer) {
// Built-in additional protection for the chrome web store origin.
GURL webstore_url(extension_urls::GetWebstoreLaunchURL());
if (response_url.SchemeIsHTTPOrHTTPS() &&
response_url.DomainIs(webstore_url.host_piece())) {
if (response_head && response_head->headers &&
!response_head->headers->HasHeaderValue("x-frame-options", "deny") &&
!response_head->headers->HasHeaderValue("x-frame-options",
"sameorigin")) {
response_head->headers->AddHeader("x-frame-options", "sameorigin");
}
}
}
#endif