blob: 82c7d49aa67cdbb0cbbfae5981327f51e7846399 [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"
[email protected]7a299a92012-10-24 23:54:5020#include "base/metrics/histogram.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]184799df2014-07-22 16:03:2435#include "chrome/browser/net/safe_search_util.h"
[email protected]6baff0b52012-03-06 01:30:1836#include "chrome/browser/profiles/profile_manager.h"
afakhry05015032015-08-14 01:09:5637#include "chrome/browser/task_management/task_manager_interface.h"
fgorski708eadb2015-11-12 01:00:4338#include "chrome/common/chrome_constants.h"
sievers2f1e8112015-12-04 18:43:5639#include "chrome/common/features.h"
[email protected]0a8db0d2011-04-13 15:15:4040#include "chrome/common/pref_names.h"
drogerb1716972015-06-30 09:04:0941#include "components/content_settings/core/browser/cookie_settings.h"
sclittleae932be2015-10-08 20:53:5042#include "components/data_usage/core/data_use_aggregator.h"
[email protected]45de676a2014-03-18 23:52:0243#include "components/domain_reliability/monitor.h"
brettw03e61962016-03-15 06:27:0444#include "components/policy/core/browser/url_blacklist_manager.h"
brettwb1fc1b82016-02-02 00:19:0845#include "components/prefs/pref_member.h"
46#include "components/prefs/pref_service.h"
[email protected]c38831a12011-10-28 12:44:4947#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5648#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3349#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3550#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1551#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3452#include "content/public/common/process_type.h"
[email protected]82b42302011-04-20 16:28:1653#include "net/base/host_port_pair.h"
sclittle3c16159e2014-12-15 19:53:1754#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1455#include "net/base/net_errors.h"
[email protected]5b9bc352012-07-18 13:13:3456#include "net/cookies/canonical_cookie.h"
57#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4458#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1659#include "net/http/http_response_headers.h"
ellyjonesd84033d2015-02-12 19:20:3460#include "net/http/http_status_code.h"
eroman87c53d62015-04-02 06:51:0761#include "net/log/net_log.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.
129void RecordNetworkErrorHistograms(const net::URLRequest* request) {
130 if (request->url().SchemeIs("http")) {
131 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
132 std::abs(request->status().error()));
133
134 if (request->load_flags() & net::LOAD_MAIN_FRAME) {
135 UMA_HISTOGRAM_SPARSE_SLOWLY(
136 "Net.HttpRequestCompletionErrorCodes.MainFrame",
137 std::abs(request->status().error()));
138 }
139 }
140}
141
ellyjonesd84033d2015-02-12 19:20:34142// Returns whether |request| is likely to be eligible for delta-encoding.
143// This is only a rough approximation right now, based on MIME type.
144bool CanRequestBeDeltaEncoded(const net::URLRequest* request) {
145 struct {
146 const char *prefix;
147 const char *suffix;
148 } kEligibleMasks[] = {
149 // All text/ types are eligible, even if not displayable.
150 { "text/", NULL },
151 // JSON (application/json and application/*+json) is eligible.
152 { "application/", "json" },
153 // Javascript is eligible.
154 { "application/", "javascript" },
155 // XML (application/xml and application/*+xml) is eligible.
156 { "application/", "xml" },
157 };
ellyjonesd84033d2015-02-12 19:20:34158
159 std::string mime_type;
160 request->GetMimeType(&mime_type);
161
162 for (size_t i = 0; i < arraysize(kEligibleMasks); i++) {
163 const char *prefix = kEligibleMasks[i].prefix;
164 const char *suffix = kEligibleMasks[i].suffix;
brettw66d1b81b2015-07-06 19:29:40165 if (prefix &&
166 !base::StartsWith(mime_type, prefix, base::CompareCase::SENSITIVE))
ellyjonesd84033d2015-02-12 19:20:34167 continue;
brettw66d1b81b2015-07-06 19:29:40168 if (suffix &&
169 !base::EndsWith(mime_type, suffix, base::CompareCase::SENSITIVE))
ellyjonesd84033d2015-02-12 19:20:34170 continue;
171 return true;
172 }
173 return false;
174}
175
176// Returns whether |request| was issued by a renderer process, as opposed to
177// the browser process or a plugin process.
178bool IsRendererInitiatedRequest(const net::URLRequest* request) {
179 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
180 return info && info->GetProcessType() == content::PROCESS_TYPE_RENDERER;
181}
182
183// Uploads UMA histograms for delta encoding eligibility. This method can only
184// be safely called after the network stack has called both OnStarted and
185// OnCompleted, since it needs the received response content length and the
186// response headers.
187void RecordCacheStateStats(const net::URLRequest* request) {
188 net::HttpRequestHeaders request_headers;
189 if (!request->GetFullRequestHeaders(&request_headers)) {
190 // GetFullRequestHeaders is guaranteed to succeed if OnResponseStarted() has
191 // been called on |request|, so if GetFullRequestHeaders() fails,
192 // RecordCacheStateStats must have been called before
193 // OnResponseStarted().
194 return;
195 }
196
197 if (!IsRendererInitiatedRequest(request)) {
198 // Ignore browser-initiated requests. These are internal requests like safe
199 // browsing and sync, and so on. Some of these could be eligible for
200 // delta-encoding, but to be conservative this function ignores all of them.
201 return;
202 }
203
204 const int kCacheAffectingFlags = net::LOAD_BYPASS_CACHE |
205 net::LOAD_DISABLE_CACHE |
206 net::LOAD_PREFERRING_CACHE;
207
208 if (request->load_flags() & kCacheAffectingFlags) {
209 // Ignore requests with cache-affecting flags, which would otherwise mess up
210 // these stats.
211 return;
212 }
213
214 enum {
215 CACHE_STATE_FROM_CACHE,
216 CACHE_STATE_STILL_VALID,
217 CACHE_STATE_NO_LONGER_VALID,
218 CACHE_STATE_NO_ENTRY,
219 CACHE_STATE_MAX,
220 } state = CACHE_STATE_NO_ENTRY;
221 bool had_cache_headers =
222 request_headers.HasHeader(net::HttpRequestHeaders::kIfModifiedSince) ||
223 request_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch) ||
224 request_headers.HasHeader(net::HttpRequestHeaders::kIfRange);
225 if (request->was_cached() && !had_cache_headers) {
226 // Entry was served directly from cache.
227 state = CACHE_STATE_FROM_CACHE;
228 } else if (request->was_cached() && had_cache_headers) {
229 // Expired entry was present in cache, and server responded with NOT
230 // MODIFIED, indicating the expired entry is still valid.
231 state = CACHE_STATE_STILL_VALID;
232 } else if (!request->was_cached() && had_cache_headers) {
233 // Expired entry was present in cache, and server responded with something
234 // other than NOT MODIFIED, indicating the entry is no longer valid.
235 state = CACHE_STATE_NO_LONGER_VALID;
236 } else if (!request->was_cached() && !had_cache_headers) {
237 // Neither |was_cached| nor |had_cache_headers|, so there's no local cache
238 // entry for this content at all.
239 state = CACHE_STATE_NO_ENTRY;
240 }
241
242 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.AllRequests", state,
243 CACHE_STATE_MAX);
244 if (CanRequestBeDeltaEncoded(request)) {
245 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.EncodeableRequests", state,
246 CACHE_STATE_MAX);
247 }
248
avi6846aef2015-12-26 01:09:38249 int64_t size = request->received_response_content_length();
ellyjonesd84033d2015-02-12 19:20:34250 if (size >= 0 && state == CACHE_STATE_NO_LONGER_VALID) {
251 UMA_HISTOGRAM_COUNTS("Net.CacheState.AllBytes", size);
252 if (CanRequestBeDeltaEncoded(request)) {
253 UMA_HISTOGRAM_COUNTS("Net.CacheState.EncodeableBytes", size);
254 }
255 }
256}
257
[email protected]d05ef99c2011-02-01 21:38:16258} // namespace
[email protected]ac039522010-06-15 16:39:44259
[email protected]0651b812011-02-24 00:22:50260ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10261 extensions::EventRouterForwarder* event_router,
gayane0b46091c2016-04-07 21:01:05262 BooleanPrefMember* enable_referrers,
263 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder)
[email protected]4a2b6232014-06-19 08:44:14264 : profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59265 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48266 enable_do_not_track_(NULL),
267 force_google_safe_search_(NULL),
treib4e26f6652014-12-01 14:34:18268 force_youtube_safety_mode_(NULL),
igorcov193d8ad32016-05-27 11:03:21269 allowed_domains_for_apps_(nullptr),
[email protected]a09159a2012-11-29 12:51:48270 url_blacklist_manager_(NULL),
[email protected]45de676a2014-03-18 23:52:02271 domain_reliability_monitor_(NULL),
gayane0b46091c2016-04-07 21:01:05272 data_use_measurement_(metrics_data_use_forwarder),
mkwst0513c9d2015-04-01 05:53:15273 experimental_web_platform_features_enabled_(
gayane0b46091c2016-04-07 21:01:05274 base::CommandLine::ForCurrentProcess()->HasSwitch(
275 switches::kEnableExperimentalWebPlatformFeatures)),
sclittleae932be2015-10-08 20:53:50276 data_use_aggregator_(nullptr),
277 is_data_usage_off_the_record_(true) {
[email protected]0a8db0d2011-04-13 15:15:40278 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14279 extensions_delegate_.reset(
280 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50281}
282
[email protected]ac039522010-06-15 16:39:44283ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
284
[email protected]a09159a2012-11-29 12:51:48285void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20286 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14287 extensions_delegate_->set_extension_info_map(extension_info_map);
288}
289
290void ChromeNetworkDelegate::set_profile(void* profile) {
291 profile_ = profile;
292 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48293}
294
295void ChromeNetworkDelegate::set_cookie_settings(
drogerb1716972015-06-30 09:04:09296 content_settings::CookieSettings* cookie_settings) {
[email protected]a09159a2012-11-29 12:51:48297 cookie_settings_ = cookie_settings;
298}
299
sclittleae932be2015-10-08 20:53:50300void ChromeNetworkDelegate::set_data_use_aggregator(
301 data_usage::DataUseAggregator* data_use_aggregator,
302 bool is_data_usage_off_the_record) {
303 data_use_aggregator_ = data_use_aggregator;
304 is_data_usage_off_the_record_ = is_data_usage_off_the_record;
305}
306
[email protected]c4a7df82012-08-09 22:48:46307// static
[email protected]9d8cfb682012-09-13 16:48:04308void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40309 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04310 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33311 BooleanPrefMember* force_google_safe_search,
treib4e26f6652014-12-01 14:34:18312 BooleanPrefMember* force_youtube_safety_mode,
igorcov193d8ad32016-05-27 11:03:21313 StringPrefMember* allowed_domains_for_apps,
[email protected]0a8db0d2011-04-13 15:15:40314 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02315 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02316 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43317 enable_referrers->MoveToThread(
thestig529ad8a2016-07-08 20:30:12318 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04319 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02320 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43321 enable_do_not_track->MoveToThread(
thestig529ad8a2016-07-08 20:30:12322 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04323 }
[email protected]d1208ba32012-11-08 11:10:33324 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18325 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33326 force_google_safe_search->MoveToThread(
thestig529ad8a2016-07-08 20:30:12327 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
[email protected]d1208ba32012-11-08 11:10:33328 }
treib4e26f6652014-12-01 14:34:18329 if (force_youtube_safety_mode) {
330 force_youtube_safety_mode->Init(prefs::kForceYouTubeSafetyMode,
331 pref_service);
332 force_youtube_safety_mode->MoveToThread(
thestig529ad8a2016-07-08 20:30:12333 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
treib4e26f6652014-12-01 14:34:18334 }
igorcov193d8ad32016-05-27 11:03:21335 if (allowed_domains_for_apps) {
336 allowed_domains_for_apps->Init(prefs::kAllowedDomainsForApps, pref_service);
337 allowed_domains_for_apps->MoveToThread(
thestig529ad8a2016-07-08 20:30:12338 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO));
igorcov193d8ad32016-05-27 11:03:21339 }
[email protected]0a8db0d2011-04-13 15:15:40340}
341
[email protected]4c219e22012-05-05 19:41:04342// static
343void ChromeNetworkDelegate::AllowAccessToAllFiles() {
344 g_allow_file_access_ = true;
345}
346
[email protected]4875ba12011-03-30 22:31:51347int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12348 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47349 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12350 GURL* new_url) {
mmenke376b8782015-05-05 04:41:30351 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
352 tracked_objects::ScopedTracker tracking_profile1(
353 FROM_HERE_WITH_EXPLICIT_FUNCTION(
354 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest"));
355
[email protected]6a5f77c32011-09-04 19:19:59356 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
357 // blocked. However, an extension might redirect the request to another URL,
358 // which is not blocked.
mmenkef1aa9072015-05-18 16:21:04359
360 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
[email protected]cb85f8e52014-04-08 11:44:40361 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
mmenkef1aa9072015-05-18 16:21:04362 if (info && content::IsResourceTypeFrame(info->GetResourceType()) &&
363 url_blacklist_manager_ &&
364 url_blacklist_manager_->ShouldBlockRequestForFrame(
365 request->url(), &error)) {
[email protected]6a5f77c32011-09-04 19:19:59366 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59367 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26368 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
369 net::NetLog::StringCallback("url",
370 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40371 return error;
[email protected]6a5f77c32011-09-04 19:19:59372 }
[email protected]6a5f77c32011-09-04 19:19:59373
mmenke376b8782015-05-05 04:41:30374 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
375 tracked_objects::ScopedTracker tracking_profile2(
376 FROM_HERE_WITH_EXPLICIT_FUNCTION(
377 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 2"));
378
[email protected]4a2b6232014-06-19 08:44:14379 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18380
[email protected]0a8db0d2011-04-13 15:15:40381 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13382 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04383 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
384 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33385
mmenke376b8782015-05-05 04:41:30386 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
387 tracked_objects::ScopedTracker tracking_profile3(
388 FROM_HERE_WITH_EXPLICIT_FUNCTION(
389 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 3"));
390
treib4e26f6652014-12-01 14:34:18391 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18392 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33393
394 net::CompletionCallback wrapped_callback = callback;
395 if (force_safe_search) {
396 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
397 callback,
398 base::Unretained(request),
399 base::Unretained(new_url));
400 }
401
[email protected]4a2b6232014-06-19 08:44:14402 int rv = extensions_delegate_->OnBeforeURLRequest(
403 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33404
mmenke376b8782015-05-05 04:41:30405 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
406 tracked_objects::ScopedTracker tracking_profile4(
407 FROM_HERE_WITH_EXPLICIT_FUNCTION(
408 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 4"));
409
[email protected]d1208ba32012-11-08 11:10:33410 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24411 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33412
mmenke376b8782015-05-05 04:41:30413 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
414 tracked_objects::ScopedTracker tracking_profile5(
415 FROM_HERE_WITH_EXPLICIT_FUNCTION(
416 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 5"));
417
igorcov193d8ad32016-05-27 11:03:21418 if (allowed_domains_for_apps_ &&
419 !allowed_domains_for_apps_->GetValue().empty() &&
420 request->url().DomainIs("google.com")) {
421 request->SetExtraRequestHeaderByName("X-GoogApps-Allowed-Domains",
422 allowed_domains_for_apps_->GetValue(),
423 true);
424 }
425
[email protected]d1208ba32012-11-08 11:10:33426 return rv;
[email protected]d05ef99c2011-02-01 21:38:16427}
428
ryansturm2343cb62016-06-15 01:09:00429int ChromeNetworkDelegate::OnBeforeStartTransaction(
[email protected]636eccd2011-06-28 12:28:01430 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47431 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12432 net::HttpRequestHeaders* headers) {
treib2b188052015-04-17 12:32:50433 if (force_youtube_safety_mode_ && force_youtube_safety_mode_->GetValue())
[email protected]184799df2014-07-22 16:03:24434 safe_search_util::ForceYouTubeSafetyMode(request, headers);
435
ryansturm2343cb62016-06-15 01:09:00436 return extensions_delegate_->OnBeforeStartTransaction(request, callback,
437 headers);
[email protected]ac039522010-06-15 16:39:44438}
[email protected]8202d0c2011-02-23 08:31:14439
ryansturm2343cb62016-06-15 01:09:00440void ChromeNetworkDelegate::OnStartTransaction(
[email protected]5796dc942011-07-14 19:26:10441 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15442 const net::HttpRequestHeaders& headers) {
ryansturm2343cb62016-06-15 01:09:00443 extensions_delegate_->OnStartTransaction(request, headers);
[email protected]82b42302011-04-20 16:28:16444}
445
[email protected]ea8141e2011-10-05 13:12:51446int ChromeNetworkDelegate::OnHeadersReceived(
447 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47448 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32449 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16450 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
451 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14452 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16453 request,
454 callback,
455 original_response_headers,
456 override_response_headers,
457 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51458}
459
[email protected]31b2e5f2011-04-20 16:58:32460void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
461 const GURL& new_location) {
amohammadkhan092adb22015-09-11 21:08:49462// Recording data use of request on redirects.
amohammadkhan092adb22015-09-11 21:08:49463 data_use_measurement_.ReportDataUseUMA(request);
[email protected]45de676a2014-03-18 23:52:02464 if (domain_reliability_monitor_)
465 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14466 extensions_delegate_->OnBeforeRedirect(request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32467}
468
469
[email protected]8202d0c2011-02-23 08:31:14470void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14471 extensions_delegate_->OnResponseStarted(request);
[email protected]8202d0c2011-02-23 08:31:14472}
473
sclittlea133de02015-11-10 23:54:21474void ChromeNetworkDelegate::OnNetworkBytesReceived(net::URLRequest* request,
475 int64_t bytes_received) {
[email protected]44879ed2012-04-06 01:11:02476#if defined(ENABLE_TASK_MANAGER)
sclittlece72c482015-08-24 20:20:59477 // Note: Currently, OnNetworkBytesReceived is only implemented for HTTP jobs,
478 // not FTP or other types, so those kinds of bytes will not be reported here.
sclittlea133de02015-11-10 23:54:21479 task_management::TaskManagerInterface::OnRawBytesRead(*request,
sclittlece72c482015-08-24 20:20:59480 bytes_received);
[email protected]44879ed2012-04-06 01:11:02481#endif // defined(ENABLE_TASK_MANAGER)
sclittleae932be2015-10-08 20:53:50482
sclittlea133de02015-11-10 23:54:21483 ReportDataUsageStats(request, 0 /* tx_bytes */, bytes_received);
sclittleae932be2015-10-08 20:53:50484}
485
sclittlea133de02015-11-10 23:54:21486void ChromeNetworkDelegate::OnNetworkBytesSent(net::URLRequest* request,
sclittleae932be2015-10-08 20:53:50487 int64_t bytes_sent) {
sclittlea133de02015-11-10 23:54:21488 ReportDataUsageStats(request, bytes_sent, 0 /* rx_bytes */);
[email protected]8523ba52011-05-22 19:00:58489}
490
[email protected]9045b8822012-01-13 20:35:35491void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
492 bool started) {
amohammadkhan092adb22015-09-11 21:08:49493 // TODO(amohammadkhan): Verify that there is no double recording in data use
494 // of redirected requests.
495 data_use_measurement_.ReportDataUseUMA(request);
sclittle3c16159e2014-12-15 19:53:17496 RecordNetworkErrorHistograms(request);
ellyjonesd84033d2015-02-12 19:20:34497 if (started) {
498 // Only call in for requests that were started, to obey the precondition
499 // that RecordCacheStateStats can only be called on requests for which
500 // OnResponseStarted was called.
501 RecordCacheStateStats(request);
502 }
sclittle3c16159e2014-12-15 19:53:17503
[email protected]2756a8e2012-09-07 18:24:29504 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
sievers2f1e8112015-12-04 18:43:56505#if BUILDFLAG(ANDROID_JAVA_UI)
rajendrant1e1eff12016-07-08 20:11:15506 precache::UpdatePrecacheMetricsAndState(request, profile_);
sievers2f1e8112015-12-04 18:43:56507#endif // BUILDFLAG(ANDROID_JAVA_UI)
[email protected]4a2b6232014-06-19 08:44:14508 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01509 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
510 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]4a2b6232014-06-19 08:44:14511 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01512 } else {
513 NOTREACHED();
[email protected]48944382011-04-23 13:28:16514 }
[email protected]45de676a2014-03-18 23:52:02515 if (domain_reliability_monitor_)
516 domain_reliability_monitor_->OnCompleted(request, started);
ttuttle05ae3f342015-07-13 17:38:35517 RecordRequestSourceBandwidth(request, started);
[email protected]4a2b6232014-06-19 08:44:14518 extensions_delegate_->ForwardProxyErrors(request);
519 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14520}
[email protected]4b50cb52011-03-10 00:29:37521
[email protected]4875ba12011-03-30 22:31:51522void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14523 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51524}
525
[email protected]82a37672011-05-03 12:02:41526void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09527 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14528 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41529}
[email protected]7efc582d2011-08-03 20:46:35530
[email protected]c2911d72011-10-03 22:16:36531net::NetworkDelegate::AuthRequiredResponse
532ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35533 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36534 const net::AuthChallengeInfo& auth_info,
535 const AuthCallback& callback,
536 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14537 return extensions_delegate_->OnAuthRequired(
538 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35539}
[email protected]9c8ae8c2012-03-09 13:13:35540
[email protected]4c219e22012-05-05 19:41:04541bool ChromeNetworkDelegate::OnCanGetCookies(
542 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35543 const net::CookieList& cookie_list) {
544 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02545 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35546 return true;
547
548 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04549 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35550
551 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16552 int render_frame_id = -1;
553 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
554 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35555 BrowserThread::PostTask(
556 BrowserThread::UI, FROM_HERE,
557 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16558 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04559 request.url(), request.first_party_for_cookies(),
davidbenf47d1912015-03-14 00:24:24560 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35561 }
562
563 return allow;
564}
565
[email protected]4c219e22012-05-05 19:41:04566bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
567 const std::string& cookie_line,
568 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35569 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02570 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35571 return true;
572
573 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04574 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35575
[email protected]9c8ae8c2012-03-09 13:13:35576 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16577 int render_frame_id = -1;
578 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
579 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35580 BrowserThread::PostTask(
581 BrowserThread::UI, FROM_HERE,
582 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16583 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04584 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43585 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35586 }
587
588 return allow;
589}
[email protected]4c219e22012-05-05 19:41:04590
591bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45592 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04593 if (g_allow_file_access_)
594 return true;
595
[email protected]d8e4f132012-09-06 04:28:05596#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
597 return true;
598#else
[email protected]4c219e22012-05-05 19:41:04599#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05600 // If we're running Chrome for ChromeOS on Linux, we want to allow file
601 // access.
[email protected]49c4cf852013-09-27 19:28:24602 if (!base::SysInfo::IsRunningOnChromeOS() ||
avi556c05022014-12-22 23:31:43603 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05604 return true;
[email protected]288538482012-09-06 21:09:35605 }
[email protected]d8e4f132012-09-06 04:28:05606
607 // Use a whitelist to only allow access to files residing in the list of
608 // directories below.
[email protected]4c219e22012-05-05 19:41:04609 static const char* const kLocalAccessWhiteList[] = {
610 "/home/chronos/user/Downloads",
611 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42612 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04613 "/media",
614 "/opt/oem",
615 "/usr/share/chromeos-assets",
616 "/tmp",
617 "/var/log",
618 };
[email protected]ae015e12013-11-04 19:11:33619
[email protected]9e733282014-06-18 16:56:55620 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
621 // the profile path ("/home/chronos/user' is a hard link to current primary
622 // logged in profile.) For the support of multi-profile sessions, we are
623 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
624 // access.
[email protected]ae015e12013-11-04 19:11:33625 if (!profile_path_.empty()) {
626 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
627 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
628 return true;
[email protected]9e733282014-06-18 16:56:55629 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
630 if (webrtc_logs == path.StripTrailingSeparators() ||
631 webrtc_logs.IsParent(path)) {
632 return true;
633 }
[email protected]ae015e12013-11-04 19:11:33634 }
[email protected]d8e4f132012-09-06 04:28:05635#elif defined(OS_ANDROID)
636 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45637 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05638 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
639 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04640 return true;
641
fgorski708eadb2015-11-12 01:00:43642 // Allow to load offline pages, which are stored in the $PROFILE_PATH/Offline
643 // Pages/archives.
644 if (!profile_path_.empty()) {
645 const base::FilePath offline_page_archives =
weiran97bba892016-06-09 17:51:46646 profile_path_.Append(chrome::kOfflinePageArchivesDirname);
fgorski708eadb2015-11-12 01:00:43647 if (offline_page_archives.IsParent(path))
648 return true;
649 }
650
[email protected]d8e4f132012-09-06 04:28:05651 // Whitelist of other allowed directories.
652 static const char* const kLocalAccessWhiteList[] = {
653 "/sdcard",
654 "/mnt/sdcard",
655 };
656#endif
657
[email protected]4c219e22012-05-05 19:41:04658 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45659 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
660 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04661 if (white_listed_path == path.StripTrailingSeparators() ||
662 white_listed_path.IsParent(path)) {
663 return true;
664 }
665 }
[email protected]d8e4f132012-09-06 04:28:05666
[email protected]288538482012-09-06 21:09:35667 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04668 return false;
[email protected]d8e4f132012-09-06 04:28:05669#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04670}
[email protected]a1d4ab072012-06-07 13:21:15671
[email protected]e6d017652013-05-17 18:01:40672bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
673 const GURL& url,
674 const GURL& first_party_for_cookies) const {
675 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02676 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40677 return false;
678
679 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
680 url, first_party_for_cookies);
681 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
682 url, first_party_for_cookies);
683 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
684 return privacy_mode;
685}
686
estark7625d812015-10-12 20:10:41687bool ChromeNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
mkwst0513c9d2015-04-01 05:53:15688 return experimental_web_platform_features_enabled_;
689}
690
jww79aceda2015-12-07 01:56:34691bool ChromeNetworkDelegate::OnAreStrictSecureCookiesEnabled() const {
692 const std::string enforce_strict_secure_group =
693 base::FieldTrialList::FindFullName("StrictSecureCookies");
694 return experimental_web_platform_features_enabled_ ||
695 base::StartsWith(enforce_strict_secure_group, "Enabled",
696 base::CompareCase::INSENSITIVE_ASCII);
697}
698
jochen0e3b3a62014-09-16 18:31:23699bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
700 const net::URLRequest& request,
701 const GURL& target_url,
702 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27703 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23704 return true;
705}
sclittlea133de02015-11-10 23:54:21706
707void ChromeNetworkDelegate::ReportDataUsageStats(net::URLRequest* request,
708 int64_t tx_bytes,
709 int64_t rx_bytes) {
710 if (!data_use_aggregator_)
711 return;
712
713 if (is_data_usage_off_the_record_) {
714 data_use_aggregator_->ReportOffTheRecordDataUse(tx_bytes, rx_bytes);
715 return;
716 }
717
718 data_use_aggregator_->ReportDataUse(request, tx_bytes, rx_bytes);
719}