2013-03-13 19:12:05 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-CHROMIUM file.
|
|
|
|
|
2013-03-27 12:53:35 +00:00
|
|
|
#include "browser/url_request_context_getter.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
|
|
|
|
#include "network_delegate.h"
|
2013-10-07 19:16:49 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
#include "base/threading/worker_pool.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
2013-04-18 03:27:53 +00:00
|
|
|
#include "content/public/browser/cookie_store_factory.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
#include "content/public/common/url_constants.h"
|
2013-04-18 03:27:53 +00:00
|
|
|
#include "net/cert/cert_verifier.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
#include "net/cookies/cookie_monster.h"
|
|
|
|
#include "net/http/http_auth_handler_factory.h"
|
|
|
|
#include "net/http/http_cache.h"
|
|
|
|
#include "net/http/http_server_properties_impl.h"
|
|
|
|
#include "net/proxy/proxy_service.h"
|
2013-03-21 19:09:00 +00:00
|
|
|
#include "net/ssl/default_server_bound_cert_store.h"
|
|
|
|
#include "net/ssl/server_bound_cert_service.h"
|
|
|
|
#include "net/ssl/ssl_config_service_defaults.h"
|
2013-06-01 16:23:59 +00:00
|
|
|
#include "net/url_request/data_protocol_handler.h"
|
|
|
|
#include "net/url_request/file_protocol_handler.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
#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_storage.h"
|
|
|
|
#include "net/url_request/url_request_job_factory_impl.h"
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
URLRequestContextGetter::URLRequestContextGetter(
|
|
|
|
const base::FilePath& base_path,
|
2013-08-23 20:56:41 +00:00
|
|
|
base::MessageLoop* io_loop,
|
|
|
|
base::MessageLoop* file_loop,
|
2013-07-17 14:21:33 +00:00
|
|
|
scoped_ptr<NetworkDelegate> network_delegate,
|
2013-03-13 19:12:05 +00:00
|
|
|
content::ProtocolHandlerMap* protocol_handlers)
|
|
|
|
: base_path_(base_path),
|
|
|
|
io_loop_(io_loop),
|
2013-07-17 14:21:33 +00:00
|
|
|
file_loop_(file_loop),
|
|
|
|
network_delegate_(network_delegate.Pass()) {
|
2013-03-13 19:12:05 +00:00
|
|
|
// Must first be created on the UI thread.
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
|
|
|
|
|
|
|
std::swap(protocol_handlers_, *protocol_handlers);
|
|
|
|
|
|
|
|
proxy_config_service_.reset(net::ProxyService::CreateSystemProxyConfigService(io_loop_->message_loop_proxy(), file_loop_));
|
|
|
|
}
|
|
|
|
|
|
|
|
URLRequestContextGetter::~URLRequestContextGetter() {
|
|
|
|
}
|
|
|
|
|
|
|
|
net::HostResolver* URLRequestContextGetter::host_resolver() {
|
|
|
|
return url_request_context_->host_resolver();
|
|
|
|
}
|
|
|
|
|
|
|
|
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext()
|
|
|
|
{
|
|
|
|
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
|
|
|
|
|
|
|
if (!url_request_context_.get()) {
|
|
|
|
url_request_context_.reset(new net::URLRequestContext());
|
|
|
|
url_request_context_->set_network_delegate(network_delegate_.get());
|
|
|
|
storage_.reset(
|
|
|
|
new net::URLRequestContextStorage(url_request_context_.get()));
|
2013-04-18 03:27:53 +00:00
|
|
|
storage_->set_cookie_store(content::CreatePersistentCookieStore(
|
|
|
|
base_path_.Append(FILE_PATH_LITERAL("Cookies")),
|
|
|
|
false,
|
|
|
|
nullptr,
|
|
|
|
nullptr));
|
2013-03-13 19:12:05 +00:00
|
|
|
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(
|
2013-03-21 19:09:00 +00:00
|
|
|
"en-us,en", EmptyString()));
|
2013-03-13 19:12:05 +00:00
|
|
|
|
|
|
|
scoped_ptr<net::HostResolver> host_resolver(
|
|
|
|
net::HostResolver::CreateDefaultResolver(NULL));
|
|
|
|
|
|
|
|
storage_->set_cert_verifier(net::CertVerifier::CreateDefault());
|
|
|
|
// TODO(jam): use v8 if possible, look at chrome code.
|
|
|
|
storage_->set_proxy_service(
|
|
|
|
net::ProxyService::CreateUsingSystemProxyResolver(
|
|
|
|
proxy_config_service_.release(),
|
|
|
|
0,
|
|
|
|
NULL));
|
|
|
|
storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults);
|
|
|
|
storage_->set_http_auth_handler_factory(
|
|
|
|
net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get()));
|
2013-10-07 20:21:01 +00:00
|
|
|
scoped_ptr<net::HttpServerProperties> server_properties(new net::HttpServerPropertiesImpl);
|
|
|
|
storage_->set_http_server_properties(server_properties.Pass());
|
2013-03-13 19:12:05 +00:00
|
|
|
|
|
|
|
base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
|
|
|
|
net::HttpCache::DefaultBackend* main_backend =
|
|
|
|
new net::HttpCache::DefaultBackend(
|
|
|
|
net::DISK_CACHE,
|
2013-05-30 19:04:51 +00:00
|
|
|
net::CACHE_BACKEND_DEFAULT,
|
2013-03-13 19:12:05 +00:00
|
|
|
cache_path,
|
|
|
|
0,
|
|
|
|
content::BrowserThread::GetMessageLoopProxyForThread(
|
|
|
|
content::BrowserThread::CACHE));
|
|
|
|
|
|
|
|
net::HttpNetworkSession::Params network_session_params;
|
|
|
|
network_session_params.cert_verifier =
|
|
|
|
url_request_context_->cert_verifier();
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
net::HttpCache* main_cache = new net::HttpCache(
|
|
|
|
network_session_params, main_backend);
|
|
|
|
storage_->set_http_transaction_factory(main_cache);
|
|
|
|
|
|
|
|
scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
|
|
|
|
new net::URLRequestJobFactoryImpl());
|
2013-05-14 18:50:24 +00:00
|
|
|
for (auto it = protocol_handlers_.begin(),
|
|
|
|
end = protocol_handlers_.end(); it != end; ++it) {
|
|
|
|
bool set_protocol = job_factory->SetProtocolHandler(it->first, it->second.release());
|
2013-03-13 19:12:05 +00:00
|
|
|
DCHECK(set_protocol);
|
|
|
|
}
|
|
|
|
protocol_handlers_.clear();
|
2013-06-01 16:23:59 +00:00
|
|
|
job_factory->SetProtocolHandler(chrome::kDataScheme, new net::DataProtocolHandler);
|
|
|
|
job_factory->SetProtocolHandler(chrome::kFileScheme, new net::FileProtocolHandler);
|
2013-03-13 19:12:05 +00:00
|
|
|
storage_->set_job_factory(job_factory.release());
|
|
|
|
}
|
|
|
|
|
|
|
|
return url_request_context_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> URLRequestContextGetter::GetNetworkTaskRunner() const
|
|
|
|
{
|
|
|
|
return content::BrowserThread::GetMessageLoopProxyForThread(content::BrowserThread::IO);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|