blob: e638fff85ac2155c997b498ada4e2f4d7eb1c4a3 [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"
drogerb1716972015-06-30 09:04:0928#include "chrome/browser/content_settings/cookie_settings_factory.h"
[email protected]9c8ae8c2012-03-09 13:13:3529#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"
ttuttle05ae3f342015-07-13 17:38:3533#include "chrome/browser/net/request_source_bandwidth_histograms.h"
[email protected]184799df2014-07-22 16:03:2434#include "chrome/browser/net/safe_search_util.h"
[email protected]6baff0b52012-03-06 01:30:1835#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5836#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4037#include "chrome/common/pref_names.h"
drogerb1716972015-06-30 09:04:0938#include "components/content_settings/core/browser/cookie_settings.h"
[email protected]45de676a2014-03-18 23:52:0239#include "components/domain_reliability/monitor.h"
[email protected]c38831a12011-10-28 12:44:4940#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5641#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3342#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3543#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1544#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3445#include "content/public/common/process_type.h"
[email protected]82b42302011-04-20 16:28:1646#include "net/base/host_port_pair.h"
sclittle3c16159e2014-12-15 19:53:1747#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1448#include "net/base/net_errors.h"
[email protected]5b9bc352012-07-18 13:13:3449#include "net/cookies/canonical_cookie.h"
50#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4451#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1652#include "net/http/http_response_headers.h"
ellyjonesd84033d2015-02-12 19:20:3453#include "net/http/http_status_code.h"
eroman87c53d62015-04-02 06:51:0754#include "net/log/net_log.h"
[email protected]d05ef99c2011-02-01 21:38:1655#include "net/url_request/url_request.h"
56
[email protected]4a2b6232014-06-19 08:44:1457#if defined(OS_ANDROID)
58#include "chrome/browser/io_thread.h"
twifkak85f97872015-06-24 01:00:1359#include "chrome/browser/precache/precache_manager_factory.h"
[email protected]4a2b6232014-06-19 08:44:1460#include "components/precache/content/precache_manager.h"
[email protected]4a2b6232014-06-19 08:44:1461#endif
62
[email protected]4c219e22012-05-05 19:41:0463#if defined(OS_CHROMEOS)
[email protected]49c4cf852013-09-27 19:28:2464#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3565#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0466#endif
67
[email protected]3e598ff12011-09-06 11:22:3468#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]f522afa2014-01-08 15:36:3669#include "components/policy/core/browser/url_blacklist_manager.h"
[email protected]3e598ff12011-09-06 11:22:3470#endif
71
[email protected]84b7a552014-07-19 04:52:0672#if defined(ENABLE_EXTENSIONS)
73#include "extensions/common/constants.h"
74#endif
75
[email protected]631bb742011-11-02 11:29:3976using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1177using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2578using content::ResourceRequestInfo;
[email protected]7491ad02014-07-05 19:10:0779using content::ResourceType;
[email protected]631bb742011-11-02 11:29:3980
[email protected]d8e4f132012-09-06 04:28:0581// By default we don't allow access to all file:// urls on ChromeOS and
82// Android.
83#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0484bool ChromeNetworkDelegate::g_allow_file_access_ = false;
85#else
86bool ChromeNetworkDelegate::g_allow_file_access_ = true;
87#endif
88
[email protected]d05ef99c2011-02-01 21:38:1689namespace {
90
[email protected]9d8cfb682012-09-13 16:48:0491const char kDNTHeader[] = "DNT";
92
[email protected]d1208ba32012-11-08 11:10:3393// Gets called when the extensions finish work on the URL. If the extensions
94// did not do a redirect (so |new_url| is empty) then we enforce the
95// SafeSearch parameters. Otherwise we will get called again after the
96// redirect and we enforce SafeSearch then.
97void ForceGoogleSafeSearchCallbackWrapper(
98 const net::CompletionCallback& callback,
99 net::URLRequest* request,
100 GURL* new_url,
101 int rv) {
102 if (rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24103 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33104 callback.Run(rv);
105}
106
[email protected]35558812013-12-18 21:58:36107#if defined(OS_ANDROID)
108void RecordPrecacheStatsOnUIThread(const GURL& url,
109 const base::Time& fetch_time, int64 size,
110 bool was_cached, void* profile_id) {
anujk.sharma2e02ce162015-04-29 23:10:02111 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]35558812013-12-18 21:58:36112
113 Profile* profile = reinterpret_cast<Profile*>(profile_id);
twifkakb2175432015-08-07 20:19:07114 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
[email protected]35558812013-12-18 21:58:36115 return;
[email protected]35558812013-12-18 21:58:36116
117 precache::PrecacheManager* precache_manager =
118 precache::PrecacheManagerFactory::GetForBrowserContext(profile);
twifkakb2175432015-08-07 20:19:07119 // |precache_manager| could be NULL if the profile is off the record.
120 if (!precache_manager || !precache_manager->WouldRun())
[email protected]35558812013-12-18 21:58:36121 return;
[email protected]35558812013-12-18 21:58:36122
123 precache_manager->RecordStatsForFetch(url, fetch_time, size, was_cached);
124}
125#endif // defined(OS_ANDROID)
126
jochen652275372015-01-16 11:35:27127void ReportInvalidReferrerSendOnUI() {
jochen0e3b3a62014-09-16 18:31:23128 base::RecordAction(
129 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
jochen652275372015-01-16 11:35:27130}
131
132void ReportInvalidReferrerSend(const GURL& target_url,
133 const GURL& referrer_url) {
jochen4c874d22015-04-13 14:01:04134 LOG(ERROR) << "Cancelling request to " << target_url
135 << " with invalid referrer " << referrer_url;
jochen652275372015-01-16 11:35:27136 // Record information to help debug http://crbug.com/422871
137 if (!target_url.SchemeIsHTTPOrHTTPS())
138 return;
139 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
140 base::Bind(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41141 base::debug::DumpWithoutCrashing();
jochen4c874d22015-04-13 14:01:04142 NOTREACHED();
jochen0e3b3a62014-09-16 18:31:23143}
144
sclittle3c16159e2014-12-15 19:53:17145// Record network errors that HTTP requests complete with, including OK and
146// ABORTED.
147void RecordNetworkErrorHistograms(const net::URLRequest* request) {
148 if (request->url().SchemeIs("http")) {
149 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
150 std::abs(request->status().error()));
151
152 if (request->load_flags() & net::LOAD_MAIN_FRAME) {
153 UMA_HISTOGRAM_SPARSE_SLOWLY(
154 "Net.HttpRequestCompletionErrorCodes.MainFrame",
155 std::abs(request->status().error()));
156 }
157 }
158}
159
ellyjonesd84033d2015-02-12 19:20:34160// Returns whether |request| is likely to be eligible for delta-encoding.
161// This is only a rough approximation right now, based on MIME type.
162bool CanRequestBeDeltaEncoded(const net::URLRequest* request) {
163 struct {
164 const char *prefix;
165 const char *suffix;
166 } kEligibleMasks[] = {
167 // All text/ types are eligible, even if not displayable.
168 { "text/", NULL },
169 // JSON (application/json and application/*+json) is eligible.
170 { "application/", "json" },
171 // Javascript is eligible.
172 { "application/", "javascript" },
173 // XML (application/xml and application/*+xml) is eligible.
174 { "application/", "xml" },
175 };
ellyjonesd84033d2015-02-12 19:20:34176
177 std::string mime_type;
178 request->GetMimeType(&mime_type);
179
180 for (size_t i = 0; i < arraysize(kEligibleMasks); i++) {
181 const char *prefix = kEligibleMasks[i].prefix;
182 const char *suffix = kEligibleMasks[i].suffix;
brettw66d1b81b2015-07-06 19:29:40183 if (prefix &&
184 !base::StartsWith(mime_type, prefix, base::CompareCase::SENSITIVE))
ellyjonesd84033d2015-02-12 19:20:34185 continue;
brettw66d1b81b2015-07-06 19:29:40186 if (suffix &&
187 !base::EndsWith(mime_type, suffix, base::CompareCase::SENSITIVE))
ellyjonesd84033d2015-02-12 19:20:34188 continue;
189 return true;
190 }
191 return false;
192}
193
194// Returns whether |request| was issued by a renderer process, as opposed to
195// the browser process or a plugin process.
196bool IsRendererInitiatedRequest(const net::URLRequest* request) {
197 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
198 return info && info->GetProcessType() == content::PROCESS_TYPE_RENDERER;
199}
200
201// Uploads UMA histograms for delta encoding eligibility. This method can only
202// be safely called after the network stack has called both OnStarted and
203// OnCompleted, since it needs the received response content length and the
204// response headers.
205void RecordCacheStateStats(const net::URLRequest* request) {
206 net::HttpRequestHeaders request_headers;
207 if (!request->GetFullRequestHeaders(&request_headers)) {
208 // GetFullRequestHeaders is guaranteed to succeed if OnResponseStarted() has
209 // been called on |request|, so if GetFullRequestHeaders() fails,
210 // RecordCacheStateStats must have been called before
211 // OnResponseStarted().
212 return;
213 }
214
215 if (!IsRendererInitiatedRequest(request)) {
216 // Ignore browser-initiated requests. These are internal requests like safe
217 // browsing and sync, and so on. Some of these could be eligible for
218 // delta-encoding, but to be conservative this function ignores all of them.
219 return;
220 }
221
222 const int kCacheAffectingFlags = net::LOAD_BYPASS_CACHE |
223 net::LOAD_DISABLE_CACHE |
224 net::LOAD_PREFERRING_CACHE;
225
226 if (request->load_flags() & kCacheAffectingFlags) {
227 // Ignore requests with cache-affecting flags, which would otherwise mess up
228 // these stats.
229 return;
230 }
231
232 enum {
233 CACHE_STATE_FROM_CACHE,
234 CACHE_STATE_STILL_VALID,
235 CACHE_STATE_NO_LONGER_VALID,
236 CACHE_STATE_NO_ENTRY,
237 CACHE_STATE_MAX,
238 } state = CACHE_STATE_NO_ENTRY;
239 bool had_cache_headers =
240 request_headers.HasHeader(net::HttpRequestHeaders::kIfModifiedSince) ||
241 request_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch) ||
242 request_headers.HasHeader(net::HttpRequestHeaders::kIfRange);
243 if (request->was_cached() && !had_cache_headers) {
244 // Entry was served directly from cache.
245 state = CACHE_STATE_FROM_CACHE;
246 } else if (request->was_cached() && had_cache_headers) {
247 // Expired entry was present in cache, and server responded with NOT
248 // MODIFIED, indicating the expired entry is still valid.
249 state = CACHE_STATE_STILL_VALID;
250 } else if (!request->was_cached() && had_cache_headers) {
251 // Expired entry was present in cache, and server responded with something
252 // other than NOT MODIFIED, indicating the entry is no longer valid.
253 state = CACHE_STATE_NO_LONGER_VALID;
254 } else if (!request->was_cached() && !had_cache_headers) {
255 // Neither |was_cached| nor |had_cache_headers|, so there's no local cache
256 // entry for this content at all.
257 state = CACHE_STATE_NO_ENTRY;
258 }
259
260 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.AllRequests", state,
261 CACHE_STATE_MAX);
262 if (CanRequestBeDeltaEncoded(request)) {
263 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.EncodeableRequests", state,
264 CACHE_STATE_MAX);
265 }
266
267 int64 size = request->received_response_content_length();
268 if (size >= 0 && state == CACHE_STATE_NO_LONGER_VALID) {
269 UMA_HISTOGRAM_COUNTS("Net.CacheState.AllBytes", size);
270 if (CanRequestBeDeltaEncoded(request)) {
271 UMA_HISTOGRAM_COUNTS("Net.CacheState.EncodeableBytes", size);
272 }
273 }
274}
275
[email protected]d05ef99c2011-02-01 21:38:16276} // namespace
[email protected]ac039522010-06-15 16:39:44277
[email protected]0651b812011-02-24 00:22:50278ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10279 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48280 BooleanPrefMember* enable_referrers)
[email protected]4a2b6232014-06-19 08:44:14281 : profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59282 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48283 enable_do_not_track_(NULL),
284 force_google_safe_search_(NULL),
treib4e26f6652014-12-01 14:34:18285 force_youtube_safety_mode_(NULL),
[email protected]4a2b6232014-06-19 08:44:14286#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]a09159a2012-11-29 12:51:48287 url_blacklist_manager_(NULL),
[email protected]4a2b6232014-06-19 08:44:14288#endif
[email protected]45de676a2014-03-18 23:52:02289 domain_reliability_monitor_(NULL),
mkwst0513c9d2015-04-01 05:53:15290 experimental_web_platform_features_enabled_(
291 base::CommandLine::ForCurrentProcess()->HasSwitch(
thestig397accd2015-04-04 00:03:04292 switches::kEnableExperimentalWebPlatformFeatures)) {
[email protected]0a8db0d2011-04-13 15:15:40293 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14294 extensions_delegate_.reset(
295 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50296}
297
[email protected]ac039522010-06-15 16:39:44298ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
299
[email protected]a09159a2012-11-29 12:51:48300void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20301 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14302 extensions_delegate_->set_extension_info_map(extension_info_map);
303}
304
305void ChromeNetworkDelegate::set_profile(void* profile) {
306 profile_ = profile;
307 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48308}
309
310void ChromeNetworkDelegate::set_cookie_settings(
drogerb1716972015-06-30 09:04:09311 content_settings::CookieSettings* cookie_settings) {
[email protected]a09159a2012-11-29 12:51:48312 cookie_settings_ = cookie_settings;
313}
314
[email protected]f53b4802012-12-20 17:04:23315void ChromeNetworkDelegate::set_predictor(
316 chrome_browser_net::Predictor* predictor) {
317 connect_interceptor_.reset(
318 new chrome_browser_net::ConnectInterceptor(predictor));
319}
320
[email protected]c4a7df82012-08-09 22:48:46321// static
[email protected]9d8cfb682012-09-13 16:48:04322void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40323 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04324 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33325 BooleanPrefMember* force_google_safe_search,
treib4e26f6652014-12-01 14:34:18326 BooleanPrefMember* force_youtube_safety_mode,
[email protected]0a8db0d2011-04-13 15:15:40327 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02328 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02329 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43330 enable_referrers->MoveToThread(
331 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04332 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02333 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43334 enable_do_not_track->MoveToThread(
335 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04336 }
[email protected]d1208ba32012-11-08 11:10:33337 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18338 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33339 force_google_safe_search->MoveToThread(
340 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
341 }
treib4e26f6652014-12-01 14:34:18342 if (force_youtube_safety_mode) {
343 force_youtube_safety_mode->Init(prefs::kForceYouTubeSafetyMode,
344 pref_service);
345 force_youtube_safety_mode->MoveToThread(
346 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
347 }
[email protected]0a8db0d2011-04-13 15:15:40348}
349
[email protected]4c219e22012-05-05 19:41:04350// static
351void ChromeNetworkDelegate::AllowAccessToAllFiles() {
352 g_allow_file_access_ = true;
353}
354
[email protected]4875ba12011-03-30 22:31:51355int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12356 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47357 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12358 GURL* new_url) {
mmenke376b8782015-05-05 04:41:30359 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
360 tracked_objects::ScopedTracker tracking_profile1(
361 FROM_HERE_WITH_EXPLICIT_FUNCTION(
362 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest"));
363
[email protected]3e598ff12011-09-06 11:22:34364#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59365 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
366 // blocked. However, an extension might redirect the request to another URL,
367 // which is not blocked.
mmenkef1aa9072015-05-18 16:21:04368
369 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
[email protected]cb85f8e52014-04-08 11:44:40370 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
mmenkef1aa9072015-05-18 16:21:04371 if (info && content::IsResourceTypeFrame(info->GetResourceType()) &&
372 url_blacklist_manager_ &&
373 url_blacklist_manager_->ShouldBlockRequestForFrame(
374 request->url(), &error)) {
[email protected]6a5f77c32011-09-04 19:19:59375 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59376 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26377 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
378 net::NetLog::StringCallback("url",
379 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40380 return error;
[email protected]6a5f77c32011-09-04 19:19:59381 }
[email protected]3e598ff12011-09-06 11:22:34382#endif
[email protected]6a5f77c32011-09-04 19:19:59383
mmenke376b8782015-05-05 04:41:30384 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
385 tracked_objects::ScopedTracker tracking_profile2(
386 FROM_HERE_WITH_EXPLICIT_FUNCTION(
387 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 2"));
388
[email protected]4a2b6232014-06-19 08:44:14389 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18390
[email protected]0a8db0d2011-04-13 15:15:40391 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13392 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04393 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
394 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33395
mmenke376b8782015-05-05 04:41:30396 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
397 tracked_objects::ScopedTracker tracking_profile3(
398 FROM_HERE_WITH_EXPLICIT_FUNCTION(
399 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 3"));
400
treib4e26f6652014-12-01 14:34:18401 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18402 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33403
404 net::CompletionCallback wrapped_callback = callback;
405 if (force_safe_search) {
406 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
407 callback,
408 base::Unretained(request),
409 base::Unretained(new_url));
410 }
411
[email protected]4a2b6232014-06-19 08:44:14412 int rv = extensions_delegate_->OnBeforeURLRequest(
413 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33414
mmenke376b8782015-05-05 04:41:30415 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
416 tracked_objects::ScopedTracker tracking_profile4(
417 FROM_HERE_WITH_EXPLICIT_FUNCTION(
418 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 4"));
419
[email protected]d1208ba32012-11-08 11:10:33420 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24421 safe_search_util::ForceGoogleSafeSearch(request, 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_profile5(
425 FROM_HERE_WITH_EXPLICIT_FUNCTION(
426 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 5"));
427
[email protected]f53b4802012-12-20 17:04:23428 if (connect_interceptor_)
429 connect_interceptor_->WitnessURLRequest(request);
430
[email protected]d1208ba32012-11-08 11:10:33431 return rv;
[email protected]d05ef99c2011-02-01 21:38:16432}
433
[email protected]4875ba12011-03-30 22:31:51434int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01435 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47436 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12437 net::HttpRequestHeaders* headers) {
treib2b188052015-04-17 12:32:50438 if (force_youtube_safety_mode_ && force_youtube_safety_mode_->GetValue())
[email protected]184799df2014-07-22 16:03:24439 safe_search_util::ForceYouTubeSafetyMode(request, headers);
440
[email protected]4a2b6232014-06-19 08:44:14441 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
[email protected]ac039522010-06-15 16:39:44442}
[email protected]8202d0c2011-02-23 08:31:14443
[email protected]5796dc942011-07-14 19:26:10444void ChromeNetworkDelegate::OnSendHeaders(
445 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15446 const net::HttpRequestHeaders& headers) {
[email protected]4a2b6232014-06-19 08:44:14447 extensions_delegate_->OnSendHeaders(request, headers);
[email protected]82b42302011-04-20 16:28:16448}
449
[email protected]ea8141e2011-10-05 13:12:51450int ChromeNetworkDelegate::OnHeadersReceived(
451 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47452 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32453 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16454 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
455 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14456 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16457 request,
458 callback,
459 original_response_headers,
460 override_response_headers,
461 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51462}
463
[email protected]31b2e5f2011-04-20 16:58:32464void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
465 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02466 if (domain_reliability_monitor_)
467 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14468 extensions_delegate_->OnBeforeRedirect(request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32469}
470
471
[email protected]8202d0c2011-02-23 08:31:14472void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14473 extensions_delegate_->OnResponseStarted(request);
[email protected]8202d0c2011-02-23 08:31:14474}
475
[email protected]8523ba52011-05-22 19:00:58476void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
477 int bytes_read) {
[email protected]44879ed2012-04-06 01:11:02478#if defined(ENABLE_TASK_MANAGER)
[email protected]17df9e7c2014-02-13 03:49:29479 // This is not completely accurate, but as a first approximation ignore
480 // requests that are served from the cache. See bug 330931 for more info.
pkasting941842c2015-04-11 01:51:30481 if (!request.was_cached())
482 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02483#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58484}
485
[email protected]9045b8822012-01-13 20:35:35486void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
487 bool started) {
sclittle3c16159e2014-12-15 19:53:17488 RecordNetworkErrorHistograms(request);
ellyjonesd84033d2015-02-12 19:20:34489 if (started) {
490 // Only call in for requests that were started, to obey the precondition
491 // that RecordCacheStateStats can only be called on requests for which
492 // OnResponseStarted was called.
493 RecordCacheStateStats(request);
494 }
sclittle3c16159e2014-12-15 19:53:17495
[email protected]2756a8e2012-09-07 18:24:29496 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
megjablonc1751452014-12-09 19:46:47497#if defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50498 // For better accuracy, we use the actual bytes read instead of the length
499 // specified with the Content-Length header, which may be inaccurate,
500 // or missing, as is the case with chunked encoding.
501 int64 received_content_length = request->received_response_content_length();
502
twifkakb2175432015-08-07 20:19:07503 // Record precache metrics when a fetch is completed successfully, if
504 // precaching is allowed.
twifkakc07ac942015-07-20 18:23:04505 BrowserThread::PostTask(
506 BrowserThread::UI, FROM_HERE,
507 base::Bind(&RecordPrecacheStatsOnUIThread, request->url(),
508 base::Time::Now(), received_content_length,
509 request->was_cached(), profile_));
[email protected]35558812013-12-18 21:58:36510#endif // defined(OS_ANDROID)
[email protected]4a2b6232014-06-19 08:44:14511 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01512 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
513 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]4a2b6232014-06-19 08:44:14514 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01515 } else {
516 NOTREACHED();
[email protected]48944382011-04-23 13:28:16517 }
[email protected]45de676a2014-03-18 23:52:02518 if (domain_reliability_monitor_)
519 domain_reliability_monitor_->OnCompleted(request, started);
ttuttle05ae3f342015-07-13 17:38:35520 RecordRequestSourceBandwidth(request, started);
[email protected]4a2b6232014-06-19 08:44:14521 extensions_delegate_->ForwardProxyErrors(request);
522 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14523}
[email protected]4b50cb52011-03-10 00:29:37524
[email protected]4875ba12011-03-30 22:31:51525void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14526 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51527}
528
[email protected]82a37672011-05-03 12:02:41529void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09530 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14531 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41532}
[email protected]7efc582d2011-08-03 20:46:35533
[email protected]c2911d72011-10-03 22:16:36534net::NetworkDelegate::AuthRequiredResponse
535ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35536 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36537 const net::AuthChallengeInfo& auth_info,
538 const AuthCallback& callback,
539 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14540 return extensions_delegate_->OnAuthRequired(
541 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35542}
[email protected]9c8ae8c2012-03-09 13:13:35543
[email protected]4c219e22012-05-05 19:41:04544bool ChromeNetworkDelegate::OnCanGetCookies(
545 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35546 const net::CookieList& cookie_list) {
547 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02548 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35549 return true;
550
551 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04552 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35553
554 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16555 int render_frame_id = -1;
556 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
557 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35558 BrowserThread::PostTask(
559 BrowserThread::UI, FROM_HERE,
560 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16561 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04562 request.url(), request.first_party_for_cookies(),
davidbenf47d1912015-03-14 00:24:24563 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35564 }
565
566 return allow;
567}
568
[email protected]4c219e22012-05-05 19:41:04569bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
570 const std::string& cookie_line,
571 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35572 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02573 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35574 return true;
575
576 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04577 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35578
[email protected]9c8ae8c2012-03-09 13:13:35579 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16580 int render_frame_id = -1;
581 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
582 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35583 BrowserThread::PostTask(
584 BrowserThread::UI, FROM_HERE,
585 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16586 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04587 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43588 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35589 }
590
591 return allow;
592}
[email protected]4c219e22012-05-05 19:41:04593
594bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45595 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04596 if (g_allow_file_access_)
597 return true;
598
[email protected]d8e4f132012-09-06 04:28:05599#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
600 return true;
601#else
[email protected]4c219e22012-05-05 19:41:04602#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05603 // If we're running Chrome for ChromeOS on Linux, we want to allow file
604 // access.
[email protected]49c4cf852013-09-27 19:28:24605 if (!base::SysInfo::IsRunningOnChromeOS() ||
avi556c05022014-12-22 23:31:43606 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05607 return true;
[email protected]288538482012-09-06 21:09:35608 }
[email protected]d8e4f132012-09-06 04:28:05609
610 // Use a whitelist to only allow access to files residing in the list of
611 // directories below.
[email protected]4c219e22012-05-05 19:41:04612 static const char* const kLocalAccessWhiteList[] = {
613 "/home/chronos/user/Downloads",
614 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42615 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04616 "/media",
617 "/opt/oem",
618 "/usr/share/chromeos-assets",
619 "/tmp",
620 "/var/log",
621 };
[email protected]ae015e12013-11-04 19:11:33622
[email protected]9e733282014-06-18 16:56:55623 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
624 // the profile path ("/home/chronos/user' is a hard link to current primary
625 // logged in profile.) For the support of multi-profile sessions, we are
626 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
627 // access.
[email protected]ae015e12013-11-04 19:11:33628 if (!profile_path_.empty()) {
629 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
630 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
631 return true;
[email protected]9e733282014-06-18 16:56:55632 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
633 if (webrtc_logs == path.StripTrailingSeparators() ||
634 webrtc_logs.IsParent(path)) {
635 return true;
636 }
[email protected]ae015e12013-11-04 19:11:33637 }
[email protected]d8e4f132012-09-06 04:28:05638#elif defined(OS_ANDROID)
639 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45640 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05641 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
642 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04643 return true;
644
[email protected]d8e4f132012-09-06 04:28:05645 // Whitelist of other allowed directories.
646 static const char* const kLocalAccessWhiteList[] = {
647 "/sdcard",
648 "/mnt/sdcard",
649 };
650#endif
651
[email protected]4c219e22012-05-05 19:41:04652 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45653 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
654 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04655 if (white_listed_path == path.StripTrailingSeparators() ||
656 white_listed_path.IsParent(path)) {
657 return true;
658 }
659 }
[email protected]d8e4f132012-09-06 04:28:05660
[email protected]288538482012-09-06 21:09:35661 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04662 return false;
[email protected]d8e4f132012-09-06 04:28:05663#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04664}
[email protected]a1d4ab072012-06-07 13:21:15665
[email protected]e6d017652013-05-17 18:01:40666bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
667 const GURL& url,
668 const GURL& first_party_for_cookies) const {
669 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02670 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40671 return false;
672
673 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
674 url, first_party_for_cookies);
675 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
676 url, first_party_for_cookies);
677 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
678 return privacy_mode;
679}
680
mkwst0513c9d2015-04-01 05:53:15681bool ChromeNetworkDelegate::OnFirstPartyOnlyCookieExperimentEnabled() const {
682 return experimental_web_platform_features_enabled_;
683}
684
jochen0e3b3a62014-09-16 18:31:23685bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
686 const net::URLRequest& request,
687 const GURL& target_url,
688 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27689 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23690 return true;
691}