blob: 2bc9977e30b4167129f77935810d1534f3c8661b [file] [log] [blame]
[email protected]8202d0c2011-02-23 08:31:141// Copyright (c) 2011 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]8523ba52011-05-22 19:00:588#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]3ce02412011-03-01 12:01:159#include "chrome/browser/extensions/extension_event_router_forwarder.h"
[email protected]c357acb42011-06-09 20:52:4210#include "chrome/browser/extensions/extension_info_map.h"
[email protected]8202d0c2011-02-23 08:31:1411#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]d05ef99c2011-02-01 21:38:1612#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]0a8db0d2011-04-13 15:15:4013#include "chrome/browser/prefs/pref_member.h"
[email protected]8523ba52011-05-22 19:00:5814#include "chrome/browser/task_manager/task_manager.h"
[email protected]0a8db0d2011-04-13 15:15:4015#include "chrome/common/pref_names.h"
[email protected]c38831a12011-10-28 12:44:4916#include "content/public/browser/browser_thread.h"
[email protected]82b42302011-04-20 16:28:1617#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1418#include "net/base/net_errors.h"
[email protected]6a5f77c32011-09-04 19:19:5919#include "net/base/net_log.h"
[email protected]ac039522010-06-15 16:39:4420#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1621#include "net/http/http_response_headers.h"
[email protected]d05ef99c2011-02-01 21:38:1622#include "net/url_request/url_request.h"
23
[email protected]3e598ff12011-09-06 11:22:3424#if defined(ENABLE_CONFIGURATION_POLICY)
25#include "chrome/browser/policy/url_blacklist_manager.h"
26#endif
27
[email protected]631bb742011-11-02 11:29:3928using content::BrowserThread;
29
[email protected]d05ef99c2011-02-01 21:38:1630namespace {
31
[email protected]8202d0c2011-02-23 08:31:1432// If the |request| failed due to problems with a proxy, forward the error to
33// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5034void ForwardProxyErrors(net::URLRequest* request,
[email protected]3ce02412011-03-01 12:01:1535 ExtensionEventRouterForwarder* event_router,
[email protected]673514522011-07-13 18:17:1836 void* profile) {
[email protected]8202d0c2011-02-23 08:31:1437 if (request->status().status() == net::URLRequestStatus::FAILED) {
[email protected]d0cc35b2011-09-08 12:02:0538 switch (request->status().error()) {
[email protected]8202d0c2011-02-23 08:31:1439 case net::ERR_PROXY_AUTH_UNSUPPORTED:
40 case net::ERR_PROXY_CONNECTION_FAILED:
41 case net::ERR_TUNNEL_CONNECTION_FAILED:
42 ExtensionProxyEventRouter::GetInstance()->OnProxyError(
[email protected]d0cc35b2011-09-08 12:02:0543 event_router, profile, request->status().error());
[email protected]8202d0c2011-02-23 08:31:1444 }
45 }
46}
47
[email protected]d05ef99c2011-02-01 21:38:1648} // namespace
[email protected]ac039522010-06-15 16:39:4449
[email protected]0651b812011-02-24 00:22:5050ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]3ce02412011-03-01 12:01:1551 ExtensionEventRouterForwarder* event_router,
[email protected]c357acb42011-06-09 20:52:4252 ExtensionInfoMap* extension_info_map,
[email protected]6a5f77c32011-09-04 19:19:5953 const policy::URLBlacklistManager* url_blacklist_manager,
[email protected]673514522011-07-13 18:17:1854 void* profile,
[email protected]a8c1e7452011-05-14 06:17:0755 BooleanPrefMember* enable_referrers)
[email protected]3ce02412011-03-01 12:01:1556 : event_router_(event_router),
[email protected]673514522011-07-13 18:17:1857 profile_(profile),
[email protected]c357acb42011-06-09 20:52:4258 extension_info_map_(extension_info_map),
[email protected]6a5f77c32011-09-04 19:19:5959 enable_referrers_(enable_referrers),
60 url_blacklist_manager_(url_blacklist_manager) {
[email protected]4b50cb52011-03-10 00:29:3761 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:4062 DCHECK(enable_referrers);
[email protected]0651b812011-02-24 00:22:5063}
64
[email protected]ac039522010-06-15 16:39:4465ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
66
[email protected]0a8db0d2011-04-13 15:15:4067// static
68void ChromeNetworkDelegate::InitializeReferrersEnabled(
69 BooleanPrefMember* enable_referrers,
70 PrefService* pref_service) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
72 enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL);
73 enable_referrers->MoveToThread(BrowserThread::IO);
74}
75
[email protected]4875ba12011-03-30 22:31:5176int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:1277 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:4778 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:1279 GURL* new_url) {
[email protected]3e598ff12011-09-06 11:22:3480#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]6a5f77c32011-09-04 19:19:5981 // TODO(joaodasilva): This prevents extensions from seeing URLs that are
82 // blocked. However, an extension might redirect the request to another URL,
83 // which is not blocked.
84 if (url_blacklist_manager_ &&
85 url_blacklist_manager_->IsURLBlocked(request->url())) {
86 // URL access blocked by policy.
87 scoped_refptr<net::NetLog::EventParameters> params;
88 params = new net::NetLogStringParameter("url", request->url().spec());
89 request->net_log().AddEvent(
90 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST, params);
91 return net::ERR_NETWORK_ACCESS_DENIED;
92 }
[email protected]3e598ff12011-09-06 11:22:3493#endif
[email protected]6a5f77c32011-09-04 19:19:5994
[email protected]0a8db0d2011-04-13 15:15:4095 if (!enable_referrers_->GetValue())
96 request->set_referrer(std::string());
[email protected]05cc4e72011-03-08 21:29:4897 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
[email protected]673514522011-07-13 18:17:1898 profile_, extension_info_map_.get(), request, callback, new_url);
[email protected]d05ef99c2011-02-01 21:38:1699}
100
[email protected]4875ba12011-03-30 22:31:51101int ChromeNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01102 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47103 const net::CompletionCallback& callback,
[email protected]4c76d7c2011-04-15 19:14:12104 net::HttpRequestHeaders* headers) {
[email protected]4875ba12011-03-30 22:31:51105 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]673514522011-07-13 18:17:18106 profile_, extension_info_map_.get(), request, callback, headers);
[email protected]ac039522010-06-15 16:39:44107}
[email protected]8202d0c2011-02-23 08:31:14108
[email protected]5796dc942011-07-14 19:26:10109void ChromeNetworkDelegate::OnSendHeaders(
110 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15111 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10112 ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
113 profile_, extension_info_map_.get(), request, headers);
[email protected]82b42302011-04-20 16:28:16114}
115
[email protected]ea8141e2011-10-05 13:12:51116int ChromeNetworkDelegate::OnHeadersReceived(
117 net::URLRequest* request,
[email protected]084262c2011-12-01 21:12:47118 const net::CompletionCallback& callback,
[email protected]ea8141e2011-10-05 13:12:51119 net::HttpResponseHeaders* original_response_headers,
120 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
121 return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
122 profile_, extension_info_map_.get(), request, callback,
123 original_response_headers, override_response_headers);
124}
125
[email protected]31b2e5f2011-04-20 16:58:32126void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
127 const GURL& new_location) {
128 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
[email protected]673514522011-07-13 18:17:18129 profile_, extension_info_map_.get(), request, new_location);
[email protected]31b2e5f2011-04-20 16:58:32130}
131
132
[email protected]8202d0c2011-02-23 08:31:14133void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]62fecae2011-04-21 11:08:24134 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
[email protected]673514522011-07-13 18:17:18135 profile_, extension_info_map_.get(), request);
136 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14137}
138
[email protected]8523ba52011-05-22 19:00:58139void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
140 int bytes_read) {
141 TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read);
142}
143
[email protected]48944382011-04-23 13:28:16144void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request) {
[email protected]a83dd332011-07-13 10:41:01145 if (request->status().status() == net::URLRequestStatus::SUCCESS ||
146 request->status().status() == net::URLRequestStatus::HANDLED_EXTERNALLY) {
[email protected]48944382011-04-23 13:28:16147 bool is_redirect = request->response_headers() &&
148 net::HttpResponseHeaders::IsRedirectResponseCode(
149 request->response_headers()->response_code());
150 if (!is_redirect) {
151 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
[email protected]673514522011-07-13 18:17:18152 profile_, extension_info_map_.get(), request);
[email protected]48944382011-04-23 13:28:16153 }
[email protected]a83dd332011-07-13 10:41:01154 } else if (request->status().status() == net::URLRequestStatus::FAILED ||
155 request->status().status() == net::URLRequestStatus::CANCELED) {
[email protected]05b6ab42011-04-23 13:46:04156 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
[email protected]96dcb1ce2011-07-13 18:24:35157 profile_, extension_info_map_.get(), request);
[email protected]a83dd332011-07-13 10:41:01158 } else {
159 NOTREACHED();
[email protected]48944382011-04-23 13:28:16160 }
[email protected]673514522011-07-13 18:17:18161 ForwardProxyErrors(request, event_router_.get(), profile_);
[email protected]8202d0c2011-02-23 08:31:14162}
[email protected]4b50cb52011-03-10 00:29:37163
[email protected]4875ba12011-03-30 22:31:51164void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
165 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
[email protected]673514522011-07-13 18:17:18166 profile_, request);
[email protected]4875ba12011-03-30 22:31:51167}
168
[email protected]82a37672011-05-03 12:02:41169void ChromeNetworkDelegate::OnPACScriptError(int line_number,
170 const string16& error) {
[email protected]3daf92302011-05-05 15:04:31171 ExtensionProxyEventRouter::GetInstance()->OnPACScriptError(
[email protected]673514522011-07-13 18:17:18172 event_router_.get(), profile_, line_number, error);
[email protected]82a37672011-05-03 12:02:41173}
[email protected]7efc582d2011-08-03 20:46:35174
[email protected]c2911d72011-10-03 22:16:36175net::NetworkDelegate::AuthRequiredResponse
176ChromeNetworkDelegate::OnAuthRequired(
[email protected]7efc582d2011-08-03 20:46:35177 net::URLRequest* request,
[email protected]c2911d72011-10-03 22:16:36178 const net::AuthChallengeInfo& auth_info,
179 const AuthCallback& callback,
180 net::AuthCredentials* credentials) {
[email protected]90449ab2011-10-11 15:36:45181 return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
182 profile_, extension_info_map_.get(), request, auth_info,
183 callback, credentials);
[email protected]7efc582d2011-08-03 20:46:35184}