blob: 8e5cf65d6a1a7f023afc5855c8ac187574e0d18c [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]6f4b4b42012-08-27 21:59:0731#include "chrome/browser/performance_monitor/performance_monitor.h"
[email protected]6baff0b52012-03-06 01:30:1832#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5833#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4034#include "chrome/common/pref_names.h"
[email protected]a1d4ab072012-06-07 13:21:1535#include "chrome/common/url_constants.h"
[email protected]a0a06932014-04-14 21:23:4236#include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.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)
[email protected]d827e112014-03-31 17:45:0567#include "chrome/browser/io_thread.h"
[email protected]35558812013-12-18 21:58:3668#include "components/precache/content/precache_manager.h"
69#include "components/precache/content/precache_manager_factory.h"
70#endif
71
[email protected]631bb742011-11-02 11:29:3972using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1173using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2574using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3975
[email protected]d8e4f132012-09-06 04:28:0576// By default we don't allow access to all file:// urls on ChromeOS and
77// Android.
78#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0479bool ChromeNetworkDelegate::g_allow_file_access_ = false;
80#else
81bool ChromeNetworkDelegate::g_allow_file_access_ = true;
82#endif
83
[email protected]c4a7df82012-08-09 22:48:4684// This remains false unless the --disable-extensions-http-throttling
85// flag is passed to the browser.
86bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
87
[email protected]d05ef99c2011-02-01 21:38:1688namespace {
89
[email protected]9d8cfb682012-09-13 16:48:0490const char kDNTHeader[] = "DNT";
91
[email protected]8202d0c2011-02-23 08:31:1492// If the |request| failed due to problems with a proxy, forward the error to
93// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5094void ForwardProxyErrors(net::URLRequest* request,
[email protected]5a38dfd2012-07-23 23:22:1095 extensions::EventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1896 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1497 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0598 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1499 case net::ERR_PROXY_AUTH_UNSUPPORTED:
100 case net::ERR_PROXY_CONNECTION_FAILED:
101 case net::ERR_TUNNEL_CONNECTION_FAILED:
[email protected]c454fe672012-03-12 21:18:01102 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:05103 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:14104 }
105 }
106}
107
[email protected]d1208ba32012-11-08 11:10:33108// 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.
111bool 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);
118}
119
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.
123std::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;
127
128 std::vector<std::string> parameters;
129 base::SplitString(query, '&', &parameters);
130
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);
136 }
137 }
138
139 new_parameters.push_back(safe_parameter);
140 new_parameters.push_back(ssui_parameter);
141 return JoinString(new_parameters, '&');
142}
143
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.
147void ForceGoogleSafeSearch(net::URLRequest* request,
148 GURL* new_url) {
[email protected]c02f79d2013-06-27 21:46:47149 if (!google_util::IsGoogleSearchUrl(request->url()) &&
150 !google_util::IsGoogleHomePageUrl(request->url()))
[email protected]d1208ba32012-11-08 11:10:33151 return;
152
153 std::string query = request->url().query();
154 std::string new_query = AddSafeSearchParameters(query);
155 if (query == new_query)
156 return;
157
158 GURL::Replacements replacements;
159 replacements.SetQueryStr(new_query);
160 *new_url = request->url().ReplaceComponents(replacements);
161}
162
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.
167void ForceGoogleSafeSearchCallbackWrapper(
168 const net::CompletionCallback& callback,
169 net::URLRequest* request,
170 GURL* new_url,
171 int rv) {
172 if (rv == net::OK && new_url->is_empty())
173 ForceGoogleSafeSearch(request, new_url);
174 callback.Run(rv);
175}
176
[email protected]6baff0b52012-03-06 01:30:18177enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
178
[email protected]98b6d942013-11-10 00:34:07179// Notifies the extensions::ProcessManager that a request has started or stopped
[email protected]9dfed872013-12-30 23:08:56180// for a particular RenderFrame.
[email protected]6baff0b52012-03-06 01:30:18181void NotifyEPMRequestStatus(RequestStatus status,
182 void* profile_id,
183 int process_id,
[email protected]9dfed872013-12-30 23:08:56184 int render_frame_id) {
[email protected]6baff0b52012-03-06 01:30:18185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
186 Profile* profile = reinterpret_cast<Profile*>(profile_id);
187 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
188 return;
189
[email protected]98b6d942013-11-10 00:34:07190 extensions::ProcessManager* process_manager =
[email protected]be93bba02012-10-24 16:44:03191 extensions::ExtensionSystem::Get(profile)->process_manager();
[email protected]e5775a52012-03-17 04:59:57192 // This may be NULL in unit tests.
[email protected]98b6d942013-11-10 00:34:07193 if (!process_manager)
[email protected]e5775a52012-03-17 04:59:57194 return;
195
[email protected]6baff0b52012-03-06 01:30:18196 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
197 // system-level request).
[email protected]9dfed872013-12-30 23:08:56198 content::RenderFrameHost* render_frame_host =
199 content::RenderFrameHost::FromID(process_id, render_frame_id);
200 if (render_frame_host) {
[email protected]6baff0b52012-03-06 01:30:18201 if (status == REQUEST_STARTED) {
[email protected]9dfed872013-12-30 23:08:56202 process_manager->OnNetworkRequestStarted(render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18203 } else if (status == REQUEST_DONE) {
[email protected]9dfed872013-12-30 23:08:56204 process_manager->OnNetworkRequestDone(render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18205 } else {
206 NOTREACHED();
207 }
208 }
209}
210
211void ForwardRequestStatus(
212 RequestStatus status, net::URLRequest* request, void* profile_id) {
[email protected]ea114722012-03-12 01:11:25213 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
214 if (!info)
215 return;
216
[email protected]9dfed872013-12-30 23:08:56217 int process_id, render_frame_id;
218 if (info->GetAssociatedRenderFrame(&process_id, &render_frame_id)) {
[email protected]6baff0b52012-03-06 01:30:18219 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
220 base::Bind(&NotifyEPMRequestStatus,
[email protected]9dfed872013-12-30 23:08:56221 status, profile_id, process_id, render_frame_id));
[email protected]6baff0b52012-03-06 01:30:18222 }
223}
224
[email protected]828eab22013-12-10 22:42:38225void UpdateContentLengthPrefs(
226 int received_content_length,
227 int original_content_length,
[email protected]a0a06932014-04-14 21:23:42228 data_reduction_proxy::DataReductionProxyRequestType request_type,
[email protected]b7796462013-12-12 19:42:34229 Profile* profile) {
[email protected]7a299a92012-10-24 23:54:50230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
231 DCHECK_GE(received_content_length, 0);
232 DCHECK_GE(original_content_length, 0);
233
[email protected]a09159a2012-11-29 12:51:48234 // Can be NULL in a unit test.
235 if (!g_browser_process)
236 return;
237
[email protected]7a299a92012-10-24 23:54:50238 PrefService* prefs = g_browser_process->local_state();
[email protected]a09159a2012-11-29 12:51:48239 if (!prefs)
240 return;
[email protected]7a299a92012-10-24 23:54:50241
[email protected]b7796462013-12-12 19:42:34242 // Ignore off-the-record data.
243 if (!g_browser_process->profile_manager()->IsValidProfile(profile) ||
244 profile->IsOffTheRecord()) {
245 return;
246 }
[email protected]0d36d1e2013-09-24 04:25:28247#if defined(OS_ANDROID)
[email protected]b1d5ed52013-12-18 02:34:56248 // If Android ever goes multi profile, the profile should be passed so that
249 // the browser preference will be taken.
[email protected]0d36d1e2013-09-24 04:25:28250 bool with_data_reduction_proxy_enabled =
[email protected]b1d5ed52013-12-18 02:34:56251 ProfileManager::GetActiveUserProfile()->GetPrefs()->GetBoolean(
[email protected]a0a06932014-04-14 21:23:42252 data_reduction_proxy::prefs::kDataReductionProxyEnabled);
[email protected]0d36d1e2013-09-24 04:25:28253#else
254 bool with_data_reduction_proxy_enabled = false;
[email protected]eac11e12013-03-19 22:04:32255#endif
[email protected]0d36d1e2013-09-24 04:25:28256
[email protected]a0a06932014-04-14 21:23:42257 data_reduction_proxy::UpdateContentLengthPrefs(received_content_length,
258 original_content_length,
259 with_data_reduction_proxy_enabled,
260 request_type, prefs);
[email protected]7a299a92012-10-24 23:54:50261}
262
[email protected]828eab22013-12-10 22:42:38263void StoreAccumulatedContentLength(
264 int received_content_length,
265 int original_content_length,
[email protected]a0a06932014-04-14 21:23:42266 data_reduction_proxy::DataReductionProxyRequestType request_type,
[email protected]b7796462013-12-12 19:42:34267 Profile* profile) {
[email protected]7a299a92012-10-24 23:54:50268 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
269 base::Bind(&UpdateContentLengthPrefs,
[email protected]2db93e42013-08-07 17:40:41270 received_content_length, original_content_length,
[email protected]a0a06932014-04-14 21:23:42271 request_type, profile));
[email protected]7a299a92012-10-24 23:54:50272}
273
274void RecordContentLengthHistograms(
[email protected]f81a5ee2013-06-20 21:08:49275 int64 received_content_length,
276 int64 original_content_length,
277 const base::TimeDelta& freshness_lifetime) {
[email protected]0d36d1e2013-09-24 04:25:28278#if defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50279 // 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);
288 } else {
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;
292 }
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);
[email protected]f81a5ee2013-06-20 21:08:49298 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpContentFreshnessLifetime",
299 freshness_lifetime.InSeconds(),
300 base::TimeDelta::FromHours(1).InSeconds(),
301 base::TimeDelta::FromDays(30).InSeconds(),
302 100);
303 if (freshness_lifetime.InSeconds() <= 0)
304 return;
305 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable",
306 received_content_length);
307 if (freshness_lifetime.InHours() < 4)
308 return;
309 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable4Hours",
310 received_content_length);
311
312 if (freshness_lifetime.InHours() < 24)
313 return;
314 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable24Hours",
315 received_content_length);
[email protected]0d36d1e2013-09-24 04:25:28316#endif // defined(OS_ANDROID)
[email protected]7a299a92012-10-24 23:54:50317}
318
[email protected]35558812013-12-18 21:58:36319#if defined(OS_ANDROID)
320void 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));
324
325 Profile* profile = reinterpret_cast<Profile*>(profile_id);
326 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) {
327 return;
328 }
329
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.
334 return;
335 }
336
337 precache_manager->RecordStatsForFetch(url, fetch_time, size, was_cached);
338}
[email protected]d827e112014-03-31 17:45:05339
340void 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);
346}
[email protected]35558812013-12-18 21:58:36347#endif // defined(OS_ANDROID)
348
[email protected]d05ef99c2011-02-01 21:38:16349} // namespace
[email protected]ac039522010-06-15 16:39:44350
[email protected]0651b812011-02-24 00:22:50351ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10352 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48353 BooleanPrefMember* enable_referrers)
[email protected]3ce02412011-03-01 12:01:15354 : event_router_(event_router),
[email protected]a09159a2012-11-29 12:51:48355 profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59356 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48357 enable_do_not_track_(NULL),
358 force_google_safe_search_(NULL),
359 url_blacklist_manager_(NULL),
[email protected]45de676a2014-03-18 23:52:02360 domain_reliability_monitor_(NULL),
[email protected]7a299a92012-10-24 23:54:50361 received_content_length_(0),
[email protected]d827e112014-03-31 17:45:05362 original_content_length_(0),
363 first_request_(true) {
[email protected]4b50cb52011-03-10 00:29:37364 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40365 DCHECK(enable_referrers);
[email protected]0651b812011-02-24 00:22:50366}
367
[email protected]ac039522010-06-15 16:39:44368ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
369
[email protected]a09159a2012-11-29 12:51:48370void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20371 extensions::InfoMap* extension_info_map) {
[email protected]a09159a2012-11-29 12:51:48372 extension_info_map_ = extension_info_map;
373}
374
375void ChromeNetworkDelegate::set_cookie_settings(
376 CookieSettings* cookie_settings) {
377 cookie_settings_ = cookie_settings;
378}
379
[email protected]f53b4802012-12-20 17:04:23380void ChromeNetworkDelegate::set_predictor(
381 chrome_browser_net::Predictor* predictor) {
382 connect_interceptor_.reset(
383 new chrome_browser_net::ConnectInterceptor(predictor));
384}
385
[email protected]3620bbc2013-10-05 05:07:23386void ChromeNetworkDelegate::SetEnableClientHints() {
387 client_hints_.reset(new ClientHints());
388 client_hints_->Init();
389}
390
[email protected]c4a7df82012-08-09 22:48:46391// static
[email protected]a1d4ab072012-06-07 13:21:15392void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46393 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15394}
395
[email protected]0a8db0d2011-04-13 15:15:40396// static
[email protected]9d8cfb682012-09-13 16:48:04397void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40398 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04399 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33400 BooleanPrefMember* force_google_safe_search,
[email protected]0a8db0d2011-04-13 15:15:40401 PrefService* pref_service) {
402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]96a5c342012-12-04 18:14:02403 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43404 enable_referrers->MoveToThread(
405 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04406 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02407 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43408 enable_do_not_track->MoveToThread(
409 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04410 }
[email protected]d1208ba32012-11-08 11:10:33411 if (force_google_safe_search) {
[email protected]96a5c342012-12-04 18:14:02412 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33413 force_google_safe_search->MoveToThread(
414 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
415 }
[email protected]0a8db0d2011-04-13 15:15:40416}
417
[email protected]4c219e22012-05-05 19:41:04418// static
419void ChromeNetworkDelegate::AllowAccessToAllFiles() {
420 g_allow_file_access_ = true;
421}
422
[email protected]7a299a92012-10-24 23:54:50423// static
[email protected]cb1078de2013-12-23 20:04:22424base::Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() {
[email protected]7a299a92012-10-24 23:54:50425 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
426 PrefService* prefs = g_browser_process->local_state();
[email protected]a0a06932014-04-14 21:23:42427 int64 total_received = prefs->GetInt64(
428 data_reduction_proxy::prefs::kHttpReceivedContentLength);
429 int64 total_original = prefs->GetInt64(
430 data_reduction_proxy::prefs::kHttpOriginalContentLength);
[email protected]7a299a92012-10-24 23:54:50431
[email protected]cb1078de2013-12-23 20:04:22432 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42433 // 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));
[email protected]7a299a92012-10-24 23:54:50438 return dict;
439}
440
[email protected]cb1078de2013-12-23 20:04:22441base::Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
442 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42443 // 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_));
[email protected]7a299a92012-10-24 23:54:50448 return dict;
449}
450
[email protected]4875ba12011-03-30 22:31:51451int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12452 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47453 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12454 GURL* new_url) {
[email protected]d827e112014-03-31 17:45:05455#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));
462#endif
463 if (record_timing) {
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));
471 }
472 }
473#endif // defined(OS_ANDROID)
474
[email protected]3e598ff12011-09-06 11:22:34475#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59476 // 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.
[email protected]cb85f8e52014-04-08 11:44:40479 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]6a5f77c32011-09-04 19:19:59480 if (url_blacklist_manager_ &&
[email protected]cb85f8e52014-04-08 11:44:40481 url_blacklist_manager_->IsRequestBlocked(*request, &error)) {
[email protected]6a5f77c32011-09-04 19:19:59482 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59483 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26484 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
485 net::NetLog::StringCallback("url",
486 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40487 return error;
[email protected]6a5f77c32011-09-04 19:19:59488 }
[email protected]3e598ff12011-09-06 11:22:34489#endif
[email protected]6a5f77c32011-09-04 19:19:59490
[email protected]6baff0b52012-03-06 01:30:18491 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
492
[email protected]0a8db0d2011-04-13 15:15:40493 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13494 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04495 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
496 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33497
[email protected]3620bbc2013-10-05 05:07:23498 if (client_hints_) {
499 request->SetExtraRequestHeaderByName(
500 ClientHints::kDevicePixelRatioHeader,
501 client_hints_->GetDevicePixelRatioHeader(), true);
502 }
503
[email protected]d1208ba32012-11-08 11:10:33504 bool force_safe_search = force_google_safe_search_ &&
505 force_google_safe_search_->GetValue();
506
507 net::CompletionCallback wrapped_callback = callback;
508 if (force_safe_search) {
509 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
510 callback,
511 base::Unretained(request),
512 base::Unretained(new_url));
513 }
514
515 int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
516 profile_, extension_info_map_.get(), request, wrapped_callback,
517 new_url);
518
519 if (force_safe_search && rv == net::OK && new_url->is_empty())
520 ForceGoogleSafeSearch(request, new_url);
521
[email protected]f53b4802012-12-20 17:04:23522 if (connect_interceptor_)
523 connect_interceptor_->WitnessURLRequest(request);
524
[email protected]d1208ba32012-11-08 11:10:33525 return rv;
[email protected]d05ef99c2011-02-01 21:38:16526}
527
[email protected]4875ba12011-03-30 22:31:51528int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01529 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47530 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12531 net::HttpRequestHeaders* headers) {
[email protected]186468f2013-10-24 02:44:11532 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest");
[email protected]4875ba12011-03-30 22:31:51533 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18534 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44535}
[email protected]8202d0c2011-02-23 08:31:14536
[email protected]5796dc942011-07-14 19:26:10537void ChromeNetworkDelegate::OnSendHeaders(
538 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15539 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10540 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
541 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16542}
543
[email protected]ea8141e2011-10-05 13:12:51544int ChromeNetworkDelegate::OnHeadersReceived(
545 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47546 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32547 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16548 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
549 GURL* allowed_unsafe_redirect_url) {
[email protected]ea8141e2011-10-05 13:12:51550 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16551 profile_,
552 extension_info_map_.get(),
553 request,
554 callback,
555 original_response_headers,
556 override_response_headers,
557 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51558}
559
[email protected]31b2e5f2011-04-20 16:58:32560void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
561 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02562 if (domain_reliability_monitor_)
563 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]31b2e5f2011-04-20 16:58:32564 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18565 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32566}
567
568
[email protected]8202d0c2011-02-23 08:31:14569void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]186468f2013-10-24 02:44:11570 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "ResponseStarted");
[email protected]62fecae2011-04-21 11:08:24571 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18572 profile_, extension_info_map_.get(), request);
573 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14574}
575
[email protected]8523ba52011-05-22 19:00:58576void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
577 int bytes_read) {
[email protected]186468f2013-10-24 02:44:11578 TRACE_EVENT_ASYNC_STEP_PAST1("net", "URLRequest", &request, "DidRead",
579 "bytes_read", bytes_read);
[email protected]6f4b4b42012-08-27 21:59:07580 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
581 request, bytes_read);
582
[email protected]44879ed2012-04-06 01:11:02583#if defined(ENABLE_TASK_MANAGER)
[email protected]17df9e7c2014-02-13 03:49:29584 // 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);
[email protected]44879ed2012-04-06 01:11:02588#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58589}
590
[email protected]9045b8822012-01-13 20:35:35591void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
592 bool started) {
[email protected]4aca5db2013-08-17 00:51:56593 TRACE_EVENT_ASYNC_END0("net", "URLRequest", request);
[email protected]2756a8e2012-09-07 18:24:29594 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
[email protected]7a299a92012-10-24 23:54:50595 // 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();
599
[email protected]35558812013-12-18 21:58:36600#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_));
609 }
610#endif // defined(OS_ANDROID)
611
[email protected]7a299a92012-10-24 23:54:50612 // Only record for http or https urls.
613 bool is_http = request->url().SchemeIs("http");
614 bool is_https = request->url().SchemeIs("https");
615
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");
[email protected]a0a06932014-04-14 21:23:42622 data_reduction_proxy::DataReductionProxyRequestType request_type =
623 data_reduction_proxy::GetDataReductionProxyRequestType(request);
[email protected]2db93e42013-08-07 17:40:41624
[email protected]f81a5ee2013-06-20 21:08:49625 base::TimeDelta freshness_lifetime =
626 request->response_info().headers->GetFreshnessLifetime(
627 request->response_info().response_time);
[email protected]828eab22013-12-10 22:42:38628 int64 adjusted_original_content_length =
[email protected]a0a06932014-04-14 21:23:42629 data_reduction_proxy::GetAdjustedOriginalContentLength(
630 request_type, original_content_length,
[email protected]828eab22013-12-10 22:42:38631 received_content_length);
[email protected]7a299a92012-10-24 23:54:50632 AccumulateContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41633 adjusted_original_content_length,
[email protected]a0a06932014-04-14 21:23:42634 request_type);
[email protected]7a299a92012-10-24 23:54:50635 RecordContentLengthHistograms(received_content_length,
[email protected]f81a5ee2013-06-20 21:08:49636 original_content_length,
637 freshness_lifetime);
[email protected]7a299a92012-10-24 23:54:50638 DVLOG(2) << __FUNCTION__
639 << " received content length: " << received_content_length
640 << " original content length: " << original_content_length
641 << " url: " << request->url();
642 }
643
[email protected]48944382011-04-23 13:28:16644 bool is_redirect = request->response_headers() &&
645 net::HttpResponseHeaders::IsRedirectResponseCode(
646 request->response_headers()->response_code());
647 if (!is_redirect) {
648 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18649 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16650 }
[email protected]a83dd332011-07-13 10:41:01651 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
652 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04653 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35654 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01655 } else {
656 NOTREACHED();
[email protected]48944382011-04-23 13:28:16657 }
[email protected]45de676a2014-03-18 23:52:02658 if (domain_reliability_monitor_)
659 domain_reliability_monitor_->OnCompleted(request, started);
[email protected]673514522011-07-13 18:17:18660 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18661
662 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14663}
[email protected]4b50cb52011-03-10 00:29:37664
[email protected]4875ba12011-03-30 22:31:51665void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
666 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18667 profile_, request);
[email protected]4875ba12011-03-30 22:31:51668}
669
[email protected]82a37672011-05-03 12:02:41670void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09671 const base::string16& error) {
[email protected]c454fe672012-03-12 21:18:01672 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18673 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41674}
[email protected]7efc582d2011-08-03 20:46:35675
[email protected]c2911d72011-10-03 22:16:36676net::NetworkDelegate::AuthRequiredResponse
677ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35678 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36679 const net::AuthChallengeInfo& auth_info,
680 const AuthCallback& callback,
681 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45682 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
683 profile_, extension_info_map_.get(), request, auth_info,
684 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35685}
[email protected]9c8ae8c2012-03-09 13:13:35686
[email protected]4c219e22012-05-05 19:41:04687bool ChromeNetworkDelegate::OnCanGetCookies(
688 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35689 const net::CookieList& cookie_list) {
690 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02691 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35692 return true;
693
694 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04695 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35696
697 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16698 int render_frame_id = -1;
[email protected]f7fa5b62014-03-19 15:20:06699
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;
710 }
711
[email protected]f3add922013-12-20 23:17:16712 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
713 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35714 BrowserThread::PostTask(
715 BrowserThread::UI, FROM_HERE,
716 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16717 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04718 request.url(), request.first_party_for_cookies(),
[email protected]f7fa5b62014-03-19 15:20:06719 cookie_list, !allow, is_for_blocking_resource));
[email protected]9c8ae8c2012-03-09 13:13:35720 }
721
722 return allow;
723}
724
[email protected]4c219e22012-05-05 19:41:04725bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
726 const std::string& cookie_line,
727 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35728 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02729 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35730 return true;
731
732 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04733 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35734
[email protected]9c8ae8c2012-03-09 13:13:35735 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16736 int render_frame_id = -1;
737 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
738 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35739 BrowserThread::PostTask(
740 BrowserThread::UI, FROM_HERE,
741 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16742 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04743 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43744 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35745 }
746
747 return allow;
748}
[email protected]4c219e22012-05-05 19:41:04749
750bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45751 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04752 if (g_allow_file_access_)
753 return true;
754
[email protected]d8e4f132012-09-06 04:28:05755#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
756 return true;
757#else
[email protected]4c219e22012-05-05 19:41:04758#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05759 // If we're running Chrome for ChromeOS on Linux, we want to allow file
760 // access.
[email protected]49c4cf852013-09-27 19:28:24761 if (!base::SysInfo::IsRunningOnChromeOS() ||
[email protected]288538482012-09-06 21:09:35762 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05763 return true;
[email protected]288538482012-09-06 21:09:35764 }
[email protected]d8e4f132012-09-06 04:28:05765
766 // Use a whitelist to only allow access to files residing in the list of
767 // directories below.
[email protected]4c219e22012-05-05 19:41:04768 static const char* const kLocalAccessWhiteList[] = {
769 "/home/chronos/user/Downloads",
770 "/home/chronos/user/log",
771 "/media",
772 "/opt/oem",
773 "/usr/share/chromeos-assets",
774 "/tmp",
775 "/var/log",
776 };
[email protected]ae015e12013-11-04 19:11:33777
778 // The actual location of "/home/chronos/user/Downloads" is the Downloads
779 // directory under the profile path ("/home/chronos/user' is a hard link to
780 // current primary logged in profile.) For the support of multi-profile
781 // sessions, we are switching to use explicit "$PROFILE_PATH/Downloads" path
782 // and here whitelist such access.
783 if (!profile_path_.empty()) {
784 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
785 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
786 return true;
787 }
[email protected]d8e4f132012-09-06 04:28:05788#elif defined(OS_ANDROID)
789 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45790 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05791 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
792 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04793 return true;
794
[email protected]d8e4f132012-09-06 04:28:05795 // Whitelist of other allowed directories.
796 static const char* const kLocalAccessWhiteList[] = {
797 "/sdcard",
798 "/mnt/sdcard",
799 };
800#endif
801
[email protected]4c219e22012-05-05 19:41:04802 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45803 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
804 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04805 if (white_listed_path == path.StripTrailingSeparators() ||
806 white_listed_path.IsParent(path)) {
807 return true;
808 }
809 }
[email protected]d8e4f132012-09-06 04:28:05810
[email protected]288538482012-09-06 21:09:35811 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04812 return false;
[email protected]d8e4f132012-09-06 04:28:05813#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04814}
[email protected]a1d4ab072012-06-07 13:21:15815
816bool ChromeNetworkDelegate::OnCanThrottleRequest(
817 const net::URLRequest& request) const {
[email protected]c4a7df82012-08-09 22:48:46818 if (g_never_throttle_requests_) {
[email protected]a1d4ab072012-06-07 13:21:15819 return false;
820 }
821
[email protected]c4a7df82012-08-09 22:48:46822 return request.first_party_for_cookies().scheme() ==
[email protected]885c0e92012-11-13 20:27:42823 extensions::kExtensionScheme;
[email protected]a1d4ab072012-06-07 13:21:15824}
[email protected]aa28181e2012-06-13 00:53:58825
[email protected]e6d017652013-05-17 18:01:40826bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
827 const GURL& url,
828 const GURL& first_party_for_cookies) const {
829 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02830 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40831 return false;
832
833 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
834 url, first_party_for_cookies);
835 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
836 url, first_party_for_cookies);
837 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
838 return privacy_mode;
839}
840
[email protected]aa28181e2012-06-13 00:53:58841int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
842 net::SocketStream* socket,
843 const net::CompletionCallback& callback) {
844#if defined(ENABLE_CONFIGURATION_POLICY)
845 if (url_blacklist_manager_ &&
846 url_blacklist_manager_->IsURLBlocked(socket->url())) {
847 // URL access blocked by policy.
[email protected]aa28181e2012-06-13 00:53:58848 socket->net_log()->AddEvent(
[email protected]2fa08912012-06-14 20:56:26849 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
850 net::NetLog::StringCallback("url",
851 &socket->url().possibly_invalid_spec()));
[email protected]91468a92013-03-06 17:21:55852 return net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]aa28181e2012-06-13 00:53:58853 }
854#endif
855 return net::OK;
856}
[email protected]5a07c192012-07-30 20:18:22857
[email protected]7a299a92012-10-24 23:54:50858void ChromeNetworkDelegate::AccumulateContentLength(
[email protected]b7796462013-12-12 19:42:34859 int64 received_content_length,
860 int64 original_content_length,
[email protected]a0a06932014-04-14 21:23:42861 data_reduction_proxy::DataReductionProxyRequestType request_type) {
[email protected]7a299a92012-10-24 23:54:50862 DCHECK_GE(received_content_length, 0);
863 DCHECK_GE(original_content_length, 0);
864 StoreAccumulatedContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41865 original_content_length,
[email protected]a0a06932014-04-14 21:23:42866 request_type,
[email protected]b7796462013-12-12 19:42:34867 reinterpret_cast<Profile*>(profile_));
[email protected]7a299a92012-10-24 23:54:50868 received_content_length_ += received_content_length;
869 original_content_length_ += original_content_length;
870}