Merge pull request #68 from brightray/expose-url_request_context_getter

Add CreateURLRequestJobFactory override for BrowserContext
This commit is contained in:
Cheng Zhao 2014-08-15 12:16:23 +08:00
commit 8aa422a3e8
4 changed files with 50 additions and 19 deletions

View file

@ -7,7 +7,6 @@
#include "browser/download_manager_delegate.h" #include "browser/download_manager_delegate.h"
#include "browser/inspectable_web_contents_impl.h" #include "browser/inspectable_web_contents_impl.h"
#include "browser/network_delegate.h" #include "browser/network_delegate.h"
#include "browser/url_request_context_getter.h"
#include "common/application_info.h" #include "common/application_info.h"
#include "base/environment.h" #include "base/environment.h"
@ -25,6 +24,8 @@
#include "base/nix/xdg_util.h" #include "base/nix/xdg_util.h"
#endif #endif
using content::BrowserThread;
namespace brightray { namespace brightray {
class BrowserContext::ResourceContext : public content::ResourceContext { class BrowserContext::ResourceContext : public content::ResourceContext {
@ -79,7 +80,7 @@ void BrowserContext::Initialize() {
base::PrefServiceFactory prefs_factory; base::PrefServiceFactory prefs_factory;
prefs_factory.SetUserPrefsFile(prefs_path, prefs_factory.SetUserPrefsFile(prefs_path,
JsonPrefStore::GetTaskRunnerForFile( JsonPrefStore::GetTaskRunnerForFile(
prefs_path, content::BrowserThread::GetBlockingPool())); prefs_path, BrowserThread::GetBlockingPool()));
auto registry = make_scoped_refptr(new PrefRegistrySimple); auto registry = make_scoped_refptr(new PrefRegistrySimple);
RegisterInternalPrefs(registry); RegisterInternalPrefs(registry);
@ -89,9 +90,9 @@ void BrowserContext::Initialize() {
} }
BrowserContext::~BrowserContext() { BrowserContext::~BrowserContext() {
content::BrowserThread::DeleteSoon(content::BrowserThread::IO, BrowserThread::DeleteSoon(BrowserThread::IO,
FROM_HERE, FROM_HERE,
resource_context_.release()); resource_context_.release());
} }
void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) { void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) {
@ -102,15 +103,12 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors) { content::ProtocolHandlerScopedVector protocol_interceptors) {
DCHECK(!url_request_getter_); DCHECK(!url_request_getter_);
auto io_loop = content::BrowserThread::UnsafeGetMessageLoopForThread(
content::BrowserThread::IO);
auto file_loop = content::BrowserThread::UnsafeGetMessageLoopForThread(
content::BrowserThread::FILE);
url_request_getter_ = new URLRequestContextGetter( url_request_getter_ = new URLRequestContextGetter(
GetPath(), GetPath(),
io_loop, BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
file_loop, BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
base::Bind(&BrowserContext::CreateNetworkDelegate, base::Unretained(this)), base::Bind(&BrowserContext::CreateNetworkDelegate, base::Unretained(this)),
base::Bind(&BrowserContext::CreateURLRequestJobFactory, base::Unretained(this)),
protocol_handlers, protocol_handlers,
protocol_interceptors.Pass()); 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());
@ -121,6 +119,12 @@ scoped_ptr<NetworkDelegate> BrowserContext::CreateNetworkDelegate() {
return make_scoped_ptr(new NetworkDelegate).Pass(); return make_scoped_ptr(new NetworkDelegate).Pass();
} }
scoped_ptr<net::URLRequestJobFactory> BrowserContext::CreateURLRequestJobFactory(
content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector* protocol_interceptors) {
return scoped_ptr<net::URLRequestJobFactory>();
}
base::FilePath BrowserContext::GetPath() const { base::FilePath BrowserContext::GetPath() const {
return path_; return path_;
} }

View file

@ -5,6 +5,8 @@
#ifndef BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_ #ifndef BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_
#define BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_ #define BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_
#include "browser/url_request_context_getter.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/content_browser_client.h" #include "content/public/browser/content_browser_client.h"
@ -15,7 +17,6 @@ namespace brightray {
class DownloadManagerDelegate; class DownloadManagerDelegate;
class NetworkDelegate; class NetworkDelegate;
class URLRequestContextGetter;
class BrowserContext : public content::BrowserContext { class BrowserContext : public content::BrowserContext {
public: public:
@ -28,6 +29,10 @@ class BrowserContext : public content::BrowserContext {
content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors); content::ProtocolHandlerScopedVector protocol_interceptors);
net::URLRequestContextGetter* url_request_context_getter() const {
return url_request_getter_.get();
}
PrefService* prefs() { return prefs_.get(); } PrefService* prefs() { return prefs_.get(); }
protected: protected:
@ -38,6 +43,12 @@ class BrowserContext : public content::BrowserContext {
// implementation. // implementation.
virtual scoped_ptr<NetworkDelegate> CreateNetworkDelegate(); virtual scoped_ptr<NetworkDelegate> CreateNetworkDelegate();
// Subclasses should override this to provide a custom URLRequestJobFactory
// implementation.
virtual scoped_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory(
content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector* protocol_interceptors);
virtual base::FilePath GetPath() const OVERRIDE; virtual base::FilePath GetPath() const OVERRIDE;
private: private:

View file

@ -36,6 +36,8 @@
#include "net/url_request/url_request_job_factory_impl.h" #include "net/url_request/url_request_job_factory_impl.h"
#include "webkit/browser/quota/special_storage_policy.h" #include "webkit/browser/quota/special_storage_policy.h"
using content::BrowserThread;
namespace brightray { namespace brightray {
URLRequestContextGetter::URLRequestContextGetter( URLRequestContextGetter::URLRequestContextGetter(
@ -43,15 +45,17 @@ 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,
URLRequestJobFactoryFactory job_factory_factory,
content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors) 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),
job_factory_factory_(job_factory_factory),
protocol_interceptors_(protocol_interceptors.Pass()) { 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(BrowserThread::CurrentlyOn(BrowserThread::UI));
std::swap(protocol_handlers_, *protocol_handlers); std::swap(protocol_handlers_, *protocol_handlers);
@ -67,7 +71,7 @@ net::HostResolver* URLRequestContextGetter::host_resolver() {
} }
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!url_request_context_.get()) { if (!url_request_context_.get()) {
url_request_context_.reset(new net::URLRequestContext()); url_request_context_.reset(new net::URLRequestContext());
@ -117,8 +121,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
net::CACHE_BACKEND_DEFAULT, net::CACHE_BACKEND_DEFAULT,
cache_path, cache_path,
0, 0,
content::BrowserThread::GetMessageLoopProxyForThread( BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
content::BrowserThread::CACHE));
net::HttpNetworkSession::Params network_session_params; net::HttpNetworkSession::Params network_session_params;
network_session_params.cert_verifier = network_session_params.cert_verifier =
@ -148,6 +151,14 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
network_session_params, main_backend); network_session_params, main_backend);
storage_->set_http_transaction_factory(main_cache); 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( scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
new net::URLRequestJobFactoryImpl()); new net::URLRequestJobFactoryImpl());
for (auto it = protocol_handlers_.begin(), for (auto it = protocol_handlers_.begin(),
@ -163,7 +174,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
job_factory->SetProtocolHandler( job_factory->SetProtocolHandler(
content::kFileScheme, content::kFileScheme,
new net::FileProtocolHandler( new net::FileProtocolHandler(
content::BrowserThread::GetBlockingPool()-> BrowserThread::GetBlockingPool()->
GetTaskRunnerWithShutdownBehavior( GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))); base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
@ -187,8 +198,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
scoped_refptr<base::SingleThreadTaskRunner> scoped_refptr<base::SingleThreadTaskRunner>
URLRequestContextGetter::GetNetworkTaskRunner() const { URLRequestContextGetter::GetNetworkTaskRunner() const {
return content::BrowserThread::GetMessageLoopProxyForThread( return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
content::BrowserThread::IO);
} }
} // namespace brightray } // namespace brightray

View file

@ -25,6 +25,10 @@ namespace brightray {
class NetworkDelegate; class NetworkDelegate;
typedef base::Callback<scoped_ptr<net::URLRequestJobFactory>(
content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector* protocol_interceptors)> URLRequestJobFactoryFactory;
class URLRequestContextGetter : public net::URLRequestContextGetter { class URLRequestContextGetter : public net::URLRequestContextGetter {
public: public:
URLRequestContextGetter( URLRequestContextGetter(
@ -32,6 +36,7 @@ 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)>,
URLRequestJobFactoryFactory job_factory_factory,
content::ProtocolHandlerMap* protocol_handlers, content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors); content::ProtocolHandlerScopedVector protocol_interceptors);
virtual ~URLRequestContextGetter(); virtual ~URLRequestContextGetter();
@ -48,6 +53,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
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_;
URLRequestJobFactoryFactory job_factory_factory_;
scoped_ptr<net::ProxyConfigService> proxy_config_service_; scoped_ptr<net::ProxyConfigService> proxy_config_service_;
scoped_ptr<NetworkDelegate> network_delegate_; scoped_ptr<NetworkDelegate> network_delegate_;