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

View file

@ -5,6 +5,8 @@
#ifndef 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/content_browser_client.h"
@ -15,7 +17,6 @@ namespace brightray {
class DownloadManagerDelegate;
class NetworkDelegate;
class URLRequestContextGetter;
class BrowserContext : public content::BrowserContext {
public:
@ -28,6 +29,10 @@ class BrowserContext : public content::BrowserContext {
content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector protocol_interceptors);
net::URLRequestContextGetter* url_request_context_getter() const {
return url_request_getter_.get();
}
PrefService* prefs() { return prefs_.get(); }
protected:
@ -38,6 +43,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(
content::ProtocolHandlerMap* protocol_handlers,
content::ProtocolHandlerScopedVector* protocol_interceptors);
virtual base::FilePath GetPath() const OVERRIDE;
private:

View file

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

View file

@ -25,6 +25,10 @@ namespace brightray {
class NetworkDelegate;
typedef base::Callback<scoped_ptr<net::URLRequestJobFactory>(
content::ProtocolHandlerMap* protocol_handlers,
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_;