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/trace_event.h"
13 #include "base/logging.h"
14 #include "base/metrics/histogram.h"
15 #include "base/path_service.h"
16 #include "base/prefs/pref_member.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_split.h"
20 #include "base/time/time.h"
21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/content_settings/cookie_settings.h"
23 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
24 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
25 #include "chrome/browser/extensions/api/proxy/proxy_api.h"
26 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
27 #include "chrome/browser/extensions/event_router_forwarder.h"
28 #include "chrome/browser/google/google_util.h"
29 #include "chrome/browser/net/client_hints.h"
30 #include "chrome/browser/net/connect_interceptor.h"
31 #include "chrome/browser/performance_monitor/performance_monitor.h"
32 #include "chrome/browser/profiles/profile_manager.h"
33 #include "chrome/browser/task_manager/task_manager.h"
34 #include "chrome/common/pref_names.h"
35 #include "chrome/common/url_constants.h"
36 #include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h"
37 #include "components/domain_reliability/monitor.h"
38 #include "content/public/browser/browser_thread.h"
39 #include "content/public/browser/render_frame_host.h"
40 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/resource_request_info.h"
42 #include "extensions/browser/extension_system.h"
43 #include "extensions/browser/info_map.h"
44 #include "extensions/browser/process_manager.h"
45 #include "extensions/common/constants.h"
46 #include "net/base/host_port_pair.h"
47 #include "net/base/net_errors.h"
48 #include "net/base/net_log.h"
49 #include "net/cookies/canonical_cookie.h"
50 #include "net/cookies/cookie_options.h"
51 #include "net/http/http_request_headers.h"
52 #include "net/http/http_response_headers.h"
53 #include "net/socket_stream/socket_stream.h"
54 #include "net/url_request/url_request.h"
56 #if defined(OS_CHROMEOS)
57 #include "base/command_line.h"
58 #include "base/sys_info.h"
59 #include "chrome/common/chrome_switches.h"
62 #if defined(ENABLE_CONFIGURATION_POLICY)
63 #include "components/policy/core/browser/url_blacklist_manager.h"
66 #if defined(OS_ANDROID)
67 #include "chrome/browser/io_thread.h"
68 #include "components/precache/content/precache_manager.h"
69 #include "components/precache/content/precache_manager_factory.h"
72 using content::BrowserThread
;
73 using content::RenderViewHost
;
74 using content::ResourceRequestInfo
;
76 // By default we don't allow access to all file:// urls on ChromeOS and
78 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
79 bool ChromeNetworkDelegate::g_allow_file_access_
= false;
81 bool ChromeNetworkDelegate::g_allow_file_access_
= true;
84 // This remains false unless the --disable-extensions-http-throttling
85 // flag is passed to the browser.
86 bool ChromeNetworkDelegate::g_never_throttle_requests_
= false;
90 const char kDNTHeader
[] = "DNT";
92 // If the |request| failed due to problems with a proxy, forward the error to
93 // the proxy extension API.
94 void ForwardProxyErrors(net::URLRequest
* request
,
95 extensions::EventRouterForwarder
* event_router
,
97 if (request
->status().status() == net::URLRequestStatus::FAILED
) {
98 switch (request
->status().error()) {
99 case net::ERR_PROXY_AUTH_UNSUPPORTED
:
100 case net::ERR_PROXY_CONNECTION_FAILED
:
101 case net::ERR_TUNNEL_CONNECTION_FAILED
:
102 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
103 event_router
, profile
, request
->status().error());
108 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
109 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters
110 // must be in key=value form.
111 bool HasSameParameterKey(const std::string
& first_parameter
,
112 const std::string
& second_parameter
) {
113 DCHECK(second_parameter
.find("=") != std::string::npos
);
114 // Prefix for "foo=bar" is "foo=".
115 std::string parameter_prefix
= second_parameter
.substr(
116 0, second_parameter
.find("=") + 1);
117 return StartsWithASCII(first_parameter
, parameter_prefix
, false);
120 // Examines the query string containing parameters and adds the necessary ones
121 // so that SafeSearch is active. |query| is the string to examine and the
122 // return value is the |query| string modified such that SafeSearch is active.
123 std::string
AddSafeSearchParameters(const std::string
& query
) {
124 std::vector
<std::string
> new_parameters
;
125 std::string safe_parameter
= chrome::kSafeSearchSafeParameter
;
126 std::string ssui_parameter
= chrome::kSafeSearchSsuiParameter
;
128 std::vector
<std::string
> parameters
;
129 base::SplitString(query
, '&', ¶meters
);
131 std::vector
<std::string
>::iterator it
;
132 for (it
= parameters
.begin(); it
< parameters
.end(); ++it
) {
133 if (!HasSameParameterKey(*it
, safe_parameter
) &&
134 !HasSameParameterKey(*it
, ssui_parameter
)) {
135 new_parameters
.push_back(*it
);
139 new_parameters
.push_back(safe_parameter
);
140 new_parameters
.push_back(ssui_parameter
);
141 return JoinString(new_parameters
, '&');
144 // If |request| is a request to Google Web Search the function
145 // enforces that the SafeSearch query parameters are set to active.
146 // Sets the query part of |new_url| with the new value of the parameters.
147 void ForceGoogleSafeSearch(net::URLRequest
* request
,
149 if (!google_util::IsGoogleSearchUrl(request
->url()) &&
150 !google_util::IsGoogleHomePageUrl(request
->url()))
153 std::string query
= request
->url().query();
154 std::string new_query
= AddSafeSearchParameters(query
);
155 if (query
== new_query
)
158 GURL::Replacements replacements
;
159 replacements
.SetQueryStr(new_query
);
160 *new_url
= request
->url().ReplaceComponents(replacements
);
163 // Gets called when the extensions finish work on the URL. If the extensions
164 // did not do a redirect (so |new_url| is empty) then we enforce the
165 // SafeSearch parameters. Otherwise we will get called again after the
166 // redirect and we enforce SafeSearch then.
167 void ForceGoogleSafeSearchCallbackWrapper(
168 const net::CompletionCallback
& callback
,
169 net::URLRequest
* request
,
172 if (rv
== net::OK
&& new_url
->is_empty())
173 ForceGoogleSafeSearch(request
, new_url
);
177 enum RequestStatus
{ REQUEST_STARTED
, REQUEST_DONE
};
179 // Notifies the extensions::ProcessManager that a request has started or stopped
180 // for a particular RenderFrame.
181 void NotifyEPMRequestStatus(RequestStatus status
,
184 int render_frame_id
) {
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
186 Profile
* profile
= reinterpret_cast<Profile
*>(profile_id
);
187 if (!g_browser_process
->profile_manager()->IsValidProfile(profile
))
190 extensions::ProcessManager
* process_manager
=
191 extensions::ExtensionSystem::Get(profile
)->process_manager();
192 // This may be NULL in unit tests.
193 if (!process_manager
)
196 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
197 // system-level request).
198 content::RenderFrameHost
* render_frame_host
=
199 content::RenderFrameHost::FromID(process_id
, render_frame_id
);
200 if (render_frame_host
) {
201 if (status
== REQUEST_STARTED
) {
202 process_manager
->OnNetworkRequestStarted(render_frame_host
);
203 } else if (status
== REQUEST_DONE
) {
204 process_manager
->OnNetworkRequestDone(render_frame_host
);
211 void ForwardRequestStatus(
212 RequestStatus status
, net::URLRequest
* request
, void* profile_id
) {
213 const ResourceRequestInfo
* info
= ResourceRequestInfo::ForRequest(request
);
217 int process_id
, render_frame_id
;
218 if (info
->GetAssociatedRenderFrame(&process_id
, &render_frame_id
)) {
219 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
220 base::Bind(&NotifyEPMRequestStatus
,
221 status
, profile_id
, process_id
, render_frame_id
));
225 void UpdateContentLengthPrefs(
226 int received_content_length
,
227 int original_content_length
,
228 data_reduction_proxy::DataReductionProxyRequestType request_type
,
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
231 DCHECK_GE(received_content_length
, 0);
232 DCHECK_GE(original_content_length
, 0);
234 // Can be NULL in a unit test.
235 if (!g_browser_process
)
238 PrefService
* prefs
= g_browser_process
->local_state();
242 // Ignore off-the-record data.
243 if (!g_browser_process
->profile_manager()->IsValidProfile(profile
) ||
244 profile
->IsOffTheRecord()) {
247 #if defined(OS_ANDROID)
248 // If Android ever goes multi profile, the profile should be passed so that
249 // the browser preference will be taken.
250 bool with_data_reduction_proxy_enabled
=
251 ProfileManager::GetActiveUserProfile()->GetPrefs()->GetBoolean(
252 data_reduction_proxy::prefs::kDataReductionProxyEnabled
);
254 bool with_data_reduction_proxy_enabled
= false;
257 data_reduction_proxy::UpdateContentLengthPrefs(received_content_length
,
258 original_content_length
,
259 with_data_reduction_proxy_enabled
,
260 request_type
, prefs
);
263 void StoreAccumulatedContentLength(
264 int received_content_length
,
265 int original_content_length
,
266 data_reduction_proxy::DataReductionProxyRequestType request_type
,
268 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
269 base::Bind(&UpdateContentLengthPrefs
,
270 received_content_length
, original_content_length
,
271 request_type
, profile
));
274 void RecordContentLengthHistograms(
275 int64 received_content_length
,
276 int64 original_content_length
,
277 const base::TimeDelta
& freshness_lifetime
) {
278 #if defined(OS_ANDROID)
279 // Add the current resource to these histograms only when a valid
280 // X-Original-Content-Length header is present.
281 if (original_content_length
>= 0) {
282 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL",
283 received_content_length
);
284 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL",
285 original_content_length
);
286 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL",
287 original_content_length
- received_content_length
);
289 // Presume the original content length is the same as the received content
290 // length if the X-Original-Content-Header is not present.
291 original_content_length
= received_content_length
;
293 UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length
);
294 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength",
295 original_content_length
);
296 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference",
297 original_content_length
- received_content_length
);
298 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpContentFreshnessLifetime",
299 freshness_lifetime
.InSeconds(),
300 base::TimeDelta::FromHours(1).InSeconds(),
301 base::TimeDelta::FromDays(30).InSeconds(),
303 if (freshness_lifetime
.InSeconds() <= 0)
305 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable",
306 received_content_length
);
307 if (freshness_lifetime
.InHours() < 4)
309 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable4Hours",
310 received_content_length
);
312 if (freshness_lifetime
.InHours() < 24)
314 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable24Hours",
315 received_content_length
);
316 #endif // defined(OS_ANDROID)
319 #if defined(OS_ANDROID)
320 void RecordPrecacheStatsOnUIThread(const GURL
& url
,
321 const base::Time
& fetch_time
, int64 size
,
322 bool was_cached
, void* profile_id
) {
323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
325 Profile
* profile
= reinterpret_cast<Profile
*>(profile_id
);
326 if (!g_browser_process
->profile_manager()->IsValidProfile(profile
)) {
330 precache::PrecacheManager
* precache_manager
=
331 precache::PrecacheManagerFactory::GetForBrowserContext(profile
);
332 if (!precache_manager
) {
333 // This could be NULL if the profile is off the record.
337 precache_manager
->RecordStatsForFetch(url
, fetch_time
, size
, was_cached
);
340 void RecordIOThreadToRequestStartOnUIThread(
341 const base::TimeTicks
& request_start
) {
342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
343 base::TimeDelta request_lag
= request_start
-
344 g_browser_process
->io_thread()->creation_time();
345 UMA_HISTOGRAM_TIMES("Net.IOThreadCreationToHTTPRequestStart", request_lag
);
347 #endif // defined(OS_ANDROID)
351 ChromeNetworkDelegate::ChromeNetworkDelegate(
352 extensions::EventRouterForwarder
* event_router
,
353 BooleanPrefMember
* enable_referrers
)
354 : event_router_(event_router
),
356 enable_referrers_(enable_referrers
),
357 enable_do_not_track_(NULL
),
358 force_google_safe_search_(NULL
),
359 url_blacklist_manager_(NULL
),
360 domain_reliability_monitor_(NULL
),
361 received_content_length_(0),
362 original_content_length_(0),
363 first_request_(true) {
364 DCHECK(event_router
);
365 DCHECK(enable_referrers
);
368 ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
370 void ChromeNetworkDelegate::set_extension_info_map(
371 extensions::InfoMap
* extension_info_map
) {
372 extension_info_map_
= extension_info_map
;
375 void ChromeNetworkDelegate::set_cookie_settings(
376 CookieSettings
* cookie_settings
) {
377 cookie_settings_
= cookie_settings
;
380 void ChromeNetworkDelegate::set_predictor(
381 chrome_browser_net::Predictor
* predictor
) {
382 connect_interceptor_
.reset(
383 new chrome_browser_net::ConnectInterceptor(predictor
));
386 void ChromeNetworkDelegate::SetEnableClientHints() {
387 client_hints_
.reset(new ClientHints());
388 client_hints_
->Init();
392 void ChromeNetworkDelegate::NeverThrottleRequests() {
393 g_never_throttle_requests_
= true;
397 void ChromeNetworkDelegate::InitializePrefsOnUIThread(
398 BooleanPrefMember
* enable_referrers
,
399 BooleanPrefMember
* enable_do_not_track
,
400 BooleanPrefMember
* force_google_safe_search
,
401 PrefService
* pref_service
) {
402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
403 enable_referrers
->Init(prefs::kEnableReferrers
, pref_service
);
404 enable_referrers
->MoveToThread(
405 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
406 if (enable_do_not_track
) {
407 enable_do_not_track
->Init(prefs::kEnableDoNotTrack
, pref_service
);
408 enable_do_not_track
->MoveToThread(
409 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
411 if (force_google_safe_search
) {
412 force_google_safe_search
->Init(prefs::kForceSafeSearch
, pref_service
);
413 force_google_safe_search
->MoveToThread(
414 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
419 void ChromeNetworkDelegate::AllowAccessToAllFiles() {
420 g_allow_file_access_
= true;
424 base::Value
* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() {
425 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
426 PrefService
* prefs
= g_browser_process
->local_state();
427 int64 total_received
= prefs
->GetInt64(
428 data_reduction_proxy::prefs::kHttpReceivedContentLength
);
429 int64 total_original
= prefs
->GetInt64(
430 data_reduction_proxy::prefs::kHttpOriginalContentLength
);
432 base::DictionaryValue
* dict
= new base::DictionaryValue();
433 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
434 dict
->SetString("historic_received_content_length",
435 base::Int64ToString(total_received
));
436 dict
->SetString("historic_original_content_length",
437 base::Int64ToString(total_original
));
441 base::Value
* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
442 base::DictionaryValue
* dict
= new base::DictionaryValue();
443 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
444 dict
->SetString("session_received_content_length",
445 base::Int64ToString(received_content_length_
));
446 dict
->SetString("session_original_content_length",
447 base::Int64ToString(original_content_length_
));
451 int ChromeNetworkDelegate::OnBeforeURLRequest(
452 net::URLRequest
* request
,
453 const net::CompletionCallback
& callback
,
455 #if defined(OS_ANDROID)
456 // This UMA tracks the time to the first user-initiated request start, so
457 // only non-null profiles are considered.
458 if (first_request_
&& profile_
) {
459 bool record_timing
= true;
460 #if defined(DATA_REDUCTION_PROXY_PROBE_URL)
461 record_timing
= (request
->url() != GURL(DATA_REDUCTION_PROXY_PROBE_URL
));
464 first_request_
= false;
465 net::LoadTimingInfo timing_info
;
466 request
->GetLoadTimingInfo(&timing_info
);
467 BrowserThread::PostTask(
468 BrowserThread::UI
, FROM_HERE
,
469 base::Bind(&RecordIOThreadToRequestStartOnUIThread
,
470 timing_info
.request_start
));
473 #endif // defined(OS_ANDROID)
475 #if defined(ENABLE_CONFIGURATION_POLICY)
476 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
477 // blocked. However, an extension might redirect the request to another URL,
478 // which is not blocked.
479 int error
= net::ERR_BLOCKED_BY_ADMINISTRATOR
;
480 if (url_blacklist_manager_
&&
481 url_blacklist_manager_
->IsRequestBlocked(*request
, &error
)) {
482 // URL access blocked by policy.
483 request
->net_log().AddEvent(
484 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST
,
485 net::NetLog::StringCallback("url",
486 &request
->url().possibly_invalid_spec()));
491 ForwardRequestStatus(REQUEST_STARTED
, request
, profile_
);
493 if (!enable_referrers_
->GetValue())
494 request
->SetReferrer(std::string());
495 if (enable_do_not_track_
&& enable_do_not_track_
->GetValue())
496 request
->SetExtraRequestHeaderByName(kDNTHeader
, "1", true /* override */);
499 request
->SetExtraRequestHeaderByName(
500 ClientHints::kDevicePixelRatioHeader
,
501 client_hints_
->GetDevicePixelRatioHeader(), true);
504 bool force_safe_search
= force_google_safe_search_
&&
505 force_google_safe_search_
->GetValue();
507 net::CompletionCallback wrapped_callback
= callback
;
508 if (force_safe_search
) {
509 wrapped_callback
= base::Bind(&ForceGoogleSafeSearchCallbackWrapper
,
511 base::Unretained(request
),
512 base::Unretained(new_url
));
515 int rv
= ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
516 profile_
, extension_info_map_
.get(), request
, wrapped_callback
,
519 if (force_safe_search
&& rv
== net::OK
&& new_url
->is_empty())
520 ForceGoogleSafeSearch(request
, new_url
);
522 if (connect_interceptor_
)
523 connect_interceptor_
->WitnessURLRequest(request
);
528 int ChromeNetworkDelegate::OnBeforeSendHeaders(
529 net::URLRequest
* request
,
530 const net::CompletionCallback
& callback
,
531 net::HttpRequestHeaders
* headers
) {
532 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request
, "SendRequest");
533 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
534 profile_
, extension_info_map_
.get(), request
, callback
, headers
);
537 void ChromeNetworkDelegate::OnSendHeaders(
538 net::URLRequest
* request
,
539 const net::HttpRequestHeaders
& headers
) {
540 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
541 profile_
, extension_info_map_
.get(), request
, headers
);
544 int ChromeNetworkDelegate::OnHeadersReceived(
545 net::URLRequest
* request
,
546 const net::CompletionCallback
& callback
,
547 const net::HttpResponseHeaders
* original_response_headers
,
548 scoped_refptr
<net::HttpResponseHeaders
>* override_response_headers
,
549 GURL
* allowed_unsafe_redirect_url
) {
550 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
552 extension_info_map_
.get(),
555 original_response_headers
,
556 override_response_headers
,
557 allowed_unsafe_redirect_url
);
560 void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest
* request
,
561 const GURL
& new_location
) {
562 if (domain_reliability_monitor_
)
563 domain_reliability_monitor_
->OnBeforeRedirect(request
);
564 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
565 profile_
, extension_info_map_
.get(), request
, new_location
);
569 void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest
* request
) {
570 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request
, "ResponseStarted");
571 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
572 profile_
, extension_info_map_
.get(), request
);
573 ForwardProxyErrors(request
, event_router_
.get(), profile_
);
576 void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest
& request
,
578 TRACE_EVENT_ASYNC_STEP_PAST1("net", "URLRequest", &request
, "DidRead",
579 "bytes_read", bytes_read
);
580 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
581 request
, bytes_read
);
583 #if defined(ENABLE_TASK_MANAGER)
584 // This is not completely accurate, but as a first approximation ignore
585 // requests that are served from the cache. See bug 330931 for more info.
586 if (!request
.was_cached())
587 TaskManager::GetInstance()->model()->NotifyBytesRead(request
, bytes_read
);
588 #endif // defined(ENABLE_TASK_MANAGER)
591 void ChromeNetworkDelegate::OnCompleted(net::URLRequest
* request
,
593 TRACE_EVENT_ASYNC_END0("net", "URLRequest", request
);
594 if (request
->status().status() == net::URLRequestStatus::SUCCESS
) {
595 // For better accuracy, we use the actual bytes read instead of the length
596 // specified with the Content-Length header, which may be inaccurate,
597 // or missing, as is the case with chunked encoding.
598 int64 received_content_length
= request
->received_response_content_length();
600 #if defined(OS_ANDROID)
601 if (precache::PrecacheManager::IsPrecachingEnabled()) {
602 // Record precache metrics when a fetch is completed successfully, if
603 // precaching is enabled.
604 BrowserThread::PostTask(
605 BrowserThread::UI
, FROM_HERE
,
606 base::Bind(&RecordPrecacheStatsOnUIThread
, request
->url(),
607 base::Time::Now(), received_content_length
,
608 request
->was_cached(), profile_
));
610 #endif // defined(OS_ANDROID)
612 // Only record for http or https urls.
613 bool is_http
= request
->url().SchemeIs("http");
614 bool is_https
= request
->url().SchemeIs("https");
616 if (!request
->was_cached() && // Don't record cached content
617 received_content_length
&& // Zero-byte responses aren't useful.
618 (is_http
|| is_https
)) { // Only record for HTTP or HTTPS urls.
619 int64 original_content_length
=
620 request
->response_info().headers
->GetInt64HeaderValue(
621 "x-original-content-length");
622 data_reduction_proxy::DataReductionProxyRequestType request_type
=
623 data_reduction_proxy::GetDataReductionProxyRequestType(request
);
625 base::TimeDelta freshness_lifetime
=
626 request
->response_info().headers
->GetFreshnessLifetime(
627 request
->response_info().response_time
);
628 int64 adjusted_original_content_length
=
629 data_reduction_proxy::GetAdjustedOriginalContentLength(
630 request_type
, original_content_length
,
631 received_content_length
);
632 AccumulateContentLength(received_content_length
,
633 adjusted_original_content_length
,
635 RecordContentLengthHistograms(received_content_length
,
636 original_content_length
,
638 DVLOG(2) << __FUNCTION__
639 << " received content length: " << received_content_length
640 << " original content length: " << original_content_length
641 << " url: " << request
->url();
644 bool is_redirect
= request
->response_headers() &&
645 net::HttpResponseHeaders::IsRedirectResponseCode(
646 request
->response_headers()->response_code());
648 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
649 profile_
, extension_info_map_
.get(), request
);
651 } else if (request
->status().status() == net::URLRequestStatus::FAILED
||
652 request
->status().status() == net::URLRequestStatus::CANCELED
) {
653 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
654 profile_
, extension_info_map_
.get(), request
, started
);
658 if (domain_reliability_monitor_
)
659 domain_reliability_monitor_
->OnCompleted(request
, started
);
660 ForwardProxyErrors(request
, event_router_
.get(), profile_
);
662 ForwardRequestStatus(REQUEST_DONE
, request
, profile_
);
665 void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest
* request
) {
666 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
670 void ChromeNetworkDelegate::OnPACScriptError(int line_number
,
671 const base::string16
& error
) {
672 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
673 event_router_
.get(), profile_
, line_number
, error
);
676 net::NetworkDelegate::AuthRequiredResponse
677 ChromeNetworkDelegate::OnAuthRequired(
678 net::URLRequest
* request
,
679 const net::AuthChallengeInfo
& auth_info
,
680 const AuthCallback
& callback
,
681 net::AuthCredentials
* credentials
) {
682 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
683 profile_
, extension_info_map_
.get(), request
, auth_info
,
684 callback
, credentials
);
687 bool ChromeNetworkDelegate::OnCanGetCookies(
688 const net::URLRequest
& request
,
689 const net::CookieList
& cookie_list
) {
690 // NULL during tests, or when we're running in the system context.
691 if (!cookie_settings_
.get())
694 bool allow
= cookie_settings_
->IsReadingCookieAllowed(
695 request
.url(), request
.first_party_for_cookies());
697 int render_process_id
= -1;
698 int render_frame_id
= -1;
700 // |is_for_blocking_resource| indicates whether the cookies read were for a
701 // blocking resource (eg script, css). It is only temporarily added for
702 // diagnostic purposes, per bug 353678. Will be removed again once data
703 // collection is finished.
704 bool is_for_blocking_resource
= false;
705 const ResourceRequestInfo
* info
= ResourceRequestInfo::ForRequest(&request
);
706 if (info
&& ((!info
->IsAsync()) ||
707 info
->GetResourceType() == ResourceType::STYLESHEET
||
708 info
->GetResourceType() == ResourceType::SCRIPT
)) {
709 is_for_blocking_resource
= true;
712 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
713 &request
, &render_process_id
, &render_frame_id
)) {
714 BrowserThread::PostTask(
715 BrowserThread::UI
, FROM_HERE
,
716 base::Bind(&TabSpecificContentSettings::CookiesRead
,
717 render_process_id
, render_frame_id
,
718 request
.url(), request
.first_party_for_cookies(),
719 cookie_list
, !allow
, is_for_blocking_resource
));
725 bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest
& request
,
726 const std::string
& cookie_line
,
727 net::CookieOptions
* options
) {
728 // NULL during tests, or when we're running in the system context.
729 if (!cookie_settings_
.get())
732 bool allow
= cookie_settings_
->IsSettingCookieAllowed(
733 request
.url(), request
.first_party_for_cookies());
735 int render_process_id
= -1;
736 int render_frame_id
= -1;
737 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
738 &request
, &render_process_id
, &render_frame_id
)) {
739 BrowserThread::PostTask(
740 BrowserThread::UI
, FROM_HERE
,
741 base::Bind(&TabSpecificContentSettings::CookieChanged
,
742 render_process_id
, render_frame_id
,
743 request
.url(), request
.first_party_for_cookies(),
744 cookie_line
, *options
, !allow
));
750 bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest
& request
,
751 const base::FilePath
& path
) const {
752 if (g_allow_file_access_
)
755 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
758 #if defined(OS_CHROMEOS)
759 // If we're running Chrome for ChromeOS on Linux, we want to allow file
761 if (!base::SysInfo::IsRunningOnChromeOS() ||
762 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
)) {
766 // Use a whitelist to only allow access to files residing in the list of
767 // directories below.
768 static const char* const kLocalAccessWhiteList
[] = {
769 "/home/chronos/user/Downloads",
770 "/home/chronos/user/log",
771 "/home/chronos/user/WebRTC Logs",
774 "/usr/share/chromeos-assets",
779 // The actual location of "/home/chronos/user/Downloads" is the Downloads
780 // directory under the profile path ("/home/chronos/user' is a hard link to
781 // current primary logged in profile.) For the support of multi-profile
782 // sessions, we are switching to use explicit "$PROFILE_PATH/Downloads" path
783 // and here whitelist such access.
784 if (!profile_path_
.empty()) {
785 const base::FilePath downloads
= profile_path_
.AppendASCII("Downloads");
786 if (downloads
== path
.StripTrailingSeparators() || downloads
.IsParent(path
))
789 #elif defined(OS_ANDROID)
790 // Access to files in external storage is allowed.
791 base::FilePath external_storage_path
;
792 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE
, &external_storage_path
);
793 if (external_storage_path
.IsParent(path
))
796 // Whitelist of other allowed directories.
797 static const char* const kLocalAccessWhiteList
[] = {
803 for (size_t i
= 0; i
< arraysize(kLocalAccessWhiteList
); ++i
) {
804 const base::FilePath
white_listed_path(kLocalAccessWhiteList
[i
]);
805 // base::FilePath::operator== should probably handle trailing separators.
806 if (white_listed_path
== path
.StripTrailingSeparators() ||
807 white_listed_path
.IsParent(path
)) {
812 DVLOG(1) << "File access denied - " << path
.value().c_str();
814 #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
817 bool ChromeNetworkDelegate::OnCanThrottleRequest(
818 const net::URLRequest
& request
) const {
819 if (g_never_throttle_requests_
) {
823 return request
.first_party_for_cookies().scheme() ==
824 extensions::kExtensionScheme
;
827 bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
829 const GURL
& first_party_for_cookies
) const {
830 // NULL during tests, or when we're running in the system context.
831 if (!cookie_settings_
.get())
834 bool reading_cookie_allowed
= cookie_settings_
->IsReadingCookieAllowed(
835 url
, first_party_for_cookies
);
836 bool setting_cookie_allowed
= cookie_settings_
->IsSettingCookieAllowed(
837 url
, first_party_for_cookies
);
838 bool privacy_mode
= !(reading_cookie_allowed
&& setting_cookie_allowed
);
842 int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
843 net::SocketStream
* socket
,
844 const net::CompletionCallback
& callback
) {
845 #if defined(ENABLE_CONFIGURATION_POLICY)
846 if (url_blacklist_manager_
&&
847 url_blacklist_manager_
->IsURLBlocked(socket
->url())) {
848 // URL access blocked by policy.
849 socket
->net_log()->AddEvent(
850 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST
,
851 net::NetLog::StringCallback("url",
852 &socket
->url().possibly_invalid_spec()));
853 return net::ERR_BLOCKED_BY_ADMINISTRATOR
;
859 void ChromeNetworkDelegate::AccumulateContentLength(
860 int64 received_content_length
,
861 int64 original_content_length
,
862 data_reduction_proxy::DataReductionProxyRequestType request_type
) {
863 DCHECK_GE(received_content_length
, 0);
864 DCHECK_GE(original_content_length
, 0);
865 StoreAccumulatedContentLength(received_content_length
,
866 original_content_length
,
868 reinterpret_cast<Profile
*>(profile_
));
869 received_content_length_
+= received_content_length
;
870 original_content_length_
+= original_content_length
;