From 33b574b43446fc8bf45e300901f6b6bc0b15cc19 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Wed, 17 Jul 2013 10:21:33 -0400 Subject: [PATCH] Allow clients to supply their own NetworkDelegate implementation --- brightray/browser/browser_context.cc | 6 ++++++ brightray/browser/browser_context.h | 4 ++++ brightray/browser/network_delegate.h | 5 +++-- brightray/browser/url_request_context_getter.cc | 5 +++-- brightray/browser/url_request_context_getter.h | 6 ++++-- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index 17ff9b2269cf..42074e74ce3f 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -5,6 +5,7 @@ #include "browser_context.h" #include "browser/inspectable_web_contents_impl.h" +#include "browser/network_delegate.h" #include "common/application_info.h" #include "base/files/file_path.h" @@ -66,11 +67,16 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext(content::Prot GetPath(), content::BrowserThread::UnsafeGetMessageLoopForThread(content::BrowserThread::IO), content::BrowserThread::UnsafeGetMessageLoopForThread(content::BrowserThread::FILE), + CreateNetworkDelegate().Pass(), protocol_handlers); resource_context_->set_url_request_context_getter(url_request_getter_.get()); return url_request_getter_.get(); } +scoped_ptr BrowserContext::CreateNetworkDelegate() { + return make_scoped_ptr(new NetworkDelegate).Pass(); +} + base::FilePath BrowserContext::GetPath() { if (!path_.empty()) return path_; diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index d814be5cdfc6..4307df0aa3b4 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -13,6 +13,7 @@ class PrefService; namespace brightray { +class NetworkDelegate; class URLRequestContextGetter; class BrowserContext : public content::BrowserContext { @@ -28,6 +29,9 @@ protected: // Subclasses should override this to register custom preferences. virtual void RegisterPrefs(PrefRegistrySimple*) {} + // Subclasses should override this to provide a custom NetworkDelegate implementation. + virtual scoped_ptr CreateNetworkDelegate(); + virtual base::FilePath GetPath() OVERRIDE; private: diff --git a/brightray/browser/network_delegate.h b/brightray/browser/network_delegate.h index 1d2645532f41..aff8e2288563 100644 --- a/brightray/browser/network_delegate.h +++ b/brightray/browser/network_delegate.h @@ -10,11 +10,11 @@ namespace brightray { class NetworkDelegate : public net::NetworkDelegate { -public: + public: NetworkDelegate(); virtual ~NetworkDelegate(); -private: + protected: virtual int OnBeforeURLRequest(net::URLRequest* request, const net::CompletionCallback& callback, GURL* new_url) OVERRIDE; virtual int OnBeforeSendHeaders(net::URLRequest* request, const net::CompletionCallback& callback, net::HttpRequestHeaders* headers) OVERRIDE; virtual void OnSendHeaders(net::URLRequest* request, const net::HttpRequestHeaders& headers) OVERRIDE; @@ -33,6 +33,7 @@ private: virtual int OnBeforeSocketStreamConnect(net::SocketStream* stream, const net::CompletionCallback& callback) OVERRIDE; virtual void OnRequestWaitStateChange(const net::URLRequest& request, RequestWaitState state) OVERRIDE; + private: DISALLOW_COPY_AND_ASSIGN(NetworkDelegate); }; diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 83a986e495e4..e6d73a608f36 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -32,10 +32,12 @@ URLRequestContextGetter::URLRequestContextGetter( const base::FilePath& base_path, MessageLoop* io_loop, MessageLoop* file_loop, + scoped_ptr network_delegate, content::ProtocolHandlerMap* protocol_handlers) : base_path_(base_path), io_loop_(io_loop), - file_loop_(file_loop) { + file_loop_(file_loop), + network_delegate_(network_delegate.Pass()) { // Must first be created on the UI thread. DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); @@ -57,7 +59,6 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() if (!url_request_context_.get()) { url_request_context_.reset(new net::URLRequestContext()); - network_delegate_.reset(new NetworkDelegate); url_request_context_->set_network_delegate(network_delegate_.get()); storage_.reset( new net::URLRequestContextStorage(url_request_context_.get())); diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index 026778e4dc55..7d4efb6c00d4 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -16,19 +16,21 @@ class MessageLoop; namespace net { class HostResolver; -class NetworkDelegate; class ProxyConfigService; class URLRequestContextStorage; } namespace brightray { +class NetworkDelegate; + class URLRequestContextGetter : public net::URLRequestContextGetter { public: URLRequestContextGetter( const base::FilePath& base_path, base::MessageLoop* io_loop, base::MessageLoop* file_loop, + scoped_ptr, content::ProtocolHandlerMap*); virtual ~URLRequestContextGetter(); @@ -43,7 +45,7 @@ private: base::MessageLoop* file_loop_; scoped_ptr proxy_config_service_; - scoped_ptr network_delegate_; + scoped_ptr network_delegate_; scoped_ptr storage_; scoped_ptr url_request_context_; content::ProtocolHandlerMap protocol_handlers_;