blob: ef78b9eda44f18d85de7a6d32e49ec5af6869aed [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"
afakhry05015032015-08-14 01:09:5636#include "chrome/browser/task_management/task_manager_interface.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"
sclittleae932be2015-10-08 20:53:5039#include "components/data_usage/core/data_use_aggregator.h"
[email protected]45de676a2014-03-18 23:52:0240#include "components/domain_reliability/monitor.h"
[email protected]c38831a12011-10-28 12:44:4941#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5642#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3343#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3544#include "content/public/browser/resource_request_info.h"
mkwst0513c9d2015-04-01 05:53:1545#include "content/public/common/content_switches.h"
ellyjonesd84033d2015-02-12 19:20:3446#include "content/public/common/process_type.h"
[email protected]82b42302011-04-20 16:28:1647#include "net/base/host_port_pair.h"
sclittle3c16159e2014-12-15 19:53:1748#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1449#include "net/base/net_errors.h"
[email protected]5b9bc352012-07-18 13:13:3450#include "net/cookies/canonical_cookie.h"
51#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4452#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1653#include "net/http/http_response_headers.h"
ellyjonesd84033d2015-02-12 19:20:3454#include "net/http/http_status_code.h"
eroman87c53d62015-04-02 06:51:0755#include "net/log/net_log.h"
[email protected]d05ef99c2011-02-01 21:38:1656#include "net/url_request/url_request.h"
57
[email protected]4a2b6232014-06-19 08:44:1458#if defined(OS_ANDROID)
59#include "chrome/browser/io_thread.h"
twifkak85f97872015-06-24 01:00:1360#include "chrome/browser/precache/precache_manager_factory.h"
[email protected]4a2b6232014-06-19 08:44:1461#include "components/precache/content/precache_manager.h"
[email protected]4a2b6232014-06-19 08:44:1462#endif
63
[email protected]4c219e22012-05-05 19:41:0464#if defined(OS_CHROMEOS)
[email protected]49c4cf852013-09-27 19:28:2465#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3566#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0467#endif
68
[email protected]3e598ff12011-09-06 11:22:3469#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]f522afa2014-01-08 15:36:3670#include "components/policy/core/browser/url_blacklist_manager.h"
[email protected]3e598ff12011-09-06 11:22:3471#endif
72
[email protected]84b7a552014-07-19 04:52:0673#if defined(ENABLE_EXTENSIONS)
74#include "extensions/common/constants.h"
75#endif
76
[email protected]631bb742011-11-02 11:29:3977using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1178using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2579using content::ResourceRequestInfo;
[email protected]7491ad02014-07-05 19:10:0780using content::ResourceType;
[email protected]631bb742011-11-02 11:29:3981
[email protected]d8e4f132012-09-06 04:28:0582// By default we don't allow access to all file:// urls on ChromeOS and
83// Android.
84#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0485bool ChromeNetworkDelegate::g_allow_file_access_ = false;
86#else
87bool ChromeNetworkDelegate::g_allow_file_access_ = true;
88#endif
89
[email protected]d05ef99c2011-02-01 21:38:1690namespace {
91
[email protected]9d8cfb682012-09-13 16:48:0492const char kDNTHeader[] = "DNT";
93
[email protected]d1208ba32012-11-08 11:10:3394// Gets called when the extensions finish work on the URL. If the extensions
95// did not do a redirect (so |new_url| is empty) then we enforce the
96// SafeSearch parameters. Otherwise we will get called again after the
97// redirect and we enforce SafeSearch then.
98void ForceGoogleSafeSearchCallbackWrapper(
99 const net::CompletionCallback& callback,
100 net::URLRequest* request,
101 GURL* new_url,
102 int rv) {
103 if (rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24104 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33105 callback.Run(rv);
106}
107
[email protected]35558812013-12-18 21:58:36108#if defined(OS_ANDROID)
109void RecordPrecacheStatsOnUIThread(const GURL& url,
twifkak981c7ea2015-08-13 17:56:08110 const GURL& referrer,
twifkak173e7512015-08-11 18:53:30111 base::TimeDelta latency,
112 const base::Time& fetch_time,
113 int64 size,
114 bool was_cached,
115 void* profile_id) {
anujk.sharma2e02ce162015-04-29 23:10:02116 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]35558812013-12-18 21:58:36117
118 Profile* profile = reinterpret_cast<Profile*>(profile_id);
twifkakb2175432015-08-07 20:19:07119 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
[email protected]35558812013-12-18 21:58:36120 return;
[email protected]35558812013-12-18 21:58:36121
122 precache::PrecacheManager* precache_manager =
123 precache::PrecacheManagerFactory::GetForBrowserContext(profile);
twifkakb2175432015-08-07 20:19:07124 // |precache_manager| could be NULL if the profile is off the record.
twifkakd0487ff2015-08-28 16:08:22125 if (!precache_manager || !precache_manager->IsPrecachingAllowed())
[email protected]35558812013-12-18 21:58:36126 return;
[email protected]35558812013-12-18 21:58:36127
twifkak981c7ea2015-08-13 17:56:08128 precache_manager->RecordStatsForFetch(url, referrer, latency, fetch_time,
129 size, was_cached);
[email protected]35558812013-12-18 21:58:36130}
131#endif // defined(OS_ANDROID)
132
jochen652275372015-01-16 11:35:27133void ReportInvalidReferrerSendOnUI() {
jochen0e3b3a62014-09-16 18:31:23134 base::RecordAction(
135 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
jochen652275372015-01-16 11:35:27136}
137
138void ReportInvalidReferrerSend(const GURL& target_url,
139 const GURL& referrer_url) {
jochen4c874d22015-04-13 14:01:04140 LOG(ERROR) << "Cancelling request to " << target_url
141 << " with invalid referrer " << referrer_url;
jochen652275372015-01-16 11:35:27142 // Record information to help debug http://crbug.com/422871
143 if (!target_url.SchemeIsHTTPOrHTTPS())
144 return;
145 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
146 base::Bind(&ReportInvalidReferrerSendOnUI));
jochenb5680e62014-10-13 08:43:41147 base::debug::DumpWithoutCrashing();
jochen4c874d22015-04-13 14:01:04148 NOTREACHED();
jochen0e3b3a62014-09-16 18:31:23149}
150
sclittle3c16159e2014-12-15 19:53:17151// Record network errors that HTTP requests complete with, including OK and
152// ABORTED.
153void RecordNetworkErrorHistograms(const net::URLRequest* request) {
154 if (request->url().SchemeIs("http")) {
155 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
156 std::abs(request->status().error()));
157
158 if (request->load_flags() & net::LOAD_MAIN_FRAME) {
159 UMA_HISTOGRAM_SPARSE_SLOWLY(
160 "Net.HttpRequestCompletionErrorCodes.MainFrame",
161 std::abs(request->status().error()));
162 }
163 }
164}
165
ellyjonesd84033d2015-02-12 19:20:34166// Returns whether |request| is likely to be eligible for delta-encoding.
167// This is only a rough approximation right now, based on MIME type.
168bool CanRequestBeDeltaEncoded(const net::URLRequest* request) {
169 struct {
170 const char *prefix;
171 const char *suffix;
172 } kEligibleMasks[] = {
173 // All text/ types are eligible, even if not displayable.
174 { "text/", NULL },
175 // JSON (application/json and application/*+json) is eligible.
176 { "application/", "json" },
177 // Javascript is eligible.
178 { "application/", "javascript" },
179 // XML (application/xml and application/*+xml) is eligible.
180 { "application/", "xml" },
181 };
ellyjonesd84033d2015-02-12 19:20:34182
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;
brettw66d1b81b2015-07-06 19:29:40189 if (prefix &&
190 !base::StartsWith(mime_type, prefix, base::CompareCase::SENSITIVE))
ellyjonesd84033d2015-02-12 19:20:34191 continue;
brettw66d1b81b2015-07-06 19:29:40192 if (suffix &&
193 !base::EndsWith(mime_type, suffix, base::CompareCase::SENSITIVE))
ellyjonesd84033d2015-02-12 19:20:34194 continue;
195 return true;
196 }
197 return false;
198}
199
200// Returns whether |request| was issued by a renderer process, as opposed to
201// the browser process or a plugin process.
202bool IsRendererInitiatedRequest(const net::URLRequest* request) {
203 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
204 return info && info->GetProcessType() == content::PROCESS_TYPE_RENDERER;
205}
206
207// Uploads UMA histograms for delta encoding eligibility. This method can only
208// be safely called after the network stack has called both OnStarted and
209// OnCompleted, since it needs the received response content length and the
210// response headers.
211void RecordCacheStateStats(const net::URLRequest* request) {
212 net::HttpRequestHeaders request_headers;
213 if (!request->GetFullRequestHeaders(&request_headers)) {
214 // GetFullRequestHeaders is guaranteed to succeed if OnResponseStarted() has
215 // been called on |request|, so if GetFullRequestHeaders() fails,
216 // RecordCacheStateStats must have been called before
217 // OnResponseStarted().
218 return;
219 }
220
221 if (!IsRendererInitiatedRequest(request)) {
222 // Ignore browser-initiated requests. These are internal requests like safe
223 // browsing and sync, and so on. Some of these could be eligible for
224 // delta-encoding, but to be conservative this function ignores all of them.
225 return;
226 }
227
228 const int kCacheAffectingFlags = net::LOAD_BYPASS_CACHE |
229 net::LOAD_DISABLE_CACHE |
230 net::LOAD_PREFERRING_CACHE;
231
232 if (request->load_flags() & kCacheAffectingFlags) {
233 // Ignore requests with cache-affecting flags, which would otherwise mess up
234 // these stats.
235 return;
236 }
237
238 enum {
239 CACHE_STATE_FROM_CACHE,
240 CACHE_STATE_STILL_VALID,
241 CACHE_STATE_NO_LONGER_VALID,
242 CACHE_STATE_NO_ENTRY,
243 CACHE_STATE_MAX,
244 } state = CACHE_STATE_NO_ENTRY;
245 bool had_cache_headers =
246 request_headers.HasHeader(net::HttpRequestHeaders::kIfModifiedSince) ||
247 request_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch) ||
248 request_headers.HasHeader(net::HttpRequestHeaders::kIfRange);
249 if (request->was_cached() && !had_cache_headers) {
250 // Entry was served directly from cache.
251 state = CACHE_STATE_FROM_CACHE;
252 } else if (request->was_cached() && had_cache_headers) {
253 // Expired entry was present in cache, and server responded with NOT
254 // MODIFIED, indicating the expired entry is still valid.
255 state = CACHE_STATE_STILL_VALID;
256 } else if (!request->was_cached() && had_cache_headers) {
257 // Expired entry was present in cache, and server responded with something
258 // other than NOT MODIFIED, indicating the entry is no longer valid.
259 state = CACHE_STATE_NO_LONGER_VALID;
260 } else if (!request->was_cached() && !had_cache_headers) {
261 // Neither |was_cached| nor |had_cache_headers|, so there's no local cache
262 // entry for this content at all.
263 state = CACHE_STATE_NO_ENTRY;
264 }
265
266 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.AllRequests", state,
267 CACHE_STATE_MAX);
268 if (CanRequestBeDeltaEncoded(request)) {
269 UMA_HISTOGRAM_ENUMERATION("Net.CacheState.EncodeableRequests", state,
270 CACHE_STATE_MAX);
271 }
272
273 int64 size = request->received_response_content_length();
274 if (size >= 0 && state == CACHE_STATE_NO_LONGER_VALID) {
275 UMA_HISTOGRAM_COUNTS("Net.CacheState.AllBytes", size);
276 if (CanRequestBeDeltaEncoded(request)) {
277 UMA_HISTOGRAM_COUNTS("Net.CacheState.EncodeableBytes", size);
278 }
279 }
280}
281
[email protected]d05ef99c2011-02-01 21:38:16282} // namespace
[email protected]ac039522010-06-15 16:39:44283
[email protected]0651b812011-02-24 00:22:50284ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10285 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48286 BooleanPrefMember* enable_referrers)
[email protected]4a2b6232014-06-19 08:44:14287 : profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59288 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48289 enable_do_not_track_(NULL),
290 force_google_safe_search_(NULL),
treib4e26f6652014-12-01 14:34:18291 force_youtube_safety_mode_(NULL),
[email protected]4a2b6232014-06-19 08:44:14292#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]a09159a2012-11-29 12:51:48293 url_blacklist_manager_(NULL),
[email protected]4a2b6232014-06-19 08:44:14294#endif
[email protected]45de676a2014-03-18 23:52:02295 domain_reliability_monitor_(NULL),
mkwst0513c9d2015-04-01 05:53:15296 experimental_web_platform_features_enabled_(
sclittleae932be2015-10-08 20:53:50297 base::CommandLine::ForCurrentProcess()
298 ->HasSwitch(switches::kEnableExperimentalWebPlatformFeatures)),
299 data_use_aggregator_(nullptr),
300 is_data_usage_off_the_record_(true) {
[email protected]0a8db0d2011-04-13 15:15:40301 DCHECK(enable_referrers);
[email protected]4a2b6232014-06-19 08:44:14302 extensions_delegate_.reset(
303 ChromeExtensionsNetworkDelegate::Create(event_router));
[email protected]0651b812011-02-24 00:22:50304}
305
[email protected]ac039522010-06-15 16:39:44306ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
307
[email protected]a09159a2012-11-29 12:51:48308void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20309 extensions::InfoMap* extension_info_map) {
[email protected]4a2b6232014-06-19 08:44:14310 extensions_delegate_->set_extension_info_map(extension_info_map);
311}
312
313void ChromeNetworkDelegate::set_profile(void* profile) {
314 profile_ = profile;
315 extensions_delegate_->set_profile(profile);
[email protected]a09159a2012-11-29 12:51:48316}
317
318void ChromeNetworkDelegate::set_cookie_settings(
drogerb1716972015-06-30 09:04:09319 content_settings::CookieSettings* cookie_settings) {
[email protected]a09159a2012-11-29 12:51:48320 cookie_settings_ = cookie_settings;
321}
322
[email protected]f53b4802012-12-20 17:04:23323void ChromeNetworkDelegate::set_predictor(
324 chrome_browser_net::Predictor* predictor) {
325 connect_interceptor_.reset(
326 new chrome_browser_net::ConnectInterceptor(predictor));
327}
328
sclittleae932be2015-10-08 20:53:50329void ChromeNetworkDelegate::set_data_use_aggregator(
330 data_usage::DataUseAggregator* data_use_aggregator,
331 bool is_data_usage_off_the_record) {
332 data_use_aggregator_ = data_use_aggregator;
333 is_data_usage_off_the_record_ = is_data_usage_off_the_record;
334}
335
[email protected]c4a7df82012-08-09 22:48:46336// static
[email protected]9d8cfb682012-09-13 16:48:04337void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40338 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04339 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33340 BooleanPrefMember* force_google_safe_search,
treib4e26f6652014-12-01 14:34:18341 BooleanPrefMember* force_youtube_safety_mode,
[email protected]0a8db0d2011-04-13 15:15:40342 PrefService* pref_service) {
anujk.sharma2e02ce162015-04-29 23:10:02343 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]96a5c342012-12-04 18:14:02344 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43345 enable_referrers->MoveToThread(
346 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04347 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02348 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43349 enable_do_not_track->MoveToThread(
350 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04351 }
[email protected]d1208ba32012-11-08 11:10:33352 if (force_google_safe_search) {
treib4e26f6652014-12-01 14:34:18353 force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33354 force_google_safe_search->MoveToThread(
355 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
356 }
treib4e26f6652014-12-01 14:34:18357 if (force_youtube_safety_mode) {
358 force_youtube_safety_mode->Init(prefs::kForceYouTubeSafetyMode,
359 pref_service);
360 force_youtube_safety_mode->MoveToThread(
361 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
362 }
[email protected]0a8db0d2011-04-13 15:15:40363}
364
[email protected]4c219e22012-05-05 19:41:04365// static
366void ChromeNetworkDelegate::AllowAccessToAllFiles() {
367 g_allow_file_access_ = true;
368}
369
[email protected]4875ba12011-03-30 22:31:51370int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12371 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47372 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12373 GURL* new_url) {
mmenke376b8782015-05-05 04:41:30374 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
375 tracked_objects::ScopedTracker tracking_profile1(
376 FROM_HERE_WITH_EXPLICIT_FUNCTION(
377 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest"));
378
[email protected]3e598ff12011-09-06 11:22:34379#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59380 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
381 // blocked. However, an extension might redirect the request to another URL,
382 // which is not blocked.
mmenkef1aa9072015-05-18 16:21:04383
384 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
[email protected]cb85f8e52014-04-08 11:44:40385 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
mmenkef1aa9072015-05-18 16:21:04386 if (info && content::IsResourceTypeFrame(info->GetResourceType()) &&
387 url_blacklist_manager_ &&
388 url_blacklist_manager_->ShouldBlockRequestForFrame(
389 request->url(), &error)) {
[email protected]6a5f77c32011-09-04 19:19:59390 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59391 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26392 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
393 net::NetLog::StringCallback("url",
394 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40395 return error;
[email protected]6a5f77c32011-09-04 19:19:59396 }
[email protected]3e598ff12011-09-06 11:22:34397#endif
[email protected]6a5f77c32011-09-04 19:19:59398
mmenke376b8782015-05-05 04:41:30399 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
400 tracked_objects::ScopedTracker tracking_profile2(
401 FROM_HERE_WITH_EXPLICIT_FUNCTION(
402 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 2"));
403
[email protected]4a2b6232014-06-19 08:44:14404 extensions_delegate_->ForwardStartRequestStatus(request);
[email protected]6baff0b52012-03-06 01:30:18405
[email protected]0a8db0d2011-04-13 15:15:40406 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13407 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04408 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
409 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33410
mmenke376b8782015-05-05 04:41:30411 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
412 tracked_objects::ScopedTracker tracking_profile3(
413 FROM_HERE_WITH_EXPLICIT_FUNCTION(
414 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 3"));
415
treib4e26f6652014-12-01 14:34:18416 bool force_safe_search =
treib4e26f6652014-12-01 14:34:18417 (force_google_safe_search_ && force_google_safe_search_->GetValue());
[email protected]d1208ba32012-11-08 11:10:33418
419 net::CompletionCallback wrapped_callback = callback;
420 if (force_safe_search) {
421 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
422 callback,
423 base::Unretained(request),
424 base::Unretained(new_url));
425 }
426
[email protected]4a2b6232014-06-19 08:44:14427 int rv = extensions_delegate_->OnBeforeURLRequest(
428 request, wrapped_callback, new_url);
[email protected]d1208ba32012-11-08 11:10:33429
mmenke376b8782015-05-05 04:41:30430 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
431 tracked_objects::ScopedTracker tracking_profile4(
432 FROM_HERE_WITH_EXPLICIT_FUNCTION(
433 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 4"));
434
[email protected]d1208ba32012-11-08 11:10:33435 if (force_safe_search && rv == net::OK && new_url->is_empty())
[email protected]184799df2014-07-22 16:03:24436 safe_search_util::ForceGoogleSafeSearch(request, new_url);
[email protected]d1208ba32012-11-08 11:10:33437
mmenke376b8782015-05-05 04:41:30438 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
439 tracked_objects::ScopedTracker tracking_profile5(
440 FROM_HERE_WITH_EXPLICIT_FUNCTION(
441 "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 5"));
442
[email protected]f53b4802012-12-20 17:04:23443 if (connect_interceptor_)
444 connect_interceptor_->WitnessURLRequest(request);
445
[email protected]d1208ba32012-11-08 11:10:33446 return rv;
[email protected]d05ef99c2011-02-01 21:38:16447}
448
[email protected]4875ba12011-03-30 22:31:51449int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01450 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47451 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12452 net::HttpRequestHeaders* headers) {
treib2b188052015-04-17 12:32:50453 if (force_youtube_safety_mode_ && force_youtube_safety_mode_->GetValue())
[email protected]184799df2014-07-22 16:03:24454 safe_search_util::ForceYouTubeSafetyMode(request, headers);
455
[email protected]4a2b6232014-06-19 08:44:14456 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
[email protected]ac039522010-06-15 16:39:44457}
[email protected]8202d0c2011-02-23 08:31:14458
[email protected]5796dc942011-07-14 19:26:10459void ChromeNetworkDelegate::OnSendHeaders(
460 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15461 const net::HttpRequestHeaders& headers) {
[email protected]4a2b6232014-06-19 08:44:14462 extensions_delegate_->OnSendHeaders(request, headers);
[email protected]82b42302011-04-20 16:28:16463}
464
[email protected]ea8141e2011-10-05 13:12:51465int ChromeNetworkDelegate::OnHeadersReceived(
466 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47467 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32468 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16469 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
470 GURL* allowed_unsafe_redirect_url) {
[email protected]4a2b6232014-06-19 08:44:14471 return extensions_delegate_->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16472 request,
473 callback,
474 original_response_headers,
475 override_response_headers,
476 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51477}
478
[email protected]31b2e5f2011-04-20 16:58:32479void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
480 const GURL& new_location) {
amohammadkhan092adb22015-09-11 21:08:49481// Recording data use of request on redirects.
482#if !defined(OS_IOS)
483 data_use_measurement_.ReportDataUseUMA(request);
484#endif
[email protected]45de676a2014-03-18 23:52:02485 if (domain_reliability_monitor_)
486 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]4a2b6232014-06-19 08:44:14487 extensions_delegate_->OnBeforeRedirect(request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32488}
489
490
[email protected]8202d0c2011-02-23 08:31:14491void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14492 extensions_delegate_->OnResponseStarted(request);
[email protected]8202d0c2011-02-23 08:31:14493}
494
sclittlece72c482015-08-24 20:20:59495void ChromeNetworkDelegate::OnNetworkBytesReceived(
496 const net::URLRequest& request,
497 int64_t bytes_received) {
[email protected]44879ed2012-04-06 01:11:02498#if defined(ENABLE_TASK_MANAGER)
sclittlece72c482015-08-24 20:20:59499 // Note: Currently, OnNetworkBytesReceived is only implemented for HTTP jobs,
500 // not FTP or other types, so those kinds of bytes will not be reported here.
501 task_management::TaskManagerInterface::OnRawBytesRead(request,
502 bytes_received);
[email protected]44879ed2012-04-06 01:11:02503#endif // defined(ENABLE_TASK_MANAGER)
sclittleae932be2015-10-08 20:53:50504
505 if (data_use_aggregator_) {
506 if (is_data_usage_off_the_record_) {
507 data_use_aggregator_->ReportOffTheRecordDataUse(0 /* tx_bytes */,
508 bytes_received);
509 } else {
510 data_use_aggregator_->ReportDataUse(request, -1 /* tab_id */,
511 0 /* tx_bytes */, bytes_received);
512 }
513 }
514}
515
516void ChromeNetworkDelegate::OnNetworkBytesSent(const net::URLRequest& request,
517 int64_t bytes_sent) {
518 if (data_use_aggregator_) {
519 if (is_data_usage_off_the_record_) {
520 data_use_aggregator_->ReportOffTheRecordDataUse(bytes_sent,
521 0 /* rx_bytes */);
522 } else {
523 data_use_aggregator_->ReportDataUse(request, -1 /* tab_id */, bytes_sent,
524 0 /* rx_bytes */);
525 }
526 }
[email protected]8523ba52011-05-22 19:00:58527}
528
[email protected]9045b8822012-01-13 20:35:35529void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
530 bool started) {
amohammadkhan092adb22015-09-11 21:08:49531#if !defined(OS_IOS)
532 // TODO(amohammadkhan): Verify that there is no double recording in data use
533 // of redirected requests.
534 data_use_measurement_.ReportDataUseUMA(request);
535#endif
sclittle3c16159e2014-12-15 19:53:17536 RecordNetworkErrorHistograms(request);
ellyjonesd84033d2015-02-12 19:20:34537 if (started) {
538 // Only call in for requests that were started, to obey the precondition
539 // that RecordCacheStateStats can only be called on requests for which
540 // OnResponseStarted was called.
541 RecordCacheStateStats(request);
542 }
sclittle3c16159e2014-12-15 19:53:17543
[email protected]2756a8e2012-09-07 18:24:29544 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
megjablonc1751452014-12-09 19:46:47545#if defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50546 // For better accuracy, we use the actual bytes read instead of the length
547 // specified with the Content-Length header, which may be inaccurate,
548 // or missing, as is the case with chunked encoding.
549 int64 received_content_length = request->received_response_content_length();
twifkak173e7512015-08-11 18:53:30550 base::TimeDelta latency = base::TimeTicks::Now() - request->creation_time();
[email protected]7a299a92012-10-24 23:54:50551
twifkakb2175432015-08-07 20:19:07552 // Record precache metrics when a fetch is completed successfully, if
553 // precaching is allowed.
twifkakc07ac942015-07-20 18:23:04554 BrowserThread::PostTask(
555 BrowserThread::UI, FROM_HERE,
twifkak981c7ea2015-08-13 17:56:08556 base::Bind(&RecordPrecacheStatsOnUIThread, request->url(),
557 GURL(request->referrer()), latency, base::Time::Now(),
558 received_content_length, request->was_cached(), profile_));
[email protected]35558812013-12-18 21:58:36559#endif // defined(OS_ANDROID)
[email protected]4a2b6232014-06-19 08:44:14560 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01561 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
562 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]4a2b6232014-06-19 08:44:14563 extensions_delegate_->OnCompleted(request, started);
[email protected]a83dd332011-07-13 10:41:01564 } else {
565 NOTREACHED();
[email protected]48944382011-04-23 13:28:16566 }
[email protected]45de676a2014-03-18 23:52:02567 if (domain_reliability_monitor_)
568 domain_reliability_monitor_->OnCompleted(request, started);
ttuttle05ae3f342015-07-13 17:38:35569 RecordRequestSourceBandwidth(request, started);
[email protected]4a2b6232014-06-19 08:44:14570 extensions_delegate_->ForwardProxyErrors(request);
571 extensions_delegate_->ForwardDoneRequestStatus(request);
[email protected]8202d0c2011-02-23 08:31:14572}
[email protected]4b50cb52011-03-10 00:29:37573
[email protected]4875ba12011-03-30 22:31:51574void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
[email protected]4a2b6232014-06-19 08:44:14575 extensions_delegate_->OnURLRequestDestroyed(request);
[email protected]4875ba12011-03-30 22:31:51576}
577
davidbena3ef4382015-09-14 22:36:10578void ChromeNetworkDelegate::OnURLRequestJobOrphaned(net::URLRequest* request) {
579 extensions_delegate_->OnURLRequestJobOrphaned(request);
580}
581
[email protected]82a37672011-05-03 12:02:41582void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09583 const base::string16& error) {
[email protected]4a2b6232014-06-19 08:44:14584 extensions_delegate_->OnPACScriptError(line_number, error);
[email protected]82a37672011-05-03 12:02:41585}
[email protected]7efc582d2011-08-03 20:46:35586
[email protected]c2911d72011-10-03 22:16:36587net::NetworkDelegate::AuthRequiredResponse
588ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35589 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36590 const net::AuthChallengeInfo& auth_info,
591 const AuthCallback& callback,
592 net::AuthCredentials* credentials) {
[email protected]4a2b6232014-06-19 08:44:14593 return extensions_delegate_->OnAuthRequired(
594 request, auth_info, callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35595}
[email protected]9c8ae8c2012-03-09 13:13:35596
[email protected]4c219e22012-05-05 19:41:04597bool ChromeNetworkDelegate::OnCanGetCookies(
598 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35599 const net::CookieList& cookie_list) {
600 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02601 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35602 return true;
603
604 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04605 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35606
607 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16608 int render_frame_id = -1;
609 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
610 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35611 BrowserThread::PostTask(
612 BrowserThread::UI, FROM_HERE,
613 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16614 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04615 request.url(), request.first_party_for_cookies(),
davidbenf47d1912015-03-14 00:24:24616 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35617 }
618
619 return allow;
620}
621
[email protected]4c219e22012-05-05 19:41:04622bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
623 const std::string& cookie_line,
624 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35625 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02626 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35627 return true;
628
629 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04630 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35631
[email protected]9c8ae8c2012-03-09 13:13:35632 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16633 int render_frame_id = -1;
634 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
635 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35636 BrowserThread::PostTask(
637 BrowserThread::UI, FROM_HERE,
638 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16639 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04640 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43641 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35642 }
643
644 return allow;
645}
[email protected]4c219e22012-05-05 19:41:04646
647bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45648 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04649 if (g_allow_file_access_)
650 return true;
651
[email protected]d8e4f132012-09-06 04:28:05652#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
653 return true;
654#else
[email protected]4c219e22012-05-05 19:41:04655#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05656 // If we're running Chrome for ChromeOS on Linux, we want to allow file
657 // access.
[email protected]49c4cf852013-09-27 19:28:24658 if (!base::SysInfo::IsRunningOnChromeOS() ||
avi556c05022014-12-22 23:31:43659 base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05660 return true;
[email protected]288538482012-09-06 21:09:35661 }
[email protected]d8e4f132012-09-06 04:28:05662
663 // Use a whitelist to only allow access to files residing in the list of
664 // directories below.
[email protected]4c219e22012-05-05 19:41:04665 static const char* const kLocalAccessWhiteList[] = {
666 "/home/chronos/user/Downloads",
667 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42668 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04669 "/media",
670 "/opt/oem",
671 "/usr/share/chromeos-assets",
672 "/tmp",
673 "/var/log",
674 };
[email protected]ae015e12013-11-04 19:11:33675
[email protected]9e733282014-06-18 16:56:55676 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
677 // the profile path ("/home/chronos/user' is a hard link to current primary
678 // logged in profile.) For the support of multi-profile sessions, we are
679 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
680 // access.
[email protected]ae015e12013-11-04 19:11:33681 if (!profile_path_.empty()) {
682 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
683 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
684 return true;
[email protected]9e733282014-06-18 16:56:55685 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
686 if (webrtc_logs == path.StripTrailingSeparators() ||
687 webrtc_logs.IsParent(path)) {
688 return true;
689 }
[email protected]ae015e12013-11-04 19:11:33690 }
[email protected]d8e4f132012-09-06 04:28:05691#elif defined(OS_ANDROID)
692 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45693 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05694 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
695 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04696 return true;
697
[email protected]d8e4f132012-09-06 04:28:05698 // Whitelist of other allowed directories.
699 static const char* const kLocalAccessWhiteList[] = {
700 "/sdcard",
701 "/mnt/sdcard",
702 };
703#endif
704
[email protected]4c219e22012-05-05 19:41:04705 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45706 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
707 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04708 if (white_listed_path == path.StripTrailingSeparators() ||
709 white_listed_path.IsParent(path)) {
710 return true;
711 }
712 }
[email protected]d8e4f132012-09-06 04:28:05713
[email protected]288538482012-09-06 21:09:35714 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04715 return false;
[email protected]d8e4f132012-09-06 04:28:05716#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04717}
[email protected]a1d4ab072012-06-07 13:21:15718
[email protected]e6d017652013-05-17 18:01:40719bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
720 const GURL& url,
721 const GURL& first_party_for_cookies) const {
722 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02723 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40724 return false;
725
726 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
727 url, first_party_for_cookies);
728 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
729 url, first_party_for_cookies);
730 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
731 return privacy_mode;
732}
733
mkwst0513c9d2015-04-01 05:53:15734bool ChromeNetworkDelegate::OnFirstPartyOnlyCookieExperimentEnabled() const {
735 return experimental_web_platform_features_enabled_;
736}
737
jochen0e3b3a62014-09-16 18:31:23738bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
739 const net::URLRequest& request,
740 const GURL& target_url,
741 const GURL& referrer_url) const {
jochen652275372015-01-16 11:35:27742 ReportInvalidReferrerSend(target_url, referrer_url);
jochen0e3b3a62014-09-16 18:31:23743 return true;
744}