[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / content / shell / browser / shell_url_request_context_getter.cc
blob40bca8ca98ed875d6d92dd68c1359f4ad64805d2
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/browser/shell_url_request_context_getter.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "base/threading/worker_pool.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/cookie_store_factory.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/url_constants.h"
18 #include "content/shell/browser/shell_network_delegate.h"
19 #include "content/shell/common/shell_switches.h"
20 #include "net/base/cache_type.h"
21 #include "net/cert/cert_verifier.h"
22 #include "net/cookies/cookie_monster.h"
23 #include "net/dns/host_resolver.h"
24 #include "net/dns/mapped_host_resolver.h"
25 #include "net/http/http_auth_handler_factory.h"
26 #include "net/http/http_cache.h"
27 #include "net/http/http_network_session.h"
28 #include "net/http/http_server_properties_impl.h"
29 #include "net/http/transport_security_state.h"
30 #include "net/proxy/proxy_service.h"
31 #include "net/ssl/default_server_bound_cert_store.h"
32 #include "net/ssl/server_bound_cert_service.h"
33 #include "net/ssl/ssl_config_service_defaults.h"
34 #include "net/url_request/data_protocol_handler.h"
35 #include "net/url_request/file_protocol_handler.h"
36 #include "net/url_request/protocol_intercept_job_factory.h"
37 #include "net/url_request/static_http_user_agent_settings.h"
38 #include "net/url_request/url_request_context.h"
39 #include "net/url_request/url_request_context_storage.h"
40 #include "net/url_request/url_request_job_factory_impl.h"
42 namespace content {
44 namespace {
46 void InstallProtocolHandlers(net::URLRequestJobFactoryImpl* job_factory,
47 ProtocolHandlerMap* protocol_handlers) {
48 for (ProtocolHandlerMap::iterator it =
49 protocol_handlers->begin();
50 it != protocol_handlers->end();
51 ++it) {
52 bool set_protocol = job_factory->SetProtocolHandler(
53 it->first, it->second.release());
54 DCHECK(set_protocol);
56 protocol_handlers->clear();
59 } // namespace
61 ShellURLRequestContextGetter::ShellURLRequestContextGetter(
62 bool ignore_certificate_errors,
63 const base::FilePath& base_path,
64 base::MessageLoop* io_loop,
65 base::MessageLoop* file_loop,
66 ProtocolHandlerMap* protocol_handlers,
67 ProtocolHandlerScopedVector protocol_interceptors,
68 net::NetLog* net_log)
69 : ignore_certificate_errors_(ignore_certificate_errors),
70 base_path_(base_path),
71 io_loop_(io_loop),
72 file_loop_(file_loop),
73 net_log_(net_log),
74 protocol_interceptors_(protocol_interceptors.Pass()) {
75 // Must first be created on the UI thread.
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
78 std::swap(protocol_handlers_, *protocol_handlers);
80 // We must create the proxy config service on the UI loop on Linux because it
81 // must synchronously run on the glib message loop. This will be passed to
82 // the URLRequestContextStorage on the IO thread in GetURLRequestContext().
83 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
84 proxy_config_service_.reset(
85 net::ProxyService::CreateSystemProxyConfigService(
86 io_loop_->message_loop_proxy().get(), file_loop_));
90 ShellURLRequestContextGetter::~ShellURLRequestContextGetter() {
93 net::URLRequestContext* ShellURLRequestContextGetter::GetURLRequestContext() {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
96 if (!url_request_context_) {
97 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
99 url_request_context_.reset(new net::URLRequestContext());
100 url_request_context_->set_net_log(net_log_);
101 network_delegate_.reset(new ShellNetworkDelegate);
102 if (command_line.HasSwitch(switches::kDumpRenderTree))
103 ShellNetworkDelegate::SetAcceptAllCookies(false);
104 url_request_context_->set_network_delegate(network_delegate_.get());
105 storage_.reset(
106 new net::URLRequestContextStorage(url_request_context_.get()));
107 storage_->set_cookie_store(
108 content::CreateCookieStore(content::CookieStoreConfig()));
109 storage_->set_server_bound_cert_service(new net::ServerBoundCertService(
110 new net::DefaultServerBoundCertStore(NULL),
111 base::WorkerPool::GetTaskRunner(true)));
112 storage_->set_http_user_agent_settings(
113 new net::StaticHttpUserAgentSettings("en-us,en", std::string()));
115 scoped_ptr<net::HostResolver> host_resolver(
116 net::HostResolver::CreateDefaultResolver(
117 url_request_context_->net_log()));
119 storage_->set_cert_verifier(net::CertVerifier::CreateDefault());
120 storage_->set_transport_security_state(new net::TransportSecurityState);
121 if (command_line.HasSwitch(switches::kDumpRenderTree)) {
122 storage_->set_proxy_service(net::ProxyService::CreateDirect());
123 } else {
124 // TODO(jam): use v8 if possible, look at chrome code.
125 storage_->set_proxy_service(
126 net::ProxyService::CreateUsingSystemProxyResolver(
127 proxy_config_service_.release(),
129 url_request_context_->net_log()));
131 storage_->set_ssl_config_service(new net::SSLConfigServiceDefaults);
132 storage_->set_http_auth_handler_factory(
133 net::HttpAuthHandlerFactory::CreateDefault(host_resolver.get()));
134 storage_->set_http_server_properties(
135 scoped_ptr<net::HttpServerProperties>(
136 new net::HttpServerPropertiesImpl()));
138 base::FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
139 net::HttpCache::DefaultBackend* main_backend =
140 new net::HttpCache::DefaultBackend(
141 net::DISK_CACHE,
142 #if defined(OS_ANDROID)
143 // TODO(rdsmith): Remove when default backend for Android is
144 // changed to simple cache.
145 net::CACHE_BACKEND_SIMPLE,
146 #else
147 net::CACHE_BACKEND_DEFAULT,
148 #endif
149 cache_path,
151 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
152 .get());
154 net::HttpNetworkSession::Params network_session_params;
155 network_session_params.cert_verifier =
156 url_request_context_->cert_verifier();
157 network_session_params.transport_security_state =
158 url_request_context_->transport_security_state();
159 network_session_params.server_bound_cert_service =
160 url_request_context_->server_bound_cert_service();
161 network_session_params.proxy_service =
162 url_request_context_->proxy_service();
163 network_session_params.ssl_config_service =
164 url_request_context_->ssl_config_service();
165 network_session_params.http_auth_handler_factory =
166 url_request_context_->http_auth_handler_factory();
167 network_session_params.network_delegate =
168 network_delegate_.get();
169 network_session_params.http_server_properties =
170 url_request_context_->http_server_properties();
171 network_session_params.net_log =
172 url_request_context_->net_log();
173 network_session_params.ignore_certificate_errors =
174 ignore_certificate_errors_;
175 if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
176 int value;
177 base::StringToInt(command_line.GetSwitchValueASCII(
178 switches::kTestingFixedHttpPort), &value);
179 network_session_params.testing_fixed_http_port = value;
181 if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
182 int value;
183 base::StringToInt(command_line.GetSwitchValueASCII(
184 switches::kTestingFixedHttpsPort), &value);
185 network_session_params.testing_fixed_https_port = value;
187 if (command_line.HasSwitch(switches::kHostResolverRules)) {
188 scoped_ptr<net::MappedHostResolver> mapped_host_resolver(
189 new net::MappedHostResolver(host_resolver.Pass()));
190 mapped_host_resolver->SetRulesFromString(
191 command_line.GetSwitchValueASCII(switches::kHostResolverRules));
192 host_resolver = mapped_host_resolver.Pass();
195 // Give |storage_| ownership at the end in case it's |mapped_host_resolver|.
196 storage_->set_host_resolver(host_resolver.Pass());
197 network_session_params.host_resolver =
198 url_request_context_->host_resolver();
200 net::HttpCache* main_cache = new net::HttpCache(
201 network_session_params, main_backend);
202 storage_->set_http_transaction_factory(main_cache);
204 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
205 new net::URLRequestJobFactoryImpl());
206 // Keep ProtocolHandlers added in sync with
207 // ShellContentBrowserClient::IsHandledURL().
208 InstallProtocolHandlers(job_factory.get(), &protocol_handlers_);
209 bool set_protocol = job_factory->SetProtocolHandler(
210 kDataScheme, new net::DataProtocolHandler);
211 DCHECK(set_protocol);
212 set_protocol = job_factory->SetProtocolHandler(
213 kFileScheme,
214 new net::FileProtocolHandler(
215 content::BrowserThread::GetBlockingPool()->
216 GetTaskRunnerWithShutdownBehavior(
217 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
218 DCHECK(set_protocol);
220 // Set up interceptors in the reverse order.
221 scoped_ptr<net::URLRequestJobFactory> top_job_factory =
222 job_factory.PassAs<net::URLRequestJobFactory>();
223 for (ProtocolHandlerScopedVector::reverse_iterator i =
224 protocol_interceptors_.rbegin();
225 i != protocol_interceptors_.rend();
226 ++i) {
227 top_job_factory.reset(new net::ProtocolInterceptJobFactory(
228 top_job_factory.Pass(), make_scoped_ptr(*i)));
230 protocol_interceptors_.weak_clear();
232 storage_->set_job_factory(top_job_factory.release());
235 return url_request_context_.get();
238 scoped_refptr<base::SingleThreadTaskRunner>
239 ShellURLRequestContextGetter::GetNetworkTaskRunner() const {
240 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
243 net::HostResolver* ShellURLRequestContextGetter::host_resolver() {
244 return url_request_context_->host_resolver();
247 } // namespace content