blob: e02dca60f1ef848d035d1a6d7053f86ba7388893 [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]6baff0b52012-03-06 01:30:1820#include "chrome/browser/browser_process.h"
[email protected]9c8ae8c2012-03-09 13:13:3521#include "chrome/browser/content_settings/cookie_settings.h"
22#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5823#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]aa84a7e2012-03-15 21:29:0624#include "chrome/browser/extensions/api/proxy/proxy_api.h"
[email protected]2b33dcd02012-03-18 01:34:1625#include "chrome/browser/extensions/api/web_request/web_request_api.h"
[email protected]5a38dfd2012-07-23 23:22:1026#include "chrome/browser/extensions/event_router_forwarder.h"
[email protected]c357acb42011-06-09 20:52:4227#include "chrome/browser/extensions/extension_info_map.h"
[email protected]6baff0b52012-03-06 01:30:1828#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]be93bba02012-10-24 16:44:0329#include "chrome/browser/extensions/extension_system.h"
[email protected]d1208ba32012-11-08 11:10:3330#include "chrome/browser/google/google_util.h"
[email protected]f53b4802012-12-20 17:04:2331#include "chrome/browser/net/connect_interceptor.h"
[email protected]a9e0d1412012-08-20 22:13:0132#include "chrome/browser/net/load_time_stats.h"
[email protected]6f4b4b42012-08-27 21:59:0733#include "chrome/browser/performance_monitor/performance_monitor.h"
[email protected]eac11e12013-03-19 22:04:3234#include "chrome/browser/prefs/scoped_user_pref_update.h"
[email protected]6baff0b52012-03-06 01:30:1835#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5836#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4037#include "chrome/common/pref_names.h"
[email protected]a1d4ab072012-06-07 13:21:1538#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4939#include "content/public/browser/browser_thread.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]885c0e92012-11-13 20:27:4242#include "extensions/common/constants.h"
[email protected]82b42302011-04-20 16:28:1643#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1444#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5945#include "net/base/net_log.h"
[email protected]5b9bc352012-07-18 13:13:3446#include "net/cookies/canonical_cookie.h"
47#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4448#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1649#include "net/http/http_response_headers.h"
[email protected]aa28181e2012-06-13 00:53:5850#include "net/socket_stream/socket_stream.h"
[email protected]d05ef99c2011-02-01 21:38:1651#include "net/url_request/url_request.h"
52
[email protected]4c219e22012-05-05 19:41:0453#if defined(OS_CHROMEOS)
54#include "base/chromeos/chromeos_version.h"
[email protected]288538482012-09-06 21:09:3555#include "base/command_line.h"
56#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0457#endif
58
[email protected]3e598ff12011-09-06 11:22:3459#if defined(ENABLE_CONFIGURATION_POLICY)
60#include "chrome/browser/policy/url_blacklist_manager.h"
61#endif
62
[email protected]631bb742011-11-02 11:29:3963using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1164using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2565using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3966
[email protected]d8e4f132012-09-06 04:28:0567// By default we don't allow access to all file:// urls on ChromeOS and
68// Android.
69#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0470bool ChromeNetworkDelegate::g_allow_file_access_ = false;
71#else
72bool ChromeNetworkDelegate::g_allow_file_access_ = true;
73#endif
74
[email protected]c4a7df82012-08-09 22:48:4675// This remains false unless the --disable-extensions-http-throttling
76// flag is passed to the browser.
77bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
78
[email protected]d05ef99c2011-02-01 21:38:1679namespace {
80
[email protected]9d8cfb682012-09-13 16:48:0481const char kDNTHeader[] = "DNT";
82
[email protected]8202d0c2011-02-23 08:31:1483// If the |request| failed due to problems with a proxy, forward the error to
84// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5085void ForwardProxyErrors(net::URLRequest* request,
[email protected]5a38dfd2012-07-23 23:22:1086 extensions::EventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1887 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1488 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0589 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1490 case net::ERR_PROXY_AUTH_UNSUPPORTED:
91 case net::ERR_PROXY_CONNECTION_FAILED:
92 case net::ERR_TUNNEL_CONNECTION_FAILED:
[email protected]c454fe672012-03-12 21:18:0193 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:0594 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:1495 }
96 }
97}
98
[email protected]d1208ba32012-11-08 11:10:3399// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
100// same key as the the |second_parameter| (e.g. foo=baz). Both parameters
101// must be in key=value form.
102bool HasSameParameterKey(const std::string& first_parameter,
103 const std::string& second_parameter) {
104 DCHECK(second_parameter.find("=") != std::string::npos);
105 // Prefix for "foo=bar" is "foo=".
106 std::string parameter_prefix = second_parameter.substr(
107 0, second_parameter.find("=") + 1);
108 return StartsWithASCII(first_parameter, parameter_prefix, false);
109}
110
111// Examines the query string containing parameters and adds the necessary ones
112// so that SafeSearch is active. |query| is the string to examine and the
113// return value is the |query| string modified such that SafeSearch is active.
114std::string AddSafeSearchParameters(const std::string& query) {
115 std::vector<std::string> new_parameters;
116 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
117 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
118
119 std::vector<std::string> parameters;
120 base::SplitString(query, '&', &parameters);
121
122 std::vector<std::string>::iterator it;
123 for (it = parameters.begin(); it < parameters.end(); ++it) {
124 if (!HasSameParameterKey(*it, safe_parameter) &&
125 !HasSameParameterKey(*it, ssui_parameter)) {
126 new_parameters.push_back(*it);
127 }
128 }
129
130 new_parameters.push_back(safe_parameter);
131 new_parameters.push_back(ssui_parameter);
132 return JoinString(new_parameters, '&');
133}
134
135// If |request| is a request to Google Web Search the function
136// enforces that the SafeSearch query parameters are set to active.
137// Sets the query part of |new_url| with the new value of the parameters.
138void ForceGoogleSafeSearch(net::URLRequest* request,
139 GURL* new_url) {
[email protected]c02f79d2013-06-27 21:46:47140 if (!google_util::IsGoogleSearchUrl(request->url()) &&
141 !google_util::IsGoogleHomePageUrl(request->url()))
[email protected]d1208ba32012-11-08 11:10:33142 return;
143
144 std::string query = request->url().query();
145 std::string new_query = AddSafeSearchParameters(query);
146 if (query == new_query)
147 return;
148
149 GURL::Replacements replacements;
150 replacements.SetQueryStr(new_query);
151 *new_url = request->url().ReplaceComponents(replacements);
152}
153
154// Gets called when the extensions finish work on the URL. If the extensions
155// did not do a redirect (so |new_url| is empty) then we enforce the
156// SafeSearch parameters. Otherwise we will get called again after the
157// redirect and we enforce SafeSearch then.
158void ForceGoogleSafeSearchCallbackWrapper(
159 const net::CompletionCallback& callback,
160 net::URLRequest* request,
161 GURL* new_url,
162 int rv) {
163 if (rv == net::OK && new_url->is_empty())
164 ForceGoogleSafeSearch(request, new_url);
165 callback.Run(rv);
166}
167
[email protected]6baff0b52012-03-06 01:30:18168enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
169
170// Notifies the ExtensionProcessManager that a request has started or stopped
171// for a particular RenderView.
172void NotifyEPMRequestStatus(RequestStatus status,
173 void* profile_id,
174 int process_id,
175 int render_view_id) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
177 Profile* profile = reinterpret_cast<Profile*>(profile_id);
178 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
179 return;
180
[email protected]e5775a52012-03-17 04:59:57181 ExtensionProcessManager* extension_process_manager =
[email protected]be93bba02012-10-24 16:44:03182 extensions::ExtensionSystem::Get(profile)->process_manager();
[email protected]e5775a52012-03-17 04:59:57183 // This may be NULL in unit tests.
184 if (!extension_process_manager)
185 return;
186
[email protected]6baff0b52012-03-06 01:30:18187 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
188 // system-level request).
[email protected]d3e898e2012-03-14 03:45:08189 RenderViewHost* render_view_host =
190 RenderViewHost::FromID(process_id, render_view_id);
[email protected]e5775a52012-03-17 04:59:57191 if (render_view_host) {
[email protected]6baff0b52012-03-06 01:30:18192 if (status == REQUEST_STARTED) {
[email protected]d3e898e2012-03-14 03:45:08193 extension_process_manager->OnNetworkRequestStarted(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18194 } else if (status == REQUEST_DONE) {
[email protected]d3e898e2012-03-14 03:45:08195 extension_process_manager->OnNetworkRequestDone(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18196 } else {
197 NOTREACHED();
198 }
199 }
200}
201
202void ForwardRequestStatus(
203 RequestStatus status, net::URLRequest* request, void* profile_id) {
[email protected]ea114722012-03-12 01:11:25204 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
205 if (!info)
206 return;
207
[email protected]6baff0b52012-03-06 01:30:18208 int process_id, render_view_id;
[email protected]ea114722012-03-12 01:11:25209 if (info->GetAssociatedRenderView(&process_id, &render_view_id)) {
[email protected]6baff0b52012-03-06 01:30:18210 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
211 base::Bind(&NotifyEPMRequestStatus,
212 status, profile_id, process_id, render_view_id));
213 }
214}
215
[email protected]2db93e42013-08-07 17:40:41216#if defined(OS_ANDROID) || defined(OS_IOS)
[email protected]eac11e12013-03-19 22:04:32217// Increments an int64, stored as a string, in a ListPref at the specified
218// index. The value must already exist and be a string representation of a
219// number.
220void AddInt64ToListPref(size_t index, int64 length,
[email protected]2db93e42013-08-07 17:40:41221 base::ListValue* list_update) {
[email protected]eac11e12013-03-19 22:04:32222 int64 value = 0;
223 std::string old_string_value;
224 bool rv = list_update->GetString(index, &old_string_value);
225 DCHECK(rv);
226 if (rv) {
227 rv = base::StringToInt64(old_string_value, &value);
228 DCHECK(rv);
229 }
230 value += length;
231 list_update->Set(index, Value::CreateStringValue(base::Int64ToString(value)));
232}
[email protected]2db93e42013-08-07 17:40:41233
234int64 ListPrefInt64Value(const base::ListValue& list_update, size_t index) {
235 std::string string_value;
236 if (!list_update.GetString(index, &string_value))
237 return 0;
238
239 int64 value = 0;
240 bool rv = base::StringToInt64(string_value, &value);
241 DCHECK(rv);
242 return value;
243}
244
[email protected]c447b172013-08-14 18:17:54245// Ensure list has exactly n elements.
246void MaintainContentLengthPrefsWindow(base::ListValue* list, size_t n) {
247 // Remove data for old days from the front.
248 while (list->GetSize() > n)
249 list->Remove(0, NULL);
250 // Newly added lists are empty. Add entries to back to fill the window,
251 // each initialized to zero.
252 while (list->GetSize() < n)
253 list->AppendString(base::Int64ToString(0));
254 DCHECK_EQ(n, list->GetSize());
255}
256
[email protected]2db93e42013-08-07 17:40:41257void RecordDailyContentLengthHistograms(
258 int64 original_length,
259 int64 received_length,
260 int64 length_with_data_reduction_enabled,
261 int64 length_via_data_reduction_proxy) {
262 // Record metrics in KB.
263 UMA_HISTOGRAM_COUNTS(
264 "Net.DailyHttpOriginalContentLength", original_length >> 10);
265 UMA_HISTOGRAM_COUNTS(
266 "Net.DailyHttpReceivedContentLength", received_length >> 10);
267 UMA_HISTOGRAM_COUNTS(
268 "Net.DailyHttpContentLengthWithDataReductionProxyEnabled",
269 length_with_data_reduction_enabled >> 10);
270 UMA_HISTOGRAM_COUNTS(
271 "Net.DailyHttpContentLengthViaDataReductionProxy",
272 length_via_data_reduction_proxy >> 10);
273
274 if (original_length > 0 && received_length > 0) {
275 int percent = (100 * (original_length - received_length)) / original_length;
276 // UMA percentage cannot be negative.
277 if (percent < 0)
278 percent = 0;
279 UMA_HISTOGRAM_PERCENTAGE("Net.DailyHttpContentSavings", percent);
280 // If the data reduction proxy is enabled for some responses.
281 if (length_with_data_reduction_enabled > 0) {
282 UMA_HISTOGRAM_PERCENTAGE(
283 "Net.DailyHttpContentSavings_DataReductionProxy", percent);
284 }
285 }
286 if (received_length > 0 && length_with_data_reduction_enabled >= 0) {
287 DCHECK_GE(received_length, length_with_data_reduction_enabled);
288 UMA_HISTOGRAM_PERCENTAGE(
289 "Net.DailyReceivedContentWithDataReductionProxyEnabled",
290 (100 * length_with_data_reduction_enabled) / received_length);
291 }
292 if (received_length > 0 &&
293 length_via_data_reduction_proxy >= 0) {
294 DCHECK_GE(received_length, length_via_data_reduction_proxy);
295 UMA_HISTOGRAM_PERCENTAGE(
296 "Net.DailyReceivedContentViaDataReductionProxy",
297 (100 * length_via_data_reduction_proxy) / received_length);
298 }
299}
300
301#endif // defined(OS_ANDROID) || defined(OS_IOS)
[email protected]eac11e12013-03-19 22:04:32302
[email protected]7a299a92012-10-24 23:54:50303void UpdateContentLengthPrefs(int received_content_length,
[email protected]2db93e42013-08-07 17:40:41304 int original_content_length,
305 bool data_reduction_proxy_was_used) {
[email protected]7a299a92012-10-24 23:54:50306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
307 DCHECK_GE(received_content_length, 0);
308 DCHECK_GE(original_content_length, 0);
309
[email protected]a09159a2012-11-29 12:51:48310 // Can be NULL in a unit test.
311 if (!g_browser_process)
312 return;
313
[email protected]7a299a92012-10-24 23:54:50314 PrefService* prefs = g_browser_process->local_state();
[email protected]a09159a2012-11-29 12:51:48315 if (!prefs)
316 return;
[email protected]7a299a92012-10-24 23:54:50317
318 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
319 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
320 total_received += received_content_length;
321 total_original += original_content_length;
322 prefs->SetInt64(prefs::kHttpReceivedContentLength, total_received);
323 prefs->SetInt64(prefs::kHttpOriginalContentLength, total_original);
[email protected]eac11e12013-03-19 22:04:32324
[email protected]2db93e42013-08-07 17:40:41325#if defined(OS_ANDROID) || defined(OS_IOS)
[email protected]eac11e12013-03-19 22:04:32326 base::Time now = base::Time::Now().LocalMidnight();
327 const size_t kNumDaysInHistory = 60;
328
329 // Each day, we calculate the total number of bytes received and the total
330 // size of all corresponding resources before any data-reducing recompression
331 // is applied. These values are used to compute the data savings realized
332 // by applying our compression techniques. Totals for the last
333 // |kNumDaysInHistory| days are maintained.
334 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength);
335 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength);
[email protected]2db93e42013-08-07 17:40:41336 ListPrefUpdate data_reduction_enabled_update(
337 prefs,
338 prefs::kDailyHttpReceivedContentLengthWithDataReductionProxyEnabled);
339 ListPrefUpdate via_data_reduction_update(
340 prefs, prefs::kDailyHttpReceivedContentLengthViaDataReductionProxy);
[email protected]eac11e12013-03-19 22:04:32341
[email protected]c447b172013-08-14 18:17:54342 // New empty lists may have been created. Maintain the invariant that
343 // there should be exactly |kNumDaysInHistory| days in the histories.
344 MaintainContentLengthPrefsWindow(original_update.Get(), kNumDaysInHistory);
345 MaintainContentLengthPrefsWindow(received_update.Get(), kNumDaysInHistory);
346 MaintainContentLengthPrefsWindow(
347 data_reduction_enabled_update.Get(), kNumDaysInHistory);
348 MaintainContentLengthPrefsWindow(
349 via_data_reduction_update.Get(), kNumDaysInHistory);
350
[email protected]eac11e12013-03-19 22:04:32351 // Determine how many days it has been since the last update.
352 int64 then_internal = prefs->GetInt64(
353 prefs::kDailyHttpContentLengthLastUpdateDate);
354 base::Time then = base::Time::FromInternalValue(then_internal);
355 int days_since_last_update = (now - then).InDays();
356
357 if (days_since_last_update) {
358 // Record the last update time.
359 prefs->SetInt64(prefs::kDailyHttpContentLengthLastUpdateDate,
360 now.ToInternalValue());
361
362 if (days_since_last_update == -1) {
363 // The system may go backwards in time by up to a day for legitimate
364 // reasons, such as with changes to the time zone. In such cases the
365 // history is likely still valid. Shift backwards one day and retain all
366 // values.
367 original_update->Remove(kNumDaysInHistory - 1, NULL);
368 received_update->Remove(kNumDaysInHistory - 1, NULL);
[email protected]2db93e42013-08-07 17:40:41369 data_reduction_enabled_update->Remove(kNumDaysInHistory - 1, NULL);
370 via_data_reduction_update->Remove(kNumDaysInHistory - 1, NULL);
[email protected]eac11e12013-03-19 22:04:32371 original_update->Insert(0, new StringValue(base::Int64ToString(0)));
372 received_update->Insert(0, new StringValue(base::Int64ToString(0)));
[email protected]2db93e42013-08-07 17:40:41373 data_reduction_enabled_update->Insert(
374 0, new StringValue(base::Int64ToString(0)));
375 via_data_reduction_update->Insert(
376 0, new StringValue(base::Int64ToString(0)));
[email protected]eac11e12013-03-19 22:04:32377 days_since_last_update = 0;
378
379 } else if (days_since_last_update < -1) {
380 // Erase all entries if the system went backwards in time by more than
381 // a day.
382 original_update->Clear();
[email protected]2db93e42013-08-07 17:40:41383 received_update->Clear();
384 data_reduction_enabled_update->Clear();
385 via_data_reduction_update->Clear();
[email protected]eac11e12013-03-19 22:04:32386 days_since_last_update = kNumDaysInHistory;
387 }
388
[email protected]2db93e42013-08-07 17:40:41389 // A new day. Report the previous day's data if exists. We'll lose usage
390 // data if Chrome isn't run for a day. Here, we prefer collecting less data
391 // but the collected data are associated with an accurate date.
392 if (days_since_last_update == 1) {
393 RecordDailyContentLengthHistograms(
394 ListPrefInt64Value(*original_update, original_update->GetSize() - 1),
395 ListPrefInt64Value(*received_update, received_update->GetSize() - 1),
396 ListPrefInt64Value(*data_reduction_enabled_update,
397 data_reduction_enabled_update->GetSize() - 1),
398 ListPrefInt64Value(*via_data_reduction_update,
399 via_data_reduction_update->GetSize() - 1));
400 }
[email protected]c447b172013-08-14 18:17:54401 // Add entries for days since last update event. This will make the
402 // lists longer than kNumDaysInHistory. The additional items will be cut off
403 // from the head of the lists by MaintainContentLengthPrefsWindow, below.
[email protected]eac11e12013-03-19 22:04:32404 for (int i = 0;
405 i < days_since_last_update && i < static_cast<int>(kNumDaysInHistory);
406 ++i) {
407 original_update->AppendString(base::Int64ToString(0));
408 received_update->AppendString(base::Int64ToString(0));
[email protected]2db93e42013-08-07 17:40:41409 data_reduction_enabled_update->AppendString(base::Int64ToString(0));
410 via_data_reduction_update->AppendString(base::Int64ToString(0));
[email protected]eac11e12013-03-19 22:04:32411 }
412
[email protected]c447b172013-08-14 18:17:54413 // Entries for new days may have been appended. Maintain the invariant that
414 // there should be exactly |kNumDaysInHistory| days in the histories.
415 MaintainContentLengthPrefsWindow(original_update.Get(), kNumDaysInHistory);
416 MaintainContentLengthPrefsWindow(received_update.Get(), kNumDaysInHistory);
417 MaintainContentLengthPrefsWindow(
418 data_reduction_enabled_update.Get(), kNumDaysInHistory);
419 MaintainContentLengthPrefsWindow(
420 via_data_reduction_update.Get(), kNumDaysInHistory);
[email protected]eac11e12013-03-19 22:04:32421 }
[email protected]2db93e42013-08-07 17:40:41422
[email protected]eac11e12013-03-19 22:04:32423 // Update the counts for the current day.
424 AddInt64ToListPref(kNumDaysInHistory - 1,
[email protected]2db93e42013-08-07 17:40:41425 original_content_length, original_update.Get());
[email protected]eac11e12013-03-19 22:04:32426 AddInt64ToListPref(kNumDaysInHistory - 1,
[email protected]2db93e42013-08-07 17:40:41427 received_content_length, received_update.Get());
428
429 if (g_browser_process->profile_manager()->GetDefaultProfile()->
430 GetPrefs()->GetBoolean(prefs::kSpdyProxyAuthEnabled)) {
431 AddInt64ToListPref(kNumDaysInHistory - 1,
432 received_content_length,
433 data_reduction_enabled_update.Get());
434 }
435 if (data_reduction_proxy_was_used) {
436 AddInt64ToListPref(kNumDaysInHistory - 1,
437 received_content_length,
438 via_data_reduction_update.Get());
439 }
[email protected]eac11e12013-03-19 22:04:32440#endif
[email protected]7a299a92012-10-24 23:54:50441}
442
443void StoreAccumulatedContentLength(int received_content_length,
[email protected]2db93e42013-08-07 17:40:41444 int original_content_length,
445 bool data_reduction_proxy_was_used) {
[email protected]7a299a92012-10-24 23:54:50446 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
447 base::Bind(&UpdateContentLengthPrefs,
[email protected]2db93e42013-08-07 17:40:41448 received_content_length, original_content_length,
449 data_reduction_proxy_was_used));
[email protected]7a299a92012-10-24 23:54:50450}
451
452void RecordContentLengthHistograms(
[email protected]f81a5ee2013-06-20 21:08:49453 int64 received_content_length,
454 int64 original_content_length,
455 const base::TimeDelta& freshness_lifetime) {
[email protected]2db93e42013-08-07 17:40:41456#if defined(OS_ANDROID) || defined(OS_IOS)
[email protected]7a299a92012-10-24 23:54:50457 // Add the current resource to these histograms only when a valid
458 // X-Original-Content-Length header is present.
459 if (original_content_length >= 0) {
460 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL",
461 received_content_length);
462 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL",
463 original_content_length);
464 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL",
465 original_content_length - received_content_length);
466 } else {
467 // Presume the original content length is the same as the received content
468 // length if the X-Original-Content-Header is not present.
469 original_content_length = received_content_length;
470 }
471 UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length);
472 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength",
473 original_content_length);
474 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference",
475 original_content_length - received_content_length);
[email protected]f81a5ee2013-06-20 21:08:49476 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpContentFreshnessLifetime",
477 freshness_lifetime.InSeconds(),
478 base::TimeDelta::FromHours(1).InSeconds(),
479 base::TimeDelta::FromDays(30).InSeconds(),
480 100);
481 if (freshness_lifetime.InSeconds() <= 0)
482 return;
483 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable",
484 received_content_length);
485 if (freshness_lifetime.InHours() < 4)
486 return;
487 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable4Hours",
488 received_content_length);
489
490 if (freshness_lifetime.InHours() < 24)
491 return;
492 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable24Hours",
493 received_content_length);
[email protected]7a299a92012-10-24 23:54:50494#endif
495}
496
[email protected]d05ef99c2011-02-01 21:38:16497} // namespace
[email protected]ac039522010-06-15 16:39:44498
[email protected]0651b812011-02-24 00:22:50499ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10500 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48501 BooleanPrefMember* enable_referrers)
[email protected]3ce02412011-03-01 12:01:15502 : event_router_(event_router),
[email protected]a09159a2012-11-29 12:51:48503 profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59504 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48505 enable_do_not_track_(NULL),
506 force_google_safe_search_(NULL),
507 url_blacklist_manager_(NULL),
508 load_time_stats_(NULL),
[email protected]7a299a92012-10-24 23:54:50509 received_content_length_(0),
510 original_content_length_(0) {
[email protected]4b50cb52011-03-10 00:29:37511 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40512 DCHECK(enable_referrers);
[email protected]0651b812011-02-24 00:22:50513}
514
[email protected]ac039522010-06-15 16:39:44515ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
516
[email protected]a09159a2012-11-29 12:51:48517void ChromeNetworkDelegate::set_extension_info_map(
518 ExtensionInfoMap* extension_info_map) {
519 extension_info_map_ = extension_info_map;
520}
521
522void ChromeNetworkDelegate::set_cookie_settings(
523 CookieSettings* cookie_settings) {
524 cookie_settings_ = cookie_settings;
525}
526
[email protected]f53b4802012-12-20 17:04:23527void ChromeNetworkDelegate::set_predictor(
528 chrome_browser_net::Predictor* predictor) {
529 connect_interceptor_.reset(
530 new chrome_browser_net::ConnectInterceptor(predictor));
531}
532
[email protected]c4a7df82012-08-09 22:48:46533// static
[email protected]a1d4ab072012-06-07 13:21:15534void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46535 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15536}
537
[email protected]0a8db0d2011-04-13 15:15:40538// static
[email protected]9d8cfb682012-09-13 16:48:04539void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40540 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04541 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33542 BooleanPrefMember* force_google_safe_search,
[email protected]0a8db0d2011-04-13 15:15:40543 PrefService* pref_service) {
544 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]96a5c342012-12-04 18:14:02545 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43546 enable_referrers->MoveToThread(
547 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04548 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02549 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43550 enable_do_not_track->MoveToThread(
551 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04552 }
[email protected]d1208ba32012-11-08 11:10:33553 if (force_google_safe_search) {
[email protected]96a5c342012-12-04 18:14:02554 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33555 force_google_safe_search->MoveToThread(
556 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
557 }
[email protected]0a8db0d2011-04-13 15:15:40558}
559
[email protected]4c219e22012-05-05 19:41:04560// static
561void ChromeNetworkDelegate::AllowAccessToAllFiles() {
562 g_allow_file_access_ = true;
563}
564
[email protected]7a299a92012-10-24 23:54:50565// static
566Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() {
567 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
568 PrefService* prefs = g_browser_process->local_state();
569 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
570 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
571
572 DictionaryValue* dict = new DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42573 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
574 dict->SetString("historic_received_content_length",
575 base::Int64ToString(total_received));
576 dict->SetString("historic_original_content_length",
577 base::Int64ToString(total_original));
[email protected]7a299a92012-10-24 23:54:50578 return dict;
579}
580
581Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
582 DictionaryValue* dict = new DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42583 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
584 dict->SetString("session_received_content_length",
585 base::Int64ToString(received_content_length_));
586 dict->SetString("session_original_content_length",
587 base::Int64ToString(original_content_length_));
[email protected]7a299a92012-10-24 23:54:50588 return dict;
589}
590
[email protected]4875ba12011-03-30 22:31:51591int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12592 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47593 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12594 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:34595#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59596 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
597 // blocked. However, an extension might redirect the request to another URL,
598 // which is not blocked.
599 if (url_blacklist_manager_ &&
[email protected]74575252013-03-12 19:58:21600 url_blacklist_manager_->IsRequestBlocked(*request)) {
[email protected]6a5f77c32011-09-04 19:19:59601 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59602 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26603 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
604 net::NetLog::StringCallback("url",
605 &request->url().possibly_invalid_spec()));
[email protected]91468a92013-03-06 17:21:55606 return net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]6a5f77c32011-09-04 19:19:59607 }
[email protected]3e598ff12011-09-06 11:22:34608#endif
[email protected]6a5f77c32011-09-04 19:19:59609
[email protected]6baff0b52012-03-06 01:30:18610 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
611
[email protected]0a8db0d2011-04-13 15:15:40612 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13613 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04614 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
615 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33616
617 bool force_safe_search = force_google_safe_search_ &&
618 force_google_safe_search_->GetValue();
619
620 net::CompletionCallback wrapped_callback = callback;
621 if (force_safe_search) {
622 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
623 callback,
624 base::Unretained(request),
625 base::Unretained(new_url));
626 }
627
628 int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
629 profile_, extension_info_map_.get(), request, wrapped_callback,
630 new_url);
631
632 if (force_safe_search && rv == net::OK && new_url->is_empty())
633 ForceGoogleSafeSearch(request, new_url);
634
[email protected]f53b4802012-12-20 17:04:23635 if (connect_interceptor_)
636 connect_interceptor_->WitnessURLRequest(request);
637
[email protected]d1208ba32012-11-08 11:10:33638 return rv;
[email protected]d05ef99c2011-02-01 21:38:16639}
640
[email protected]4875ba12011-03-30 22:31:51641int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01642 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47643 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12644 net::HttpRequestHeaders* headers) {
[email protected]4aca5db2013-08-17 00:51:56645 TRACE_EVENT_ASYNC_STEP0("net", "URLRequest", request, "SendRequest");
[email protected]4875ba12011-03-30 22:31:51646 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18647 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44648}
[email protected]8202d0c2011-02-23 08:31:14649
[email protected]5796dc942011-07-14 19:26:10650void ChromeNetworkDelegate::OnSendHeaders(
651 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15652 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10653 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
654 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16655}
656
[email protected]ea8141e2011-10-05 13:12:51657int ChromeNetworkDelegate::OnHeadersReceived(
658 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47659 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32660 const net::HttpResponseHeaders* original_response_headers,
[email protected]ea8141e2011-10-05 13:12:51661 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
662 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
663 profile_, extension_info_map_.get(), request, callback,
664 original_response_headers, override_response_headers);
665}
666
[email protected]31b2e5f2011-04-20 16:58:32667void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
668 const GURL& new_location) {
669 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18670 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32671}
672
673
[email protected]8202d0c2011-02-23 08:31:14674void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]4aca5db2013-08-17 00:51:56675 TRACE_EVENT_ASYNC_STEP0("net", "URLRequest", request, "ResponseStarted");
[email protected]62fecae2011-04-21 11:08:24676 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18677 profile_, extension_info_map_.get(), request);
678 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14679}
680
[email protected]8523ba52011-05-22 19:00:58681void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
682 int bytes_read) {
[email protected]4aca5db2013-08-17 00:51:56683 TRACE_EVENT_ASYNC_STEP1("net", "URLRequest", &request, "DidRead",
684 "bytes_read", bytes_read);
[email protected]6f4b4b42012-08-27 21:59:07685 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
686 request, bytes_read);
687
[email protected]44879ed2012-04-06 01:11:02688#if defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58689 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02690#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58691}
692
[email protected]9045b8822012-01-13 20:35:35693void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
694 bool started) {
[email protected]4aca5db2013-08-17 00:51:56695 TRACE_EVENT_ASYNC_END0("net", "URLRequest", request);
[email protected]2756a8e2012-09-07 18:24:29696 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
[email protected]7a299a92012-10-24 23:54:50697 // For better accuracy, we use the actual bytes read instead of the length
698 // specified with the Content-Length header, which may be inaccurate,
699 // or missing, as is the case with chunked encoding.
700 int64 received_content_length = request->received_response_content_length();
701
702 // Only record for http or https urls.
703 bool is_http = request->url().SchemeIs("http");
704 bool is_https = request->url().SchemeIs("https");
705
706 if (!request->was_cached() && // Don't record cached content
707 received_content_length && // Zero-byte responses aren't useful.
708 (is_http || is_https)) { // Only record for HTTP or HTTPS urls.
709 int64 original_content_length =
710 request->response_info().headers->GetInt64HeaderValue(
711 "x-original-content-length");
[email protected]2db93e42013-08-07 17:40:41712 bool data_reduction_proxy_was_used =
713 request->response_info().headers->HasHeaderValue(
714 "via", "1.1 Chrome Compression Proxy");
715
[email protected]7a299a92012-10-24 23:54:50716 // Since there was no indication of the original content length, presume
717 // it is no different from the number of bytes read.
718 int64 adjusted_original_content_length = original_content_length;
719 if (adjusted_original_content_length == -1)
720 adjusted_original_content_length = received_content_length;
[email protected]f81a5ee2013-06-20 21:08:49721 base::TimeDelta freshness_lifetime =
722 request->response_info().headers->GetFreshnessLifetime(
723 request->response_info().response_time);
[email protected]7a299a92012-10-24 23:54:50724 AccumulateContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41725 adjusted_original_content_length,
726 data_reduction_proxy_was_used);
[email protected]7a299a92012-10-24 23:54:50727 RecordContentLengthHistograms(received_content_length,
[email protected]f81a5ee2013-06-20 21:08:49728 original_content_length,
729 freshness_lifetime);
[email protected]7a299a92012-10-24 23:54:50730 DVLOG(2) << __FUNCTION__
731 << " received content length: " << received_content_length
732 << " original content length: " << original_content_length
733 << " url: " << request->url();
734 }
735
[email protected]48944382011-04-23 13:28:16736 bool is_redirect = request->response_headers() &&
737 net::HttpResponseHeaders::IsRedirectResponseCode(
738 request->response_headers()->response_code());
739 if (!is_redirect) {
740 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18741 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16742 }
[email protected]a83dd332011-07-13 10:41:01743 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
744 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04745 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35746 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01747 } else {
748 NOTREACHED();
[email protected]48944382011-04-23 13:28:16749 }
[email protected]673514522011-07-13 18:17:18750 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18751
752 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14753}
[email protected]4b50cb52011-03-10 00:29:37754
[email protected]4875ba12011-03-30 22:31:51755void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
756 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18757 profile_, request);
[email protected]16d5c72b2012-09-14 20:42:37758 if (load_time_stats_)
759 load_time_stats_->OnURLRequestDestroyed(*request);
[email protected]4875ba12011-03-30 22:31:51760}
761
[email protected]82a37672011-05-03 12:02:41762void ChromeNetworkDelegate::OnPACScriptError(int line_number,
763 const string16& error) {
[email protected]c454fe672012-03-12 21:18:01764 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18765 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41766}
[email protected]7efc582d2011-08-03 20:46:35767
[email protected]c2911d72011-10-03 22:16:36768net::NetworkDelegate::AuthRequiredResponse
769ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35770 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36771 const net::AuthChallengeInfo& auth_info,
772 const AuthCallback& callback,
773 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45774 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
775 profile_, extension_info_map_.get(), request, auth_info,
776 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35777}
[email protected]9c8ae8c2012-03-09 13:13:35778
[email protected]4c219e22012-05-05 19:41:04779bool ChromeNetworkDelegate::OnCanGetCookies(
780 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35781 const net::CookieList& cookie_list) {
782 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02783 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35784 return true;
785
786 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04787 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35788
789 int render_process_id = -1;
790 int render_view_id = -1;
791 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04792 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35793 BrowserThread::PostTask(
794 BrowserThread::UI, FROM_HERE,
795 base::Bind(&TabSpecificContentSettings::CookiesRead,
796 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04797 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43798 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35799 }
800
801 return allow;
802}
803
[email protected]4c219e22012-05-05 19:41:04804bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
805 const std::string& cookie_line,
806 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35807 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02808 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35809 return true;
810
811 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04812 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35813
[email protected]9c8ae8c2012-03-09 13:13:35814 int render_process_id = -1;
815 int render_view_id = -1;
816 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04817 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35818 BrowserThread::PostTask(
819 BrowserThread::UI, FROM_HERE,
820 base::Bind(&TabSpecificContentSettings::CookieChanged,
821 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04822 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43823 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35824 }
825
826 return allow;
827}
[email protected]4c219e22012-05-05 19:41:04828
829bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45830 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04831 if (g_allow_file_access_)
832 return true;
833
[email protected]d8e4f132012-09-06 04:28:05834#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
835 return true;
836#else
[email protected]4c219e22012-05-05 19:41:04837#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05838 // If we're running Chrome for ChromeOS on Linux, we want to allow file
839 // access.
[email protected]288538482012-09-06 21:09:35840 if (!base::chromeos::IsRunningOnChromeOS() ||
841 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05842 return true;
[email protected]288538482012-09-06 21:09:35843 }
[email protected]d8e4f132012-09-06 04:28:05844
845 // Use a whitelist to only allow access to files residing in the list of
846 // directories below.
[email protected]4c219e22012-05-05 19:41:04847 static const char* const kLocalAccessWhiteList[] = {
848 "/home/chronos/user/Downloads",
849 "/home/chronos/user/log",
850 "/media",
851 "/opt/oem",
852 "/usr/share/chromeos-assets",
853 "/tmp",
854 "/var/log",
855 };
[email protected]d8e4f132012-09-06 04:28:05856#elif defined(OS_ANDROID)
857 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45858 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05859 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
860 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04861 return true;
862
[email protected]d8e4f132012-09-06 04:28:05863 // Whitelist of other allowed directories.
864 static const char* const kLocalAccessWhiteList[] = {
865 "/sdcard",
866 "/mnt/sdcard",
867 };
868#endif
869
[email protected]4c219e22012-05-05 19:41:04870 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45871 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
872 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04873 if (white_listed_path == path.StripTrailingSeparators() ||
874 white_listed_path.IsParent(path)) {
875 return true;
876 }
877 }
[email protected]d8e4f132012-09-06 04:28:05878
[email protected]288538482012-09-06 21:09:35879 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04880 return false;
[email protected]d8e4f132012-09-06 04:28:05881#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04882}
[email protected]a1d4ab072012-06-07 13:21:15883
884bool ChromeNetworkDelegate::OnCanThrottleRequest(
885 const net::URLRequest& request) const {
[email protected]c4a7df82012-08-09 22:48:46886 if (g_never_throttle_requests_) {
[email protected]a1d4ab072012-06-07 13:21:15887 return false;
888 }
889
[email protected]c4a7df82012-08-09 22:48:46890 return request.first_party_for_cookies().scheme() ==
[email protected]885c0e92012-11-13 20:27:42891 extensions::kExtensionScheme;
[email protected]a1d4ab072012-06-07 13:21:15892}
[email protected]aa28181e2012-06-13 00:53:58893
[email protected]e6d017652013-05-17 18:01:40894bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
895 const GURL& url,
896 const GURL& first_party_for_cookies) const {
897 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02898 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40899 return false;
900
901 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
902 url, first_party_for_cookies);
903 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
904 url, first_party_for_cookies);
905 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
906 return privacy_mode;
907}
908
[email protected]aa28181e2012-06-13 00:53:58909int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
910 net::SocketStream* socket,
911 const net::CompletionCallback& callback) {
912#if defined(ENABLE_CONFIGURATION_POLICY)
913 if (url_blacklist_manager_ &&
914 url_blacklist_manager_->IsURLBlocked(socket->url())) {
915 // URL access blocked by policy.
[email protected]aa28181e2012-06-13 00:53:58916 socket->net_log()->AddEvent(
[email protected]2fa08912012-06-14 20:56:26917 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
918 net::NetLog::StringCallback("url",
919 &socket->url().possibly_invalid_spec()));
[email protected]91468a92013-03-06 17:21:55920 return net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]aa28181e2012-06-13 00:53:58921 }
922#endif
923 return net::OK;
924}
[email protected]5a07c192012-07-30 20:18:22925
[email protected]a9e0d1412012-08-20 22:13:01926void ChromeNetworkDelegate::OnRequestWaitStateChange(
[email protected]5a07c192012-07-30 20:18:22927 const net::URLRequest& request,
[email protected]a9e0d1412012-08-20 22:13:01928 RequestWaitState state) {
929 if (load_time_stats_)
930 load_time_stats_->OnRequestWaitStateChange(request, state);
[email protected]5a07c192012-07-30 20:18:22931}
[email protected]7a299a92012-10-24 23:54:50932
933void ChromeNetworkDelegate::AccumulateContentLength(
[email protected]2db93e42013-08-07 17:40:41934 int64 received_content_length, int64 original_content_length,
935 bool data_reduction_proxy_was_used) {
[email protected]7a299a92012-10-24 23:54:50936 DCHECK_GE(received_content_length, 0);
937 DCHECK_GE(original_content_length, 0);
938 StoreAccumulatedContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41939 original_content_length,
940 data_reduction_proxy_was_used);
[email protected]7a299a92012-10-24 23:54:50941 received_content_length_ += received_content_length;
942 original_content_length_ += original_content_length;
943}