| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_request_context_builder.h" | 5 #include "net/url_request/url_request_context_builder.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "net/base/cache_type.h" | 15 #include "net/base/cache_type.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/network_delegate.h" | 17 #include "net/base/network_delegate.h" |
| 18 #include "net/cert/cert_verifier.h" | 18 #include "net/cert/cert_verifier.h" |
| 19 #include "net/cookies/cookie_monster.h" | 19 #include "net/cookies/cookie_monster.h" |
| 20 #include "net/dns/host_resolver.h" | 20 #include "net/dns/host_resolver.h" |
| 21 #include "net/ftp/ftp_network_layer.h" | 21 #include "net/ftp/ftp_network_layer.h" |
| 22 #include "net/http/http_auth_handler_factory.h" | 22 #include "net/http/http_auth_handler_factory.h" |
| 23 #include "net/http/http_cache.h" | 23 #include "net/http/http_cache.h" |
| 24 #include "net/http/http_network_layer.h" | 24 #include "net/http/http_network_layer.h" |
| 25 #include "net/http/http_network_session.h" | 25 #include "net/http/http_network_session.h" |
| 26 #include "net/http/http_server_properties_impl.h" | 26 #include "net/http/http_server_properties_impl.h" |
| 27 #include "net/http/transport_security_persister.h" |
| 27 #include "net/http/transport_security_state.h" | 28 #include "net/http/transport_security_state.h" |
| 28 #include "net/proxy/proxy_service.h" | 29 #include "net/ssl/default_server_bound_cert_store.h" |
| 30 #include "net/ssl/server_bound_cert_service.h" |
| 29 #include "net/ssl/ssl_config_service_defaults.h" | 31 #include "net/ssl/ssl_config_service_defaults.h" |
| 30 #include "net/url_request/data_protocol_handler.h" | 32 #include "net/url_request/data_protocol_handler.h" |
| 31 #include "net/url_request/static_http_user_agent_settings.h" | 33 #include "net/url_request/static_http_user_agent_settings.h" |
| 32 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
| 33 #include "net/url_request/url_request_context_storage.h" | 35 #include "net/url_request/url_request_context_storage.h" |
| 34 #include "net/url_request/url_request_job_factory_impl.h" | 36 #include "net/url_request/url_request_job_factory_impl.h" |
| 37 #include "net/url_request/url_request_throttler_manager.h" |
| 35 | 38 |
| 36 #if !defined(DISABLE_FILE_SUPPORT) | 39 #if !defined(DISABLE_FILE_SUPPORT) |
| 37 #include "net/url_request/file_protocol_handler.h" | 40 #include "net/url_request/file_protocol_handler.h" |
| 38 #endif | 41 #endif |
| 39 | 42 |
| 40 #if !defined(DISABLE_FTP_SUPPORT) | 43 #if !defined(DISABLE_FTP_SUPPORT) |
| 41 #include "net/url_request/ftp_protocol_handler.h" | 44 #include "net/url_request/ftp_protocol_handler.h" |
| 42 #endif | 45 #endif |
| 43 | 46 |
| 44 namespace net { | 47 namespace net { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 CookieOptions* options) OVERRIDE { | 111 CookieOptions* options) OVERRIDE { |
| 109 return true; | 112 return true; |
| 110 } | 113 } |
| 111 | 114 |
| 112 virtual bool OnCanAccessFile(const net::URLRequest& request, | 115 virtual bool OnCanAccessFile(const net::URLRequest& request, |
| 113 const base::FilePath& path) const OVERRIDE { | 116 const base::FilePath& path) const OVERRIDE { |
| 114 return true; | 117 return true; |
| 115 } | 118 } |
| 116 | 119 |
| 117 virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE { | 120 virtual bool OnCanThrottleRequest(const URLRequest& request) const OVERRIDE { |
| 118 return false; | 121 // Returning true will only enable throttling if there's also a |
| 122 // URLRequestThrottlerManager, which there isn't, by default. |
| 123 return true; |
| 119 } | 124 } |
| 120 | 125 |
| 121 virtual int OnBeforeSocketStreamConnect( | 126 virtual int OnBeforeSocketStreamConnect( |
| 122 SocketStream* stream, | 127 SocketStream* stream, |
| 123 const CompletionCallback& callback) OVERRIDE { | 128 const CompletionCallback& callback) OVERRIDE { |
| 124 return OK; | 129 return OK; |
| 125 } | 130 } |
| 126 | 131 |
| 127 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); | 132 DISALLOW_COPY_AND_ASSIGN(BasicNetworkDelegate); |
| 128 }; | 133 }; |
| 129 | 134 |
| 130 class BasicURLRequestContext : public URLRequestContext { | 135 class BasicURLRequestContext : public URLRequestContext { |
| 131 public: | 136 public: |
| 132 BasicURLRequestContext() | 137 BasicURLRequestContext() |
| 133 : storage_(this) {} | 138 : storage_(this) {} |
| 134 | 139 |
| 135 URLRequestContextStorage* storage() { | 140 URLRequestContextStorage* storage() { |
| 136 return &storage_; | 141 return &storage_; |
| 137 } | 142 } |
| 138 | 143 |
| 139 base::Thread* GetCacheThread() { | 144 base::Thread* GetCacheThread() { |
| 140 if (!cache_thread_) { | 145 if (!cache_thread_) { |
| 141 cache_thread_.reset(new base::Thread("Cache Thread")); | 146 cache_thread_.reset(new base::Thread("Network Cache Thread")); |
| 142 cache_thread_->StartWithOptions( | 147 cache_thread_->StartWithOptions( |
| 143 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 148 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 144 } | 149 } |
| 145 return cache_thread_.get(); | 150 return cache_thread_.get(); |
| 146 } | 151 } |
| 147 | 152 |
| 148 base::Thread* GetFileThread() { | 153 base::Thread* GetFileThread() { |
| 149 if (!file_thread_) { | 154 if (!file_thread_) { |
| 150 file_thread_.reset(new base::Thread("File Thread")); | 155 file_thread_.reset(new base::Thread("Network File Thread")); |
| 151 file_thread_->StartWithOptions( | 156 file_thread_->StartWithOptions( |
| 152 base::Thread::Options(base::MessageLoop::TYPE_DEFAULT, 0)); | 157 base::Thread::Options(base::MessageLoop::TYPE_DEFAULT, 0)); |
| 153 } | 158 } |
| 154 return file_thread_.get(); | 159 return file_thread_.get(); |
| 155 } | 160 } |
| 156 | 161 |
| 162 void set_transport_security_persister( |
| 163 scoped_ptr<TransportSecurityPersister> transport_security_persister) { |
| 164 transport_security_persister = transport_security_persister.Pass(); |
| 165 } |
| 166 |
| 157 protected: | 167 protected: |
| 158 virtual ~BasicURLRequestContext() {} | 168 virtual ~BasicURLRequestContext() {} |
| 159 | 169 |
| 160 private: | 170 private: |
| 171 // Threads should be torn down last. |
| 161 scoped_ptr<base::Thread> cache_thread_; | 172 scoped_ptr<base::Thread> cache_thread_; |
| 162 scoped_ptr<base::Thread> file_thread_; | 173 scoped_ptr<base::Thread> file_thread_; |
| 174 |
| 163 URLRequestContextStorage storage_; | 175 URLRequestContextStorage storage_; |
| 176 scoped_ptr<TransportSecurityPersister> transport_security_persister_; |
| 177 |
| 164 DISALLOW_COPY_AND_ASSIGN(BasicURLRequestContext); | 178 DISALLOW_COPY_AND_ASSIGN(BasicURLRequestContext); |
| 165 }; | 179 }; |
| 166 | 180 |
| 167 } // namespace | 181 } // namespace |
| 168 | 182 |
| 169 URLRequestContextBuilder::HttpCacheParams::HttpCacheParams() | 183 URLRequestContextBuilder::HttpCacheParams::HttpCacheParams() |
| 170 : type(IN_MEMORY), | 184 : type(IN_MEMORY), |
| 171 max_size(0) {} | 185 max_size(0) {} |
| 172 URLRequestContextBuilder::HttpCacheParams::~HttpCacheParams() {} | 186 URLRequestContextBuilder::HttpCacheParams::~HttpCacheParams() {} |
| 173 | 187 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 198 file_enabled_(false), | 212 file_enabled_(false), |
| 199 #endif | 213 #endif |
| 200 #if !defined(DISABLE_FTP_SUPPORT) | 214 #if !defined(DISABLE_FTP_SUPPORT) |
| 201 ftp_enabled_(false), | 215 ftp_enabled_(false), |
| 202 #endif | 216 #endif |
| 203 http_cache_enabled_(true) { | 217 http_cache_enabled_(true) { |
| 204 } | 218 } |
| 205 | 219 |
| 206 URLRequestContextBuilder::~URLRequestContextBuilder() {} | 220 URLRequestContextBuilder::~URLRequestContextBuilder() {} |
| 207 | 221 |
| 208 void URLRequestContextBuilder::set_proxy_config_service( | 222 void URLRequestContextBuilder::EnableHttpCache(const HttpCacheParams& params) { |
| 209 ProxyConfigService* proxy_config_service) { | 223 http_cache_enabled_ = true; |
| 210 proxy_config_service_.reset(proxy_config_service); | 224 http_cache_params_ = params; |
| 225 } |
| 226 |
| 227 void URLRequestContextBuilder::DisableHttpCache() { |
| 228 http_cache_enabled_ = false; |
| 229 http_cache_params_ = HttpCacheParams(); |
| 211 } | 230 } |
| 212 | 231 |
| 213 void URLRequestContextBuilder::SetSpdyAndQuicEnabled(bool spdy_enabled, | 232 void URLRequestContextBuilder::SetSpdyAndQuicEnabled(bool spdy_enabled, |
| 214 bool quic_enabled) { | 233 bool quic_enabled) { |
| 215 http_network_session_params_.next_protos = | 234 http_network_session_params_.next_protos = |
| 216 NextProtosWithSpdyAndQuic(spdy_enabled, quic_enabled); | 235 NextProtosWithSpdyAndQuic(spdy_enabled, quic_enabled); |
| 217 } | 236 } |
| 218 | 237 |
| 219 URLRequestContext* URLRequestContextBuilder::Build() { | 238 URLRequestContext* URLRequestContextBuilder::Build() { |
| 220 BasicURLRequestContext* context = new BasicURLRequestContext; | 239 BasicURLRequestContext* context = new BasicURLRequestContext; |
| 221 URLRequestContextStorage* storage = context->storage(); | 240 URLRequestContextStorage* storage = context->storage(); |
| 222 | 241 |
| 223 storage->set_http_user_agent_settings(new StaticHttpUserAgentSettings( | 242 storage->set_http_user_agent_settings(new StaticHttpUserAgentSettings( |
| 224 accept_language_, user_agent_)); | 243 accept_language_, user_agent_)); |
| 225 | 244 |
| 226 if (!network_delegate_) | 245 if (!network_delegate_) |
| 227 network_delegate_.reset(new BasicNetworkDelegate); | 246 network_delegate_.reset(new BasicNetworkDelegate); |
| 228 NetworkDelegate* network_delegate = network_delegate_.release(); | 247 NetworkDelegate* network_delegate = network_delegate_.release(); |
| 229 storage->set_network_delegate(network_delegate); | 248 storage->set_network_delegate(network_delegate); |
| 230 | 249 |
| 231 if (!host_resolver_) | 250 if (net_log_) { |
| 232 host_resolver_ = net::HostResolver::CreateDefaultResolver(NULL); | 251 storage->set_net_log(net_log_.release()); |
| 252 } else { |
| 253 storage->set_net_log(new net::NetLog); |
| 254 } |
| 255 |
| 256 if (!host_resolver_) { |
| 257 host_resolver_ = net::HostResolver::CreateDefaultResolver( |
| 258 context->net_log()); |
| 259 } |
| 233 storage->set_host_resolver(host_resolver_.Pass()); | 260 storage->set_host_resolver(host_resolver_.Pass()); |
| 234 | 261 |
| 235 storage->set_net_log(new net::NetLog); | 262 if (!proxy_service_) { |
| 263 // TODO(willchan): Switch to using this code when |
| 264 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck. |
| 265 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 266 ProxyConfigService* proxy_config_service = proxy_config_service_.release(); |
| 267 #else |
| 268 ProxyConfigService* proxy_config_service = NULL; |
| 269 if (proxy_config_service_) { |
| 270 proxy_config_service = proxy_config_service_.release(); |
| 271 } else { |
| 272 proxy_config_service = |
| 273 ProxyService::CreateSystemProxyConfigService( |
| 274 base::ThreadTaskRunnerHandle::Get().get(), |
| 275 context->GetFileThread()->message_loop()); |
| 276 } |
| 277 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 278 proxy_service_.reset( |
| 279 ProxyService::CreateUsingSystemProxyResolver( |
| 280 proxy_config_service, |
| 281 0, // This results in using the default value. |
| 282 context->net_log())); |
| 283 } |
| 284 storage->set_proxy_service(proxy_service_.release()); |
| 236 | 285 |
| 237 // TODO(willchan): Switch to using this code when | |
| 238 // ProxyService::CreateSystemProxyConfigService()'s signature doesn't suck. | |
| 239 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
| 240 ProxyConfigService* proxy_config_service = proxy_config_service_.release(); | |
| 241 #else | |
| 242 ProxyConfigService* proxy_config_service = NULL; | |
| 243 if (proxy_config_service_) { | |
| 244 proxy_config_service = proxy_config_service_.release(); | |
| 245 } else { | |
| 246 proxy_config_service = | |
| 247 ProxyService::CreateSystemProxyConfigService( | |
| 248 base::ThreadTaskRunnerHandle::Get().get(), | |
| 249 context->GetFileThread()->message_loop()); | |
| 250 } | |
| 251 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
| 252 storage->set_proxy_service( | |
| 253 ProxyService::CreateUsingSystemProxyResolver( | |
| 254 proxy_config_service, | |
| 255 4, // TODO(willchan): Find a better constant somewhere. | |
| 256 context->net_log())); | |
| 257 storage->set_ssl_config_service(new net::SSLConfigServiceDefaults); | 286 storage->set_ssl_config_service(new net::SSLConfigServiceDefaults); |
| 258 HttpAuthHandlerRegistryFactory* http_auth_handler_registry_factory = | 287 HttpAuthHandlerRegistryFactory* http_auth_handler_registry_factory = |
| 259 net::HttpAuthHandlerRegistryFactory::CreateDefault( | 288 net::HttpAuthHandlerRegistryFactory::CreateDefault( |
| 260 context->host_resolver()); | 289 context->host_resolver()); |
| 261 for (size_t i = 0; i < extra_http_auth_handlers_.size(); ++i) { | 290 for (size_t i = 0; i < extra_http_auth_handlers_.size(); ++i) { |
| 262 http_auth_handler_registry_factory->RegisterSchemeFactory( | 291 http_auth_handler_registry_factory->RegisterSchemeFactory( |
| 263 extra_http_auth_handlers_[i].scheme, | 292 extra_http_auth_handlers_[i].scheme, |
| 264 extra_http_auth_handlers_[i].factory); | 293 extra_http_auth_handlers_[i].factory); |
| 265 } | 294 } |
| 266 storage->set_http_auth_handler_factory(http_auth_handler_registry_factory); | 295 storage->set_http_auth_handler_factory(http_auth_handler_registry_factory); |
| 267 storage->set_cookie_store(new CookieMonster(NULL, NULL)); | 296 storage->set_cookie_store(new CookieMonster(NULL, NULL)); |
| 297 |
| 298 // TODO(mmenke): This always creates a file thread, even when it ends up |
| 299 // not being used. Consider lazily creating the thread. |
| 300 storage->set_server_bound_cert_service( |
| 301 new ServerBoundCertService( |
| 302 new DefaultServerBoundCertStore(NULL), |
| 303 context->GetFileThread()->message_loop_proxy())); |
| 304 |
| 268 storage->set_transport_security_state(new net::TransportSecurityState()); | 305 storage->set_transport_security_state(new net::TransportSecurityState()); |
| 306 if (!transport_security_persister_path_.empty()) { |
| 307 context->set_transport_security_persister( |
| 308 make_scoped_ptr<TransportSecurityPersister>( |
| 309 new TransportSecurityPersister( |
| 310 context->transport_security_state(), |
| 311 transport_security_persister_path_, |
| 312 context->GetFileThread()->message_loop_proxy(), |
| 313 false))); |
| 314 } |
| 315 |
| 269 storage->set_http_server_properties( | 316 storage->set_http_server_properties( |
| 270 scoped_ptr<net::HttpServerProperties>( | 317 scoped_ptr<net::HttpServerProperties>( |
| 271 new net::HttpServerPropertiesImpl())); | 318 new net::HttpServerPropertiesImpl())); |
| 272 storage->set_cert_verifier(CertVerifier::CreateDefault()); | 319 storage->set_cert_verifier(CertVerifier::CreateDefault()); |
| 273 | 320 |
| 321 if (throttling_enabled_) |
| 322 storage->set_throttler_manager(new URLRequestThrottlerManager()); |
| 323 |
| 274 net::HttpNetworkSession::Params network_session_params; | 324 net::HttpNetworkSession::Params network_session_params; |
| 275 network_session_params.host_resolver = context->host_resolver(); | 325 network_session_params.host_resolver = context->host_resolver(); |
| 276 network_session_params.cert_verifier = context->cert_verifier(); | 326 network_session_params.cert_verifier = context->cert_verifier(); |
| 277 network_session_params.transport_security_state = | 327 network_session_params.transport_security_state = |
| 278 context->transport_security_state(); | 328 context->transport_security_state(); |
| 279 network_session_params.proxy_service = context->proxy_service(); | 329 network_session_params.proxy_service = context->proxy_service(); |
| 280 network_session_params.ssl_config_service = | 330 network_session_params.ssl_config_service = |
| 281 context->ssl_config_service(); | 331 context->ssl_config_service(); |
| 282 network_session_params.http_auth_handler_factory = | 332 network_session_params.http_auth_handler_factory = |
| 283 context->http_auth_handler_factory(); | 333 context->http_auth_handler_factory(); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 #endif // !defined(DISABLE_FTP_SUPPORT) | 399 #endif // !defined(DISABLE_FTP_SUPPORT) |
| 350 | 400 |
| 351 storage->set_job_factory(job_factory); | 401 storage->set_job_factory(job_factory); |
| 352 | 402 |
| 353 // TODO(willchan): Support sdch. | 403 // TODO(willchan): Support sdch. |
| 354 | 404 |
| 355 return context; | 405 return context; |
| 356 } | 406 } |
| 357 | 407 |
| 358 } // namespace net | 408 } // namespace net |
| OLD | NEW |