blob: c604207b08513b502b11623215a603c42daa2422 [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"
[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"
drogerb1716972015-06-30 09:04:0937#include "components/content_settings/core/browser/cookie_settings.h"
[email protected]45de676a2014-03-18 23:52:0238#include "components/domain_reliability/monitor.h"
[email protected]c38831a12011-10-28 12:44:4939#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5640#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3341#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3542#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1543#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3444#include "content/public/common/process_type.h"
[email protected]82b42302011-04-20 16:28:1645#include "net/base/host_port_pair.h"
sclittle3c16159e2014-12-15 19:53:1746#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1447#include "net/base/net_errors.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"
eroman87c53d62015-04-02 06:51:0753#include "net/log/net_log.h"
[email protected]d05ef99c2011-02-01 21:38:1654#include "net/url_request/url_request.h"
55
[email protected]4a2b6232014-06-19 08:44:1456#if defined(OS_ANDROID)
57#include "chrome/browser/io_thread.h"
twifkak85f97872015-06-24 01:00:1358#include "chrome/browser/precache/precache_manager_factory.h"
[email protected]4a2b6232014-06-19 08:44:1459#include "components/precache/content/precache_manager.h"
[email protected]4a2b6232014-06-19 08:44:1460#endif
61
[email protected]4c219e22012-05-05 19:41:0462#if defined(OS_CHROMEOS)
[email protected]49c4cf852013-09-27 19:28:2463#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3564#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0465#endif
66
[email protected]3e598ff12011-09-06 11:22:3467#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]f522afa2014-01-08 15:36:3668#include "components/policy/core/browser/url_blacklist_manager.h"
[email protected]3e598ff12011-09-06 11:22:3469#endif
70
[email protected]84b7a552014-07-19 04:52:0671#if defined(ENABLE_EXTENSIONS)
72#include "extensions/common/constants.h"
73#endif
74
[email protected]631bb742011-11-02 11:29:3975using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1176using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2577using content::ResourceRequestInfo;
[email protected]7491ad02014-07-05 19:10:0778using content::ResourceType;
[email protected]631bb742011-11-02 11:29:3979
[email protected]d8e4f132012-09-06 04:28:0580// By default we don't allow access to all file:// urls on ChromeOS and
81// Android.
82#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0483bool ChromeNetworkDelegate::g_allow_file_access_ = false;
84#else
85bool ChromeNetworkDelegate::g_allow_file_access_ = true;
86#endif
87
[email protected]d05ef99c2011-02-01 21:38:1688namespace {
89
[email protected]9d8cfb682012-09-13 16:48:0490const char kDNTHeader[] = "DNT";
91
[email protected]d1208ba32012-11-08 11:10:3392// Gets called when the extensions finish work on the URL. If the extensions
93// did not do a redirect (so |new_url| is empty) then we enforce the
94// SafeSearch parameters. Otherwise we will get called again after the
95// redirect and we enforce SafeSearch then.
96void ForceGoogleSafeSearchCallbackWrapper(
97 const net::CompletionCallback& callback,
98 net::URLRequest* request,
99 GURL* new_url,
100 int rv) {
101 if (rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24102 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33103 callback.Run(rv);
104}
105
[email protected]35558812013-12-18 21:58:36106#if defined(OS_ANDROID)
107void RecordPrecacheStatsOnUIThread(const GURL& url,
108 const base::Time& fetch_time, int64 size,
109 bool was_cached, void* profile_id) {
anujk.sharma2e02ce162015-04-29 23:10:02110 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]35558812013-12-18 21:58:36111
112 Profile* profile = reinterpret_cast<Profile*>(profile_id);
113 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) {
114 return;
115 }
116
117 precache::PrecacheManager* precache_manager =
118 precache::PrecacheManagerFactory::GetForBrowserContext(profile);
tanviraumi387f6332014-08-29 19:04:36119 if (!precache_manager || !precache_manager->IsPrecachingAllowed()) {
120 // |precache_manager| could be NULL if the profile is off the record.
[email protected]35558812013-12-18 21:58:36121 return;
122 }
123
124 precache_manager->RecordStatsForFetch(url, fetch_time, size, was_cached);
125}
126#endif // defined(OS_ANDROID)
127
jochen652275372015-01-16 11:35:27128void ReportInvalidReferrerSendOnUI() {
jochen0e3b3a62014-09-16 18:31:23129 base::RecordAction(
130 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
jochen652275372015-01-16 11:35:27131}
132
133void ReportInvalidReferrerSend(const GURL& target_url,
134 const GURL& referrer_url) {
jochen4c874d22015-04-13 14:01:04135 LOG(ERROR) << "Cancelling request to " << target_url
136 << " with invalid referrer " << referrer_url;
jochen652275372015-01-16 11:35:27137 // Record information to help debug http://crbug.com/422871
138 if (!target_url.SchemeIsHTTPOrHTTPS())
139 return;
140 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
141 base::Bind(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41142 base::debug::DumpWithoutCrashing();
jochen4c874d22015-04-13 14:01:04143 NOTREACHED();
jochen0e3b3a62014-09-16 18:31:23144}
145
sclittle3c16159e2014-12-15 19:53:17146// Record network errors that HTTP requests complete with, including OK and
147// ABORTED.
148void RecordNetworkErrorHistograms(const net::URLRequest* request) {
149 if (request->url().SchemeIs("http")) {
150 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
151 std::abs(request->status().error()));
152
153 if (request->load_flags() & net::LOAD_MAIN_FRAME) {
154 UMA_HISTOGRAM_SPARSE_SLOWLY(
155 "Net.HttpRequestCompletionErrorCodes.MainFrame",
156 std::abs(request->status().error()));
157 }
158 }
159}
160
ellyjonesd84033d2015-02-12 19:20:34161// Returns whether |request| is likely to be eligible for delta-encoding.
162// This is only a rough approximation right now, based on MIME type.
163bool CanRequestBeDeltaEncoded(const net::URLRequest* request) {
164 struct {
165 const char *prefix;
166 const char *suffix;
167 } kEligibleMasks[] = {
168 // All text/ types are eligible, even if not displayable.
169 { "text/", NULL },
170 // JSON (application/json and application/*+json) is eligible.
171 { "application/", "json" },
172 // Javascript is eligible.
173 { "application/", "javascript" },
174 // XML (application/xml and application/*+xml) is eligible.
175 { "application/", "xml" },
176 };
ellyjonesd84033d2015-02-12 19:20:34177
178 std::string mime_type;
179 request->GetMimeType(&mime_type);
180
181 for (size_t i = 0; i < arraysize(kEligibleMasks); i++) {
182 const char *prefix = kEligibleMasks[i].prefix;
183 const char *suffix = kEligibleMasks[i].suffix;
brettw66d1b81b2015-07-06 19:29:40184 if (prefix &&
185 !base::StartsWith(mime_type, prefix, base::CompareCase::SENSITIVE))
ellyjonesd84033d2015-02-12 19:20:34186 continue;
brettw66d1b81b2015-07-06 19:29:40187 if (suffix &&
188 !base::EndsWith(mime_type, suffix, base::CompareCase::SENSITIVE))
ellyjonesd84033d2015-02-12 19:20:34189 continue;
190 return true;
191 }
192 return false;
193}
194
195// Returns whether |request| was issued by a renderer process, as opposed to
196// the browser process or a plugin process.
197bool IsRendererInitiatedRequest(const net::URLRequest* request) {
198 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
199 return info && info->GetProcessType() == content::PROCESS_TYPE_RENDERER;
200}
201
202// Uploads UMA histograms for delta encoding eligibility. This method can only
203// be safely called after the network stack has called both OnStarted and
204// OnCompleted, since it needs the received response content length and the
205// response headers.
206void RecordCacheStateStats(const net::URLRequest* request) {
207 net::HttpRequestHeaders request_headers;
208 if (!request->GetFullRequestHeaders(&request_headers)) {
209 // GetFullRequestHeaders is guaranteed to succeed if OnResponseStarted() has
210 // been called on |request|, so if GetFullRequestHeaders() fails,
211 // RecordCacheStateStats must have been called before
212 // OnResponseStarted().
213 return;
214 }
215
216 if (!IsRendererInitiatedRequest(request)) {
217 // Ignore browser-initiated requests. These are internal requests like safe
218 // browsing and sync, and so on. Some of these could be eligible for
219 // delta-encoding, but to be conservative this function ignores all of them.
220 return;
221 }
222
223 const int kCacheAffectingFlags = net::LOAD_BYPASS_CACHE |
224 net::LOAD_DISABLE_CACHE |
225 net::LOAD_PREFERRING_CACHE;
226
227 if (request->load_flags() & kCacheAffectingFlags) {
228 // Ignore requests with cache-affecting flags, which would otherwise mess up
229 // these stats.
230 return;
231 }
232
233 enum {
234 CACHE_STATE_FROM_CACHE,
235 CACHE_STATE_STILL_VALID,
236 CACHE_STATE_NO_LONGER_VALID,
237 CACHE_STATE_NO_ENTRY,
238 CACHE_STATE_MAX,
239 } state = CACHE_STATE_NO_ENTRY;
240 bool had_cache_headers =
241 request_headers.HasHeader(net::HttpRequestHeaders::kIfModifiedSince) ||
242 request_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch) ||
243 request_headers.HasHeader(net::HttpRequestHeaders::kIfRange);
244 if (request->was_cached() && !had_cache_headers) {
245 // Entry was served directly from cache.
246 state = CACHE_STATE_FROM_CACHE;
247 } else if (request->was_cached() && had_cache_headers) {
248 // Expired entry was present in cache, and server responded with NOT
249 // MODIFIED, indicating the expired entry is still valid.
250 state = CACHE_STATE_STILL_VALID;
251 } else if (!request->was_cached() && had_cache_headers) {
252 // Expired entry was present in cache, and server responded with something
253 // other than NOT MODIFIED, indicating the entry is no longer valid.
254 state = CACHE_STATE_NO_LONGER_VALID;
255 } else if (!request->was_cached() && !had_cache_headers) {
256 // Neither |was_cached| nor |had_cache_headers|, so there's no local cache
257 // entry for this content at all.
258 state = CACHE_STATE_NO_ENTRY;
259 }
260
261 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.AllRequests", state,
262 CACHE_STATE_MAX);
263 if (CanRequestBeDeltaEncoded(request)) {
264 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.EncodeableRequests", state,
265 CACHE_STATE_MAX);
266 }
267
268 int64 size = request->received_response_content_length();
269 if (size >= 0 && state == CACHE_STATE_NO_LONGER_VALID) {
270 UMA_HISTOGRAM_COUNTS("Net.CacheState.AllBytes", size);
271 if (CanRequestBeDeltaEncoded(request)) {
272 UMA_HISTOGRAM_COUNTS("Net.CacheState.EncodeableBytes", size);
273 }
274 }
275}
276
[email protected]d05ef99c2011-02-01 21:38:16277} // namespace
[email protected]ac039522010-06-15 16:39:44278
[email protected]0651b812011-02-24 00:22:50279ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10280 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48281 BooleanPrefMember* enable_referrers)
[email protected]4a2b6232014-06-19 08:44:14282 : profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59283 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48284 enable_do_not_track_(NULL),
285 force_google_safe_search_(NULL),
treib4e26f6652014-12-01 14:34:18286 force_youtube_safety_mode_(NULL),
[email protected]4a2b6232014-06-19 08:44:14287#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]a09159a2012-11-29 12:51:48288 url_blacklist_manager_(NULL),
[email protected]4a2b6232014-06-19 08:44:14289#endif
[email protected]45de676a2014-03-18 23:52:02290 domain_reliability_monitor_(NULL),
mkwst0513c9d2015-04-01 05:53:15291 experimental_web_platform_features_enabled_(
292 base::CommandLine::ForCurrentProcess()->HasSwitch(
thestig397accd2015-04-04 00:03:04293 switches::kEnableExperimentalWebPlatformFeatures)) {
[email protected]0a8db0d2011-04-13 15:15:40294 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14295 extensions_delegate_.reset(
296 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50297}
298
[email protected]ac039522010-06-15 16:39:44299ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
300
[email protected]a09159a2012-11-29 12:51:48301void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20302 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14303 extensions_delegate_->set_extension_info_map(extension_info_map);
304}
305
306void ChromeNetworkDelegate::set_profile(void* profile) {
307 profile_ = profile;
308 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48309}
310
311void ChromeNetworkDelegate::set_cookie_settings(
drogerb1716972015-06-30 09:04:09312 content_settings::CookieSettings* cookie_settings) {
[email protected]a09159a2012-11-29 12:51:48313 cookie_settings_ = cookie_settings;
314}
315
[email protected]f53b4802012-12-20 17:04:23316void ChromeNetworkDelegate::set_predictor(
317 chrome_browser_net::Predictor* predictor) {
318 connect_interceptor_.reset(
319 new chrome_browser_net::ConnectInterceptor(predictor));
320}
321
[email protected]c4a7df82012-08-09 22:48:46322// static
[email protected]9d8cfb682012-09-13 16:48:04323void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40324 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04325 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33326 BooleanPrefMember* force_google_safe_search,
treib4e26f6652014-12-01 14:34:18327 BooleanPrefMember* force_youtube_safety_mode,
[email protected]0a8db0d2011-04-13 15:15:40328 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02329 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02330 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43331 enable_referrers->MoveToThread(
332 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04333 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02334 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43335 enable_do_not_track->MoveToThread(
336 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04337 }
[email protected]d1208ba32012-11-08 11:10:33338 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18339 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33340 force_google_safe_search->MoveToThread(
341 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
342 }
treib4e26f6652014-12-01 14:34:18343 if (force_youtube_safety_mode) {
344 force_youtube_safety_mode->Init(prefs::kForceYouTubeSafetyMode,
345 pref_service);
346 force_youtube_safety_mode->MoveToThread(
347 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
348 }
[email protected]0a8db0d2011-04-13 15:15:40349}
350
[email protected]4c219e22012-05-05 19:41:04351// static
352void ChromeNetworkDelegate::AllowAccessToAllFiles() {
353 g_allow_file_access_ = true;
354}
355
[email protected]4875ba12011-03-30 22:31:51356int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12357 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47358 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12359 GURL* new_url) {
mmenke376b8782015-05-05 04:41:30360 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
361 tracked_objects::ScopedTracker tracking_profile1(
362 FROM_HERE_WITH_EXPLICIT_FUNCTION(
363 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest"));
364
[email protected]3e598ff12011-09-06 11:22:34365#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59366 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
367 // blocked. However, an extension might redirect the request to another URL,
368 // which is not blocked.
mmenkef1aa9072015-05-18 16:21:04369
370 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
[email protected]cb85f8e52014-04-08 11:44:40371 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
mmenkef1aa9072015-05-18 16:21:04372 if (info && content::IsResourceTypeFrame(info->GetResourceType()) &&
373 url_blacklist_manager_ &&
374 url_blacklist_manager_->ShouldBlockRequestForFrame(
375 request->url(), &error)) {
[email protected]6a5f77c32011-09-04 19:19:59376 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59377 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26378 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
379 net::NetLog::StringCallback("url",
380 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40381 return error;
[email protected]6a5f77c32011-09-04 19:19:59382 }
[email protected]3e598ff12011-09-06 11:22:34383#endif
[email protected]6a5f77c32011-09-04 19:19:59384
mmenke376b8782015-05-05 04:41:30385 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
386 tracked_objects::ScopedTracker tracking_profile2(
387 FROM_HERE_WITH_EXPLICIT_FUNCTION(
388 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 2"));
389
[email protected]4a2b6232014-06-19 08:44:14390 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18391
[email protected]0a8db0d2011-04-13 15:15:40392 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13393 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04394 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
395 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33396
mmenke376b8782015-05-05 04:41:30397 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
398 tracked_objects::ScopedTracker tracking_profile3(
399 FROM_HERE_WITH_EXPLICIT_FUNCTION(
400 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 3"));
401
treib4e26f6652014-12-01 14:34:18402 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18403 (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
mmenke376b8782015-05-05 04:41:30416 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
417 tracked_objects::ScopedTracker tracking_profile4(
418 FROM_HERE_WITH_EXPLICIT_FUNCTION(
419 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 4"));
420
[email protected]d1208ba32012-11-08 11:10:33421 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24422 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33423
mmenke376b8782015-05-05 04:41:30424 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
425 tracked_objects::ScopedTracker tracking_profile5(
426 FROM_HERE_WITH_EXPLICIT_FUNCTION(
427 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 5"));
428
[email protected]f53b4802012-12-20 17:04:23429 if (connect_interceptor_)
430 connect_interceptor_->WitnessURLRequest(request);
431
[email protected]d1208ba32012-11-08 11:10:33432 return rv;
[email protected]d05ef99c2011-02-01 21:38:16433}
434
[email protected]4875ba12011-03-30 22:31:51435int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01436 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47437 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12438 net::HttpRequestHeaders* headers) {
treib2b188052015-04-17 12:32:50439 if (force_youtube_safety_mode_ && force_youtube_safety_mode_->GetValue())
[email protected]184799df2014-07-22 16:03:24440 safe_search_util::ForceYouTubeSafetyMode(request, headers);
441
[email protected]4a2b6232014-06-19 08:44:14442 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
[email protected]ac039522010-06-15 16:39:44443}
[email protected]8202d0c2011-02-23 08:31:14444
[email protected]5796dc942011-07-14 19:26:10445void ChromeNetworkDelegate::OnSendHeaders(
446 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15447 const net::HttpRequestHeaders& headers) {
[email protected]4a2b6232014-06-19 08:44:14448 extensions_delegate_->OnSendHeaders(request, headers);
[email protected]82b42302011-04-20 16:28:16449}
450
[email protected]ea8141e2011-10-05 13:12:51451int ChromeNetworkDelegate::OnHeadersReceived(
452 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47453 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32454 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16455 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
456 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14457 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16458 request,
459 callback,
460 original_response_headers,
461 override_response_headers,
462 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51463}
464
[email protected]31b2e5f2011-04-20 16:58:32465void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
466 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02467 if (domain_reliability_monitor_)
468 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14469 extensions_delegate_->OnBeforeRedirect(request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32470}
471
472
[email protected]8202d0c2011-02-23 08:31:14473void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14474 extensions_delegate_->OnResponseStarted(request);
[email protected]8202d0c2011-02-23 08:31:14475}
476
[email protected]8523ba52011-05-22 19:00:58477void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
478 int bytes_read) {
[email protected]44879ed2012-04-06 01:11:02479#if defined(ENABLE_TASK_MANAGER)
[email protected]17df9e7c2014-02-13 03:49:29480 // This is not completely accurate, but as a first approximation ignore
481 // requests that are served from the cache. See bug 330931 for more info.
pkasting941842c2015-04-11 01:51:30482 if (!request.was_cached())
483 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02484#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58485}
486
[email protected]9045b8822012-01-13 20:35:35487void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
488 bool started) {
sclittle3c16159e2014-12-15 19:53:17489 RecordNetworkErrorHistograms(request);
ellyjonesd84033d2015-02-12 19:20:34490 if (started) {
491 // Only call in for requests that were started, to obey the precondition
492 // that RecordCacheStateStats can only be called on requests for which
493 // OnResponseStarted was called.
494 RecordCacheStateStats(request);
495 }
sclittle3c16159e2014-12-15 19:53:17496
[email protected]2756a8e2012-09-07 18:24:29497 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
megjablonc1751452014-12-09 19:46:47498#if defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50499 // For better accuracy, we use the actual bytes read instead of the length
500 // specified with the Content-Length header, which may be inaccurate,
501 // or missing, as is the case with chunked encoding.
502 int64 received_content_length = request->received_response_content_length();
503
[email protected]35558812013-12-18 21:58:36504 if (precache::PrecacheManager::IsPrecachingEnabled()) {
505 // Record precache metrics when a fetch is completed successfully, if
506 // precaching is enabled.
507 BrowserThread::PostTask(
508 BrowserThread::UI, FROM_HERE,
509 base::Bind(&RecordPrecacheStatsOnUIThread, request->url(),
510 base::Time::Now(), received_content_length,
511 request->was_cached(), profile_));
512 }
513#endif // defined(OS_ANDROID)
[email protected]4a2b6232014-06-19 08:44:14514 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01515 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
516 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]4a2b6232014-06-19 08:44:14517 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01518 } else {
519 NOTREACHED();
[email protected]48944382011-04-23 13:28:16520 }
[email protected]45de676a2014-03-18 23:52:02521 if (domain_reliability_monitor_)
522 domain_reliability_monitor_->OnCompleted(request, started);
[email protected]4a2b6232014-06-19 08:44:14523 extensions_delegate_->ForwardProxyErrors(request);
524 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14525}
[email protected]4b50cb52011-03-10 00:29:37526
[email protected]4875ba12011-03-30 22:31:51527void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14528 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51529}
530
[email protected]82a37672011-05-03 12:02:41531void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09532 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14533 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41534}
[email protected]7efc582d2011-08-03 20:46:35535
[email protected]c2911d72011-10-03 22:16:36536net::NetworkDelegate::AuthRequiredResponse
537ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35538 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36539 const net::AuthChallengeInfo& auth_info,
540 const AuthCallback& callback,
541 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14542 return extensions_delegate_->OnAuthRequired(
543 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35544}
[email protected]9c8ae8c2012-03-09 13:13:35545
[email protected]4c219e22012-05-05 19:41:04546bool ChromeNetworkDelegate::OnCanGetCookies(
547 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35548 const net::CookieList& cookie_list) {
549 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02550 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35551 return true;
552
553 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04554 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35555
556 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16557 int render_frame_id = -1;
558 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
559 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35560 BrowserThread::PostTask(
561 BrowserThread::UI, FROM_HERE,
562 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16563 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04564 request.url(), request.first_party_for_cookies(),
davidbenf47d1912015-03-14 00:24:24565 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35566 }
567
568 return allow;
569}
570
[email protected]4c219e22012-05-05 19:41:04571bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
572 const std::string& cookie_line,
573 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35574 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02575 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35576 return true;
577
578 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04579 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35580
[email protected]9c8ae8c2012-03-09 13:13:35581 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16582 int render_frame_id = -1;
583 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
584 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35585 BrowserThread::PostTask(
586 BrowserThread::UI, FROM_HERE,
587 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16588 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04589 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43590 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35591 }
592
593 return allow;
594}
[email protected]4c219e22012-05-05 19:41:04595
596bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45597 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04598 if (g_allow_file_access_)
599 return true;
600
[email protected]d8e4f132012-09-06 04:28:05601#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
602 return true;
603#else
[email protected]4c219e22012-05-05 19:41:04604#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05605 // If we're running Chrome for ChromeOS on Linux, we want to allow file
606 // access.
[email protected]49c4cf852013-09-27 19:28:24607 if (!base::SysInfo::IsRunningOnChromeOS() ||
avi556c05022014-12-22 23:31:43608 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05609 return true;
[email protected]288538482012-09-06 21:09:35610 }
[email protected]d8e4f132012-09-06 04:28:05611
612 // Use a whitelist to only allow access to files residing in the list of
613 // directories below.
[email protected]4c219e22012-05-05 19:41:04614 static const char* const kLocalAccessWhiteList[] = {
615 "/home/chronos/user/Downloads",
616 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42617 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04618 "/media",
619 "/opt/oem",
620 "/usr/share/chromeos-assets",
621 "/tmp",
622 "/var/log",
623 };
[email protected]ae015e12013-11-04 19:11:33624
[email protected]9e733282014-06-18 16:56:55625 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
626 // the profile path ("/home/chronos/user' is a hard link to current primary
627 // logged in profile.) For the support of multi-profile sessions, we are
628 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
629 // access.
[email protected]ae015e12013-11-04 19:11:33630 if (!profile_path_.empty()) {
631 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
632 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
633 return true;
[email protected]9e733282014-06-18 16:56:55634 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
635 if (webrtc_logs == path.StripTrailingSeparators() ||
636 webrtc_logs.IsParent(path)) {
637 return true;
638 }
[email protected]ae015e12013-11-04 19:11:33639 }
[email protected]d8e4f132012-09-06 04:28:05640#elif defined(OS_ANDROID)
641 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45642 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05643 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
644 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04645 return true;
646
[email protected]d8e4f132012-09-06 04:28:05647 // Whitelist of other allowed directories.
648 static const char* const kLocalAccessWhiteList[] = {
649 "/sdcard",
650 "/mnt/sdcard",
651 };
652#endif
653
[email protected]4c219e22012-05-05 19:41:04654 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45655 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
656 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04657 if (white_listed_path == path.StripTrailingSeparators() ||
658 white_listed_path.IsParent(path)) {
659 return true;
660 }
661 }
[email protected]d8e4f132012-09-06 04:28:05662
[email protected]288538482012-09-06 21:09:35663 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04664 return false;
[email protected]d8e4f132012-09-06 04:28:05665#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04666}
[email protected]a1d4ab072012-06-07 13:21:15667
[email protected]e6d017652013-05-17 18:01:40668bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
669 const GURL& url,
670 const GURL& first_party_for_cookies) const {
671 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02672 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40673 return false;
674
675 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
676 url, first_party_for_cookies);
677 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
678 url, first_party_for_cookies);
679 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
680 return privacy_mode;
681}
682
mkwst0513c9d2015-04-01 05:53:15683bool ChromeNetworkDelegate::OnFirstPartyOnlyCookieExperimentEnabled() const {
684 return experimental_web_platform_features_enabled_;
685}
686
jochen0e3b3a62014-09-16 18:31:23687bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
688 const net::URLRequest& request,
689 const GURL& target_url,
690 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27691 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23692 return true;
693}