blob: fae150e3c1cbafe3022030df11c97765e4a47f92 [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"
mmenke376b8782015-05-05 04:41:3023#include "base/profiler/scoped_tracker.h"
[email protected]3ea1b182013-02-08 22:38:4124#include "base/strings/string_number_conversions.h"
ellyjonesd84033d2015-02-12 19:20:3425#include "base/strings/string_util.h"
[email protected]069c49162013-09-11 20:46:3526#include "base/time/time.h"
avi6846aef2015-12-26 01:09:3827#include "build/build_config.h"
[email protected]6baff0b52012-03-06 01:30:1828#include "chrome/browser/browser_process.h"
drogerb1716972015-06-30 09:04:0929#include "chrome/browser/content_settings/cookie_settings_factory.h"
[email protected]9c8ae8c2012-03-09 13:13:3530#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5831#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]4a2b6232014-06-19 08:44:1432#include "chrome/browser/net/chrome_extensions_network_delegate.h"
ttuttle05ae3f342015-07-13 17:38:3533#include "chrome/browser/net/request_source_bandwidth_histograms.h"
[email protected]6baff0b52012-03-06 01:30:1834#include "chrome/browser/profiles/profile_manager.h"
avi24d693f2016-08-06 18:03:5235#include "chrome/browser/task_manager/task_manager_interface.h"
sievers2f1e8112015-12-04 18:43:5636#include "chrome/common/features.h"
[email protected]0a8db0d2011-04-13 15:15:4037#include "chrome/common/pref_names.h"
drogerb1716972015-06-30 09:04:0938#include "components/content_settings/core/browser/cookie_settings.h"
sclittleae932be2015-10-08 20:53:5039#include "components/data_usage/core/data_use_aggregator.h"
[email protected]45de676a2014-03-18 23:52:0240#include "components/domain_reliability/monitor.h"
brettw03e61962016-03-15 06:27:0441#include "components/policy/core/browser/url_blacklist_manager.h"
brettwb1fc1b82016-02-02 00:19:0842#include "components/prefs/pref_member.h"
43#include "components/prefs/pref_service.h"
[email protected]c38831a12011-10-28 12:44:4944#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5645#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3346#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3547#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1548#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3449#include "content/public/common/process_type.h"
tyoshino430d2f42016-12-15 09:30:0050#include "content/public/common/resource_type.h"
brettw00899e62016-11-12 02:10:1751#include "extensions/features/features.h"
[email protected]82b42302011-04-20 16:28:1652#include "net/base/host_port_pair.h"
sclittle3c16159e2014-12-15 19:53:1753#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1454#include "net/base/net_errors.h"
[email protected]5b9bc352012-07-18 13:13:3455#include "net/cookies/canonical_cookie.h"
56#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4457#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1658#include "net/http/http_response_headers.h"
ellyjonesd84033d2015-02-12 19:20:3459#include "net/http/http_status_code.h"
eroman87c53d62015-04-02 06:51:0760#include "net/log/net_log.h"
mikecirone8b85c432016-09-08 19:11:0061#include "net/log/net_log_event_type.h"
mikecironef22f9812016-10-04 03:40:1962#include "net/log/net_log_with_source.h"
[email protected]d05ef99c2011-02-01 21:38:1663#include "net/url_request/url_request.h"
64
zpengdb4a58e2017-01-10 17:40:3265#if defined(OS_ANDROID)
[email protected]4a2b6232014-06-19 08:44:1466#include "chrome/browser/io_thread.h"
rajendrant1e1eff12016-07-08 20:11:1567#include "chrome/browser/precache/precache_util.h"
[email protected]4a2b6232014-06-19 08:44:1468#endif
69
[email protected]4c219e22012-05-05 19:41:0470#if defined(OS_CHROMEOS)
[email protected]49c4cf852013-09-27 19:28:2471#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3572#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0473#endif
74
brettw00899e62016-11-12 02:10:1775#if BUILDFLAG(ENABLE_EXTENSIONS)
[email protected]84b7a552014-07-19 04:52:0676#include "extensions/common/constants.h"
77#endif
78
[email protected]631bb742011-11-02 11:29:3979using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1180using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2581using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3982
[email protected]d05ef99c2011-02-01 21:38:1683namespace {
84
satoruxd18e61a2017-06-08 06:38:4685bool g_access_to_all_files_enabled = false;
86
[email protected]9d8cfb682012-09-13 16:48:0487const char kDNTHeader[] = "DNT";
88
[email protected]d1208ba32012-11-08 11:10:3389// Gets called when the extensions finish work on the URL. If the extensions
90// did not do a redirect (so |new_url| is empty) then we enforce the
91// SafeSearch parameters. Otherwise we will get called again after the
92// redirect and we enforce SafeSearch then.
93void ForceGoogleSafeSearchCallbackWrapper(
94 const net::CompletionCallback& callback,
95 net::URLRequest* request,
96 GURL* new_url,
97 int rv) {
98 if (rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:2499 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33100 callback.Run(rv);
101}
102
jochen652275372015-01-16 11:35:27103void ReportInvalidReferrerSendOnUI() {
jochen0e3b3a62014-09-16 18:31:23104 base::RecordAction(
105 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
jochen652275372015-01-16 11:35:27106}
107
108void ReportInvalidReferrerSend(const GURL& target_url,
109 const GURL& referrer_url) {
jochen4c874d22015-04-13 14:01:04110 LOG(ERROR) << "Cancelling request to " << target_url
111 << " with invalid referrer " << referrer_url;
jochen652275372015-01-16 11:35:27112 // Record information to help debug http://crbug.com/422871
113 if (!target_url.SchemeIsHTTPOrHTTPS())
114 return;
115 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48116 base::BindOnce(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41117 base::debug::DumpWithoutCrashing();
jochen4c874d22015-04-13 14:01:04118 NOTREACHED();
jochen0e3b3a62014-09-16 18:31:23119}
120
sclittle3c16159e2014-12-15 19:53:17121// Record network errors that HTTP requests complete with, including OK and
122// ABORTED.
maksim.sisov1b83bb72016-10-07 06:07:23123void RecordNetworkErrorHistograms(const net::URLRequest* request,
124 int net_error) {
sclittle3c16159e2014-12-15 19:53:17125 if (request->url().SchemeIs("http")) {
126 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
maksim.sisov1b83bb72016-10-07 06:07:23127 std::abs(net_error));
sclittle3c16159e2014-12-15 19:53:17128
rdsmith4de1fcf2016-08-30 05:35:21129 if (request->load_flags() & net::LOAD_MAIN_FRAME_DEPRECATED) {
sclittle3c16159e2014-12-15 19:53:17130 UMA_HISTOGRAM_SPARSE_SLOWLY(
maksim.sisov1b83bb72016-10-07 06:07:23131 "Net.HttpRequestCompletionErrorCodes.MainFrame", std::abs(net_error));
sclittle3c16159e2014-12-15 19:53:17132 }
133 }
134}
135
[email protected]d05ef99c2011-02-01 21:38:16136} // namespace
[email protected]ac039522010-06-15 16:39:44137
[email protected]0651b812011-02-24 00:22:50138ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10139 extensions::EventRouterForwarder* event_router,
rajendrant14a5a852016-11-17 17:58:48140 BooleanPrefMember* enable_referrers)
ljusten98684cb72016-10-11 13:26:18141 : profile_(nullptr),
[email protected]6a5f77c32011-09-04 19:19:59142 enable_referrers_(enable_referrers),
ljusten98684cb72016-10-11 13:26:18143 enable_do_not_track_(nullptr),
144 force_google_safe_search_(nullptr),
145 force_youtube_restrict_(nullptr),
igorcov193d8ad32016-05-27 11:03:21146 allowed_domains_for_apps_(nullptr),
juliatuttle127604ea2016-12-19 19:13:04147 url_blacklist_manager_(NULL),
mkwst0513c9d2015-04-01 05:53:15148 experimental_web_platform_features_enabled_(
gayane0b46091c2016-04-07 21:01:05149 base::CommandLine::ForCurrentProcess()->HasSwitch(
150 switches::kEnableExperimentalWebPlatformFeatures)),
sclittleae932be2015-10-08 20:53:50151 data_use_aggregator_(nullptr),
152 is_data_usage_off_the_record_(true) {
[email protected]0a8db0d2011-04-13 15:15:40153 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14154 extensions_delegate_.reset(
155 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50156}
157
[email protected]ac039522010-06-15 16:39:44158ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
159
[email protected]a09159a2012-11-29 12:51:48160void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20161 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14162 extensions_delegate_->set_extension_info_map(extension_info_map);
163}
164
165void ChromeNetworkDelegate::set_profile(void* profile) {
166 profile_ = profile;
167 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48168}
169
170void ChromeNetworkDelegate::set_cookie_settings(
drogerb1716972015-06-30 09:04:09171 content_settings::CookieSettings* cookie_settings) {
[email protected]a09159a2012-11-29 12:51:48172 cookie_settings_ = cookie_settings;
173}
174
sclittleae932be2015-10-08 20:53:50175void ChromeNetworkDelegate::set_data_use_aggregator(
176 data_usage::DataUseAggregator* data_use_aggregator,
177 bool is_data_usage_off_the_record) {
178 data_use_aggregator_ = data_use_aggregator;
179 is_data_usage_off_the_record_ = is_data_usage_off_the_record;
180}
181
[email protected]c4a7df82012-08-09 22:48:46182// static
[email protected]9d8cfb682012-09-13 16:48:04183void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40184 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04185 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33186 BooleanPrefMember* force_google_safe_search,
ljusten98684cb72016-10-11 13:26:18187 IntegerPrefMember* force_youtube_restrict,
igorcov193d8ad32016-05-27 11:03:21188 StringPrefMember* allowed_domains_for_apps,
[email protected]0a8db0d2011-04-13 15:15:40189 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02190 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02191 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43192 enable_referrers->MoveToThread(
thestig529ad8a2016-07-08 20:30:12193 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04194 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02195 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43196 enable_do_not_track->MoveToThread(
thestig529ad8a2016-07-08 20:30:12197 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04198 }
[email protected]d1208ba32012-11-08 11:10:33199 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18200 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33201 force_google_safe_search->MoveToThread(
thestig529ad8a2016-07-08 20:30:12202 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]d1208ba32012-11-08 11:10:33203 }
ljusten98684cb72016-10-11 13:26:18204 if (force_youtube_restrict) {
205 force_youtube_restrict->Init(prefs::kForceYouTubeRestrict, pref_service);
206 force_youtube_restrict->MoveToThread(
thestig529ad8a2016-07-08 20:30:12207 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
treib4e26f6652014-12-01 14:34:18208 }
igorcov193d8ad32016-05-27 11:03:21209 if (allowed_domains_for_apps) {
210 allowed_domains_for_apps->Init(prefs::kAllowedDomainsForApps, pref_service);
211 allowed_domains_for_apps->MoveToThread(
thestig529ad8a2016-07-08 20:30:12212 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
igorcov193d8ad32016-05-27 11:03:21213 }
[email protected]0a8db0d2011-04-13 15:15:40214}
215
[email protected]4875ba12011-03-30 22:31:51216int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12217 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47218 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12219 GURL* new_url) {
mmenke376b8782015-05-05 04:41:30220 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
221 tracked_objects::ScopedTracker tracking_profile1(
222 FROM_HERE_WITH_EXPLICIT_FUNCTION(
223 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest"));
224
[email protected]6a5f77c32011-09-04 19:19:59225 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
226 // blocked. However, an extension might redirect the request to another URL,
227 // which is not blocked.
mmenkef1aa9072015-05-18 16:21:04228
229 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
[email protected]cb85f8e52014-04-08 11:44:40230 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
mmenkef1aa9072015-05-18 16:21:04231 if (info && content::IsResourceTypeFrame(info->GetResourceType()) &&
232 url_blacklist_manager_ &&
233 url_blacklist_manager_->ShouldBlockRequestForFrame(
234 request->url(), &error)) {
[email protected]6a5f77c32011-09-04 19:19:59235 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59236 request->net_log().AddEvent(
mikecirone8b85c432016-09-08 19:11:00237 net::NetLogEventType::CHROME_POLICY_ABORTED_REQUEST,
[email protected]2fa08912012-06-14 20:56:26238 net::NetLog::StringCallback("url",
239 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40240 return error;
[email protected]6a5f77c32011-09-04 19:19:59241 }
242
mmenke376b8782015-05-05 04:41:30243 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
244 tracked_objects::ScopedTracker tracking_profile2(
245 FROM_HERE_WITH_EXPLICIT_FUNCTION(
246 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 2"));
247
[email protected]4a2b6232014-06-19 08:44:14248 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18249
[email protected]0a8db0d2011-04-13 15:15:40250 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13251 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04252 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
253 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33254
mmenke376b8782015-05-05 04:41:30255 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
256 tracked_objects::ScopedTracker tracking_profile3(
257 FROM_HERE_WITH_EXPLICIT_FUNCTION(
258 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 3"));
259
treib4e26f6652014-12-01 14:34:18260 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18261 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33262
263 net::CompletionCallback wrapped_callback = callback;
264 if (force_safe_search) {
265 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
266 callback,
267 base::Unretained(request),
268 base::Unretained(new_url));
269 }
270
[email protected]4a2b6232014-06-19 08:44:14271 int rv = extensions_delegate_->OnBeforeURLRequest(
272 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33273
mmenke376b8782015-05-05 04:41:30274 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
275 tracked_objects::ScopedTracker tracking_profile4(
276 FROM_HERE_WITH_EXPLICIT_FUNCTION(
277 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 4"));
278
[email protected]d1208ba32012-11-08 11:10:33279 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24280 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33281
mmenke376b8782015-05-05 04:41:30282 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
283 tracked_objects::ScopedTracker tracking_profile5(
284 FROM_HERE_WITH_EXPLICIT_FUNCTION(
285 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 5"));
286
igorcov193d8ad32016-05-27 11:03:21287 if (allowed_domains_for_apps_ &&
288 !allowed_domains_for_apps_->GetValue().empty() &&
289 request->url().DomainIs("google.com")) {
290 request->SetExtraRequestHeaderByName("X-GoogApps-Allowed-Domains",
291 allowed_domains_for_apps_->GetValue(),
292 true);
293 }
294
[email protected]d1208ba32012-11-08 11:10:33295 return rv;
[email protected]d05ef99c2011-02-01 21:38:16296}
297
ryansturm2343cb62016-06-15 01:09:00298int ChromeNetworkDelegate::OnBeforeStartTransaction(
[email protected]636eccd2011-06-28 12:28:01299 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47300 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12301 net::HttpRequestHeaders* headers) {
ljusten98684cb72016-10-11 13:26:18302 if (force_youtube_restrict_) {
303 int value = force_youtube_restrict_->GetValue();
304 static_assert(safe_search_util::YOUTUBE_RESTRICT_OFF == 0,
305 "OFF must be first");
306 if (value > safe_search_util::YOUTUBE_RESTRICT_OFF &&
307 value < safe_search_util::YOUTUBE_RESTRICT_COUNT) {
308 safe_search_util::ForceYouTubeRestrict(request, headers,
309 static_cast<safe_search_util::YouTubeRestrictMode>(value));
310 }
311 }
[email protected]184799df2014-07-22 16:03:24312
ryansturm2343cb62016-06-15 01:09:00313 return extensions_delegate_->OnBeforeStartTransaction(request, callback,
314 headers);
[email protected]ac039522010-06-15 16:39:44315}
[email protected]8202d0c2011-02-23 08:31:14316
ryansturm2343cb62016-06-15 01:09:00317void ChromeNetworkDelegate::OnStartTransaction(
[email protected]5796dc942011-07-14 19:26:10318 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15319 const net::HttpRequestHeaders& headers) {
ryansturm2343cb62016-06-15 01:09:00320 extensions_delegate_->OnStartTransaction(request, headers);
[email protected]82b42302011-04-20 16:28:16321}
322
[email protected]ea8141e2011-10-05 13:12:51323int ChromeNetworkDelegate::OnHeadersReceived(
324 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47325 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32326 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16327 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
328 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14329 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16330 request,
331 callback,
332 original_response_headers,
333 override_response_headers,
334 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51335}
336
[email protected]31b2e5f2011-04-20 16:58:32337void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
338 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02339 if (domain_reliability_monitor_)
340 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14341 extensions_delegate_->OnBeforeRedirect(request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32342}
343
maksim.sisov1b83bb72016-10-07 06:07:23344void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request,
345 int net_error) {
346 extensions_delegate_->OnResponseStarted(request, net_error);
[email protected]8202d0c2011-02-23 08:31:14347}
348
sclittlea133de02015-11-10 23:54:21349void ChromeNetworkDelegate::OnNetworkBytesReceived(net::URLRequest* request,
350 int64_t bytes_received) {
brettwee486bb2016-12-08 17:56:34351#if !defined(OS_ANDROID)
sclittlece72c482015-08-24 20:20:59352 // Note: Currently, OnNetworkBytesReceived is only implemented for HTTP jobs,
353 // not FTP or other types, so those kinds of bytes will not be reported here.
avi24d693f2016-08-06 18:03:52354 task_manager::TaskManagerInterface::OnRawBytesRead(*request, bytes_received);
brettwee486bb2016-12-08 17:56:34355#endif // !defined(OS_ANDROID)
sclittleae932be2015-10-08 20:53:50356
sclittlea133de02015-11-10 23:54:21357 ReportDataUsageStats(request, 0 /* tx_bytes */, bytes_received);
sclittleae932be2015-10-08 20:53:50358}
359
sclittlea133de02015-11-10 23:54:21360void ChromeNetworkDelegate::OnNetworkBytesSent(net::URLRequest* request,
sclittleae932be2015-10-08 20:53:50361 int64_t bytes_sent) {
cburne9d2f3612017-06-20 22:15:03362#if !defined(OS_ANDROID)
363 // Note: Currently, OnNetworkBytesSent is only implemented for HTTP jobs,
364 // not FTP or other types, so those kinds of bytes will not be reported here.
365 task_manager::TaskManagerInterface::OnRawBytesSent(*request, bytes_sent);
366#endif // !defined(OS_ANDROID)
367
sclittlea133de02015-11-10 23:54:21368 ReportDataUsageStats(request, bytes_sent, 0 /* rx_bytes */);
[email protected]8523ba52011-05-22 19:00:58369}
370
[email protected]9045b8822012-01-13 20:35:35371void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
maksim.sisov1b83bb72016-10-07 06:07:23372 bool started,
373 int net_error) {
374 DCHECK_NE(net::ERR_IO_PENDING, net_error);
sclittle3c16159e2014-12-15 19:53:17375
maksim.sisov1b83bb72016-10-07 06:07:23376 // TODO(amohammadkhan): Verify that there is no double recording in data use
377 // of redirected requests.
maksim.sisov1b83bb72016-10-07 06:07:23378 RecordNetworkErrorHistograms(request, net_error);
379
380 if (net_error == net::OK) {
zpengdb4a58e2017-01-10 17:40:32381#if defined(OS_ANDROID)
rajendrant1e1eff12016-07-08 20:11:15382 precache::UpdatePrecacheMetricsAndState(request, profile_);
zpengdb4a58e2017-01-10 17:40:32383#endif // defined(OS_ANDROID)
[email protected]48944382011-04-23 13:28:16384 }
maksim.sisov1b83bb72016-10-07 06:07:23385
386 extensions_delegate_->OnCompleted(request, started, net_error);
[email protected]45de676a2014-03-18 23:52:02387 if (domain_reliability_monitor_)
388 domain_reliability_monitor_->OnCompleted(request, started);
ttuttle05ae3f342015-07-13 17:38:35389 RecordRequestSourceBandwidth(request, started);
maksim.sisov1b83bb72016-10-07 06:07:23390 extensions_delegate_->ForwardProxyErrors(request, net_error);
[email protected]4a2b6232014-06-19 08:44:14391 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14392}
[email protected]4b50cb52011-03-10 00:29:37393
[email protected]4875ba12011-03-30 22:31:51394void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14395 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51396}
397
[email protected]82a37672011-05-03 12:02:41398void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09399 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14400 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41401}
[email protected]7efc582d2011-08-03 20:46:35402
[email protected]c2911d72011-10-03 22:16:36403net::NetworkDelegate::AuthRequiredResponse
404ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35405 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36406 const net::AuthChallengeInfo& auth_info,
407 const AuthCallback& callback,
408 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14409 return extensions_delegate_->OnAuthRequired(
410 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35411}
[email protected]9c8ae8c2012-03-09 13:13:35412
[email protected]4c219e22012-05-05 19:41:04413bool ChromeNetworkDelegate::OnCanGetCookies(
414 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35415 const net::CookieList& cookie_list) {
ljusten98684cb72016-10-11 13:26:18416 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02417 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35418 return true;
419
falken71691402017-01-27 03:38:00420 bool allow = cookie_settings_->IsCookieAccessAllowed(
[email protected]4c219e22012-05-05 19:41:04421 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35422
jam092d3be2016-09-27 15:56:36423 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
424 if (info) {
[email protected]9c8ae8c2012-03-09 13:13:35425 BrowserThread::PostTask(
426 BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48427 base::BindOnce(&TabSpecificContentSettings::CookiesRead,
428 info->GetWebContentsGetterForRequest(), request.url(),
429 request.first_party_for_cookies(), cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35430 }
431
432 return allow;
433}
434
[email protected]4c219e22012-05-05 19:41:04435bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
436 const std::string& cookie_line,
437 net::CookieOptions* options) {
ljusten98684cb72016-10-11 13:26:18438 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02439 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35440 return true;
441
falken71691402017-01-27 03:38:00442 bool allow = cookie_settings_->IsCookieAccessAllowed(
[email protected]4c219e22012-05-05 19:41:04443 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35444
jam092d3be2016-09-27 15:56:36445 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
446 if (info) {
[email protected]9c8ae8c2012-03-09 13:13:35447 BrowserThread::PostTask(
448 BrowserThread::UI, FROM_HERE,
tzik53a7b9022017-04-24 11:14:48449 base::BindOnce(&TabSpecificContentSettings::CookieChanged,
450 info->GetWebContentsGetterForRequest(), request.url(),
451 request.first_party_for_cookies(), cookie_line, *options,
452 !allow));
[email protected]9c8ae8c2012-03-09 13:13:35453 }
454
455 return allow;
456}
[email protected]4c219e22012-05-05 19:41:04457
satoruxddac0442017-05-29 06:06:18458bool ChromeNetworkDelegate::OnCanAccessFile(
459 const net::URLRequest& request,
460 const base::FilePath& original_path,
461 const base::FilePath& absolute_path) const {
satoruxd18e61a2017-06-08 06:38:46462 if (g_access_to_all_files_enabled)
[email protected]d8e4f132012-09-06 04:28:05463 return true;
464
satoruxddac0442017-05-29 06:06:18465#if defined(OS_ANDROID)
466 // Android's whitelist relies on symbolic links (ex. /sdcard is whitelisted
467 // and commonly a symbolic link), thus do not check absolute paths.
468 return IsAccessAllowed(original_path, profile_path_);
469#else
470 return (IsAccessAllowed(original_path, profile_path_) &&
471 IsAccessAllowed(absolute_path, profile_path_));
472#endif
satorux7c5360582017-01-27 07:24:29473}
474
475// static
476bool ChromeNetworkDelegate::IsAccessAllowed(
477 const base::FilePath& path,
478 const base::FilePath& profile_path) {
479#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
480 return true;
481#else
482
satoruxca8b5642017-06-08 04:26:35483 std::vector<base::FilePath> whitelist;
satorux7c5360582017-01-27 07:24:29484#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05485 // Use a whitelist to only allow access to files residing in the list of
486 // directories below.
satoruxca8b5642017-06-08 04:26:35487 static const base::FilePath::CharType* const kLocalAccessWhiteList[] = {
[email protected]4c219e22012-05-05 19:41:04488 "/home/chronos/user/Downloads",
489 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42490 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04491 "/media",
492 "/opt/oem",
493 "/usr/share/chromeos-assets",
[email protected]4c219e22012-05-05 19:41:04494 "/var/log",
495 };
[email protected]ae015e12013-11-04 19:11:33496
satoruxca8b5642017-06-08 04:26:35497 base::FilePath temp_dir;
498 if (PathService::Get(base::DIR_TEMP, &temp_dir))
499 whitelist.push_back(temp_dir);
500
[email protected]9e733282014-06-18 16:56:55501 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
502 // the profile path ("/home/chronos/user' is a hard link to current primary
503 // logged in profile.) For the support of multi-profile sessions, we are
504 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
505 // access.
satorux7c5360582017-01-27 07:24:29506 if (!profile_path.empty()) {
507 const base::FilePath downloads = profile_path.AppendASCII("Downloads");
satoruxca8b5642017-06-08 04:26:35508 whitelist.push_back(downloads);
satorux7c5360582017-01-27 07:24:29509 const base::FilePath webrtc_logs = profile_path.AppendASCII("WebRTC Logs");
satoruxca8b5642017-06-08 04:26:35510 whitelist.push_back(webrtc_logs);
[email protected]ae015e12013-11-04 19:11:33511 }
[email protected]d8e4f132012-09-06 04:28:05512#elif defined(OS_ANDROID)
513 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45514 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05515 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
516 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04517 return true;
518
[email protected]d8e4f132012-09-06 04:28:05519 // Whitelist of other allowed directories.
satoruxca8b5642017-06-08 04:26:35520 static const base::FilePath::CharType* const kLocalAccessWhiteList[] = {
521 "/sdcard", "/mnt/sdcard",
[email protected]d8e4f132012-09-06 04:28:05522 };
523#endif
524
satoruxca8b5642017-06-08 04:26:35525 for (const auto* whitelisted_path : kLocalAccessWhiteList)
526 whitelist.push_back(base::FilePath(whitelisted_path));
527
528 for (const auto& whitelisted_path : whitelist) {
[email protected]650b2d52013-02-10 03:41:45529 // base::FilePath::operator== should probably handle trailing separators.
satoruxca8b5642017-06-08 04:26:35530 if (whitelisted_path == path.StripTrailingSeparators() ||
531 whitelisted_path.IsParent(path)) {
[email protected]4c219e22012-05-05 19:41:04532 return true;
533 }
534 }
[email protected]d8e4f132012-09-06 04:28:05535
[email protected]288538482012-09-06 21:09:35536 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04537 return false;
[email protected]d8e4f132012-09-06 04:28:05538#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04539}
[email protected]a1d4ab072012-06-07 13:21:15540
satoruxd18e61a2017-06-08 06:38:46541// static
542void ChromeNetworkDelegate::EnableAccessToAllFilesForTesting(bool enabled) {
543 g_access_to_all_files_enabled = enabled;
544}
545
[email protected]e6d017652013-05-17 18:01:40546bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
547 const GURL& url,
548 const GURL& first_party_for_cookies) const {
ljusten98684cb72016-10-11 13:26:18549 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02550 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40551 return false;
552
falken71691402017-01-27 03:38:00553 return !cookie_settings_->IsCookieAccessAllowed(url, first_party_for_cookies);
[email protected]e6d017652013-05-17 18:01:40554}
555
estark7625d812015-10-12 20:10:41556bool ChromeNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
mkwst0513c9d2015-04-01 05:53:15557 return experimental_web_platform_features_enabled_;
558}
559
jochen0e3b3a62014-09-16 18:31:23560bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
561 const net::URLRequest& request,
562 const GURL& target_url,
563 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27564 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23565 return true;
566}
sclittlea133de02015-11-10 23:54:21567
juliatuttlefcf47202017-05-23 15:53:02568bool ChromeNetworkDelegate::OnCanQueueReportingReport(
569 const url::Origin& origin) const {
570 if (!cookie_settings_)
571 return true;
572
573 return cookie_settings_->IsCookieAccessAllowed(origin.GetURL(),
574 origin.GetURL());
575}
576
577bool ChromeNetworkDelegate::OnCanSendReportingReport(
578 const url::Origin& origin) const {
579 if (!cookie_settings_)
580 return true;
581
582 return cookie_settings_->IsCookieAccessAllowed(origin.GetURL(),
583 origin.GetURL());
584}
585
586bool ChromeNetworkDelegate::OnCanSetReportingClient(
587 const url::Origin& origin,
588 const GURL& endpoint) const {
589 if (!cookie_settings_)
590 return true;
591
592 return cookie_settings_->IsCookieAccessAllowed(endpoint, origin.GetURL());
593}
594
595bool ChromeNetworkDelegate::OnCanUseReportingClient(
596 const url::Origin& origin,
597 const GURL& endpoint) const {
598 if (!cookie_settings_)
599 return true;
600
601 return cookie_settings_->IsCookieAccessAllowed(endpoint, origin.GetURL());
602}
603
sclittlea133de02015-11-10 23:54:21604void ChromeNetworkDelegate::ReportDataUsageStats(net::URLRequest* request,
605 int64_t tx_bytes,
606 int64_t rx_bytes) {
607 if (!data_use_aggregator_)
608 return;
609
610 if (is_data_usage_off_the_record_) {
611 data_use_aggregator_->ReportOffTheRecordDataUse(tx_bytes, rx_bytes);
612 return;
613 }
614
615 data_use_aggregator_->ReportDataUse(request, tx_bytes, rx_bytes);
616}