blob: b605d9f1b2839a1778f3c8d41e1fc75ba8c79a45 [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]7a299a92012-10-24 23:54:5012#include "base/logging.h"
13#include "base/metrics/histogram.h"
[email protected]d8e4f132012-09-06 04:28:0514#include "base/path_service.h"
[email protected]1ab137b2013-03-21 03:33:1815#include "base/prefs/pref_member.h"
[email protected]3853a4c2013-02-11 17:15:5716#include "base/prefs/pref_service.h"
[email protected]3ea1b182013-02-08 22:38:4117#include "base/strings/string_number_conversions.h"
[email protected]1988e1c2013-02-28 20:27:4218#include "base/strings/string_split.h"
[email protected]6baff0b52012-03-06 01:30:1819#include "chrome/browser/browser_process.h"
[email protected]9c8ae8c2012-03-09 13:13:3520#include "chrome/browser/content_settings/cookie_settings.h"
21#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5822#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]aa84a7e2012-03-15 21:29:0623#include "chrome/browser/extensions/api/proxy/proxy_api.h"
[email protected]2b33dcd02012-03-18 01:34:1624#include "chrome/browser/extensions/api/web_request/web_request_api.h"
[email protected]5a38dfd2012-07-23 23:22:1025#include "chrome/browser/extensions/event_router_forwarder.h"
[email protected]c357acb42011-06-09 20:52:4226#include "chrome/browser/extensions/extension_info_map.h"
[email protected]6baff0b52012-03-06 01:30:1827#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]be93bba02012-10-24 16:44:0328#include "chrome/browser/extensions/extension_system.h"
[email protected]d1208ba32012-11-08 11:10:3329#include "chrome/browser/google/google_util.h"
[email protected]f53b4802012-12-20 17:04:2330#include "chrome/browser/net/connect_interceptor.h"
[email protected]a9e0d1412012-08-20 22:13:0131#include "chrome/browser/net/load_time_stats.h"
[email protected]6f4b4b42012-08-27 21:59:0732#include "chrome/browser/performance_monitor/performance_monitor.h"
[email protected]eac11e12013-03-19 22:04:3233#include "chrome/browser/prefs/scoped_user_pref_update.h"
[email protected]6baff0b52012-03-06 01:30:1834#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5835#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4036#include "chrome/common/pref_names.h"
[email protected]a1d4ab072012-06-07 13:21:1537#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4938#include "content/public/browser/browser_thread.h"
[email protected]9c1662b2012-03-06 15:44:3339#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3540#include "content/public/browser/resource_request_info.h"
[email protected]885c0e92012-11-13 20:27:4241#include "extensions/common/constants.h"
[email protected]82b42302011-04-20 16:28:1642#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1443#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5944#include "net/base/net_log.h"
[email protected]5b9bc352012-07-18 13:13:3445#include "net/cookies/canonical_cookie.h"
46#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4447#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1648#include "net/http/http_response_headers.h"
[email protected]aa28181e2012-06-13 00:53:5849#include "net/socket_stream/socket_stream.h"
[email protected]d05ef99c2011-02-01 21:38:1650#include "net/url_request/url_request.h"
51
[email protected]4c219e22012-05-05 19:41:0452#if defined(OS_CHROMEOS)
53#include "base/chromeos/chromeos_version.h"
[email protected]288538482012-09-06 21:09:3554#include "base/command_line.h"
55#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0456#endif
57
[email protected]3e598ff12011-09-06 11:22:3458#if defined(ENABLE_CONFIGURATION_POLICY)
59#include "chrome/browser/policy/url_blacklist_manager.h"
60#endif
61
[email protected]631bb742011-11-02 11:29:3962using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1163using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2564using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3965
[email protected]d8e4f132012-09-06 04:28:0566// By default we don't allow access to all file:// urls on ChromeOS and
67// Android.
68#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0469bool ChromeNetworkDelegate::g_allow_file_access_ = false;
70#else
71bool ChromeNetworkDelegate::g_allow_file_access_ = true;
72#endif
73
[email protected]c4a7df82012-08-09 22:48:4674// This remains false unless the --disable-extensions-http-throttling
75// flag is passed to the browser.
76bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
77
[email protected]d05ef99c2011-02-01 21:38:1678namespace {
79
[email protected]9d8cfb682012-09-13 16:48:0480const char kDNTHeader[] = "DNT";
81
[email protected]8202d0c2011-02-23 08:31:1482// If the |request| failed due to problems with a proxy, forward the error to
83// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5084void ForwardProxyErrors(net::URLRequest* request,
[email protected]5a38dfd2012-07-23 23:22:1085 extensions::EventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1886 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1487 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0588 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1489 case net::ERR_PROXY_AUTH_UNSUPPORTED:
90 case net::ERR_PROXY_CONNECTION_FAILED:
91 case net::ERR_TUNNEL_CONNECTION_FAILED:
[email protected]c454fe672012-03-12 21:18:0192 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:0593 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:1494 }
95 }
96}
97
[email protected]d1208ba32012-11-08 11:10:3398// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
99// same key as the the |second_parameter| (e.g. foo=baz). Both parameters
100// must be in key=value form.
101bool HasSameParameterKey(const std::string& first_parameter,
102 const std::string& second_parameter) {
103 DCHECK(second_parameter.find("=") != std::string::npos);
104 // Prefix for "foo=bar" is "foo=".
105 std::string parameter_prefix = second_parameter.substr(
106 0, second_parameter.find("=") + 1);
107 return StartsWithASCII(first_parameter, parameter_prefix, false);
108}
109
110// Examines the query string containing parameters and adds the necessary ones
111// so that SafeSearch is active. |query| is the string to examine and the
112// return value is the |query| string modified such that SafeSearch is active.
113std::string AddSafeSearchParameters(const std::string& query) {
114 std::vector<std::string> new_parameters;
115 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
116 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
117
118 std::vector<std::string> parameters;
119 base::SplitString(query, '&', &parameters);
120
121 std::vector<std::string>::iterator it;
122 for (it = parameters.begin(); it < parameters.end(); ++it) {
123 if (!HasSameParameterKey(*it, safe_parameter) &&
124 !HasSameParameterKey(*it, ssui_parameter)) {
125 new_parameters.push_back(*it);
126 }
127 }
128
129 new_parameters.push_back(safe_parameter);
130 new_parameters.push_back(ssui_parameter);
131 return JoinString(new_parameters, '&');
132}
133
134// If |request| is a request to Google Web Search the function
135// enforces that the SafeSearch query parameters are set to active.
136// Sets the query part of |new_url| with the new value of the parameters.
137void ForceGoogleSafeSearch(net::URLRequest* request,
138 GURL* new_url) {
[email protected]c02f79d2013-06-27 21:46:47139 if (!google_util::IsGoogleSearchUrl(request->url()) &&
140 !google_util::IsGoogleHomePageUrl(request->url()))
[email protected]d1208ba32012-11-08 11:10:33141 return;
142
143 std::string query = request->url().query();
144 std::string new_query = AddSafeSearchParameters(query);
145 if (query == new_query)
146 return;
147
148 GURL::Replacements replacements;
149 replacements.SetQueryStr(new_query);
150 *new_url = request->url().ReplaceComponents(replacements);
151}
152
153// Gets called when the extensions finish work on the URL. If the extensions
154// did not do a redirect (so |new_url| is empty) then we enforce the
155// SafeSearch parameters. Otherwise we will get called again after the
156// redirect and we enforce SafeSearch then.
157void ForceGoogleSafeSearchCallbackWrapper(
158 const net::CompletionCallback& callback,
159 net::URLRequest* request,
160 GURL* new_url,
161 int rv) {
162 if (rv == net::OK && new_url->is_empty())
163 ForceGoogleSafeSearch(request, new_url);
164 callback.Run(rv);
165}
166
[email protected]6baff0b52012-03-06 01:30:18167enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
168
169// Notifies the ExtensionProcessManager that a request has started or stopped
170// for a particular RenderView.
171void NotifyEPMRequestStatus(RequestStatus status,
172 void* profile_id,
173 int process_id,
174 int render_view_id) {
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
176 Profile* profile = reinterpret_cast<Profile*>(profile_id);
177 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
178 return;
179
[email protected]e5775a52012-03-17 04:59:57180 ExtensionProcessManager* extension_process_manager =
[email protected]be93bba02012-10-24 16:44:03181 extensions::ExtensionSystem::Get(profile)->process_manager();
[email protected]e5775a52012-03-17 04:59:57182 // This may be NULL in unit tests.
183 if (!extension_process_manager)
184 return;
185
[email protected]6baff0b52012-03-06 01:30:18186 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
187 // system-level request).
[email protected]d3e898e2012-03-14 03:45:08188 RenderViewHost* render_view_host =
189 RenderViewHost::FromID(process_id, render_view_id);
[email protected]e5775a52012-03-17 04:59:57190 if (render_view_host) {
[email protected]6baff0b52012-03-06 01:30:18191 if (status == REQUEST_STARTED) {
[email protected]d3e898e2012-03-14 03:45:08192 extension_process_manager->OnNetworkRequestStarted(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18193 } else if (status == REQUEST_DONE) {
[email protected]d3e898e2012-03-14 03:45:08194 extension_process_manager->OnNetworkRequestDone(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18195 } else {
196 NOTREACHED();
197 }
198 }
199}
200
201void ForwardRequestStatus(
202 RequestStatus status, net::URLRequest* request, void* profile_id) {
[email protected]ea114722012-03-12 01:11:25203 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
204 if (!info)
205 return;
206
[email protected]6baff0b52012-03-06 01:30:18207 int process_id, render_view_id;
[email protected]ea114722012-03-12 01:11:25208 if (info->GetAssociatedRenderView(&process_id, &render_view_id)) {
[email protected]6baff0b52012-03-06 01:30:18209 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
210 base::Bind(&NotifyEPMRequestStatus,
211 status, profile_id, process_id, render_view_id));
212 }
213}
214
[email protected]2db93e42013-08-07 17:40:41215#if defined(OS_ANDROID) || defined(OS_IOS)
[email protected]eac11e12013-03-19 22:04:32216// Increments an int64, stored as a string, in a ListPref at the specified
217// index. The value must already exist and be a string representation of a
218// number.
219void AddInt64ToListPref(size_t index, int64 length,
[email protected]2db93e42013-08-07 17:40:41220 base::ListValue* list_update) {
[email protected]eac11e12013-03-19 22:04:32221 int64 value = 0;
222 std::string old_string_value;
223 bool rv = list_update->GetString(index, &old_string_value);
224 DCHECK(rv);
225 if (rv) {
226 rv = base::StringToInt64(old_string_value, &value);
227 DCHECK(rv);
228 }
229 value += length;
230 list_update->Set(index, Value::CreateStringValue(base::Int64ToString(value)));
231}
[email protected]2db93e42013-08-07 17:40:41232
233int64 ListPrefInt64Value(const base::ListValue& list_update, size_t index) {
234 std::string string_value;
235 if (!list_update.GetString(index, &string_value))
236 return 0;
237
238 int64 value = 0;
239 bool rv = base::StringToInt64(string_value, &value);
240 DCHECK(rv);
241 return value;
242}
243
[email protected]c447b172013-08-14 18:17:54244// Ensure list has exactly n elements.
245void MaintainContentLengthPrefsWindow(base::ListValue* list, size_t n) {
246 // Remove data for old days from the front.
247 while (list->GetSize() > n)
248 list->Remove(0, NULL);
249 // Newly added lists are empty. Add entries to back to fill the window,
250 // each initialized to zero.
251 while (list->GetSize() < n)
252 list->AppendString(base::Int64ToString(0));
253 DCHECK_EQ(n, list->GetSize());
254}
255
[email protected]2db93e42013-08-07 17:40:41256void RecordDailyContentLengthHistograms(
257 int64 original_length,
258 int64 received_length,
259 int64 length_with_data_reduction_enabled,
260 int64 length_via_data_reduction_proxy) {
261 // Record metrics in KB.
262 UMA_HISTOGRAM_COUNTS(
263 "Net.DailyHttpOriginalContentLength", original_length >> 10);
264 UMA_HISTOGRAM_COUNTS(
265 "Net.DailyHttpReceivedContentLength", received_length >> 10);
266 UMA_HISTOGRAM_COUNTS(
267 "Net.DailyHttpContentLengthWithDataReductionProxyEnabled",
268 length_with_data_reduction_enabled >> 10);
269 UMA_HISTOGRAM_COUNTS(
270 "Net.DailyHttpContentLengthViaDataReductionProxy",
271 length_via_data_reduction_proxy >> 10);
272
273 if (original_length > 0 && received_length > 0) {
274 int percent = (100 * (original_length - received_length)) / original_length;
275 // UMA percentage cannot be negative.
276 if (percent < 0)
277 percent = 0;
278 UMA_HISTOGRAM_PERCENTAGE("Net.DailyHttpContentSavings", percent);
279 // If the data reduction proxy is enabled for some responses.
280 if (length_with_data_reduction_enabled > 0) {
281 UMA_HISTOGRAM_PERCENTAGE(
282 "Net.DailyHttpContentSavings_DataReductionProxy", percent);
283 }
284 }
285 if (received_length > 0 && length_with_data_reduction_enabled >= 0) {
286 DCHECK_GE(received_length, length_with_data_reduction_enabled);
287 UMA_HISTOGRAM_PERCENTAGE(
288 "Net.DailyReceivedContentWithDataReductionProxyEnabled",
289 (100 * length_with_data_reduction_enabled) / received_length);
290 }
291 if (received_length > 0 &&
292 length_via_data_reduction_proxy >= 0) {
293 DCHECK_GE(received_length, length_via_data_reduction_proxy);
294 UMA_HISTOGRAM_PERCENTAGE(
295 "Net.DailyReceivedContentViaDataReductionProxy",
296 (100 * length_via_data_reduction_proxy) / received_length);
297 }
298}
299
300#endif // defined(OS_ANDROID) || defined(OS_IOS)
[email protected]eac11e12013-03-19 22:04:32301
[email protected]7a299a92012-10-24 23:54:50302void UpdateContentLengthPrefs(int received_content_length,
[email protected]2db93e42013-08-07 17:40:41303 int original_content_length,
304 bool data_reduction_proxy_was_used) {
[email protected]7a299a92012-10-24 23:54:50305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
306 DCHECK_GE(received_content_length, 0);
307 DCHECK_GE(original_content_length, 0);
308
[email protected]a09159a2012-11-29 12:51:48309 // Can be NULL in a unit test.
310 if (!g_browser_process)
311 return;
312
[email protected]7a299a92012-10-24 23:54:50313 PrefService* prefs = g_browser_process->local_state();
[email protected]a09159a2012-11-29 12:51:48314 if (!prefs)
315 return;
[email protected]7a299a92012-10-24 23:54:50316
317 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
318 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
319 total_received += received_content_length;
320 total_original += original_content_length;
321 prefs->SetInt64(prefs::kHttpReceivedContentLength, total_received);
322 prefs->SetInt64(prefs::kHttpOriginalContentLength, total_original);
[email protected]eac11e12013-03-19 22:04:32323
[email protected]2db93e42013-08-07 17:40:41324#if defined(OS_ANDROID) || defined(OS_IOS)
[email protected]eac11e12013-03-19 22:04:32325 base::Time now = base::Time::Now().LocalMidnight();
326 const size_t kNumDaysInHistory = 60;
327
328 // Each day, we calculate the total number of bytes received and the total
329 // size of all corresponding resources before any data-reducing recompression
330 // is applied. These values are used to compute the data savings realized
331 // by applying our compression techniques. Totals for the last
332 // |kNumDaysInHistory| days are maintained.
333 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength);
334 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength);
[email protected]2db93e42013-08-07 17:40:41335 ListPrefUpdate data_reduction_enabled_update(
336 prefs,
337 prefs::kDailyHttpReceivedContentLengthWithDataReductionProxyEnabled);
338 ListPrefUpdate via_data_reduction_update(
339 prefs, prefs::kDailyHttpReceivedContentLengthViaDataReductionProxy);
[email protected]eac11e12013-03-19 22:04:32340
[email protected]c447b172013-08-14 18:17:54341 // New empty lists may have been created. Maintain the invariant that
342 // there should be exactly |kNumDaysInHistory| days in the histories.
343 MaintainContentLengthPrefsWindow(original_update.Get(), kNumDaysInHistory);
344 MaintainContentLengthPrefsWindow(received_update.Get(), kNumDaysInHistory);
345 MaintainContentLengthPrefsWindow(
346 data_reduction_enabled_update.Get(), kNumDaysInHistory);
347 MaintainContentLengthPrefsWindow(
348 via_data_reduction_update.Get(), kNumDaysInHistory);
349
[email protected]eac11e12013-03-19 22:04:32350 // Determine how many days it has been since the last update.
351 int64 then_internal = prefs->GetInt64(
352 prefs::kDailyHttpContentLengthLastUpdateDate);
353 base::Time then = base::Time::FromInternalValue(then_internal);
354 int days_since_last_update = (now - then).InDays();
355
356 if (days_since_last_update) {
357 // Record the last update time.
358 prefs->SetInt64(prefs::kDailyHttpContentLengthLastUpdateDate,
359 now.ToInternalValue());
360
361 if (days_since_last_update == -1) {
362 // The system may go backwards in time by up to a day for legitimate
363 // reasons, such as with changes to the time zone. In such cases the
364 // history is likely still valid. Shift backwards one day and retain all
365 // values.
366 original_update->Remove(kNumDaysInHistory - 1, NULL);
367 received_update->Remove(kNumDaysInHistory - 1, NULL);
[email protected]2db93e42013-08-07 17:40:41368 data_reduction_enabled_update->Remove(kNumDaysInHistory - 1, NULL);
369 via_data_reduction_update->Remove(kNumDaysInHistory - 1, NULL);
[email protected]eac11e12013-03-19 22:04:32370 original_update->Insert(0, new StringValue(base::Int64ToString(0)));
371 received_update->Insert(0, new StringValue(base::Int64ToString(0)));
[email protected]2db93e42013-08-07 17:40:41372 data_reduction_enabled_update->Insert(
373 0, new StringValue(base::Int64ToString(0)));
374 via_data_reduction_update->Insert(
375 0, new StringValue(base::Int64ToString(0)));
[email protected]eac11e12013-03-19 22:04:32376 days_since_last_update = 0;
377
378 } else if (days_since_last_update < -1) {
379 // Erase all entries if the system went backwards in time by more than
380 // a day.
381 original_update->Clear();
[email protected]2db93e42013-08-07 17:40:41382 received_update->Clear();
383 data_reduction_enabled_update->Clear();
384 via_data_reduction_update->Clear();
[email protected]eac11e12013-03-19 22:04:32385 days_since_last_update = kNumDaysInHistory;
386 }
387
[email protected]2db93e42013-08-07 17:40:41388 // A new day. Report the previous day's data if exists. We'll lose usage
389 // data if Chrome isn't run for a day. Here, we prefer collecting less data
390 // but the collected data are associated with an accurate date.
391 if (days_since_last_update == 1) {
392 RecordDailyContentLengthHistograms(
393 ListPrefInt64Value(*original_update, original_update->GetSize() - 1),
394 ListPrefInt64Value(*received_update, received_update->GetSize() - 1),
395 ListPrefInt64Value(*data_reduction_enabled_update,
396 data_reduction_enabled_update->GetSize() - 1),
397 ListPrefInt64Value(*via_data_reduction_update,
398 via_data_reduction_update->GetSize() - 1));
399 }
[email protected]c447b172013-08-14 18:17:54400 // Add entries for days since last update event. This will make the
401 // lists longer than kNumDaysInHistory. The additional items will be cut off
402 // from the head of the lists by MaintainContentLengthPrefsWindow, below.
[email protected]eac11e12013-03-19 22:04:32403 for (int i = 0;
404 i < days_since_last_update && i < static_cast<int>(kNumDaysInHistory);
405 ++i) {
406 original_update->AppendString(base::Int64ToString(0));
407 received_update->AppendString(base::Int64ToString(0));
[email protected]2db93e42013-08-07 17:40:41408 data_reduction_enabled_update->AppendString(base::Int64ToString(0));
409 via_data_reduction_update->AppendString(base::Int64ToString(0));
[email protected]eac11e12013-03-19 22:04:32410 }
411
[email protected]c447b172013-08-14 18:17:54412 // Entries for new days may have been appended. Maintain the invariant that
413 // there should be exactly |kNumDaysInHistory| days in the histories.
414 MaintainContentLengthPrefsWindow(original_update.Get(), kNumDaysInHistory);
415 MaintainContentLengthPrefsWindow(received_update.Get(), kNumDaysInHistory);
416 MaintainContentLengthPrefsWindow(
417 data_reduction_enabled_update.Get(), kNumDaysInHistory);
418 MaintainContentLengthPrefsWindow(
419 via_data_reduction_update.Get(), kNumDaysInHistory);
[email protected]eac11e12013-03-19 22:04:32420 }
[email protected]2db93e42013-08-07 17:40:41421
[email protected]eac11e12013-03-19 22:04:32422 // Update the counts for the current day.
423 AddInt64ToListPref(kNumDaysInHistory - 1,
[email protected]2db93e42013-08-07 17:40:41424 original_content_length, original_update.Get());
[email protected]eac11e12013-03-19 22:04:32425 AddInt64ToListPref(kNumDaysInHistory - 1,
[email protected]2db93e42013-08-07 17:40:41426 received_content_length, received_update.Get());
427
428 if (g_browser_process->profile_manager()->GetDefaultProfile()->
429 GetPrefs()->GetBoolean(prefs::kSpdyProxyAuthEnabled)) {
430 AddInt64ToListPref(kNumDaysInHistory - 1,
431 received_content_length,
432 data_reduction_enabled_update.Get());
433 }
434 if (data_reduction_proxy_was_used) {
435 AddInt64ToListPref(kNumDaysInHistory - 1,
436 received_content_length,
437 via_data_reduction_update.Get());
438 }
[email protected]eac11e12013-03-19 22:04:32439#endif
[email protected]7a299a92012-10-24 23:54:50440}
441
442void StoreAccumulatedContentLength(int received_content_length,
[email protected]2db93e42013-08-07 17:40:41443 int original_content_length,
444 bool data_reduction_proxy_was_used) {
[email protected]7a299a92012-10-24 23:54:50445 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
446 base::Bind(&UpdateContentLengthPrefs,
[email protected]2db93e42013-08-07 17:40:41447 received_content_length, original_content_length,
448 data_reduction_proxy_was_used));
[email protected]7a299a92012-10-24 23:54:50449}
450
451void RecordContentLengthHistograms(
[email protected]f81a5ee2013-06-20 21:08:49452 int64 received_content_length,
453 int64 original_content_length,
454 const base::TimeDelta& freshness_lifetime) {
[email protected]2db93e42013-08-07 17:40:41455#if defined(OS_ANDROID) || defined(OS_IOS)
[email protected]7a299a92012-10-24 23:54:50456 // Add the current resource to these histograms only when a valid
457 // X-Original-Content-Length header is present.
458 if (original_content_length >= 0) {
459 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL",
460 received_content_length);
461 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL",
462 original_content_length);
463 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL",
464 original_content_length - received_content_length);
465 } else {
466 // Presume the original content length is the same as the received content
467 // length if the X-Original-Content-Header is not present.
468 original_content_length = received_content_length;
469 }
470 UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length);
471 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength",
472 original_content_length);
473 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference",
474 original_content_length - received_content_length);
[email protected]f81a5ee2013-06-20 21:08:49475 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.HttpContentFreshnessLifetime",
476 freshness_lifetime.InSeconds(),
477 base::TimeDelta::FromHours(1).InSeconds(),
478 base::TimeDelta::FromDays(30).InSeconds(),
479 100);
480 if (freshness_lifetime.InSeconds() <= 0)
481 return;
482 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable",
483 received_content_length);
484 if (freshness_lifetime.InHours() < 4)
485 return;
486 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable4Hours",
487 received_content_length);
488
489 if (freshness_lifetime.InHours() < 24)
490 return;
491 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthCacheable24Hours",
492 received_content_length);
[email protected]7a299a92012-10-24 23:54:50493#endif
494}
495
[email protected]d05ef99c2011-02-01 21:38:16496} // namespace
[email protected]ac039522010-06-15 16:39:44497
[email protected]0651b812011-02-24 00:22:50498ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10499 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48500 BooleanPrefMember* enable_referrers)
[email protected]3ce02412011-03-01 12:01:15501 : event_router_(event_router),
[email protected]a09159a2012-11-29 12:51:48502 profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59503 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48504 enable_do_not_track_(NULL),
505 force_google_safe_search_(NULL),
506 url_blacklist_manager_(NULL),
507 load_time_stats_(NULL),
[email protected]7a299a92012-10-24 23:54:50508 received_content_length_(0),
509 original_content_length_(0) {
[email protected]4b50cb52011-03-10 00:29:37510 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40511 DCHECK(enable_referrers);
[email protected]0651b812011-02-24 00:22:50512}
513
[email protected]ac039522010-06-15 16:39:44514ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
515
[email protected]a09159a2012-11-29 12:51:48516void ChromeNetworkDelegate::set_extension_info_map(
517 ExtensionInfoMap* extension_info_map) {
518 extension_info_map_ = extension_info_map;
519}
520
521void ChromeNetworkDelegate::set_cookie_settings(
522 CookieSettings* cookie_settings) {
523 cookie_settings_ = cookie_settings;
524}
525
[email protected]f53b4802012-12-20 17:04:23526void ChromeNetworkDelegate::set_predictor(
527 chrome_browser_net::Predictor* predictor) {
528 connect_interceptor_.reset(
529 new chrome_browser_net::ConnectInterceptor(predictor));
530}
531
[email protected]c4a7df82012-08-09 22:48:46532// static
[email protected]a1d4ab072012-06-07 13:21:15533void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46534 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15535}
536
[email protected]0a8db0d2011-04-13 15:15:40537// static
[email protected]9d8cfb682012-09-13 16:48:04538void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40539 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04540 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33541 BooleanPrefMember* force_google_safe_search,
[email protected]0a8db0d2011-04-13 15:15:40542 PrefService* pref_service) {
543 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]96a5c342012-12-04 18:14:02544 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43545 enable_referrers->MoveToThread(
546 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04547 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02548 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43549 enable_do_not_track->MoveToThread(
550 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04551 }
[email protected]d1208ba32012-11-08 11:10:33552 if (force_google_safe_search) {
[email protected]96a5c342012-12-04 18:14:02553 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33554 force_google_safe_search->MoveToThread(
555 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
556 }
[email protected]0a8db0d2011-04-13 15:15:40557}
558
[email protected]4c219e22012-05-05 19:41:04559// static
560void ChromeNetworkDelegate::AllowAccessToAllFiles() {
561 g_allow_file_access_ = true;
562}
563
[email protected]7a299a92012-10-24 23:54:50564// static
565Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() {
566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
567 PrefService* prefs = g_browser_process->local_state();
568 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
569 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
570
571 DictionaryValue* dict = new DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42572 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
573 dict->SetString("historic_received_content_length",
574 base::Int64ToString(total_received));
575 dict->SetString("historic_original_content_length",
576 base::Int64ToString(total_original));
[email protected]7a299a92012-10-24 23:54:50577 return dict;
578}
579
580Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
581 DictionaryValue* dict = new DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42582 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
583 dict->SetString("session_received_content_length",
584 base::Int64ToString(received_content_length_));
585 dict->SetString("session_original_content_length",
586 base::Int64ToString(original_content_length_));
[email protected]7a299a92012-10-24 23:54:50587 return dict;
588}
589
[email protected]4875ba12011-03-30 22:31:51590int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12591 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47592 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12593 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:34594#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59595 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
596 // blocked. However, an extension might redirect the request to another URL,
597 // which is not blocked.
598 if (url_blacklist_manager_ &&
[email protected]74575252013-03-12 19:58:21599 url_blacklist_manager_->IsRequestBlocked(*request)) {
[email protected]6a5f77c32011-09-04 19:19:59600 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59601 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26602 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
603 net::NetLog::StringCallback("url",
604 &request->url().possibly_invalid_spec()));
[email protected]91468a92013-03-06 17:21:55605 return net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]6a5f77c32011-09-04 19:19:59606 }
[email protected]3e598ff12011-09-06 11:22:34607#endif
[email protected]6a5f77c32011-09-04 19:19:59608
[email protected]6baff0b52012-03-06 01:30:18609 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
610
[email protected]0a8db0d2011-04-13 15:15:40611 if (!enable_referrers_->GetValue())
[email protected]99ecf6e2013-04-10 22:46:13612 request->SetReferrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04613 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
614 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33615
616 bool force_safe_search = force_google_safe_search_ &&
617 force_google_safe_search_->GetValue();
618
619 net::CompletionCallback wrapped_callback = callback;
620 if (force_safe_search) {
621 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
622 callback,
623 base::Unretained(request),
624 base::Unretained(new_url));
625 }
626
627 int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
628 profile_, extension_info_map_.get(), request, wrapped_callback,
629 new_url);
630
631 if (force_safe_search && rv == net::OK && new_url->is_empty())
632 ForceGoogleSafeSearch(request, new_url);
633
[email protected]f53b4802012-12-20 17:04:23634 if (connect_interceptor_)
635 connect_interceptor_->WitnessURLRequest(request);
636
[email protected]d1208ba32012-11-08 11:10:33637 return rv;
[email protected]d05ef99c2011-02-01 21:38:16638}
639
[email protected]4875ba12011-03-30 22:31:51640int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01641 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47642 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12643 net::HttpRequestHeaders* headers) {
[email protected]4875ba12011-03-30 22:31:51644 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18645 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44646}
[email protected]8202d0c2011-02-23 08:31:14647
[email protected]5796dc942011-07-14 19:26:10648void ChromeNetworkDelegate::OnSendHeaders(
649 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15650 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10651 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
652 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16653}
654
[email protected]ea8141e2011-10-05 13:12:51655int ChromeNetworkDelegate::OnHeadersReceived(
656 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47657 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32658 const net::HttpResponseHeaders* original_response_headers,
[email protected]ea8141e2011-10-05 13:12:51659 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
660 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
661 profile_, extension_info_map_.get(), request, callback,
662 original_response_headers, override_response_headers);
663}
664
[email protected]31b2e5f2011-04-20 16:58:32665void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
666 const GURL& new_location) {
667 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18668 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32669}
670
671
[email protected]8202d0c2011-02-23 08:31:14672void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]62fecae2011-04-21 11:08:24673 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18674 profile_, extension_info_map_.get(), request);
675 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14676}
677
[email protected]8523ba52011-05-22 19:00:58678void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
679 int bytes_read) {
[email protected]6f4b4b42012-08-27 21:59:07680 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
681 request, bytes_read);
682
[email protected]44879ed2012-04-06 01:11:02683#if defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58684 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02685#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58686}
687
[email protected]9045b8822012-01-13 20:35:35688void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
689 bool started) {
[email protected]2756a8e2012-09-07 18:24:29690 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
[email protected]7a299a92012-10-24 23:54:50691 // For better accuracy, we use the actual bytes read instead of the length
692 // specified with the Content-Length header, which may be inaccurate,
693 // or missing, as is the case with chunked encoding.
694 int64 received_content_length = request->received_response_content_length();
695
696 // Only record for http or https urls.
697 bool is_http = request->url().SchemeIs("http");
698 bool is_https = request->url().SchemeIs("https");
699
700 if (!request->was_cached() && // Don't record cached content
701 received_content_length && // Zero-byte responses aren't useful.
702 (is_http || is_https)) { // Only record for HTTP or HTTPS urls.
703 int64 original_content_length =
704 request->response_info().headers->GetInt64HeaderValue(
705 "x-original-content-length");
[email protected]2db93e42013-08-07 17:40:41706 bool data_reduction_proxy_was_used =
707 request->response_info().headers->HasHeaderValue(
708 "via", "1.1 Chrome Compression Proxy");
709
[email protected]7a299a92012-10-24 23:54:50710 // Since there was no indication of the original content length, presume
711 // it is no different from the number of bytes read.
712 int64 adjusted_original_content_length = original_content_length;
713 if (adjusted_original_content_length == -1)
714 adjusted_original_content_length = received_content_length;
[email protected]f81a5ee2013-06-20 21:08:49715 base::TimeDelta freshness_lifetime =
716 request->response_info().headers->GetFreshnessLifetime(
717 request->response_info().response_time);
[email protected]7a299a92012-10-24 23:54:50718 AccumulateContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41719 adjusted_original_content_length,
720 data_reduction_proxy_was_used);
[email protected]7a299a92012-10-24 23:54:50721 RecordContentLengthHistograms(received_content_length,
[email protected]f81a5ee2013-06-20 21:08:49722 original_content_length,
723 freshness_lifetime);
[email protected]7a299a92012-10-24 23:54:50724 DVLOG(2) << __FUNCTION__
725 << " received content length: " << received_content_length
726 << " original content length: " << original_content_length
727 << " url: " << request->url();
728 }
729
[email protected]48944382011-04-23 13:28:16730 bool is_redirect = request->response_headers() &&
731 net::HttpResponseHeaders::IsRedirectResponseCode(
732 request->response_headers()->response_code());
733 if (!is_redirect) {
734 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18735 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16736 }
[email protected]a83dd332011-07-13 10:41:01737 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
738 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04739 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35740 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01741 } else {
742 NOTREACHED();
[email protected]48944382011-04-23 13:28:16743 }
[email protected]673514522011-07-13 18:17:18744 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18745
746 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14747}
[email protected]4b50cb52011-03-10 00:29:37748
[email protected]4875ba12011-03-30 22:31:51749void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
750 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18751 profile_, request);
[email protected]16d5c72b2012-09-14 20:42:37752 if (load_time_stats_)
753 load_time_stats_->OnURLRequestDestroyed(*request);
[email protected]4875ba12011-03-30 22:31:51754}
755
[email protected]82a37672011-05-03 12:02:41756void ChromeNetworkDelegate::OnPACScriptError(int line_number,
757 const string16& error) {
[email protected]c454fe672012-03-12 21:18:01758 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18759 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41760}
[email protected]7efc582d2011-08-03 20:46:35761
[email protected]c2911d72011-10-03 22:16:36762net::NetworkDelegate::AuthRequiredResponse
763ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35764 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36765 const net::AuthChallengeInfo& auth_info,
766 const AuthCallback& callback,
767 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45768 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
769 profile_, extension_info_map_.get(), request, auth_info,
770 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35771}
[email protected]9c8ae8c2012-03-09 13:13:35772
[email protected]4c219e22012-05-05 19:41:04773bool ChromeNetworkDelegate::OnCanGetCookies(
774 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35775 const net::CookieList& cookie_list) {
776 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02777 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35778 return true;
779
780 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04781 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35782
783 int render_process_id = -1;
784 int render_view_id = -1;
785 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04786 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35787 BrowserThread::PostTask(
788 BrowserThread::UI, FROM_HERE,
789 base::Bind(&TabSpecificContentSettings::CookiesRead,
790 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04791 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43792 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35793 }
794
795 return allow;
796}
797
[email protected]4c219e22012-05-05 19:41:04798bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
799 const std::string& cookie_line,
800 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35801 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02802 if (!cookie_settings_.get())
[email protected]9c8ae8c2012-03-09 13:13:35803 return true;
804
805 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04806 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35807
[email protected]9c8ae8c2012-03-09 13:13:35808 int render_process_id = -1;
809 int render_view_id = -1;
810 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04811 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35812 BrowserThread::PostTask(
813 BrowserThread::UI, FROM_HERE,
814 base::Bind(&TabSpecificContentSettings::CookieChanged,
815 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04816 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43817 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35818 }
819
820 return allow;
821}
[email protected]4c219e22012-05-05 19:41:04822
823bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
[email protected]650b2d52013-02-10 03:41:45824 const base::FilePath& path) const {
[email protected]4c219e22012-05-05 19:41:04825 if (g_allow_file_access_)
826 return true;
827
[email protected]d8e4f132012-09-06 04:28:05828#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
829 return true;
830#else
[email protected]4c219e22012-05-05 19:41:04831#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05832 // If we're running Chrome for ChromeOS on Linux, we want to allow file
833 // access.
[email protected]288538482012-09-06 21:09:35834 if (!base::chromeos::IsRunningOnChromeOS() ||
835 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05836 return true;
[email protected]288538482012-09-06 21:09:35837 }
[email protected]d8e4f132012-09-06 04:28:05838
839 // Use a whitelist to only allow access to files residing in the list of
840 // directories below.
[email protected]4c219e22012-05-05 19:41:04841 static const char* const kLocalAccessWhiteList[] = {
842 "/home/chronos/user/Downloads",
843 "/home/chronos/user/log",
844 "/media",
845 "/opt/oem",
846 "/usr/share/chromeos-assets",
847 "/tmp",
848 "/var/log",
849 };
[email protected]d8e4f132012-09-06 04:28:05850#elif defined(OS_ANDROID)
851 // Access to files in external storage is allowed.
[email protected]650b2d52013-02-10 03:41:45852 base::FilePath external_storage_path;
[email protected]d8e4f132012-09-06 04:28:05853 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
854 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04855 return true;
856
[email protected]d8e4f132012-09-06 04:28:05857 // Whitelist of other allowed directories.
858 static const char* const kLocalAccessWhiteList[] = {
859 "/sdcard",
860 "/mnt/sdcard",
861 };
862#endif
863
[email protected]4c219e22012-05-05 19:41:04864 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
[email protected]650b2d52013-02-10 03:41:45865 const base::FilePath white_listed_path(kLocalAccessWhiteList[i]);
866 // base::FilePath::operator== should probably handle trailing separators.
[email protected]4c219e22012-05-05 19:41:04867 if (white_listed_path == path.StripTrailingSeparators() ||
868 white_listed_path.IsParent(path)) {
869 return true;
870 }
871 }
[email protected]d8e4f132012-09-06 04:28:05872
[email protected]288538482012-09-06 21:09:35873 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04874 return false;
[email protected]d8e4f132012-09-06 04:28:05875#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04876}
[email protected]a1d4ab072012-06-07 13:21:15877
878bool ChromeNetworkDelegate::OnCanThrottleRequest(
879 const net::URLRequest& request) const {
[email protected]c4a7df82012-08-09 22:48:46880 if (g_never_throttle_requests_) {
[email protected]a1d4ab072012-06-07 13:21:15881 return false;
882 }
883
[email protected]c4a7df82012-08-09 22:48:46884 return request.first_party_for_cookies().scheme() ==
[email protected]885c0e92012-11-13 20:27:42885 extensions::kExtensionScheme;
[email protected]a1d4ab072012-06-07 13:21:15886}
[email protected]aa28181e2012-06-13 00:53:58887
[email protected]e6d017652013-05-17 18:01:40888bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(
889 const GURL& url,
890 const GURL& first_party_for_cookies) const {
891 // NULL during tests, or when we're running in the system context.
[email protected]5173de8b2013-06-02 21:16:02892 if (!cookie_settings_.get())
[email protected]e6d017652013-05-17 18:01:40893 return false;
894
895 bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed(
896 url, first_party_for_cookies);
897 bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed(
898 url, first_party_for_cookies);
899 bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed);
900 return privacy_mode;
901}
902
[email protected]aa28181e2012-06-13 00:53:58903int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
904 net::SocketStream* socket,
905 const net::CompletionCallback& callback) {
906#if defined(ENABLE_CONFIGURATION_POLICY)
907 if (url_blacklist_manager_ &&
908 url_blacklist_manager_->IsURLBlocked(socket->url())) {
909 // URL access blocked by policy.
[email protected]aa28181e2012-06-13 00:53:58910 socket->net_log()->AddEvent(
[email protected]2fa08912012-06-14 20:56:26911 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
912 net::NetLog::StringCallback("url",
913 &socket->url().possibly_invalid_spec()));
[email protected]91468a92013-03-06 17:21:55914 return net::ERR_BLOCKED_BY_ADMINISTRATOR;
[email protected]aa28181e2012-06-13 00:53:58915 }
916#endif
917 return net::OK;
918}
[email protected]5a07c192012-07-30 20:18:22919
[email protected]a9e0d1412012-08-20 22:13:01920void ChromeNetworkDelegate::OnRequestWaitStateChange(
[email protected]5a07c192012-07-30 20:18:22921 const net::URLRequest& request,
[email protected]a9e0d1412012-08-20 22:13:01922 RequestWaitState state) {
923 if (load_time_stats_)
924 load_time_stats_->OnRequestWaitStateChange(request, state);
[email protected]5a07c192012-07-30 20:18:22925}
[email protected]7a299a92012-10-24 23:54:50926
927void ChromeNetworkDelegate::AccumulateContentLength(
[email protected]2db93e42013-08-07 17:40:41928 int64 received_content_length, int64 original_content_length,
929 bool data_reduction_proxy_was_used) {
[email protected]7a299a92012-10-24 23:54:50930 DCHECK_GE(received_content_length, 0);
931 DCHECK_GE(original_content_length, 0);
932 StoreAccumulatedContentLength(received_content_length,
[email protected]2db93e42013-08-07 17:40:41933 original_content_length,
934 data_reduction_proxy_was_used);
[email protected]7a299a92012-10-24 23:54:50935 received_content_length_ += received_content_length;
936 original_content_length_ += original_content_length;
937}