[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/url_request/url_request_context.h" |
| 6 | |
[email protected] | 18590f2 | 2011-07-29 16:41:28 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
[email protected] | 7c52ed9 | 2012-04-06 15:42:40 | [diff] [blame] | 8 | #include "base/debug/alias.h" |
[email protected] | 4eee045 | 2012-05-12 01:29:27 | [diff] [blame] | 9 | #include "base/debug/stack_trace.h" |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 11 | #include "net/base/host_resolver.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 12 | #include "net/cookies/cookie_store.h" |
[email protected] | f6c21cb | 2011-02-16 19:45:41 | [diff] [blame] | 13 | #include "net/ftp/ftp_transaction_factory.h" |
| 14 | #include "net/http/http_transaction_factory.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame^] | 15 | #include "net/url_request/http_user_agent_settings.h" |
[email protected] | 7c52ed9 | 2012-04-06 15:42:40 | [diff] [blame] | 16 | #include "net/url_request/url_request.h" |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 17 | |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 18 | namespace net { |
| 19 | |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 20 | URLRequestContext::URLRequestContext() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 21 | : net_log_(NULL), |
[email protected] | c3a7bce | 2010-10-18 17:36:54 | [diff] [blame] | 22 | host_resolver_(NULL), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 23 | cert_verifier_(NULL), |
[email protected] | 9c4eff2 | 2012-03-20 22:42:29 | [diff] [blame] | 24 | server_bound_cert_service_(NULL), |
[email protected] | b97d770 | 2011-10-16 01:11:07 | [diff] [blame] | 25 | fraudulent_certificate_reporter_(NULL), |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 26 | http_auth_handler_factory_(NULL), |
[email protected] | 160014da | 2011-05-13 19:37:46 | [diff] [blame] | 27 | proxy_service_(NULL), |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 28 | network_delegate_(NULL), |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 29 | http_server_properties_(NULL), |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame^] | 30 | http_user_agent_settings_(NULL), |
[email protected] | 70b9234 | 2010-10-12 05:54:06 | [diff] [blame] | 31 | transport_security_state_(NULL), |
[email protected] | 9bfe0ab | 2012-08-30 13:18:11 | [diff] [blame] | 32 | #if !defined(DISABLE_FTP_SUPPORT) |
[email protected] | ede9666 | 2011-07-14 12:34:18 | [diff] [blame] | 33 | ftp_auth_cache_(new FtpAuthCache), |
[email protected] | 9bfe0ab | 2012-08-30 13:18:11 | [diff] [blame] | 34 | #endif |
[email protected] | f6c21cb | 2011-02-16 19:45:41 | [diff] [blame] | 35 | http_transaction_factory_(NULL), |
[email protected] | a8c1e745 | 2011-05-14 06:17:07 | [diff] [blame] | 36 | ftp_transaction_factory_(NULL), |
[email protected] | 7c52ed9 | 2012-04-06 15:42:40 | [diff] [blame] | 37 | job_factory_(NULL), |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 38 | throttler_manager_(NULL), |
[email protected] | 7c52ed9 | 2012-04-06 15:42:40 | [diff] [blame] | 39 | url_requests_(new std::set<const URLRequest*>) { |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 40 | } |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 41 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 42 | URLRequestContext::~URLRequestContext() { |
| 43 | AssertNoURLRequests(); |
| 44 | } |
| 45 | |
[email protected] | fa22a6f1 | 2012-05-11 17:19:59 | [diff] [blame] | 46 | void URLRequestContext::CopyFrom(const URLRequestContext* other) { |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 47 | // Copy URLRequestContext parameters. |
[email protected] | fa22a6f1 | 2012-05-11 17:19:59 | [diff] [blame] | 48 | set_net_log(other->net_log_); |
| 49 | set_host_resolver(other->host_resolver_); |
| 50 | set_cert_verifier(other->cert_verifier_); |
| 51 | set_server_bound_cert_service(other->server_bound_cert_service_); |
| 52 | set_fraudulent_certificate_reporter(other->fraudulent_certificate_reporter_); |
| 53 | set_http_auth_handler_factory(other->http_auth_handler_factory_); |
| 54 | set_proxy_service(other->proxy_service_); |
| 55 | set_ssl_config_service(other->ssl_config_service_); |
| 56 | set_network_delegate(other->network_delegate_); |
| 57 | set_http_server_properties(other->http_server_properties_); |
| 58 | set_cookie_store(other->cookie_store_); |
| 59 | set_transport_security_state(other->transport_security_state_); |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 60 | // FTPAuthCache is unique per context. |
[email protected] | fa22a6f1 | 2012-05-11 17:19:59 | [diff] [blame] | 61 | set_http_transaction_factory(other->http_transaction_factory_); |
| 62 | set_ftp_transaction_factory(other->ftp_transaction_factory_); |
| 63 | set_job_factory(other->job_factory_); |
| 64 | set_throttler_manager(other->throttler_manager_); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame^] | 65 | set_http_user_agent_settings(other->http_user_agent_settings_); |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 68 | const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams( |
| 69 | ) const { |
| 70 | HttpTransactionFactory* transaction_factory = http_transaction_factory(); |
| 71 | if (!transaction_factory) |
| 72 | return NULL; |
| 73 | HttpNetworkSession* network_session = transaction_factory->GetSession(); |
| 74 | if (!network_session) |
| 75 | return NULL; |
| 76 | return &network_session->params(); |
| 77 | } |
| 78 | |
[email protected] | 8a26ff6 | 2012-08-24 21:49:20 | [diff] [blame] | 79 | URLRequest* URLRequestContext::CreateRequest( |
| 80 | const GURL& url, URLRequest::Delegate* delegate) const { |
| 81 | return new URLRequest(url, delegate, this, network_delegate_); |
| 82 | } |
| 83 | |
[email protected] | d100e44f | 2011-01-26 22:47:11 | [diff] [blame] | 84 | void URLRequestContext::set_cookie_store(CookieStore* cookie_store) { |
| 85 | cookie_store_ = cookie_store; |
| 86 | } |
| 87 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame^] | 88 | std::string URLRequestContext::GetAcceptCharset() const { |
| 89 | return http_user_agent_settings_ ? |
| 90 | http_user_agent_settings_->GetAcceptCharset() : EmptyString(); |
| 91 | } |
| 92 | |
| 93 | std::string URLRequestContext::GetAcceptLanguage() const { |
| 94 | return http_user_agent_settings_ ? |
| 95 | http_user_agent_settings_->GetAcceptLanguage() : EmptyString(); |
| 96 | } |
| 97 | |
| 98 | std::string URLRequestContext::GetUserAgent(const GURL& url) const { |
| 99 | return http_user_agent_settings_ ? |
| 100 | http_user_agent_settings_->GetUserAgent(url) : EmptyString(); |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 101 | } |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 102 | |
[email protected] | 1e714bba | 2012-04-10 17:01:05 | [diff] [blame] | 103 | void URLRequestContext::AssertNoURLRequests() const { |
[email protected] | 7c52ed9 | 2012-04-06 15:42:40 | [diff] [blame] | 104 | int num_requests = url_requests_->size(); |
| 105 | if (num_requests != 0) { |
| 106 | // We're leaking URLRequests :( Dump the URL of the first one and record how |
| 107 | // many we leaked so we have an idea of how bad it is. |
| 108 | char url_buf[128]; |
| 109 | const URLRequest* request = *url_requests_->begin(); |
| 110 | base::strlcpy(url_buf, request->url().spec().c_str(), arraysize(url_buf)); |
[email protected] | eb4ecaca | 2012-05-04 01:05:03 | [diff] [blame] | 111 | bool has_delegate = request->has_delegate(); |
| 112 | int load_flags = request->load_flags(); |
[email protected] | 4eee045 | 2012-05-12 01:29:27 | [diff] [blame] | 113 | base::debug::StackTrace stack_trace(NULL, 0); |
| 114 | if (request->stack_trace()) |
| 115 | stack_trace = *request->stack_trace(); |
[email protected] | 7c52ed9 | 2012-04-06 15:42:40 | [diff] [blame] | 116 | base::debug::Alias(url_buf); |
| 117 | base::debug::Alias(&num_requests); |
[email protected] | eb4ecaca | 2012-05-04 01:05:03 | [diff] [blame] | 118 | base::debug::Alias(&has_delegate); |
| 119 | base::debug::Alias(&load_flags); |
[email protected] | 4eee045 | 2012-05-12 01:29:27 | [diff] [blame] | 120 | base::debug::Alias(&stack_trace); |
[email protected] | 1f5f8e2 | 2012-09-06 23:40:54 | [diff] [blame] | 121 | CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: " |
| 122 | << request->url().spec().c_str() << "."; |
[email protected] | 7c52ed9 | 2012-04-06 15:42:40 | [diff] [blame] | 123 | } |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 124 | } |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 125 | |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 126 | } // namespace net |