blob: d05a93a59b599ae24cbaceac02cdc16dd7cada13 [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]d1208ba32012-11-08 11:10:337#include <vector>
8
[email protected]d8e4f132012-09-06 04:28:059#include "base/base_paths.h"
[email protected]7a299a92012-10-24 23:54:5010#include "base/logging.h"
11#include "base/metrics/histogram.h"
[email protected]d8e4f132012-09-06 04:28:0512#include "base/path_service.h"
[email protected]0abb67122012-12-12 11:36:0513#include "base/prefs/public/pref_member.h"
[email protected]d1208ba32012-11-08 11:10:3314#include "base/string_split.h"
[email protected]3ea1b182013-02-08 22:38:4115#include "base/strings/string_number_conversions.h"
[email protected]6baff0b52012-03-06 01:30:1816#include "chrome/browser/browser_process.h"
[email protected]9c8ae8c2012-03-09 13:13:3517#include "chrome/browser/content_settings/cookie_settings.h"
18#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5819#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]aa84a7e2012-03-15 21:29:0620#include "chrome/browser/extensions/api/proxy/proxy_api.h"
[email protected]2b33dcd02012-03-18 01:34:1621#include "chrome/browser/extensions/api/web_request/web_request_api.h"
[email protected]5a38dfd2012-07-23 23:22:1022#include "chrome/browser/extensions/event_router_forwarder.h"
[email protected]c357acb42011-06-09 20:52:4223#include "chrome/browser/extensions/extension_info_map.h"
[email protected]6baff0b52012-03-06 01:30:1824#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]be93bba02012-10-24 16:44:0325#include "chrome/browser/extensions/extension_system.h"
[email protected]d1208ba32012-11-08 11:10:3326#include "chrome/browser/google/google_util.h"
[email protected]f53b4802012-12-20 17:04:2327#include "chrome/browser/net/connect_interceptor.h"
[email protected]a9e0d1412012-08-20 22:13:0128#include "chrome/browser/net/load_time_stats.h"
[email protected]6f4b4b42012-08-27 21:59:0729#include "chrome/browser/performance_monitor/performance_monitor.h"
[email protected]fb8fdf12012-08-21 16:28:2030#include "chrome/browser/prefs/pref_service.h"
[email protected]6baff0b52012-03-06 01:30:1831#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5832#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4033#include "chrome/common/pref_names.h"
[email protected]a1d4ab072012-06-07 13:21:1534#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4935#include "content/public/browser/browser_thread.h"
[email protected]9c1662b2012-03-06 15:44:3336#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3537#include "content/public/browser/resource_request_info.h"
[email protected]885c0e92012-11-13 20:27:4238#include "extensions/common/constants.h"
[email protected]82b42302011-04-20 16:28:1639#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1440#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5941#include "net/base/net_log.h"
[email protected]5b9bc352012-07-18 13:13:3442#include "net/cookies/canonical_cookie.h"
43#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4444#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1645#include "net/http/http_response_headers.h"
[email protected]aa28181e2012-06-13 00:53:5846#include "net/socket_stream/socket_stream.h"
[email protected]d05ef99c2011-02-01 21:38:1647#include "net/url_request/url_request.h"
48
[email protected]4c219e22012-05-05 19:41:0449#if defined(OS_CHROMEOS)
50#include "base/chromeos/chromeos_version.h"
[email protected]288538482012-09-06 21:09:3551#include "base/command_line.h"
52#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0453#endif
54
[email protected]3e598ff12011-09-06 11:22:3455#if defined(ENABLE_CONFIGURATION_POLICY)
56#include "chrome/browser/policy/url_blacklist_manager.h"
57#endif
58
[email protected]631bb742011-11-02 11:29:3959using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1160using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2561using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3962
[email protected]d8e4f132012-09-06 04:28:0563// By default we don't allow access to all file:// urls on ChromeOS and
64// Android.
65#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0466bool ChromeNetworkDelegate::g_allow_file_access_ = false;
67#else
68bool ChromeNetworkDelegate::g_allow_file_access_ = true;
69#endif
70
[email protected]c4a7df82012-08-09 22:48:4671// This remains false unless the --disable-extensions-http-throttling
72// flag is passed to the browser.
73bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
74
[email protected]d05ef99c2011-02-01 21:38:1675namespace {
76
[email protected]9d8cfb682012-09-13 16:48:0477const char kDNTHeader[] = "DNT";
78
[email protected]8202d0c2011-02-23 08:31:1479// If the |request| failed due to problems with a proxy, forward the error to
80// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5081void ForwardProxyErrors(net::URLRequest* request,
[email protected]5a38dfd2012-07-23 23:22:1082 extensions::EventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1883 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1484 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0585 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1486 case net::ERR_PROXY_AUTH_UNSUPPORTED:
87 case net::ERR_PROXY_CONNECTION_FAILED:
88 case net::ERR_TUNNEL_CONNECTION_FAILED:
[email protected]c454fe672012-03-12 21:18:0189 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:0590 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:1491 }
92 }
93}
94
[email protected]d1208ba32012-11-08 11:10:3395// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
96// same key as the the |second_parameter| (e.g. foo=baz). Both parameters
97// must be in key=value form.
98bool HasSameParameterKey(const std::string& first_parameter,
99 const std::string& second_parameter) {
100 DCHECK(second_parameter.find("=") != std::string::npos);
101 // Prefix for "foo=bar" is "foo=".
102 std::string parameter_prefix = second_parameter.substr(
103 0, second_parameter.find("=") + 1);
104 return StartsWithASCII(first_parameter, parameter_prefix, false);
105}
106
107// Examines the query string containing parameters and adds the necessary ones
108// so that SafeSearch is active. |query| is the string to examine and the
109// return value is the |query| string modified such that SafeSearch is active.
110std::string AddSafeSearchParameters(const std::string& query) {
111 std::vector<std::string> new_parameters;
112 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
113 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
114
115 std::vector<std::string> parameters;
116 base::SplitString(query, '&', &parameters);
117
118 std::vector<std::string>::iterator it;
119 for (it = parameters.begin(); it < parameters.end(); ++it) {
120 if (!HasSameParameterKey(*it, safe_parameter) &&
121 !HasSameParameterKey(*it, ssui_parameter)) {
122 new_parameters.push_back(*it);
123 }
124 }
125
126 new_parameters.push_back(safe_parameter);
127 new_parameters.push_back(ssui_parameter);
128 return JoinString(new_parameters, '&');
129}
130
131// If |request| is a request to Google Web Search the function
132// enforces that the SafeSearch query parameters are set to active.
133// Sets the query part of |new_url| with the new value of the parameters.
134void ForceGoogleSafeSearch(net::URLRequest* request,
135 GURL* new_url) {
136 if (!google_util::IsGoogleSearchUrl(request->url().spec()) &&
137 !google_util::IsGoogleHomePageUrl(request->url().spec()))
138 return;
139
140 std::string query = request->url().query();
141 std::string new_query = AddSafeSearchParameters(query);
142 if (query == new_query)
143 return;
144
145 GURL::Replacements replacements;
146 replacements.SetQueryStr(new_query);
147 *new_url = request->url().ReplaceComponents(replacements);
148}
149
150// Gets called when the extensions finish work on the URL. If the extensions
151// did not do a redirect (so |new_url| is empty) then we enforce the
152// SafeSearch parameters. Otherwise we will get called again after the
153// redirect and we enforce SafeSearch then.
154void ForceGoogleSafeSearchCallbackWrapper(
155 const net::CompletionCallback& callback,
156 net::URLRequest* request,
157 GURL* new_url,
158 int rv) {
159 if (rv == net::OK && new_url->is_empty())
160 ForceGoogleSafeSearch(request, new_url);
161 callback.Run(rv);
162}
163
[email protected]6baff0b52012-03-06 01:30:18164enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
165
166// Notifies the ExtensionProcessManager that a request has started or stopped
167// for a particular RenderView.
168void NotifyEPMRequestStatus(RequestStatus status,
169 void* profile_id,
170 int process_id,
171 int render_view_id) {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
173 Profile* profile = reinterpret_cast<Profile*>(profile_id);
174 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
175 return;
176
[email protected]e5775a52012-03-17 04:59:57177 ExtensionProcessManager* extension_process_manager =
[email protected]be93bba02012-10-24 16:44:03178 extensions::ExtensionSystem::Get(profile)->process_manager();
[email protected]e5775a52012-03-17 04:59:57179 // This may be NULL in unit tests.
180 if (!extension_process_manager)
181 return;
182
[email protected]6baff0b52012-03-06 01:30:18183 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
184 // system-level request).
[email protected]d3e898e2012-03-14 03:45:08185 RenderViewHost* render_view_host =
186 RenderViewHost::FromID(process_id, render_view_id);
[email protected]e5775a52012-03-17 04:59:57187 if (render_view_host) {
[email protected]6baff0b52012-03-06 01:30:18188 if (status == REQUEST_STARTED) {
[email protected]d3e898e2012-03-14 03:45:08189 extension_process_manager->OnNetworkRequestStarted(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18190 } else if (status == REQUEST_DONE) {
[email protected]d3e898e2012-03-14 03:45:08191 extension_process_manager->OnNetworkRequestDone(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18192 } else {
193 NOTREACHED();
194 }
195 }
196}
197
198void ForwardRequestStatus(
199 RequestStatus status, net::URLRequest* request, void* profile_id) {
[email protected]ea114722012-03-12 01:11:25200 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
201 if (!info)
202 return;
203
[email protected]6baff0b52012-03-06 01:30:18204 int process_id, render_view_id;
[email protected]ea114722012-03-12 01:11:25205 if (info->GetAssociatedRenderView(&process_id, &render_view_id)) {
[email protected]6baff0b52012-03-06 01:30:18206 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
207 base::Bind(&NotifyEPMRequestStatus,
208 status, profile_id, process_id, render_view_id));
209 }
210}
211
[email protected]7a299a92012-10-24 23:54:50212void UpdateContentLengthPrefs(int received_content_length,
213 int original_content_length) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 DCHECK_GE(received_content_length, 0);
216 DCHECK_GE(original_content_length, 0);
217
[email protected]a09159a2012-11-29 12:51:48218 // Can be NULL in a unit test.
219 if (!g_browser_process)
220 return;
221
[email protected]7a299a92012-10-24 23:54:50222 PrefService* prefs = g_browser_process->local_state();
[email protected]a09159a2012-11-29 12:51:48223 if (!prefs)
224 return;
[email protected]7a299a92012-10-24 23:54:50225
226 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
227 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
228 total_received += received_content_length;
229 total_original += original_content_length;
230 prefs->SetInt64(prefs::kHttpReceivedContentLength, total_received);
231 prefs->SetInt64(prefs::kHttpOriginalContentLength, total_original);
232}
233
234void StoreAccumulatedContentLength(int received_content_length,
235 int original_content_length) {
236 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
237 base::Bind(&UpdateContentLengthPrefs,
238 received_content_length, original_content_length));
239}
240
241void RecordContentLengthHistograms(
242 int64 received_content_length, int64 original_content_length) {
243#if defined(OS_ANDROID)
244 // Add the current resource to these histograms only when a valid
245 // X-Original-Content-Length header is present.
246 if (original_content_length >= 0) {
247 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL",
248 received_content_length);
249 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL",
250 original_content_length);
251 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL",
252 original_content_length - received_content_length);
253 } else {
254 // Presume the original content length is the same as the received content
255 // length if the X-Original-Content-Header is not present.
256 original_content_length = received_content_length;
257 }
258 UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length);
259 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength",
260 original_content_length);
261 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference",
262 original_content_length - received_content_length);
263#endif
264}
265
[email protected]d05ef99c2011-02-01 21:38:16266} // namespace
[email protected]ac039522010-06-15 16:39:44267
[email protected]0651b812011-02-24 00:22:50268ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10269 extensions::EventRouterForwarder* event_router,
[email protected]a09159a2012-11-29 12:51:48270 BooleanPrefMember* enable_referrers)
[email protected]3ce02412011-03-01 12:01:15271 : event_router_(event_router),
[email protected]a09159a2012-11-29 12:51:48272 profile_(NULL),
[email protected]6a5f77c32011-09-04 19:19:59273 enable_referrers_(enable_referrers),
[email protected]a09159a2012-11-29 12:51:48274 enable_do_not_track_(NULL),
275 force_google_safe_search_(NULL),
276 url_blacklist_manager_(NULL),
277 load_time_stats_(NULL),
[email protected]7a299a92012-10-24 23:54:50278 received_content_length_(0),
279 original_content_length_(0) {
[email protected]4b50cb52011-03-10 00:29:37280 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40281 DCHECK(enable_referrers);
[email protected]0651b812011-02-24 00:22:50282}
283
[email protected]ac039522010-06-15 16:39:44284ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
285
[email protected]a09159a2012-11-29 12:51:48286void ChromeNetworkDelegate::set_extension_info_map(
287 ExtensionInfoMap* extension_info_map) {
288 extension_info_map_ = extension_info_map;
289}
290
291void ChromeNetworkDelegate::set_cookie_settings(
292 CookieSettings* cookie_settings) {
293 cookie_settings_ = cookie_settings;
294}
295
[email protected]f53b4802012-12-20 17:04:23296void ChromeNetworkDelegate::set_predictor(
297 chrome_browser_net::Predictor* predictor) {
298 connect_interceptor_.reset(
299 new chrome_browser_net::ConnectInterceptor(predictor));
300}
301
[email protected]c4a7df82012-08-09 22:48:46302// static
[email protected]a1d4ab072012-06-07 13:21:15303void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46304 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15305}
306
[email protected]0a8db0d2011-04-13 15:15:40307// static
[email protected]9d8cfb682012-09-13 16:48:04308void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40309 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04310 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33311 BooleanPrefMember* force_google_safe_search,
[email protected]0a8db0d2011-04-13 15:15:40312 PrefService* pref_service) {
313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]96a5c342012-12-04 18:14:02314 enable_referrers->Init(prefs::kEnableReferrers, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43315 enable_referrers->MoveToThread(
316 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04317 if (enable_do_not_track) {
[email protected]96a5c342012-12-04 18:14:02318 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service);
[email protected]6a1c98e02012-10-24 21:49:43319 enable_do_not_track->MoveToThread(
320 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04321 }
[email protected]d1208ba32012-11-08 11:10:33322 if (force_google_safe_search) {
[email protected]96a5c342012-12-04 18:14:02323 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service);
[email protected]d1208ba32012-11-08 11:10:33324 force_google_safe_search->MoveToThread(
325 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
326 }
[email protected]0a8db0d2011-04-13 15:15:40327}
328
[email protected]4c219e22012-05-05 19:41:04329// static
330void ChromeNetworkDelegate::AllowAccessToAllFiles() {
331 g_allow_file_access_ = true;
332}
333
[email protected]7a299a92012-10-24 23:54:50334// static
335Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() {
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
337 PrefService* prefs = g_browser_process->local_state();
338 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
339 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
340
341 DictionaryValue* dict = new DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42342 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
343 dict->SetString("historic_received_content_length",
344 base::Int64ToString(total_received));
345 dict->SetString("historic_original_content_length",
346 base::Int64ToString(total_original));
[email protected]7a299a92012-10-24 23:54:50347 return dict;
348}
349
350Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
351 DictionaryValue* dict = new DictionaryValue();
[email protected]ae638fbe22012-12-07 02:57:42352 // Use strings to avoid overflow. base::Value only supports 32-bit integers.
353 dict->SetString("session_received_content_length",
354 base::Int64ToString(received_content_length_));
355 dict->SetString("session_original_content_length",
356 base::Int64ToString(original_content_length_));
[email protected]7a299a92012-10-24 23:54:50357 return dict;
358}
359
[email protected]4875ba12011-03-30 22:31:51360int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12361 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47362 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12363 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:34364#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59365 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
366 // blocked. However, an extension might redirect the request to another URL,
367 // which is not blocked.
368 if (url_blacklist_manager_ &&
369 url_blacklist_manager_->IsURLBlocked(request->url())) {
370 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59371 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26372 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
373 net::NetLog::StringCallback("url",
374 &request->url().possibly_invalid_spec()));
[email protected]6a5f77c32011-09-04 19:19:59375 return net::ERR_NETWORK_ACCESS_DENIED;
376 }
[email protected]3e598ff12011-09-06 11:22:34377#endif
[email protected]6a5f77c32011-09-04 19:19:59378
[email protected]6baff0b52012-03-06 01:30:18379 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
380
[email protected]0a8db0d2011-04-13 15:15:40381 if (!enable_referrers_->GetValue())
382 request->set_referrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04383 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
384 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33385
386 bool force_safe_search = force_google_safe_search_ &&
387 force_google_safe_search_->GetValue();
388
389 net::CompletionCallback wrapped_callback = callback;
390 if (force_safe_search) {
391 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
392 callback,
393 base::Unretained(request),
394 base::Unretained(new_url));
395 }
396
397 int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
398 profile_, extension_info_map_.get(), request, wrapped_callback,
399 new_url);
400
401 if (force_safe_search && rv == net::OK && new_url->is_empty())
402 ForceGoogleSafeSearch(request, new_url);
403
[email protected]f53b4802012-12-20 17:04:23404 if (connect_interceptor_)
405 connect_interceptor_->WitnessURLRequest(request);
406
[email protected]d1208ba32012-11-08 11:10:33407 return rv;
[email protected]d05ef99c2011-02-01 21:38:16408}
409
[email protected]4875ba12011-03-30 22:31:51410int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01411 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47412 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12413 net::HttpRequestHeaders* headers) {
[email protected]4875ba12011-03-30 22:31:51414 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18415 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44416}
[email protected]8202d0c2011-02-23 08:31:14417
[email protected]5796dc942011-07-14 19:26:10418void ChromeNetworkDelegate::OnSendHeaders(
419 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15420 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10421 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
422 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16423}
424
[email protected]ea8141e2011-10-05 13:12:51425int ChromeNetworkDelegate::OnHeadersReceived(
426 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47427 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32428 const net::HttpResponseHeaders* original_response_headers,
[email protected]ea8141e2011-10-05 13:12:51429 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
430 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
431 profile_, extension_info_map_.get(), request, callback,
432 original_response_headers, override_response_headers);
433}
434
[email protected]31b2e5f2011-04-20 16:58:32435void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
436 const GURL& new_location) {
437 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18438 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32439}
440
441
[email protected]8202d0c2011-02-23 08:31:14442void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]62fecae2011-04-21 11:08:24443 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18444 profile_, extension_info_map_.get(), request);
445 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14446}
447
[email protected]8523ba52011-05-22 19:00:58448void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
449 int bytes_read) {
[email protected]6f4b4b42012-08-27 21:59:07450 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
451 request, bytes_read);
452
[email protected]44879ed2012-04-06 01:11:02453#if defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58454 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02455#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58456}
457
[email protected]9045b8822012-01-13 20:35:35458void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
459 bool started) {
[email protected]2756a8e2012-09-07 18:24:29460 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
[email protected]7a299a92012-10-24 23:54:50461 // For better accuracy, we use the actual bytes read instead of the length
462 // specified with the Content-Length header, which may be inaccurate,
463 // or missing, as is the case with chunked encoding.
464 int64 received_content_length = request->received_response_content_length();
465
466 // Only record for http or https urls.
467 bool is_http = request->url().SchemeIs("http");
468 bool is_https = request->url().SchemeIs("https");
469
470 if (!request->was_cached() && // Don't record cached content
471 received_content_length && // Zero-byte responses aren't useful.
472 (is_http || is_https)) { // Only record for HTTP or HTTPS urls.
473 int64 original_content_length =
474 request->response_info().headers->GetInt64HeaderValue(
475 "x-original-content-length");
476 // Since there was no indication of the original content length, presume
477 // it is no different from the number of bytes read.
478 int64 adjusted_original_content_length = original_content_length;
479 if (adjusted_original_content_length == -1)
480 adjusted_original_content_length = received_content_length;
481 AccumulateContentLength(received_content_length,
482 adjusted_original_content_length);
483 RecordContentLengthHistograms(received_content_length,
484 original_content_length);
485 DVLOG(2) << __FUNCTION__
486 << " received content length: " << received_content_length
487 << " original content length: " << original_content_length
488 << " url: " << request->url();
489 }
490
[email protected]48944382011-04-23 13:28:16491 bool is_redirect = request->response_headers() &&
492 net::HttpResponseHeaders::IsRedirectResponseCode(
493 request->response_headers()->response_code());
494 if (!is_redirect) {
495 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18496 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16497 }
[email protected]a83dd332011-07-13 10:41:01498 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
499 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04500 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35501 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01502 } else {
503 NOTREACHED();
[email protected]48944382011-04-23 13:28:16504 }
[email protected]673514522011-07-13 18:17:18505 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18506
507 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14508}
[email protected]4b50cb52011-03-10 00:29:37509
[email protected]4875ba12011-03-30 22:31:51510void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
511 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18512 profile_, request);
[email protected]16d5c72b2012-09-14 20:42:37513 if (load_time_stats_)
514 load_time_stats_->OnURLRequestDestroyed(*request);
[email protected]4875ba12011-03-30 22:31:51515}
516
[email protected]82a37672011-05-03 12:02:41517void ChromeNetworkDelegate::OnPACScriptError(int line_number,
518 const string16& error) {
[email protected]c454fe672012-03-12 21:18:01519 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18520 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41521}
[email protected]7efc582d2011-08-03 20:46:35522
[email protected]c2911d72011-10-03 22:16:36523net::NetworkDelegate::AuthRequiredResponse
524ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35525 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36526 const net::AuthChallengeInfo& auth_info,
527 const AuthCallback& callback,
528 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45529 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
530 profile_, extension_info_map_.get(), request, auth_info,
531 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35532}
[email protected]9c8ae8c2012-03-09 13:13:35533
[email protected]4c219e22012-05-05 19:41:04534bool ChromeNetworkDelegate::OnCanGetCookies(
535 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35536 const net::CookieList& cookie_list) {
537 // NULL during tests, or when we're running in the system context.
538 if (!cookie_settings_)
539 return true;
540
541 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04542 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35543
544 int render_process_id = -1;
545 int render_view_id = -1;
546 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04547 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35548 BrowserThread::PostTask(
549 BrowserThread::UI, FROM_HERE,
550 base::Bind(&TabSpecificContentSettings::CookiesRead,
551 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04552 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43553 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35554 }
555
556 return allow;
557}
558
[email protected]4c219e22012-05-05 19:41:04559bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
560 const std::string& cookie_line,
561 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35562 // NULL during tests, or when we're running in the system context.
563 if (!cookie_settings_)
564 return true;
565
566 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04567 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35568
[email protected]9c8ae8c2012-03-09 13:13:35569 int render_process_id = -1;
570 int render_view_id = -1;
571 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04572 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35573 BrowserThread::PostTask(
574 BrowserThread::UI, FROM_HERE,
575 base::Bind(&TabSpecificContentSettings::CookieChanged,
576 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04577 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43578 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35579 }
580
581 return allow;
582}
[email protected]4c219e22012-05-05 19:41:04583
584bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
585 const FilePath& path) const {
586 if (g_allow_file_access_)
587 return true;
588
[email protected]d8e4f132012-09-06 04:28:05589#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
590 return true;
591#else
[email protected]4c219e22012-05-05 19:41:04592#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05593 // If we're running Chrome for ChromeOS on Linux, we want to allow file
594 // access.
[email protected]288538482012-09-06 21:09:35595 if (!base::chromeos::IsRunningOnChromeOS() ||
596 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05597 return true;
[email protected]288538482012-09-06 21:09:35598 }
[email protected]d8e4f132012-09-06 04:28:05599
600 // Use a whitelist to only allow access to files residing in the list of
601 // directories below.
[email protected]4c219e22012-05-05 19:41:04602 static const char* const kLocalAccessWhiteList[] = {
603 "/home/chronos/user/Downloads",
604 "/home/chronos/user/log",
605 "/media",
606 "/opt/oem",
607 "/usr/share/chromeos-assets",
608 "/tmp",
609 "/var/log",
610 };
[email protected]d8e4f132012-09-06 04:28:05611#elif defined(OS_ANDROID)
612 // Access to files in external storage is allowed.
613 FilePath external_storage_path;
614 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
615 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04616 return true;
617
[email protected]d8e4f132012-09-06 04:28:05618 // Whitelist of other allowed directories.
619 static const char* const kLocalAccessWhiteList[] = {
620 "/sdcard",
621 "/mnt/sdcard",
622 };
623#endif
624
[email protected]4c219e22012-05-05 19:41:04625 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
626 const FilePath white_listed_path(kLocalAccessWhiteList[i]);
627 // FilePath::operator== should probably handle trailing separators.
628 if (white_listed_path == path.StripTrailingSeparators() ||
629 white_listed_path.IsParent(path)) {
630 return true;
631 }
632 }
[email protected]d8e4f132012-09-06 04:28:05633
[email protected]288538482012-09-06 21:09:35634 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04635 return false;
[email protected]d8e4f132012-09-06 04:28:05636#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04637}
[email protected]a1d4ab072012-06-07 13:21:15638
639bool ChromeNetworkDelegate::OnCanThrottleRequest(
640 const net::URLRequest& request) const {
[email protected]c4a7df82012-08-09 22:48:46641 if (g_never_throttle_requests_) {
[email protected]a1d4ab072012-06-07 13:21:15642 return false;
643 }
644
[email protected]c4a7df82012-08-09 22:48:46645 return request.first_party_for_cookies().scheme() ==
[email protected]885c0e92012-11-13 20:27:42646 extensions::kExtensionScheme;
[email protected]a1d4ab072012-06-07 13:21:15647}
[email protected]aa28181e2012-06-13 00:53:58648
649int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
650 net::SocketStream* socket,
651 const net::CompletionCallback& callback) {
652#if defined(ENABLE_CONFIGURATION_POLICY)
653 if (url_blacklist_manager_ &&
654 url_blacklist_manager_->IsURLBlocked(socket->url())) {
655 // URL access blocked by policy.
[email protected]aa28181e2012-06-13 00:53:58656 socket->net_log()->AddEvent(
[email protected]2fa08912012-06-14 20:56:26657 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
658 net::NetLog::StringCallback("url",
659 &socket->url().possibly_invalid_spec()));
[email protected]aa28181e2012-06-13 00:53:58660 return net::ERR_NETWORK_ACCESS_DENIED;
661 }
662#endif
663 return net::OK;
664}
[email protected]5a07c192012-07-30 20:18:22665
[email protected]a9e0d1412012-08-20 22:13:01666void ChromeNetworkDelegate::OnRequestWaitStateChange(
[email protected]5a07c192012-07-30 20:18:22667 const net::URLRequest& request,
[email protected]a9e0d1412012-08-20 22:13:01668 RequestWaitState state) {
669 if (load_time_stats_)
670 load_time_stats_->OnRequestWaitStateChange(request, state);
[email protected]5a07c192012-07-30 20:18:22671}
[email protected]7a299a92012-10-24 23:54:50672
673void ChromeNetworkDelegate::AccumulateContentLength(
674 int64 received_content_length, int64 original_content_length) {
675 DCHECK_GE(received_content_length, 0);
676 DCHECK_GE(original_content_length, 0);
677 StoreAccumulatedContentLength(received_content_length,
678 original_content_length);
679 received_content_length_ += received_content_length;
680 original_content_length_ += original_content_length;
681}