blob: 9348cef93a3ee3ca9c641bd4eb4e504d006de7cd [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
[email protected]eac11e12013-03-19 22:04:327#include <stdlib.h>
8
[email protected]d1208ba32012-11-08 11:10:339#include <vector>
10
[email protected]d8e4f132012-09-06 04:28:0511#include "base/base_paths.h"
mkwst0513c9d2015-04-01 05:53:1512#include "base/command_line.h"
jochenb93dba3c2014-11-11 05:28:4113#include "base/debug/alias.h"
jochenb5680e62014-10-13 08:43:4114#include "base/debug/dump_without_crashing.h"
jochenb93dba3c2014-11-11 05:28:4115#include "base/debug/stack_trace.h"
[email protected]7a299a92012-10-24 23:54:5016#include "base/logging.h"
17#include "base/metrics/histogram.h"
sclittle3c16159e2014-12-15 19:53:1718#include "base/metrics/sparse_histogram.h"
jochen0e3b3a62014-09-16 18:31:2319#include "base/metrics/user_metrics.h"
[email protected]d8e4f132012-09-06 04:28:0520#include "base/path_service.h"
[email protected]1ab137b2013-03-21 03:33:1821#include "base/prefs/pref_member.h"
[email protected]3853a4c2013-02-11 17:15:5722#include "base/prefs/pref_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"
[email protected]6baff0b52012-03-06 01:30:1827#include "chrome/browser/browser_process.h"
[email protected]9c8ae8c2012-03-09 13:13:3528#include "chrome/browser/content_settings/cookie_settings.h"
29#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5830#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]4a2b6232014-06-19 08:44:1431#include "chrome/browser/net/chrome_extensions_network_delegate.h"
[email protected]f53b4802012-12-20 17:04:2332#include "chrome/browser/net/connect_interceptor.h"
[email protected]184799df2014-07-22 16:03:2433#include "chrome/browser/net/safe_search_util.h"
[email protected]6baff0b52012-03-06 01:30:1834#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5835#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4036#include "chrome/common/pref_names.h"
[email protected]45de676a2014-03-18 23:52:0237#include "components/domain_reliability/monitor.h"
[email protected]c38831a12011-10-28 12:44:4938#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5639#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3340#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3541#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1542#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3443#include "content/public/common/process_type.h"
[email protected]82b42302011-04-20 16:28:1644#include "net/base/host_port_pair.h"
sclittle3c16159e2014-12-15 19:53:1745#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1446#include "net/base/net_errors.h"
[email protected]5b9bc352012-07-18 13:13:3447#include "net/cookies/canonical_cookie.h"
48#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4449#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1650#include "net/http/http_response_headers.h"
ellyjonesd84033d2015-02-12 19:20:3451#include "net/http/http_status_code.h"
eroman87c53d62015-04-02 06:51:0752#include "net/log/net_log.h"
[email protected]d05ef99c2011-02-01 21:38:1653#include "net/url_request/url_request.h"
[email protected]d05ef99c2011-02-01 21:38:1654
[email protected]4a2b6232014-06-19 08:44:1455#if defined(OS_ANDROID)
56#include "chrome/browser/io_thread.h"
57#include "components/precache/content/precache_manager.h"
58#include "components/precache/content/precache_manager_factory.h"
59#endif
60
[email protected]4c219e22012-05-05 19:41:0461#if defined(OS_CHROMEOS)
[email protected]49c4cf852013-09-27 19:28:2462#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3563#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0464#endif
65
[email protected]3e598ff12011-09-06 11:22:3466#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]f522afa2014-01-08 15:36:3667#include "components/policy/core/browser/url_blacklist_manager.h"
[email protected]3e598ff12011-09-06 11:22:3468#endif
69
[email protected]84b7a552014-07-19 04:52:0670#if defined(ENABLE_EXTENSIONS)
71#include "extensions/common/constants.h"
72#endif
73
[email protected]631bb742011-11-02 11:29:3974using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1175using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2576using content::ResourceRequestInfo;
[email protected]7491ad02014-07-05 19:10:0777using content::ResourceType;
[email protected]631bb742011-11-02 11:29:3978
[email protected]d8e4f132012-09-06 04:28:0579// By default we don't allow access to all file:// urls on ChromeOS and
80// Android.
81#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0482bool ChromeNetworkDelegate::g_allow_file_access_ = false;
83#else
84bool ChromeNetworkDelegate::g_allow_file_access_ = true;
85#endif
86
[email protected]84b7a552014-07-19 04:52:0687#if defined(ENABLE_EXTENSIONS)
[email protected]c4a7df82012-08-09 22:48:4688// This remains false unless the --disable-extensions-http-throttling
89// flag is passed to the browser.
90bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
[email protected]84b7a552014-07-19 04:52:0691#endif
[email protected]c4a7df82012-08-09 22:48:4692
[email protected]d05ef99c2011-02-01 21:38:1693namespace {
94
[email protected]9d8cfb682012-09-13 16:48:0495const char kDNTHeader[] = "DNT";
96
[email protected]d1208ba32012-11-08 11:10:3397// Gets called when the extensions finish work on the URL. If the extensions
98// did not do a redirect (so |new_url| is empty) then we enforce the
99// SafeSearch parameters. Otherwise we will get called again after the
100// redirect and we enforce SafeSearch then.
101void ForceGoogleSafeSearchCallbackWrapper(
102 const net::CompletionCallback& callback,
103 net::URLRequest* request,
104 GURL* new_url,
105 int rv) {
106 if (rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24107 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33108 callback.Run(rv);
109}
110
[email protected]35558812013-12-18 21:58:36111#if defined(OS_ANDROID)
112void RecordPrecacheStatsOnUIThread(const GURL& url,
113 const base::Time& fetch_time, int64 size,
114 bool was_cached, void* profile_id) {
anujk.sharma2e02ce162015-04-29 23:10:02115 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]35558812013-12-18 21:58:36116
117 Profile* profile = reinterpret_cast<Profile*>(profile_id);
118 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) {
119 return;
120 }
121
122 precache::PrecacheManager* precache_manager =
123 precache::PrecacheManagerFactory::GetForBrowserContext(profile);
tanviraumi387f6332014-08-29 19:04:36124 if (!precache_manager || !precache_manager->IsPrecachingAllowed()) {
125 // |precache_manager| could be NULL if the profile is off the record.
[email protected]35558812013-12-18 21:58:36126 return;
127 }
128
129 precache_manager->RecordStatsForFetch(url, fetch_time, size, was_cached);
130}
131#endif // defined(OS_ANDROID)
132
jochen652275372015-01-16 11:35:27133void ReportInvalidReferrerSendOnUI() {
jochen0e3b3a62014-09-16 18:31:23134 base::RecordAction(
135 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
jochen652275372015-01-16 11:35:27136}
137
138void ReportInvalidReferrerSend(const GURL& target_url,
139 const GURL& referrer_url) {
jochen4c874d22015-04-13 14:01:04140 LOG(ERROR) << "Cancelling request to " << target_url
141 << " with invalid referrer " << referrer_url;
jochen652275372015-01-16 11:35:27142 // Record information to help debug http://crbug.com/422871
143 if (!target_url.SchemeIsHTTPOrHTTPS())
144 return;
145 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
146 base::Bind(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41147 base::debug::DumpWithoutCrashing();
jochen4c874d22015-04-13 14:01:04148 NOTREACHED();
jochen0e3b3a62014-09-16 18:31:23149}
150
sclittle3c16159e2014-12-15 19:53:17151// Record network errors that HTTP requests complete with, including OK and
152// ABORTED.
153void RecordNetworkErrorHistograms(const net::URLRequest* request) {
154 if (request->url().SchemeIs("http")) {
155 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
156 std::abs(request->status().error()));
157
158 if (request->load_flags() & net::LOAD_MAIN_FRAME) {
159 UMA_HISTOGRAM_SPARSE_SLOWLY(
160 "Net.HttpRequestCompletionErrorCodes.MainFrame",
161 std::abs(request->status().error()));
162 }
163 }
164}
165
ellyjonesd84033d2015-02-12 19:20:34166// Returns whether |request| is likely to be eligible for delta-encoding.
167// This is only a rough approximation right now, based on MIME type.
168bool CanRequestBeDeltaEncoded(const net::URLRequest* request) {
169 struct {
170 const char *prefix;
171 const char *suffix;
172 } kEligibleMasks[] = {
173 // All text/ types are eligible, even if not displayable.
174 { "text/", NULL },
175 // JSON (application/json and application/*+json) is eligible.
176 { "application/", "json" },
177 // Javascript is eligible.
178 { "application/", "javascript" },
179 // XML (application/xml and application/*+xml) is eligible.
180 { "application/", "xml" },
181 };
182 const bool kCaseSensitive = true;
183
184 std::string mime_type;
185 request->GetMimeType(&mime_type);
186
187 for (size_t i = 0; i < arraysize(kEligibleMasks); i++) {
188 const char *prefix = kEligibleMasks[i].prefix;
189 const char *suffix = kEligibleMasks[i].suffix;
190 if (prefix && !StartsWithASCII(mime_type, prefix, kCaseSensitive))
191 continue;
192 if (suffix && !EndsWith(mime_type, suffix, kCaseSensitive))
193 continue;
194 return true;
195 }
196 return false;
197}
198
199// Returns whether |request| was issued by a renderer process, as opposed to
200// the browser process or a plugin process.
201bool IsRendererInitiatedRequest(const net::URLRequest* request) {
202 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
203 return info && info->GetProcessType() == content::PROCESS_TYPE_RENDERER;
204}
205
206// Uploads UMA histograms for delta encoding eligibility. This method can only
207// be safely called after the network stack has called both OnStarted and
208// OnCompleted, since it needs the received response content length and the
209// response headers.
210void RecordCacheStateStats(const net::URLRequest* request) {
211 net::HttpRequestHeaders request_headers;
212 if (!request->GetFullRequestHeaders(&request_headers)) {
213 // GetFullRequestHeaders is guaranteed to succeed if OnResponseStarted() has
214 // been called on |request|, so if GetFullRequestHeaders() fails,
215 // RecordCacheStateStats must have been called before
216 // OnResponseStarted().
217 return;
218 }
219
220 if (!IsRendererInitiatedRequest(request)) {
221 // Ignore browser-initiated requests. These are internal requests like safe
222 // browsing and sync, and so on. Some of these could be eligible for
223 // delta-encoding, but to be conservative this function ignores all of them.
224 return;
225 }
226
227 const int kCacheAffectingFlags = net::LOAD_BYPASS_CACHE |
228 net::LOAD_DISABLE_CACHE |
229 net::LOAD_PREFERRING_CACHE;
230
231 if (request->load_flags() & kCacheAffectingFlags) {
232 // Ignore requests with cache-affecting flags, which would otherwise mess up
233 // these stats.
234 return;
235 }
236
237 enum {
238 CACHE_STATE_FROM_CACHE,
239 CACHE_STATE_STILL_VALID,
240 CACHE_STATE_NO_LONGER_VALID,
241 CACHE_STATE_NO_ENTRY,
242 CACHE_STATE_MAX,
243 } state = CACHE_STATE_NO_ENTRY;
244 bool had_cache_headers =
245 request_headers.HasHeader(net::HttpRequestHeaders::kIfModifiedSince) ||
246 request_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch) ||
247 request_headers.HasHeader(net::HttpRequestHeaders::kIfRange);
248 if (request->was_cached() && !had_cache_headers) {
249 // Entry was served directly from cache.
250 state = CACHE_STATE_FROM_CACHE;
251 } else if (request->was_cached() && had_cache_headers) {
252 // Expired entry was present in cache, and server responded with NOT
253 // MODIFIED, indicating the expired entry is still valid.
254 state = CACHE_STATE_STILL_VALID;
255 } else if (!request->was_cached() && had_cache_headers) {
256 // Expired entry was present in cache, and server responded with something
257 // other than NOT MODIFIED, indicating the entry is no longer valid.
258 state = CACHE_STATE_NO_LONGER_VALID;
259 } else if (!request->was_cached() && !had_cache_headers) {
260 // Neither |was_cached| nor |had_cache_headers|, so there's no local cache
261 // entry for this content at all.
262 state = CACHE_STATE_NO_ENTRY;
263 }
264
265 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.AllRequests", state,
266 CACHE_STATE_MAX);
267 if (CanRequestBeDeltaEncoded(request)) {
268 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.EncodeableRequests", state,
269 CACHE_STATE_MAX);
270 }
271
272 int64 size = request->received_response_content_length();
273 if (size >= 0 && state == CACHE_STATE_NO_LONGER_VALID) {
274 UMA_HISTOGRAM_COUNTS("Net.CacheState.AllBytes", size);
275 if (CanRequestBeDeltaEncoded(request)) {
276 UMA_HISTOGRAM_COUNTS("Net.CacheState.EncodeableBytes", size);
277 }
278 }
279}
280
[email protected]d05ef99c2011-02-01 21:38:16281} // namespace
[email protected]ac039522010-06-15 16:39:44282
[email protected]0651b812011-02-24 00:22:50283ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10284 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48285 BooleanPrefMember* enable_referrers)
[email protected]4a2b6232014-06-19 08:44:14286 : profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59287 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48288 enable_do_not_track_(NULL),
[email protected]a09159a2012-11-29 12:51:48289 force_google_safe_search_(NULL),
treib4e26f6652014-12-01 14:34:18290 force_youtube_safety_mode_(NULL),
[email protected]4a2b6232014-06-19 08:44:14291#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]a09159a2012-11-29 12:51:48292 url_blacklist_manager_(NULL),
[email protected]4a2b6232014-06-19 08:44:14293#endif
[email protected]45de676a2014-03-18 23:52:02294 domain_reliability_monitor_(NULL),
mkwst0513c9d2015-04-01 05:53:15295 experimental_web_platform_features_enabled_(
296 base::CommandLine::ForCurrentProcess()->HasSwitch(
thestig397accd2015-04-04 00:03:04297 switches::kEnableExperimentalWebPlatformFeatures)) {
[email protected]0a8db0d2011-04-13 15:15:40298 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14299 extensions_delegate_.reset(
300 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50301}
302
[email protected]ac039522010-06-15 16:39:44303ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
304
[email protected]a09159a2012-11-29 12:51:48305void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20306 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14307 extensions_delegate_->set_extension_info_map(extension_info_map);
308}
309
310void ChromeNetworkDelegate::set_profile(void* profile) {
311 profile_ = profile;
312 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48313}
314
315void ChromeNetworkDelegate::set_cookie_settings(
316 CookieSettings* cookie_settings) {
317 cookie_settings_ = cookie_settings;
318}
319
[email protected]f53b4802012-12-20 17:04:23320void ChromeNetworkDelegate::set_predictor(
321 chrome_browser_net::Predictor* predictor) {
322 connect_interceptor_.reset(
323 new chrome_browser_net::ConnectInterceptor(predictor));
324}
325
[email protected]c4a7df82012-08-09 22:48:46326// static
[email protected]84b7a552014-07-19 04:52:06327#if defined(ENABLE_EXTENSIONS)
[email protected]a1d4ab072012-06-07 13:21:15328void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46329 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15330}
[email protected]84b7a552014-07-19 04:52:06331#endif
[email protected]a1d4ab072012-06-07 13:21:15332
[email protected]0a8db0d2011-04-13 15:15:40333// static
[email protected]9d8cfb682012-09-13 16:48:04334void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40335 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04336 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33337 BooleanPrefMember* force_google_safe_search,
treib4e26f6652014-12-01 14:34:18338 BooleanPrefMember* force_youtube_safety_mode,
[email protected]0a8db0d2011-04-13 15:15:40339 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02340 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02341 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43342 enable_referrers->MoveToThread(
343 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04344 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02345 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43346 enable_do_not_track->MoveToThread(
347 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04348 }
[email protected]d1208ba32012-11-08 11:10:33349 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18350 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33351 force_google_safe_search->MoveToThread(
352 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
353 }
treib4e26f6652014-12-01 14:34:18354 if (force_youtube_safety_mode) {
355 force_youtube_safety_mode->Init(prefs::kForceYouTubeSafetyMode,
356 pref_service);
357 force_youtube_safety_mode->MoveToThread(
358 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
359 }
[email protected]0a8db0d2011-04-13 15:15:40360}
361
[email protected]4c219e22012-05-05 19:41:04362// static
363void ChromeNetworkDelegate::AllowAccessToAllFiles() {
364 g_allow_file_access_ = true;
365}
366
[email protected]4875ba12011-03-30 22:31:51367int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12368 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47369 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12370 GURL* new_url) {
mmenke376b8782015-05-05 04:41:30371 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
372 tracked_objects::ScopedTracker tracking_profile1(
373 FROM_HERE_WITH_EXPLICIT_FUNCTION(
374 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest"));
375
[email protected]3e598ff12011-09-06 11:22:34376#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59377 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
378 // blocked. However, an extension might redirect the request to another URL,
379 // which is not blocked.
[email protected]cb85f8e52014-04-08 11:44:40380 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]6a5f77c32011-09-04 19:19:59381 if (url_blacklist_manager_ &&
[email protected]cb85f8e52014-04-08 11:44:40382 url_blacklist_manager_->IsRequestBlocked(*request, &error)) {
[email protected]6a5f77c32011-09-04 19:19:59383 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59384 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26385 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
386 net::NetLog::StringCallback("url",
387 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40388 return error;
[email protected]6a5f77c32011-09-04 19:19:59389 }
[email protected]3e598ff12011-09-06 11:22:34390#endif
[email protected]6a5f77c32011-09-04 19:19:59391
mmenke376b8782015-05-05 04:41:30392 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
393 tracked_objects::ScopedTracker tracking_profile2(
394 FROM_HERE_WITH_EXPLICIT_FUNCTION(
395 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 2"));
396
[email protected]4a2b6232014-06-19 08:44:14397 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18398
[email protected]0a8db0d2011-04-13 15:15:40399 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13400 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04401 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
402 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33403
mmenke376b8782015-05-05 04:41:30404 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
405 tracked_objects::ScopedTracker tracking_profile3(
406 FROM_HERE_WITH_EXPLICIT_FUNCTION(
407 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 3"));
408
treib4e26f6652014-12-01 14:34:18409 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18410 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33411
412 net::CompletionCallback wrapped_callback = callback;
413 if (force_safe_search) {
414 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
415 callback,
416 base::Unretained(request),
417 base::Unretained(new_url));
418 }
419
[email protected]4a2b6232014-06-19 08:44:14420 int rv = extensions_delegate_->OnBeforeURLRequest(
421 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33422
mmenke376b8782015-05-05 04:41:30423 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
424 tracked_objects::ScopedTracker tracking_profile4(
425 FROM_HERE_WITH_EXPLICIT_FUNCTION(
426 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 4"));
427
[email protected]d1208ba32012-11-08 11:10:33428 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24429 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33430
mmenke376b8782015-05-05 04:41:30431 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
432 tracked_objects::ScopedTracker tracking_profile5(
433 FROM_HERE_WITH_EXPLICIT_FUNCTION(
434 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 5"));
435
[email protected]f53b4802012-12-20 17:04:23436 if (connect_interceptor_)
437 connect_interceptor_->WitnessURLRequest(request);
438
[email protected]d1208ba32012-11-08 11:10:33439 return rv;
[email protected]d05ef99c2011-02-01 21:38:16440}
441
[email protected]4875ba12011-03-30 22:31:51442int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01443 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47444 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12445 net::HttpRequestHeaders* headers) {
treib2b188052015-04-17 12:32:50446 if (force_youtube_safety_mode_ && force_youtube_safety_mode_->GetValue())
[email protected]184799df2014-07-22 16:03:24447 safe_search_util::ForceYouTubeSafetyMode(request, headers);
448
[email protected]4a2b6232014-06-19 08:44:14449 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
[email protected]ac039522010-06-15 16:39:44450}
[email protected]8202d0c2011-02-23 08:31:14451
[email protected]5796dc942011-07-14 19:26:10452void ChromeNetworkDelegate::OnSendHeaders(
453 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15454 const net::HttpRequestHeaders& headers) {
[email protected]4a2b6232014-06-19 08:44:14455 extensions_delegate_->OnSendHeaders(request, headers);
[email protected]82b42302011-04-20 16:28:16456}
457
[email protected]ea8141e2011-10-05 13:12:51458int ChromeNetworkDelegate::OnHeadersReceived(
459 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47460 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32461 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16462 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
463 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14464 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16465 request,
466 callback,
467 original_response_headers,
468 override_response_headers,
469 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51470}
471
[email protected]31b2e5f2011-04-20 16:58:32472void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
473 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02474 if (domain_reliability_monitor_)
475 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14476 extensions_delegate_->OnBeforeRedirect(request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32477}
478
479
[email protected]8202d0c2011-02-23 08:31:14480void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14481 extensions_delegate_->OnResponseStarted(request);
[email protected]8202d0c2011-02-23 08:31:14482}
483
[email protected]8523ba52011-05-22 19:00:58484void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
485 int bytes_read) {
[email protected]44879ed2012-04-06 01:11:02486#if defined(ENABLE_TASK_MANAGER)
[email protected]17df9e7c2014-02-13 03:49:29487 // This is not completely accurate, but as a first approximation ignore
488 // requests that are served from the cache. See bug 330931 for more info.
pkasting941842c2015-04-11 01:51:30489 if (!request.was_cached())
490 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02491#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58492}
493
[email protected]9045b8822012-01-13 20:35:35494void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
495 bool started) {
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) {
megjablonc1751452014-12-09 19:46:47505#if defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50506 // For better accuracy, we use the actual bytes read instead of the length
507 // specified with the Content-Length header, which may be inaccurate,
508 // or missing, as is the case with chunked encoding.
509 int64 received_content_length = request->received_response_content_length();
510
[email protected]35558812013-12-18 21:58:36511 if (precache::PrecacheManager::IsPrecachingEnabled()) {
512 // Record precache metrics when a fetch is completed successfully, if
513 // precaching is enabled.
514 BrowserThread::PostTask(
515 BrowserThread::UI, FROM_HERE,
516 base::Bind(&RecordPrecacheStatsOnUIThread, request->url(),
517 base::Time::Now(), received_content_length,
518 request->was_cached(), profile_));
519 }
520#endif // defined(OS_ANDROID)
[email protected]4a2b6232014-06-19 08:44:14521 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01522 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
523 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]4a2b6232014-06-19 08:44:14524 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01525 } else {
526 NOTREACHED();
[email protected]48944382011-04-23 13:28:16527 }
[email protected]45de676a2014-03-18 23:52:02528 if (domain_reliability_monitor_)
529 domain_reliability_monitor_->OnCompleted(request, started);
[email protected]4a2b6232014-06-19 08:44:14530 extensions_delegate_->ForwardProxyErrors(request);
531 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14532}
[email protected]4b50cb52011-03-10 00:29:37533
[email protected]4875ba12011-03-30 22:31:51534void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14535 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51536}
537
[email protected]82a37672011-05-03 12:02:41538void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09539 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14540 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41541}
[email protected]7efc582d2011-08-03 20:46:35542
[email protected]c2911d72011-10-03 22:16:36543net::NetworkDelegate::AuthRequiredResponse
544ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35545 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36546 const net::AuthChallengeInfo& auth_info,
547 const AuthCallback& callback,
548 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14549 return extensions_delegate_->OnAuthRequired(
550 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35551}
[email protected]9c8ae8c2012-03-09 13:13:35552
[email protected]4c219e22012-05-05 19:41:04553bool ChromeNetworkDelegate::OnCanGetCookies(
554 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35555 const net::CookieList& cookie_list) {
556 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02557 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35558 return true;
559
560 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04561 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35562
563 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16564 int render_frame_id = -1;
[email protected]f3add922013-12-20 23:17:16565 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
566 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35567 BrowserThread::PostTask(
568 BrowserThread::UI, FROM_HERE,
569 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16570 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04571 request.url(), request.first_party_for_cookies(),
davidbenf47d1912015-03-14 00:24:24572 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35573 }
574
575 return allow;
576}
577
[email protected]4c219e22012-05-05 19:41:04578bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
579 const std::string& cookie_line,
580 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35581 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02582 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35583 return true;
584
585 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04586 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35587
[email protected]9c8ae8c2012-03-09 13:13:35588 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16589 int render_frame_id = -1;
590 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
591 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35592 BrowserThread::PostTask(
593 BrowserThread::UI, FROM_HERE,
594 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16595 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04596 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43597 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35598 }
599
[email protected]9c8ae8c2012-03-09 13:13:35600 return allow;
601}
[email protected]4c219e22012-05-05 19:41:04602
603bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45604 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04605 if (g_allow_file_access_)
606 return true;
607
[email protected]d8e4f132012-09-06 04:28:05608#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
609 return true;
610#else
[email protected]4c219e22012-05-05 19:41:04611#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05612 // If we're running Chrome for ChromeOS on Linux, we want to allow file
613 // access.
[email protected]49c4cf852013-09-27 19:28:24614 if (!base::SysInfo::IsRunningOnChromeOS() ||
avi556c05022014-12-22 23:31:43615 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05616 return true;
[email protected]288538482012-09-06 21:09:35617 }
[email protected]d8e4f132012-09-06 04:28:05618
619 // Use a whitelist to only allow access to files residing in the list of
620 // directories below.
[email protected]4c219e22012-05-05 19:41:04621 static const char* const kLocalAccessWhiteList[] = {
622 "/home/chronos/user/Downloads",
623 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42624 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04625 "/media",
626 "/opt/oem",
627 "/usr/share/chromeos-assets",
628 "/tmp",
629 "/var/log",
630 };
[email protected]ae015e12013-11-04 19:11:33631
[email protected]9e733282014-06-18 16:56:55632 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
633 // the profile path ("/home/chronos/user' is a hard link to current primary
634 // logged in profile.) For the support of multi-profile sessions, we are
635 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
636 // access.
[email protected]ae015e12013-11-04 19:11:33637 if (!profile_path_.empty()) {
638 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
639 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
640 return true;
[email protected]9e733282014-06-18 16:56:55641 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
642 if (webrtc_logs == path.StripTrailingSeparators() ||
643 webrtc_logs.IsParent(path)) {
644 return true;
645 }
[email protected]ae015e12013-11-04 19:11:33646 }
[email protected]d8e4f132012-09-06 04:28:05647#elif defined(OS_ANDROID)
648 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45649 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05650 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
651 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04652 return true;
653
[email protected]d8e4f132012-09-06 04:28:05654 // Whitelist of other allowed directories.
655 static const char* const kLocalAccessWhiteList[] = {
656 "/sdcard",
657 "/mnt/sdcard",
658 };
659#endif
660
[email protected]4c219e22012-05-05 19:41:04661 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45662 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
663 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04664 if (white_listed_path == path.StripTrailingSeparators() ||
665 white_listed_path.IsParent(path)) {
666 return true;
667 }
668 }
[email protected]d8e4f132012-09-06 04:28:05669
[email protected]288538482012-09-06 21:09:35670 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04671 return false;
[email protected]d8e4f132012-09-06 04:28:05672#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04673}
[email protected]a1d4ab072012-06-07 13:21:15674
675bool ChromeNetworkDelegate::OnCanThrottleRequest(
676 const net::URLRequest& request) const {
[email protected]84b7a552014-07-19 04:52:06677#if defined(ENABLE_EXTENSIONS)
[email protected]4a2b6232014-06-19 08:44:14678 if (g_never_throttle_requests_)
[email protected]a1d4ab072012-06-07 13:21:15679 return false;
[email protected]c4a7df82012-08-09 22:48:46680 return request.first_party_for_cookies().scheme() ==
[email protected]885c0e92012-11-13 20:27:42681 extensions::kExtensionScheme;
[email protected]84b7a552014-07-19 04:52:06682#else
683 return false;
684#endif
[email protected]a1d4ab072012-06-07 13:21:15685}
[email protected]aa28181e2012-06-13 00:53:58686
[email protected]e6d017652013-05-17 18:01:40687bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
688 const GURL& url,
689 const GURL& first_party_for_cookies) const {
690 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02691 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40692 return false;
693
694 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
695 url, first_party_for_cookies);
696 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
697 url, first_party_for_cookies);
698 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
699 return privacy_mode;
700}
701
mkwst0513c9d2015-04-01 05:53:15702bool ChromeNetworkDelegate::OnFirstPartyOnlyCookieExperimentEnabled() const {
703 return experimental_web_platform_features_enabled_;
704}
705
jochen0e3b3a62014-09-16 18:31:23706bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
707 const net::URLRequest& request,
708 const GURL& target_url,
709 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27710 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23711 return true;
712}