Merge pull request #71 from brightray/request-context-getter-delegate
Add delegate class for URLRequestContextGetter
This commit is contained in:
commit
69bf54cebe
4 changed files with 91 additions and 117 deletions
|
@ -104,25 +104,24 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
|
|||
content::ProtocolHandlerScopedVector protocol_interceptors) {
|
||||
DCHECK(!url_request_getter_);
|
||||
url_request_getter_ = new URLRequestContextGetter(
|
||||
this,
|
||||
GetPath(),
|
||||
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());
|
||||
return url_request_getter_.get();
|
||||
}
|
||||
|
||||
scoped_ptr<NetworkDelegate> BrowserContext::CreateNetworkDelegate() {
|
||||
return make_scoped_ptr(new NetworkDelegate).Pass();
|
||||
net::NetworkDelegate* BrowserContext::CreateNetworkDelegate() {
|
||||
return new NetworkDelegate;
|
||||
}
|
||||
|
||||
scoped_ptr<net::URLRequestJobFactory> BrowserContext::CreateURLRequestJobFactory(
|
||||
net::URLRequestJobFactory* BrowserContext::CreateURLRequestJobFactory(
|
||||
content::ProtocolHandlerMap* protocol_handlers,
|
||||
content::ProtocolHandlerScopedVector* protocol_interceptors) {
|
||||
return scoped_ptr<net::URLRequestJobFactory>();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
base::FilePath BrowserContext::GetPath() const {
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
#include "browser/url_request_context_getter.h"
|
||||
|
||||
#include "content/public/browser/browser_context.h"
|
||||
#include "content/public/browser/content_browser_client.h"
|
||||
|
||||
class PrefRegistrySimple;
|
||||
class PrefService;
|
||||
|
@ -16,9 +15,9 @@ class PrefService;
|
|||
namespace brightray {
|
||||
|
||||
class DownloadManagerDelegate;
|
||||
class NetworkDelegate;
|
||||
|
||||
class BrowserContext : public content::BrowserContext {
|
||||
class BrowserContext : public content::BrowserContext,
|
||||
public brightray::URLRequestContextGetter::Delegate {
|
||||
public:
|
||||
BrowserContext();
|
||||
~BrowserContext();
|
||||
|
@ -39,15 +38,11 @@ class BrowserContext : public content::BrowserContext {
|
|||
// Subclasses should override this to register custom preferences.
|
||||
virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {}
|
||||
|
||||
// Subclasses should override this to provide a custom NetworkDelegate
|
||||
// implementation.
|
||||
virtual scoped_ptr<NetworkDelegate> CreateNetworkDelegate();
|
||||
|
||||
// Subclasses should override this to provide a custom URLRequestJobFactory
|
||||
// implementation.
|
||||
virtual scoped_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory(
|
||||
// URLRequestContextGetter::Delegate:
|
||||
virtual net::NetworkDelegate* CreateNetworkDelegate() OVERRIDE;
|
||||
virtual net::URLRequestJobFactory* CreateURLRequestJobFactory(
|
||||
content::ProtocolHandlerMap* protocol_handlers,
|
||||
content::ProtocolHandlerScopedVector* protocol_interceptors);
|
||||
content::ProtocolHandlerScopedVector* protocol_interceptors) OVERRIDE;
|
||||
|
||||
virtual base::FilePath GetPath() const OVERRIDE;
|
||||
|
||||
|
|
|
@ -73,25 +73,52 @@ const char kProxyServer[] = "proxy-server";
|
|||
|
||||
} // namespace
|
||||
|
||||
net::URLRequestJobFactory* URLRequestContextGetter::Delegate::CreateURLRequestJobFactory(
|
||||
content::ProtocolHandlerMap* protocol_handlers,
|
||||
content::ProtocolHandlerScopedVector* protocol_interceptors) {
|
||||
scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(new net::URLRequestJobFactoryImpl);
|
||||
|
||||
for (auto it = protocol_handlers->begin(); it != protocol_handlers->end(); ++it)
|
||||
job_factory->SetProtocolHandler(it->first, it->second.release());
|
||||
protocol_handlers->clear();
|
||||
|
||||
job_factory->SetProtocolHandler(content::kDataScheme, new net::DataProtocolHandler);
|
||||
job_factory->SetProtocolHandler(content::kFileScheme, new net::FileProtocolHandler(
|
||||
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
|
||||
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
|
||||
|
||||
// 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();
|
||||
|
||||
return top_job_factory.release();
|
||||
}
|
||||
|
||||
URLRequestContextGetter::URLRequestContextGetter(
|
||||
Delegate* delegate,
|
||||
const base::FilePath& base_path,
|
||||
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),
|
||||
: delegate_(delegate),
|
||||
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));
|
||||
|
||||
std::swap(protocol_handlers_, *protocol_handlers);
|
||||
|
||||
// We must create the proxy config service on the UI loop on Linux because it
|
||||
// must synchronously run on the glib message loop. This will be passed to
|
||||
// the URLRequestContextStorage on the IO thread in GetURLRequestContext().
|
||||
proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(
|
||||
io_loop_->message_loop_proxy(), file_loop_));
|
||||
}
|
||||
|
@ -107,28 +134,24 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
|
|||
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
||||
|
||||
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
|
||||
|
||||
if (!url_request_context_.get()) {
|
||||
url_request_context_.reset(new net::URLRequestContext());
|
||||
network_delegate_ = network_delegate_factory_.Run().Pass();
|
||||
url_request_context_.reset(new net::URLRequestContext);
|
||||
network_delegate_.reset(delegate_->CreateNetworkDelegate());
|
||||
url_request_context_->set_network_delegate(network_delegate_.get());
|
||||
storage_.reset(
|
||||
new net::URLRequestContextStorage(url_request_context_.get()));
|
||||
|
||||
storage_.reset(new net::URLRequestContextStorage(url_request_context_.get()));
|
||||
auto cookie_config = content::CookieStoreConfig(
|
||||
base_path_.Append(FILE_PATH_LITERAL("Cookies")),
|
||||
content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
|
||||
nullptr,
|
||||
nullptr);
|
||||
NULL, NULL);
|
||||
storage_->set_cookie_store(content::CreateCookieStore(cookie_config));
|
||||
storage_->set_server_bound_cert_service(new net::ServerBoundCertService(
|
||||
new net::DefaultServerBoundCertStore(NULL),
|
||||
base::WorkerPool::GetTaskRunner(true)));
|
||||
storage_->set_http_user_agent_settings(
|
||||
new net::StaticHttpUserAgentSettings(
|
||||
"en-us,en", base::EmptyString()));
|
||||
storage_->set_http_user_agent_settings(new net::StaticHttpUserAgentSettings(
|
||||
"en-us,en", base::EmptyString()));
|
||||
|
||||
scoped_ptr<net::HostResolver> host_resolver(
|
||||
net::HostResolver::CreateDefaultResolver(NULL));
|
||||
scoped_ptr<net::HostResolver> host_resolver(net::HostResolver::CreateDefaultResolver(NULL));
|
||||
|
||||
// --host-resolver-rules
|
||||
if (command_line.HasSwitch(switches::kHostResolverRules)) {
|
||||
|
@ -139,6 +162,7 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
|
|||
host_resolver = remapped_resolver.PassAs<net::HostResolver>();
|
||||
}
|
||||
|
||||
// --proxy-server
|
||||
net::DhcpProxyScriptFetcherFactory dhcp_factory;
|
||||
if (command_line.HasSwitch(kNoProxyServer))
|
||||
storage_->set_proxy_service(net::ProxyService::CreateDirect());
|
||||
|
@ -164,33 +188,19 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
|
|||
new net::HttpServerPropertiesImpl);
|
||||
storage_->set_http_server_properties(server_properties.Pass());
|
||||
|
||||
base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
|
||||
net::HttpCache::DefaultBackend* main_backend =
|
||||
new net::HttpCache::DefaultBackend(
|
||||
net::DISK_CACHE,
|
||||
net::CACHE_BACKEND_DEFAULT,
|
||||
cache_path,
|
||||
0,
|
||||
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
|
||||
|
||||
net::HttpNetworkSession::Params network_session_params;
|
||||
network_session_params.cert_verifier =
|
||||
url_request_context_->cert_verifier();
|
||||
network_session_params.cert_verifier = url_request_context_->cert_verifier();
|
||||
network_session_params.proxy_service = url_request_context_->proxy_service();
|
||||
network_session_params.ssl_config_service = url_request_context_->ssl_config_service();
|
||||
network_session_params.network_delegate = url_request_context_->network_delegate();
|
||||
network_session_params.http_server_properties = url_request_context_->http_server_properties();
|
||||
network_session_params.ignore_certificate_errors = false;
|
||||
network_session_params.transport_security_state =
|
||||
url_request_context_->transport_security_state();
|
||||
network_session_params.server_bound_cert_service =
|
||||
url_request_context_->server_bound_cert_service();
|
||||
network_session_params.proxy_service =
|
||||
url_request_context_->proxy_service();
|
||||
network_session_params.ssl_config_service =
|
||||
url_request_context_->ssl_config_service();
|
||||
network_session_params.http_auth_handler_factory =
|
||||
url_request_context_->http_auth_handler_factory();
|
||||
network_session_params.network_delegate =
|
||||
url_request_context_->network_delegate();
|
||||
network_session_params.http_server_properties =
|
||||
url_request_context_->http_server_properties();
|
||||
network_session_params.ignore_certificate_errors = false;
|
||||
|
||||
// --host-rules
|
||||
if (command_line.HasSwitch(kHostRules)) {
|
||||
|
@ -201,60 +211,26 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
|
|||
|
||||
// Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
|
||||
storage_->set_host_resolver(host_resolver.Pass());
|
||||
network_session_params.host_resolver =
|
||||
url_request_context_->host_resolver();
|
||||
network_session_params.host_resolver = url_request_context_->host_resolver();
|
||||
|
||||
net::HttpCache* main_cache = new net::HttpCache(
|
||||
network_session_params, main_backend);
|
||||
storage_->set_http_transaction_factory(main_cache);
|
||||
base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
|
||||
net::HttpCache::DefaultBackend* backend =
|
||||
new net::HttpCache::DefaultBackend(
|
||||
net::DISK_CACHE,
|
||||
net::CACHE_BACKEND_DEFAULT,
|
||||
cache_path,
|
||||
0,
|
||||
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
|
||||
storage_->set_http_transaction_factory(new net::HttpCache(network_session_params, backend));
|
||||
|
||||
// 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(),
|
||||
end = protocol_handlers_.end(); it != end; ++it) {
|
||||
bool set_protocol = job_factory->SetProtocolHandler(
|
||||
it->first, it->second.release());
|
||||
DCHECK(set_protocol);
|
||||
(void)set_protocol; // silence unused-variable warning in Release builds on Windows
|
||||
}
|
||||
protocol_handlers_.clear();
|
||||
job_factory->SetProtocolHandler(
|
||||
content::kDataScheme, new net::DataProtocolHandler);
|
||||
job_factory->SetProtocolHandler(
|
||||
content::kFileScheme,
|
||||
new net::FileProtocolHandler(
|
||||
BrowserThread::GetBlockingPool()->
|
||||
GetTaskRunnerWithShutdownBehavior(
|
||||
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
|
||||
|
||||
// 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());
|
||||
storage_->set_job_factory(delegate_->CreateURLRequestJobFactory(
|
||||
&protocol_handlers_, &protocol_interceptors_));
|
||||
}
|
||||
|
||||
return url_request_context_.get();
|
||||
}
|
||||
|
||||
scoped_refptr<base::SingleThreadTaskRunner>
|
||||
URLRequestContextGetter::GetNetworkTaskRunner() const {
|
||||
scoped_refptr<base::SingleThreadTaskRunner> URLRequestContextGetter::GetNetworkTaskRunner() const {
|
||||
return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#ifndef BRIGHTRAY_BROWSER_URL_REQUEST_CONTEXT_GETTER_H_
|
||||
#define BRIGHTRAY_BROWSER_URL_REQUEST_CONTEXT_GETTER_H_
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "content/public/browser/content_browser_client.h"
|
||||
|
@ -18,46 +17,51 @@ class MessageLoop;
|
|||
namespace net {
|
||||
class HostMappingRules;
|
||||
class HostResolver;
|
||||
class NetworkDelegate;
|
||||
class ProxyConfigService;
|
||||
class URLRequestContextStorage;
|
||||
class URLRequestJobFactory;
|
||||
}
|
||||
|
||||
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:
|
||||
class Delegate {
|
||||
public:
|
||||
Delegate() {}
|
||||
virtual ~Delegate() {}
|
||||
|
||||
virtual net::NetworkDelegate* CreateNetworkDelegate() { return NULL; }
|
||||
virtual net::URLRequestJobFactory* CreateURLRequestJobFactory(
|
||||
content::ProtocolHandlerMap* protocol_handlers,
|
||||
content::ProtocolHandlerScopedVector* protocol_interceptors);
|
||||
};
|
||||
|
||||
URLRequestContextGetter(
|
||||
Delegate* delegate,
|
||||
const base::FilePath& base_path,
|
||||
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();
|
||||
|
||||
net::HostResolver* host_resolver();
|
||||
// net::URLRequestContextGetter:
|
||||
virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE;
|
||||
virtual scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() const OVERRIDE;
|
||||
|
||||
net::HostResolver* host_resolver();
|
||||
|
||||
private:
|
||||
virtual scoped_refptr<base::SingleThreadTaskRunner>
|
||||
GetNetworkTaskRunner() const OVERRIDE;
|
||||
Delegate* delegate_;
|
||||
|
||||
base::FilePath base_path_;
|
||||
base::MessageLoop* io_loop_;
|
||||
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_;
|
||||
scoped_ptr<net::NetworkDelegate> network_delegate_;
|
||||
scoped_ptr<net::URLRequestContextStorage> storage_;
|
||||
scoped_ptr<net::URLRequestContext> url_request_context_;
|
||||
scoped_ptr<net::HostMappingRules> host_mapping_rules_;
|
||||
|
|
Loading…
Reference in a new issue