blob: 340d903ff7478560e27052b908b27a5a55def1f6 [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"
jww79aceda2015-12-07 01:56:3419#include "base/metrics/field_trial.h"
asvitkineaa060312016-09-01 22:44:1320#include "base/metrics/histogram_macros.h"
sclittle3c16159e2014-12-15 19:53:1721#include "base/metrics/sparse_histogram.h"
jochen0e3b3a62014-09-16 18:31:2322#include "base/metrics/user_metrics.h"
[email protected]d8e4f132012-09-06 04:28:0523#include "base/path_service.h"
mmenke376b8782015-05-05 04:41:3024#include "base/profiler/scoped_tracker.h"
[email protected]3ea1b182013-02-08 22:38:4125#include "base/strings/string_number_conversions.h"
ellyjonesd84033d2015-02-12 19:20:3426#include "base/strings/string_util.h"
[email protected]069c49162013-09-11 20:46:3527#include "base/time/time.h"
avi6846aef2015-12-26 01:09:3828#include "build/build_config.h"
[email protected]6baff0b52012-03-06 01:30:1829#include "chrome/browser/browser_process.h"
drogerb1716972015-06-30 09:04:0930#include "chrome/browser/content_settings/cookie_settings_factory.h"
[email protected]9c8ae8c2012-03-09 13:13:3531#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5832#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]4a2b6232014-06-19 08:44:1433#include "chrome/browser/net/chrome_extensions_network_delegate.h"
ttuttle05ae3f342015-07-13 17:38:3534#include "chrome/browser/net/request_source_bandwidth_histograms.h"
[email protected]6baff0b52012-03-06 01:30:1835#include "chrome/browser/profiles/profile_manager.h"
avi24d693f2016-08-06 18:03:5236#include "chrome/browser/task_manager/task_manager_interface.h"
sievers2f1e8112015-12-04 18:43:5637#include "chrome/common/features.h"
[email protected]0a8db0d2011-04-13 15:15:4038#include "chrome/common/pref_names.h"
drogerb1716972015-06-30 09:04:0939#include "components/content_settings/core/browser/cookie_settings.h"
sclittleae932be2015-10-08 20:53:5040#include "components/data_usage/core/data_use_aggregator.h"
[email protected]45de676a2014-03-18 23:52:0241#include "components/domain_reliability/monitor.h"
brettw03e61962016-03-15 06:27:0442#include "components/policy/core/browser/url_blacklist_manager.h"
brettwb1fc1b82016-02-02 00:19:0843#include "components/prefs/pref_member.h"
44#include "components/prefs/pref_service.h"
[email protected]c38831a12011-10-28 12:44:4945#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5646#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3347#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3548#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1549#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3450#include "content/public/common/process_type.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
sievers2f1e8112015-12-04 18:43:5664#if BUILDFLAG(ANDROID_JAVA_UI)
[email protected]4a2b6232014-06-19 08:44:1465#include "chrome/browser/io_thread.h"
rajendrant1e1eff12016-07-08 20:11:1566#include "chrome/browser/precache/precache_util.h"
[email protected]4a2b6232014-06-19 08:44:1467#endif
68
[email protected]4c219e22012-05-05 19:41:0469#if defined(OS_CHROMEOS)
[email protected]49c4cf852013-09-27 19:28:2470#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3571#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0472#endif
73
[email protected]84b7a552014-07-19 04:52:0674#if defined(ENABLE_EXTENSIONS)
75#include "extensions/common/constants.h"
76#endif
77
[email protected]631bb742011-11-02 11:29:3978using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1179using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2580using content::ResourceRequestInfo;
[email protected]7491ad02014-07-05 19:10:0781using content::ResourceType;
[email protected]631bb742011-11-02 11:29:3982
[email protected]d8e4f132012-09-06 04:28:0583// By default we don't allow access to all file:// urls on ChromeOS and
84// Android.
85#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0486bool ChromeNetworkDelegate::g_allow_file_access_ = false;
87#else
88bool ChromeNetworkDelegate::g_allow_file_access_ = true;
89#endif
90
[email protected]d05ef99c2011-02-01 21:38:1691namespace {
92
[email protected]9d8cfb682012-09-13 16:48:0493const char kDNTHeader[] = "DNT";
94
[email protected]d1208ba32012-11-08 11:10:3395// Gets called when the extensions finish work on the URL. If the extensions
96// did not do a redirect (so |new_url| is empty) then we enforce the
97// SafeSearch parameters. Otherwise we will get called again after the
98// redirect and we enforce SafeSearch then.
99void ForceGoogleSafeSearchCallbackWrapper(
100 const net::CompletionCallback& callback,
101 net::URLRequest* request,
102 GURL* new_url,
103 int rv) {
104 if (rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24105 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33106 callback.Run(rv);
107}
108
jochen652275372015-01-16 11:35:27109void ReportInvalidReferrerSendOnUI() {
jochen0e3b3a62014-09-16 18:31:23110 base::RecordAction(
111 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
jochen652275372015-01-16 11:35:27112}
113
114void ReportInvalidReferrerSend(const GURL& target_url,
115 const GURL& referrer_url) {
jochen4c874d22015-04-13 14:01:04116 LOG(ERROR) << "Cancelling request to " << target_url
117 << " with invalid referrer " << referrer_url;
jochen652275372015-01-16 11:35:27118 // Record information to help debug http://crbug.com/422871
119 if (!target_url.SchemeIsHTTPOrHTTPS())
120 return;
121 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
122 base::Bind(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41123 base::debug::DumpWithoutCrashing();
jochen4c874d22015-04-13 14:01:04124 NOTREACHED();
jochen0e3b3a62014-09-16 18:31:23125}
126
sclittle3c16159e2014-12-15 19:53:17127// Record network errors that HTTP requests complete with, including OK and
128// ABORTED.
maksim.sisov1b83bb72016-10-07 06:07:23129void RecordNetworkErrorHistograms(const net::URLRequest* request,
130 int net_error) {
sclittle3c16159e2014-12-15 19:53:17131 if (request->url().SchemeIs("http")) {
132 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
maksim.sisov1b83bb72016-10-07 06:07:23133 std::abs(net_error));
sclittle3c16159e2014-12-15 19:53:17134
rdsmith4de1fcf2016-08-30 05:35:21135 if (request->load_flags() & net::LOAD_MAIN_FRAME_DEPRECATED) {
sclittle3c16159e2014-12-15 19:53:17136 UMA_HISTOGRAM_SPARSE_SLOWLY(
maksim.sisov1b83bb72016-10-07 06:07:23137 "Net.HttpRequestCompletionErrorCodes.MainFrame", std::abs(net_error));
sclittle3c16159e2014-12-15 19:53:17138 }
139 }
140}
141
[email protected]d05ef99c2011-02-01 21:38:16142} // namespace
[email protected]ac039522010-06-15 16:39:44143
[email protected]0651b812011-02-24 00:22:50144ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10145 extensions::EventRouterForwarder* event_router,
gayane0b46091c2016-04-07 21:01:05146 BooleanPrefMember* enable_referrers,
147 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder)
ljusten98684cb72016-10-11 13:26:18148 : profile_(nullptr),
[email protected]6a5f77c32011-09-04 19:19:59149 enable_referrers_(enable_referrers),
ljusten98684cb72016-10-11 13:26:18150 enable_do_not_track_(nullptr),
151 force_google_safe_search_(nullptr),
152 force_youtube_restrict_(nullptr),
igorcov193d8ad32016-05-27 11:03:21153 allowed_domains_for_apps_(nullptr),
ljusten98684cb72016-10-11 13:26:18154 url_blacklist_manager_(nullptr),
155 domain_reliability_monitor_(nullptr),
gayane0b46091c2016-04-07 21:01:05156 data_use_measurement_(metrics_data_use_forwarder),
mkwst0513c9d2015-04-01 05:53:15157 experimental_web_platform_features_enabled_(
gayane0b46091c2016-04-07 21:01:05158 base::CommandLine::ForCurrentProcess()->HasSwitch(
159 switches::kEnableExperimentalWebPlatformFeatures)),
sclittleae932be2015-10-08 20:53:50160 data_use_aggregator_(nullptr),
161 is_data_usage_off_the_record_(true) {
[email protected]0a8db0d2011-04-13 15:15:40162 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14163 extensions_delegate_.reset(
164 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50165}
166
[email protected]ac039522010-06-15 16:39:44167ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
168
[email protected]a09159a2012-11-29 12:51:48169void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20170 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14171 extensions_delegate_->set_extension_info_map(extension_info_map);
172}
173
174void ChromeNetworkDelegate::set_profile(void* profile) {
175 profile_ = profile;
176 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48177}
178
179void ChromeNetworkDelegate::set_cookie_settings(
drogerb1716972015-06-30 09:04:09180 content_settings::CookieSettings* cookie_settings) {
[email protected]a09159a2012-11-29 12:51:48181 cookie_settings_ = cookie_settings;
182}
183
sclittleae932be2015-10-08 20:53:50184void ChromeNetworkDelegate::set_data_use_aggregator(
185 data_usage::DataUseAggregator* data_use_aggregator,
186 bool is_data_usage_off_the_record) {
187 data_use_aggregator_ = data_use_aggregator;
188 is_data_usage_off_the_record_ = is_data_usage_off_the_record;
189}
190
[email protected]c4a7df82012-08-09 22:48:46191// static
[email protected]9d8cfb682012-09-13 16:48:04192void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40193 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04194 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33195 BooleanPrefMember* force_google_safe_search,
ljusten98684cb72016-10-11 13:26:18196 IntegerPrefMember* force_youtube_restrict,
igorcov193d8ad32016-05-27 11:03:21197 StringPrefMember* allowed_domains_for_apps,
[email protected]0a8db0d2011-04-13 15:15:40198 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02199 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02200 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43201 enable_referrers->MoveToThread(
thestig529ad8a2016-07-08 20:30:12202 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04203 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02204 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43205 enable_do_not_track->MoveToThread(
thestig529ad8a2016-07-08 20:30:12206 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04207 }
[email protected]d1208ba32012-11-08 11:10:33208 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18209 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33210 force_google_safe_search->MoveToThread(
thestig529ad8a2016-07-08 20:30:12211 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]d1208ba32012-11-08 11:10:33212 }
ljusten98684cb72016-10-11 13:26:18213 if (force_youtube_restrict) {
214 force_youtube_restrict->Init(prefs::kForceYouTubeRestrict, pref_service);
215 force_youtube_restrict->MoveToThread(
thestig529ad8a2016-07-08 20:30:12216 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
treib4e26f6652014-12-01 14:34:18217 }
igorcov193d8ad32016-05-27 11:03:21218 if (allowed_domains_for_apps) {
219 allowed_domains_for_apps->Init(prefs::kAllowedDomainsForApps, pref_service);
220 allowed_domains_for_apps->MoveToThread(
thestig529ad8a2016-07-08 20:30:12221 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
igorcov193d8ad32016-05-27 11:03:21222 }
[email protected]0a8db0d2011-04-13 15:15:40223}
224
[email protected]4c219e22012-05-05 19:41:04225// static
226void ChromeNetworkDelegate::AllowAccessToAllFiles() {
227 g_allow_file_access_ = true;
228}
229
[email protected]4875ba12011-03-30 22:31:51230int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12231 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47232 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12233 GURL* new_url) {
mmenke376b8782015-05-05 04:41:30234 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
235 tracked_objects::ScopedTracker tracking_profile1(
236 FROM_HERE_WITH_EXPLICIT_FUNCTION(
237 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest"));
238
rajendrantda6c2ca52016-09-27 06:13:18239 data_use_measurement_.OnBeforeURLRequest(request);
240
[email protected]6a5f77c32011-09-04 19:19:59241 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
242 // blocked. However, an extension might redirect the request to another URL,
243 // which is not blocked.
mmenkef1aa9072015-05-18 16:21:04244
245 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
[email protected]cb85f8e52014-04-08 11:44:40246 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
mmenkef1aa9072015-05-18 16:21:04247 if (info && content::IsResourceTypeFrame(info->GetResourceType()) &&
248 url_blacklist_manager_ &&
249 url_blacklist_manager_->ShouldBlockRequestForFrame(
250 request->url(), &error)) {
[email protected]6a5f77c32011-09-04 19:19:59251 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59252 request->net_log().AddEvent(
mikecirone8b85c432016-09-08 19:11:00253 net::NetLogEventType::CHROME_POLICY_ABORTED_REQUEST,
[email protected]2fa08912012-06-14 20:56:26254 net::NetLog::StringCallback("url",
255 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40256 return error;
[email protected]6a5f77c32011-09-04 19:19:59257 }
[email protected]6a5f77c32011-09-04 19:19:59258
mmenke376b8782015-05-05 04:41:30259 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
260 tracked_objects::ScopedTracker tracking_profile2(
261 FROM_HERE_WITH_EXPLICIT_FUNCTION(
262 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 2"));
263
[email protected]4a2b6232014-06-19 08:44:14264 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18265
[email protected]0a8db0d2011-04-13 15:15:40266 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13267 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04268 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
269 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33270
mmenke376b8782015-05-05 04:41:30271 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
272 tracked_objects::ScopedTracker tracking_profile3(
273 FROM_HERE_WITH_EXPLICIT_FUNCTION(
274 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 3"));
275
treib4e26f6652014-12-01 14:34:18276 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18277 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33278
279 net::CompletionCallback wrapped_callback = callback;
280 if (force_safe_search) {
281 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
282 callback,
283 base::Unretained(request),
284 base::Unretained(new_url));
285 }
286
[email protected]4a2b6232014-06-19 08:44:14287 int rv = extensions_delegate_->OnBeforeURLRequest(
288 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33289
mmenke376b8782015-05-05 04:41:30290 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
291 tracked_objects::ScopedTracker tracking_profile4(
292 FROM_HERE_WITH_EXPLICIT_FUNCTION(
293 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 4"));
294
[email protected]d1208ba32012-11-08 11:10:33295 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24296 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33297
mmenke376b8782015-05-05 04:41:30298 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
299 tracked_objects::ScopedTracker tracking_profile5(
300 FROM_HERE_WITH_EXPLICIT_FUNCTION(
301 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 5"));
302
igorcov193d8ad32016-05-27 11:03:21303 if (allowed_domains_for_apps_ &&
304 !allowed_domains_for_apps_->GetValue().empty() &&
305 request->url().DomainIs("google.com")) {
306 request->SetExtraRequestHeaderByName("X-GoogApps-Allowed-Domains",
307 allowed_domains_for_apps_->GetValue(),
308 true);
309 }
310
[email protected]d1208ba32012-11-08 11:10:33311 return rv;
[email protected]d05ef99c2011-02-01 21:38:16312}
313
ryansturm2343cb62016-06-15 01:09:00314int ChromeNetworkDelegate::OnBeforeStartTransaction(
[email protected]636eccd2011-06-28 12:28:01315 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47316 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12317 net::HttpRequestHeaders* headers) {
ljusten98684cb72016-10-11 13:26:18318 if (force_youtube_restrict_) {
319 int value = force_youtube_restrict_->GetValue();
320 static_assert(safe_search_util::YOUTUBE_RESTRICT_OFF == 0,
321 "OFF must be first");
322 if (value > safe_search_util::YOUTUBE_RESTRICT_OFF &&
323 value < safe_search_util::YOUTUBE_RESTRICT_COUNT) {
324 safe_search_util::ForceYouTubeRestrict(request, headers,
325 static_cast<safe_search_util::YouTubeRestrictMode>(value));
326 }
327 }
[email protected]184799df2014-07-22 16:03:24328
ryansturm2343cb62016-06-15 01:09:00329 return extensions_delegate_->OnBeforeStartTransaction(request, callback,
330 headers);
[email protected]ac039522010-06-15 16:39:44331}
[email protected]8202d0c2011-02-23 08:31:14332
ryansturm2343cb62016-06-15 01:09:00333void ChromeNetworkDelegate::OnStartTransaction(
[email protected]5796dc942011-07-14 19:26:10334 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15335 const net::HttpRequestHeaders& headers) {
ryansturm2343cb62016-06-15 01:09:00336 extensions_delegate_->OnStartTransaction(request, headers);
[email protected]82b42302011-04-20 16:28:16337}
338
[email protected]ea8141e2011-10-05 13:12:51339int ChromeNetworkDelegate::OnHeadersReceived(
340 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47341 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32342 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16343 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
344 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14345 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16346 request,
347 callback,
348 original_response_headers,
349 override_response_headers,
350 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51351}
352
[email protected]31b2e5f2011-04-20 16:58:32353void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
354 const GURL& new_location) {
tbansal21318e32016-08-23 19:09:59355 data_use_measurement_.OnBeforeRedirect(*request, 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);
[email protected]31b2e5f2011-04-20 16:58:32359}
360
maksim.sisov1b83bb72016-10-07 06:07:23361void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request,
362 int net_error) {
363 extensions_delegate_->OnResponseStarted(request, net_error);
[email protected]8202d0c2011-02-23 08:31:14364}
365
sclittlea133de02015-11-10 23:54:21366void ChromeNetworkDelegate::OnNetworkBytesReceived(net::URLRequest* request,
367 int64_t bytes_received) {
[email protected]44879ed2012-04-06 01:11:02368#if defined(ENABLE_TASK_MANAGER)
sclittlece72c482015-08-24 20:20:59369 // Note: Currently, OnNetworkBytesReceived is only implemented for HTTP jobs,
370 // not FTP or other types, so those kinds of bytes will not be reported here.
avi24d693f2016-08-06 18:03:52371 task_manager::TaskManagerInterface::OnRawBytesRead(*request, bytes_received);
[email protected]44879ed2012-04-06 01:11:02372#endif // defined(ENABLE_TASK_MANAGER)
sclittleae932be2015-10-08 20:53:50373
sclittlea133de02015-11-10 23:54:21374 ReportDataUsageStats(request, 0 /* tx_bytes */, bytes_received);
tbansal086197ba2016-09-22 19:13:08375 data_use_measurement_.OnNetworkBytesReceived(*request, 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) {
sclittlea133de02015-11-10 23:54:21380 ReportDataUsageStats(request, bytes_sent, 0 /* rx_bytes */);
tbansal086197ba2016-09-22 19:13:08381 data_use_measurement_.OnNetworkBytesSent(*request, bytes_sent);
[email protected]8523ba52011-05-22 19:00:58382}
383
[email protected]9045b8822012-01-13 20:35:35384void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
maksim.sisov1b83bb72016-10-07 06:07:23385 bool started,
386 int net_error) {
387 DCHECK_NE(net::ERR_IO_PENDING, net_error);
sclittle3c16159e2014-12-15 19:53:17388
maksim.sisov1b83bb72016-10-07 06:07:23389 // TODO(amohammadkhan): Verify that there is no double recording in data use
390 // of redirected requests.
391 data_use_measurement_.OnCompleted(*request, started);
392 RecordNetworkErrorHistograms(request, net_error);
393
394 if (net_error == net::OK) {
sievers2f1e8112015-12-04 18:43:56395#if BUILDFLAG(ANDROID_JAVA_UI)
rajendrant1e1eff12016-07-08 20:11:15396 precache::UpdatePrecacheMetricsAndState(request, profile_);
sievers2f1e8112015-12-04 18:43:56397#endif // BUILDFLAG(ANDROID_JAVA_UI)
[email protected]48944382011-04-23 13:28:16398 }
maksim.sisov1b83bb72016-10-07 06:07:23399
400 extensions_delegate_->OnCompleted(request, started, net_error);
[email protected]45de676a2014-03-18 23:52:02401 if (domain_reliability_monitor_)
402 domain_reliability_monitor_->OnCompleted(request, started);
ttuttle05ae3f342015-07-13 17:38:35403 RecordRequestSourceBandwidth(request, started);
maksim.sisov1b83bb72016-10-07 06:07:23404 extensions_delegate_->ForwardProxyErrors(request, net_error);
[email protected]4a2b6232014-06-19 08:44:14405 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14406}
[email protected]4b50cb52011-03-10 00:29:37407
[email protected]4875ba12011-03-30 22:31:51408void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14409 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51410}
411
[email protected]82a37672011-05-03 12:02:41412void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09413 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14414 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41415}
[email protected]7efc582d2011-08-03 20:46:35416
[email protected]c2911d72011-10-03 22:16:36417net::NetworkDelegate::AuthRequiredResponse
418ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35419 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36420 const net::AuthChallengeInfo& auth_info,
421 const AuthCallback& callback,
422 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14423 return extensions_delegate_->OnAuthRequired(
424 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35425}
[email protected]9c8ae8c2012-03-09 13:13:35426
[email protected]4c219e22012-05-05 19:41:04427bool ChromeNetworkDelegate::OnCanGetCookies(
428 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35429 const net::CookieList& cookie_list) {
ljusten98684cb72016-10-11 13:26:18430 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02431 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35432 return true;
433
434 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04435 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35436
jam092d3be2016-09-27 15:56:36437 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
438 if (info) {
[email protected]9c8ae8c2012-03-09 13:13:35439 BrowserThread::PostTask(
440 BrowserThread::UI, FROM_HERE,
441 base::Bind(&TabSpecificContentSettings::CookiesRead,
jam092d3be2016-09-27 15:56:36442 info->GetWebContentsGetterForRequest(),
[email protected]4c219e22012-05-05 19:41:04443 request.url(), request.first_party_for_cookies(),
davidbenf47d1912015-03-14 00:24:24444 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35445 }
446
447 return allow;
448}
449
[email protected]4c219e22012-05-05 19:41:04450bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
451 const std::string& cookie_line,
452 net::CookieOptions* options) {
ljusten98684cb72016-10-11 13:26:18453 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02454 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35455 return true;
456
457 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04458 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35459
jam092d3be2016-09-27 15:56:36460 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
461 if (info) {
[email protected]9c8ae8c2012-03-09 13:13:35462 BrowserThread::PostTask(
463 BrowserThread::UI, FROM_HERE,
464 base::Bind(&TabSpecificContentSettings::CookieChanged,
jam092d3be2016-09-27 15:56:36465 info->GetWebContentsGetterForRequest(),
[email protected]4c219e22012-05-05 19:41:04466 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43467 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35468 }
469
470 return allow;
471}
[email protected]4c219e22012-05-05 19:41:04472
473bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45474 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04475 if (g_allow_file_access_)
476 return true;
477
[email protected]d8e4f132012-09-06 04:28:05478#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
479 return true;
480#else
[email protected]4c219e22012-05-05 19:41:04481#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05482 // If we're running Chrome for ChromeOS on Linux, we want to allow file
483 // access.
[email protected]49c4cf852013-09-27 19:28:24484 if (!base::SysInfo::IsRunningOnChromeOS() ||
avi556c05022014-12-22 23:31:43485 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05486 return true;
[email protected]288538482012-09-06 21:09:35487 }
[email protected]d8e4f132012-09-06 04:28:05488
489 // Use a whitelist to only allow access to files residing in the list of
490 // directories below.
[email protected]4c219e22012-05-05 19:41:04491 static const char* const kLocalAccessWhiteList[] = {
492 "/home/chronos/user/Downloads",
493 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42494 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04495 "/media",
496 "/opt/oem",
497 "/usr/share/chromeos-assets",
498 "/tmp",
499 "/var/log",
500 };
[email protected]ae015e12013-11-04 19:11:33501
[email protected]9e733282014-06-18 16:56:55502 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
503 // the profile path ("/home/chronos/user' is a hard link to current primary
504 // logged in profile.) For the support of multi-profile sessions, we are
505 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
506 // access.
[email protected]ae015e12013-11-04 19:11:33507 if (!profile_path_.empty()) {
508 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
509 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
510 return true;
[email protected]9e733282014-06-18 16:56:55511 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
512 if (webrtc_logs == path.StripTrailingSeparators() ||
513 webrtc_logs.IsParent(path)) {
514 return true;
515 }
[email protected]ae015e12013-11-04 19:11:33516 }
[email protected]d8e4f132012-09-06 04:28:05517#elif defined(OS_ANDROID)
518 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45519 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05520 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
521 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04522 return true;
523
[email protected]d8e4f132012-09-06 04:28:05524 // Whitelist of other allowed directories.
525 static const char* const kLocalAccessWhiteList[] = {
526 "/sdcard",
527 "/mnt/sdcard",
528 };
529#endif
530
[email protected]4c219e22012-05-05 19:41:04531 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45532 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
533 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04534 if (white_listed_path == path.StripTrailingSeparators() ||
535 white_listed_path.IsParent(path)) {
536 return true;
537 }
538 }
[email protected]d8e4f132012-09-06 04:28:05539
[email protected]288538482012-09-06 21:09:35540 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04541 return false;
[email protected]d8e4f132012-09-06 04:28:05542#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04543}
[email protected]a1d4ab072012-06-07 13:21:15544
[email protected]e6d017652013-05-17 18:01:40545bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
546 const GURL& url,
547 const GURL& first_party_for_cookies) const {
ljusten98684cb72016-10-11 13:26:18548 // nullptr during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02549 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40550 return false;
551
552 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
553 url, first_party_for_cookies);
554 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
555 url, first_party_for_cookies);
556 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
557 return privacy_mode;
558}
559
estark7625d812015-10-12 20:10:41560bool ChromeNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
mkwst0513c9d2015-04-01 05:53:15561 return experimental_web_platform_features_enabled_;
562}
563
jww79aceda2015-12-07 01:56:34564bool ChromeNetworkDelegate::OnAreStrictSecureCookiesEnabled() const {
565 const std::string enforce_strict_secure_group =
566 base::FieldTrialList::FindFullName("StrictSecureCookies");
567 return experimental_web_platform_features_enabled_ ||
568 base::StartsWith(enforce_strict_secure_group, "Enabled",
569 base::CompareCase::INSENSITIVE_ASCII);
570}
571
jochen0e3b3a62014-09-16 18:31:23572bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
573 const net::URLRequest& request,
574 const GURL& target_url,
575 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27576 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23577 return true;
578}
sclittlea133de02015-11-10 23:54:21579
580void ChromeNetworkDelegate::ReportDataUsageStats(net::URLRequest* request,
581 int64_t tx_bytes,
582 int64_t rx_bytes) {
583 if (!data_use_aggregator_)
584 return;
585
586 if (is_data_usage_off_the_record_) {
587 data_use_aggregator_->ReportOffTheRecordDataUse(tx_bytes, rx_bytes);
588 return;
589 }
590
591 data_use_aggregator_->ReportDataUse(request, tx_bytes, rx_bytes);
592}