From 42b86242ea52cb42b2dc363bcb510a947d4918aa Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Thu, 26 Jun 2014 16:27:22 -0400 Subject: [PATCH] Add support for Chrome 35's "protocol interceptors" See https://codereview.chromium.org/187223003 --- brightray/browser/browser_client.cc | 5 +++-- brightray/browser/browser_client.h | 4 +++- brightray/browser/browser_context.cc | 6 +++-- brightray/browser/browser_context.h | 3 ++- .../browser/url_request_context_getter.cc | 22 ++++++++++++++++--- .../browser/url_request_context_getter.h | 4 +++- 6 files changed, 34 insertions(+), 10 deletions(-) diff --git a/brightray/browser/browser_client.cc b/brightray/browser/browser_client.cc index 82c0d7acb5f..fabd51cc74c 100644 --- a/brightray/browser/browser_client.cc +++ b/brightray/browser/browser_client.cc @@ -56,9 +56,10 @@ content::BrowserMainParts* BrowserClient::CreateBrowserMainParts( net::URLRequestContextGetter* BrowserClient::CreateRequestContext( content::BrowserContext* browser_context, - content::ProtocolHandlerMap* protocol_handlers) { + content::ProtocolHandlerMap* protocol_handlers, + content::ProtocolHandlerScopedVector protocol_interceptors) { auto context = static_cast(browser_context); - return context->CreateRequestContext(protocol_handlers); + return context->CreateRequestContext(protocol_handlers, protocol_interceptors.Pass()); } void BrowserClient::ShowDesktopNotification( diff --git a/brightray/browser/browser_client.h b/brightray/browser/browser_client.h index 41a3861f61f..42a7380a63c 100644 --- a/brightray/browser/browser_client.h +++ b/brightray/browser/browser_client.h @@ -34,7 +34,9 @@ class BrowserClient : public content::ContentBrowserClient { // Subclasses that override this (e.g., to provide their own protocol // handlers) should call this implementation after doing their own work. virtual net::URLRequestContextGetter* CreateRequestContext( - content::BrowserContext*, content::ProtocolHandlerMap*) OVERRIDE; + content::BrowserContext* browser_context, + content::ProtocolHandlerMap* protocol_handlers, + content::ProtocolHandlerScopedVector protocol_interceptors) OVERRIDE; private: virtual content::BrowserMainParts* CreateBrowserMainParts( diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index cef50925de3..f2bd0e82923 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -99,7 +99,8 @@ void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) { } net::URLRequestContextGetter* BrowserContext::CreateRequestContext( - content::ProtocolHandlerMap* protocol_handlers) { + content::ProtocolHandlerMap* protocol_handlers, + content::ProtocolHandlerScopedVector protocol_interceptors) { DCHECK(!url_request_getter_); auto io_loop = content::BrowserThread::UnsafeGetMessageLoopForThread( content::BrowserThread::IO); @@ -110,7 +111,8 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext( io_loop, file_loop, base::Bind(&BrowserContext::CreateNetworkDelegate, base::Unretained(this)), - protocol_handlers); + protocol_handlers, + protocol_interceptors.Pass()); resource_context_->set_url_request_context_getter(url_request_getter_.get()); return url_request_getter_.get(); } diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index b92deb75c57..667d6a4802f 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -25,7 +25,8 @@ class BrowserContext : public content::BrowserContext { virtual void Initialize(); net::URLRequestContextGetter* CreateRequestContext( - content::ProtocolHandlerMap*); + content::ProtocolHandlerMap* protocol_handlers, + content::ProtocolHandlerScopedVector protocol_interceptors); PrefService* prefs() { return prefs_.get(); } diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 4b26d7a9364..0e590aa5429 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -29,6 +29,7 @@ #include "net/ssl/ssl_config_service_defaults.h" #include "net/url_request/data_protocol_handler.h" #include "net/url_request/file_protocol_handler.h" +#include "net/url_request/protocol_intercept_job_factory.h" #include "net/url_request/static_http_user_agent_settings.h" #include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context_storage.h" @@ -41,11 +42,13 @@ URLRequestContextGetter::URLRequestContextGetter( base::MessageLoop* io_loop, base::MessageLoop* file_loop, base::Callback(void)> network_delegate_factory, - content::ProtocolHandlerMap* protocol_handlers) + content::ProtocolHandlerMap* protocol_handlers, + content::ProtocolHandlerScopedVector protocol_interceptors) : base_path_(base_path), io_loop_(io_loop), file_loop_(file_loop), - network_delegate_factory_(network_delegate_factory) { + network_delegate_factory_(network_delegate_factory), + protocol_interceptors_(protocol_interceptors.Pass()) { // Must first be created on the UI thread. DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); @@ -161,7 +164,20 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { content::BrowserThread::GetBlockingPool()-> GetTaskRunnerWithShutdownBehavior( base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))); - storage_->set_job_factory(job_factory.release()); + + // 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()); } return url_request_context_.get(); diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index 7d3e174cd40..ad09e2b409c 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -32,7 +32,8 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { base::MessageLoop* io_loop, base::MessageLoop* file_loop, base::Callback(void)>, - content::ProtocolHandlerMap*); + content::ProtocolHandlerMap* protocol_handlers, + content::ProtocolHandlerScopedVector protocol_interceptors); virtual ~URLRequestContextGetter(); net::HostResolver* host_resolver(); @@ -53,6 +54,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { scoped_ptr storage_; scoped_ptr url_request_context_; content::ProtocolHandlerMap protocol_handlers_; + content::ProtocolHandlerScopedVector protocol_interceptors_; DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter); };