Add support for Chrome 35's "protocol interceptors"

See https://codereview.chromium.org/187223003
This commit is contained in:
Adam Roben 2014-06-26 16:27:22 -04:00
parent 500861d63d
commit 42b86242ea
6 changed files with 34 additions and 10 deletions

View file

@ -56,9 +56,10 @@ content::BrowserMainParts* BrowserClient::CreateBrowserMainParts(
net::URLRequestContextGetter* BrowserClient::CreateRequestContext( net::URLRequestContextGetter* BrowserClient::CreateRequestContext(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
content::ProtocolHandlerMap* protocol_handlers) { content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors) {
auto context = static_cast<BrowserContext*>(browser_context); auto context = static_cast<BrowserContext*>(browser_context);
return context->CreateRequestContext(protocol_handlers); return context->CreateRequestContext(protocol_handlers, protocol_interceptors.Pass());
} }
void BrowserClient::ShowDesktopNotification( void BrowserClient::ShowDesktopNotification(

View file

@ -34,7 +34,9 @@ class BrowserClient : public content::ContentBrowserClient {
// Subclasses that override this (e.g., to provide their own protocol // Subclasses that override this (e.g., to provide their own protocol
// handlers) should call this implementation after doing their own work. // handlers) should call this implementation after doing their own work.
virtual net::URLRequestContextGetter* CreateRequestContext( virtual net::URLRequestContextGetter* CreateRequestContext(
content::BrowserContext*, content::ProtocolHandlerMap*) OVERRIDE; content::BrowserContext* browser_context,
content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors) OVERRIDE;
private: private:
virtual content::BrowserMainParts* CreateBrowserMainParts( virtual content::BrowserMainParts* CreateBrowserMainParts(

View file

@ -99,7 +99,8 @@ void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) {
} }
net::URLRequestContextGetter* BrowserContext::CreateRequestContext( net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers) { content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors) {
DCHECK(!url_request_getter_); DCHECK(!url_request_getter_);
auto io_loop = content::BrowserThread::UnsafeGetMessageLoopForThread( auto io_loop = content::BrowserThread::UnsafeGetMessageLoopForThread(
content::BrowserThread::IO); content::BrowserThread::IO);
@ -110,7 +111,8 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
io_loop, io_loop,
file_loop, file_loop,
base::Bind(&BrowserContext::CreateNetworkDelegate, base::Unretained(this)), 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()); resource_context_->set_url_request_context_getter(url_request_getter_.get());
return url_request_getter_.get(); return url_request_getter_.get();
} }

View file

@ -25,7 +25,8 @@ class BrowserContext : public content::BrowserContext {
virtual void Initialize(); virtual void Initialize();
net::URLRequestContextGetter* CreateRequestContext( net::URLRequestContextGetter* CreateRequestContext(
content::ProtocolHandlerMap*); content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors);
PrefService* prefs() { return prefs_.get(); } PrefService* prefs() { return prefs_.get(); }

View file

@ -29,6 +29,7 @@
#include "net/ssl/ssl_config_service_defaults.h" #include "net/ssl/ssl_config_service_defaults.h"
#include "net/url_request/data_protocol_handler.h" #include "net/url_request/data_protocol_handler.h"
#include "net/url_request/file_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/static_http_user_agent_settings.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_storage.h" #include "net/url_request/url_request_context_storage.h"
@ -41,11 +42,13 @@ URLRequestContextGetter::URLRequestContextGetter(
base::MessageLoop* io_loop, base::MessageLoop* io_loop,
base::MessageLoop* file_loop, base::MessageLoop* file_loop,
base::Callback<scoped_ptr<NetworkDelegate>(void)> network_delegate_factory, base::Callback<scoped_ptr<NetworkDelegate>(void)> network_delegate_factory,
content::ProtocolHandlerMap* protocol_handlers) content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors)
: base_path_(base_path), : base_path_(base_path),
io_loop_(io_loop), io_loop_(io_loop),
file_loop_(file_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. // Must first be created on the UI thread.
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
@ -161,7 +164,20 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
content::BrowserThread::GetBlockingPool()-> content::BrowserThread::GetBlockingPool()->
GetTaskRunnerWithShutdownBehavior( GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))); base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
storage_->set_job_factory(job_factory.release());
// Set up interceptors in the reverse order.
scoped_ptr<net::URLRequestJobFactory> top_job_factory =
job_factory.PassAs<net::URLRequestJobFactory>();
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(); return url_request_context_.get();

View file

@ -32,7 +32,8 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
base::MessageLoop* io_loop, base::MessageLoop* io_loop,
base::MessageLoop* file_loop, base::MessageLoop* file_loop,
base::Callback<scoped_ptr<NetworkDelegate>(void)>, base::Callback<scoped_ptr<NetworkDelegate>(void)>,
content::ProtocolHandlerMap*); content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors);
virtual ~URLRequestContextGetter(); virtual ~URLRequestContextGetter();
net::HostResolver* host_resolver(); net::HostResolver* host_resolver();
@ -53,6 +54,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
scoped_ptr<net::URLRequestContextStorage> storage_; scoped_ptr<net::URLRequestContextStorage> storage_;
scoped_ptr<net::URLRequestContext> url_request_context_; scoped_ptr<net::URLRequestContext> url_request_context_;
content::ProtocolHandlerMap protocol_handlers_; content::ProtocolHandlerMap protocol_handlers_;
content::ProtocolHandlerScopedVector protocol_interceptors_;
DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter); DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter);
}; };