1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/net/chrome_network_delegate.h"
11 #include "base/base_paths.h"
12 #include "base/debug/dump_without_crashing.h"
13 #include "base/debug/trace_event.h"
14 #include "base/logging.h"
15 #include "base/metrics/histogram.h"
16 #include "base/metrics/user_metrics.h"
17 #include "base/path_service.h"
18 #include "base/prefs/pref_member.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/time/time.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/content_settings/cookie_settings.h"
24 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
25 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
26 #include "chrome/browser/net/chrome_extensions_network_delegate.h"
27 #include "chrome/browser/net/client_hints.h"
28 #include "chrome/browser/net/connect_interceptor.h"
29 #include "chrome/browser/net/safe_search_util.h"
30 #include "chrome/browser/prerender/prerender_tracker.h"
31 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/task_manager/task_manager.h"
33 #include "chrome/common/pref_names.h"
34 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
35 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"
36 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
37 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats.h"
38 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
39 #include "components/domain_reliability/monitor.h"
40 #include "content/public/browser/browser_thread.h"
41 #include "content/public/browser/render_frame_host.h"
42 #include "content/public/browser/render_view_host.h"
43 #include "content/public/browser/resource_request_info.h"
44 #include "net/base/host_port_pair.h"
45 #include "net/base/net_errors.h"
46 #include "net/base/net_log.h"
47 #include "net/cookies/canonical_cookie.h"
48 #include "net/cookies/cookie_options.h"
49 #include "net/http/http_request_headers.h"
50 #include "net/http/http_response_headers.h"
51 #include "net/proxy/proxy_config.h"
52 #include "net/proxy/proxy_info.h"
53 #include "net/proxy/proxy_retry_info.h"
54 #include "net/proxy/proxy_server.h"
55 #include "net/url_request/url_request.h"
56 #include "net/url_request/url_request_context.h"
58 #if defined(OS_ANDROID)
59 #include "chrome/browser/io_thread.h"
60 #include "components/precache/content/precache_manager.h"
61 #include "components/precache/content/precache_manager_factory.h"
64 #if defined(OS_CHROMEOS)
65 #include "base/command_line.h"
66 #include "base/sys_info.h"
67 #include "chrome/common/chrome_switches.h"
70 #if defined(ENABLE_CONFIGURATION_POLICY)
71 #include "components/policy/core/browser/url_blacklist_manager.h"
74 #if defined(ENABLE_EXTENSIONS)
75 #include "extensions/common/constants.h"
78 using content::BrowserThread;
79 using content::RenderViewHost;
80 using content::ResourceRequestInfo;
81 using content::ResourceType;
83 // By default we don't allow access to all file:// urls on ChromeOS and
85 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
86 bool ChromeNetworkDelegate::g_allow_file_access_ = false;
88 bool ChromeNetworkDelegate::g_allow_file_access_ = true;
91 #if defined(ENABLE_EXTENSIONS)
92 // This remains false unless the --disable-extensions-http-throttling
93 // flag is passed to the browser.
94 bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
99 const char kDNTHeader[] = "DNT";
101 // Gets called when the extensions finish work on the URL. If the extensions
102 // did not do a redirect (so |new_url| is empty) then we enforce the
103 // SafeSearch parameters. Otherwise we will get called again after the
104 // redirect and we enforce SafeSearch then.
105 void ForceGoogleSafeSearchCallbackWrapper(
106 const net::CompletionCallback& callback,
107 net::URLRequest* request,
110 if (rv == net::OK && new_url->is_empty())
111 safe_search_util::ForceGoogleSafeSearch(request, new_url);
115 void UpdateContentLengthPrefs(
116 int received_content_length,
117 int original_content_length,
118 data_reduction_proxy::DataReductionProxyRequestType request_type,
120 data_reduction_proxy::DataReductionProxyStatisticsPrefs* statistics_prefs) {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
122 DCHECK_GE(received_content_length, 0);
123 DCHECK_GE(original_content_length, 0);
125 // Can be NULL in a unit test.
126 if (!g_browser_process)
129 // Ignore off-the-record data.
130 if (!g_browser_process->profile_manager()->IsValidProfile(profile) ||
131 profile->IsOffTheRecord()) {
134 data_reduction_proxy::UpdateContentLengthPrefs(
135 received_content_length,
136 original_content_length,
138 request_type, statistics_prefs);
141 void StoreAccumulatedContentLength(
142 int received_content_length,
143 int original_content_length,
144 data_reduction_proxy::DataReductionProxyRequestType request_type,
146 data_reduction_proxy::DataReductionProxyStatisticsPrefs* statistics_prefs) {
147 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
148 base::Bind(&UpdateContentLengthPrefs,
149 received_content_length,
150 original_content_length,
156 void RecordContentLengthHistograms(
157 int64 received_content_length,
158 int64 original_content_length,
159 const base::TimeDelta& freshness_lifetime) {
160 // Add the current resource to these histograms only when a valid
161 // X-Original-Content-Length header is present.
162 if (original_content_length >= 0) {
163 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL",
164 received_content_length);
165 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL",
166 original_content_length);
167 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL",
168 original_content_length - received_content_length);
170 // Presume the original content length is the same as the received content
171 // length if the X-Original-Content-Header is not present.
172 original_content_length = received_content_length;
174 UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length);
175 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength",
176 original_content_length);
177 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference",
178 original_content_length - received_content_length);
179 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpContentFreshnessLifetime",
180 freshness_lifetime.InSeconds(),
181 base::TimeDelta::FromHours(1).InSeconds(),
182 base::TimeDelta::FromDays(30).InSeconds(),
184 if (freshness_lifetime.InSeconds() <= 0)
186 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable",
187 received_content_length);
188 if (freshness_lifetime.InHours() < 4)
190 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable4Hours",
191 received_content_length);
193 if (freshness_lifetime.InHours() < 24)
195 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable24Hours",
196 received_content_length);
199 #if defined(OS_ANDROID)
200 void RecordPrecacheStatsOnUIThread(const GURL& url,
201 const base::Time& fetch_time, int64 size,
202 bool was_cached, void* profile_id) {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
205 Profile* profile = reinterpret_cast<Profile*>(profile_id);
206 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) {
210 precache::PrecacheManager* precache_manager =
211 precache::PrecacheManagerFactory::GetForBrowserContext(profile);
212 if (!precache_manager || !precache_manager->IsPrecachingAllowed()) {
213 // |precache_manager| could be NULL if the profile is off the record.
217 precache_manager->RecordStatsForFetch(url, fetch_time, size, was_cached);
220 void RecordIOThreadToRequestStartOnUIThread(
221 const base::TimeTicks& request_start) {
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
223 base::TimeDelta request_lag = request_start -
224 g_browser_process->io_thread()->creation_time();
225 UMA_HISTOGRAM_TIMES("Net.IOThreadCreationToHTTPRequestStart", request_lag);
227 #endif // defined(OS_ANDROID)
229 void ReportInvalidReferrerSend(const GURL& target_url,
230 const GURL& referrer_url) {
232 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
233 base::debug::DumpWithoutCrashing();
239 ChromeNetworkDelegate::ChromeNetworkDelegate(
240 extensions::EventRouterForwarder* event_router,
241 BooleanPrefMember* enable_referrers)
243 enable_referrers_(enable_referrers),
244 enable_do_not_track_(NULL),
245 force_google_safe_search_(NULL),
246 data_reduction_proxy_enabled_(NULL),
247 #if defined(ENABLE_CONFIGURATION_POLICY)
248 url_blacklist_manager_(NULL),
250 domain_reliability_monitor_(NULL),
251 received_content_length_(0),
252 original_content_length_(0),
253 first_request_(true),
254 prerender_tracker_(NULL),
255 data_reduction_proxy_params_(NULL),
256 data_reduction_proxy_usage_stats_(NULL),
257 data_reduction_proxy_auth_request_handler_(NULL),
258 data_reduction_proxy_statistics_prefs_(NULL) {
259 DCHECK(enable_referrers);
260 extensions_delegate_.reset(
261 ChromeExtensionsNetworkDelegate::Create(event_router));
264 ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
266 void ChromeNetworkDelegate::set_extension_info_map(
267 extensions::InfoMap* extension_info_map) {
268 extensions_delegate_->set_extension_info_map(extension_info_map);
271 void ChromeNetworkDelegate::set_profile(void* profile) {
273 extensions_delegate_->set_profile(profile);
276 void ChromeNetworkDelegate::set_cookie_settings(
277 CookieSettings* cookie_settings) {
278 cookie_settings_ = cookie_settings;
281 void ChromeNetworkDelegate::set_predictor(
282 chrome_browser_net::Predictor* predictor) {
283 connect_interceptor_.reset(
284 new chrome_browser_net::ConnectInterceptor(predictor));
287 void ChromeNetworkDelegate::SetEnableClientHints() {
288 client_hints_.reset(new ClientHints());
289 client_hints_->Init();
293 #if defined(ENABLE_EXTENSIONS)
294 void ChromeNetworkDelegate::NeverThrottleRequests() {
295 g_never_throttle_requests_ = true;
300 void ChromeNetworkDelegate::InitializePrefsOnUIThread(
301 BooleanPrefMember* enable_referrers,
302 BooleanPrefMember* enable_do_not_track,
303 BooleanPrefMember* force_google_safe_search,
304 PrefService* pref_service) {
305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
306 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
307 enable_referrers->MoveToThread(
308 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
309 if (enable_do_not_track) {
310 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
311 enable_do_not_track->MoveToThread(
312 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
314 if (force_google_safe_search) {
315 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service);
316 force_google_safe_search->MoveToThread(
317 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
322 void ChromeNetworkDelegate::AllowAccessToAllFiles() {
323 g_allow_file_access_ = true;
327 // TODO(megjablon): Use data_reduction_proxy_delayed_pref_service to read prefs.
328 // Until updated the pref values may be up to an hour behind on desktop.
329 base::Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue(
330 PrefService* prefs) {
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
332 int64 total_received = prefs->GetInt64(
333 data_reduction_proxy::prefs::kHttpReceivedContentLength);
334 int64 total_original = prefs->GetInt64(
335 data_reduction_proxy::prefs::kHttpOriginalContentLength);
337 base::DictionaryValue* dict = new base::DictionaryValue();
338 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
339 dict->SetString("historic_received_content_length",
340 base::Int64ToString(total_received));
341 dict->SetString("historic_original_content_length",
342 base::Int64ToString(total_original));
346 base::Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
347 base::DictionaryValue* dict = new base::DictionaryValue();
348 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
349 dict->SetString("session_received_content_length",
350 base::Int64ToString(received_content_length_));
351 dict->SetString("session_original_content_length",
352 base::Int64ToString(original_content_length_));
356 int ChromeNetworkDelegate::OnBeforeURLRequest(
357 net::URLRequest* request,
358 const net::CompletionCallback& callback,
360 #if defined(OS_ANDROID)
361 // This UMA tracks the time to the first user-initiated request start, so
362 // only non-null profiles are considered.
363 if (first_request_ && profile_) {
364 bool record_timing = true;
365 if (data_reduction_proxy_params_) {
367 (request->url() != data_reduction_proxy_params_->probe_url()) &&
368 (request->url() != data_reduction_proxy_params_->warmup_url());
371 first_request_ = false;
372 net::LoadTimingInfo timing_info;
373 request->GetLoadTimingInfo(&timing_info);
374 BrowserThread::PostTask(
375 BrowserThread::UI, FROM_HERE,
376 base::Bind(&RecordIOThreadToRequestStartOnUIThread,
377 timing_info.request_start));
380 #endif // defined(OS_ANDROID)
382 #if defined(ENABLE_CONFIGURATION_POLICY)
383 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
384 // blocked. However, an extension might redirect the request to another URL,
385 // which is not blocked.
386 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
387 if (url_blacklist_manager_ &&
388 url_blacklist_manager_->IsRequestBlocked(*request, &error)) {
389 // URL access blocked by policy.
390 request->net_log().AddEvent(
391 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
392 net::NetLog::StringCallback("url",
393 &request->url().possibly_invalid_spec()));
398 extensions_delegate_->ForwardStartRequestStatus(request);
400 if (!enable_referrers_->GetValue())
401 request->SetReferrer(std::string());
402 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
403 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
406 request->SetExtraRequestHeaderByName(
407 ClientHints::kDevicePixelRatioHeader,
408 client_hints_->GetDevicePixelRatioHeader(), true);
411 bool force_safe_search = force_google_safe_search_ &&
412 force_google_safe_search_->GetValue();
414 net::CompletionCallback wrapped_callback = callback;
415 if (force_safe_search) {
416 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
418 base::Unretained(request),
419 base::Unretained(new_url));
422 int rv = extensions_delegate_->OnBeforeURLRequest(
423 request, wrapped_callback, new_url);
425 if (force_safe_search && rv == net::OK && new_url->is_empty())
426 safe_search_util::ForceGoogleSafeSearch(request, new_url);
428 if (connect_interceptor_)
429 connect_interceptor_->WitnessURLRequest(request);
434 void ChromeNetworkDelegate::OnResolveProxy(
437 const net::ProxyService& proxy_service,
438 net::ProxyInfo* result) {
439 if (!on_resolve_proxy_handler_.is_null() &&
440 !proxy_config_getter_.is_null()) {
441 on_resolve_proxy_handler_.Run(url, load_flags,
442 proxy_config_getter_.Run(),
443 proxy_service.config(),
444 proxy_service.proxy_retry_info(),
445 data_reduction_proxy_params_, result);
449 void ChromeNetworkDelegate::OnProxyFallback(const net::ProxyServer& bad_proxy,
451 if (data_reduction_proxy_usage_stats_) {
452 data_reduction_proxy_usage_stats_->OnProxyFallback(
453 bad_proxy, net_error);
457 int ChromeNetworkDelegate::OnBeforeSendHeaders(
458 net::URLRequest* request,
459 const net::CompletionCallback& callback,
460 net::HttpRequestHeaders* headers) {
461 bool force_safe_search = force_google_safe_search_ &&
462 force_google_safe_search_->GetValue();
463 if (force_safe_search)
464 safe_search_util::ForceYouTubeSafetyMode(request, headers);
466 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest");
467 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
470 void ChromeNetworkDelegate::OnBeforeSendProxyHeaders(
471 net::URLRequest* request,
472 const net::ProxyInfo& proxy_info,
473 net::HttpRequestHeaders* headers) {
474 if (data_reduction_proxy_auth_request_handler_) {
475 data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader(
476 request, proxy_info.proxy_server(), headers);
480 void ChromeNetworkDelegate::OnSendHeaders(
481 net::URLRequest* request,
482 const net::HttpRequestHeaders& headers) {
483 extensions_delegate_->OnSendHeaders(request, headers);
486 int ChromeNetworkDelegate::OnHeadersReceived(
487 net::URLRequest* request,
488 const net::CompletionCallback& callback,
489 const net::HttpResponseHeaders* original_response_headers,
490 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
491 GURL* allowed_unsafe_redirect_url) {
492 return extensions_delegate_->OnHeadersReceived(
495 original_response_headers,
496 override_response_headers,
497 allowed_unsafe_redirect_url);
500 void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
501 const GURL& new_location) {
502 if (domain_reliability_monitor_)
503 domain_reliability_monitor_->OnBeforeRedirect(request);
504 extensions_delegate_->OnBeforeRedirect(request, new_location);
508 void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
509 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "ResponseStarted");
510 extensions_delegate_->OnResponseStarted(request);
513 void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
515 TRACE_EVENT_ASYNC_STEP_PAST1("net", "URLRequest", &request, "DidRead",
516 "bytes_read", bytes_read);
517 #if defined(ENABLE_TASK_MANAGER)
518 // This is not completely accurate, but as a first approximation ignore
519 // requests that are served from the cache. See bug 330931 for more info.
520 if (!request.was_cached())
521 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
522 #endif // defined(ENABLE_TASK_MANAGER)
525 void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
527 if (data_reduction_proxy_usage_stats_)
528 data_reduction_proxy_usage_stats_->OnUrlRequestCompleted(request, started);
530 TRACE_EVENT_ASYNC_END0("net", "URLRequest", request);
531 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
532 // For better accuracy, we use the actual bytes read instead of the length
533 // specified with the Content-Length header, which may be inaccurate,
534 // or missing, as is the case with chunked encoding.
535 int64 received_content_length = request->received_response_content_length();
537 #if defined(OS_ANDROID)
538 if (precache::PrecacheManager::IsPrecachingEnabled()) {
539 // Record precache metrics when a fetch is completed successfully, if
540 // precaching is enabled.
541 BrowserThread::PostTask(
542 BrowserThread::UI, FROM_HERE,
543 base::Bind(&RecordPrecacheStatsOnUIThread, request->url(),
544 base::Time::Now(), received_content_length,
545 request->was_cached(), profile_));
547 #endif // defined(OS_ANDROID)
549 // Only record for http or https urls.
550 bool is_http = request->url().SchemeIs("http");
551 bool is_https = request->url().SchemeIs("https");
553 if (!request->was_cached() && // Don't record cached content
554 received_content_length && // Zero-byte responses aren't useful.
555 (is_http || is_https)) { // Only record for HTTP or HTTPS urls.
556 int64 original_content_length =
557 request->response_info().headers->GetInt64HeaderValue(
558 "x-original-content-length");
559 data_reduction_proxy::DataReductionProxyRequestType request_type =
560 data_reduction_proxy::GetDataReductionProxyRequestType(request);
562 base::TimeDelta freshness_lifetime =
563 request->response_info().headers->GetFreshnessLifetimes(
564 request->response_info().response_time).freshness;
565 int64 adjusted_original_content_length =
566 data_reduction_proxy::GetAdjustedOriginalContentLength(
567 request_type, original_content_length,
568 received_content_length);
569 AccumulateContentLength(received_content_length,
570 adjusted_original_content_length,
572 RecordContentLengthHistograms(received_content_length,
573 original_content_length,
576 if (data_reduction_proxy_enabled_ &&
577 data_reduction_proxy_usage_stats_ &&
578 !proxy_config_getter_.is_null()) {
579 data_reduction_proxy_usage_stats_->RecordBytesHistograms(
581 *data_reduction_proxy_enabled_,
582 proxy_config_getter_.Run());
584 DVLOG(2) << __FUNCTION__
585 << " received content length: " << received_content_length
586 << " original content length: " << original_content_length
587 << " url: " << request->url();
590 extensions_delegate_->OnCompleted(request, started);
591 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
592 request->status().status() == net::URLRequestStatus::CANCELED) {
593 extensions_delegate_->OnCompleted(request, started);
597 if (domain_reliability_monitor_)
598 domain_reliability_monitor_->OnCompleted(request, started);
599 extensions_delegate_->ForwardProxyErrors(request);
600 extensions_delegate_->ForwardDoneRequestStatus(request);
603 void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
604 extensions_delegate_->OnURLRequestDestroyed(request);
607 void ChromeNetworkDelegate::OnPACScriptError(int line_number,
608 const base::string16& error) {
609 extensions_delegate_->OnPACScriptError(line_number, error);
612 net::NetworkDelegate::AuthRequiredResponse
613 ChromeNetworkDelegate::OnAuthRequired(
614 net::URLRequest* request,
615 const net::AuthChallengeInfo& auth_info,
616 const AuthCallback& callback,
617 net::AuthCredentials* credentials) {
618 return extensions_delegate_->OnAuthRequired(
619 request, auth_info, callback, credentials);
622 bool ChromeNetworkDelegate::OnCanGetCookies(
623 const net::URLRequest& request,
624 const net::CookieList& cookie_list) {
625 // NULL during tests, or when we're running in the system context.
626 if (!cookie_settings_.get())
629 bool allow = cookie_settings_->IsReadingCookieAllowed(
630 request.url(), request.first_party_for_cookies());
632 int render_process_id = -1;
633 int render_frame_id = -1;
635 // |is_for_blocking_resource| indicates whether the cookies read were for a
636 // blocking resource (eg script, css). It is only temporarily added for
637 // diagnostic purposes, per bug 353678. Will be removed again once data
638 // collection is finished.
639 bool is_for_blocking_resource = false;
640 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
641 if (info && ((!info->IsAsync()) ||
642 info->GetResourceType() == content::RESOURCE_TYPE_STYLESHEET ||
643 info->GetResourceType() == content::RESOURCE_TYPE_SCRIPT)) {
644 is_for_blocking_resource = true;
647 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
648 &request, &render_process_id, &render_frame_id)) {
649 BrowserThread::PostTask(
650 BrowserThread::UI, FROM_HERE,
651 base::Bind(&TabSpecificContentSettings::CookiesRead,
652 render_process_id, render_frame_id,
653 request.url(), request.first_party_for_cookies(),
654 cookie_list, !allow, is_for_blocking_resource));
660 bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
661 const std::string& cookie_line,
662 net::CookieOptions* options) {
663 // NULL during tests, or when we're running in the system context.
664 if (!cookie_settings_.get())
667 bool allow = cookie_settings_->IsSettingCookieAllowed(
668 request.url(), request.first_party_for_cookies());
670 int render_process_id = -1;
671 int render_frame_id = -1;
672 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
673 &request, &render_process_id, &render_frame_id)) {
674 BrowserThread::PostTask(
675 BrowserThread::UI, FROM_HERE,
676 base::Bind(&TabSpecificContentSettings::CookieChanged,
677 render_process_id, render_frame_id,
678 request.url(), request.first_party_for_cookies(),
679 cookie_line, *options, !allow));
682 if (prerender_tracker_) {
683 prerender_tracker_->OnCookieChangedForURL(
685 request.context()->cookie_store()->GetCookieMonster(),
692 bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
693 const base::FilePath& path) const {
694 if (g_allow_file_access_)
697 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
700 #if defined(OS_CHROMEOS)
701 // If we're running Chrome for ChromeOS on Linux, we want to allow file
703 if (!base::SysInfo::IsRunningOnChromeOS() ||
704 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
708 // Use a whitelist to only allow access to files residing in the list of
709 // directories below.
710 static const char* const kLocalAccessWhiteList[] = {
711 "/home/chronos/user/Downloads",
712 "/home/chronos/user/log",
713 "/home/chronos/user/WebRTC Logs",
716 "/usr/share/chromeos-assets",
721 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
722 // the profile path ("/home/chronos/user' is a hard link to current primary
723 // logged in profile.) For the support of multi-profile sessions, we are
724 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
726 if (!profile_path_.empty()) {
727 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
728 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
730 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
731 if (webrtc_logs == path.StripTrailingSeparators() ||
732 webrtc_logs.IsParent(path)) {
736 #elif defined(OS_ANDROID)
737 // Access to files in external storage is allowed.
738 base::FilePath external_storage_path;
739 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
740 if (external_storage_path.IsParent(path))
743 // Whitelist of other allowed directories.
744 static const char* const kLocalAccessWhiteList[] = {
750 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
751 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
752 // base::FilePath::operator== should probably handle trailing separators.
753 if (white_listed_path == path.StripTrailingSeparators() ||
754 white_listed_path.IsParent(path)) {
759 DVLOG(1) << "File access denied - " << path.value().c_str();
761 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
764 bool ChromeNetworkDelegate::OnCanThrottleRequest(
765 const net::URLRequest& request) const {
766 #if defined(ENABLE_EXTENSIONS)
767 if (g_never_throttle_requests_)
769 return request.first_party_for_cookies().scheme() ==
770 extensions::kExtensionScheme;
776 bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
778 const GURL& first_party_for_cookies) const {
779 // NULL during tests, or when we're running in the system context.
780 if (!cookie_settings_.get())
783 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
784 url, first_party_for_cookies);
785 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
786 url, first_party_for_cookies);
787 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
791 bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
792 const net::URLRequest& request,
793 const GURL& target_url,
794 const GURL& referrer_url) const {
795 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
796 base::Bind(&ReportInvalidReferrerSend, target_url, referrer_url));
800 void ChromeNetworkDelegate::AccumulateContentLength(
801 int64 received_content_length,
802 int64 original_content_length,
803 data_reduction_proxy::DataReductionProxyRequestType request_type) {
804 DCHECK_GE(received_content_length, 0);
805 DCHECK_GE(original_content_length, 0);
806 if (data_reduction_proxy_statistics_prefs_) {
807 StoreAccumulatedContentLength(received_content_length,
808 original_content_length,
810 reinterpret_cast<Profile*>(profile_),
811 data_reduction_proxy_statistics_prefs_);
813 received_content_length_ += received_content_length;
814 original_content_length_ += original_content_length;