We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / url_request / url_request_context_builder.cc
blobaa589f564c61bfa00c35077f50174bf5fd3994cf
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/single_thread_task_runner.h"
13 #include "base/strings/string_util.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/threading/thread.h"
16 #include "net/base/cache_type.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/network_delegate_impl.h"
19 #include "net/cert/cert_verifier.h"
20 #include "net/cookies/cookie_monster.h"
21 #include "net/dns/host_resolver.h"
22 #include "net/ftp/ftp_network_layer.h"
23 #include "net/http/http_auth_handler_factory.h"
24 #include "net/http/http_cache.h"
25 #include "net/http/http_network_layer.h"
26 #include "net/http/http_network_session.h"
27 #include "net/http/http_server_properties_impl.h"
28 #include "net/http/transport_security_persister.h"
29 #include "net/http/transport_security_state.h"
30 #include "net/ssl/channel_id_service.h"
31 #include "net/ssl/default_channel_id_store.h"
32 #include "net/ssl/ssl_config_service_defaults.h"
33 #include "net/url_request/data_protocol_handler.h"
34 #include "net/url_request/static_http_user_agent_settings.h"
35 #include "net/url_request/url_request_context.h"
36 #include "net/url_request/url_request_context_storage.h"
37 #include "net/url_request/url_request_job_factory_impl.h"
38 #include "net/url_request/url_request_throttler_manager.h"
40 #if !defined(DISABLE_FILE_SUPPORT)
41 #include "net/url_request/file_protocol_handler.h"
42 #endif
44 #if !defined(DISABLE_FTP_SUPPORT)
45 #include "net/url_request/ftp_protocol_handler.h"
46 #endif
48 namespace net {
50 namespace {
52 class BasicNetworkDelegate : public NetworkDelegateImpl {
53 public:
54 BasicNetworkDelegate() {}
55 ~BasicNetworkDelegate() override {}
57 private:
58 int OnBeforeURLRequest(URLRequest* request,
59 const CompletionCallback& callback,
60 GURL* new_url) override {
61 return OK;
64 int OnBeforeSendHeaders(URLRequest* request,
65 const CompletionCallback& callback,
66 HttpRequestHeaders* headers) override {
67 return OK;
70 void OnSendHeaders(URLRequest* request,
71 const HttpRequestHeaders& headers) override {}
73 int OnHeadersReceived(
74 URLRequest* request,
75 const CompletionCallback& callback,
76 const HttpResponseHeaders* original_response_headers,
77 scoped_refptr<HttpResponseHeaders>* override_response_headers,
78 GURL* allowed_unsafe_redirect_url) override {
79 return OK;
82 void OnBeforeRedirect(URLRequest* request,
83 const GURL& new_location) override {}
85 void OnResponseStarted(URLRequest* request) override {}
87 void OnRawBytesRead(const URLRequest& request, int bytes_read) override {}
89 void OnCompleted(URLRequest* request, bool started) override {}
91 void OnURLRequestDestroyed(URLRequest* request) override {}
93 void OnPACScriptError(int line_number, const base::string16& error) override {
96 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 bool OnCanGetCookies(const URLRequest& request,
105 const CookieList& cookie_list) override {
106 return true;
109 bool OnCanSetCookie(const URLRequest& request,
110 const std::string& cookie_line,
111 CookieOptions* options) override {
112 return true;
115 bool OnCanAccessFile(const net::URLRequest& request,
116 const base::FilePath& path) const override {
117 return true;
120 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 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
129 class BasicURLRequestContext : public URLRequestContext {
130 public:
131 explicit BasicURLRequestContext(
132 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
133 : file_task_runner_(file_task_runner), storage_(this) {}
135 URLRequestContextStorage* storage() {
136 return &storage_;
139 scoped_refptr<base::SingleThreadTaskRunner>& GetFileTaskRunner() {
140 // Create a new thread to run file tasks, if needed.
141 if (!file_task_runner_) {
142 DCHECK(!file_thread_);
143 file_thread_.reset(new base::Thread("Network File Thread"));
144 file_thread_->StartWithOptions(
145 base::Thread::Options(base::MessageLoop::TYPE_DEFAULT, 0));
146 file_task_runner_ = file_thread_->task_runner();
148 return file_task_runner_;
151 void set_transport_security_persister(
152 scoped_ptr<TransportSecurityPersister> transport_security_persister) {
153 transport_security_persister = transport_security_persister.Pass();
156 protected:
157 ~BasicURLRequestContext() override { AssertNoURLRequests(); }
159 private:
160 // The thread should be torn down last.
161 scoped_ptr<base::Thread> file_thread_;
162 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
164 URLRequestContextStorage storage_;
165 scoped_ptr<TransportSecurityPersister> transport_security_persister_;
167 DISALLOW_COPY_AND_ASSIGN(BasicURLRequestContext);
170 } // namespace
172 URLRequestContextBuilder::HttpCacheParams::HttpCacheParams()
173 : type(IN_MEMORY),
174 max_size(0) {}
175 URLRequestContextBuilder::HttpCacheParams::~HttpCacheParams() {}
177 URLRequestContextBuilder::HttpNetworkSessionParams::HttpNetworkSessionParams()
178 : ignore_certificate_errors(false),
179 host_mapping_rules(NULL),
180 testing_fixed_http_port(0),
181 testing_fixed_https_port(0),
182 next_protos(NextProtosDefaults()),
183 use_alternate_protocols(true),
184 enable_quic(false) {
187 URLRequestContextBuilder::HttpNetworkSessionParams::~HttpNetworkSessionParams()
190 URLRequestContextBuilder::SchemeFactory::SchemeFactory(
191 const std::string& auth_scheme,
192 net::HttpAuthHandlerFactory* auth_handler_factory)
193 : scheme(auth_scheme), factory(auth_handler_factory) {
196 URLRequestContextBuilder::SchemeFactory::~SchemeFactory() {
199 URLRequestContextBuilder::URLRequestContextBuilder()
200 : data_enabled_(false),
201 #if !defined(DISABLE_FILE_SUPPORT)
202 file_enabled_(false),
203 #endif
204 #if !defined(DISABLE_FTP_SUPPORT)
205 ftp_enabled_(false),
206 #endif
207 http_cache_enabled_(true),
208 throttling_enabled_(false) {
211 URLRequestContextBuilder::~URLRequestContextBuilder() {}
213 void URLRequestContextBuilder::EnableHttpCache(const HttpCacheParams& params) {
214 http_cache_enabled_ = true;
215 http_cache_params_ = params;
218 void URLRequestContextBuilder::DisableHttpCache() {
219 http_cache_enabled_ = false;
220 http_cache_params_ = HttpCacheParams();
223 void URLRequestContextBuilder::SetSpdyAndQuicEnabled(bool spdy_enabled,
224 bool quic_enabled) {
225 http_network_session_params_.next_protos =
226 NextProtosWithSpdyAndQuic(spdy_enabled, quic_enabled);
227 http_network_session_params_.enable_quic = quic_enabled;
230 void URLRequestContextBuilder::SetCookieAndChannelIdStores(
231 const scoped_refptr<CookieStore>& cookie_store,
232 scoped_ptr<ChannelIDService> channel_id_service) {
233 DCHECK(cookie_store);
234 cookie_store_ = cookie_store;
235 channel_id_service_ = channel_id_service.Pass();
238 void URLRequestContextBuilder::SetFileTaskRunner(
239 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) {
240 file_task_runner_ = task_runner;
243 URLRequestContext* URLRequestContextBuilder::Build() {
244 BasicURLRequestContext* context =
245 new BasicURLRequestContext(file_task_runner_);
246 URLRequestContextStorage* storage = context->storage();
248 storage->set_http_user_agent_settings(new StaticHttpUserAgentSettings(
249 accept_language_, user_agent_));
251 if (!network_delegate_)
252 network_delegate_.reset(new BasicNetworkDelegate);
253 NetworkDelegate* network_delegate = network_delegate_.release();
254 storage->set_network_delegate(network_delegate);
256 if (net_log_) {
257 storage->set_net_log(net_log_.release());
258 } else {
259 storage->set_net_log(new net::NetLog);
262 if (!host_resolver_) {
263 host_resolver_ = net::HostResolver::CreateDefaultResolver(
264 context->net_log());
266 storage->set_host_resolver(host_resolver_.Pass());
268 if (!proxy_service_) {
269 // TODO(willchan): Switch to using this code when
270 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck.
271 #if defined(OS_LINUX) || defined(OS_ANDROID)
272 ProxyConfigService* proxy_config_service = proxy_config_service_.release();
273 #else
274 ProxyConfigService* proxy_config_service = NULL;
275 if (proxy_config_service_) {
276 proxy_config_service = proxy_config_service_.release();
277 } else {
278 proxy_config_service = ProxyService::CreateSystemProxyConfigService(
279 base::ThreadTaskRunnerHandle::Get().get(),
280 context->GetFileTaskRunner());
282 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
283 proxy_service_.reset(
284 ProxyService::CreateUsingSystemProxyResolver(
285 proxy_config_service,
286 0, // This results in using the default value.
287 context->net_log()));
289 storage->set_proxy_service(proxy_service_.release());
291 storage->set_ssl_config_service(new net::SSLConfigServiceDefaults);
292 HttpAuthHandlerRegistryFactory* http_auth_handler_registry_factory =
293 net::HttpAuthHandlerRegistryFactory::CreateDefault(
294 context->host_resolver());
295 for (size_t i = 0; i < extra_http_auth_handlers_.size(); ++i) {
296 http_auth_handler_registry_factory->RegisterSchemeFactory(
297 extra_http_auth_handlers_[i].scheme,
298 extra_http_auth_handlers_[i].factory);
300 storage->set_http_auth_handler_factory(http_auth_handler_registry_factory);
302 if (cookie_store_) {
303 storage->set_cookie_store(cookie_store_.get());
304 storage->set_channel_id_service(channel_id_service_.Pass());
305 } else {
306 storage->set_cookie_store(new CookieMonster(NULL, NULL));
307 // TODO(mmenke): This always creates a file thread, even when it ends up
308 // not being used. Consider lazily creating the thread.
309 storage->set_channel_id_service(make_scoped_ptr(new ChannelIDService(
310 new DefaultChannelIDStore(NULL), context->GetFileTaskRunner())));
313 storage->set_transport_security_state(new net::TransportSecurityState());
314 if (!transport_security_persister_path_.empty()) {
315 context->set_transport_security_persister(
316 make_scoped_ptr<TransportSecurityPersister>(
317 new TransportSecurityPersister(context->transport_security_state(),
318 transport_security_persister_path_,
319 context->GetFileTaskRunner(),
320 false)));
323 storage->set_http_server_properties(
324 scoped_ptr<net::HttpServerProperties>(
325 new net::HttpServerPropertiesImpl()));
326 storage->set_cert_verifier(CertVerifier::CreateDefault());
328 if (throttling_enabled_)
329 storage->set_throttler_manager(new URLRequestThrottlerManager());
331 net::HttpNetworkSession::Params network_session_params;
332 network_session_params.host_resolver = context->host_resolver();
333 network_session_params.cert_verifier = context->cert_verifier();
334 network_session_params.transport_security_state =
335 context->transport_security_state();
336 network_session_params.proxy_service = context->proxy_service();
337 network_session_params.ssl_config_service =
338 context->ssl_config_service();
339 network_session_params.http_auth_handler_factory =
340 context->http_auth_handler_factory();
341 network_session_params.network_delegate = network_delegate;
342 network_session_params.http_server_properties =
343 context->http_server_properties();
344 network_session_params.net_log = context->net_log();
346 network_session_params.ignore_certificate_errors =
347 http_network_session_params_.ignore_certificate_errors;
348 network_session_params.host_mapping_rules =
349 http_network_session_params_.host_mapping_rules;
350 network_session_params.testing_fixed_http_port =
351 http_network_session_params_.testing_fixed_http_port;
352 network_session_params.testing_fixed_https_port =
353 http_network_session_params_.testing_fixed_https_port;
354 network_session_params.use_alternate_protocols =
355 http_network_session_params_.use_alternate_protocols;
356 network_session_params.trusted_spdy_proxy =
357 http_network_session_params_.trusted_spdy_proxy;
358 network_session_params.next_protos = http_network_session_params_.next_protos;
359 network_session_params.enable_quic = http_network_session_params_.enable_quic;
360 network_session_params.quic_connection_options =
361 http_network_session_params_.quic_connection_options;
363 HttpTransactionFactory* http_transaction_factory = NULL;
364 if (http_cache_enabled_) {
365 network_session_params.channel_id_service =
366 context->channel_id_service();
367 HttpCache::BackendFactory* http_cache_backend = NULL;
368 if (http_cache_params_.type == HttpCacheParams::DISK) {
369 http_cache_backend = new HttpCache::DefaultBackend(
370 DISK_CACHE, net::CACHE_BACKEND_DEFAULT, http_cache_params_.path,
371 http_cache_params_.max_size, context->GetFileTaskRunner());
372 } else {
373 http_cache_backend =
374 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size);
377 http_transaction_factory = new HttpCache(
378 network_session_params, http_cache_backend);
379 } else {
380 scoped_refptr<net::HttpNetworkSession> network_session(
381 new net::HttpNetworkSession(network_session_params));
383 http_transaction_factory = new HttpNetworkLayer(network_session.get());
385 storage->set_http_transaction_factory(http_transaction_factory);
387 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl;
388 if (data_enabled_)
389 job_factory->SetProtocolHandler("data", new DataProtocolHandler);
391 #if !defined(DISABLE_FILE_SUPPORT)
392 if (file_enabled_) {
393 job_factory->SetProtocolHandler(
394 "file", new FileProtocolHandler(context->GetFileTaskRunner()));
396 #endif // !defined(DISABLE_FILE_SUPPORT)
398 #if !defined(DISABLE_FTP_SUPPORT)
399 if (ftp_enabled_) {
400 ftp_transaction_factory_.reset(
401 new FtpNetworkLayer(context->host_resolver()));
402 job_factory->SetProtocolHandler("ftp",
403 new FtpProtocolHandler(ftp_transaction_factory_.get()));
405 #endif // !defined(DISABLE_FTP_SUPPORT)
407 storage->set_job_factory(job_factory);
409 // TODO(willchan): Support sdch.
411 return context;
414 } // namespace net