blob: fbc7d30fbd5646d9553c13933cb4abf187d1b185 [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"
mkwst3f3daac2015-02-26 20:15:2612#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"
vadimt81ef0df2014-11-18 19:42:3123#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"
mkwst3f3daac2015-02-26 20:15:2642#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]6a5f77c32011-09-04 19:19:5947#include "net/base/net_log.h"
[email protected]5b9bc352012-07-18 13:13:3448#include "net/cookies/canonical_cookie.h"
49#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4450#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1651#include "net/http/http_response_headers.h"
ellyjonesd84033d2015-02-12 19:20:3452#include "net/http/http_status_code.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) {
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
116
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) {
140 // Record information to help debug http://crbug.com/422871
141 if (!target_url.SchemeIsHTTPOrHTTPS())
142 return;
143 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
144 base::Bind(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41145 base::debug::DumpWithoutCrashing();
jochen652275372015-01-16 11:35:27146 NOTREACHED() << "Sending request to " << target_url
147 << " with invalid referrer " << referrer_url;
jochen0e3b3a62014-09-16 18:31:23148}
149
sclittle3c16159e2014-12-15 19:53:17150// Record network errors that HTTP requests complete with, including OK and
151// ABORTED.
152void RecordNetworkErrorHistograms(const net::URLRequest* request) {
153 if (request->url().SchemeIs("http")) {
154 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
155 std::abs(request->status().error()));
156
157 if (request->load_flags() & net::LOAD_MAIN_FRAME) {
158 UMA_HISTOGRAM_SPARSE_SLOWLY(
159 "Net.HttpRequestCompletionErrorCodes.MainFrame",
160 std::abs(request->status().error()));
161 }
162 }
163}
164
ellyjonesd84033d2015-02-12 19:20:34165// Returns whether |request| is likely to be eligible for delta-encoding.
166// This is only a rough approximation right now, based on MIME type.
167bool CanRequestBeDeltaEncoded(const net::URLRequest* request) {
168 struct {
169 const char *prefix;
170 const char *suffix;
171 } kEligibleMasks[] = {
172 // All text/ types are eligible, even if not displayable.
173 { "text/", NULL },
174 // JSON (application/json and application/*+json) is eligible.
175 { "application/", "json" },
176 // Javascript is eligible.
177 { "application/", "javascript" },
178 // XML (application/xml and application/*+xml) is eligible.
179 { "application/", "xml" },
180 };
181 const bool kCaseSensitive = true;
182
183 std::string mime_type;
184 request->GetMimeType(&mime_type);
185
186 for (size_t i = 0; i < arraysize(kEligibleMasks); i++) {
187 const char *prefix = kEligibleMasks[i].prefix;
188 const char *suffix = kEligibleMasks[i].suffix;
189 if (prefix && !StartsWithASCII(mime_type, prefix, kCaseSensitive))
190 continue;
191 if (suffix && !EndsWith(mime_type, suffix, kCaseSensitive))
192 continue;
193 return true;
194 }
195 return false;
196}
197
198// Returns whether |request| was issued by a renderer process, as opposed to
199// the browser process or a plugin process.
200bool IsRendererInitiatedRequest(const net::URLRequest* request) {
201 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
202 return info && info->GetProcessType() == content::PROCESS_TYPE_RENDERER;
203}
204
205// Uploads UMA histograms for delta encoding eligibility. This method can only
206// be safely called after the network stack has called both OnStarted and
207// OnCompleted, since it needs the received response content length and the
208// response headers.
209void RecordCacheStateStats(const net::URLRequest* request) {
210 net::HttpRequestHeaders request_headers;
211 if (!request->GetFullRequestHeaders(&request_headers)) {
212 // GetFullRequestHeaders is guaranteed to succeed if OnResponseStarted() has
213 // been called on |request|, so if GetFullRequestHeaders() fails,
214 // RecordCacheStateStats must have been called before
215 // OnResponseStarted().
216 return;
217 }
218
219 if (!IsRendererInitiatedRequest(request)) {
220 // Ignore browser-initiated requests. These are internal requests like safe
221 // browsing and sync, and so on. Some of these could be eligible for
222 // delta-encoding, but to be conservative this function ignores all of them.
223 return;
224 }
225
226 const int kCacheAffectingFlags = net::LOAD_BYPASS_CACHE |
227 net::LOAD_DISABLE_CACHE |
228 net::LOAD_PREFERRING_CACHE;
229
230 if (request->load_flags() & kCacheAffectingFlags) {
231 // Ignore requests with cache-affecting flags, which would otherwise mess up
232 // these stats.
233 return;
234 }
235
236 enum {
237 CACHE_STATE_FROM_CACHE,
238 CACHE_STATE_STILL_VALID,
239 CACHE_STATE_NO_LONGER_VALID,
240 CACHE_STATE_NO_ENTRY,
241 CACHE_STATE_MAX,
242 } state = CACHE_STATE_NO_ENTRY;
243 bool had_cache_headers =
244 request_headers.HasHeader(net::HttpRequestHeaders::kIfModifiedSince) ||
245 request_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch) ||
246 request_headers.HasHeader(net::HttpRequestHeaders::kIfRange);
247 if (request->was_cached() && !had_cache_headers) {
248 // Entry was served directly from cache.
249 state = CACHE_STATE_FROM_CACHE;
250 } else if (request->was_cached() && had_cache_headers) {
251 // Expired entry was present in cache, and server responded with NOT
252 // MODIFIED, indicating the expired entry is still valid.
253 state = CACHE_STATE_STILL_VALID;
254 } else if (!request->was_cached() && had_cache_headers) {
255 // Expired entry was present in cache, and server responded with something
256 // other than NOT MODIFIED, indicating the entry is no longer valid.
257 state = CACHE_STATE_NO_LONGER_VALID;
258 } else if (!request->was_cached() && !had_cache_headers) {
259 // Neither |was_cached| nor |had_cache_headers|, so there's no local cache
260 // entry for this content at all.
261 state = CACHE_STATE_NO_ENTRY;
262 }
263
264 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.AllRequests", state,
265 CACHE_STATE_MAX);
266 if (CanRequestBeDeltaEncoded(request)) {
267 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.EncodeableRequests", state,
268 CACHE_STATE_MAX);
269 }
270
271 int64 size = request->received_response_content_length();
272 if (size >= 0 && state == CACHE_STATE_NO_LONGER_VALID) {
273 UMA_HISTOGRAM_COUNTS("Net.CacheState.AllBytes", size);
274 if (CanRequestBeDeltaEncoded(request)) {
275 UMA_HISTOGRAM_COUNTS("Net.CacheState.EncodeableBytes", size);
276 }
277 }
278}
279
[email protected]d05ef99c2011-02-01 21:38:16280} // namespace
[email protected]ac039522010-06-15 16:39:44281
[email protected]0651b812011-02-24 00:22:50282ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10283 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48284 BooleanPrefMember* enable_referrers)
[email protected]4a2b6232014-06-19 08:44:14285 : profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59286 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48287 enable_do_not_track_(NULL),
treib4e26f6652014-12-01 14:34:18288 force_safe_search_(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),
mkwst3f3daac2015-02-26 20:15:26295 experimental_web_platform_features_enabled_(
296 base::CommandLine::ForCurrentProcess()->HasSwitch(
297 switches::kEnableExperimentalWebPlatformFeatures)),
davidben879199c2015-03-06 00:55:04298 first_request_(true) {
[email protected]0a8db0d2011-04-13 15:15:40299 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14300 extensions_delegate_.reset(
301 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50302}
303
[email protected]ac039522010-06-15 16:39:44304ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
305
[email protected]a09159a2012-11-29 12:51:48306void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20307 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14308 extensions_delegate_->set_extension_info_map(extension_info_map);
309}
310
311void ChromeNetworkDelegate::set_profile(void* profile) {
312 profile_ = profile;
313 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48314}
315
316void ChromeNetworkDelegate::set_cookie_settings(
317 CookieSettings* cookie_settings) {
318 cookie_settings_ = cookie_settings;
319}
320
[email protected]f53b4802012-12-20 17:04:23321void ChromeNetworkDelegate::set_predictor(
322 chrome_browser_net::Predictor* predictor) {
323 connect_interceptor_.reset(
324 new chrome_browser_net::ConnectInterceptor(predictor));
325}
326
[email protected]c4a7df82012-08-09 22:48:46327// static
[email protected]84b7a552014-07-19 04:52:06328#if defined(ENABLE_EXTENSIONS)
[email protected]a1d4ab072012-06-07 13:21:15329void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46330 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15331}
[email protected]84b7a552014-07-19 04:52:06332#endif
[email protected]a1d4ab072012-06-07 13:21:15333
[email protected]0a8db0d2011-04-13 15:15:40334// static
[email protected]9d8cfb682012-09-13 16:48:04335void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40336 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04337 BooleanPrefMember* enable_do_not_track,
treib4e26f6652014-12-01 14:34:18338 BooleanPrefMember* force_safe_search,
[email protected]d1208ba32012-11-08 11:10:33339 BooleanPrefMember* force_google_safe_search,
treib4e26f6652014-12-01 14:34:18340 BooleanPrefMember* force_youtube_safety_mode,
[email protected]0a8db0d2011-04-13 15:15:40341 PrefService* pref_service) {
342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]96a5c342012-12-04 18:14:02343 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43344 enable_referrers->MoveToThread(
345 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04346 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02347 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43348 enable_do_not_track->MoveToThread(
349 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04350 }
treib4e26f6652014-12-01 14:34:18351 if (force_safe_search) {
352 force_safe_search->Init(prefs::kForceSafeSearch, pref_service);
353 force_safe_search->MoveToThread(
354 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
355 }
[email protected]d1208ba32012-11-08 11:10:33356 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18357 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33358 force_google_safe_search->MoveToThread(
359 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
360 }
treib4e26f6652014-12-01 14:34:18361 if (force_youtube_safety_mode) {
362 force_youtube_safety_mode->Init(prefs::kForceYouTubeSafetyMode,
363 pref_service);
364 force_youtube_safety_mode->MoveToThread(
365 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
366 }
[email protected]0a8db0d2011-04-13 15:15:40367}
368
[email protected]4c219e22012-05-05 19:41:04369// static
370void ChromeNetworkDelegate::AllowAccessToAllFiles() {
371 g_allow_file_access_ = true;
372}
373
[email protected]4875ba12011-03-30 22:31:51374int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12375 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47376 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12377 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:34378#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59379 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
380 // blocked. However, an extension might redirect the request to another URL,
381 // which is not blocked.
[email protected]cb85f8e52014-04-08 11:44:40382 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]6a5f77c32011-09-04 19:19:59383 if (url_blacklist_manager_ &&
[email protected]cb85f8e52014-04-08 11:44:40384 url_blacklist_manager_->IsRequestBlocked(*request, &error)) {
[email protected]6a5f77c32011-09-04 19:19:59385 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59386 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26387 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
388 net::NetLog::StringCallback("url",
389 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40390 return error;
[email protected]6a5f77c32011-09-04 19:19:59391 }
[email protected]3e598ff12011-09-06 11:22:34392#endif
[email protected]6a5f77c32011-09-04 19:19:59393
[email protected]4a2b6232014-06-19 08:44:14394 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18395
[email protected]0a8db0d2011-04-13 15:15:40396 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13397 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04398 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
399 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33400
treib4e26f6652014-12-01 14:34:18401 bool force_safe_search =
402 (force_safe_search_ && force_safe_search_->GetValue()) ||
403 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33404
405 net::CompletionCallback wrapped_callback = callback;
406 if (force_safe_search) {
407 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
408 callback,
409 base::Unretained(request),
410 base::Unretained(new_url));
411 }
412
[email protected]4a2b6232014-06-19 08:44:14413 int rv = extensions_delegate_->OnBeforeURLRequest(
414 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33415
416 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24417 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33418
[email protected]f53b4802012-12-20 17:04:23419 if (connect_interceptor_)
420 connect_interceptor_->WitnessURLRequest(request);
421
[email protected]d1208ba32012-11-08 11:10:33422 return rv;
[email protected]d05ef99c2011-02-01 21:38:16423}
424
[email protected]4875ba12011-03-30 22:31:51425int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01426 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47427 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12428 net::HttpRequestHeaders* headers) {
treib4e26f6652014-12-01 14:34:18429 bool force_safety_mode =
430 (force_safe_search_ && force_safe_search_->GetValue()) ||
431 (force_youtube_safety_mode_ && force_youtube_safety_mode_->GetValue());
432 if (force_safety_mode)
[email protected]184799df2014-07-22 16:03:24433 safe_search_util::ForceYouTubeSafetyMode(request, headers);
434
[email protected]4a2b6232014-06-19 08:44:14435 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
[email protected]ac039522010-06-15 16:39:44436}
[email protected]8202d0c2011-02-23 08:31:14437
[email protected]5796dc942011-07-14 19:26:10438void ChromeNetworkDelegate::OnSendHeaders(
439 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15440 const net::HttpRequestHeaders& headers) {
[email protected]4a2b6232014-06-19 08:44:14441 extensions_delegate_->OnSendHeaders(request, headers);
[email protected]82b42302011-04-20 16:28:16442}
443
[email protected]ea8141e2011-10-05 13:12:51444int ChromeNetworkDelegate::OnHeadersReceived(
445 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47446 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32447 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16448 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
449 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14450 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16451 request,
452 callback,
453 original_response_headers,
454 override_response_headers,
455 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51456}
457
[email protected]31b2e5f2011-04-20 16:58:32458void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
459 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02460 if (domain_reliability_monitor_)
461 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14462 extensions_delegate_->OnBeforeRedirect(request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32463}
464
465
[email protected]8202d0c2011-02-23 08:31:14466void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14467 extensions_delegate_->OnResponseStarted(request);
[email protected]8202d0c2011-02-23 08:31:14468}
469
[email protected]8523ba52011-05-22 19:00:58470void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
471 int bytes_read) {
vadimt81ef0df2014-11-18 19:42:31472 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
473 tracked_objects::ScopedTracker tracking_profile(
474 FROM_HERE_WITH_EXPLICIT_FUNCTION(
475 "423948 ChromeNetworkDelegate::OnRawBytesRead"));
476
[email protected]44879ed2012-04-06 01:11:02477#if defined(ENABLE_TASK_MANAGER)
[email protected]17df9e7c2014-02-13 03:49:29478 // This is not completely accurate, but as a first approximation ignore
479 // requests that are served from the cache. See bug 330931 for more info.
vadimt2df40b372014-11-27 02:01:41480 if (!request.was_cached()) {
481 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
482 // I suspect that the jank is in creating a TaskManager instance. After the
483 // bug is fixed, rewrite the operators below as one line.
484 tracked_objects::ScopedTracker tracking_profile1(
485 FROM_HERE_WITH_EXPLICIT_FUNCTION(
486 "423948 ChromeNetworkDelegate::OnRawBytesRead1"));
487
488 TaskManager* task_manager = TaskManager::GetInstance();
489
490 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
491 tracked_objects::ScopedTracker tracking_profile2(
492 FROM_HERE_WITH_EXPLICIT_FUNCTION(
493 "423948 ChromeNetworkDelegate::OnRawBytesRead2"));
494
495 task_manager->model()->NotifyBytesRead(request, bytes_read);
496 }
[email protected]44879ed2012-04-06 01:11:02497#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58498}
499
[email protected]9045b8822012-01-13 20:35:35500void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
501 bool started) {
sclittle3c16159e2014-12-15 19:53:17502 RecordNetworkErrorHistograms(request);
ellyjonesd84033d2015-02-12 19:20:34503 if (started) {
504 // Only call in for requests that were started, to obey the precondition
505 // that RecordCacheStateStats can only be called on requests for which
506 // OnResponseStarted was called.
507 RecordCacheStateStats(request);
508 }
sclittle3c16159e2014-12-15 19:53:17509
[email protected]2756a8e2012-09-07 18:24:29510 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
megjablonc1751452014-12-09 19:46:47511#if defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50512 // For better accuracy, we use the actual bytes read instead of the length
513 // specified with the Content-Length header, which may be inaccurate,
514 // or missing, as is the case with chunked encoding.
515 int64 received_content_length = request->received_response_content_length();
516
[email protected]35558812013-12-18 21:58:36517 if (precache::PrecacheManager::IsPrecachingEnabled()) {
518 // Record precache metrics when a fetch is completed successfully, if
519 // precaching is enabled.
520 BrowserThread::PostTask(
521 BrowserThread::UI, FROM_HERE,
522 base::Bind(&RecordPrecacheStatsOnUIThread, request->url(),
523 base::Time::Now(), received_content_length,
524 request->was_cached(), profile_));
525 }
526#endif // defined(OS_ANDROID)
[email protected]4a2b6232014-06-19 08:44:14527 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01528 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
529 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]4a2b6232014-06-19 08:44:14530 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01531 } else {
532 NOTREACHED();
[email protected]48944382011-04-23 13:28:16533 }
[email protected]45de676a2014-03-18 23:52:02534 if (domain_reliability_monitor_)
535 domain_reliability_monitor_->OnCompleted(request, started);
[email protected]4a2b6232014-06-19 08:44:14536 extensions_delegate_->ForwardProxyErrors(request);
537 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14538}
[email protected]4b50cb52011-03-10 00:29:37539
[email protected]4875ba12011-03-30 22:31:51540void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14541 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51542}
543
[email protected]82a37672011-05-03 12:02:41544void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09545 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14546 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41547}
[email protected]7efc582d2011-08-03 20:46:35548
[email protected]c2911d72011-10-03 22:16:36549net::NetworkDelegate::AuthRequiredResponse
550ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35551 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36552 const net::AuthChallengeInfo& auth_info,
553 const AuthCallback& callback,
554 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14555 return extensions_delegate_->OnAuthRequired(
556 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35557}
[email protected]9c8ae8c2012-03-09 13:13:35558
[email protected]4c219e22012-05-05 19:41:04559bool ChromeNetworkDelegate::OnCanGetCookies(
560 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35561 const net::CookieList& cookie_list) {
562 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02563 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35564 return true;
565
566 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04567 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35568
569 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16570 int render_frame_id = -1;
[email protected]f3add922013-12-20 23:17:16571 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
572 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35573 BrowserThread::PostTask(
574 BrowserThread::UI, FROM_HERE,
575 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16576 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04577 request.url(), request.first_party_for_cookies(),
davidbenf47d1912015-03-14 00:24:24578 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35579 }
580
581 return allow;
582}
583
[email protected]4c219e22012-05-05 19:41:04584bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
585 const std::string& cookie_line,
586 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35587 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02588 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35589 return true;
590
591 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04592 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35593
[email protected]9c8ae8c2012-03-09 13:13:35594 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16595 int render_frame_id = -1;
596 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
597 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35598 BrowserThread::PostTask(
599 BrowserThread::UI, FROM_HERE,
600 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16601 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04602 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43603 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35604 }
605
[email protected]9c8ae8c2012-03-09 13:13:35606 return allow;
607}
[email protected]4c219e22012-05-05 19:41:04608
609bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45610 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04611 if (g_allow_file_access_)
612 return true;
613
[email protected]d8e4f132012-09-06 04:28:05614#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
615 return true;
616#else
[email protected]4c219e22012-05-05 19:41:04617#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05618 // If we're running Chrome for ChromeOS on Linux, we want to allow file
619 // access.
[email protected]49c4cf852013-09-27 19:28:24620 if (!base::SysInfo::IsRunningOnChromeOS() ||
avi556c05022014-12-22 23:31:43621 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05622 return true;
[email protected]288538482012-09-06 21:09:35623 }
[email protected]d8e4f132012-09-06 04:28:05624
625 // Use a whitelist to only allow access to files residing in the list of
626 // directories below.
[email protected]4c219e22012-05-05 19:41:04627 static const char* const kLocalAccessWhiteList[] = {
628 "/home/chronos/user/Downloads",
629 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42630 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04631 "/media",
632 "/opt/oem",
633 "/usr/share/chromeos-assets",
634 "/tmp",
635 "/var/log",
636 };
[email protected]ae015e12013-11-04 19:11:33637
[email protected]9e733282014-06-18 16:56:55638 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
639 // the profile path ("/home/chronos/user' is a hard link to current primary
640 // logged in profile.) For the support of multi-profile sessions, we are
641 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
642 // access.
[email protected]ae015e12013-11-04 19:11:33643 if (!profile_path_.empty()) {
644 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
645 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
646 return true;
[email protected]9e733282014-06-18 16:56:55647 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
648 if (webrtc_logs == path.StripTrailingSeparators() ||
649 webrtc_logs.IsParent(path)) {
650 return true;
651 }
[email protected]ae015e12013-11-04 19:11:33652 }
[email protected]d8e4f132012-09-06 04:28:05653#elif defined(OS_ANDROID)
654 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45655 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05656 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
657 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04658 return true;
659
[email protected]d8e4f132012-09-06 04:28:05660 // Whitelist of other allowed directories.
661 static const char* const kLocalAccessWhiteList[] = {
662 "/sdcard",
663 "/mnt/sdcard",
664 };
665#endif
666
[email protected]4c219e22012-05-05 19:41:04667 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45668 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
669 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04670 if (white_listed_path == path.StripTrailingSeparators() ||
671 white_listed_path.IsParent(path)) {
672 return true;
673 }
674 }
[email protected]d8e4f132012-09-06 04:28:05675
[email protected]288538482012-09-06 21:09:35676 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04677 return false;
[email protected]d8e4f132012-09-06 04:28:05678#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04679}
[email protected]a1d4ab072012-06-07 13:21:15680
681bool ChromeNetworkDelegate::OnCanThrottleRequest(
682 const net::URLRequest& request) const {
[email protected]84b7a552014-07-19 04:52:06683#if defined(ENABLE_EXTENSIONS)
[email protected]4a2b6232014-06-19 08:44:14684 if (g_never_throttle_requests_)
[email protected]a1d4ab072012-06-07 13:21:15685 return false;
[email protected]c4a7df82012-08-09 22:48:46686 return request.first_party_for_cookies().scheme() ==
[email protected]885c0e92012-11-13 20:27:42687 extensions::kExtensionScheme;
[email protected]84b7a552014-07-19 04:52:06688#else
689 return false;
690#endif
[email protected]a1d4ab072012-06-07 13:21:15691}
[email protected]aa28181e2012-06-13 00:53:58692
[email protected]e6d017652013-05-17 18:01:40693bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
694 const GURL& url,
695 const GURL& first_party_for_cookies) const {
696 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02697 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40698 return false;
699
700 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
701 url, first_party_for_cookies);
702 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
703 url, first_party_for_cookies);
704 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
705 return privacy_mode;
706}
707
mkwst3f3daac2015-02-26 20:15:26708bool ChromeNetworkDelegate::OnFirstPartyOnlyCookieExperimentEnabled() const {
709 return experimental_web_platform_features_enabled_;
710}
711
jochen0e3b3a62014-09-16 18:31:23712bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
713 const net::URLRequest& request,
714 const GURL& target_url,
715 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27716 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23717 return true;
718}