From c30f11f38c632946a495198ec110e4e97edbffe2 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 20 Aug 2014 14:39:09 +0800 Subject: [PATCH] Add Delegate for URLRequestContextGetter. --- brightray/browser/browser_context.cc | 11 +++---- brightray/browser/browser_context.h | 17 ++++------ .../browser/url_request_context_getter.cc | 12 +++---- .../browser/url_request_context_getter.h | 32 +++++++++++-------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index 8a5e63e8c74f..3396531ed35d 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -104,25 +104,24 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext( content::ProtocolHandlerScopedVector protocol_interceptors) { DCHECK(!url_request_getter_); url_request_getter_ = new URLRequestContextGetter( + this, GetPath(), BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO), BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE), - base::Bind(&BrowserContext::CreateNetworkDelegate, base::Unretained(this)), - base::Bind(&BrowserContext::CreateURLRequestJobFactory, base::Unretained(this)), protocol_handlers, protocol_interceptors.Pass()); 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(); +net::NetworkDelegate* BrowserContext::CreateNetworkDelegate() { + return new NetworkDelegate; } -scoped_ptr BrowserContext::CreateURLRequestJobFactory( +net::URLRequestJobFactory* BrowserContext::CreateURLRequestJobFactory( content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerScopedVector* protocol_interceptors) { - return scoped_ptr(); + return NULL; } base::FilePath BrowserContext::GetPath() const { diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index 486a9eda9036..6bd2a1b81d9c 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -8,7 +8,6 @@ #include "browser/url_request_context_getter.h" #include "content/public/browser/browser_context.h" -#include "content/public/browser/content_browser_client.h" class PrefRegistrySimple; class PrefService; @@ -16,9 +15,9 @@ class PrefService; namespace brightray { class DownloadManagerDelegate; -class NetworkDelegate; -class BrowserContext : public content::BrowserContext { +class BrowserContext : public content::BrowserContext, + public brightray::URLRequestContextGetter::Delegate { public: BrowserContext(); ~BrowserContext(); @@ -39,15 +38,11 @@ class BrowserContext : public content::BrowserContext { // Subclasses should override this to register custom preferences. virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {} - // Subclasses should override this to provide a custom NetworkDelegate - // implementation. - virtual scoped_ptr CreateNetworkDelegate(); - - // Subclasses should override this to provide a custom URLRequestJobFactory - // implementation. - virtual scoped_ptr CreateURLRequestJobFactory( + // URLRequestContextGetter::Delegate: + virtual net::NetworkDelegate* CreateNetworkDelegate() OVERRIDE; + virtual net::URLRequestJobFactory* CreateURLRequestJobFactory( content::ProtocolHandlerMap* protocol_handlers, - content::ProtocolHandlerScopedVector* protocol_interceptors); + content::ProtocolHandlerScopedVector* protocol_interceptors) OVERRIDE; virtual base::FilePath GetPath() const OVERRIDE; diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 9a93ac155f0c..fba872f5f730 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -74,18 +74,16 @@ const char kProxyServer[] = "proxy-server"; } // namespace URLRequestContextGetter::URLRequestContextGetter( + Delegate* delegate, const base::FilePath& base_path, base::MessageLoop* io_loop, base::MessageLoop* file_loop, - base::Callback(void)> network_delegate_factory, - URLRequestJobFactoryFactory job_factory_factory, content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerScopedVector protocol_interceptors) - : base_path_(base_path), + : delegate_(delegate), + base_path_(base_path), io_loop_(io_loop), file_loop_(file_loop), - network_delegate_factory_(network_delegate_factory), - job_factory_factory_(job_factory_factory), protocol_interceptors_(protocol_interceptors.Pass()) { // Must first be created on the UI thread. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); @@ -110,7 +108,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { if (!url_request_context_.get()) { url_request_context_.reset(new net::URLRequestContext()); - network_delegate_ = network_delegate_factory_.Run().Pass(); + network_delegate_.reset(delegate_->CreateNetworkDelegate()); url_request_context_->set_network_delegate(network_delegate_.get()); storage_.reset( new net::URLRequestContextStorage(url_request_context_.get())); @@ -210,7 +208,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { // Give user a chance to create their own job factory. scoped_ptr user_job_factory( - job_factory_factory_.Run(&protocol_handlers_, &protocol_interceptors_)); + delegate_->CreateURLRequestJobFactory(&protocol_handlers_, &protocol_interceptors_)); if (user_job_factory) { storage_->set_job_factory(user_job_factory.release()); return url_request_context_.get(); diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index e179f4e15ec7..b2dfc1e7abde 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -5,7 +5,6 @@ #ifndef BRIGHTRAY_BROWSER_URL_REQUEST_CONTEXT_GETTER_H_ #define BRIGHTRAY_BROWSER_URL_REQUEST_CONTEXT_GETTER_H_ -#include "base/callback.h" #include "base/files/file_path.h" #include "base/memory/scoped_ptr.h" #include "content/public/browser/content_browser_client.h" @@ -18,26 +17,34 @@ class MessageLoop; namespace net { class HostMappingRules; class HostResolver; +class NetworkDelegate; class ProxyConfigService; class URLRequestContextStorage; +class URLRequestJobFactory; } namespace brightray { -class NetworkDelegate; - -typedef base::Callback( - content::ProtocolHandlerMap* protocol_handlers, - content::ProtocolHandlerScopedVector* protocol_interceptors)> URLRequestJobFactoryFactory; - class URLRequestContextGetter : public net::URLRequestContextGetter { public: + class Delegate { + public: + Delegate() {} + virtual ~Delegate() {} + + virtual net::NetworkDelegate* CreateNetworkDelegate() { return NULL; }; + virtual net::URLRequestJobFactory* CreateURLRequestJobFactory( + content::ProtocolHandlerMap* protocol_handlers, + content::ProtocolHandlerScopedVector* protocol_interceptors) { + return NULL; + }; + }; + URLRequestContextGetter( + Delegate* delegate, const base::FilePath& base_path, base::MessageLoop* io_loop, base::MessageLoop* file_loop, - base::Callback(void)>, - URLRequestJobFactoryFactory job_factory_factory, content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerScopedVector protocol_interceptors); virtual ~URLRequestContextGetter(); @@ -49,15 +56,14 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { virtual scoped_refptr GetNetworkTaskRunner() const OVERRIDE; + Delegate* delegate_; + base::FilePath base_path_; base::MessageLoop* io_loop_; base::MessageLoop* file_loop_; - base::Callback(void)> network_delegate_factory_; - URLRequestJobFactoryFactory job_factory_factory_; - scoped_ptr proxy_config_service_; - scoped_ptr network_delegate_; + scoped_ptr network_delegate_; scoped_ptr storage_; scoped_ptr url_request_context_; scoped_ptr host_mapping_rules_;