blob: fa084a2f3878b55ec7838f467b95508f39f2a934 [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
7#include "base/logging.h"
[email protected]d8e4f132012-09-06 04:28:058#include "base/base_paths.h"
9#include "base/path_service.h"
[email protected]c5aa8262012-08-21 11:19:5410#include "chrome/browser/api/prefs/pref_member.h"
[email protected]6baff0b52012-03-06 01:30:1811#include "chrome/browser/browser_process.h"
[email protected]9c8ae8c2012-03-09 13:13:3512#include "chrome/browser/content_settings/cookie_settings.h"
13#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5814#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]aa84a7e2012-03-15 21:29:0615#include "chrome/browser/extensions/api/proxy/proxy_api.h"
[email protected]2b33dcd02012-03-18 01:34:1616#include "chrome/browser/extensions/api/web_request/web_request_api.h"
[email protected]5a38dfd2012-07-23 23:22:1017#include "chrome/browser/extensions/event_router_forwarder.h"
[email protected]c357acb42011-06-09 20:52:4218#include "chrome/browser/extensions/extension_info_map.h"
[email protected]6baff0b52012-03-06 01:30:1819#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]be93bba02012-10-24 16:44:0320#include "chrome/browser/extensions/extension_system.h"
[email protected]a9e0d1412012-08-20 22:13:0121#include "chrome/browser/net/load_time_stats.h"
[email protected]6f4b4b42012-08-27 21:59:0722#include "chrome/browser/performance_monitor/performance_monitor.h"
[email protected]fb8fdf12012-08-21 16:28:2023#include "chrome/browser/prefs/pref_service.h"
[email protected]6baff0b52012-03-06 01:30:1824#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5825#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4026#include "chrome/common/pref_names.h"
[email protected]a1d4ab072012-06-07 13:21:1527#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4928#include "content/public/browser/browser_thread.h"
[email protected]9c1662b2012-03-06 15:44:3329#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3530#include "content/public/browser/resource_request_info.h"
[email protected]82b42302011-04-20 16:28:1631#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1432#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5933#include "net/base/net_log.h"
[email protected]5b9bc352012-07-18 13:13:3434#include "net/cookies/canonical_cookie.h"
35#include "net/cookies/cookie_options.h"
[email protected]ac039522010-06-15 16:39:4436#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1637#include "net/http/http_response_headers.h"
[email protected]aa28181e2012-06-13 00:53:5838#include "net/socket_stream/socket_stream.h"
[email protected]d05ef99c2011-02-01 21:38:1639#include "net/url_request/url_request.h"
40
[email protected]e9c41d22012-08-17 00:08:1541#if !defined(OS_ANDROID)
42#include "chrome/browser/managed_mode_url_filter.h"
43#endif
44
[email protected]4c219e22012-05-05 19:41:0445#if defined(OS_CHROMEOS)
46#include "base/chromeos/chromeos_version.h"
[email protected]288538482012-09-06 21:09:3547#include "base/command_line.h"
48#include "chrome/common/chrome_switches.h"
[email protected]4c219e22012-05-05 19:41:0449#endif
50
[email protected]3e598ff12011-09-06 11:22:3451#if defined(ENABLE_CONFIGURATION_POLICY)
52#include "chrome/browser/policy/url_blacklist_manager.h"
53#endif
54
[email protected]631bb742011-11-02 11:29:3955using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1156using content::RenderViewHost;
[email protected]ea114722012-03-12 01:11:2557using content::ResourceRequestInfo;
[email protected]631bb742011-11-02 11:29:3958
[email protected]d8e4f132012-09-06 04:28:0559// By default we don't allow access to all file:// urls on ChromeOS and
60// Android.
61#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:0462bool ChromeNetworkDelegate::g_allow_file_access_ = false;
63#else
64bool ChromeNetworkDelegate::g_allow_file_access_ = true;
65#endif
66
[email protected]c4a7df82012-08-09 22:48:4667// This remains false unless the --disable-extensions-http-throttling
68// flag is passed to the browser.
69bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
70
[email protected]d05ef99c2011-02-01 21:38:1671namespace {
72
[email protected]9d8cfb682012-09-13 16:48:0473const char kDNTHeader[] = "DNT";
74
[email protected]8202d0c2011-02-23 08:31:1475// If the |request| failed due to problems with a proxy, forward the error to
76// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5077void ForwardProxyErrors(net::URLRequest* request,
[email protected]5a38dfd2012-07-23 23:22:1078 extensions::EventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1879 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1480 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0581 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1482 case net::ERR_PROXY_AUTH_UNSUPPORTED:
83 case net::ERR_PROXY_CONNECTION_FAILED:
84 case net::ERR_TUNNEL_CONNECTION_FAILED:
[email protected]c454fe672012-03-12 21:18:0185 extensions::ProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:0586 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:1487 }
88 }
89}
90
[email protected]6baff0b52012-03-06 01:30:1891enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
92
93// Notifies the ExtensionProcessManager that a request has started or stopped
94// for a particular RenderView.
95void NotifyEPMRequestStatus(RequestStatus status,
96 void* profile_id,
97 int process_id,
98 int render_view_id) {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
100 Profile* profile = reinterpret_cast<Profile*>(profile_id);
101 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
102 return;
103
[email protected]e5775a52012-03-17 04:59:57104 ExtensionProcessManager* extension_process_manager =
[email protected]be93bba02012-10-24 16:44:03105 extensions::ExtensionSystem::Get(profile)->process_manager();
[email protected]e5775a52012-03-17 04:59:57106 // This may be NULL in unit tests.
107 if (!extension_process_manager)
108 return;
109
[email protected]6baff0b52012-03-06 01:30:18110 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
111 // system-level request).
[email protected]d3e898e2012-03-14 03:45:08112 RenderViewHost* render_view_host =
113 RenderViewHost::FromID(process_id, render_view_id);
[email protected]e5775a52012-03-17 04:59:57114 if (render_view_host) {
[email protected]6baff0b52012-03-06 01:30:18115 if (status == REQUEST_STARTED) {
[email protected]d3e898e2012-03-14 03:45:08116 extension_process_manager->OnNetworkRequestStarted(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18117 } else if (status == REQUEST_DONE) {
[email protected]d3e898e2012-03-14 03:45:08118 extension_process_manager->OnNetworkRequestDone(render_view_host);
[email protected]6baff0b52012-03-06 01:30:18119 } else {
120 NOTREACHED();
121 }
122 }
123}
124
125void ForwardRequestStatus(
126 RequestStatus status, net::URLRequest* request, void* profile_id) {
[email protected]ea114722012-03-12 01:11:25127 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
128 if (!info)
129 return;
130
[email protected]6baff0b52012-03-06 01:30:18131 int process_id, render_view_id;
[email protected]ea114722012-03-12 01:11:25132 if (info->GetAssociatedRenderView(&process_id, &render_view_id)) {
[email protected]6baff0b52012-03-06 01:30:18133 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
134 base::Bind(&NotifyEPMRequestStatus,
135 status, profile_id, process_id, render_view_id));
136 }
137}
138
[email protected]d05ef99c2011-02-01 21:38:16139} // namespace
[email protected]ac039522010-06-15 16:39:44140
[email protected]0651b812011-02-24 00:22:50141ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]5a38dfd2012-07-23 23:22:10142 extensions::EventRouterForwarder* event_router,
[email protected]c357acb42011-06-09 20:52:42143 ExtensionInfoMap* extension_info_map,
[email protected]6a5f77c32011-09-04 19:19:59144 const policy::URLBlacklistManager* url_blacklist_manager,
[email protected]e9c41d22012-08-17 00:08:15145 const ManagedModeURLFilter* managed_mode_url_filter,
[email protected]673514522011-07-13 18:17:18146 void* profile,
[email protected]9c8ae8c2012-03-09 13:13:35147 CookieSettings* cookie_settings,
[email protected]5a07c192012-07-30 20:18:22148 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04149 BooleanPrefMember* enable_do_not_track,
[email protected]a9e0d1412012-08-20 22:13:01150 chrome_browser_net::LoadTimeStats* load_time_stats)
[email protected]3ce02412011-03-01 12:01:15151 : event_router_(event_router),
[email protected]673514522011-07-13 18:17:18152 profile_(profile),
[email protected]9c8ae8c2012-03-09 13:13:35153 cookie_settings_(cookie_settings),
[email protected]c357acb42011-06-09 20:52:42154 extension_info_map_(extension_info_map),
[email protected]6a5f77c32011-09-04 19:19:59155 enable_referrers_(enable_referrers),
[email protected]9d8cfb682012-09-13 16:48:04156 enable_do_not_track_(enable_do_not_track),
[email protected]5a07c192012-07-30 20:18:22157 url_blacklist_manager_(url_blacklist_manager),
[email protected]e9c41d22012-08-17 00:08:15158 managed_mode_url_filter_(managed_mode_url_filter),
[email protected]a9e0d1412012-08-20 22:13:01159 load_time_stats_(load_time_stats) {
[email protected]4b50cb52011-03-10 00:29:37160 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40161 DCHECK(enable_referrers);
[email protected]9c8ae8c2012-03-09 13:13:35162 DCHECK(!profile || cookie_settings);
[email protected]0651b812011-02-24 00:22:50163}
164
[email protected]ac039522010-06-15 16:39:44165ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
166
[email protected]c4a7df82012-08-09 22:48:46167// static
[email protected]a1d4ab072012-06-07 13:21:15168void ChromeNetworkDelegate::NeverThrottleRequests() {
[email protected]c4a7df82012-08-09 22:48:46169 g_never_throttle_requests_ = true;
[email protected]a1d4ab072012-06-07 13:21:15170}
171
[email protected]0a8db0d2011-04-13 15:15:40172// static
[email protected]9d8cfb682012-09-13 16:48:04173void ChromeNetworkDelegate::InitializePrefsOnUIThread(
[email protected]0a8db0d2011-04-13 15:15:40174 BooleanPrefMember* enable_referrers,
[email protected]9d8cfb682012-09-13 16:48:04175 BooleanPrefMember* enable_do_not_track,
[email protected]0a8db0d2011-04-13 15:15:40176 PrefService* pref_service) {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
178 enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL);
179 enable_referrers->MoveToThread(BrowserThread::IO);
[email protected]9d8cfb682012-09-13 16:48:04180 if (enable_do_not_track) {
181 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service, NULL);
182 enable_do_not_track->MoveToThread(BrowserThread::IO);
183 }
[email protected]0a8db0d2011-04-13 15:15:40184}
185
[email protected]4c219e22012-05-05 19:41:04186// static
187void ChromeNetworkDelegate::AllowAccessToAllFiles() {
188 g_allow_file_access_ = true;
189}
190
[email protected]4875ba12011-03-30 22:31:51191int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12192 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47193 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12194 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:34195#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59196 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
197 // blocked. However, an extension might redirect the request to another URL,
198 // which is not blocked.
199 if (url_blacklist_manager_ &&
200 url_blacklist_manager_->IsURLBlocked(request->url())) {
201 // URL access blocked by policy.
[email protected]6a5f77c32011-09-04 19:19:59202 request->net_log().AddEvent(
[email protected]2fa08912012-06-14 20:56:26203 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
204 net::NetLog::StringCallback("url",
205 &request->url().possibly_invalid_spec()));
[email protected]6a5f77c32011-09-04 19:19:59206 return net::ERR_NETWORK_ACCESS_DENIED;
207 }
[email protected]3e598ff12011-09-06 11:22:34208#endif
[email protected]6a5f77c32011-09-04 19:19:59209
[email protected]e9c41d22012-08-17 00:08:15210#if !defined(OS_ANDROID)
211 if (managed_mode_url_filter_ &&
212 !managed_mode_url_filter_->IsURLWhitelisted(request->url())) {
213 // Block for now.
214 return net::ERR_NETWORK_ACCESS_DENIED;
215 }
216#endif
217
[email protected]6baff0b52012-03-06 01:30:18218 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
219
[email protected]0a8db0d2011-04-13 15:15:40220 if (!enable_referrers_->GetValue())
221 request->set_referrer(std::string());
[email protected]9d8cfb682012-09-13 16:48:04222 if (enable_do_not_track_ && enable_do_not_track_->GetValue())
223 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */);
[email protected]05cc4e72011-03-08 21:29:48224 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
[email protected]673514522011-07-13 18:17:18225 profile_, extension_info_map_.get(), request, callback, new_url);
[email protected]d05ef99c2011-02-01 21:38:16226}
227
[email protected]4875ba12011-03-30 22:31:51228int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01229 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47230 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12231 net::HttpRequestHeaders* headers) {
[email protected]4875ba12011-03-30 22:31:51232 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18233 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44234}
[email protected]8202d0c2011-02-23 08:31:14235
[email protected]5796dc942011-07-14 19:26:10236void ChromeNetworkDelegate::OnSendHeaders(
237 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15238 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10239 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
240 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16241}
242
[email protected]ea8141e2011-10-05 13:12:51243int ChromeNetworkDelegate::OnHeadersReceived(
244 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47245 const net::CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32246 const net::HttpResponseHeaders* original_response_headers,
[email protected]ea8141e2011-10-05 13:12:51247 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
248 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
249 profile_, extension_info_map_.get(), request, callback,
250 original_response_headers, override_response_headers);
251}
252
[email protected]31b2e5f2011-04-20 16:58:32253void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
254 const GURL& new_location) {
255 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18256 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32257}
258
259
[email protected]8202d0c2011-02-23 08:31:14260void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]62fecae2011-04-21 11:08:24261 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18262 profile_, extension_info_map_.get(), request);
263 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14264}
265
[email protected]8523ba52011-05-22 19:00:58266void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
267 int bytes_read) {
[email protected]6f4b4b42012-08-27 21:59:07268 performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread(
269 request, bytes_read);
270
[email protected]44879ed2012-04-06 01:11:02271#if defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58272 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
[email protected]44879ed2012-04-06 01:11:02273#endif // defined(ENABLE_TASK_MANAGER)
[email protected]8523ba52011-05-22 19:00:58274}
275
[email protected]9045b8822012-01-13 20:35:35276void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
277 bool started) {
[email protected]2756a8e2012-09-07 18:24:29278 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
[email protected]48944382011-04-23 13:28:16279 bool is_redirect = request->response_headers() &&
280 net::HttpResponseHeaders::IsRedirectResponseCode(
281 request->response_headers()->response_code());
282 if (!is_redirect) {
283 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18284 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16285 }
[email protected]a83dd332011-07-13 10:41:01286 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
287 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04288 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35289 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01290 } else {
291 NOTREACHED();
[email protected]48944382011-04-23 13:28:16292 }
[email protected]673514522011-07-13 18:17:18293 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18294
295 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14296}
[email protected]4b50cb52011-03-10 00:29:37297
[email protected]4875ba12011-03-30 22:31:51298void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
299 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18300 profile_, request);
[email protected]16d5c72b2012-09-14 20:42:37301 if (load_time_stats_)
302 load_time_stats_->OnURLRequestDestroyed(*request);
[email protected]4875ba12011-03-30 22:31:51303}
304
[email protected]82a37672011-05-03 12:02:41305void ChromeNetworkDelegate::OnPACScriptError(int line_number,
306 const string16& error) {
[email protected]c454fe672012-03-12 21:18:01307 extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18308 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41309}
[email protected]7efc582d2011-08-03 20:46:35310
[email protected]c2911d72011-10-03 22:16:36311net::NetworkDelegate::AuthRequiredResponse
312ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35313 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36314 const net::AuthChallengeInfo& auth_info,
315 const AuthCallback& callback,
316 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45317 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
318 profile_, extension_info_map_.get(), request, auth_info,
319 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35320}
[email protected]9c8ae8c2012-03-09 13:13:35321
[email protected]4c219e22012-05-05 19:41:04322bool ChromeNetworkDelegate::OnCanGetCookies(
323 const net::URLRequest& request,
[email protected]9c8ae8c2012-03-09 13:13:35324 const net::CookieList& cookie_list) {
325 // NULL during tests, or when we're running in the system context.
326 if (!cookie_settings_)
327 return true;
328
329 bool allow = cookie_settings_->IsReadingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04330 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35331
332 int render_process_id = -1;
333 int render_view_id = -1;
334 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04335 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35336 BrowserThread::PostTask(
337 BrowserThread::UI, FROM_HERE,
338 base::Bind(&TabSpecificContentSettings::CookiesRead,
339 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04340 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43341 cookie_list, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35342 }
343
344 return allow;
345}
346
[email protected]4c219e22012-05-05 19:41:04347bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
348 const std::string& cookie_line,
349 net::CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35350 // NULL during tests, or when we're running in the system context.
351 if (!cookie_settings_)
352 return true;
353
354 bool allow = cookie_settings_->IsSettingCookieAllowed(
[email protected]4c219e22012-05-05 19:41:04355 request.url(), request.first_party_for_cookies());
[email protected]9c8ae8c2012-03-09 13:13:35356
[email protected]9c8ae8c2012-03-09 13:13:35357 int render_process_id = -1;
358 int render_view_id = -1;
359 if (content::ResourceRequestInfo::GetRenderViewForRequest(
[email protected]4c219e22012-05-05 19:41:04360 &request, &render_process_id, &render_view_id)) {
[email protected]9c8ae8c2012-03-09 13:13:35361 BrowserThread::PostTask(
362 BrowserThread::UI, FROM_HERE,
363 base::Bind(&TabSpecificContentSettings::CookieChanged,
364 render_process_id, render_view_id,
[email protected]4c219e22012-05-05 19:41:04365 request.url(), request.first_party_for_cookies(),
[email protected]fd473d12012-04-05 11:38:43366 cookie_line, *options, !allow));
[email protected]9c8ae8c2012-03-09 13:13:35367 }
368
369 return allow;
370}
[email protected]4c219e22012-05-05 19:41:04371
372bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
373 const FilePath& path) const {
374 if (g_allow_file_access_)
375 return true;
376
[email protected]d8e4f132012-09-06 04:28:05377#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
378 return true;
379#else
[email protected]4c219e22012-05-05 19:41:04380#if defined(OS_CHROMEOS)
[email protected]d8e4f132012-09-06 04:28:05381 // If we're running Chrome for ChromeOS on Linux, we want to allow file
382 // access.
[email protected]288538482012-09-06 21:09:35383 if (!base::chromeos::IsRunningOnChromeOS() ||
384 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) {
[email protected]d8e4f132012-09-06 04:28:05385 return true;
[email protected]288538482012-09-06 21:09:35386 }
[email protected]d8e4f132012-09-06 04:28:05387
388 // Use a whitelist to only allow access to files residing in the list of
389 // directories below.
[email protected]4c219e22012-05-05 19:41:04390 static const char* const kLocalAccessWhiteList[] = {
391 "/home/chronos/user/Downloads",
392 "/home/chronos/user/log",
393 "/media",
394 "/opt/oem",
395 "/usr/share/chromeos-assets",
396 "/tmp",
397 "/var/log",
398 };
[email protected]d8e4f132012-09-06 04:28:05399#elif defined(OS_ANDROID)
400 // Access to files in external storage is allowed.
401 FilePath external_storage_path;
402 PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path);
403 if (external_storage_path.IsParent(path))
[email protected]4c219e22012-05-05 19:41:04404 return true;
405
[email protected]d8e4f132012-09-06 04:28:05406 // Whitelist of other allowed directories.
407 static const char* const kLocalAccessWhiteList[] = {
408 "/sdcard",
409 "/mnt/sdcard",
410 };
411#endif
412
[email protected]4c219e22012-05-05 19:41:04413 for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) {
414 const FilePath white_listed_path(kLocalAccessWhiteList[i]);
415 // FilePath::operator== should probably handle trailing separators.
416 if (white_listed_path == path.StripTrailingSeparators() ||
417 white_listed_path.IsParent(path)) {
418 return true;
419 }
420 }
[email protected]d8e4f132012-09-06 04:28:05421
[email protected]288538482012-09-06 21:09:35422 DVLOG(1) << "File access denied - " << path.value().c_str();
[email protected]4c219e22012-05-05 19:41:04423 return false;
[email protected]d8e4f132012-09-06 04:28:05424#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
[email protected]4c219e22012-05-05 19:41:04425}
[email protected]a1d4ab072012-06-07 13:21:15426
427bool ChromeNetworkDelegate::OnCanThrottleRequest(
428 const net::URLRequest& request) const {
[email protected]c4a7df82012-08-09 22:48:46429 if (g_never_throttle_requests_) {
[email protected]a1d4ab072012-06-07 13:21:15430 return false;
431 }
432
[email protected]c4a7df82012-08-09 22:48:46433 return request.first_party_for_cookies().scheme() ==
[email protected]a1d4ab072012-06-07 13:21:15434 chrome::kExtensionScheme;
435}
[email protected]aa28181e2012-06-13 00:53:58436
437int ChromeNetworkDelegate::OnBeforeSocketStreamConnect(
438 net::SocketStream* socket,
439 const net::CompletionCallback& callback) {
440#if defined(ENABLE_CONFIGURATION_POLICY)
441 if (url_blacklist_manager_ &&
442 url_blacklist_manager_->IsURLBlocked(socket->url())) {
443 // URL access blocked by policy.
[email protected]aa28181e2012-06-13 00:53:58444 socket->net_log()->AddEvent(
[email protected]2fa08912012-06-14 20:56:26445 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST,
446 net::NetLog::StringCallback("url",
447 &socket->url().possibly_invalid_spec()));
[email protected]aa28181e2012-06-13 00:53:58448 return net::ERR_NETWORK_ACCESS_DENIED;
449 }
450#endif
451 return net::OK;
452}
[email protected]5a07c192012-07-30 20:18:22453
[email protected]a9e0d1412012-08-20 22:13:01454void ChromeNetworkDelegate::OnRequestWaitStateChange(
[email protected]5a07c192012-07-30 20:18:22455 const net::URLRequest& request,
[email protected]a9e0d1412012-08-20 22:13:01456 RequestWaitState state) {
457 if (load_time_stats_)
458 load_time_stats_->OnRequestWaitStateChange(request, state);
[email protected]5a07c192012-07-30 20:18:22459}