Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/url_request/url_request_context.h" |
| 6 | |
| 7 | #include "base/compiler_specific.h" |
| 8 | #include "base/debug/alias.h" |
| 9 | #include "base/debug/stack_trace.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 10 | #include "base/strings/string_util.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 11 | #include "net/cookies/cookie_store.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 12 | #include "net/dns/host_resolver.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "net/http/http_transaction_factory.h" |
| 14 | #include "net/url_request/http_user_agent_settings.h" |
| 15 | #include "net/url_request/url_request.h" |
| 16 | |
| 17 | namespace net { |
| 18 | |
| 19 | URLRequestContext::URLRequestContext() |
| 20 | : net_log_(NULL), |
| 21 | host_resolver_(NULL), |
| 22 | cert_verifier_(NULL), |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 23 | channel_id_service_(NULL), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 24 | fraudulent_certificate_reporter_(NULL), |
| 25 | http_auth_handler_factory_(NULL), |
| 26 | proxy_service_(NULL), |
| 27 | network_delegate_(NULL), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 28 | http_user_agent_settings_(NULL), |
| 29 | transport_security_state_(NULL), |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 30 | cert_transparency_verifier_(NULL), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | http_transaction_factory_(NULL), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 32 | job_factory_(NULL), |
| 33 | throttler_manager_(NULL), |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 34 | sdch_manager_(NULL), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 35 | url_requests_(new std::set<const URLRequest*>) { |
| 36 | } |
| 37 | |
| 38 | URLRequestContext::~URLRequestContext() { |
| 39 | AssertNoURLRequests(); |
| 40 | } |
| 41 | |
| 42 | void URLRequestContext::CopyFrom(const URLRequestContext* other) { |
| 43 | // Copy URLRequestContext parameters. |
| 44 | set_net_log(other->net_log_); |
| 45 | set_host_resolver(other->host_resolver_); |
| 46 | set_cert_verifier(other->cert_verifier_); |
Torne (Richard Coles) | 5f1c943 | 2014-08-12 13:47:38 +0100 | [diff] [blame] | 47 | set_channel_id_service(other->channel_id_service_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 48 | set_fraudulent_certificate_reporter(other->fraudulent_certificate_reporter_); |
| 49 | set_http_auth_handler_factory(other->http_auth_handler_factory_); |
| 50 | set_proxy_service(other->proxy_service_); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 51 | set_ssl_config_service(other->ssl_config_service_.get()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 52 | set_network_delegate(other->network_delegate_); |
| 53 | set_http_server_properties(other->http_server_properties_); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 54 | set_cookie_store(other->cookie_store_.get()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 55 | set_transport_security_state(other->transport_security_state_); |
Torne (Richard Coles) | a3f6a49 | 2013-12-18 16:25:09 +0000 | [diff] [blame] | 56 | set_cert_transparency_verifier(other->cert_transparency_verifier_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 57 | set_http_transaction_factory(other->http_transaction_factory_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 58 | set_job_factory(other->job_factory_); |
| 59 | set_throttler_manager(other->throttler_manager_); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 60 | set_sdch_manager(other->sdch_manager_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 61 | set_http_user_agent_settings(other->http_user_agent_settings_); |
| 62 | } |
| 63 | |
| 64 | const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams( |
| 65 | ) const { |
| 66 | HttpTransactionFactory* transaction_factory = http_transaction_factory(); |
| 67 | if (!transaction_factory) |
| 68 | return NULL; |
| 69 | HttpNetworkSession* network_session = transaction_factory->GetSession(); |
| 70 | if (!network_session) |
| 71 | return NULL; |
| 72 | return &network_session->params(); |
| 73 | } |
| 74 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 75 | scoped_ptr<URLRequest> URLRequestContext::CreateRequest( |
| 76 | const GURL& url, |
| 77 | RequestPriority priority, |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 78 | URLRequest::Delegate* delegate, |
| 79 | CookieStore* cookie_store) const { |
| 80 | return scoped_ptr<URLRequest>( |
Torne (Richard Coles) | 03b57e0 | 2014-08-28 12:05:23 +0100 | [diff] [blame] | 81 | new URLRequest(url, priority, delegate, this, cookie_store, |
| 82 | network_delegate_)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void URLRequestContext::set_cookie_store(CookieStore* cookie_store) { |
| 86 | cookie_store_ = cookie_store; |
| 87 | } |
| 88 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 89 | void URLRequestContext::AssertNoURLRequests() const { |
| 90 | int num_requests = url_requests_->size(); |
| 91 | if (num_requests != 0) { |
| 92 | // We're leaking URLRequests :( Dump the URL of the first one and record how |
| 93 | // many we leaked so we have an idea of how bad it is. |
| 94 | char url_buf[128]; |
| 95 | const URLRequest* request = *url_requests_->begin(); |
| 96 | base::strlcpy(url_buf, request->url().spec().c_str(), arraysize(url_buf)); |
| 97 | bool has_delegate = request->has_delegate(); |
| 98 | int load_flags = request->load_flags(); |
| 99 | base::debug::StackTrace stack_trace(NULL, 0); |
| 100 | if (request->stack_trace()) |
| 101 | stack_trace = *request->stack_trace(); |
| 102 | base::debug::Alias(url_buf); |
| 103 | base::debug::Alias(&num_requests); |
| 104 | base::debug::Alias(&has_delegate); |
| 105 | base::debug::Alias(&load_flags); |
| 106 | base::debug::Alias(&stack_trace); |
| 107 | CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: " |
| 108 | << request->url().spec().c_str() << "."; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } // namespace net |