blob: 7500b1f89e8c7e082982847c4c310fbc3a6b4dc2 [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"
Ilya Sherman982457e62017-12-13 02:19:3619#include "base/metrics/histogram_functions.h"
jochen0e3b3a62014-09-16 18:31:2320#include "base/metrics/user_metrics.h"
[email protected]d8e4f132012-09-06 04:28:0521#include "base/path_service.h"
[email protected]3ea1b182013-02-08 22:38:4122#include "base/strings/string_number_conversions.h"
ellyjonesd84033d2015-02-12 19:20:3423#include "base/strings/string_util.h"
[email protected]069c49162013-09-11 20:46:3524#include "base/time/time.h"
avi6846aef2015-12-26 01:09:3825#include "build/build_config.h"
[email protected]6baff0b52012-03-06 01:30:1826#include "chrome/browser/browser_process.h"
drogerb1716972015-06-30 09:04:0927#include "chrome/browser/content_settings/cookie_settings_factory.h"
[email protected]9c8ae8c2012-03-09 13:13:3528#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5829#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]4a2b6232014-06-19 08:44:1430#include "chrome/browser/net/chrome_extensions_network_delegate.h"
[email protected]6baff0b52012-03-06 01:30:1831#include "chrome/browser/profiles/profile_manager.h"
avi24d693f2016-08-06 18:03:5232#include "chrome/browser/task_manager/task_manager_interface.h"
Scott Violet6200d332018-02-23 21:29:2333#include "chrome/common/buildflags.h"
[email protected]0a8db0d2011-04-13 15:15:4034#include "chrome/common/pref_names.h"
drogerb1716972015-06-30 09:04:0935#include "components/content_settings/core/browser/cookie_settings.h"
sclittleae932be2015-10-08 20:53:5036#include "components/data_usage/core/data_use_aggregator.h"
[email protected]45de676a2014-03-18 23:52:0237#include "components/domain_reliability/monitor.h"
brettwb1fc1b82016-02-02 00:19:0838#include "components/prefs/pref_member.h"
39#include "components/prefs/pref_service.h"
Dominic Battre5e8a1cd2018-01-02 16:16:5840#include "components/variations/net/variations_http_headers.h"
[email protected]c38831a12011-10-28 12:44:4941#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5642#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3343#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3544#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1545#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3446#include "content/public/common/process_type.h"
tyoshino430d2f42016-12-15 09:30:0047#include "content/public/common/resource_type.h"
brettw00899e62016-11-12 02:10:1748#include "extensions/features/features.h"
[email protected]82b42302011-04-20 16:28:1649#include "net/base/host_port_pair.h"
sclittle3c16159e2014-12-15 19:53:1750#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1451#include "net/base/net_errors.h"
[email protected]5b9bc352012-07-18 13:13:3452#include "net/cookies/canonical_cookie.h"
53#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4454#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1655#include "net/http/http_response_headers.h"
ellyjonesd84033d2015-02-12 19:20:3456#include "net/http/http_status_code.h"
eroman87c53d62015-04-02 06:51:0757#include "net/log/net_log.h"
mikecirone8b85c432016-09-08 19:11:0058#include "net/log/net_log_event_type.h"
mikecironef22f9812016-10-04 03:40:1959#include "net/log/net_log_with_source.h"
[email protected]d05ef99c2011-02-01 21:38:1660#include "net/url_request/url_request.h"
61
zpengdb4a58e2017-01-10 17:40:3262#if defined(OS_ANDROID)
[email protected]4a2b6232014-06-19 08:44:1463#include "chrome/browser/io_thread.h"
[email protected]4a2b6232014-06-19 08:44:1464#endif
65
[email protected]4c219e22012-05-05 19:41:0466#if defined(OS_CHROMEOS)
[email protected]49c4cf852013-09-27 19:28:2467#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3568#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0469#endif
70
brettw00899e62016-11-12 02:10:1771#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]84b7a552014-07-19 04:52:0672#include "extensions/common/constants.h"
73#endif
74
[email protected]631bb742011-11-02 11:29:3975using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1176using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2577using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3978
[email protected]d05ef99c2011-02-01 21:38:1679namespace {
80
satoruxd18e61a2017-06-08 06:38:4681bool g_access_to_all_files_enabled = false;
82
[email protected]9d8cfb682012-09-13 16:48:0483const char kDNTHeader[] = "DNT";
84
[email protected]d1208ba32012-11-08 11:10:3385// Gets called when the extensions finish work on the URL. If the extensions
86// did not do a redirect (so |new_url| is empty) then we enforce the
87// SafeSearch parameters. Otherwise we will get called again after the
88// redirect and we enforce SafeSearch then.
89void ForceGoogleSafeSearchCallbackWrapper(
90 const net::CompletionCallback& callback,
91 net::URLRequest* request,
92 GURL* new_url,
93 int rv) {
94 if (rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:2495 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:3396 callback.Run(rv);
97}
98
jochen652275372015-01-16 11:35:2799void ReportInvalidReferrerSendOnUI() {
jochen0e3b3a62014-09-16 18:31:23100 base::RecordAction(
101 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
jochen652275372015-01-16 11:35:27102}
103
104void ReportInvalidReferrerSend(const GURL& target_url,
105 const GURL& referrer_url) {
jochen4c874d22015-04-13 14:01:04106 LOG(ERROR) << "Cancelling request to " << target_url
107 << " with invalid referrer " << referrer_url;
jochen652275372015-01-16 11:35:27108 // Record information to help debug http://crbug.com/422871
109 if (!target_url.SchemeIsHTTPOrHTTPS())
110 return;
111 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48112 base::BindOnce(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41113 base::debug::DumpWithoutCrashing();
jochen4c874d22015-04-13 14:01:04114 NOTREACHED();
jochen0e3b3a62014-09-16 18:31:23115}
116
sclittle3c16159e2014-12-15 19:53:17117// Record network errors that HTTP requests complete with, including OK and
118// ABORTED.
maksim.sisov1b83bb72016-10-07 06:07:23119void RecordNetworkErrorHistograms(const net::URLRequest* request,
120 int net_error) {
sclittle3c16159e2014-12-15 19:53:17121 if (request->url().SchemeIs("http")) {
Ilya Sherman982457e62017-12-13 02:19:36122 base::UmaHistogramSparse("Net.HttpRequestCompletionErrorCodes",
123 std::abs(net_error));
sclittle3c16159e2014-12-15 19:53:17124
rdsmith4de1fcf2016-08-30 05:35:21125 if (request->load_flags() & net::LOAD_MAIN_FRAME_DEPRECATED) {
Ilya Sherman982457e62017-12-13 02:19:36126 base::UmaHistogramSparse("Net.HttpRequestCompletionErrorCodes.MainFrame",
127 std::abs(net_error));
sclittle3c16159e2014-12-15 19:53:17128 }
129 }
130}
131
Ken Rockot314714c2017-11-05 23:36:24132bool IsAccessAllowedInternal(const base::FilePath& path,
133 const base::FilePath& profile_path) {
134#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
135 return true;
136#else
137
138 std::vector<base::FilePath> whitelist;
139#if defined(OS_CHROMEOS)
140 // Use a whitelist to only allow access to files residing in the list of
141 // directories below.
142 static const base::FilePath::CharType* const kLocalAccessWhiteList[] = {
143 "/home/chronos/user/Downloads",
144 "/home/chronos/user/log",
145 "/home/chronos/user/WebRTC Logs",
146 "/media",
147 "/opt/oem",
148 "/usr/share/chromeos-assets",
149 "/var/log",
150 };
151
152 base::FilePath temp_dir;
153 if (PathService::Get(base::DIR_TEMP, &temp_dir))
154 whitelist.push_back(temp_dir);
155
156 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
157 // the profile path ("/home/chronos/user' is a hard link to current primary
158 // logged in profile.) For the support of multi-profile sessions, we are
159 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
160 // access.
161 if (!profile_path.empty()) {
162 const base::FilePath downloads = profile_path.AppendASCII("Downloads");
163 whitelist.push_back(downloads);
164 const base::FilePath webrtc_logs = profile_path.AppendASCII("WebRTC Logs");
165 whitelist.push_back(webrtc_logs);
166 }
167#elif defined(OS_ANDROID)
168 // Access to files in external storage is allowed.
169 base::FilePath external_storage_path;
170 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
171 if (external_storage_path.IsParent(path))
172 return true;
173
174 // Whitelist of other allowed directories.
175 static const base::FilePath::CharType* const kLocalAccessWhiteList[] = {
176 "/sdcard", "/mnt/sdcard",
177 };
178#endif
179
180 for (const auto* whitelisted_path : kLocalAccessWhiteList)
181 whitelist.push_back(base::FilePath(whitelisted_path));
182
183 for (const auto& whitelisted_path : whitelist) {
184 // base::FilePath::operator== should probably handle trailing separators.
185 if (whitelisted_path == path.StripTrailingSeparators() ||
186 whitelisted_path.IsParent(path)) {
187 return true;
188 }
189 }
190
191 DVLOG(1) << "File access denied - " << path.value().c_str();
192 return false;
193#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
194}
195
[email protected]d05ef99c2011-02-01 21:38:16196} // namespace
[email protected]ac039522010-06-15 16:39:44197
[email protected]0651b812011-02-24 00:22:50198ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10199 extensions::EventRouterForwarder* event_router,
rajendrant14a5a852016-11-17 17:58:48200 BooleanPrefMember* enable_referrers)
ljusten98684cb72016-10-11 13:26:18201 : profile_(nullptr),
[email protected]6a5f77c32011-09-04 19:19:59202 enable_referrers_(enable_referrers),
ljusten98684cb72016-10-11 13:26:18203 enable_do_not_track_(nullptr),
204 force_google_safe_search_(nullptr),
205 force_youtube_restrict_(nullptr),
igorcov193d8ad32016-05-27 11:03:21206 allowed_domains_for_apps_(nullptr),
mkwst0513c9d2015-04-01 05:53:15207 experimental_web_platform_features_enabled_(
gayane0b46091c2016-04-07 21:01:05208 base::CommandLine::ForCurrentProcess()->HasSwitch(
209 switches::kEnableExperimentalWebPlatformFeatures)),
sclittleae932be2015-10-08 20:53:50210 data_use_aggregator_(nullptr),
211 is_data_usage_off_the_record_(true) {
[email protected]0a8db0d2011-04-13 15:15:40212 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14213 extensions_delegate_.reset(
214 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50215}
216
[email protected]ac039522010-06-15 16:39:44217ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
218
[email protected]a09159a2012-11-29 12:51:48219void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20220 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14221 extensions_delegate_->set_extension_info_map(extension_info_map);
222}
223
224void ChromeNetworkDelegate::set_profile(void* profile) {
225 profile_ = profile;
226 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48227}
228
229void ChromeNetworkDelegate::set_cookie_settings(
drogerb1716972015-06-30 09:04:09230 content_settings::CookieSettings* cookie_settings) {
[email protected]a09159a2012-11-29 12:51:48231 cookie_settings_ = cookie_settings;
232}
233
sclittleae932be2015-10-08 20:53:50234void ChromeNetworkDelegate::set_data_use_aggregator(
235 data_usage::DataUseAggregator* data_use_aggregator,
236 bool is_data_usage_off_the_record) {
237 data_use_aggregator_ = data_use_aggregator;
238 is_data_usage_off_the_record_ = is_data_usage_off_the_record;
239}
240
[email protected]c4a7df82012-08-09 22:48:46241// static
[email protected]9d8cfb682012-09-13 16:48:04242void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40243 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04244 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33245 BooleanPrefMember* force_google_safe_search,
ljusten98684cb72016-10-11 13:26:18246 IntegerPrefMember* force_youtube_restrict,
igorcov193d8ad32016-05-27 11:03:21247 StringPrefMember* allowed_domains_for_apps,
[email protected]0a8db0d2011-04-13 15:15:40248 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02249 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02250 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43251 enable_referrers->MoveToThread(
thestig529ad8a2016-07-08 20:30:12252 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04253 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02254 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43255 enable_do_not_track->MoveToThread(
thestig529ad8a2016-07-08 20:30:12256 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04257 }
[email protected]d1208ba32012-11-08 11:10:33258 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18259 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33260 force_google_safe_search->MoveToThread(
thestig529ad8a2016-07-08 20:30:12261 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]d1208ba32012-11-08 11:10:33262 }
ljusten98684cb72016-10-11 13:26:18263 if (force_youtube_restrict) {
264 force_youtube_restrict->Init(prefs::kForceYouTubeRestrict, pref_service);
265 force_youtube_restrict->MoveToThread(
thestig529ad8a2016-07-08 20:30:12266 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
treib4e26f6652014-12-01 14:34:18267 }
igorcov193d8ad32016-05-27 11:03:21268 if (allowed_domains_for_apps) {
269 allowed_domains_for_apps->Init(prefs::kAllowedDomainsForApps, pref_service);
270 allowed_domains_for_apps->MoveToThread(
thestig529ad8a2016-07-08 20:30:12271 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
igorcov193d8ad32016-05-27 11:03:21272 }
[email protected]0a8db0d2011-04-13 15:15:40273}
274
[email protected]4875ba12011-03-30 22:31:51275int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12276 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47277 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12278 GURL* new_url) {
[email protected]6a5f77c32011-09-04 19:19:59279
[email protected]4a2b6232014-06-19 08:44:14280 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18281
[email protected]0a8db0d2011-04-13 15:15:40282 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13283 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04284 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
285 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33286
treib4e26f6652014-12-01 14:34:18287 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18288 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33289
290 net::CompletionCallback wrapped_callback = callback;
291 if (force_safe_search) {
292 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
293 callback,
294 base::Unretained(request),
295 base::Unretained(new_url));
296 }
297
[email protected]4a2b6232014-06-19 08:44:14298 int rv = extensions_delegate_->OnBeforeURLRequest(
299 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33300
301 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24302 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33303
igorcov193d8ad32016-05-27 11:03:21304 if (allowed_domains_for_apps_ &&
305 !allowed_domains_for_apps_->GetValue().empty() &&
306 request->url().DomainIs("google.com")) {
307 request->SetExtraRequestHeaderByName("X-GoogApps-Allowed-Domains",
308 allowed_domains_for_apps_->GetValue(),
309 true);
310 }
311
[email protected]d1208ba32012-11-08 11:10:33312 return rv;
[email protected]d05ef99c2011-02-01 21:38:16313}
314
ryansturm2343cb62016-06-15 01:09:00315int ChromeNetworkDelegate::OnBeforeStartTransaction(
[email protected]636eccd2011-06-28 12:28:01316 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47317 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12318 net::HttpRequestHeaders* headers) {
ljusten98684cb72016-10-11 13:26:18319 if (force_youtube_restrict_) {
320 int value = force_youtube_restrict_->GetValue();
321 static_assert(safe_search_util::YOUTUBE_RESTRICT_OFF == 0,
322 "OFF must be first");
323 if (value > safe_search_util::YOUTUBE_RESTRICT_OFF &&
324 value < safe_search_util::YOUTUBE_RESTRICT_COUNT) {
325 safe_search_util::ForceYouTubeRestrict(request, headers,
326 static_cast<safe_search_util::YouTubeRestrictMode>(value));
327 }
328 }
[email protected]184799df2014-07-22 16:03:24329
ryansturm2343cb62016-06-15 01:09:00330 return extensions_delegate_->OnBeforeStartTransaction(request, callback,
331 headers);
[email protected]ac039522010-06-15 16:39:44332}
[email protected]8202d0c2011-02-23 08:31:14333
ryansturm2343cb62016-06-15 01:09:00334void ChromeNetworkDelegate::OnStartTransaction(
[email protected]5796dc942011-07-14 19:26:10335 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15336 const net::HttpRequestHeaders& headers) {
ryansturm2343cb62016-06-15 01:09:00337 extensions_delegate_->OnStartTransaction(request, headers);
[email protected]82b42302011-04-20 16:28:16338}
339
[email protected]ea8141e2011-10-05 13:12:51340int ChromeNetworkDelegate::OnHeadersReceived(
341 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47342 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32343 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16344 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
345 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14346 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16347 request,
348 callback,
349 original_response_headers,
350 override_response_headers,
351 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51352}
353
[email protected]31b2e5f2011-04-20 16:58:32354void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
355 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02356 if (domain_reliability_monitor_)
357 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14358 extensions_delegate_->OnBeforeRedirect(request, new_location);
Dominic Battre5e8a1cd2018-01-02 16:16:58359 variations::StripVariationHeaderIfNeeded(new_location, request);
[email protected]31b2e5f2011-04-20 16:58:32360}
361
maksim.sisov1b83bb72016-10-07 06:07:23362void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request,
363 int net_error) {
364 extensions_delegate_->OnResponseStarted(request, net_error);
[email protected]8202d0c2011-02-23 08:31:14365}
366
sclittlea133de02015-11-10 23:54:21367void ChromeNetworkDelegate::OnNetworkBytesReceived(net::URLRequest* request,
368 int64_t bytes_received) {
brettwee486bb2016-12-08 17:56:34369#if !defined(OS_ANDROID)
sclittlece72c482015-08-24 20:20:59370 // Note: Currently, OnNetworkBytesReceived is only implemented for HTTP jobs,
371 // not FTP or other types, so those kinds of bytes will not be reported here.
avi24d693f2016-08-06 18:03:52372 task_manager::TaskManagerInterface::OnRawBytesRead(*request, bytes_received);
brettwee486bb2016-12-08 17:56:34373#endif // !defined(OS_ANDROID)
sclittleae932be2015-10-08 20:53:50374
sclittlea133de02015-11-10 23:54:21375 ReportDataUsageStats(request, 0 /* tx_bytes */, bytes_received);
sclittleae932be2015-10-08 20:53:50376}
377
sclittlea133de02015-11-10 23:54:21378void ChromeNetworkDelegate::OnNetworkBytesSent(net::URLRequest* request,
sclittleae932be2015-10-08 20:53:50379 int64_t bytes_sent) {
cburne9d2f3612017-06-20 22:15:03380#if !defined(OS_ANDROID)
381 // Note: Currently, OnNetworkBytesSent is only implemented for HTTP jobs,
382 // not FTP or other types, so those kinds of bytes will not be reported here.
383 task_manager::TaskManagerInterface::OnRawBytesSent(*request, bytes_sent);
384#endif // !defined(OS_ANDROID)
385
sclittlea133de02015-11-10 23:54:21386 ReportDataUsageStats(request, bytes_sent, 0 /* rx_bytes */);
[email protected]8523ba52011-05-22 19:00:58387}
388
[email protected]9045b8822012-01-13 20:35:35389void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
maksim.sisov1b83bb72016-10-07 06:07:23390 bool started,
391 int net_error) {
392 DCHECK_NE(net::ERR_IO_PENDING, net_error);
sclittle3c16159e2014-12-15 19:53:17393
maksim.sisov1b83bb72016-10-07 06:07:23394 // TODO(amohammadkhan): Verify that there is no double recording in data use
395 // of redirected requests.
maksim.sisov1b83bb72016-10-07 06:07:23396 RecordNetworkErrorHistograms(request, net_error);
397
maksim.sisov1b83bb72016-10-07 06:07:23398 extensions_delegate_->OnCompleted(request, started, net_error);
[email protected]45de676a2014-03-18 23:52:02399 if (domain_reliability_monitor_)
400 domain_reliability_monitor_->OnCompleted(request, started);
maksim.sisov1b83bb72016-10-07 06:07:23401 extensions_delegate_->ForwardProxyErrors(request, net_error);
[email protected]4a2b6232014-06-19 08:44:14402 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14403}
[email protected]4b50cb52011-03-10 00:29:37404
[email protected]4875ba12011-03-30 22:31:51405void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14406 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51407}
408
[email protected]82a37672011-05-03 12:02:41409void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09410 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14411 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41412}
[email protected]7efc582d2011-08-03 20:46:35413
[email protected]c2911d72011-10-03 22:16:36414net::NetworkDelegate::AuthRequiredResponse
415ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35416 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36417 const net::AuthChallengeInfo& auth_info,
418 const AuthCallback& callback,
419 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14420 return extensions_delegate_->OnAuthRequired(
421 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35422}
[email protected]9c8ae8c2012-03-09 13:13:35423
[email protected]4c219e22012-05-05 19:41:04424bool ChromeNetworkDelegate::OnCanGetCookies(
425 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35426 const net::CookieList& cookie_list) {
ljusten98684cb72016-10-11 13:26:18427 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02428 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35429 return true;
430
falken71691402017-01-27 03:38:00431 bool allow = cookie_settings_->IsCookieAccessAllowed(
Mike Westb85da8ed2017-08-10 14:16:46432 request.url(), request.site_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35433
jam092d3be2016-09-27 15:56:36434 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
435 if (info) {
[email protected]9c8ae8c2012-03-09 13:13:35436 BrowserThread::PostTask(
437 BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48438 base::BindOnce(&TabSpecificContentSettings::CookiesRead,
439 info->GetWebContentsGetterForRequest(), request.url(),
Mike Westb85da8ed2017-08-10 14:16:46440 request.site_for_cookies(), cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35441 }
442
443 return allow;
444}
445
[email protected]4c219e22012-05-05 19:41:04446bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
Victor Costan70f85512017-11-20 16:14:46447 const net::CanonicalCookie& cookie,
[email protected]4c219e22012-05-05 19:41:04448 net::CookieOptions* options) {
ljusten98684cb72016-10-11 13:26:18449 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02450 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35451 return true;
452
falken71691402017-01-27 03:38:00453 bool allow = cookie_settings_->IsCookieAccessAllowed(
Mike Westb85da8ed2017-08-10 14:16:46454 request.url(), request.site_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35455
jam092d3be2016-09-27 15:56:36456 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
457 if (info) {
[email protected]9c8ae8c2012-03-09 13:13:35458 BrowserThread::PostTask(
459 BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48460 base::BindOnce(&TabSpecificContentSettings::CookieChanged,
461 info->GetWebContentsGetterForRequest(), request.url(),
Victor Costan70f85512017-11-20 16:14:46462 request.site_for_cookies(), cookie, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35463 }
464
465 return allow;
466}
[email protected]4c219e22012-05-05 19:41:04467
satoruxddac0442017-05-29 06:06:18468bool ChromeNetworkDelegate::OnCanAccessFile(
469 const net::URLRequest& request,
470 const base::FilePath& original_path,
471 const base::FilePath& absolute_path) const {
satoruxd18e61a2017-06-08 06:38:46472 if (g_access_to_all_files_enabled)
[email protected]d8e4f132012-09-06 04:28:05473 return true;
Ken Rockot314714c2017-11-05 23:36:24474 return IsAccessAllowed(original_path, absolute_path, profile_path_);
satorux7c5360582017-01-27 07:24:29475}
476
477// static
478bool ChromeNetworkDelegate::IsAccessAllowed(
479 const base::FilePath& path,
480 const base::FilePath& profile_path) {
Ken Rockot314714c2017-11-05 23:36:24481 return IsAccessAllowedInternal(path, profile_path);
482}
483
484// static
485bool ChromeNetworkDelegate::IsAccessAllowed(
486 const base::FilePath& path,
487 const base::FilePath& absolute_path,
488 const base::FilePath& profile_path) {
489#if defined(OS_ANDROID)
490 // Android's whitelist relies on symbolic links (ex. /sdcard is whitelisted
491 // and commonly a symbolic link), thus do not check absolute paths.
492 return IsAccessAllowedInternal(path, profile_path);
satorux7c5360582017-01-27 07:24:29493#else
Ken Rockot314714c2017-11-05 23:36:24494 return (IsAccessAllowedInternal(path, profile_path) &&
495 IsAccessAllowedInternal(absolute_path, profile_path));
[email protected]d8e4f132012-09-06 04:28:05496#endif
[email protected]4c219e22012-05-05 19:41:04497}
[email protected]a1d4ab072012-06-07 13:21:15498
satoruxd18e61a2017-06-08 06:38:46499// static
500void ChromeNetworkDelegate::EnableAccessToAllFilesForTesting(bool enabled) {
501 g_access_to_all_files_enabled = enabled;
502}
503
[email protected]e6d017652013-05-17 18:01:40504bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
505 const GURL& url,
Mike Westb85da8ed2017-08-10 14:16:46506 const GURL& site_for_cookies) const {
ljusten98684cb72016-10-11 13:26:18507 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02508 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40509 return false;
510
Mike Westb85da8ed2017-08-10 14:16:46511 return !cookie_settings_->IsCookieAccessAllowed(url, site_for_cookies);
[email protected]e6d017652013-05-17 18:01:40512}
513
estark7625d812015-10-12 20:10:41514bool ChromeNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
mkwst0513c9d2015-04-01 05:53:15515 return experimental_web_platform_features_enabled_;
516}
517
jochen0e3b3a62014-09-16 18:31:23518bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
519 const net::URLRequest& request,
520 const GURL& target_url,
521 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27522 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23523 return true;
524}
sclittlea133de02015-11-10 23:54:21525
juliatuttlefcf47202017-05-23 15:53:02526bool ChromeNetworkDelegate::OnCanQueueReportingReport(
527 const url::Origin& origin) const {
528 if (!cookie_settings_)
529 return true;
530
531 return cookie_settings_->IsCookieAccessAllowed(origin.GetURL(),
532 origin.GetURL());
533}
534
Douglas Creager7b07ea42018-02-27 21:08:08535void ChromeNetworkDelegate::OnCanSendReportingReports(
536 std::set<url::Origin> origins,
537 base::OnceCallback<void(std::set<url::Origin>)> result_callback) const {
538 if (!cookie_settings_) {
539 std::move(result_callback).Run(std::move(origins));
540 return;
541 }
juliatuttlefcf47202017-05-23 15:53:02542
Douglas Creager7b07ea42018-02-27 21:08:08543 for (auto it = origins.begin(); it != origins.end();) {
544 const auto& origin = *it;
545 if (!cookie_settings_->IsCookieAccessAllowed(origin.GetURL(),
546 origin.GetURL())) {
547 origins.erase(it++);
548 } else {
549 ++it;
550 }
551 }
552 std::move(result_callback).Run(std::move(origins));
juliatuttlefcf47202017-05-23 15:53:02553}
554
555bool ChromeNetworkDelegate::OnCanSetReportingClient(
556 const url::Origin& origin,
557 const GURL& endpoint) const {
558 if (!cookie_settings_)
559 return true;
560
561 return cookie_settings_->IsCookieAccessAllowed(endpoint, origin.GetURL());
562}
563
564bool ChromeNetworkDelegate::OnCanUseReportingClient(
565 const url::Origin& origin,
566 const GURL& endpoint) const {
567 if (!cookie_settings_)
568 return true;
569
570 return cookie_settings_->IsCookieAccessAllowed(endpoint, origin.GetURL());
571}
572
sclittlea133de02015-11-10 23:54:21573void ChromeNetworkDelegate::ReportDataUsageStats(net::URLRequest* request,
574 int64_t tx_bytes,
575 int64_t rx_bytes) {
576 if (!data_use_aggregator_)
577 return;
578
579 if (is_data_usage_off_the_record_) {
580 data_use_aggregator_->ReportOffTheRecordDataUse(tx_bytes, rx_bytes);
581 return;
582 }
583
584 data_use_aggregator_->ReportDataUse(request, tx_bytes, rx_bytes);
585}