Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / url_request / url_request_context_builder.cc
blobb58a0da1c26507b71dfc570593b95df61d6e087a
1 // Copyright (c) 2012 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 "net/url_request/url_request_context_builder.h"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/thread.h"
15 #include "net/base/cache_type.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/network_delegate.h"
18 #include "net/cert/cert_verifier.h"
19 #include "net/cookies/cookie_monster.h"
20 #include "net/dns/host_resolver.h"
21 #include "net/ftp/ftp_network_layer.h"
22 #include "net/http/http_auth_handler_factory.h"
23 #include "net/http/http_cache.h"
24 #include "net/http/http_network_layer.h"
25 #include "net/http/http_network_session.h"
26 #include "net/http/http_server_properties_impl.h"
27 #include "net/http/transport_security_persister.h"
28 #include "net/http/transport_security_state.h"
29 #include "net/ssl/default_server_bound_cert_store.h"
30 #include "net/ssl/server_bound_cert_service.h"
31 #include "net/ssl/ssl_config_service_defaults.h"
32 #include "net/url_request/data_protocol_handler.h"
33 #include "net/url_request/static_http_user_agent_settings.h"
34 #include "net/url_request/url_request_context.h"
35 #include "net/url_request/url_request_context_storage.h"
36 #include "net/url_request/url_request_job_factory_impl.h"
37 #include "net/url_request/url_request_throttler_manager.h"
39 #if !defined(DISABLE_FILE_SUPPORT)
40 #include "net/url_request/file_protocol_handler.h"
41 #endif
43 #if !defined(DISABLE_FTP_SUPPORT)
44 #include "net/url_request/ftp_protocol_handler.h"
45 #endif
47 namespace net {
49 namespace {
51 class BasicNetworkDelegate : public NetworkDelegate {
52 public:
53 BasicNetworkDelegate() {}
54 virtual ~BasicNetworkDelegate() {}
56 private:
57 virtual int OnBeforeURLRequest(URLRequest* request,
58 const CompletionCallback& callback,
59 GURL* new_url) OVERRIDE {
60 return OK;
63 virtual int OnBeforeSendHeaders(URLRequest* request,
64 const CompletionCallback& callback,
65 HttpRequestHeaders* headers) OVERRIDE {
66 return OK;
69 virtual void OnSendHeaders(URLRequest* request,
70 const HttpRequestHeaders& headers) OVERRIDE {}
72 virtual int OnHeadersReceived(
73 URLRequest* request,
74 const CompletionCallback& callback,
75 const HttpResponseHeaders* original_response_headers,
76 scoped_refptr<HttpResponseHeaders>* override_response_headers,
77 GURL* allowed_unsafe_redirect_url) OVERRIDE {
78 return OK;
81 virtual void OnBeforeRedirect(URLRequest* request,
82 const GURL& new_location) OVERRIDE {}
84 virtual void OnResponseStarted(URLRequest* request) OVERRIDE {}
86 virtual void OnRawBytesRead(const URLRequest& request,
87 int bytes_read) OVERRIDE {}
89 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE {}
91 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE {}
93 virtual void OnPACScriptError(int line_number,
94 const base::string16& error) OVERRIDE {}
96 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
97 URLRequest* request,
98 const AuthChallengeInfo& auth_info,
99 const AuthCallback& callback,
100 AuthCredentials* credentials) OVERRIDE {
101 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
104 virtual bool OnCanGetCookies(const URLRequest& request,
105 const CookieList& cookie_list) OVERRIDE {
106 return true;
109 virtual bool OnCanSetCookie(const URLRequest& request,
110 const std::string& cookie_line,
111 CookieOptions* options) OVERRIDE {
112 return true;
115 virtual bool OnCanAccessFile(const net::URLRequest& request,
116 const base::FilePath& path) const OVERRIDE {
117 return true;
120 virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE {
121 // Returning true will only enable throttling if there's also a
122 // URLRequestThrottlerManager, which there isn't, by default.
123 return true;
126 virtual int OnBeforeSocketStreamConnect(
127 SocketStream* stream,
128 const CompletionCallback& callback) OVERRIDE {
129 return OK;
132 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
135 class BasicURLRequestContext : public URLRequestContext {
136 public:
137 BasicURLRequestContext()
138 : storage_(this) {}
140 URLRequestContextStorage* storage() {
141 return &storage_;
144 base::Thread* GetCacheThread() {
145 if (!cache_thread_) {
146 cache_thread_.reset(new base::Thread("Network Cache Thread"));
147 cache_thread_->StartWithOptions(
148 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
150 return cache_thread_.get();
153 base::Thread* GetFileThread() {
154 if (!file_thread_) {
155 file_thread_.reset(new base::Thread("Network File Thread"));
156 file_thread_->StartWithOptions(
157 base::Thread::Options(base::MessageLoop::TYPE_DEFAULT, 0));
159 return file_thread_.get();
162 void set_transport_security_persister(
163 scoped_ptr<TransportSecurityPersister> transport_security_persister) {
164 transport_security_persister = transport_security_persister.Pass();
167 protected:
168 virtual ~BasicURLRequestContext() {}
170 private:
171 // Threads should be torn down last.
172 scoped_ptr<base::Thread> cache_thread_;
173 scoped_ptr<base::Thread> file_thread_;
175 URLRequestContextStorage storage_;
176 scoped_ptr<TransportSecurityPersister> transport_security_persister_;
178 DISALLOW_COPY_AND_ASSIGN(BasicURLRequestContext);
181 } // namespace
183 URLRequestContextBuilder::HttpCacheParams::HttpCacheParams()
184 : type(IN_MEMORY),
185 max_size(0) {}
186 URLRequestContextBuilder::HttpCacheParams::~HttpCacheParams() {}
188 URLRequestContextBuilder::HttpNetworkSessionParams::HttpNetworkSessionParams()
189 : ignore_certificate_errors(false),
190 host_mapping_rules(NULL),
191 testing_fixed_http_port(0),
192 testing_fixed_https_port(0),
193 next_protos(NextProtosDefaults()),
194 use_alternate_protocols(true) {
197 URLRequestContextBuilder::HttpNetworkSessionParams::~HttpNetworkSessionParams()
200 URLRequestContextBuilder::SchemeFactory::SchemeFactory(
201 const std::string& auth_scheme,
202 net::HttpAuthHandlerFactory* auth_handler_factory)
203 : scheme(auth_scheme), factory(auth_handler_factory) {
206 URLRequestContextBuilder::SchemeFactory::~SchemeFactory() {
209 URLRequestContextBuilder::URLRequestContextBuilder()
210 : data_enabled_(false),
211 #if !defined(DISABLE_FILE_SUPPORT)
212 file_enabled_(false),
213 #endif
214 #if !defined(DISABLE_FTP_SUPPORT)
215 ftp_enabled_(false),
216 #endif
217 http_cache_enabled_(true),
218 throttling_enabled_(false) {
221 URLRequestContextBuilder::~URLRequestContextBuilder() {}
223 void URLRequestContextBuilder::EnableHttpCache(const HttpCacheParams& params) {
224 http_cache_enabled_ = true;
225 http_cache_params_ = params;
228 void URLRequestContextBuilder::DisableHttpCache() {
229 http_cache_enabled_ = false;
230 http_cache_params_ = HttpCacheParams();
233 void URLRequestContextBuilder::SetSpdyAndQuicEnabled(bool spdy_enabled,
234 bool quic_enabled) {
235 http_network_session_params_.next_protos =
236 NextProtosWithSpdyAndQuic(spdy_enabled, quic_enabled);
239 URLRequestContext* URLRequestContextBuilder::Build() {
240 BasicURLRequestContext* context = new BasicURLRequestContext;
241 URLRequestContextStorage* storage = context->storage();
243 storage->set_http_user_agent_settings(new StaticHttpUserAgentSettings(
244 accept_language_, user_agent_));
246 if (!network_delegate_)
247 network_delegate_.reset(new BasicNetworkDelegate);
248 NetworkDelegate* network_delegate = network_delegate_.release();
249 storage->set_network_delegate(network_delegate);
251 if (net_log_) {
252 storage->set_net_log(net_log_.release());
253 } else {
254 storage->set_net_log(new net::NetLog);
257 if (!host_resolver_) {
258 host_resolver_ = net::HostResolver::CreateDefaultResolver(
259 context->net_log());
261 storage->set_host_resolver(host_resolver_.Pass());
263 if (!proxy_service_) {
264 // TODO(willchan): Switch to using this code when
265 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck.
266 #if defined(OS_LINUX) || defined(OS_ANDROID)
267 ProxyConfigService* proxy_config_service = proxy_config_service_.release();
268 #else
269 ProxyConfigService* proxy_config_service = NULL;
270 if (proxy_config_service_) {
271 proxy_config_service = proxy_config_service_.release();
272 } else {
273 proxy_config_service =
274 ProxyService::CreateSystemProxyConfigService(
275 base::ThreadTaskRunnerHandle::Get().get(),
276 context->GetFileThread()->message_loop());
278 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
279 proxy_service_.reset(
280 ProxyService::CreateUsingSystemProxyResolver(
281 proxy_config_service,
282 0, // This results in using the default value.
283 context->net_log()));
285 storage->set_proxy_service(proxy_service_.release());
287 storage->set_ssl_config_service(new net::SSLConfigServiceDefaults);
288 HttpAuthHandlerRegistryFactory* http_auth_handler_registry_factory =
289 net::HttpAuthHandlerRegistryFactory::CreateDefault(
290 context->host_resolver());
291 for (size_t i = 0; i < extra_http_auth_handlers_.size(); ++i) {
292 http_auth_handler_registry_factory->RegisterSchemeFactory(
293 extra_http_auth_handlers_[i].scheme,
294 extra_http_auth_handlers_[i].factory);
296 storage->set_http_auth_handler_factory(http_auth_handler_registry_factory);
297 storage->set_cookie_store(new CookieMonster(NULL, NULL));
299 // TODO(mmenke): This always creates a file thread, even when it ends up
300 // not being used. Consider lazily creating the thread.
301 storage->set_server_bound_cert_service(
302 new ServerBoundCertService(
303 new DefaultServerBoundCertStore(NULL),
304 context->GetFileThread()->message_loop_proxy()));
306 storage->set_transport_security_state(new net::TransportSecurityState());
307 if (!transport_security_persister_path_.empty()) {
308 context->set_transport_security_persister(
309 make_scoped_ptr<TransportSecurityPersister>(
310 new TransportSecurityPersister(
311 context->transport_security_state(),
312 transport_security_persister_path_,
313 context->GetFileThread()->message_loop_proxy(),
314 false)));
317 storage->set_http_server_properties(
318 scoped_ptr<net::HttpServerProperties>(
319 new net::HttpServerPropertiesImpl()));
320 storage->set_cert_verifier(CertVerifier::CreateDefault());
322 if (throttling_enabled_)
323 storage->set_throttler_manager(new URLRequestThrottlerManager());
325 net::HttpNetworkSession::Params network_session_params;
326 network_session_params.host_resolver = context->host_resolver();
327 network_session_params.cert_verifier = context->cert_verifier();
328 network_session_params.transport_security_state =
329 context->transport_security_state();
330 network_session_params.proxy_service = context->proxy_service();
331 network_session_params.ssl_config_service =
332 context->ssl_config_service();
333 network_session_params.http_auth_handler_factory =
334 context->http_auth_handler_factory();
335 network_session_params.network_delegate = network_delegate;
336 network_session_params.http_server_properties =
337 context->http_server_properties();
338 network_session_params.net_log = context->net_log();
340 network_session_params.ignore_certificate_errors =
341 http_network_session_params_.ignore_certificate_errors;
342 network_session_params.host_mapping_rules =
343 http_network_session_params_.host_mapping_rules;
344 network_session_params.testing_fixed_http_port =
345 http_network_session_params_.testing_fixed_http_port;
346 network_session_params.testing_fixed_https_port =
347 http_network_session_params_.testing_fixed_https_port;
348 network_session_params.use_alternate_protocols =
349 http_network_session_params_.use_alternate_protocols;
350 network_session_params.trusted_spdy_proxy =
351 http_network_session_params_.trusted_spdy_proxy;
352 network_session_params.next_protos = http_network_session_params_.next_protos;
354 HttpTransactionFactory* http_transaction_factory = NULL;
355 if (http_cache_enabled_) {
356 network_session_params.server_bound_cert_service =
357 context->server_bound_cert_service();
358 HttpCache::BackendFactory* http_cache_backend = NULL;
359 if (http_cache_params_.type == HttpCacheParams::DISK) {
360 http_cache_backend = new HttpCache::DefaultBackend(
361 DISK_CACHE,
362 net::CACHE_BACKEND_DEFAULT,
363 http_cache_params_.path,
364 http_cache_params_.max_size,
365 context->GetCacheThread()->message_loop_proxy().get());
366 } else {
367 http_cache_backend =
368 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size);
371 http_transaction_factory = new HttpCache(
372 network_session_params, http_cache_backend);
373 } else {
374 scoped_refptr<net::HttpNetworkSession> network_session(
375 new net::HttpNetworkSession(network_session_params));
377 http_transaction_factory = new HttpNetworkLayer(network_session.get());
379 storage->set_http_transaction_factory(http_transaction_factory);
381 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl;
382 if (data_enabled_)
383 job_factory->SetProtocolHandler("data", new DataProtocolHandler);
385 #if !defined(DISABLE_FILE_SUPPORT)
386 if (file_enabled_) {
387 job_factory->SetProtocolHandler(
388 "file",
389 new FileProtocolHandler(context->GetFileThread()->message_loop_proxy()));
391 #endif // !defined(DISABLE_FILE_SUPPORT)
393 #if !defined(DISABLE_FTP_SUPPORT)
394 if (ftp_enabled_) {
395 ftp_transaction_factory_.reset(
396 new FtpNetworkLayer(context->host_resolver()));
397 job_factory->SetProtocolHandler("ftp",
398 new FtpProtocolHandler(ftp_transaction_factory_.get()));
400 #endif // !defined(DISABLE_FTP_SUPPORT)
402 storage->set_job_factory(job_factory);
404 // TODO(willchan): Support sdch.
406 return context;
409 } // namespace net