Give user a chance to create custom URLRequestJobFactory.

This commit is contained in:
Cheng Zhao 2014-08-13 15:48:16 +08:00
parent d01aa51686
commit 2d03c983e4
4 changed files with 34 additions and 0 deletions

View file

@ -20,6 +20,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/storage_partition.h"
#include "net/url_request/url_request_job_factory_impl.h"
#if defined(OS_LINUX)
#include "base/nix/xdg_util.h"
@ -109,6 +110,7 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
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());
@ -119,6 +121,12 @@ scoped_ptr<NetworkDelegate> BrowserContext::CreateNetworkDelegate() {
return make_scoped_ptr(new NetworkDelegate).Pass();
}
scoped_ptr<net::URLRequestJobFactory> BrowserContext::CreateURLRequestJobFactory(
const content::ProtocolHandlerMap& protocol_handlers,
const content::ProtocolHandlerScopedVector& protocol_interceptors) {
return scoped_ptr<net::URLRequestJobFactory>();
}
base::FilePath BrowserContext::GetPath() const {
return path_;
}

View file

@ -11,6 +11,10 @@
class PrefRegistrySimple;
class PrefService;
namespace browser_context {
class URLRequestJobFactory;
}
namespace brightray {
class DownloadManagerDelegate;
@ -38,6 +42,12 @@ class BrowserContext : public content::BrowserContext {
// implementation.
virtual scoped_ptr<NetworkDelegate> CreateNetworkDelegate();
// Subclasses should override this to provide a custom URLRequestJobFactory
// implementation.
virtual scoped_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory(
const content::ProtocolHandlerMap& protocol_handlers,
const content::ProtocolHandlerScopedVector& protocol_interceptors);
virtual base::FilePath GetPath() const OVERRIDE;
private:

View file

@ -45,12 +45,14 @@ URLRequestContextGetter::URLRequestContextGetter(
base::MessageLoop* io_loop,
base::MessageLoop* file_loop,
base::Callback<scoped_ptr<NetworkDelegate>(void)> network_delegate_factory,
URLRequestJobFactoryFactory job_factory_factory,
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),
job_factory_factory_(job_factory_factory),
protocol_interceptors_(protocol_interceptors.Pass()) {
// Must first be created on the UI thread.
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
@ -149,6 +151,14 @@ 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<net::URLRequestJobFactory> user_job_factory(
job_factory_factory_.Run(protocol_handlers_, protocol_interceptors_));
if (user_job_factory) {
storage_->set_job_factory(user_job_factory.release());
return url_request_context_.get();
}
scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
new net::URLRequestJobFactoryImpl());
for (auto it = protocol_handlers_.begin(),

View file

@ -25,6 +25,10 @@ namespace brightray {
class NetworkDelegate;
typedef base::Callback<scoped_ptr<net::URLRequestJobFactory>(
const content::ProtocolHandlerMap& protocol_handlers,
const content::ProtocolHandlerScopedVector& protocol_interceptors)> URLRequestJobFactoryFactory;
class URLRequestContextGetter : public net::URLRequestContextGetter {
public:
URLRequestContextGetter(
@ -32,6 +36,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
base::MessageLoop* io_loop,
base::MessageLoop* file_loop,
base::Callback<scoped_ptr<NetworkDelegate>(void)>,
URLRequestJobFactoryFactory job_factory_factory,
content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors);
virtual ~URLRequestContextGetter();
@ -48,6 +53,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
base::MessageLoop* file_loop_;
base::Callback<scoped_ptr<NetworkDelegate>(void)> network_delegate_factory_;
URLRequestJobFactoryFactory job_factory_factory_;
scoped_ptr<net::ProxyConfigService> proxy_config_service_;
scoped_ptr<NetworkDelegate> network_delegate_;