blob: 6d8a542bceeb64f9522efe8c2bfc08fc03685d29 [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]4b50cb52011-03-10 00:29:378#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]8202d0c2011-02-23 08:31:1410#include "chrome/browser/extensions/extension_proxy_api.h"
[email protected]d05ef99c2011-02-01 21:38:1611#include "chrome/browser/extensions/extension_webrequest_api.h"
[email protected]0a8db0d2011-04-13 15:15:4012#include "chrome/browser/prefs/pref_member.h"
13#include "chrome/common/pref_names.h"
14#include "content/browser/browser_thread.h"
[email protected]82b42302011-04-20 16:28:1615#include "net/base/host_port_pair.h"
[email protected]8202d0c2011-02-23 08:31:1416#include "net/base/net_errors.h"
[email protected]ac039522010-06-15 16:39:4417#include "net/http/http_request_headers.h"
[email protected]48944382011-04-23 13:28:1618#include "net/http/http_response_headers.h"
[email protected]d05ef99c2011-02-01 21:38:1619#include "net/url_request/url_request.h"
20
21namespace {
22
[email protected]8202d0c2011-02-23 08:31:1423// If the |request| failed due to problems with a proxy, forward the error to
24// the proxy extension API.
[email protected]0651b812011-02-24 00:22:5025void ForwardProxyErrors(net::URLRequest* request,
[email protected]3ce02412011-03-01 12:01:1526 ExtensionEventRouterForwarder* event_router,
27 ProfileId profile_id) {
[email protected]8202d0c2011-02-23 08:31:1428 if (request->status().status() == net::URLRequestStatus::FAILED) {
29 switch (request->status().os_error()) {
30 case net::ERR_PROXY_AUTH_UNSUPPORTED:
31 case net::ERR_PROXY_CONNECTION_FAILED:
32 case net::ERR_TUNNEL_CONNECTION_FAILED:
33 ExtensionProxyEventRouter::GetInstance()->OnProxyError(
[email protected]3ce02412011-03-01 12:01:1534 event_router, profile_id, request->status().os_error());
[email protected]8202d0c2011-02-23 08:31:1435 }
36 }
37}
38
[email protected]d05ef99c2011-02-01 21:38:1639} // namespace
[email protected]ac039522010-06-15 16:39:4440
[email protected]0651b812011-02-24 00:22:5041ChromeNetworkDelegate::ChromeNetworkDelegate(
[email protected]3ce02412011-03-01 12:01:1542 ExtensionEventRouterForwarder* event_router,
[email protected]4b50cb52011-03-10 00:29:3743 ProfileId profile_id,
[email protected]0a8db0d2011-04-13 15:15:4044 BooleanPrefMember* enable_referrers,
[email protected]4b50cb52011-03-10 00:29:3745 ProtocolHandlerRegistry* protocol_handler_registry)
[email protected]3ce02412011-03-01 12:01:1546 : event_router_(event_router),
[email protected]4b50cb52011-03-10 00:29:3747 profile_id_(profile_id),
[email protected]0a8db0d2011-04-13 15:15:4048 enable_referrers_(enable_referrers),
[email protected]4b50cb52011-03-10 00:29:3749 protocol_handler_registry_(protocol_handler_registry) {
50 DCHECK(event_router);
[email protected]0a8db0d2011-04-13 15:15:4051 DCHECK(enable_referrers);
[email protected]0651b812011-02-24 00:22:5052}
53
[email protected]ac039522010-06-15 16:39:4454ChromeNetworkDelegate::~ChromeNetworkDelegate() {}
55
[email protected]0a8db0d2011-04-13 15:15:4056// static
57void ChromeNetworkDelegate::InitializeReferrersEnabled(
58 BooleanPrefMember* enable_referrers,
59 PrefService* pref_service) {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
61 enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL);
62 enable_referrers->MoveToThread(BrowserThread::IO);
63}
64
[email protected]4875ba12011-03-30 22:31:5165int ChromeNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:1266 net::URLRequest* request,
67 net::CompletionCallback* callback,
68 GURL* new_url) {
[email protected]0a8db0d2011-04-13 15:15:4069 if (!enable_referrers_->GetValue())
70 request->set_referrer(std::string());
[email protected]05cc4e72011-03-08 21:29:4871 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
[email protected]4c76d7c2011-04-15 19:14:1272 profile_id_, event_router_.get(), request, callback, new_url);
[email protected]d05ef99c2011-02-01 21:38:1673}
74
[email protected]4875ba12011-03-30 22:31:5175int ChromeNetworkDelegate::OnBeforeSendHeaders(
76 uint64 request_id,
[email protected]4c76d7c2011-04-15 19:14:1277 net::CompletionCallback* callback,
78 net::HttpRequestHeaders* headers) {
[email protected]4875ba12011-03-30 22:31:5179 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
[email protected]4c76d7c2011-04-15 19:14:1280 profile_id_, event_router_.get(), request_id, callback, headers);
[email protected]ac039522010-06-15 16:39:4481}
[email protected]8202d0c2011-02-23 08:31:1482
[email protected]82b42302011-04-20 16:28:1683void ChromeNetworkDelegate::OnRequestSent(
84 uint64 request_id,
85 const net::HostPortPair& socket_address) {
86 ExtensionWebRequestEventRouter::GetInstance()->OnRequestSent(
87 profile_id_, event_router_.get(), request_id, socket_address);
88}
89
[email protected]31b2e5f2011-04-20 16:58:3290void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
91 const GURL& new_location) {
92 ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
93 profile_id_, event_router_.get(), request, new_location);
94}
95
96
[email protected]8202d0c2011-02-23 08:31:1497void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]62fecae2011-04-21 11:08:2498 ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
99 profile_id_, event_router_.get(), request);
[email protected]3ce02412011-03-01 12:01:15100 ForwardProxyErrors(request, event_router_.get(), profile_id_);
[email protected]8202d0c2011-02-23 08:31:14101}
102
[email protected]48944382011-04-23 13:28:16103void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request) {
104 if (request->status().status() == net::URLRequestStatus::SUCCESS) {
105 bool is_redirect = request->response_headers() &&
106 net::HttpResponseHeaders::IsRedirectResponseCode(
107 request->response_headers()->response_code());
108 if (!is_redirect) {
109 ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
110 profile_id_, event_router_.get(), request);
111 }
[email protected]05b6ab42011-04-23 13:46:04112 } else if (request->status().status() == net::URLRequestStatus::FAILED) {
113 ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
114 profile_id_, event_router_.get(), request);
[email protected]48944382011-04-23 13:28:16115 }
[email protected]3ce02412011-03-01 12:01:15116 ForwardProxyErrors(request, event_router_.get(), profile_id_);
[email protected]8202d0c2011-02-23 08:31:14117}
[email protected]4b50cb52011-03-10 00:29:37118
[email protected]4875ba12011-03-30 22:31:51119void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) {
120 ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
121 profile_id_, request);
122}
123
[email protected]5aa20132011-04-27 23:11:34124void ChromeNetworkDelegate::OnHttpTransactionDestroyed(uint64 request_id) {
125 ExtensionWebRequestEventRouter::GetInstance()->OnHttpTransactionDestroyed(
126 profile_id_, request_id);
127}
128
[email protected]4b50cb52011-03-10 00:29:37129net::URLRequestJob* ChromeNetworkDelegate::OnMaybeCreateURLRequestJob(
130 net::URLRequest* request) {
131 if (!protocol_handler_registry_)
132 return NULL;
133 return protocol_handler_registry_->MaybeCreateJob(request);
134}
[email protected]82a37672011-05-03 12:02:41135
136void ChromeNetworkDelegate::OnPACScriptError(int line_number,
137 const string16& error) {
[email protected]3daf92302011-05-05 15:04:31138 ExtensionProxyEventRouter::GetInstance()->OnPACScriptError(
139 event_router_.get(), profile_id_, line_number, error);
[email protected]82a37672011-05-03 12:02:41140}