blob: 08d274051bedbaff22ae7494be0ac1fc7cbd9a68 [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ac039522010-06-15 16:39:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/net/chrome_network_delegate.h"
6
avi6846aef2015-12-26 01:09:387#include <stddef.h>
[email protected]eac11e12013-03-19 22:04:328#include <stdlib.h>
9
[email protected]d1208ba32012-11-08 11:10:3310#include <vector>
11
[email protected]d8e4f132012-09-06 04:28:0512#include "base/base_paths.h"
mkwst0513c9d2015-04-01 05:53:1513#include "base/command_line.h"
jochenb93dba3c2014-11-11 05:28:4114#include "base/debug/alias.h"
jochenb5680e62014-10-13 08:43:4115#include "base/debug/dump_without_crashing.h"
jochenb93dba3c2014-11-11 05:28:4116#include "base/debug/stack_trace.h"
[email protected]7a299a92012-10-24 23:54:5017#include "base/logging.h"
avi6846aef2015-12-26 01:09:3818#include "base/macros.h"
asvitkineaa060312016-09-01 22:44:1319#include "base/metrics/histogram_macros.h"
sclittle3c16159e2014-12-15 19:53:1720#include "base/metrics/sparse_histogram.h"
jochen0e3b3a62014-09-16 18:31:2321#include "base/metrics/user_metrics.h"
[email protected]d8e4f132012-09-06 04:28:0522#include "base/path_service.h"
[email protected]3ea1b182013-02-08 22:38:4123#include "base/strings/string_number_conversions.h"
ellyjonesd84033d2015-02-12 19:20:3424#include "base/strings/string_util.h"
[email protected]069c49162013-09-11 20:46:3525#include "base/time/time.h"
avi6846aef2015-12-26 01:09:3826#include "build/build_config.h"
[email protected]6baff0b52012-03-06 01:30:1827#include "chrome/browser/browser_process.h"
drogerb1716972015-06-30 09:04:0928#include "chrome/browser/content_settings/cookie_settings_factory.h"
[email protected]9c8ae8c2012-03-09 13:13:3529#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5830#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]4a2b6232014-06-19 08:44:1431#include "chrome/browser/net/chrome_extensions_network_delegate.h"
ttuttle05ae3f342015-07-13 17:38:3532#include "chrome/browser/net/request_source_bandwidth_histograms.h"
[email protected]6baff0b52012-03-06 01:30:1833#include "chrome/browser/profiles/profile_manager.h"
avi24d693f2016-08-06 18:03:5234#include "chrome/browser/task_manager/task_manager_interface.h"
sievers2f1e8112015-12-04 18:43:5635#include "chrome/common/features.h"
[email protected]0a8db0d2011-04-13 15:15:4036#include "chrome/common/pref_names.h"
drogerb1716972015-06-30 09:04:0937#include "components/content_settings/core/browser/cookie_settings.h"
sclittleae932be2015-10-08 20:53:5038#include "components/data_usage/core/data_use_aggregator.h"
[email protected]45de676a2014-03-18 23:52:0239#include "components/domain_reliability/monitor.h"
brettw03e61962016-03-15 06:27:0440#include "components/policy/core/browser/url_blacklist_manager.h"
brettwb1fc1b82016-02-02 00:19:0841#include "components/prefs/pref_member.h"
42#include "components/prefs/pref_service.h"
[email protected]c38831a12011-10-28 12:44:4943#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5644#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3345#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3546#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1547#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3448#include "content/public/common/process_type.h"
tyoshino430d2f42016-12-15 09:30:0049#include "content/public/common/resource_type.h"
brettw00899e62016-11-12 02:10:1750#include "extensions/features/features.h"
[email protected]82b42302011-04-20 16:28:1651#include "net/base/host_port_pair.h"
sclittle3c16159e2014-12-15 19:53:1752#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1453#include "net/base/net_errors.h"
[email protected]5b9bc352012-07-18 13:13:3454#include "net/cookies/canonical_cookie.h"
55#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4456#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1657#include "net/http/http_response_headers.h"
ellyjonesd84033d2015-02-12 19:20:3458#include "net/http/http_status_code.h"
eroman87c53d62015-04-02 06:51:0759#include "net/log/net_log.h"
mikecirone8b85c432016-09-08 19:11:0060#include "net/log/net_log_event_type.h"
mikecironef22f9812016-10-04 03:40:1961#include "net/log/net_log_with_source.h"
[email protected]d05ef99c2011-02-01 21:38:1662#include "net/url_request/url_request.h"
63
zpengdb4a58e2017-01-10 17:40:3264#if defined(OS_ANDROID)
[email protected]4a2b6232014-06-19 08:44:1465#include "chrome/browser/io_thread.h"
[email protected]4a2b6232014-06-19 08:44:1466#endif
67
[email protected]4c219e22012-05-05 19:41:0468#if defined(OS_CHROMEOS)
[email protected]49c4cf852013-09-27 19:28:2469#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3570#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0471#endif
72
brettw00899e62016-11-12 02:10:1773#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]84b7a552014-07-19 04:52:0674#include "extensions/common/constants.h"
75#endif
76
[email protected]631bb742011-11-02 11:29:3977using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1178using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2579using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3980
[email protected]d05ef99c2011-02-01 21:38:1681namespace {
82
satoruxd18e61a2017-06-08 06:38:4683bool g_access_to_all_files_enabled = false;
84
[email protected]9d8cfb682012-09-13 16:48:0485const char kDNTHeader[] = "DNT";
86
[email protected]d1208ba32012-11-08 11:10:3387// Gets called when the extensions finish work on the URL. If the extensions
88// did not do a redirect (so |new_url| is empty) then we enforce the
89// SafeSearch parameters. Otherwise we will get called again after the
90// redirect and we enforce SafeSearch then.
91void ForceGoogleSafeSearchCallbackWrapper(
92 const net::CompletionCallback& callback,
93 net::URLRequest* request,
94 GURL* new_url,
95 int rv) {
96 if (rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:2497 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:3398 callback.Run(rv);
99}
100
jochen652275372015-01-16 11:35:27101void ReportInvalidReferrerSendOnUI() {
jochen0e3b3a62014-09-16 18:31:23102 base::RecordAction(
103 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
jochen652275372015-01-16 11:35:27104}
105
106void ReportInvalidReferrerSend(const GURL& target_url,
107 const GURL& referrer_url) {
jochen4c874d22015-04-13 14:01:04108 LOG(ERROR) << "Cancelling request to " << target_url
109 << " with invalid referrer " << referrer_url;
jochen652275372015-01-16 11:35:27110 // Record information to help debug http://crbug.com/422871
111 if (!target_url.SchemeIsHTTPOrHTTPS())
112 return;
113 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48114 base::BindOnce(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41115 base::debug::DumpWithoutCrashing();
jochen4c874d22015-04-13 14:01:04116 NOTREACHED();
jochen0e3b3a62014-09-16 18:31:23117}
118
sclittle3c16159e2014-12-15 19:53:17119// Record network errors that HTTP requests complete with, including OK and
120// ABORTED.
maksim.sisov1b83bb72016-10-07 06:07:23121void RecordNetworkErrorHistograms(const net::URLRequest* request,
122 int net_error) {
sclittle3c16159e2014-12-15 19:53:17123 if (request->url().SchemeIs("http")) {
124 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
maksim.sisov1b83bb72016-10-07 06:07:23125 std::abs(net_error));
sclittle3c16159e2014-12-15 19:53:17126
rdsmith4de1fcf2016-08-30 05:35:21127 if (request->load_flags() & net::LOAD_MAIN_FRAME_DEPRECATED) {
sclittle3c16159e2014-12-15 19:53:17128 UMA_HISTOGRAM_SPARSE_SLOWLY(
maksim.sisov1b83bb72016-10-07 06:07:23129 "Net.HttpRequestCompletionErrorCodes.MainFrame", std::abs(net_error));
sclittle3c16159e2014-12-15 19:53:17130 }
131 }
132}
133
Ken Rockot314714c2017-11-05 23:36:24134bool IsAccessAllowedInternal(const base::FilePath& path,
135 const base::FilePath& profile_path) {
136#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
137 return true;
138#else
139
140 std::vector<base::FilePath> whitelist;
141#if defined(OS_CHROMEOS)
142 // Use a whitelist to only allow access to files residing in the list of
143 // directories below.
144 static const base::FilePath::CharType* const kLocalAccessWhiteList[] = {
145 "/home/chronos/user/Downloads",
146 "/home/chronos/user/log",
147 "/home/chronos/user/WebRTC Logs",
148 "/media",
149 "/opt/oem",
150 "/usr/share/chromeos-assets",
151 "/var/log",
152 };
153
154 base::FilePath temp_dir;
155 if (PathService::Get(base::DIR_TEMP, &temp_dir))
156 whitelist.push_back(temp_dir);
157
158 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
159 // the profile path ("/home/chronos/user' is a hard link to current primary
160 // logged in profile.) For the support of multi-profile sessions, we are
161 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
162 // access.
163 if (!profile_path.empty()) {
164 const base::FilePath downloads = profile_path.AppendASCII("Downloads");
165 whitelist.push_back(downloads);
166 const base::FilePath webrtc_logs = profile_path.AppendASCII("WebRTC Logs");
167 whitelist.push_back(webrtc_logs);
168 }
169#elif defined(OS_ANDROID)
170 // Access to files in external storage is allowed.
171 base::FilePath external_storage_path;
172 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
173 if (external_storage_path.IsParent(path))
174 return true;
175
176 // Whitelist of other allowed directories.
177 static const base::FilePath::CharType* const kLocalAccessWhiteList[] = {
178 "/sdcard", "/mnt/sdcard",
179 };
180#endif
181
182 for (const auto* whitelisted_path : kLocalAccessWhiteList)
183 whitelist.push_back(base::FilePath(whitelisted_path));
184
185 for (const auto& whitelisted_path : whitelist) {
186 // base::FilePath::operator== should probably handle trailing separators.
187 if (whitelisted_path == path.StripTrailingSeparators() ||
188 whitelisted_path.IsParent(path)) {
189 return true;
190 }
191 }
192
193 DVLOG(1) << "File access denied - " << path.value().c_str();
194 return false;
195#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
196}
197
[email protected]d05ef99c2011-02-01 21:38:16198} // namespace
[email protected]ac039522010-06-15 16:39:44199
[email protected]0651b812011-02-24 00:22:50200ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10201 extensions::EventRouterForwarder* event_router,
rajendrant14a5a852016-11-17 17:58:48202 BooleanPrefMember* enable_referrers)
ljusten98684cb72016-10-11 13:26:18203 : profile_(nullptr),
[email protected]6a5f77c32011-09-04 19:19:59204 enable_referrers_(enable_referrers),
ljusten98684cb72016-10-11 13:26:18205 enable_do_not_track_(nullptr),
206 force_google_safe_search_(nullptr),
207 force_youtube_restrict_(nullptr),
igorcov193d8ad32016-05-27 11:03:21208 allowed_domains_for_apps_(nullptr),
juliatuttle127604ea2016-12-19 19:13:04209 url_blacklist_manager_(NULL),
mkwst0513c9d2015-04-01 05:53:15210 experimental_web_platform_features_enabled_(
gayane0b46091c2016-04-07 21:01:05211 base::CommandLine::ForCurrentProcess()->HasSwitch(
212 switches::kEnableExperimentalWebPlatformFeatures)),
sclittleae932be2015-10-08 20:53:50213 data_use_aggregator_(nullptr),
214 is_data_usage_off_the_record_(true) {
[email protected]0a8db0d2011-04-13 15:15:40215 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14216 extensions_delegate_.reset(
217 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50218}
219
[email protected]ac039522010-06-15 16:39:44220ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
221
[email protected]a09159a2012-11-29 12:51:48222void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20223 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14224 extensions_delegate_->set_extension_info_map(extension_info_map);
225}
226
227void ChromeNetworkDelegate::set_profile(void* profile) {
228 profile_ = profile;
229 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48230}
231
232void ChromeNetworkDelegate::set_cookie_settings(
drogerb1716972015-06-30 09:04:09233 content_settings::CookieSettings* cookie_settings) {
[email protected]a09159a2012-11-29 12:51:48234 cookie_settings_ = cookie_settings;
235}
236
sclittleae932be2015-10-08 20:53:50237void ChromeNetworkDelegate::set_data_use_aggregator(
238 data_usage::DataUseAggregator* data_use_aggregator,
239 bool is_data_usage_off_the_record) {
240 data_use_aggregator_ = data_use_aggregator;
241 is_data_usage_off_the_record_ = is_data_usage_off_the_record;
242}
243
[email protected]c4a7df82012-08-09 22:48:46244// static
[email protected]9d8cfb682012-09-13 16:48:04245void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40246 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04247 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33248 BooleanPrefMember* force_google_safe_search,
ljusten98684cb72016-10-11 13:26:18249 IntegerPrefMember* force_youtube_restrict,
igorcov193d8ad32016-05-27 11:03:21250 StringPrefMember* allowed_domains_for_apps,
[email protected]0a8db0d2011-04-13 15:15:40251 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02252 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02253 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43254 enable_referrers->MoveToThread(
thestig529ad8a2016-07-08 20:30:12255 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04256 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02257 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43258 enable_do_not_track->MoveToThread(
thestig529ad8a2016-07-08 20:30:12259 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04260 }
[email protected]d1208ba32012-11-08 11:10:33261 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18262 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33263 force_google_safe_search->MoveToThread(
thestig529ad8a2016-07-08 20:30:12264 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]d1208ba32012-11-08 11:10:33265 }
ljusten98684cb72016-10-11 13:26:18266 if (force_youtube_restrict) {
267 force_youtube_restrict->Init(prefs::kForceYouTubeRestrict, pref_service);
268 force_youtube_restrict->MoveToThread(
thestig529ad8a2016-07-08 20:30:12269 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
treib4e26f6652014-12-01 14:34:18270 }
igorcov193d8ad32016-05-27 11:03:21271 if (allowed_domains_for_apps) {
272 allowed_domains_for_apps->Init(prefs::kAllowedDomainsForApps, pref_service);
273 allowed_domains_for_apps->MoveToThread(
thestig529ad8a2016-07-08 20:30:12274 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
igorcov193d8ad32016-05-27 11:03:21275 }
[email protected]0a8db0d2011-04-13 15:15:40276}
277
[email protected]4875ba12011-03-30 22:31:51278int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12279 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47280 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12281 GURL* new_url) {
[email protected]6a5f77c32011-09-04 19:19:59282 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
283 // blocked. However, an extension might redirect the request to another URL,
284 // which is not blocked.
mmenkef1aa9072015-05-18 16:21:04285
286 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
[email protected]cb85f8e52014-04-08 11:44:40287 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
mmenkef1aa9072015-05-18 16:21:04288 if (info && content::IsResourceTypeFrame(info->GetResourceType()) &&
289 url_blacklist_manager_ &&
290 url_blacklist_manager_->ShouldBlockRequestForFrame(
291 request->url(), &error)) {
[email protected]6a5f77c32011-09-04 19:19:59292 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59293 request->net_log().AddEvent(
mikecirone8b85c432016-09-08 19:11:00294 net::NetLogEventType::CHROME_POLICY_ABORTED_REQUEST,
[email protected]2fa08912012-06-14 20:56:26295 net::NetLog::StringCallback("url",
296 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40297 return error;
[email protected]6a5f77c32011-09-04 19:19:59298 }
299
[email protected]4a2b6232014-06-19 08:44:14300 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18301
[email protected]0a8db0d2011-04-13 15:15:40302 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13303 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04304 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
305 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33306
treib4e26f6652014-12-01 14:34:18307 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18308 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33309
310 net::CompletionCallback wrapped_callback = callback;
311 if (force_safe_search) {
312 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
313 callback,
314 base::Unretained(request),
315 base::Unretained(new_url));
316 }
317
[email protected]4a2b6232014-06-19 08:44:14318 int rv = extensions_delegate_->OnBeforeURLRequest(
319 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33320
[email protected]d1208ba32012-11-08 11:10:33321 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24322 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33323
igorcov193d8ad32016-05-27 11:03:21324 if (allowed_domains_for_apps_ &&
325 !allowed_domains_for_apps_->GetValue().empty() &&
326 request->url().DomainIs("google.com")) {
327 request->SetExtraRequestHeaderByName("X-GoogApps-Allowed-Domains",
328 allowed_domains_for_apps_->GetValue(),
329 true);
330 }
331
[email protected]d1208ba32012-11-08 11:10:33332 return rv;
[email protected]d05ef99c2011-02-01 21:38:16333}
334
ryansturm2343cb62016-06-15 01:09:00335int ChromeNetworkDelegate::OnBeforeStartTransaction(
[email protected]636eccd2011-06-28 12:28:01336 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47337 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12338 net::HttpRequestHeaders* headers) {
ljusten98684cb72016-10-11 13:26:18339 if (force_youtube_restrict_) {
340 int value = force_youtube_restrict_->GetValue();
341 static_assert(safe_search_util::YOUTUBE_RESTRICT_OFF == 0,
342 "OFF must be first");
343 if (value > safe_search_util::YOUTUBE_RESTRICT_OFF &&
344 value < safe_search_util::YOUTUBE_RESTRICT_COUNT) {
345 safe_search_util::ForceYouTubeRestrict(request, headers,
346 static_cast<safe_search_util::YouTubeRestrictMode>(value));
347 }
348 }
[email protected]184799df2014-07-22 16:03:24349
ryansturm2343cb62016-06-15 01:09:00350 return extensions_delegate_->OnBeforeStartTransaction(request, callback,
351 headers);
[email protected]ac039522010-06-15 16:39:44352}
[email protected]8202d0c2011-02-23 08:31:14353
ryansturm2343cb62016-06-15 01:09:00354void ChromeNetworkDelegate::OnStartTransaction(
[email protected]5796dc942011-07-14 19:26:10355 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15356 const net::HttpRequestHeaders& headers) {
ryansturm2343cb62016-06-15 01:09:00357 extensions_delegate_->OnStartTransaction(request, headers);
[email protected]82b42302011-04-20 16:28:16358}
359
[email protected]ea8141e2011-10-05 13:12:51360int ChromeNetworkDelegate::OnHeadersReceived(
361 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47362 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32363 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16364 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
365 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14366 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16367 request,
368 callback,
369 original_response_headers,
370 override_response_headers,
371 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51372}
373
[email protected]31b2e5f2011-04-20 16:58:32374void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
375 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02376 if (domain_reliability_monitor_)
377 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14378 extensions_delegate_->OnBeforeRedirect(request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32379}
380
maksim.sisov1b83bb72016-10-07 06:07:23381void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request,
382 int net_error) {
383 extensions_delegate_->OnResponseStarted(request, net_error);
[email protected]8202d0c2011-02-23 08:31:14384}
385
sclittlea133de02015-11-10 23:54:21386void ChromeNetworkDelegate::OnNetworkBytesReceived(net::URLRequest* request,
387 int64_t bytes_received) {
brettwee486bb2016-12-08 17:56:34388#if !defined(OS_ANDROID)
sclittlece72c482015-08-24 20:20:59389 // Note: Currently, OnNetworkBytesReceived is only implemented for HTTP jobs,
390 // not FTP or other types, so those kinds of bytes will not be reported here.
avi24d693f2016-08-06 18:03:52391 task_manager::TaskManagerInterface::OnRawBytesRead(*request, bytes_received);
brettwee486bb2016-12-08 17:56:34392#endif // !defined(OS_ANDROID)
sclittleae932be2015-10-08 20:53:50393
sclittlea133de02015-11-10 23:54:21394 ReportDataUsageStats(request, 0 /* tx_bytes */, bytes_received);
sclittleae932be2015-10-08 20:53:50395}
396
sclittlea133de02015-11-10 23:54:21397void ChromeNetworkDelegate::OnNetworkBytesSent(net::URLRequest* request,
sclittleae932be2015-10-08 20:53:50398 int64_t bytes_sent) {
cburne9d2f3612017-06-20 22:15:03399#if !defined(OS_ANDROID)
400 // Note: Currently, OnNetworkBytesSent is only implemented for HTTP jobs,
401 // not FTP or other types, so those kinds of bytes will not be reported here.
402 task_manager::TaskManagerInterface::OnRawBytesSent(*request, bytes_sent);
403#endif // !defined(OS_ANDROID)
404
sclittlea133de02015-11-10 23:54:21405 ReportDataUsageStats(request, bytes_sent, 0 /* rx_bytes */);
[email protected]8523ba52011-05-22 19:00:58406}
407
[email protected]9045b8822012-01-13 20:35:35408void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
maksim.sisov1b83bb72016-10-07 06:07:23409 bool started,
410 int net_error) {
411 DCHECK_NE(net::ERR_IO_PENDING, net_error);
sclittle3c16159e2014-12-15 19:53:17412
maksim.sisov1b83bb72016-10-07 06:07:23413 // TODO(amohammadkhan): Verify that there is no double recording in data use
414 // of redirected requests.
maksim.sisov1b83bb72016-10-07 06:07:23415 RecordNetworkErrorHistograms(request, net_error);
416
maksim.sisov1b83bb72016-10-07 06:07:23417 extensions_delegate_->OnCompleted(request, started, net_error);
[email protected]45de676a2014-03-18 23:52:02418 if (domain_reliability_monitor_)
419 domain_reliability_monitor_->OnCompleted(request, started);
ttuttle05ae3f342015-07-13 17:38:35420 RecordRequestSourceBandwidth(request, started);
maksim.sisov1b83bb72016-10-07 06:07:23421 extensions_delegate_->ForwardProxyErrors(request, net_error);
[email protected]4a2b6232014-06-19 08:44:14422 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14423}
[email protected]4b50cb52011-03-10 00:29:37424
[email protected]4875ba12011-03-30 22:31:51425void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14426 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51427}
428
[email protected]82a37672011-05-03 12:02:41429void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09430 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14431 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41432}
[email protected]7efc582d2011-08-03 20:46:35433
[email protected]c2911d72011-10-03 22:16:36434net::NetworkDelegate::AuthRequiredResponse
435ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35436 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36437 const net::AuthChallengeInfo& auth_info,
438 const AuthCallback& callback,
439 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14440 return extensions_delegate_->OnAuthRequired(
441 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35442}
[email protected]9c8ae8c2012-03-09 13:13:35443
[email protected]4c219e22012-05-05 19:41:04444bool ChromeNetworkDelegate::OnCanGetCookies(
445 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35446 const net::CookieList& cookie_list) {
ljusten98684cb72016-10-11 13:26:18447 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02448 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35449 return true;
450
falken71691402017-01-27 03:38:00451 bool allow = cookie_settings_->IsCookieAccessAllowed(
Mike Westb85da8ed2017-08-10 14:16:46452 request.url(), request.site_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35453
jam092d3be2016-09-27 15:56:36454 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
455 if (info) {
[email protected]9c8ae8c2012-03-09 13:13:35456 BrowserThread::PostTask(
457 BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48458 base::BindOnce(&TabSpecificContentSettings::CookiesRead,
459 info->GetWebContentsGetterForRequest(), request.url(),
Mike Westb85da8ed2017-08-10 14:16:46460 request.site_for_cookies(), cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35461 }
462
463 return allow;
464}
465
[email protected]4c219e22012-05-05 19:41:04466bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
467 const std::string& cookie_line,
468 net::CookieOptions* options) {
ljusten98684cb72016-10-11 13:26:18469 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02470 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35471 return true;
472
falken71691402017-01-27 03:38:00473 bool allow = cookie_settings_->IsCookieAccessAllowed(
Mike Westb85da8ed2017-08-10 14:16:46474 request.url(), request.site_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35475
jam092d3be2016-09-27 15:56:36476 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
477 if (info) {
[email protected]9c8ae8c2012-03-09 13:13:35478 BrowserThread::PostTask(
479 BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48480 base::BindOnce(&TabSpecificContentSettings::CookieChanged,
481 info->GetWebContentsGetterForRequest(), request.url(),
Mike Westb85da8ed2017-08-10 14:16:46482 request.site_for_cookies(), cookie_line, *options,
tzik53a7b9022017-04-24 11:14:48483 !allow));
[email protected]9c8ae8c2012-03-09 13:13:35484 }
485
486 return allow;
487}
[email protected]4c219e22012-05-05 19:41:04488
satoruxddac0442017-05-29 06:06:18489bool ChromeNetworkDelegate::OnCanAccessFile(
490 const net::URLRequest& request,
491 const base::FilePath& original_path,
492 const base::FilePath& absolute_path) const {
satoruxd18e61a2017-06-08 06:38:46493 if (g_access_to_all_files_enabled)
[email protected]d8e4f132012-09-06 04:28:05494 return true;
Ken Rockot314714c2017-11-05 23:36:24495 return IsAccessAllowed(original_path, absolute_path, profile_path_);
satorux7c5360582017-01-27 07:24:29496}
497
498// static
499bool ChromeNetworkDelegate::IsAccessAllowed(
500 const base::FilePath& path,
501 const base::FilePath& profile_path) {
Ken Rockot314714c2017-11-05 23:36:24502 return IsAccessAllowedInternal(path, profile_path);
503}
504
505// static
506bool ChromeNetworkDelegate::IsAccessAllowed(
507 const base::FilePath& path,
508 const base::FilePath& absolute_path,
509 const base::FilePath& profile_path) {
510#if defined(OS_ANDROID)
511 // Android's whitelist relies on symbolic links (ex. /sdcard is whitelisted
512 // and commonly a symbolic link), thus do not check absolute paths.
513 return IsAccessAllowedInternal(path, profile_path);
satorux7c5360582017-01-27 07:24:29514#else
Ken Rockot314714c2017-11-05 23:36:24515 return (IsAccessAllowedInternal(path, profile_path) &&
516 IsAccessAllowedInternal(absolute_path, profile_path));
[email protected]d8e4f132012-09-06 04:28:05517#endif
[email protected]4c219e22012-05-05 19:41:04518}
[email protected]a1d4ab072012-06-07 13:21:15519
satoruxd18e61a2017-06-08 06:38:46520// static
521void ChromeNetworkDelegate::EnableAccessToAllFilesForTesting(bool enabled) {
522 g_access_to_all_files_enabled = enabled;
523}
524
[email protected]e6d017652013-05-17 18:01:40525bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
526 const GURL& url,
Mike Westb85da8ed2017-08-10 14:16:46527 const GURL& site_for_cookies) const {
ljusten98684cb72016-10-11 13:26:18528 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02529 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40530 return false;
531
Mike Westb85da8ed2017-08-10 14:16:46532 return !cookie_settings_->IsCookieAccessAllowed(url, site_for_cookies);
[email protected]e6d017652013-05-17 18:01:40533}
534
estark7625d812015-10-12 20:10:41535bool ChromeNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
mkwst0513c9d2015-04-01 05:53:15536 return experimental_web_platform_features_enabled_;
537}
538
jochen0e3b3a62014-09-16 18:31:23539bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
540 const net::URLRequest& request,
541 const GURL& target_url,
542 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27543 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23544 return true;
545}
sclittlea133de02015-11-10 23:54:21546
juliatuttlefcf47202017-05-23 15:53:02547bool ChromeNetworkDelegate::OnCanQueueReportingReport(
548 const url::Origin& origin) const {
549 if (!cookie_settings_)
550 return true;
551
552 return cookie_settings_->IsCookieAccessAllowed(origin.GetURL(),
553 origin.GetURL());
554}
555
556bool ChromeNetworkDelegate::OnCanSendReportingReport(
557 const url::Origin& origin) const {
558 if (!cookie_settings_)
559 return true;
560
561 return cookie_settings_->IsCookieAccessAllowed(origin.GetURL(),
562 origin.GetURL());
563}
564
565bool ChromeNetworkDelegate::OnCanSetReportingClient(
566 const url::Origin& origin,
567 const GURL& endpoint) const {
568 if (!cookie_settings_)
569 return true;
570
571 return cookie_settings_->IsCookieAccessAllowed(endpoint, origin.GetURL());
572}
573
574bool ChromeNetworkDelegate::OnCanUseReportingClient(
575 const url::Origin& origin,
576 const GURL& endpoint) const {
577 if (!cookie_settings_)
578 return true;
579
580 return cookie_settings_->IsCookieAccessAllowed(endpoint, origin.GetURL());
581}
582
sclittlea133de02015-11-10 23:54:21583void ChromeNetworkDelegate::ReportDataUsageStats(net::URLRequest* request,
584 int64_t tx_bytes,
585 int64_t rx_bytes) {
586 if (!data_use_aggregator_)
587 return;
588
589 if (is_data_usage_off_the_record_) {
590 data_use_aggregator_->ReportOffTheRecordDataUse(tx_bytes, rx_bytes);
591 return;
592 }
593
594 data_use_aggregator_->ReportDataUse(request, tx_bytes, rx_bytes);
595}