blob: a79c012b7cef887469858a2d8e82e82cc3097aa5 [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]d1208ba32012-11-08 11:10:3313#include "base/string_split.h"
[email protected]c5aa8262012-08-21 11:19:5414#include "chrome/browser/api/prefs/pref_member.h"
[email protected]6baff0b52012-03-06 01:30:1815#include "chrome/browser/browser_process.h"
[email protected]9c8ae8c2012-03-09 13:13:3516#include "chrome/browser/content_settings/cookie_settings.h"
17#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5818#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]aa84a7e2012-03-15 21:29:0619#include "chrome/browser/extensions/api/proxy/proxy_api.h"
[email protected]2b33dcd02012-03-18 01:34:1620#include "chrome/browser/extensions/api/web_request/web_request_api.h"
[email protected]5a38dfd2012-07-23 23:22:1021#include "chrome/browser/extensions/event_router_forwarder.h"
[email protected]c357acb42011-06-09 20:52:4222#include "chrome/browser/extensions/extension_info_map.h"
[email protected]6baff0b52012-03-06 01:30:1823#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]be93bba02012-10-24 16:44:0324#include "chrome/browser/extensions/extension_system.h"
[email protected]d1208ba32012-11-08 11:10:3325#include "chrome/browser/google/google_util.h"
[email protected]a9e0d1412012-08-20 22:13:0126#include "chrome/browser/net/load_time_stats.h"
[email protected]6f4b4b42012-08-27 21:59:0727#include "chrome/browser/performance_monitor/performance_monitor.h"
[email protected]fb8fdf12012-08-21 16:28:2028#include "chrome/browser/prefs/pref_service.h"
[email protected]6baff0b52012-03-06 01:30:1829#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5830#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4031#include "chrome/common/pref_names.h"
[email protected]a1d4ab072012-06-07 13:21:1532#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4933#include "content/public/browser/browser_thread.h"
[email protected]9c1662b2012-03-06 15:44:3334#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3535#include "content/public/browser/resource_request_info.h"
[email protected]82b42302011-04-20 16:28:1636#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1437#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5938#include "net/base/net_log.h"
[email protected]5b9bc352012-07-18 13:13:3439#include "net/cookies/canonical_cookie.h"
40#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4441#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1642#include "net/http/http_response_headers.h"
[email protected]aa28181e2012-06-13 00:53:5843#include "net/socket_stream/socket_stream.h"
[email protected]d05ef99c2011-02-01 21:38:1644#include "net/url_request/url_request.h"
45
[email protected]e9c41d22012-08-17 00:08:1546#if !defined(OS_ANDROID)
[email protected]46690b72012-10-26 19:33:3247#include "chrome/browser/managed_mode/managed_mode_url_filter.h"
[email protected]e9c41d22012-08-17 00:08:1548#endif
49
[email protected]4c219e22012-05-05 19:41:0450#if defined(OS_CHROMEOS)
51#include "base/chromeos/chromeos_version.h"
[email protected]288538482012-09-06 21:09:3552#include "base/command_line.h"
53#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0454#endif
55
[email protected]3e598ff12011-09-06 11:22:3456#if defined(ENABLE_CONFIGURATION_POLICY)
57#include "chrome/browser/policy/url_blacklist_manager.h"
58#endif
59
[email protected]631bb742011-11-02 11:29:3960using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1161using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2562using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3963
[email protected]d8e4f132012-09-06 04:28:0564// By default we don't allow access to all file:// urls on ChromeOS and
65// Android.
66#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0467bool ChromeNetworkDelegate::g_allow_file_access_ = false;
68#else
69bool ChromeNetworkDelegate::g_allow_file_access_ = true;
70#endif
71
[email protected]c4a7df82012-08-09 22:48:4672// This remains false unless the --disable-extensions-http-throttling
73// flag is passed to the browser.
74bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
75
[email protected]d05ef99c2011-02-01 21:38:1676namespace {
77
[email protected]9d8cfb682012-09-13 16:48:0478const char kDNTHeader[] = "DNT";
79
[email protected]8202d0c2011-02-23 08:31:1480// If the |request| failed due to problems with a proxy, forward the error to
81// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5082void ForwardProxyErrors(net::URLRequest* request,
[email protected]5a38dfd2012-07-23 23:22:1083 extensions::EventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1884 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1485 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0586 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1487 case net::ERR_PROXY_AUTH_UNSUPPORTED:
88 case net::ERR_PROXY_CONNECTION_FAILED:
89 case net::ERR_TUNNEL_CONNECTION_FAILED:
[email protected]c454fe672012-03-12 21:18:0190 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:0591 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:1492 }
93 }
94}
95
[email protected]d1208ba32012-11-08 11:10:3396// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
97// same key as the the |second_parameter| (e.g. foo=baz). Both parameters
98// must be in key=value form.
99bool HasSameParameterKey(const std::string& first_parameter,
100 const std::string& second_parameter) {
101 DCHECK(second_parameter.find("=") != std::string::npos);
102 // Prefix for "foo=bar" is "foo=".
103 std::string parameter_prefix = second_parameter.substr(
104 0, second_parameter.find("=") + 1);
105 return StartsWithASCII(first_parameter, parameter_prefix, false);
106}
107
108// Examines the query string containing parameters and adds the necessary ones
109// so that SafeSearch is active. |query| is the string to examine and the
110// return value is the |query| string modified such that SafeSearch is active.
111std::string AddSafeSearchParameters(const std::string& query) {
112 std::vector<std::string> new_parameters;
113 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
114 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
115
116 std::vector<std::string> parameters;
117 base::SplitString(query, '&', &parameters);
118
119 std::vector<std::string>::iterator it;
120 for (it = parameters.begin(); it < parameters.end(); ++it) {
121 if (!HasSameParameterKey(*it, safe_parameter) &&
122 !HasSameParameterKey(*it, ssui_parameter)) {
123 new_parameters.push_back(*it);
124 }
125 }
126
127 new_parameters.push_back(safe_parameter);
128 new_parameters.push_back(ssui_parameter);
129 return JoinString(new_parameters, '&');
130}
131
132// If |request| is a request to Google Web Search the function
133// enforces that the SafeSearch query parameters are set to active.
134// Sets the query part of |new_url| with the new value of the parameters.
135void ForceGoogleSafeSearch(net::URLRequest* request,
136 GURL* new_url) {
137 if (!google_util::IsGoogleSearchUrl(request->url().spec()) &&
138 !google_util::IsGoogleHomePageUrl(request->url().spec()))
139 return;
140
141 std::string query = request->url().query();
142 std::string new_query = AddSafeSearchParameters(query);
143 if (query == new_query)
144 return;
145
146 GURL::Replacements replacements;
147 replacements.SetQueryStr(new_query);
148 *new_url = request->url().ReplaceComponents(replacements);
149}
150
151// Gets called when the extensions finish work on the URL. If the extensions
152// did not do a redirect (so |new_url| is empty) then we enforce the
153// SafeSearch parameters. Otherwise we will get called again after the
154// redirect and we enforce SafeSearch then.
155void ForceGoogleSafeSearchCallbackWrapper(
156 const net::CompletionCallback& callback,
157 net::URLRequest* request,
158 GURL* new_url,
159 int rv) {
160 if (rv == net::OK && new_url->is_empty())
161 ForceGoogleSafeSearch(request, new_url);
162 callback.Run(rv);
163}
164
[email protected]6baff0b52012-03-06 01:30:18165enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
166
167// Notifies the ExtensionProcessManager that a request has started or stopped
168// for a particular RenderView.
169void NotifyEPMRequestStatus(RequestStatus status,
170 void* profile_id,
171 int process_id,
172 int render_view_id) {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
174 Profile* profile = reinterpret_cast<Profile*>(profile_id);
175 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
176 return;
177
[email protected]e5775a52012-03-17 04:59:57178 ExtensionProcessManager* extension_process_manager =
[email protected]be93bba02012-10-24 16:44:03179 extensions::ExtensionSystem::Get(profile)->process_manager();
[email protected]e5775a52012-03-17 04:59:57180 // This may be NULL in unit tests.
181 if (!extension_process_manager)
182 return;
183
[email protected]6baff0b52012-03-06 01:30:18184 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
185 // system-level request).
[email protected]d3e898e2012-03-14 03:45:08186 RenderViewHost* render_view_host =
187 RenderViewHost::FromID(process_id, render_view_id);
[email protected]e5775a52012-03-17 04:59:57188 if (render_view_host) {
[email protected]6baff0b52012-03-06 01:30:18189 if (status == REQUEST_STARTED) {
[email protected]d3e898e2012-03-14 03:45:08190 extension_process_manager->OnNetworkRequestStarted(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18191 } else if (status == REQUEST_DONE) {
[email protected]d3e898e2012-03-14 03:45:08192 extension_process_manager->OnNetworkRequestDone(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18193 } else {
194 NOTREACHED();
195 }
196 }
197}
198
199void ForwardRequestStatus(
200 RequestStatus status, net::URLRequest* request, void* profile_id) {
[email protected]ea114722012-03-12 01:11:25201 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
202 if (!info)
203 return;
204
[email protected]6baff0b52012-03-06 01:30:18205 int process_id, render_view_id;
[email protected]ea114722012-03-12 01:11:25206 if (info->GetAssociatedRenderView(&process_id, &render_view_id)) {
[email protected]6baff0b52012-03-06 01:30:18207 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
208 base::Bind(&NotifyEPMRequestStatus,
209 status, profile_id, process_id, render_view_id));
210 }
211}
212
[email protected]7a299a92012-10-24 23:54:50213void UpdateContentLengthPrefs(int received_content_length,
214 int original_content_length) {
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
216 DCHECK_GE(received_content_length, 0);
217 DCHECK_GE(original_content_length, 0);
218
219 PrefService* prefs = g_browser_process->local_state();
220
221 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
222 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
223 total_received += received_content_length;
224 total_original += original_content_length;
225 prefs->SetInt64(prefs::kHttpReceivedContentLength, total_received);
226 prefs->SetInt64(prefs::kHttpOriginalContentLength, total_original);
227}
228
229void StoreAccumulatedContentLength(int received_content_length,
230 int original_content_length) {
231 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
232 base::Bind(&UpdateContentLengthPrefs,
233 received_content_length, original_content_length));
234}
235
236void RecordContentLengthHistograms(
237 int64 received_content_length, int64 original_content_length) {
238#if defined(OS_ANDROID)
239 // Add the current resource to these histograms only when a valid
240 // X-Original-Content-Length header is present.
241 if (original_content_length >= 0) {
242 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL",
243 received_content_length);
244 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL",
245 original_content_length);
246 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL",
247 original_content_length - received_content_length);
248 } else {
249 // Presume the original content length is the same as the received content
250 // length if the X-Original-Content-Header is not present.
251 original_content_length = received_content_length;
252 }
253 UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length);
254 UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength",
255 original_content_length);
256 UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference",
257 original_content_length - received_content_length);
258#endif
259}
260
[email protected]d05ef99c2011-02-01 21:38:16261} // namespace
[email protected]ac039522010-06-15 16:39:44262
[email protected]0651b812011-02-24 00:22:50263ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10264 extensions::EventRouterForwarder* event_router,
[email protected]c357acb42011-06-09 20:52:42265 ExtensionInfoMap* extension_info_map,
[email protected]6a5f77c32011-09-04 19:19:59266 const policy::URLBlacklistManager* url_blacklist_manager,
[email protected]e9c41d22012-08-17 00:08:15267 const ManagedModeURLFilter* managed_mode_url_filter,
[email protected]673514522011-07-13 18:17:18268 void* profile,
[email protected]9c8ae8c2012-03-09 13:13:35269 CookieSettings* cookie_settings,
[email protected]5a07c192012-07-30 20:18:22270 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04271 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33272 BooleanPrefMember* force_google_safe_search,
[email protected]a9e0d1412012-08-20 22:13:01273 chrome_browser_net::LoadTimeStats* load_time_stats)
[email protected]3ce02412011-03-01 12:01:15274 : event_router_(event_router),
[email protected]673514522011-07-13 18:17:18275 profile_(profile),
[email protected]9c8ae8c2012-03-09 13:13:35276 cookie_settings_(cookie_settings),
[email protected]c357acb42011-06-09 20:52:42277 extension_info_map_(extension_info_map),
[email protected]6a5f77c32011-09-04 19:19:59278 enable_referrers_(enable_referrers),
[email protected]9d8cfb682012-09-13 16:48:04279 enable_do_not_track_(enable_do_not_track),
[email protected]d1208ba32012-11-08 11:10:33280 force_google_safe_search_(force_google_safe_search),
[email protected]5a07c192012-07-30 20:18:22281 url_blacklist_manager_(url_blacklist_manager),
[email protected]e9c41d22012-08-17 00:08:15282 managed_mode_url_filter_(managed_mode_url_filter),
[email protected]7a299a92012-10-24 23:54:50283 load_time_stats_(load_time_stats),
284 received_content_length_(0),
285 original_content_length_(0) {
[email protected]4b50cb52011-03-10 00:29:37286 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40287 DCHECK(enable_referrers);
[email protected]9c8ae8c2012-03-09 13:13:35288 DCHECK(!profile || cookie_settings);
[email protected]0651b812011-02-24 00:22:50289}
290
[email protected]ac039522010-06-15 16:39:44291ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
292
[email protected]c4a7df82012-08-09 22:48:46293// static
[email protected]a1d4ab072012-06-07 13:21:15294void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46295 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15296}
297
[email protected]0a8db0d2011-04-13 15:15:40298// static
[email protected]9d8cfb682012-09-13 16:48:04299void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40300 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04301 BooleanPrefMember* enable_do_not_track,
[email protected]d1208ba32012-11-08 11:10:33302 BooleanPrefMember* force_google_safe_search,
[email protected]0a8db0d2011-04-13 15:15:40303 PrefService* pref_service) {
304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
305 enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL);
[email protected]6a1c98e02012-10-24 21:49:43306 enable_referrers->MoveToThread(
307 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04308 if (enable_do_not_track) {
309 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service, NULL);
[email protected]6a1c98e02012-10-24 21:49:43310 enable_do_not_track->MoveToThread(
311 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]9d8cfb682012-09-13 16:48:04312 }
[email protected]d1208ba32012-11-08 11:10:33313 if (force_google_safe_search) {
314 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service, NULL);
315 force_google_safe_search->MoveToThread(
316 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
317 }
[email protected]0a8db0d2011-04-13 15:15:40318}
319
[email protected]4c219e22012-05-05 19:41:04320// static
321void ChromeNetworkDelegate::AllowAccessToAllFiles() {
322 g_allow_file_access_ = true;
323}
324
[email protected]7a299a92012-10-24 23:54:50325// static
326Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() {
327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
328 PrefService* prefs = g_browser_process->local_state();
329 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength);
330 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength);
331
332 DictionaryValue* dict = new DictionaryValue();
333 dict->SetInteger("historic_received_content_length", total_received);
334 dict->SetInteger("historic_original_content_length", total_original);
335 return dict;
336}
337
338Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const {
339 DictionaryValue* dict = new DictionaryValue();
340 dict->SetInteger("session_received_content_length", received_content_length_);
341 dict->SetInteger("session_original_content_length", original_content_length_);
342 return dict;
343}
344
[email protected]4875ba12011-03-30 22:31:51345int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12346 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47347 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12348 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:34349#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59350 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
351 // blocked. However, an extension might redirect the request to another URL,
352 // which is not blocked.
353 if (url_blacklist_manager_ &&
354 url_blacklist_manager_->IsURLBlocked(request->url())) {
355 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59356 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26357 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
358 net::NetLog::StringCallback("url",
359 &request->url().possibly_invalid_spec()));
[email protected]6a5f77c32011-09-04 19:19:59360 return net::ERR_NETWORK_ACCESS_DENIED;
361 }
[email protected]3e598ff12011-09-06 11:22:34362#endif
[email protected]6a5f77c32011-09-04 19:19:59363
[email protected]e9c41d22012-08-17 00:08:15364#if !defined(OS_ANDROID)
365 if (managed_mode_url_filter_ &&
366 !managed_mode_url_filter_->IsURLWhitelisted(request->url())) {
367 // Block for now.
368 return net::ERR_NETWORK_ACCESS_DENIED;
369 }
370#endif
371
[email protected]6baff0b52012-03-06 01:30:18372 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
373
[email protected]0a8db0d2011-04-13 15:15:40374 if (!enable_referrers_->GetValue())
375 request->set_referrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04376 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
377 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]d1208ba32012-11-08 11:10:33378
379 bool force_safe_search = force_google_safe_search_ &&
380 force_google_safe_search_->GetValue();
381
382 net::CompletionCallback wrapped_callback = callback;
383 if (force_safe_search) {
384 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
385 callback,
386 base::Unretained(request),
387 base::Unretained(new_url));
388 }
389
390 int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
391 profile_, extension_info_map_.get(), request, wrapped_callback,
392 new_url);
393
394 if (force_safe_search && rv == net::OK && new_url->is_empty())
395 ForceGoogleSafeSearch(request, new_url);
396
397 return rv;
[email protected]d05ef99c2011-02-01 21:38:16398}
399
[email protected]4875ba12011-03-30 22:31:51400int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01401 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47402 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12403 net::HttpRequestHeaders* headers) {
[email protected]4875ba12011-03-30 22:31:51404 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18405 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44406}
[email protected]8202d0c2011-02-23 08:31:14407
[email protected]5796dc942011-07-14 19:26:10408void ChromeNetworkDelegate::OnSendHeaders(
409 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15410 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10411 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
412 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16413}
414
[email protected]ea8141e2011-10-05 13:12:51415int ChromeNetworkDelegate::OnHeadersReceived(
416 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47417 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32418 const net::HttpResponseHeaders* original_response_headers,
[email protected]ea8141e2011-10-05 13:12:51419 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
420 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
421 profile_, extension_info_map_.get(), request, callback,
422 original_response_headers, override_response_headers);
423}
424
[email protected]31b2e5f2011-04-20 16:58:32425void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
426 const GURL& new_location) {
427 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18428 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32429}
430
431
[email protected]8202d0c2011-02-23 08:31:14432void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]62fecae2011-04-21 11:08:24433 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18434 profile_, extension_info_map_.get(), request);
435 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14436}
437
[email protected]8523ba52011-05-22 19:00:58438void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
439 int bytes_read) {
[email protected]6f4b4b42012-08-27 21:59:07440 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
441 request, bytes_read);
442
[email protected]44879ed2012-04-06 01:11:02443#if defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58444 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02445#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58446}
447
[email protected]9045b8822012-01-13 20:35:35448void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
449 bool started) {
[email protected]2756a8e2012-09-07 18:24:29450 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
[email protected]7a299a92012-10-24 23:54:50451 // For better accuracy, we use the actual bytes read instead of the length
452 // specified with the Content-Length header, which may be inaccurate,
453 // or missing, as is the case with chunked encoding.
454 int64 received_content_length = request->received_response_content_length();
455
456 // Only record for http or https urls.
457 bool is_http = request->url().SchemeIs("http");
458 bool is_https = request->url().SchemeIs("https");
459
460 if (!request->was_cached() && // Don't record cached content
461 received_content_length && // Zero-byte responses aren't useful.
462 (is_http || is_https)) { // Only record for HTTP or HTTPS urls.
463 int64 original_content_length =
464 request->response_info().headers->GetInt64HeaderValue(
465 "x-original-content-length");
466 // Since there was no indication of the original content length, presume
467 // it is no different from the number of bytes read.
468 int64 adjusted_original_content_length = original_content_length;
469 if (adjusted_original_content_length == -1)
470 adjusted_original_content_length = received_content_length;
471 AccumulateContentLength(received_content_length,
472 adjusted_original_content_length);
473 RecordContentLengthHistograms(received_content_length,
474 original_content_length);
475 DVLOG(2) << __FUNCTION__
476 << " received content length: " << received_content_length
477 << " original content length: " << original_content_length
478 << " url: " << request->url();
479 }
480
[email protected]48944382011-04-23 13:28:16481 bool is_redirect = request->response_headers() &&
482 net::HttpResponseHeaders::IsRedirectResponseCode(
483 request->response_headers()->response_code());
484 if (!is_redirect) {
485 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18486 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16487 }
[email protected]a83dd332011-07-13 10:41:01488 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
489 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04490 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35491 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01492 } else {
493 NOTREACHED();
[email protected]48944382011-04-23 13:28:16494 }
[email protected]673514522011-07-13 18:17:18495 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18496
497 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14498}
[email protected]4b50cb52011-03-10 00:29:37499
[email protected]4875ba12011-03-30 22:31:51500void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
501 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18502 profile_, request);
[email protected]16d5c72b2012-09-14 20:42:37503 if (load_time_stats_)
504 load_time_stats_->OnURLRequestDestroyed(*request);
[email protected]4875ba12011-03-30 22:31:51505}
506
[email protected]82a37672011-05-03 12:02:41507void ChromeNetworkDelegate::OnPACScriptError(int line_number,
508 const string16& error) {
[email protected]c454fe672012-03-12 21:18:01509 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18510 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41511}
[email protected]7efc582d2011-08-03 20:46:35512
[email protected]c2911d72011-10-03 22:16:36513net::NetworkDelegate::AuthRequiredResponse
514ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35515 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36516 const net::AuthChallengeInfo& auth_info,
517 const AuthCallback& callback,
518 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45519 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
520 profile_, extension_info_map_.get(), request, auth_info,
521 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35522}
[email protected]9c8ae8c2012-03-09 13:13:35523
[email protected]4c219e22012-05-05 19:41:04524bool ChromeNetworkDelegate::OnCanGetCookies(
525 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35526 const net::CookieList& cookie_list) {
527 // NULL during tests, or when we're running in the system context.
528 if (!cookie_settings_)
529 return true;
530
531 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04532 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35533
534 int render_process_id = -1;
535 int render_view_id = -1;
536 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04537 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35538 BrowserThread::PostTask(
539 BrowserThread::UI, FROM_HERE,
540 base::Bind(&TabSpecificContentSettings::CookiesRead,
541 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04542 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43543 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35544 }
545
546 return allow;
547}
548
[email protected]4c219e22012-05-05 19:41:04549bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
550 const std::string& cookie_line,
551 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35552 // NULL during tests, or when we're running in the system context.
553 if (!cookie_settings_)
554 return true;
555
556 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04557 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35558
[email protected]9c8ae8c2012-03-09 13:13:35559 int render_process_id = -1;
560 int render_view_id = -1;
561 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04562 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35563 BrowserThread::PostTask(
564 BrowserThread::UI, FROM_HERE,
565 base::Bind(&TabSpecificContentSettings::CookieChanged,
566 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04567 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43568 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35569 }
570
571 return allow;
572}
[email protected]4c219e22012-05-05 19:41:04573
574bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
575 const FilePath& path) const {
576 if (g_allow_file_access_)
577 return true;
578
[email protected]d8e4f132012-09-06 04:28:05579#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
580 return true;
581#else
[email protected]4c219e22012-05-05 19:41:04582#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05583 // If we're running Chrome for ChromeOS on Linux, we want to allow file
584 // access.
[email protected]288538482012-09-06 21:09:35585 if (!base::chromeos::IsRunningOnChromeOS() ||
586 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05587 return true;
[email protected]288538482012-09-06 21:09:35588 }
[email protected]d8e4f132012-09-06 04:28:05589
590 // Use a whitelist to only allow access to files residing in the list of
591 // directories below.
[email protected]4c219e22012-05-05 19:41:04592 static const char* const kLocalAccessWhiteList[] = {
593 "/home/chronos/user/Downloads",
594 "/home/chronos/user/log",
595 "/media",
596 "/opt/oem",
597 "/usr/share/chromeos-assets",
598 "/tmp",
599 "/var/log",
600 };
[email protected]d8e4f132012-09-06 04:28:05601#elif defined(OS_ANDROID)
602 // Access to files in external storage is allowed.
603 FilePath external_storage_path;
604 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
605 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04606 return true;
607
[email protected]d8e4f132012-09-06 04:28:05608 // Whitelist of other allowed directories.
609 static const char* const kLocalAccessWhiteList[] = {
610 "/sdcard",
611 "/mnt/sdcard",
612 };
613#endif
614
[email protected]4c219e22012-05-05 19:41:04615 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
616 const FilePath white_listed_path(kLocalAccessWhiteList[i]);
617 // FilePath::operator== should probably handle trailing separators.
618 if (white_listed_path == path.StripTrailingSeparators() ||
619 white_listed_path.IsParent(path)) {
620 return true;
621 }
622 }
[email protected]d8e4f132012-09-06 04:28:05623
[email protected]288538482012-09-06 21:09:35624 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04625 return false;
[email protected]d8e4f132012-09-06 04:28:05626#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04627}
[email protected]a1d4ab072012-06-07 13:21:15628
629bool ChromeNetworkDelegate::OnCanThrottleRequest(
630 const net::URLRequest& request) const {
[email protected]c4a7df82012-08-09 22:48:46631 if (g_never_throttle_requests_) {
[email protected]a1d4ab072012-06-07 13:21:15632 return false;
633 }
634
[email protected]c4a7df82012-08-09 22:48:46635 return request.first_party_for_cookies().scheme() ==
[email protected]a1d4ab072012-06-07 13:21:15636 chrome::kExtensionScheme;
637}
[email protected]aa28181e2012-06-13 00:53:58638
639int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
640 net::SocketStream* socket,
641 const net::CompletionCallback& callback) {
642#if defined(ENABLE_CONFIGURATION_POLICY)
643 if (url_blacklist_manager_ &&
644 url_blacklist_manager_->IsURLBlocked(socket->url())) {
645 // URL access blocked by policy.
[email protected]aa28181e2012-06-13 00:53:58646 socket->net_log()->AddEvent(
[email protected]2fa08912012-06-14 20:56:26647 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
648 net::NetLog::StringCallback("url",
649 &socket->url().possibly_invalid_spec()));
[email protected]aa28181e2012-06-13 00:53:58650 return net::ERR_NETWORK_ACCESS_DENIED;
651 }
652#endif
653 return net::OK;
654}
[email protected]5a07c192012-07-30 20:18:22655
[email protected]a9e0d1412012-08-20 22:13:01656void ChromeNetworkDelegate::OnRequestWaitStateChange(
[email protected]5a07c192012-07-30 20:18:22657 const net::URLRequest& request,
[email protected]a9e0d1412012-08-20 22:13:01658 RequestWaitState state) {
659 if (load_time_stats_)
660 load_time_stats_->OnRequestWaitStateChange(request, state);
[email protected]5a07c192012-07-30 20:18:22661}
[email protected]7a299a92012-10-24 23:54:50662
663void ChromeNetworkDelegate::AccumulateContentLength(
664 int64 received_content_length, int64 original_content_length) {
665 DCHECK_GE(received_content_length, 0);
666 DCHECK_GE(original_content_length, 0);
667 StoreAccumulatedContentLength(received_content_length,
668 original_content_length);
669 received_content_length_ += received_content_length;
670 original_content_length_ += original_content_length;
671}