blob: 72ec5391aa2e262876856d6506912cf59f1447f7 [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]28c5d0b72014-05-13 08:19:5932#include "chrome/browser/prerender/prerender_tracker.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]a0a06932014-04-14 21:23:4237#include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h"
[email protected]d8fc4722014-06-13 13:17:1538#include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
39#include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.h"
[email protected]45de676a2014-03-18 23:52:0240#include "components/domain_reliability/monitor.h"
[email protected]c38831a12011-10-28 12:44:4941#include "content/public/browser/browser_thread.h"
[email protected]9dfed872013-12-30 23:08:5642#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3343#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3544#include "content/public/browser/resource_request_info.h"
[email protected]59b0e602014-01-30 00:41:2445#include "extensions/browser/extension_system.h"
[email protected]38427a12013-11-09 17:34:2046#include "extensions/browser/info_map.h"
[email protected]98b6d942013-11-10 00:34:0747#include "extensions/browser/process_manager.h"
[email protected]885c0e92012-11-13 20:27:4248#include "extensions/common/constants.h"
[email protected]82b42302011-04-20 16:28:1649#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1450#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5951#include "net/base/net_log.h"
[email protected]5b9bc352012-07-18 13:13:3452#include "net/cookies/canonical_cookie.h"
53#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4454#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1655#include "net/http/http_response_headers.h"
[email protected]aa28181e2012-06-13 00:53:5856#include "net/socket_stream/socket_stream.h"
[email protected]d05ef99c2011-02-01 21:38:1657#include "net/url_request/url_request.h"
[email protected]28c5d0b72014-05-13 08:19:5958#include "net/url_request/url_request_context.h"
[email protected]d05ef99c2011-02-01 21:38:1659
[email protected]4c219e22012-05-05 19:41:0460#if defined(OS_CHROMEOS)
[email protected]288538482012-09-06 21:09:3561#include "base/command_line.h"
[email protected]49c4cf852013-09-27 19:28:2462#include "base/sys_info.h"
[email protected]288538482012-09-06 21:09:3563#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0464#endif
65
[email protected]3e598ff12011-09-06 11:22:3466#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]f522afa2014-01-08 15:36:3667#include "components/policy/core/browser/url_blacklist_manager.h"
[email protected]3e598ff12011-09-06 11:22:3468#endif
69
[email protected]35558812013-12-18 21:58:3670#if defined(OS_ANDROID)
[email protected]d827e112014-03-31 17:45:0571#include "chrome/browser/io_thread.h"
[email protected]35558812013-12-18 21:58:3672#include "components/precache/content/precache_manager.h"
73#include "components/precache/content/precache_manager_factory.h"
74#endif
75
[email protected]631bb742011-11-02 11:29:3976using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1177using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2578using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3979
[email protected]d8e4f132012-09-06 04:28:0580// By default we don't allow access to all file:// urls on ChromeOS and
81// Android.
82#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0483bool ChromeNetworkDelegate::g_allow_file_access_ = false;
84#else
85bool ChromeNetworkDelegate::g_allow_file_access_ = true;
86#endif
87
[email protected]c4a7df82012-08-09 22:48:4688// This remains false unless the --disable-extensions-http-throttling
89// flag is passed to the browser.
90bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
91
[email protected]d05ef99c2011-02-01 21:38:1692namespace {
93
[email protected]9d8cfb682012-09-13 16:48:0494const char kDNTHeader[] = "DNT";
95
[email protected]8202d0c2011-02-23 08:31:1496// If the |request| failed due to problems with a proxy, forward the error to
97// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5098void ForwardProxyErrors(net::URLRequest* request,
[email protected]5a38dfd2012-07-23 23:22:1099 extensions::EventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:18100 void* profile) {
[email protected]8202d0c2011-02-23 08:31:14101 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:05102 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:14103 case net::ERR_PROXY_AUTH_UNSUPPORTED:
104 case net::ERR_PROXY_CONNECTION_FAILED:
105 case net::ERR_TUNNEL_CONNECTION_FAILED:
[email protected]c454fe672012-03-12 21:18:01106 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:05107 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:14108 }
109 }
110}
111
[email protected]d1208ba32012-11-08 11:10:33112// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
113// same key as the the |second_parameter| (e.g. foo=baz). Both parameters
114// must be in key=value form.
115bool HasSameParameterKey(const std::string& first_parameter,
116 const std::string& second_parameter) {
117 DCHECK(second_parameter.find("=") != std::string::npos);
118 // Prefix for "foo=bar" is "foo=".
119 std::string parameter_prefix = second_parameter.substr(
120 0, second_parameter.find("=") + 1);
121 return StartsWithASCII(first_parameter, parameter_prefix, false);
122}
123
124// Examines the query string containing parameters and adds the necessary ones
125// so that SafeSearch is active. |query| is the string to examine and the
126// return value is the |query| string modified such that SafeSearch is active.
127std::string AddSafeSearchParameters(const std::string& query) {
128 std::vector<std::string> new_parameters;
129 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
130 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
131
132 std::vector<std::string> parameters;
133 base::SplitString(query, '&', &parameters);
134
135 std::vector<std::string>::iterator it;
136 for (it = parameters.begin(); it < parameters.end(); ++it) {
137 if (!HasSameParameterKey(*it, safe_parameter) &&
138 !HasSameParameterKey(*it, ssui_parameter)) {
139 new_parameters.push_back(*it);
140 }
141 }
142
143 new_parameters.push_back(safe_parameter);
144 new_parameters.push_back(ssui_parameter);
145 return JoinString(new_parameters, '&');
146}
147
148// If |request| is a request to Google Web Search the function
149// enforces that the SafeSearch query parameters are set to active.
150// Sets the query part of |new_url| with the new value of the parameters.
151void ForceGoogleSafeSearch(net::URLRequest* request,
152 GURL* new_url) {
[email protected]c02f79d2013-06-27 21:46:47153 if (!google_util::IsGoogleSearchUrl(request->url()) &&
154 !google_util::IsGoogleHomePageUrl(request->url()))
[email protected]d1208ba32012-11-08 11:10:33155 return;
156
157 std::string query = request->url().query();
158 std::string new_query = AddSafeSearchParameters(query);
159 if (query == new_query)
160 return;
161
162 GURL::Replacements replacements;
163 replacements.SetQueryStr(new_query);
164 *new_url = request->url().ReplaceComponents(replacements);
165}
166
167// Gets called when the extensions finish work on the URL. If the extensions
168// did not do a redirect (so |new_url| is empty) then we enforce the
169// SafeSearch parameters. Otherwise we will get called again after the
170// redirect and we enforce SafeSearch then.
171void ForceGoogleSafeSearchCallbackWrapper(
172 const net::CompletionCallback& callback,
173 net::URLRequest* request,
174 GURL* new_url,
175 int rv) {
176 if (rv == net::OK && new_url->is_empty())
177 ForceGoogleSafeSearch(request, new_url);
178 callback.Run(rv);
179}
180
[email protected]6baff0b52012-03-06 01:30:18181enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
182
[email protected]98b6d942013-11-10 00:34:07183// Notifies the extensions::ProcessManager that a request has started or stopped
[email protected]9dfed872013-12-30 23:08:56184// for a particular RenderFrame.
[email protected]6baff0b52012-03-06 01:30:18185void NotifyEPMRequestStatus(RequestStatus status,
186 void* profile_id,
187 int process_id,
[email protected]9dfed872013-12-30 23:08:56188 int render_frame_id) {
[email protected]6baff0b52012-03-06 01:30:18189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
190 Profile* profile = reinterpret_cast<Profile*>(profile_id);
191 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
192 return;
193
[email protected]98b6d942013-11-10 00:34:07194 extensions::ProcessManager* process_manager =
[email protected]be93bba02012-10-24 16:44:03195 extensions::ExtensionSystem::Get(profile)->process_manager();
[email protected]e5775a52012-03-17 04:59:57196 // This may be NULL in unit tests.
[email protected]98b6d942013-11-10 00:34:07197 if (!process_manager)
[email protected]e5775a52012-03-17 04:59:57198 return;
199
[email protected]6baff0b52012-03-06 01:30:18200 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
201 // system-level request).
[email protected]9dfed872013-12-30 23:08:56202 content::RenderFrameHost* render_frame_host =
203 content::RenderFrameHost::FromID(process_id, render_frame_id);
204 if (render_frame_host) {
[email protected]6baff0b52012-03-06 01:30:18205 if (status == REQUEST_STARTED) {
[email protected]9dfed872013-12-30 23:08:56206 process_manager->OnNetworkRequestStarted(render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18207 } else if (status == REQUEST_DONE) {
[email protected]9dfed872013-12-30 23:08:56208 process_manager->OnNetworkRequestDone(render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18209 } else {
210 NOTREACHED();
211 }
212 }
213}
214
215void ForwardRequestStatus(
216 RequestStatus status, net::URLRequest* request, void* profile_id) {
[email protected]ea114722012-03-12 01:11:25217 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
218 if (!info)
219 return;
220
[email protected]9dfed872013-12-30 23:08:56221 int process_id, render_frame_id;
222 if (info->GetAssociatedRenderFrame(&process_id, &render_frame_id)) {
[email protected]6baff0b52012-03-06 01:30:18223 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
224 base::Bind(&NotifyEPMRequestStatus,
[email protected]9dfed872013-12-30 23:08:56225 status, profile_id, process_id, render_frame_id));
[email protected]6baff0b52012-03-06 01:30:18226 }
227}
228
[email protected]828eab22013-12-10 22:42:38229void UpdateContentLengthPrefs(
230 int received_content_length,
231 int original_content_length,
[email protected]a0a06932014-04-14 21:23:42232 data_reduction_proxy::DataReductionProxyRequestType request_type,
[email protected]b7796462013-12-12 19:42:34233 Profile* profile) {
[email protected]7a299a92012-10-24 23:54:50234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
235 DCHECK_GE(received_content_length, 0);
236 DCHECK_GE(original_content_length, 0);
237
[email protected]a09159a2012-11-29 12:51:48238 // Can be NULL in a unit test.
239 if (!g_browser_process)
240 return;
241
[email protected]7a299a92012-10-24 23:54:50242 PrefService* prefs = g_browser_process->local_state();
[email protected]a09159a2012-11-29 12:51:48243 if (!prefs)
244 return;
[email protected]7a299a92012-10-24 23:54:50245
[email protected]b7796462013-12-12 19:42:34246 // Ignore off-the-record data.
247 if (!g_browser_process->profile_manager()->IsValidProfile(profile) ||
248 profile->IsOffTheRecord()) {
249 return;
250 }
[email protected]0d36d1e2013-09-24 04:25:28251#if defined(OS_ANDROID)
[email protected]b1d5ed52013-12-18 02:34:56252 // If Android ever goes multi profile, the profile should be passed so that
253 // the browser preference will be taken.
[email protected]0d36d1e2013-09-24 04:25:28254 bool with_data_reduction_proxy_enabled =
[email protected]b1d5ed52013-12-18 02:34:56255 ProfileManager::GetActiveUserProfile()->GetPrefs()->GetBoolean(
[email protected]a0a06932014-04-14 21:23:42256 data_reduction_proxy::prefs::kDataReductionProxyEnabled);
[email protected]0d36d1e2013-09-24 04:25:28257#else
258 bool with_data_reduction_proxy_enabled = false;
[email protected]eac11e12013-03-19 22:04:32259#endif
[email protected]0d36d1e2013-09-24 04:25:28260
[email protected]a0a06932014-04-14 21:23:42261 data_reduction_proxy::UpdateContentLengthPrefs(received_content_length,
262 original_content_length,
263 with_data_reduction_proxy_enabled,
264 request_type, prefs);
[email protected]7a299a92012-10-24 23:54:50265}
266
[email protected]828eab22013-12-10 22:42:38267void StoreAccumulatedContentLength(
268 int received_content_length,
269 int original_content_length,
[email protected]a0a06932014-04-14 21:23:42270 data_reduction_proxy::DataReductionProxyRequestType request_type,
[email protected]b7796462013-12-12 19:42:34271 Profile* profile) {
[email protected]7a299a92012-10-24 23:54:50272 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
273 base::Bind(&UpdateContentLengthPrefs,
[email protected]2db93e42013-08-07 17:40:41274 received_content_length, original_content_length,
[email protected]a0a06932014-04-14 21:23:42275 request_type, profile));
[email protected]7a299a92012-10-24 23:54:50276}
277
278void RecordContentLengthHistograms(
[email protected]f81a5ee2013-06-20 21:08:49279 int64 received_content_length,
280 int64 original_content_length,
281 const base::TimeDelta& freshness_lifetime) {
[email protected]7a299a92012-10-24 23:54:50282 // Add the current resource to these histograms only when a valid
283 // X-Original-Content-Length header is present.
284 if (original_content_length >= 0) {
285 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL",
286 received_content_length);
287 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL",
288 original_content_length);
289 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL",
290 original_content_length - received_content_length);
291 } else {
292 // Presume the original content length is the same as the received content
293 // length if the X-Original-Content-Header is not present.
294 original_content_length = received_content_length;
295 }
296 UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length);
297 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength",
298 original_content_length);
299 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference",
300 original_content_length - received_content_length);
[email protected]f81a5ee2013-06-20 21:08:49301 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpContentFreshnessLifetime",
302 freshness_lifetime.InSeconds(),
303 base::TimeDelta::FromHours(1).InSeconds(),
304 base::TimeDelta::FromDays(30).InSeconds(),
305 100);
306 if (freshness_lifetime.InSeconds() <= 0)
307 return;
308 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable",
309 received_content_length);
310 if (freshness_lifetime.InHours() < 4)
311 return;
312 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable4Hours",
313 received_content_length);
314
315 if (freshness_lifetime.InHours() < 24)
316 return;
317 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable24Hours",
318 received_content_length);
[email protected]7a299a92012-10-24 23:54:50319}
320
[email protected]35558812013-12-18 21:58:36321#if defined(OS_ANDROID)
322void RecordPrecacheStatsOnUIThread(const GURL& url,
323 const base::Time& fetch_time, int64 size,
324 bool was_cached, void* profile_id) {
325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
326
327 Profile* profile = reinterpret_cast<Profile*>(profile_id);
328 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) {
329 return;
330 }
331
332 precache::PrecacheManager* precache_manager =
333 precache::PrecacheManagerFactory::GetForBrowserContext(profile);
334 if (!precache_manager) {
335 // This could be NULL if the profile is off the record.
336 return;
337 }
338
339 precache_manager->RecordStatsForFetch(url, fetch_time, size, was_cached);
340}
[email protected]d827e112014-03-31 17:45:05341
342void RecordIOThreadToRequestStartOnUIThread(
343 const base::TimeTicks& request_start) {
344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
345 base::TimeDelta request_lag = request_start -
346 g_browser_process->io_thread()->creation_time();
347 UMA_HISTOGRAM_TIMES("Net.IOThreadCreationToHTTPRequestStart", request_lag);
348}
[email protected]35558812013-12-18 21:58:36349#endif // defined(OS_ANDROID)
350
[email protected]d05ef99c2011-02-01 21:38:16351} // namespace
[email protected]ac039522010-06-15 16:39:44352
[email protected]0651b812011-02-24 00:22:50353ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10354 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48355 BooleanPrefMember* enable_referrers)
[email protected]3ce02412011-03-01 12:01:15356 : event_router_(event_router),
[email protected]a09159a2012-11-29 12:51:48357 profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59358 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48359 enable_do_not_track_(NULL),
360 force_google_safe_search_(NULL),
361 url_blacklist_manager_(NULL),
[email protected]45de676a2014-03-18 23:52:02362 domain_reliability_monitor_(NULL),
[email protected]7a299a92012-10-24 23:54:50363 received_content_length_(0),
[email protected]d827e112014-03-31 17:45:05364 original_content_length_(0),
[email protected]28c5d0b72014-05-13 08:19:59365 first_request_(true),
[email protected]d8fc4722014-06-13 13:17:15366 prerender_tracker_(NULL),
367 data_reduction_proxy_params_(NULL) {
[email protected]4b50cb52011-03-10 00:29:37368 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40369 DCHECK(enable_referrers);
[email protected]0651b812011-02-24 00:22:50370}
371
[email protected]ac039522010-06-15 16:39:44372ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
373
[email protected]a09159a2012-11-29 12:51:48374void ChromeNetworkDelegate::set_extension_info_map(
[email protected]38427a12013-11-09 17:34:20375 extensions::InfoMap* extension_info_map) {
[email protected]a09159a2012-11-29 12:51:48376 extension_info_map_ = extension_info_map;
377}
378
379void ChromeNetworkDelegate::set_cookie_settings(
380 CookieSettings* cookie_settings) {
381 cookie_settings_ = cookie_settings;
382}
383
[email protected]f53b4802012-12-20 17:04:23384void ChromeNetworkDelegate::set_predictor(
385 chrome_browser_net::Predictor* predictor) {
386 connect_interceptor_.reset(
387 new chrome_browser_net::ConnectInterceptor(predictor));
388}
389
[email protected]3620bbc2013-10-05 05:07:23390void ChromeNetworkDelegate::SetEnableClientHints() {
391 client_hints_.reset(new ClientHints());
392 client_hints_->Init();
393}
394
[email protected]c4a7df82012-08-09 22:48:46395// static
[email protected]a1d4ab072012-06-07 13:21:15396void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46397 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15398}
399
[email protected]0a8db0d2011-04-13 15:15:40400// static
[email protected]9d8cfb682012-09-13 16:48:04401void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40402 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04403 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33404 BooleanPrefMember* force_google_safe_search,
[email protected]0a8db0d2011-04-13 15:15:40405 PrefService* pref_service) {
406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]96a5c342012-12-04 18:14:02407 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43408 enable_referrers->MoveToThread(
409 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04410 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02411 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43412 enable_do_not_track->MoveToThread(
413 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04414 }
[email protected]d1208ba32012-11-08 11:10:33415 if (force_google_safe_search) {
[email protected]96a5c342012-12-04 18:14:02416 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33417 force_google_safe_search->MoveToThread(
418 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
419 }
[email protected]0a8db0d2011-04-13 15:15:40420}
421
[email protected]4c219e22012-05-05 19:41:04422// static
423void ChromeNetworkDelegate::AllowAccessToAllFiles() {
424 g_allow_file_access_ = true;
425}
426
[email protected]7a299a92012-10-24 23:54:50427// static
[email protected]cb1078de2013-12-23 20:04:22428base::Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() {
[email protected]7a299a92012-10-24 23:54:50429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
430 PrefService* prefs = g_browser_process->local_state();
[email protected]a0a06932014-04-14 21:23:42431 int64 total_received = prefs->GetInt64(
432 data_reduction_proxy::prefs::kHttpReceivedContentLength);
433 int64 total_original = prefs->GetInt64(
434 data_reduction_proxy::prefs::kHttpOriginalContentLength);
[email protected]7a299a92012-10-24 23:54:50435
[email protected]cb1078de2013-12-23 20:04:22436 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42437 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
438 dict->SetString("historic_received_content_length",
439 base::Int64ToString(total_received));
440 dict->SetString("historic_original_content_length",
441 base::Int64ToString(total_original));
[email protected]7a299a92012-10-24 23:54:50442 return dict;
443}
444
[email protected]cb1078de2013-12-23 20:04:22445base::Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
446 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42447 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
448 dict->SetString("session_received_content_length",
449 base::Int64ToString(received_content_length_));
450 dict->SetString("session_original_content_length",
451 base::Int64ToString(original_content_length_));
[email protected]7a299a92012-10-24 23:54:50452 return dict;
453}
454
[email protected]4875ba12011-03-30 22:31:51455int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12456 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47457 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12458 GURL* new_url) {
[email protected]d827e112014-03-31 17:45:05459#if defined(OS_ANDROID)
460 // This UMA tracks the time to the first user-initiated request start, so
461 // only non-null profiles are considered.
462 if (first_request_ && profile_) {
463 bool record_timing = true;
464#if defined(DATA_REDUCTION_PROXY_PROBE_URL)
465 record_timing = (request->url() != GURL(DATA_REDUCTION_PROXY_PROBE_URL));
466#endif
467 if (record_timing) {
468 first_request_ = false;
469 net::LoadTimingInfo timing_info;
470 request->GetLoadTimingInfo(&timing_info);
471 BrowserThread::PostTask(
472 BrowserThread::UI, FROM_HERE,
473 base::Bind(&RecordIOThreadToRequestStartOnUIThread,
474 timing_info.request_start));
475 }
476 }
477#endif // defined(OS_ANDROID)
478
[email protected]3e598ff12011-09-06 11:22:34479#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59480 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
481 // blocked. However, an extension might redirect the request to another URL,
482 // which is not blocked.
[email protected]cb85f8e52014-04-08 11:44:40483 int error = net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]6a5f77c32011-09-04 19:19:59484 if (url_blacklist_manager_ &&
[email protected]cb85f8e52014-04-08 11:44:40485 url_blacklist_manager_->IsRequestBlocked(*request, &error)) {
[email protected]6a5f77c32011-09-04 19:19:59486 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59487 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26488 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
489 net::NetLog::StringCallback("url",
490 &request->url().possibly_invalid_spec()));
[email protected]cb85f8e52014-04-08 11:44:40491 return error;
[email protected]6a5f77c32011-09-04 19:19:59492 }
[email protected]3e598ff12011-09-06 11:22:34493#endif
[email protected]6a5f77c32011-09-04 19:19:59494
[email protected]6baff0b52012-03-06 01:30:18495 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
496
[email protected]0a8db0d2011-04-13 15:15:40497 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13498 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04499 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
500 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33501
[email protected]3620bbc2013-10-05 05:07:23502 if (client_hints_) {
503 request->SetExtraRequestHeaderByName(
504 ClientHints::kDevicePixelRatioHeader,
505 client_hints_->GetDevicePixelRatioHeader(), true);
506 }
507
[email protected]d1208ba32012-11-08 11:10:33508 bool force_safe_search = force_google_safe_search_ &&
509 force_google_safe_search_->GetValue();
510
511 net::CompletionCallback wrapped_callback = callback;
512 if (force_safe_search) {
513 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
514 callback,
515 base::Unretained(request),
516 base::Unretained(new_url));
517 }
518
519 int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
520 profile_, extension_info_map_.get(), request, wrapped_callback,
521 new_url);
522
523 if (force_safe_search && rv == net::OK && new_url->is_empty())
524 ForceGoogleSafeSearch(request, new_url);
525
[email protected]f53b4802012-12-20 17:04:23526 if (connect_interceptor_)
527 connect_interceptor_->WitnessURLRequest(request);
528
[email protected]d1208ba32012-11-08 11:10:33529 return rv;
[email protected]d05ef99c2011-02-01 21:38:16530}
531
[email protected]4875ba12011-03-30 22:31:51532int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01533 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47534 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12535 net::HttpRequestHeaders* headers) {
[email protected]186468f2013-10-24 02:44:11536 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest");
[email protected]4875ba12011-03-30 22:31:51537 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18538 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44539}
[email protected]8202d0c2011-02-23 08:31:14540
[email protected]5796dc942011-07-14 19:26:10541void ChromeNetworkDelegate::OnSendHeaders(
542 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15543 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10544 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
545 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16546}
547
[email protected]ea8141e2011-10-05 13:12:51548int ChromeNetworkDelegate::OnHeadersReceived(
549 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47550 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32551 const net::HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16552 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
553 GURL* allowed_unsafe_redirect_url) {
[email protected]d8fc4722014-06-13 13:17:15554
555 if (data_reduction_proxy::MaybeBypassProxyAndPrepareToRetry(
556 data_reduction_proxy_params_,
557 request,
558 original_response_headers,
559 override_response_headers)) {
560 return net::OK;
561 }
562
[email protected]ea8141e2011-10-05 13:12:51563 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
[email protected]5f714132014-03-26 10:41:16564 profile_,
565 extension_info_map_.get(),
566 request,
567 callback,
568 original_response_headers,
569 override_response_headers,
570 allowed_unsafe_redirect_url);
[email protected]ea8141e2011-10-05 13:12:51571}
572
[email protected]31b2e5f2011-04-20 16:58:32573void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
574 const GURL& new_location) {
[email protected]45de676a2014-03-18 23:52:02575 if (domain_reliability_monitor_)
576 domain_reliability_monitor_->OnBeforeRedirect(request);
[email protected]31b2e5f2011-04-20 16:58:32577 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18578 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32579}
580
581
[email protected]8202d0c2011-02-23 08:31:14582void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]186468f2013-10-24 02:44:11583 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "ResponseStarted");
[email protected]62fecae2011-04-21 11:08:24584 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18585 profile_, extension_info_map_.get(), request);
586 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14587}
588
[email protected]8523ba52011-05-22 19:00:58589void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
590 int bytes_read) {
[email protected]186468f2013-10-24 02:44:11591 TRACE_EVENT_ASYNC_STEP_PAST1("net", "URLRequest", &request, "DidRead",
592 "bytes_read", bytes_read);
[email protected]6f4b4b42012-08-27 21:59:07593 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
594 request, bytes_read);
595
[email protected]44879ed2012-04-06 01:11:02596#if defined(ENABLE_TASK_MANAGER)
[email protected]17df9e7c2014-02-13 03:49:29597 // This is not completely accurate, but as a first approximation ignore
598 // requests that are served from the cache. See bug 330931 for more info.
599 if (!request.was_cached())
600 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02601#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58602}
603
[email protected]9045b8822012-01-13 20:35:35604void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
605 bool started) {
[email protected]4aca5db2013-08-17 00:51:56606 TRACE_EVENT_ASYNC_END0("net", "URLRequest", request);
[email protected]2756a8e2012-09-07 18:24:29607 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
[email protected]7a299a92012-10-24 23:54:50608 // For better accuracy, we use the actual bytes read instead of the length
609 // specified with the Content-Length header, which may be inaccurate,
610 // or missing, as is the case with chunked encoding.
611 int64 received_content_length = request->received_response_content_length();
612
[email protected]35558812013-12-18 21:58:36613#if defined(OS_ANDROID)
614 if (precache::PrecacheManager::IsPrecachingEnabled()) {
615 // Record precache metrics when a fetch is completed successfully, if
616 // precaching is enabled.
617 BrowserThread::PostTask(
618 BrowserThread::UI, FROM_HERE,
619 base::Bind(&RecordPrecacheStatsOnUIThread, request->url(),
620 base::Time::Now(), received_content_length,
621 request->was_cached(), profile_));
622 }
623#endif // defined(OS_ANDROID)
624
[email protected]7a299a92012-10-24 23:54:50625 // Only record for http or https urls.
626 bool is_http = request->url().SchemeIs("http");
627 bool is_https = request->url().SchemeIs("https");
628
629 if (!request->was_cached() && // Don't record cached content
630 received_content_length && // Zero-byte responses aren't useful.
631 (is_http || is_https)) { // Only record for HTTP or HTTPS urls.
632 int64 original_content_length =
633 request->response_info().headers->GetInt64HeaderValue(
634 "x-original-content-length");
[email protected]a0a06932014-04-14 21:23:42635 data_reduction_proxy::DataReductionProxyRequestType request_type =
636 data_reduction_proxy::GetDataReductionProxyRequestType(request);
[email protected]2db93e42013-08-07 17:40:41637
[email protected]f81a5ee2013-06-20 21:08:49638 base::TimeDelta freshness_lifetime =
639 request->response_info().headers->GetFreshnessLifetime(
640 request->response_info().response_time);
[email protected]828eab22013-12-10 22:42:38641 int64 adjusted_original_content_length =
[email protected]a0a06932014-04-14 21:23:42642 data_reduction_proxy::GetAdjustedOriginalContentLength(
643 request_type, original_content_length,
[email protected]828eab22013-12-10 22:42:38644 received_content_length);
[email protected]7a299a92012-10-24 23:54:50645 AccumulateContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41646 adjusted_original_content_length,
[email protected]a0a06932014-04-14 21:23:42647 request_type);
[email protected]7a299a92012-10-24 23:54:50648 RecordContentLengthHistograms(received_content_length,
[email protected]f81a5ee2013-06-20 21:08:49649 original_content_length,
650 freshness_lifetime);
[email protected]7a299a92012-10-24 23:54:50651 DVLOG(2) << __FUNCTION__
652 << " received content length: " << received_content_length
653 << " original content length: " << original_content_length
654 << " url: " << request->url();
655 }
656
[email protected]48944382011-04-23 13:28:16657 bool is_redirect = request->response_headers() &&
658 net::HttpResponseHeaders::IsRedirectResponseCode(
659 request->response_headers()->response_code());
660 if (!is_redirect) {
661 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18662 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16663 }
[email protected]a83dd332011-07-13 10:41:01664 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
665 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04666 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35667 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01668 } else {
669 NOTREACHED();
[email protected]48944382011-04-23 13:28:16670 }
[email protected]45de676a2014-03-18 23:52:02671 if (domain_reliability_monitor_)
672 domain_reliability_monitor_->OnCompleted(request, started);
[email protected]673514522011-07-13 18:17:18673 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18674
675 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14676}
[email protected]4b50cb52011-03-10 00:29:37677
[email protected]4875ba12011-03-30 22:31:51678void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
679 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18680 profile_, request);
[email protected]4875ba12011-03-30 22:31:51681}
682
[email protected]82a37672011-05-03 12:02:41683void ChromeNetworkDelegate::OnPACScriptError(int line_number,
[email protected]439f1e32013-12-09 20:09:09684 const base::string16& error) {
[email protected]c454fe672012-03-12 21:18:01685 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18686 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41687}
[email protected]7efc582d2011-08-03 20:46:35688
[email protected]c2911d72011-10-03 22:16:36689net::NetworkDelegate::AuthRequiredResponse
690ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35691 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36692 const net::AuthChallengeInfo& auth_info,
693 const AuthCallback& callback,
694 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45695 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
696 profile_, extension_info_map_.get(), request, auth_info,
697 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35698}
[email protected]9c8ae8c2012-03-09 13:13:35699
[email protected]4c219e22012-05-05 19:41:04700bool ChromeNetworkDelegate::OnCanGetCookies(
701 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35702 const net::CookieList& cookie_list) {
703 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02704 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35705 return true;
706
707 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04708 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35709
710 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16711 int render_frame_id = -1;
[email protected]f7fa5b62014-03-19 15:20:06712
713 // |is_for_blocking_resource| indicates whether the cookies read were for a
714 // blocking resource (eg script, css). It is only temporarily added for
715 // diagnostic purposes, per bug 353678. Will be removed again once data
716 // collection is finished.
717 bool is_for_blocking_resource = false;
718 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
719 if (info && ((!info->IsAsync()) ||
720 info->GetResourceType() == ResourceType::STYLESHEET ||
721 info->GetResourceType() == ResourceType::SCRIPT)) {
722 is_for_blocking_resource = true;
723 }
724
[email protected]f3add922013-12-20 23:17:16725 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
726 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35727 BrowserThread::PostTask(
728 BrowserThread::UI, FROM_HERE,
729 base::Bind(&TabSpecificContentSettings::CookiesRead,
[email protected]f3add922013-12-20 23:17:16730 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04731 request.url(), request.first_party_for_cookies(),
[email protected]f7fa5b62014-03-19 15:20:06732 cookie_list, !allow, is_for_blocking_resource));
[email protected]9c8ae8c2012-03-09 13:13:35733 }
734
735 return allow;
736}
737
[email protected]4c219e22012-05-05 19:41:04738bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
739 const std::string& cookie_line,
740 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35741 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02742 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35743 return true;
744
745 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04746 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35747
[email protected]9c8ae8c2012-03-09 13:13:35748 int render_process_id = -1;
[email protected]f3add922013-12-20 23:17:16749 int render_frame_id = -1;
750 if (content::ResourceRequestInfo::GetRenderFrameForRequest(
751 &request, &render_process_id, &render_frame_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35752 BrowserThread::PostTask(
753 BrowserThread::UI, FROM_HERE,
754 base::Bind(&TabSpecificContentSettings::CookieChanged,
[email protected]f3add922013-12-20 23:17:16755 render_process_id, render_frame_id,
[email protected]4c219e22012-05-05 19:41:04756 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43757 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35758 }
759
[email protected]28c5d0b72014-05-13 08:19:59760 if (prerender_tracker_) {
761 prerender_tracker_->OnCookieChangedForURL(
762 render_process_id,
763 request.context()->cookie_store()->GetCookieMonster(),
764 request.url());
765 }
766
[email protected]9c8ae8c2012-03-09 13:13:35767 return allow;
768}
[email protected]4c219e22012-05-05 19:41:04769
770bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45771 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04772 if (g_allow_file_access_)
773 return true;
774
[email protected]d8e4f132012-09-06 04:28:05775#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
776 return true;
777#else
[email protected]4c219e22012-05-05 19:41:04778#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05779 // If we're running Chrome for ChromeOS on Linux, we want to allow file
780 // access.
[email protected]49c4cf852013-09-27 19:28:24781 if (!base::SysInfo::IsRunningOnChromeOS() ||
[email protected]288538482012-09-06 21:09:35782 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05783 return true;
[email protected]288538482012-09-06 21:09:35784 }
[email protected]d8e4f132012-09-06 04:28:05785
786 // Use a whitelist to only allow access to files residing in the list of
787 // directories below.
[email protected]4c219e22012-05-05 19:41:04788 static const char* const kLocalAccessWhiteList[] = {
789 "/home/chronos/user/Downloads",
790 "/home/chronos/user/log",
[email protected]4791af43d2014-05-05 13:19:42791 "/home/chronos/user/WebRTC Logs",
[email protected]4c219e22012-05-05 19:41:04792 "/media",
793 "/opt/oem",
794 "/usr/share/chromeos-assets",
795 "/tmp",
796 "/var/log",
797 };
[email protected]ae015e12013-11-04 19:11:33798
[email protected]9e733282014-06-18 16:56:55799 // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under
800 // the profile path ("/home/chronos/user' is a hard link to current primary
801 // logged in profile.) For the support of multi-profile sessions, we are
802 // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such
803 // access.
[email protected]ae015e12013-11-04 19:11:33804 if (!profile_path_.empty()) {
805 const base::FilePath downloads = profile_path_.AppendASCII("Downloads");
806 if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path))
807 return true;
[email protected]9e733282014-06-18 16:56:55808 const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs");
809 if (webrtc_logs == path.StripTrailingSeparators() ||
810 webrtc_logs.IsParent(path)) {
811 return true;
812 }
[email protected]ae015e12013-11-04 19:11:33813 }
[email protected]d8e4f132012-09-06 04:28:05814#elif defined(OS_ANDROID)
815 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45816 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05817 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
818 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04819 return true;
820
[email protected]d8e4f132012-09-06 04:28:05821 // Whitelist of other allowed directories.
822 static const char* const kLocalAccessWhiteList[] = {
823 "/sdcard",
824 "/mnt/sdcard",
825 };
826#endif
827
[email protected]4c219e22012-05-05 19:41:04828 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45829 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
830 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04831 if (white_listed_path == path.StripTrailingSeparators() ||
832 white_listed_path.IsParent(path)) {
833 return true;
834 }
835 }
[email protected]d8e4f132012-09-06 04:28:05836
[email protected]288538482012-09-06 21:09:35837 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04838 return false;
[email protected]d8e4f132012-09-06 04:28:05839#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04840}
[email protected]a1d4ab072012-06-07 13:21:15841
842bool ChromeNetworkDelegate::OnCanThrottleRequest(
843 const net::URLRequest& request) const {
[email protected]c4a7df82012-08-09 22:48:46844 if (g_never_throttle_requests_) {
[email protected]a1d4ab072012-06-07 13:21:15845 return false;
846 }
847
[email protected]c4a7df82012-08-09 22:48:46848 return request.first_party_for_cookies().scheme() ==
[email protected]885c0e92012-11-13 20:27:42849 extensions::kExtensionScheme;
[email protected]a1d4ab072012-06-07 13:21:15850}
[email protected]aa28181e2012-06-13 00:53:58851
[email protected]e6d017652013-05-17 18:01:40852bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
853 const GURL& url,
854 const GURL& first_party_for_cookies) const {
855 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02856 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40857 return false;
858
859 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
860 url, first_party_for_cookies);
861 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
862 url, first_party_for_cookies);
863 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
864 return privacy_mode;
865}
866
[email protected]aa28181e2012-06-13 00:53:58867int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
868 net::SocketStream* socket,
869 const net::CompletionCallback& callback) {
870#if defined(ENABLE_CONFIGURATION_POLICY)
871 if (url_blacklist_manager_ &&
872 url_blacklist_manager_->IsURLBlocked(socket->url())) {
873 // URL access blocked by policy.
[email protected]aa28181e2012-06-13 00:53:58874 socket->net_log()->AddEvent(
[email protected]2fa08912012-06-14 20:56:26875 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
876 net::NetLog::StringCallback("url",
877 &socket->url().possibly_invalid_spec()));
[email protected]91468a92013-03-06 17:21:55878 return net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]aa28181e2012-06-13 00:53:58879 }
880#endif
881 return net::OK;
882}
[email protected]5a07c192012-07-30 20:18:22883
[email protected]7a299a92012-10-24 23:54:50884void ChromeNetworkDelegate::AccumulateContentLength(
[email protected]b7796462013-12-12 19:42:34885 int64 received_content_length,
886 int64 original_content_length,
[email protected]a0a06932014-04-14 21:23:42887 data_reduction_proxy::DataReductionProxyRequestType request_type) {
[email protected]7a299a92012-10-24 23:54:50888 DCHECK_GE(received_content_length, 0);
889 DCHECK_GE(original_content_length, 0);
890 StoreAccumulatedContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41891 original_content_length,
[email protected]a0a06932014-04-14 21:23:42892 request_type,
[email protected]b7796462013-12-12 19:42:34893 reinterpret_cast<Profile*>(profile_));
[email protected]7a299a92012-10-24 23:54:50894 received_content_length_ += received_content_length;
895 original_content_length_ += original_content_length;
896}