Add a function to create a bookmark app from a WebApplicationInfo.
[chromium-blink-merge.git] / net / url_request / url_request_context_builder.cc
blob84051635266dfb7e42f665719b4c0fed3c915b32
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_state.h"
28 #include "net/proxy/proxy_service.h"
29 #include "net/ssl/ssl_config_service_defaults.h"
30 #include "net/url_request/data_protocol_handler.h"
31 #include "net/url_request/file_protocol_handler.h"
32 #include "net/url_request/ftp_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"
38 namespace net {
40 namespace {
42 class BasicNetworkDelegate : public NetworkDelegate {
43 public:
44 BasicNetworkDelegate() {}
45 virtual ~BasicNetworkDelegate() {}
47 private:
48 virtual int OnBeforeURLRequest(URLRequest* request,
49 const CompletionCallback& callback,
50 GURL* new_url) OVERRIDE {
51 return OK;
54 virtual int OnBeforeSendHeaders(URLRequest* request,
55 const CompletionCallback& callback,
56 HttpRequestHeaders* headers) OVERRIDE {
57 return OK;
60 virtual void OnSendHeaders(URLRequest* request,
61 const HttpRequestHeaders& headers) OVERRIDE {}
63 virtual int OnHeadersReceived(
64 URLRequest* request,
65 const CompletionCallback& callback,
66 const HttpResponseHeaders* original_response_headers,
67 scoped_refptr<HttpResponseHeaders>* override_response_headers,
68 GURL* allowed_unsafe_redirect_url) OVERRIDE {
69 return OK;
72 virtual void OnBeforeRedirect(URLRequest* request,
73 const GURL& new_location) OVERRIDE {}
75 virtual void OnResponseStarted(URLRequest* request) OVERRIDE {}
77 virtual void OnRawBytesRead(const URLRequest& request,
78 int bytes_read) OVERRIDE {}
80 virtual void OnCompleted(URLRequest* request, bool started) OVERRIDE {}
82 virtual void OnURLRequestDestroyed(URLRequest* request) OVERRIDE {}
84 virtual void OnPACScriptError(int line_number,
85 const base::string16& error) OVERRIDE {}
87 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
88 URLRequest* request,
89 const AuthChallengeInfo& auth_info,
90 const AuthCallback& callback,
91 AuthCredentials* credentials) OVERRIDE {
92 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
95 virtual bool OnCanGetCookies(const URLRequest& request,
96 const CookieList& cookie_list) OVERRIDE {
97 return true;
100 virtual bool OnCanSetCookie(const URLRequest& request,
101 const std::string& cookie_line,
102 CookieOptions* options) OVERRIDE {
103 return true;
106 virtual bool OnCanAccessFile(const net::URLRequest& request,
107 const base::FilePath& path) const OVERRIDE {
108 return true;
111 virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE {
112 return false;
115 virtual int OnBeforeSocketStreamConnect(
116 SocketStream* stream,
117 const CompletionCallback& callback) OVERRIDE {
118 return OK;
121 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate);
124 class BasicURLRequestContext : public URLRequestContext {
125 public:
126 BasicURLRequestContext()
127 : cache_thread_("Cache Thread"),
128 file_thread_("File Thread"),
129 storage_(this) {}
131 URLRequestContextStorage* storage() {
132 return &storage_;
135 void StartCacheThread() {
136 cache_thread_.StartWithOptions(
137 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
140 scoped_refptr<base::MessageLoopProxy> cache_message_loop_proxy() {
141 DCHECK(cache_thread_.IsRunning());
142 return cache_thread_.message_loop_proxy();
145 void StartFileThread() {
146 file_thread_.StartWithOptions(
147 base::Thread::Options(base::MessageLoop::TYPE_DEFAULT, 0));
150 base::MessageLoop* file_message_loop() {
151 DCHECK(file_thread_.IsRunning());
152 return file_thread_.message_loop();
155 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy() {
156 DCHECK(file_thread_.IsRunning());
157 return file_thread_.message_loop_proxy();
160 protected:
161 virtual ~BasicURLRequestContext() {}
163 private:
164 base::Thread cache_thread_;
165 base::Thread file_thread_;
166 URLRequestContextStorage storage_;
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 http_pipelining_enabled(false),
181 testing_fixed_http_port(0),
182 testing_fixed_https_port(0),
183 trusted_spdy_proxy() {}
185 URLRequestContextBuilder::HttpNetworkSessionParams::~HttpNetworkSessionParams()
188 URLRequestContextBuilder::URLRequestContextBuilder()
189 : data_enabled_(false),
190 file_enabled_(false),
191 #if !defined(DISABLE_FTP_SUPPORT)
192 ftp_enabled_(false),
193 #endif
194 http_cache_enabled_(true) {
197 URLRequestContextBuilder::~URLRequestContextBuilder() {}
199 void URLRequestContextBuilder::set_proxy_config_service(
200 ProxyConfigService* proxy_config_service) {
201 proxy_config_service_.reset(proxy_config_service);
204 URLRequestContext* URLRequestContextBuilder::Build() {
205 BasicURLRequestContext* context = new BasicURLRequestContext;
206 URLRequestContextStorage* storage = context->storage();
208 storage->set_http_user_agent_settings(new StaticHttpUserAgentSettings(
209 accept_language_, user_agent_));
211 if (!network_delegate_)
212 network_delegate_.reset(new BasicNetworkDelegate);
213 NetworkDelegate* network_delegate = network_delegate_.release();
214 storage->set_network_delegate(network_delegate);
216 if (!host_resolver_)
217 host_resolver_ = net::HostResolver::CreateDefaultResolver(NULL);
218 storage->set_host_resolver(host_resolver_.Pass());
220 context->StartFileThread();
222 // TODO(willchan): Switch to using this code when
223 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck.
224 #if defined(OS_LINUX) || defined(OS_ANDROID)
225 ProxyConfigService* proxy_config_service = proxy_config_service_.release();
226 #else
227 ProxyConfigService* proxy_config_service = NULL;
228 if (proxy_config_service_) {
229 proxy_config_service = proxy_config_service_.release();
230 } else {
231 proxy_config_service =
232 ProxyService::CreateSystemProxyConfigService(
233 base::ThreadTaskRunnerHandle::Get().get(),
234 context->file_message_loop());
236 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
237 storage->set_proxy_service(
238 ProxyService::CreateUsingSystemProxyResolver(
239 proxy_config_service,
240 4, // TODO(willchan): Find a better constant somewhere.
241 context->net_log()));
242 storage->set_ssl_config_service(new net::SSLConfigServiceDefaults);
243 storage->set_http_auth_handler_factory(
244 net::HttpAuthHandlerRegistryFactory::CreateDefault(
245 context->host_resolver()));
246 storage->set_cookie_store(new CookieMonster(NULL, NULL));
247 storage->set_transport_security_state(new net::TransportSecurityState());
248 storage->set_http_server_properties(
249 scoped_ptr<net::HttpServerProperties>(
250 new net::HttpServerPropertiesImpl()));
251 storage->set_cert_verifier(CertVerifier::CreateDefault());
253 net::HttpNetworkSession::Params network_session_params;
254 network_session_params.host_resolver = context->host_resolver();
255 network_session_params.cert_verifier = context->cert_verifier();
256 network_session_params.transport_security_state =
257 context->transport_security_state();
258 network_session_params.proxy_service = context->proxy_service();
259 network_session_params.ssl_config_service =
260 context->ssl_config_service();
261 network_session_params.http_auth_handler_factory =
262 context->http_auth_handler_factory();
263 network_session_params.network_delegate = network_delegate;
264 network_session_params.http_server_properties =
265 context->http_server_properties();
266 network_session_params.net_log = context->net_log();
267 network_session_params.ignore_certificate_errors =
268 http_network_session_params_.ignore_certificate_errors;
269 network_session_params.host_mapping_rules =
270 http_network_session_params_.host_mapping_rules;
271 network_session_params.http_pipelining_enabled =
272 http_network_session_params_.http_pipelining_enabled;
273 network_session_params.testing_fixed_http_port =
274 http_network_session_params_.testing_fixed_http_port;
275 network_session_params.testing_fixed_https_port =
276 http_network_session_params_.testing_fixed_https_port;
277 network_session_params.trusted_spdy_proxy =
278 http_network_session_params_.trusted_spdy_proxy;
280 HttpTransactionFactory* http_transaction_factory = NULL;
281 if (http_cache_enabled_) {
282 network_session_params.server_bound_cert_service =
283 context->server_bound_cert_service();
284 HttpCache::BackendFactory* http_cache_backend = NULL;
285 if (http_cache_params_.type == HttpCacheParams::DISK) {
286 context->StartCacheThread();
287 http_cache_backend = new HttpCache::DefaultBackend(
288 DISK_CACHE,
289 net::CACHE_BACKEND_DEFAULT,
290 http_cache_params_.path,
291 http_cache_params_.max_size,
292 context->cache_message_loop_proxy().get());
293 } else {
294 http_cache_backend =
295 HttpCache::DefaultBackend::InMemory(http_cache_params_.max_size);
298 http_transaction_factory = new HttpCache(
299 network_session_params, http_cache_backend);
300 } else {
301 scoped_refptr<net::HttpNetworkSession> network_session(
302 new net::HttpNetworkSession(network_session_params));
304 http_transaction_factory = new HttpNetworkLayer(network_session.get());
306 storage->set_http_transaction_factory(http_transaction_factory);
308 URLRequestJobFactoryImpl* job_factory = new URLRequestJobFactoryImpl;
309 if (data_enabled_)
310 job_factory->SetProtocolHandler("data", new DataProtocolHandler);
311 if (file_enabled_)
312 job_factory->SetProtocolHandler(
313 "file", new FileProtocolHandler(context->file_message_loop_proxy()));
314 #if !defined(DISABLE_FTP_SUPPORT)
315 if (ftp_enabled_) {
316 ftp_transaction_factory_.reset(
317 new FtpNetworkLayer(context->host_resolver()));
318 job_factory->SetProtocolHandler("ftp",
319 new FtpProtocolHandler(ftp_transaction_factory_.get()));
321 #endif
322 storage->set_job_factory(job_factory);
324 // TODO(willchan): Support sdch.
326 return context;
329 } // namespace net