blob: ea86b3d895eb263a30b3944cfeba16d75a2f4cfe [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]6baff0b52012-03-06 01:30:188#include "chrome/browser/browser_process.h"
[email protected]9c8ae8c2012-03-09 13:13:359#include "chrome/browser/content_settings/cookie_settings.h"
10#include "chrome/browser/content_settings/tab_specific_content_settings.h"
[email protected]8523ba52011-05-22 19:00:5811#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]6e672df2012-01-25 10:39:4712#include "chrome/browser/extensions/api/webrequest/webrequest_api.h"
[email protected]3ce02412011-03-01 12:01:1513#include "chrome/browser/extensions/extension_event_router_forwarder.h"
[email protected]c357acb42011-06-09 20:52:4214#include "chrome/browser/extensions/extension_info_map.h"
[email protected]6baff0b52012-03-06 01:30:1815#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8202d0c2011-02-23 08:31:1416#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]0a8db0d2011-04-13 15:15:4017#include "chrome/browser/prefs/pref_member.h"
[email protected]6baff0b52012-03-06 01:30:1818#include "chrome/browser/profiles/profile_manager.h"
[email protected]8523ba52011-05-22 19:00:5819#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4020#include "chrome/common/pref_names.h"
[email protected]6baff0b52012-03-06 01:30:1821#include "content/browser/renderer_host/resource_dispatcher_host.h"
[email protected]c38831a12011-10-28 12:44:4922#include "content/public/browser/browser_thread.h"
[email protected]9c1662b2012-03-06 15:44:3323#include "content/public/browser/render_view_host.h"
[email protected]9c8ae8c2012-03-09 13:13:3524#include "content/public/browser/resource_request_info.h"
25#include "net/base/cookie_monster.h"
[email protected]82b42302011-04-20 16:28:1626#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1427#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5928#include "net/base/net_log.h"
[email protected]ac039522010-06-15 16:39:4429#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1630#include "net/http/http_response_headers.h"
[email protected]d05ef99c2011-02-01 21:38:1631#include "net/url_request/url_request.h"
32
[email protected]3e598ff12011-09-06 11:22:3433#if defined(ENABLE_CONFIGURATION_POLICY)
34#include "chrome/browser/policy/url_blacklist_manager.h"
35#endif
36
[email protected]631bb742011-11-02 11:29:3937using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1138using content::RenderViewHost;
[email protected]631bb742011-11-02 11:29:3939
[email protected]d05ef99c2011-02-01 21:38:1640namespace {
41
[email protected]8202d0c2011-02-23 08:31:1442// If the |request| failed due to problems with a proxy, forward the error to
43// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5044void ForwardProxyErrors(net::URLRequest* request,
[email protected]3ce02412011-03-01 12:01:1545 ExtensionEventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1846 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1447 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0548 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1449 case net::ERR_PROXY_AUTH_UNSUPPORTED:
50 case net::ERR_PROXY_CONNECTION_FAILED:
51 case net::ERR_TUNNEL_CONNECTION_FAILED:
52 ExtensionProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:0553 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:1454 }
55 }
56}
57
[email protected]6baff0b52012-03-06 01:30:1858enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
59
60// Notifies the ExtensionProcessManager that a request has started or stopped
61// for a particular RenderView.
62void NotifyEPMRequestStatus(RequestStatus status,
63 void* profile_id,
64 int process_id,
65 int render_view_id) {
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67 Profile* profile = reinterpret_cast<Profile*>(profile_id);
68 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
69 return;
70
71 RenderViewHost* render_view_host =
72 RenderViewHost::FromID(process_id, render_view_id);
73 // Will be NULL if the request was not issued on behalf of a renderer (e.g. a
74 // system-level request).
75 if (render_view_host) {
76 if (status == REQUEST_STARTED) {
77 profile->GetExtensionProcessManager()->OnNetworkRequestStarted(
78 render_view_host);
79 } else if (status == REQUEST_DONE) {
80 profile->GetExtensionProcessManager()->OnNetworkRequestDone(
81 render_view_host);
82 } else {
83 NOTREACHED();
84 }
85 }
86}
87
88void ForwardRequestStatus(
89 RequestStatus status, net::URLRequest* request, void* profile_id) {
90 int process_id, render_view_id;
91 if (ResourceDispatcherHost::RenderViewForRequest(
92 request, &process_id, &render_view_id)) {
93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
94 base::Bind(&NotifyEPMRequestStatus,
95 status, profile_id, process_id, render_view_id));
96 }
97}
98
[email protected]d05ef99c2011-02-01 21:38:1699} // namespace
[email protected]ac039522010-06-15 16:39:44100
[email protected]0651b812011-02-24 00:22:50101ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]3ce02412011-03-01 12:01:15102 ExtensionEventRouterForwarder* event_router,
[email protected]c357acb42011-06-09 20:52:42103 ExtensionInfoMap* extension_info_map,
[email protected]6a5f77c32011-09-04 19:19:59104 const policy::URLBlacklistManager* url_blacklist_manager,
[email protected]673514522011-07-13 18:17:18105 void* profile,
[email protected]9c8ae8c2012-03-09 13:13:35106 CookieSettings* cookie_settings,
[email protected]a8c1e7452011-05-14 06:17:07107 BooleanPrefMember* enable_referrers)
[email protected]3ce02412011-03-01 12:01:15108 : event_router_(event_router),
[email protected]673514522011-07-13 18:17:18109 profile_(profile),
[email protected]9c8ae8c2012-03-09 13:13:35110 cookie_settings_(cookie_settings),
[email protected]c357acb42011-06-09 20:52:42111 extension_info_map_(extension_info_map),
[email protected]6a5f77c32011-09-04 19:19:59112 enable_referrers_(enable_referrers),
113 url_blacklist_manager_(url_blacklist_manager) {
[email protected]4b50cb52011-03-10 00:29:37114 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:40115 DCHECK(enable_referrers);
[email protected]9c8ae8c2012-03-09 13:13:35116 DCHECK(!profile || cookie_settings);
[email protected]0651b812011-02-24 00:22:50117}
118
[email protected]ac039522010-06-15 16:39:44119ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
120
[email protected]0a8db0d2011-04-13 15:15:40121// static
122void ChromeNetworkDelegate::InitializeReferrersEnabled(
123 BooleanPrefMember* enable_referrers,
124 PrefService* pref_service) {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126 enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL);
127 enable_referrers->MoveToThread(BrowserThread::IO);
128}
129
[email protected]4875ba12011-03-30 22:31:51130int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12131 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47132 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12133 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:34134#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:59135 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
136 // blocked. However, an extension might redirect the request to another URL,
137 // which is not blocked.
138 if (url_blacklist_manager_ &&
139 url_blacklist_manager_->IsURLBlocked(request->url())) {
140 // URL access blocked by policy.
141 scoped_refptr<net::NetLog::EventParameters> params;
142 params = new net::NetLogStringParameter("url", request->url().spec());
143 request->net_log().AddEvent(
144 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST, params);
145 return net::ERR_NETWORK_ACCESS_DENIED;
146 }
[email protected]3e598ff12011-09-06 11:22:34147#endif
[email protected]6a5f77c32011-09-04 19:19:59148
[email protected]6baff0b52012-03-06 01:30:18149 ForwardRequestStatus(REQUEST_STARTED, request, profile_);
150
[email protected]0a8db0d2011-04-13 15:15:40151 if (!enable_referrers_->GetValue())
152 request->set_referrer(std::string());
[email protected]05cc4e72011-03-08 21:29:48153 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
[email protected]673514522011-07-13 18:17:18154 profile_, extension_info_map_.get(), request, callback, new_url);
[email protected]d05ef99c2011-02-01 21:38:16155}
156
[email protected]4875ba12011-03-30 22:31:51157int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01158 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47159 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12160 net::HttpRequestHeaders* headers) {
[email protected]4875ba12011-03-30 22:31:51161 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18162 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44163}
[email protected]8202d0c2011-02-23 08:31:14164
[email protected]5796dc942011-07-14 19:26:10165void ChromeNetworkDelegate::OnSendHeaders(
166 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15167 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10168 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
169 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16170}
171
[email protected]ea8141e2011-10-05 13:12:51172int ChromeNetworkDelegate::OnHeadersReceived(
173 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47174 const net::CompletionCallback& callback,
[email protected]ea8141e2011-10-05 13:12:51175 net::HttpResponseHeaders* original_response_headers,
176 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
177 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
178 profile_, extension_info_map_.get(), request, callback,
179 original_response_headers, override_response_headers);
180}
181
[email protected]31b2e5f2011-04-20 16:58:32182void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
183 const GURL& new_location) {
184 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18185 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32186}
187
188
[email protected]8202d0c2011-02-23 08:31:14189void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]62fecae2011-04-21 11:08:24190 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18191 profile_, extension_info_map_.get(), request);
192 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14193}
194
[email protected]8523ba52011-05-22 19:00:58195void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
196 int bytes_read) {
197 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
198}
199
[email protected]9045b8822012-01-13 20:35:35200void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
201 bool started) {
[email protected]a83dd332011-07-13 10:41:01202 if (request->status().status() == net::URLRequestStatus::SUCCESS ||
203 request->status().status() == net::URLRequestStatus::HANDLED_EXTERNALLY) {
[email protected]48944382011-04-23 13:28:16204 bool is_redirect = request->response_headers() &&
205 net::HttpResponseHeaders::IsRedirectResponseCode(
206 request->response_headers()->response_code());
207 if (!is_redirect) {
208 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18209 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16210 }
[email protected]a83dd332011-07-13 10:41:01211 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
212 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04213 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]9045b8822012-01-13 20:35:35214 profile_, extension_info_map_.get(), request, started);
[email protected]a83dd332011-07-13 10:41:01215 } else {
216 NOTREACHED();
[email protected]48944382011-04-23 13:28:16217 }
[email protected]673514522011-07-13 18:17:18218 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]6baff0b52012-03-06 01:30:18219
220 ForwardRequestStatus(REQUEST_DONE, request, profile_);
[email protected]8202d0c2011-02-23 08:31:14221}
[email protected]4b50cb52011-03-10 00:29:37222
[email protected]4875ba12011-03-30 22:31:51223void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
224 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18225 profile_, request);
[email protected]4875ba12011-03-30 22:31:51226}
227
[email protected]82a37672011-05-03 12:02:41228void ChromeNetworkDelegate::OnPACScriptError(int line_number,
229 const string16& error) {
[email protected]3daf92302011-05-05 15:04:31230 ExtensionProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18231 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41232}
[email protected]7efc582d2011-08-03 20:46:35233
[email protected]c2911d72011-10-03 22:16:36234net::NetworkDelegate::AuthRequiredResponse
235ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35236 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36237 const net::AuthChallengeInfo& auth_info,
238 const AuthCallback& callback,
239 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45240 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
241 profile_, extension_info_map_.get(), request, auth_info,
242 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35243}
[email protected]9c8ae8c2012-03-09 13:13:35244
245bool ChromeNetworkDelegate::CanGetCookies(
246 const net::URLRequest* request,
247 const net::CookieList& cookie_list) {
248 // NULL during tests, or when we're running in the system context.
249 if (!cookie_settings_)
250 return true;
251
252 bool allow = cookie_settings_->IsReadingCookieAllowed(
253 request->url(), request->first_party_for_cookies());
254
255 int render_process_id = -1;
256 int render_view_id = -1;
257 if (content::ResourceRequestInfo::GetRenderViewForRequest(
258 request, &render_process_id, &render_view_id)) {
259 BrowserThread::PostTask(
260 BrowserThread::UI, FROM_HERE,
261 base::Bind(&TabSpecificContentSettings::CookiesRead,
262 render_process_id, render_view_id,
263 request->url(), cookie_list, !allow));
264 }
265
266 return allow;
267}
268
269bool ChromeNetworkDelegate::CanSetCookie(
270 const net::URLRequest* request,
271 const std::string& cookie_line,
272 net::CookieOptions* options) {
273 // NULL during tests, or when we're running in the system context.
274 if (!cookie_settings_)
275 return true;
276
277 bool allow = cookie_settings_->IsSettingCookieAllowed(
278 request->url(), request->first_party_for_cookies());
279
280 if (cookie_settings_->IsCookieSessionOnly(request->url()))
281 options->set_force_session();
282
283 int render_process_id = -1;
284 int render_view_id = -1;
285 if (content::ResourceRequestInfo::GetRenderViewForRequest(
286 request, &render_process_id, &render_view_id)) {
287 BrowserThread::PostTask(
288 BrowserThread::UI, FROM_HERE,
289 base::Bind(&TabSpecificContentSettings::CookieChanged,
290 render_process_id, render_view_id,
291 request->url(), cookie_line, *options, !allow));
292 }
293
294 return allow;
295}