url_request_context.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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"
10#include "base/string_util.h"
11#include "net/cookies/cookie_store.h"
12#include "net/dns/host_resolver.h"
13#include "net/ftp/ftp_transaction_factory.h"
14#include "net/http/http_transaction_factory.h"
15#include "net/url_request/http_user_agent_settings.h"
16#include "net/url_request/url_request.h"
17
18namespace net {
19
20URLRequestContext::URLRequestContext()
21    : net_log_(NULL),
22      host_resolver_(NULL),
23      cert_verifier_(NULL),
24      server_bound_cert_service_(NULL),
25      fraudulent_certificate_reporter_(NULL),
26      http_auth_handler_factory_(NULL),
27      proxy_service_(NULL),
28      network_delegate_(NULL),
29      http_server_properties_(NULL),
30      http_user_agent_settings_(NULL),
31      transport_security_state_(NULL),
32#if !defined(DISABLE_FTP_SUPPORT)
33      ftp_auth_cache_(new FtpAuthCache),
34#endif
35      http_transaction_factory_(NULL),
36      ftp_transaction_factory_(NULL),
37      job_factory_(NULL),
38      throttler_manager_(NULL),
39      url_requests_(new std::set<const URLRequest*>) {
40}
41
42URLRequestContext::~URLRequestContext() {
43  AssertNoURLRequests();
44}
45
46void URLRequestContext::CopyFrom(const URLRequestContext* other) {
47  // Copy URLRequestContext parameters.
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_);
60  // FTPAuthCache is unique per context.
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_);
65  set_http_user_agent_settings(other->http_user_agent_settings_);
66}
67
68const 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
79URLRequest* URLRequestContext::CreateRequest(
80    const GURL& url, URLRequest::Delegate* delegate) const {
81  return new URLRequest(url, delegate, this, network_delegate_);
82}
83
84void URLRequestContext::set_cookie_store(CookieStore* cookie_store) {
85  cookie_store_ = cookie_store;
86}
87
88std::string URLRequestContext::GetAcceptLanguage() const {
89  return http_user_agent_settings_ ?
90      http_user_agent_settings_->GetAcceptLanguage() : EmptyString();
91}
92
93std::string URLRequestContext::GetUserAgent(const GURL& url) const {
94  return http_user_agent_settings_ ?
95      http_user_agent_settings_->GetUserAgent(url) : EmptyString();
96}
97
98void URLRequestContext::AssertNoURLRequests() const {
99  int num_requests = url_requests_->size();
100  if (num_requests != 0) {
101    // We're leaking URLRequests :( Dump the URL of the first one and record how
102    // many we leaked so we have an idea of how bad it is.
103    char url_buf[128];
104    const URLRequest* request = *url_requests_->begin();
105    base::strlcpy(url_buf, request->url().spec().c_str(), arraysize(url_buf));
106    bool has_delegate = request->has_delegate();
107    int load_flags = request->load_flags();
108    base::debug::StackTrace stack_trace(NULL, 0);
109    if (request->stack_trace())
110      stack_trace = *request->stack_trace();
111    base::debug::Alias(url_buf);
112    base::debug::Alias(&num_requests);
113    base::debug::Alias(&has_delegate);
114    base::debug::Alias(&load_flags);
115    base::debug::Alias(&stack_trace);
116    CHECK(false) << "Leaked " << num_requests << " URLRequest(s). First URL: "
117                 << request->url().spec().c_str() << ".";
118  }
119}
120
121}  // namespace net
122