15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_context.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/debug/alias.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/debug/stack_trace.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/cookies/cookie_store.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/dns/host_resolver.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_transaction_factory.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/http_user_agent_settings.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestContext::URLRequestContext()
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : net_log_(NULL),
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      host_resolver_(NULL),
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cert_verifier_(NULL),
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      channel_id_service_(NULL),
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fraudulent_certificate_reporter_(NULL),
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      http_auth_handler_factory_(NULL),
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      proxy_service_(NULL),
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      network_delegate_(NULL),
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      http_user_agent_settings_(NULL),
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      transport_security_state_(NULL),
30a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      cert_transparency_verifier_(NULL),
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      http_transaction_factory_(NULL),
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      job_factory_(NULL),
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      throttler_manager_(NULL),
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      sdch_manager_(NULL),
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      url_requests_(new std::set<const URLRequest*>) {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestContext::~URLRequestContext() {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AssertNoURLRequests();
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestContext::CopyFrom(const URLRequestContext* other) {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Copy URLRequestContext parameters.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_net_log(other->net_log_);
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_host_resolver(other->host_resolver_);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_cert_verifier(other->cert_verifier_);
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  set_channel_id_service(other->channel_id_service_);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_fraudulent_certificate_reporter(other->fraudulent_certificate_reporter_);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_http_auth_handler_factory(other->http_auth_handler_factory_);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_proxy_service(other->proxy_service_);
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  set_ssl_config_service(other->ssl_config_service_.get());
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_network_delegate(other->network_delegate_);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_http_server_properties(other->http_server_properties_);
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  set_cookie_store(other->cookie_store_.get());
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_transport_security_state(other->transport_security_state_);
56a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  set_cert_transparency_verifier(other->cert_transparency_verifier_);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_http_transaction_factory(other->http_transaction_factory_);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_job_factory(other->job_factory_);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_throttler_manager(other->throttler_manager_);
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  set_sdch_manager(other->sdch_manager_);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  set_http_user_agent_settings(other->http_user_agent_settings_);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams(
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ) const {
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HttpTransactionFactory* transaction_factory = http_transaction_factory();
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!transaction_factory)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HttpNetworkSession* network_session = transaction_factory->GetSession();
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!network_session)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return &network_session->params();
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)scoped_ptr<URLRequest> URLRequestContext::CreateRequest(
760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const GURL& url,
770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    RequestPriority priority,
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    URLRequest::Delegate* delegate,
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    CookieStore* cookie_store) const {
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return scoped_ptr<URLRequest>(
8103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      new URLRequest(url, priority, delegate, this, cookie_store,
8203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                     network_delegate_));
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cookie_store_ = cookie_store;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestContext::AssertNoURLRequests() const {
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_requests = url_requests_->size();
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (num_requests != 0) {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We're leaking URLRequests :( Dump the URL of the first one and record how
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // many we leaked so we have an idea of how bad it is.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    char url_buf[128];
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const URLRequest* request = *url_requests_->begin();
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::strlcpy(url_buf, request->url().spec().c_str(), arraysize(url_buf));
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool has_delegate = request->has_delegate();
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int load_flags = request->load_flags();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::debug::StackTrace stack_trace(NULL, 0);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (request->stack_trace())
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      stack_trace = *request->stack_trace();
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::debug::Alias(url_buf);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::debug::Alias(&num_requests);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::debug::Alias(&has_delegate);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::debug::Alias(&load_flags);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::debug::Alias(&stack_trace);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << request->url().spec().c_str() << ".";
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
113