From c30f11f38c632946a495198ec110e4e97edbffe2 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 20 Aug 2014 14:39:09 +0800 Subject: [PATCH 1/3] 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 8a5e63e8c74..3396531ed35 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 486a9eda903..6bd2a1b81d9 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 9a93ac155f0..fba872f5f73 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 e179f4e15ec..b2dfc1e7abd 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_; From 3fefdcba0de57164df2ef25a6112eea44e230174 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 20 Aug 2014 14:48:02 +0800 Subject: [PATCH 2/3] Move creation of URLRequestJobFactoryImpl into default delegate. --- .../browser/url_request_context_getter.cc | 69 ++++++++----------- .../browser/url_request_context_getter.h | 6 +- 2 files changed, 31 insertions(+), 44 deletions(-) diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index fba872f5f73..597f8be588a 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -73,6 +73,32 @@ const char kProxyServer[] = "proxy-server"; } // namespace +net::URLRequestJobFactory* URLRequestContextGetter::Delegate::CreateURLRequestJobFactory( + content::ProtocolHandlerMap* protocol_handlers, + content::ProtocolHandlerScopedVector* protocol_interceptors) { + scoped_ptr job_factory(new net::URLRequestJobFactoryImpl); + + for (auto it = protocol_handlers->begin(); it != protocol_handlers->end(); ++it) + job_factory->SetProtocolHandler(it->first, it->second.release()); + protocol_handlers->clear(); + + job_factory->SetProtocolHandler(content::kDataScheme, new net::DataProtocolHandler); + job_factory->SetProtocolHandler(content::kFileScheme, new net::FileProtocolHandler( + BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( + base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))); + + // Set up interceptors in the reverse order. + scoped_ptr top_job_factory = + job_factory.PassAs(); + for (content::ProtocolHandlerScopedVector::reverse_iterator i = protocol_interceptors->rbegin(); + i != protocol_interceptors->rend(); ++i) + top_job_factory.reset(new net::ProtocolInterceptJobFactory( + top_job_factory.Pass(), make_scoped_ptr(*i))); + protocol_interceptors->weak_clear(); + + return top_job_factory.release(); +} + URLRequestContextGetter::URLRequestContextGetter( Delegate* delegate, const base::FilePath& base_path, @@ -137,6 +163,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { host_resolver = remapped_resolver.PassAs(); } + // --proxy-server net::DhcpProxyScriptFetcherFactory dhcp_factory; if (command_line.HasSwitch(kNoProxyServer)) storage_->set_proxy_service(net::ProxyService::CreateDirect()); @@ -206,46 +233,8 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { network_session_params, main_backend); storage_->set_http_transaction_factory(main_cache); - // Give user a chance to create their own job factory. - scoped_ptr user_job_factory( - delegate_->CreateURLRequestJobFactory(&protocol_handlers_, &protocol_interceptors_)); - if (user_job_factory) { - storage_->set_job_factory(user_job_factory.release()); - return url_request_context_.get(); - } - - scoped_ptr job_factory( - new net::URLRequestJobFactoryImpl()); - for (auto it = protocol_handlers_.begin(), - end = protocol_handlers_.end(); it != end; ++it) { - bool set_protocol = job_factory->SetProtocolHandler( - it->first, it->second.release()); - DCHECK(set_protocol); - (void)set_protocol; // silence unused-variable warning in Release builds on Windows - } - protocol_handlers_.clear(); - job_factory->SetProtocolHandler( - content::kDataScheme, new net::DataProtocolHandler); - job_factory->SetProtocolHandler( - content::kFileScheme, - new net::FileProtocolHandler( - BrowserThread::GetBlockingPool()-> - GetTaskRunnerWithShutdownBehavior( - base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))); - - // Set up interceptors in the reverse order. - scoped_ptr top_job_factory = - job_factory.PassAs(); - for (content::ProtocolHandlerScopedVector::reverse_iterator i = - protocol_interceptors_.rbegin(); - i != protocol_interceptors_.rend(); - ++i) { - top_job_factory.reset(new net::ProtocolInterceptJobFactory( - top_job_factory.Pass(), make_scoped_ptr(*i))); - } - protocol_interceptors_.weak_clear(); - - storage_->set_job_factory(top_job_factory.release()); + storage_->set_job_factory(delegate_->CreateURLRequestJobFactory( + &protocol_handlers_, &protocol_interceptors_)); } return url_request_context_.get(); diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index b2dfc1e7abd..b22df64e1fb 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -32,12 +32,10 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { Delegate() {} virtual ~Delegate() {} - virtual net::NetworkDelegate* CreateNetworkDelegate() { return NULL; }; + virtual net::NetworkDelegate* CreateNetworkDelegate() { return NULL; } virtual net::URLRequestJobFactory* CreateURLRequestJobFactory( content::ProtocolHandlerMap* protocol_handlers, - content::ProtocolHandlerScopedVector* protocol_interceptors) { - return NULL; - }; + content::ProtocolHandlerScopedVector* protocol_interceptors); }; URLRequestContextGetter( From 6e420d5137abbb1d60fcb5ac918996b27796ce07 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 20 Aug 2014 15:19:25 +0800 Subject: [PATCH 3/3] Cleanup URLRequestContextGetter. In brightray line length limit is 100 so we could make code formatted more tidy. --- .../browser/url_request_context_getter.cc | 65 ++++++++----------- .../browser/url_request_context_getter.h | 8 +-- 2 files changed, 31 insertions(+), 42 deletions(-) diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 597f8be588a..da57591ed95 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -116,6 +116,9 @@ URLRequestContextGetter::URLRequestContextGetter( std::swap(protocol_handlers_, *protocol_handlers); + // We must create the proxy config service on the UI loop on Linux because it + // must synchronously run on the glib message loop. This will be passed to + // the URLRequestContextStorage on the IO thread in GetURLRequestContext(). proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService( io_loop_->message_loop_proxy(), file_loop_)); } @@ -131,28 +134,24 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); const CommandLine& command_line = *CommandLine::ForCurrentProcess(); - if (!url_request_context_.get()) { - url_request_context_.reset(new net::URLRequestContext()); + url_request_context_.reset(new net::URLRequestContext); network_delegate_.reset(delegate_->CreateNetworkDelegate()); url_request_context_->set_network_delegate(network_delegate_.get()); - storage_.reset( - new net::URLRequestContextStorage(url_request_context_.get())); + + storage_.reset(new net::URLRequestContextStorage(url_request_context_.get())); auto cookie_config = content::CookieStoreConfig( base_path_.Append(FILE_PATH_LITERAL("Cookies")), content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES, - nullptr, - nullptr); + NULL, NULL); storage_->set_cookie_store(content::CreateCookieStore(cookie_config)); storage_->set_server_bound_cert_service(new net::ServerBoundCertService( new net::DefaultServerBoundCertStore(NULL), base::WorkerPool::GetTaskRunner(true))); - storage_->set_http_user_agent_settings( - new net::StaticHttpUserAgentSettings( - "en-us,en", base::EmptyString())); + storage_->set_http_user_agent_settings(new net::StaticHttpUserAgentSettings( + "en-us,en", base::EmptyString())); - scoped_ptr host_resolver( - net::HostResolver::CreateDefaultResolver(NULL)); + scoped_ptr host_resolver(net::HostResolver::CreateDefaultResolver(NULL)); // --host-resolver-rules if (command_line.HasSwitch(switches::kHostResolverRules)) { @@ -189,33 +188,19 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { new net::HttpServerPropertiesImpl); storage_->set_http_server_properties(server_properties.Pass()); - base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache")); - net::HttpCache::DefaultBackend* main_backend = - new net::HttpCache::DefaultBackend( - net::DISK_CACHE, - net::CACHE_BACKEND_DEFAULT, - cache_path, - 0, - BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); - net::HttpNetworkSession::Params network_session_params; - network_session_params.cert_verifier = - url_request_context_->cert_verifier(); + network_session_params.cert_verifier = url_request_context_->cert_verifier(); + network_session_params.proxy_service = url_request_context_->proxy_service(); + network_session_params.ssl_config_service = url_request_context_->ssl_config_service(); + network_session_params.network_delegate = url_request_context_->network_delegate(); + network_session_params.http_server_properties = url_request_context_->http_server_properties(); + network_session_params.ignore_certificate_errors = false; network_session_params.transport_security_state = url_request_context_->transport_security_state(); network_session_params.server_bound_cert_service = url_request_context_->server_bound_cert_service(); - network_session_params.proxy_service = - url_request_context_->proxy_service(); - network_session_params.ssl_config_service = - url_request_context_->ssl_config_service(); network_session_params.http_auth_handler_factory = url_request_context_->http_auth_handler_factory(); - network_session_params.network_delegate = - url_request_context_->network_delegate(); - network_session_params.http_server_properties = - url_request_context_->http_server_properties(); - network_session_params.ignore_certificate_errors = false; // --host-rules if (command_line.HasSwitch(kHostRules)) { @@ -226,12 +211,17 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { // Give |storage_| ownership at the end in case it's |mapped_host_resolver|. storage_->set_host_resolver(host_resolver.Pass()); - network_session_params.host_resolver = - url_request_context_->host_resolver(); + network_session_params.host_resolver = url_request_context_->host_resolver(); - net::HttpCache* main_cache = new net::HttpCache( - network_session_params, main_backend); - storage_->set_http_transaction_factory(main_cache); + base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache")); + net::HttpCache::DefaultBackend* backend = + new net::HttpCache::DefaultBackend( + net::DISK_CACHE, + net::CACHE_BACKEND_DEFAULT, + cache_path, + 0, + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); + storage_->set_http_transaction_factory(new net::HttpCache(network_session_params, backend)); storage_->set_job_factory(delegate_->CreateURLRequestJobFactory( &protocol_handlers_, &protocol_interceptors_)); @@ -240,8 +230,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { return url_request_context_.get(); } -scoped_refptr - URLRequestContextGetter::GetNetworkTaskRunner() const { +scoped_refptr URLRequestContextGetter::GetNetworkTaskRunner() const { return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); } diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index b22df64e1fb..c7379f2248a 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -47,13 +47,13 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { content::ProtocolHandlerScopedVector protocol_interceptors); virtual ~URLRequestContextGetter(); - net::HostResolver* host_resolver(); + // net::URLRequestContextGetter: virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; + virtual scoped_refptr GetNetworkTaskRunner() const OVERRIDE; + + net::HostResolver* host_resolver(); private: - virtual scoped_refptr - GetNetworkTaskRunner() const OVERRIDE; - Delegate* delegate_; base::FilePath base_path_;