blob: 2e9d3e7b89dadaf6ae39390ddb9ca9e5f15fd20e [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"
[email protected]4aca5db2013-08-17 00:51:5612#include "base/debug/trace_event.h"
[email protected]7a299a92012-10-24 23:54:5013#include "base/logging.h"
14#include "base/metrics/histogram.h"
[email protected]d8e4f132012-09-06 04:28:0515#include "base/path_service.h"
[email protected]1ab137b2013-03-21 03:33:1816#include "base/prefs/pref_member.h"
[email protected]3853a4c2013-02-11 17:15:5717#include "base/prefs/pref_service.h"
[email protected]3ea1b182013-02-08 22:38:4118#include "base/strings/string_number_conversions.h"
[email protected]1988e1c2013-02-28 20:27:4219#include "base/strings/string_split.h"
[email protected]069c49162013-09-11 20:46:3520#include "base/time/time.h"
[email protected]6baff0b52012-03-06 01:30:1821#include "chrome/browser/browser_process.h"
[email protected]9c8ae8c2012-03-09 13:13:3522#include "chrome/browser/content_settings/cookie_settings.h"
23#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5824#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]aa84a7e2012-03-15 21:29:0625#include "chrome/browser/extensions/api/proxy/proxy_api.h"
[email protected]2b33dcd02012-03-18 01:34:1626#include "chrome/browser/extensions/api/web_request/web_request_api.h"
[email protected]5a38dfd2012-07-23 23:22:1027#include "chrome/browser/extensions/event_router_forwarder.h"
[email protected]d1208ba32012-11-08 11:10:3328#include "chrome/browser/google/google_util.h"
[email protected]3620bbc2013-10-05 05:07:2329#include "chrome/browser/net/client_hints.h"
[email protected]f53b4802012-12-20 17:04:2330#include "chrome/browser/net/connect_interceptor.h"
[email protected]9a0d6002013-12-19 02:04:2831#include "chrome/browser/net/spdyproxy/data_saving_metrics.h"
[email protected]6f4b4b42012-08-27 21:59:0732#include "chrome/browser/performance_monitor/performance_monitor.h"
[email protected]6baff0b52012-03-06 01:30:1833#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5834#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4035#include "chrome/common/pref_names.h"
[email protected]a1d4ab072012-06-07 13:21:1536#include "chrome/common/url_constants.h"
[email protected]45de676a2014-03-18 23:52:0237#include "components/domain_reliability/monitor.h"
[email protected]c38831a12011-10-28 12:44:4938#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5639#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3340#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3541#include "content/public/browser/resource_request_info.h"
[email protected]59b0e602014-01-30 00:41:2442#include "extensions/browser/extension_system.h"
[email protected]38427a12013-11-09 17:34:2043#include "extensions/browser/info_map.h"
[email protected]98b6d942013-11-10 00:34:0744#include "extensions/browser/process_manager.h"
[email protected]885c0e92012-11-13 20:27:4245#include "extensions/common/constants.h"
[email protected]82b42302011-04-20 16:28:1646#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1447#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5948#include "net/base/net_log.h"
[email protected]5b9bc352012-07-18 13:13:3449#include "net/cookies/canonical_cookie.h"
50#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4451#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1652#include "net/http/http_response_headers.h"
[email protected]aa28181e2012-06-13 00:53:5853#include "net/socket_stream/socket_stream.h"
[email protected]d05ef99c2011-02-01 21:38:1654#include "net/url_request/url_request.h"
55
[email protected]4c219e22012-05-05 19:41:0456#if defined(OS_CHROMEOS)
[email protected]288538482012-09-06 21:09:3557#include "base/command_line.h"
[email protected]49c4cf852013-09-27 19:28:2458#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3559#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0460#endif
61
[email protected]3e598ff12011-09-06 11:22:3462#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]f522afa2014-01-08 15:36:3663#include "components/policy/core/browser/url_blacklist_manager.h"
[email protected]3e598ff12011-09-06 11:22:3464#endif
65
[email protected]35558812013-12-18 21:58:3666#if defined(OS_ANDROID)
67#include "components/precache/content/precache_manager.h"
68#include "components/precache/content/precache_manager_factory.h"
69#endif
70
[email protected]631bb742011-11-02 11:29:3971using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1172using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2573using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3974
[email protected]d8e4f132012-09-06 04:28:0575// By default we don't allow access to all file:// urls on ChromeOS and
76// Android.
77#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0478bool ChromeNetworkDelegate::g_allow_file_access_ = false;
79#else
80bool ChromeNetworkDelegate::g_allow_file_access_ = true;
81#endif
82
[email protected]c4a7df82012-08-09 22:48:4683// This remains false unless the --disable-extensions-http-throttling
84// flag is passed to the browser.
85bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
86
[email protected]d05ef99c2011-02-01 21:38:1687namespace {
88
[email protected]9d8cfb682012-09-13 16:48:0489const char kDNTHeader[] = "DNT";
90
[email protected]8202d0c2011-02-23 08:31:1491// If the |request| failed due to problems with a proxy, forward the error to
92// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5093void ForwardProxyErrors(net::URLRequest* request,
[email protected]5a38dfd2012-07-23 23:22:1094 extensions::EventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1895 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1496 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0597 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1498 case net::ERR_PROXY_AUTH_UNSUPPORTED:
99 case net::ERR_PROXY_CONNECTION_FAILED:
100 case net::ERR_TUNNEL_CONNECTION_FAILED:
[email protected]c454fe672012-03-12 21:18:01101 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:05102 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:14103 }
104 }
105}
106
[email protected]d1208ba32012-11-08 11:10:33107// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
108// same key as the the |second_parameter| (e.g. foo=baz). Both parameters
109// must be in key=value form.
110bool HasSameParameterKey(const std::string& first_parameter,
111 const std::string& second_parameter) {
112 DCHECK(second_parameter.find("=") != std::string::npos);
113 // Prefix for "foo=bar" is "foo=".
114 std::string parameter_prefix = second_parameter.substr(
115 0, second_parameter.find("=") + 1);
116 return StartsWithASCII(first_parameter, parameter_prefix, false);
117}
118
119// Examines the query string containing parameters and adds the necessary ones
120// so that SafeSearch is active. |query| is the string to examine and the
121// return value is the |query| string modified such that SafeSearch is active.
122std::string AddSafeSearchParameters(const std::string& query) {
123 std::vector<std::string> new_parameters;
124 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
125 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
126
127 std::vector<std::string> parameters;
128 base::SplitString(query, '&', &parameters);
129
130 std::vector<std::string>::iterator it;
131 for (it = parameters.begin(); it < parameters.end(); ++it) {
132 if (!HasSameParameterKey(*it, safe_parameter) &&
133 !HasSameParameterKey(*it, ssui_parameter)) {
134 new_parameters.push_back(*it);
135 }
136 }
137
138 new_parameters.push_back(safe_parameter);
139 new_parameters.push_back(ssui_parameter);
140 return JoinString(new_parameters, '&');
141}
142
143// If |request| is a request to Google Web Search the function
144// enforces that the SafeSearch query parameters are set to active.
145// Sets the query part of |new_url| with the new value of the parameters.
146void ForceGoogleSafeSearch(net::URLRequest* request,
147 GURL* new_url) {
[email protected]c02f79d2013-06-27 21:46:47148 if (!google_util::IsGoogleSearchUrl(request->url()) &&
149 !google_util::IsGoogleHomePageUrl(request->url()))
[email protected]d1208ba32012-11-08 11:10:33150 return;
151
152 std::string query = request->url().query();
153 std::string new_query = AddSafeSearchParameters(query);
154 if (query == new_query)
155 return;
156
157 GURL::Replacements replacements;
158 replacements.SetQueryStr(new_query);
159 *new_url = request->url().ReplaceComponents(replacements);
160}
161
162// Gets called when the extensions finish work on the URL. If the extensions
163// did not do a redirect (so |new_url| is empty) then we enforce the
164// SafeSearch parameters. Otherwise we will get called again after the
165// redirect and we enforce SafeSearch then.
166void ForceGoogleSafeSearchCallbackWrapper(
167 const net::CompletionCallback& callback,
168 net::URLRequest* request,
169 GURL* new_url,
170 int rv) {
171 if (rv == net::OK && new_url->is_empty())
172 ForceGoogleSafeSearch(request, new_url);
173 callback.Run(rv);
174}
175
[email protected]6baff0b52012-03-06 01:30:18176enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
177
[email protected]98b6d942013-11-10 00:34:07178// Notifies the extensions::ProcessManager that a request has started or stopped
[email protected]9dfed872013-12-30 23:08:56179// for a particular RenderFrame.
[email protected]6baff0b52012-03-06 01:30:18180void NotifyEPMRequestStatus(RequestStatus status,
181 void* profile_id,
182 int process_id,
[email protected]9dfed872013-12-30 23:08:56183 int render_frame_id) {
[email protected]6baff0b52012-03-06 01:30:18184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
185 Profile* profile = reinterpret_cast<Profile*>(profile_id);
186 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
187 return;
188
[email protected]98b6d942013-11-10 00:34:07189 extensions::ProcessManager* process_manager =
[email protected]be93bba02012-10-24 16:44:03190 extensions::ExtensionSystem::Get(profile)->process_manager();
[email protected]e5775a52012-03-17 04:59:57191 // This may be NULL in unit tests.
[email protected]98b6d942013-11-10 00:34:07192 if (!process_manager)
[email protected]e5775a52012-03-17 04:59:57193 return;
194
[email protected]6baff0b52012-03-06 01:30:18195 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
196 // system-level request).
[email protected]9dfed872013-12-30 23:08:56197 content::RenderFrameHost* render_frame_host =
198 content::RenderFrameHost::FromID(process_id, render_frame_id);
199 if (render_frame_host) {
[email protected]6baff0b52012-03-06 01:30:18200 if (status == REQUEST_STARTED) {
[email protected]9dfed872013-12-30 23:08:56201 process_manager->OnNetworkRequestStarted(render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18202 } else if (status == REQUEST_DONE) {
[email protected]9dfed872013-12-30 23:08:56203 process_manager->OnNetworkRequestDone(render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18204 } else {
205 NOTREACHED();
206 }
207 }
208}
209
210void ForwardRequestStatus(
211 RequestStatus status, net::URLRequest* request, void* profile_id) {
[email protected]ea114722012-03-12 01:11:25212 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
213 if (!info)
214 return;
215
[email protected]9dfed872013-12-30 23:08:56216 int process_id, render_frame_id;
217 if (info->GetAssociatedRenderFrame(&process_id, &render_frame_id)) {
[email protected]6baff0b52012-03-06 01:30:18218 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
219 base::Bind(&NotifyEPMRequestStatus,
[email protected]9dfed872013-12-30 23:08:56220 status, profile_id, process_id, render_frame_id));
[email protected]6baff0b52012-03-06 01:30:18221 }
222}
223
[email protected]828eab22013-12-10 22:42:38224void UpdateContentLengthPrefs(
225 int received_content_length,
226 int original_content_length,
[email protected]9a0d6002013-12-19 02:04:28227 spdyproxy::DataReductionRequestType data_reduction_type,
[email protected]b7796462013-12-12 19:42:34228 Profile* profile) {
[email protected]7a299a92012-10-24 23:54:50229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
230 DCHECK_GE(received_content_length, 0);
231 DCHECK_GE(original_content_length, 0);
232
[email protected]a09159a2012-11-29 12:51:48233 // Can be NULL in a unit test.
234 if (!g_browser_process)
235 return;
236
[email protected]7a299a92012-10-24 23:54:50237 PrefService* prefs = g_browser_process->local_state();
[email protected]a09159a2012-11-29 12:51:48238 if (!prefs)
239 return;
[email protected]7a299a92012-10-24 23:54:50240
[email protected]b7796462013-12-12 19:42:34241 // Ignore off-the-record data.
242 if (!g_browser_process->profile_manager()->IsValidProfile(profile) ||
243 profile->IsOffTheRecord()) {
244 return;
245 }
[email protected]0d36d1e2013-09-24 04:25:28246#if defined(OS_ANDROID)
[email protected]b1d5ed52013-12-18 02:34:56247 // If Android ever goes multi profile, the profile should be passed so that
248 // the browser preference will be taken.
[email protected]0d36d1e2013-09-24 04:25:28249 bool with_data_reduction_proxy_enabled =
[email protected]b1d5ed52013-12-18 02:34:56250 ProfileManager::GetActiveUserProfile()->GetPrefs()->GetBoolean(
251 prefs::kSpdyProxyAuthEnabled);
[email protected]0d36d1e2013-09-24 04:25:28252#else
253 bool with_data_reduction_proxy_enabled = false;
[email protected]eac11e12013-03-19 22:04:32254#endif
[email protected]0d36d1e2013-09-24 04:25:28255
[email protected]9a0d6002013-12-19 02:04:28256 spdyproxy::UpdateContentLengthPrefs(received_content_length,
257 original_content_length,
258 with_data_reduction_proxy_enabled,
259 data_reduction_type, prefs);
[email protected]7a299a92012-10-24 23:54:50260}
261
[email protected]828eab22013-12-10 22:42:38262void StoreAccumulatedContentLength(
263 int received_content_length,
264 int original_content_length,
[email protected]9a0d6002013-12-19 02:04:28265 spdyproxy::DataReductionRequestType data_reduction_type,
[email protected]b7796462013-12-12 19:42:34266 Profile* profile) {
[email protected]7a299a92012-10-24 23:54:50267 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
268 base::Bind(&UpdateContentLengthPrefs,
[email protected]2db93e42013-08-07 17:40:41269 received_content_length, original_content_length,
[email protected]b7796462013-12-12 19:42:34270 data_reduction_type, profile));
[email protected]7a299a92012-10-24 23:54:50271}
272
273void RecordContentLengthHistograms(
[email protected]f81a5ee2013-06-20 21:08:49274 int64 received_content_length,
275 int64 original_content_length,
276 const base::TimeDelta& freshness_lifetime) {
[email protected]0d36d1e2013-09-24 04:25:28277#if defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50278 // Add the current resource to these histograms only when a valid
279 // X-Original-Content-Length header is present.
280 if (original_content_length >= 0) {
281 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL",
282 received_content_length);
283 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL",
284 original_content_length);
285 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL",
286 original_content_length - received_content_length);
287 } else {
288 // Presume the original content length is the same as the received content
289 // length if the X-Original-Content-Header is not present.
290 original_content_length = received_content_length;
291 }
292 UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length);
293 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength",
294 original_content_length);
295 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference",
296 original_content_length - received_content_length);
[email protected]f81a5ee2013-06-20 21:08:49297 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpContentFreshnessLifetime",
298 freshness_lifetime.InSeconds(),
299 base::TimeDelta::FromHours(1).InSeconds(),
300 base::TimeDelta::FromDays(30).InSeconds(),
301 100);
302 if (freshness_lifetime.InSeconds() <= 0)
303 return;
304 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable",
305 received_content_length);
306 if (freshness_lifetime.InHours() < 4)
307 return;
308 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable4Hours",
309 received_content_length);
310
311 if (freshness_lifetime.InHours() < 24)
312 return;
313 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable24Hours",
314 received_content_length);
[email protected]0d36d1e2013-09-24 04:25:28315#endif // defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50316}
317
[email protected]35558812013-12-18 21:58:36318#if defined(OS_ANDROID)
319void RecordPrecacheStatsOnUIThread(const GURL& url,
320 const base::Time& fetch_time, int64 size,
321 bool was_cached, void* profile_id) {
322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
323
324 Profile* profile = reinterpret_cast<Profile*>(profile_id);
325 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) {
326 return;
327 }
328
329 precache::PrecacheManager* precache_manager =
330 precache::PrecacheManagerFactory::GetForBrowserContext(profile);
331 if (!precache_manager) {
332 // This could be NULL if the profile is off the record.
333 return;
334 }
335
336 precache_manager->RecordStatsForFetch(url, fetch_time, size, was_cached);
337}
338#endif // defined(OS_ANDROID)
339
[email protected]d05ef99c2011-02-01 21:38:16340} // namespace
[email protected]ac039522010-06-15 16:39:44341
[email protected]0651b812011-02-24 00:22:50342ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10343 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48344 BooleanPrefMember* enable_referrers)
[email protected]3ce02412011-03-01 12:01:15345 : event_router_(event_router),
[email protected]a09159a2012-11-29 12:51:48346 profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59347 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48348 enable_do_not_track_(NULL),
349 force_google_safe_search_(NULL),
350 url_blacklist_manager_(NULL),
[email protected]45de676a2014-03-18 23:52:02351 domain_reliability_monitor_(NULL),
[email protected]7a299a92012-10-24 23:54:50352 received_content_length_(0),
353 original_content_length_(0) {
[email protected]4b50cb52011-03-10 00:29:37354 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40355 DCHECK(enable_referrers);
[email protected]0651b812011-02-24 00:22:50356}
357
[email protected]ac039522010-06-15 16:39:44358ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
359
[email protected]a09159a2012-11-29 12:51:48360void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20361 extensions::InfoMap* extension_info_map) {
[email protected]a09159a2012-11-29 12:51:48362 extension_info_map_ = extension_info_map;
363}
364
365void ChromeNetworkDelegate::set_cookie_settings(
366 CookieSettings* cookie_settings) {
367 cookie_settings_ = cookie_settings;
368}
369
[email protected]f53b4802012-12-20 17:04:23370void ChromeNetworkDelegate::set_predictor(
371 chrome_browser_net::Predictor* predictor) {
372 connect_interceptor_.reset(
373 new chrome_browser_net::ConnectInterceptor(predictor));
374}
375
[email protected]3620bbc2013-10-05 05:07:23376void ChromeNetworkDelegate::SetEnableClientHints() {
377 client_hints_.reset(new ClientHints());
378 client_hints_->Init();
379}
380
[email protected]c4a7df82012-08-09 22:48:46381// static
[email protected]a1d4ab072012-06-07 13:21:15382void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46383 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15384}
385
[email protected]0a8db0d2011-04-13 15:15:40386// static
[email protected]9d8cfb682012-09-13 16:48:04387void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40388 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04389 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33390 BooleanPrefMember* force_google_safe_search,
[email protected]0a8db0d2011-04-13 15:15:40391 PrefService* pref_service) {
392 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]96a5c342012-12-04 18:14:02393 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43394 enable_referrers->MoveToThread(
395 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04396 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02397 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43398 enable_do_not_track->MoveToThread(
399 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04400 }
[email protected]d1208ba32012-11-08 11:10:33401 if (force_google_safe_search) {
[email protected]96a5c342012-12-04 18:14:02402 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33403 force_google_safe_search->MoveToThread(
404 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
405 }
[email protected]0a8db0d2011-04-13 15:15:40406}
407
[email protected]4c219e22012-05-05 19:41:04408// static
409void ChromeNetworkDelegate::AllowAccessToAllFiles() {
410 g_allow_file_access_ = true;
411}
412
[email protected]7a299a92012-10-24 23:54:50413// static
[email protected]cb1078de2013-12-23 20:04:22414base::Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() {
[email protected]7a299a92012-10-24 23:54:50415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
416 PrefService* prefs = g_browser_process->local_state();
417 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
418 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
419
[email protected]cb1078de2013-12-23 20:04:22420 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42421 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
422 dict->SetString("historic_received_content_length",
423 base::Int64ToString(total_received));
424 dict->SetString("historic_original_content_length",
425 base::Int64ToString(total_original));
[email protected]7a299a92012-10-24 23:54:50426 return dict;
427}
428
[email protected]cb1078de2013-12-23 20:04:22429base::Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
430 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42431 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
432 dict->SetString("session_received_content_length",
433 base::Int64ToString(received_content_length_));
434 dict->SetString("session_original_content_length",
435 base::Int64ToString(original_content_length_));
[email protected]7a299a92012-10-24 23:54:50436 return dict;
437}
438
[email protected]4875ba12011-03-30 22:31:51439int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12440 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47441 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12442 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:34443#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59444 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
445 // blocked. However, an extension might redirect the request to another URL,
446 // which is not blocked.
447 if (url_blacklist_manager_ &&
[email protected]74575252013-03-12 19:58:21448 url_blacklist_manager_->IsRequestBlocked(*request)) {
[email protected]6a5f77c32011-09-04 19:19:59449 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59450 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26451 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
452 net::NetLog::StringCallback("url",
453 &request->url().possibly_invalid_spec()));
[email protected]91468a92013-03-06 17:21:55454 return net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]6a5f77c32011-09-04 19:19:59455 }
[email protected]3e598ff12011-09-06 11:22:34456#endif
[email protected]6a5f77c32011-09-04 19:19:59457
[email protected]6baff0b52012-03-06 01:30:18458 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
459
[email protected]0a8db0d2011-04-13 15:15:40460 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13461 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04462 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
463 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33464
[email protected]3620bbc2013-10-05 05:07:23465 if (client_hints_) {
466 request->SetExtraRequestHeaderByName(
467 ClientHints::kDevicePixelRatioHeader,
468 client_hints_->GetDevicePixelRatioHeader(), true);
469 }
470
[email protected]d1208ba32012-11-08 11:10:33471 bool force_safe_search = force_google_safe_search_ &&
472 force_google_safe_search_->GetValue();
473
474 net::CompletionCallback wrapped_callback = callback;
475 if (force_safe_search) {
476 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
477 callback,
478 base::Unretained(request),
479 base::Unretained(new_url));
480 }
481
482 int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
483 profile_, extension_info_map_.get(), request, wrapped_callback,
484 new_url);
485
486 if (force_safe_search && rv == net::OK && new_url->is_empty())
487 ForceGoogleSafeSearch(request, new_url);
488
[email protected]f53b4802012-12-20 17:04:23489 if (connect_interceptor_)
490 connect_interceptor_->WitnessURLRequest(request);
491
[email protected]d1208ba32012-11-08 11:10:33492 return rv;
[email protected]d05ef99c2011-02-01 21:38:16493}
494
[email protected]4875ba12011-03-30 22:31:51495int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01496 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47497 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12498 net::HttpRequestHeaders* headers) {
[email protected]186468f2013-10-24 02:44:11499 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest");
[email protected]4875ba12011-03-30 22:31:51500 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18501 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44502}
[email protected]8202d0c2011-02-23 08:31:14503
[email protected]5796dc942011-07-14 19:26:10504void ChromeNetworkDelegate::OnSendHeaders(
505 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15506 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10507 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
508 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16509}
510
[email protected]ea8141e2011-10-05 13:12:51511int ChromeNetworkDelegate::OnHeadersReceived(
512 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47513 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32514 const net::HttpResponseHeaders* original_response_headers,
[email protected]ea8141e2011-10-05 13:12:51515 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
516 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
517 profile_, extension_info_map_.get(), request, callback,
518 original_response_headers, override_response_headers);
519}
520
[email protected]31b2e5f2011-04-20 16:58:32521void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
522 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02523 if (domain_reliability_monitor_)
524 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]31b2e5f2011-04-20 16:58:32525 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18526 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32527}
528
529
[email protected]8202d0c2011-02-23 08:31:14530void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]186468f2013-10-24 02:44:11531 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "ResponseStarted");
[email protected]62fecae2011-04-21 11:08:24532 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18533 profile_, extension_info_map_.get(), request);
534 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14535}
536
[email protected]8523ba52011-05-22 19:00:58537void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
538 int bytes_read) {
[email protected]186468f2013-10-24 02:44:11539 TRACE_EVENT_ASYNC_STEP_PAST1("net", "URLRequest", &request, "DidRead",
540 "bytes_read", bytes_read);
[email protected]6f4b4b42012-08-27 21:59:07541 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
542 request, bytes_read);
543
[email protected]44879ed2012-04-06 01:11:02544#if defined(ENABLE_TASK_MANAGER)
[email protected]17df9e7c2014-02-13 03:49:29545 // This is not completely accurate, but as a first approximation ignore
546 // requests that are served from the cache. See bug 330931 for more info.
547 if (!request.was_cached())
548 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02549#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58550}
551
[email protected]9045b8822012-01-13 20:35:35552void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
553 bool started) {
[email protected]4aca5db2013-08-17 00:51:56554 TRACE_EVENT_ASYNC_END0("net", "URLRequest", request);
[email protected]2756a8e2012-09-07 18:24:29555 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
[email protected]7a299a92012-10-24 23:54:50556 // For better accuracy, we use the actual bytes read instead of the length
557 // specified with the Content-Length header, which may be inaccurate,
558 // or missing, as is the case with chunked encoding.
559 int64 received_content_length = request->received_response_content_length();
560
[email protected]35558812013-12-18 21:58:36561#if defined(OS_ANDROID)
562 if (precache::PrecacheManager::IsPrecachingEnabled()) {
563 // Record precache metrics when a fetch is completed successfully, if
564 // precaching is enabled.
565 BrowserThread::PostTask(
566 BrowserThread::UI, FROM_HERE,
567 base::Bind(&RecordPrecacheStatsOnUIThread, request->url(),
568 base::Time::Now(), received_content_length,
569 request->was_cached(), profile_));
570 }
571#endif // defined(OS_ANDROID)
572
[email protected]7a299a92012-10-24 23:54:50573 // Only record for http or https urls.
574 bool is_http = request->url().SchemeIs("http");
575 bool is_https = request->url().SchemeIs("https");
576
577 if (!request->was_cached() && // Don't record cached content
578 received_content_length && // Zero-byte responses aren't useful.
579 (is_http || is_https)) { // Only record for HTTP or HTTPS urls.
580 int64 original_content_length =
581 request->response_info().headers->GetInt64HeaderValue(
582 "x-original-content-length");
[email protected]9a0d6002013-12-19 02:04:28583 spdyproxy::DataReductionRequestType data_reduction_type =
584 spdyproxy::GetDataReductionRequestType(request);
[email protected]2db93e42013-08-07 17:40:41585
[email protected]f81a5ee2013-06-20 21:08:49586 base::TimeDelta freshness_lifetime =
587 request->response_info().headers->GetFreshnessLifetime(
588 request->response_info().response_time);
[email protected]828eab22013-12-10 22:42:38589 int64 adjusted_original_content_length =
[email protected]9a0d6002013-12-19 02:04:28590 spdyproxy::GetAdjustedOriginalContentLength(
[email protected]828eab22013-12-10 22:42:38591 data_reduction_type, original_content_length,
592 received_content_length);
[email protected]7a299a92012-10-24 23:54:50593 AccumulateContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41594 adjusted_original_content_length,
[email protected]9a0d6002013-12-19 02:04:28595 data_reduction_type);
[email protected]7a299a92012-10-24 23:54:50596 RecordContentLengthHistograms(received_content_length,
[email protected]f81a5ee2013-06-20 21:08:49597 original_content_length,
598 freshness_lifetime);
[email protected]7a299a92012-10-24 23:54:50599 DVLOG(2) << __FUNCTION__
600 << " received content length: " << received_content_length
601 << " original content length: " << original_content_length
602 << " url: " << request->url();
603 }
604
[email protected]48944382011-04-23 13:28:16605 bool is_redirect = request->response_headers() &&
606 net::HttpResponseHeaders::IsRedirectResponseCode(
607 request->response_headers()->response_code());
608 if (!is_redirect) {
609 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18610 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16611 }
[email protected]a83dd332011-07-13 10:41:01612 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
613 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04614 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35615 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01616 } else {
617 NOTREACHED();
[email protected]48944382011-04-23 13:28:16618 }
[email protected]45de676a2014-03-18 23:52:02619 if (domain_reliability_monitor_)
620 domain_reliability_monitor_->OnCompleted(request, started);
[email protected]673514522011-07-13 18:17:18621 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18622
623 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14624}
[email protected]4b50cb52011-03-10 00:29:37625
[email protected]4875ba12011-03-30 22:31:51626void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
627 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18628 profile_, request);
[email protected]4875ba12011-03-30 22:31:51629}
630
[email protected]82a37672011-05-03 12:02:41631void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09632 const base::string16& error) {
[email protected]c454fe672012-03-12 21:18:01633 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18634 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41635}
[email protected]7efc582d2011-08-03 20:46:35636
[email protected]c2911d72011-10-03 22:16:36637net::NetworkDelegate::AuthRequiredResponse
638ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35639 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36640 const net::AuthChallengeInfo& auth_info,
641 const AuthCallback& callback,
642 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45643 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
644 profile_, extension_info_map_.get(), request, auth_info,
645 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35646}
[email protected]9c8ae8c2012-03-09 13:13:35647
[email protected]4c219e22012-05-05 19:41:04648bool ChromeNetworkDelegate::OnCanGetCookies(
649 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35650 const net::CookieList& cookie_list) {
651 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02652 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35653 return true;
654
655 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04656 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35657
658 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16659 int render_frame_id = -1;
[email protected]f7fa5b62014-03-19 15:20:06660
661 // |is_for_blocking_resource| indicates whether the cookies read were for a
662 // blocking resource (eg script, css). It is only temporarily added for
663 // diagnostic purposes, per bug 353678. Will be removed again once data
664 // collection is finished.
665 bool is_for_blocking_resource = false;
666 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
667 if (info && ((!info->IsAsync()) ||
668 info->GetResourceType() == ResourceType::STYLESHEET ||
669 info->GetResourceType() == ResourceType::SCRIPT)) {
670 is_for_blocking_resource = true;
671 }
672
[email protected]f3add922013-12-20 23:17:16673 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
674 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35675 BrowserThread::PostTask(
676 BrowserThread::UI, FROM_HERE,
677 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16678 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04679 request.url(), request.first_party_for_cookies(),
[email protected]f7fa5b62014-03-19 15:20:06680 cookie_list, !allow, is_for_blocking_resource));
[email protected]9c8ae8c2012-03-09 13:13:35681 }
682
683 return allow;
684}
685
[email protected]4c219e22012-05-05 19:41:04686bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
687 const std::string& cookie_line,
688 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35689 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02690 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35691 return true;
692
693 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04694 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35695
[email protected]9c8ae8c2012-03-09 13:13:35696 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16697 int render_frame_id = -1;
698 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
699 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35700 BrowserThread::PostTask(
701 BrowserThread::UI, FROM_HERE,
702 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16703 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04704 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43705 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35706 }
707
708 return allow;
709}
[email protected]4c219e22012-05-05 19:41:04710
711bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45712 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04713 if (g_allow_file_access_)
714 return true;
715
[email protected]d8e4f132012-09-06 04:28:05716#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
717 return true;
718#else
[email protected]4c219e22012-05-05 19:41:04719#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05720 // If we're running Chrome for ChromeOS on Linux, we want to allow file
721 // access.
[email protected]49c4cf852013-09-27 19:28:24722 if (!base::SysInfo::IsRunningOnChromeOS() ||
[email protected]288538482012-09-06 21:09:35723 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05724 return true;
[email protected]288538482012-09-06 21:09:35725 }
[email protected]d8e4f132012-09-06 04:28:05726
727 // Use a whitelist to only allow access to files residing in the list of
728 // directories below.
[email protected]4c219e22012-05-05 19:41:04729 static const char* const kLocalAccessWhiteList[] = {
730 "/home/chronos/user/Downloads",
731 "/home/chronos/user/log",
732 "/media",
733 "/opt/oem",
734 "/usr/share/chromeos-assets",
735 "/tmp",
736 "/var/log",
737 };
[email protected]ae015e12013-11-04 19:11:33738
739 // The actual location of "/home/chronos/user/Downloads" is the Downloads
740 // directory under the profile path ("/home/chronos/user' is a hard link to
741 // current primary logged in profile.) For the support of multi-profile
742 // sessions, we are switching to use explicit "$PROFILE_PATH/Downloads" path
743 // and here whitelist such access.
744 if (!profile_path_.empty()) {
745 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
746 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
747 return true;
748 }
[email protected]d8e4f132012-09-06 04:28:05749#elif defined(OS_ANDROID)
750 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45751 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05752 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
753 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04754 return true;
755
[email protected]d8e4f132012-09-06 04:28:05756 // Whitelist of other allowed directories.
757 static const char* const kLocalAccessWhiteList[] = {
758 "/sdcard",
759 "/mnt/sdcard",
760 };
761#endif
762
[email protected]4c219e22012-05-05 19:41:04763 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45764 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
765 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04766 if (white_listed_path == path.StripTrailingSeparators() ||
767 white_listed_path.IsParent(path)) {
768 return true;
769 }
770 }
[email protected]d8e4f132012-09-06 04:28:05771
[email protected]288538482012-09-06 21:09:35772 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04773 return false;
[email protected]d8e4f132012-09-06 04:28:05774#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04775}
[email protected]a1d4ab072012-06-07 13:21:15776
777bool ChromeNetworkDelegate::OnCanThrottleRequest(
778 const net::URLRequest& request) const {
[email protected]c4a7df82012-08-09 22:48:46779 if (g_never_throttle_requests_) {
[email protected]a1d4ab072012-06-07 13:21:15780 return false;
781 }
782
[email protected]c4a7df82012-08-09 22:48:46783 return request.first_party_for_cookies().scheme() ==
[email protected]885c0e92012-11-13 20:27:42784 extensions::kExtensionScheme;
[email protected]a1d4ab072012-06-07 13:21:15785}
[email protected]aa28181e2012-06-13 00:53:58786
[email protected]e6d017652013-05-17 18:01:40787bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
788 const GURL& url,
789 const GURL& first_party_for_cookies) const {
790 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02791 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40792 return false;
793
794 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
795 url, first_party_for_cookies);
796 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
797 url, first_party_for_cookies);
798 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
799 return privacy_mode;
800}
801
[email protected]aa28181e2012-06-13 00:53:58802int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
803 net::SocketStream* socket,
804 const net::CompletionCallback& callback) {
805#if defined(ENABLE_CONFIGURATION_POLICY)
806 if (url_blacklist_manager_ &&
807 url_blacklist_manager_->IsURLBlocked(socket->url())) {
808 // URL access blocked by policy.
[email protected]aa28181e2012-06-13 00:53:58809 socket->net_log()->AddEvent(
[email protected]2fa08912012-06-14 20:56:26810 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
811 net::NetLog::StringCallback("url",
812 &socket->url().possibly_invalid_spec()));
[email protected]91468a92013-03-06 17:21:55813 return net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]aa28181e2012-06-13 00:53:58814 }
815#endif
816 return net::OK;
817}
[email protected]5a07c192012-07-30 20:18:22818
[email protected]7a299a92012-10-24 23:54:50819void ChromeNetworkDelegate::AccumulateContentLength(
[email protected]b7796462013-12-12 19:42:34820 int64 received_content_length,
821 int64 original_content_length,
[email protected]9a0d6002013-12-19 02:04:28822 spdyproxy::DataReductionRequestType data_reduction_type) {
[email protected]7a299a92012-10-24 23:54:50823 DCHECK_GE(received_content_length, 0);
824 DCHECK_GE(original_content_length, 0);
825 StoreAccumulatedContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41826 original_content_length,
[email protected]b7796462013-12-12 19:42:34827 data_reduction_type,
828 reinterpret_cast<Profile*>(profile_));
[email protected]7a299a92012-10-24 23:54:50829 received_content_length_ += received_content_length;
830 original_content_length_ += original_content_length;
831}