[email protected] | d24fc3a0 | 2012-02-11 02:08:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [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 "chrome/browser/io_thread.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 6 | |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | 21ee224e | 2011-11-21 02:17:53 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | c93123fa | 2012-04-19 02:49:48 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 13 | #include "base/debug/leak_tracker.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 16 | #include "base/string_number_conversions.h" |
[email protected] | 4e5ae20f | 2010-09-24 04:52:11 | [diff] [blame] | 17 | #include "base/string_split.h" |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 18 | #include "base/string_util.h" |
[email protected] | 3fc40c14 | 2011-12-01 13:09:04 | [diff] [blame] | 19 | #include "base/threading/thread.h" |
[email protected] | 5bab49ec | 2012-05-04 21:13:19 | [diff] [blame] | 20 | #include "base/threading/worker_pool.h" |
[email protected] | addb324 | 2011-06-13 21:39:16 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | df2840d | 2011-02-20 16:32:32 | [diff] [blame] | 22 | #include "chrome/browser/browser_process.h" |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 23 | #include "chrome/browser/extensions/event_router_forwarder.h" |
[email protected] | 026876f3 | 2012-08-22 23:53:40 | [diff] [blame] | 24 | #include "chrome/browser/net/async_dns_field_trial.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 25 | #include "chrome/browser/net/chrome_net_log.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 26 | #include "chrome/browser/net/chrome_network_delegate.h" |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 27 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 1889dc1b | 2010-10-14 22:03:13 | [diff] [blame] | 28 | #include "chrome/browser/net/connect_interceptor.h" |
[email protected] | 7613faae | 2012-04-18 01:01:19 | [diff] [blame] | 29 | #include "chrome/browser/net/http_pipelining_compatibility_client.h" |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 30 | #include "chrome/browser/net/load_time_stats.h" |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 31 | #include "chrome/browser/net/pref_proxy_config_tracker.h" |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 32 | #include "chrome/browser/net/proxy_service_factory.h" |
[email protected] | 3b543ab | 2011-09-17 21:47:00 | [diff] [blame] | 33 | #include "chrome/browser/net/sdch_dictionary_fetcher.h" |
[email protected] | ec44ee0 | 2012-09-28 21:31:51 | [diff] [blame] | 34 | #include "chrome/browser/net/spdyproxy/http_auth_handler_spdyproxy.h" |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 35 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 36 | #include "chrome/common/chrome_switches.h" |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 37 | #include "chrome/common/pref_names.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 38 | #include "content/public/browser/browser_thread.h" |
[email protected] | 5d1fa24 | 2011-10-18 23:35:38 | [diff] [blame] | 39 | #include "content/public/common/content_client.h" |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 40 | #include "net/base/cert_verifier.h" |
[email protected] | 32cb7fb | 2012-03-22 22:41:11 | [diff] [blame] | 41 | #include "net/base/default_server_bound_cert_store.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 42 | #include "net/base/host_cache.h" |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 43 | #include "net/base/host_mapping_rules.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 44 | #include "net/base/host_resolver.h" |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 45 | #include "net/base/mapped_host_resolver.h" |
[email protected] | 32eaa33 | 2010-02-08 22:15:54 | [diff] [blame] | 46 | #include "net/base/net_util.h" |
[email protected] | 3b543ab | 2011-09-17 21:47:00 | [diff] [blame] | 47 | #include "net/base/sdch_manager.h" |
[email protected] | 32cb7fb | 2012-03-22 22:41:11 | [diff] [blame] | 48 | #include "net/base/server_bound_cert_service.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 49 | #include "net/cookies/cookie_monster.h" |
[email protected] | 933bc5c6 | 2011-04-12 19:08:02 | [diff] [blame] | 50 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 51 | #include "net/http/http_auth_filter.h" |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 52 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 53 | #include "net/http/http_network_layer.h" |
[email protected] | 57cb0f7 | 2011-01-28 06:33:58 | [diff] [blame] | 54 | #include "net/http/http_network_session.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 55 | #include "net/http/http_server_properties_impl.h" |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 56 | #include "net/proxy/proxy_config_service.h" |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 57 | #include "net/proxy/proxy_script_fetcher_impl.h" |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 58 | #include "net/proxy/proxy_service.h" |
[email protected] | 3dc1bc4 | 2012-06-19 08:20:53 | [diff] [blame] | 59 | #include "net/url_request/url_fetcher.h" |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 60 | #include "net/url_request/url_request_throttler_manager.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 61 | |
[email protected] | 77feb46 | 2011-05-16 23:37:25 | [diff] [blame] | 62 | #if defined(USE_NSS) |
| 63 | #include "net/ocsp/nss_ocsp.h" |
| 64 | #endif // defined(USE_NSS) |
| 65 | |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 66 | #if defined(OS_CHROMEOS) |
| 67 | #include "chrome/browser/chromeos/proxy_config_service_impl.h" |
| 68 | #endif // defined(OS_CHROMEOS) |
| 69 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 70 | using content::BrowserThread; |
| 71 | |
[email protected] | 075c032 | 2012-02-14 00:56:44 | [diff] [blame] | 72 | class SafeBrowsingURLRequestContext; |
| 73 | |
[email protected] | 21ee224e | 2011-11-21 02:17:53 | [diff] [blame] | 74 | // The IOThread object must outlive any tasks posted to the IO thread before the |
| 75 | // Quit task, so base::Bind() calls are not refcounted. |
| 76 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 77 | namespace { |
| 78 | |
[email protected] | 11f5e3a | 2012-09-27 00:30:13 | [diff] [blame] | 79 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 80 | void ObserveKeychainEvents() { |
| 81 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 82 | net::CertDatabase::GetInstance()->SetMessageLoopForKeychainEvents(); |
| 83 | } |
| 84 | #endif |
| 85 | |
[email protected] | bd0875e | 2011-03-25 05:13:35 | [diff] [blame] | 86 | // Custom URLRequestContext used by requests which aren't associated with a |
| 87 | // particular profile. We need to use a subclass of URLRequestContext in order |
| 88 | // to provide the correct User-Agent. |
| 89 | class URLRequestContextWithUserAgent : public net::URLRequestContext { |
| 90 | public: |
| 91 | virtual const std::string& GetUserAgent( |
| 92 | const GURL& url) const OVERRIDE { |
[email protected] | b7552023 | 2011-10-12 23:45:25 | [diff] [blame] | 93 | return content::GetUserAgent(url); |
[email protected] | bd0875e | 2011-03-25 05:13:35 | [diff] [blame] | 94 | } |
[email protected] | 649d1c0 | 2012-04-27 02:56:21 | [diff] [blame] | 95 | |
| 96 | protected: |
| 97 | virtual ~URLRequestContextWithUserAgent() {} |
[email protected] | bd0875e | 2011-03-25 05:13:35 | [diff] [blame] | 98 | }; |
| 99 | |
[email protected] | 77feb46 | 2011-05-16 23:37:25 | [diff] [blame] | 100 | // Used for the "system" URLRequestContext. If this grows more complicated, then |
| 101 | // consider inheriting directly from URLRequestContext rather than using |
| 102 | // implementation inheritance. |
| 103 | class SystemURLRequestContext : public URLRequestContextWithUserAgent { |
| 104 | public: |
| 105 | SystemURLRequestContext() { |
| 106 | #if defined(USE_NSS) |
[email protected] | 8c434cbc | 2012-03-14 14:25:09 | [diff] [blame] | 107 | net::SetURLRequestContextForNSSHttpIO(this); |
[email protected] | 77feb46 | 2011-05-16 23:37:25 | [diff] [blame] | 108 | #endif // defined(USE_NSS) |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | virtual ~SystemURLRequestContext() { |
| 113 | #if defined(USE_NSS) |
[email protected] | 8c434cbc | 2012-03-14 14:25:09 | [diff] [blame] | 114 | net::SetURLRequestContextForNSSHttpIO(NULL); |
[email protected] | 77feb46 | 2011-05-16 23:37:25 | [diff] [blame] | 115 | #endif // defined(USE_NSS) |
| 116 | } |
| 117 | }; |
| 118 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 119 | scoped_ptr<net::HostResolver> CreateGlobalHostResolver(net::NetLog* net_log) { |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 120 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 121 | |
[email protected] | 026876f3 | 2012-08-22 23:53:40 | [diff] [blame] | 122 | bool allow_async_dns_field_trial = true; |
| 123 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 124 | net::HostResolver::Options options; |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 125 | |
| 126 | // Use the concurrency override from the command-line, if any. |
| 127 | if (command_line.HasSwitch(switches::kHostResolverParallelism)) { |
[email protected] | 026876f3 | 2012-08-22 23:53:40 | [diff] [blame] | 128 | allow_async_dns_field_trial = false; |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 129 | std::string s = |
| 130 | command_line.GetSwitchValueASCII(switches::kHostResolverParallelism); |
| 131 | |
| 132 | // Parse the switch (it should be a positive integer formatted as decimal). |
| 133 | int n; |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 134 | if (base::StringToInt(s, &n) && n > 0) { |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 135 | options.max_concurrent_resolves = static_cast<size_t>(n); |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 136 | } else { |
| 137 | LOG(ERROR) << "Invalid switch for host resolver parallelism: " << s; |
| 138 | } |
| 139 | } |
| 140 | |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 141 | // Use the retry attempts override from the command-line, if any. |
| 142 | if (command_line.HasSwitch(switches::kHostResolverRetryAttempts)) { |
[email protected] | 026876f3 | 2012-08-22 23:53:40 | [diff] [blame] | 143 | allow_async_dns_field_trial = false; |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 144 | std::string s = |
| 145 | command_line.GetSwitchValueASCII(switches::kHostResolverRetryAttempts); |
| 146 | // Parse the switch (it should be a non-negative integer). |
| 147 | int n; |
| 148 | if (base::StringToInt(s, &n) && n >= 0) { |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 149 | options.max_retry_attempts = static_cast<size_t>(n); |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 150 | } else { |
| 151 | LOG(ERROR) << "Invalid switch for host resolver retry attempts: " << s; |
| 152 | } |
| 153 | } |
| 154 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 155 | if (command_line.HasSwitch(switches::kEnableAsyncDns)) { |
[email protected] | 026876f3 | 2012-08-22 23:53:40 | [diff] [blame] | 156 | allow_async_dns_field_trial = false; |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 157 | options.enable_async = true; |
[email protected] | 026876f3 | 2012-08-22 23:53:40 | [diff] [blame] | 158 | } else if (command_line.HasSwitch(switches::kDisableAsyncDns)) { |
| 159 | allow_async_dns_field_trial = false; |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 160 | options.enable_async = false; |
[email protected] | d987cca | 2011-07-21 12:54:37 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 026876f3 | 2012-08-22 23:53:40 | [diff] [blame] | 163 | if (allow_async_dns_field_trial) |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 164 | options.enable_async = chrome_browser_net::ConfigureAsyncDnsFieldTrial(); |
[email protected] | 026876f3 | 2012-08-22 23:53:40 | [diff] [blame] | 165 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 166 | scoped_ptr<net::HostResolver> global_host_resolver( |
| 167 | net::HostResolver::CreateSystemResolver(options, net_log)); |
[email protected] | 9087aa3 | 2010-02-18 08:03:38 | [diff] [blame] | 168 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 169 | // Determine if we should disable IPv6 support. |
[email protected] | 9087aa3 | 2010-02-18 08:03:38 | [diff] [blame] | 170 | if (!command_line.HasSwitch(switches::kEnableIPv6)) { |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 171 | if (command_line.HasSwitch(switches::kDisableIPv6)) { |
[email protected] | 46f6e20 | 2010-02-26 06:07:25 | [diff] [blame] | 172 | global_host_resolver->SetDefaultAddressFamily(net::ADDRESS_FAMILY_IPV4); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 173 | } else { |
[email protected] | a78f427 | 2011-10-21 19:16:33 | [diff] [blame] | 174 | global_host_resolver->ProbeIPv6Support(); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 175 | } |
[email protected] | 9087aa3 | 2010-02-18 08:03:38 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 9087aa3 | 2010-02-18 08:03:38 | [diff] [blame] | 178 | // If hostname remappings were specified on the command-line, layer these |
| 179 | // rules on top of the real host resolver. This allows forwarding all requests |
| 180 | // through a designated test server. |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 181 | if (!command_line.HasSwitch(switches::kHostResolverRules)) |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 182 | return global_host_resolver.PassAs<net::HostResolver>(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 183 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 184 | scoped_ptr<net::MappedHostResolver> remapped_resolver( |
| 185 | new net::MappedHostResolver(global_host_resolver.Pass())); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 186 | remapped_resolver->SetRulesFromString( |
| 187 | command_line.GetSwitchValueASCII(switches::kHostResolverRules)); |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 188 | return remapped_resolver.PassAs<net::HostResolver>(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 191 | // TODO(willchan): Remove proxy script fetcher context since it's not necessary |
| 192 | // now that I got rid of refcounting URLRequestContexts. |
[email protected] | 77feb46 | 2011-05-16 23:37:25 | [diff] [blame] | 193 | // See IOThread::Globals for details. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 194 | net::URLRequestContext* |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 195 | ConstructProxyScriptFetcherContext(IOThread::Globals* globals, |
| 196 | net::NetLog* net_log) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 197 | net::URLRequestContext* context = new URLRequestContextWithUserAgent; |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 198 | context->set_net_log(net_log); |
| 199 | context->set_host_resolver(globals->host_resolver.get()); |
| 200 | context->set_cert_verifier(globals->cert_verifier.get()); |
[email protected] | a2a4197 | 2011-12-07 17:47:27 | [diff] [blame] | 201 | context->set_transport_security_state( |
| 202 | globals->transport_security_state.get()); |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 203 | context->set_http_auth_handler_factory( |
| 204 | globals->http_auth_handler_factory.get()); |
| 205 | context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get()); |
| 206 | context->set_http_transaction_factory( |
[email protected] | 52617df | 2010-12-24 07:30:01 | [diff] [blame] | 207 | globals->proxy_script_fetcher_http_transaction_factory.get()); |
[email protected] | 933bc5c6 | 2011-04-12 19:08:02 | [diff] [blame] | 208 | context->set_ftp_transaction_factory( |
| 209 | globals->proxy_script_fetcher_ftp_transaction_factory.get()); |
[email protected] | 273e37d | 2011-08-11 01:49:12 | [diff] [blame] | 210 | context->set_cookie_store(globals->system_cookie_store.get()); |
[email protected] | 9c4eff2 | 2012-03-20 22:42:29 | [diff] [blame] | 211 | context->set_server_bound_cert_service( |
| 212 | globals->system_server_bound_cert_service.get()); |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 213 | context->set_network_delegate(globals->system_network_delegate.get()); |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 214 | // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the |
| 215 | // system URLRequestContext too. There's no reason this should be tied to a |
| 216 | // profile. |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 217 | return context; |
| 218 | } |
| 219 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 220 | net::URLRequestContext* |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 221 | ConstructSystemRequestContext(IOThread::Globals* globals, |
| 222 | net::NetLog* net_log) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 223 | net::URLRequestContext* context = new SystemURLRequestContext; |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 224 | context->set_net_log(net_log); |
| 225 | context->set_host_resolver(globals->host_resolver.get()); |
| 226 | context->set_cert_verifier(globals->cert_verifier.get()); |
[email protected] | a2a4197 | 2011-12-07 17:47:27 | [diff] [blame] | 227 | context->set_transport_security_state( |
| 228 | globals->transport_security_state.get()); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 229 | context->set_http_auth_handler_factory( |
| 230 | globals->http_auth_handler_factory.get()); |
| 231 | context->set_proxy_service(globals->system_proxy_service.get()); |
| 232 | context->set_http_transaction_factory( |
| 233 | globals->system_http_transaction_factory.get()); |
[email protected] | 933bc5c6 | 2011-04-12 19:08:02 | [diff] [blame] | 234 | context->set_ftp_transaction_factory( |
| 235 | globals->system_ftp_transaction_factory.get()); |
[email protected] | 273e37d | 2011-08-11 01:49:12 | [diff] [blame] | 236 | context->set_cookie_store(globals->system_cookie_store.get()); |
[email protected] | 9c4eff2 | 2012-03-20 22:42:29 | [diff] [blame] | 237 | context->set_server_bound_cert_service( |
| 238 | globals->system_server_bound_cert_service.get()); |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 239 | context->set_throttler_manager(globals->throttler_manager.get()); |
[email protected] | 815c69cf | 2012-06-30 00:52:08 | [diff] [blame] | 240 | context->set_network_delegate(globals->system_network_delegate.get()); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 241 | return context; |
| 242 | } |
| 243 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 244 | } // namespace |
| 245 | |
[email protected] | e0845d5f | 2012-05-29 00:11:41 | [diff] [blame] | 246 | class IOThread::LoggingNetworkChangeObserver |
| 247 | : public net::NetworkChangeNotifier::IPAddressObserver { |
| 248 | public: |
| 249 | // |net_log| must remain valid throughout our lifetime. |
| 250 | explicit LoggingNetworkChangeObserver(net::NetLog* net_log) |
| 251 | : net_log_(net_log) { |
| 252 | net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 253 | } |
| 254 | |
| 255 | ~LoggingNetworkChangeObserver() { |
| 256 | net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 257 | } |
| 258 | |
| 259 | virtual void OnIPAddressChanged() { |
| 260 | VLOG(1) << "Observed a change to the network IP addresses"; |
| 261 | |
[email protected] | 2fa0891 | 2012-06-14 20:56:26 | [diff] [blame] | 262 | net_log_->AddGlobalEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED); |
[email protected] | e0845d5f | 2012-05-29 00:11:41 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | private: |
| 266 | net::NetLog* net_log_; |
| 267 | DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver); |
| 268 | }; |
| 269 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 270 | class SystemURLRequestContextGetter : public net::URLRequestContextGetter { |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 271 | public: |
| 272 | explicit SystemURLRequestContextGetter(IOThread* io_thread); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 273 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 274 | // Implementation for net::UrlRequestContextGetter. |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 275 | virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; |
| 276 | virtual scoped_refptr<base::SingleThreadTaskRunner> |
| 277 | GetNetworkTaskRunner() const OVERRIDE; |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 278 | |
[email protected] | 13ed17f8 | 2012-04-06 02:27:18 | [diff] [blame] | 279 | protected: |
| 280 | virtual ~SystemURLRequestContextGetter(); |
| 281 | |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 282 | private: |
| 283 | IOThread* const io_thread_; // Weak pointer, owned by BrowserProcess. |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 284 | scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 285 | |
| 286 | base::debug::LeakTracker<SystemURLRequestContextGetter> leak_tracker_; |
| 287 | }; |
| 288 | |
| 289 | SystemURLRequestContextGetter::SystemURLRequestContextGetter( |
| 290 | IOThread* io_thread) |
| 291 | : io_thread_(io_thread), |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 292 | network_task_runner_( |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 293 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)) { |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | SystemURLRequestContextGetter::~SystemURLRequestContextGetter() {} |
| 297 | |
| 298 | net::URLRequestContext* SystemURLRequestContextGetter::GetURLRequestContext() { |
| 299 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 300 | DCHECK(io_thread_->globals()->system_request_context.get()); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 301 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 302 | return io_thread_->globals()->system_request_context.get(); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 305 | scoped_refptr<base::SingleThreadTaskRunner> |
| 306 | SystemURLRequestContextGetter::GetNetworkTaskRunner() const { |
| 307 | return network_task_runner_; |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 308 | } |
| 309 | |
[email protected] | c93123fa | 2012-04-19 02:49:48 | [diff] [blame] | 310 | IOThread::Globals:: |
| 311 | SystemRequestContextLeakChecker::SystemRequestContextLeakChecker( |
| 312 | Globals* globals) |
| 313 | : globals_(globals) { |
| 314 | DCHECK(globals_); |
[email protected] | 7613faae | 2012-04-18 01:01:19 | [diff] [blame] | 315 | } |
[email protected] | 1889dc1b | 2010-10-14 22:03:13 | [diff] [blame] | 316 | |
[email protected] | c93123fa | 2012-04-19 02:49:48 | [diff] [blame] | 317 | IOThread::Globals:: |
| 318 | SystemRequestContextLeakChecker::~SystemRequestContextLeakChecker() { |
| 319 | if (globals_->system_request_context.get()) |
| 320 | globals_->system_request_context->AssertNoURLRequests(); |
| 321 | } |
| 322 | |
| 323 | IOThread::Globals::Globals() |
| 324 | : ALLOW_THIS_IN_INITIALIZER_LIST( |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 325 | system_request_context_leak_checker(this)), |
| 326 | ignore_certificate_errors(false), |
| 327 | http_pipelining_enabled(false), |
| 328 | testing_fixed_http_port(0), |
| 329 | testing_fixed_https_port(0) {} |
| 330 | |
[email protected] | c93123fa | 2012-04-19 02:49:48 | [diff] [blame] | 331 | IOThread::Globals::~Globals() {} |
| 332 | |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 333 | // |local_state| is passed in explicitly in order to (1) reduce implicit |
| 334 | // dependencies and (2) make IOThread more flexible for testing. |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 335 | IOThread::IOThread( |
| 336 | PrefService* local_state, |
| 337 | ChromeNetLog* net_log, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 338 | extensions::EventRouterForwarder* extension_event_router_forwarder) |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 339 | : net_log_(net_log), |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 340 | extension_event_router_forwarder_(extension_event_router_forwarder), |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 341 | globals_(NULL), |
[email protected] | 4a10949 | 2011-09-24 21:00:12 | [diff] [blame] | 342 | sdch_manager_(NULL), |
[email protected] | 21ee224e | 2011-11-21 02:17:53 | [diff] [blame] | 343 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 344 | // We call RegisterPrefs() here (instead of inside browser_prefs.cc) to make |
| 345 | // sure that everything is initialized in the right order. |
| 346 | RegisterPrefs(local_state); |
| 347 | auth_schemes_ = local_state->GetString(prefs::kAuthSchemes); |
| 348 | negotiate_disable_cname_lookup_ = local_state->GetBoolean( |
| 349 | prefs::kDisableAuthNegotiateCnameLookup); |
| 350 | negotiate_enable_port_ = local_state->GetBoolean( |
| 351 | prefs::kEnableAuthNegotiatePort); |
| 352 | auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist); |
| 353 | auth_delegate_whitelist_ = local_state->GetString( |
| 354 | prefs::kAuthNegotiateDelegateWhitelist); |
[email protected] | ac7f3fdb | 2010-11-12 12:47:05 | [diff] [blame] | 355 | gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName); |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 356 | pref_proxy_config_tracker_.reset( |
| 357 | ProxyServiceFactory::CreatePrefProxyConfigTracker(local_state)); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 358 | ChromeNetworkDelegate::InitializePrefsOnUIThread( |
| 359 | &system_enable_referrers_, |
| 360 | NULL, |
| 361 | local_state); |
[email protected] | 4d45a6de | 2011-05-13 05:20:18 | [diff] [blame] | 362 | ssl_config_service_manager_.reset( |
[email protected] | e6426c1b | 2012-09-12 21:51:41 | [diff] [blame] | 363 | SSLConfigServiceManager::CreateDefaultManager(local_state, NULL)); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 364 | |
| 365 | BrowserThread::SetDelegate(BrowserThread::IO, this); |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 366 | } |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 367 | |
| 368 | IOThread::~IOThread() { |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 369 | // This isn't needed for production code, but in tests, IOThread may |
| 370 | // be multiply constructed. |
| 371 | BrowserThread::SetDelegate(BrowserThread::IO, NULL); |
| 372 | |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 373 | if (pref_proxy_config_tracker_.get()) |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 374 | pref_proxy_config_tracker_->DetachFromPrefService(); |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 375 | DCHECK(!globals_); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 376 | } |
| 377 | |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 378 | IOThread::Globals* IOThread::globals() { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 379 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 380 | return globals_; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 381 | } |
| 382 | |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 383 | ChromeNetLog* IOThread::net_log() { |
| 384 | return net_log_; |
| 385 | } |
| 386 | |
[email protected] | b09f76d6 | 2011-12-07 01:51:06 | [diff] [blame] | 387 | void IOThread::ChangedToOnTheRecord() { |
| 388 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 389 | BrowserThread::PostTask( |
| 390 | BrowserThread::IO, |
| 391 | FROM_HERE, |
| 392 | base::Bind(&IOThread::ChangedToOnTheRecordOnIOThread, |
| 393 | base::Unretained(this))); |
| 394 | } |
| 395 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 396 | net::URLRequestContextGetter* IOThread::system_url_request_context_getter() { |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 397 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 398 | if (!system_url_request_context_getter_) { |
[email protected] | addb324 | 2011-06-13 21:39:16 | [diff] [blame] | 399 | InitSystemRequestContext(); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 400 | } |
| 401 | return system_url_request_context_getter_; |
| 402 | } |
| 403 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 404 | void IOThread::Init() { |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 405 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 70b9234 | 2010-10-12 05:54:06 | [diff] [blame] | 406 | |
| 407 | #if defined(USE_NSS) |
[email protected] | 8c434cbc | 2012-03-14 14:25:09 | [diff] [blame] | 408 | net::SetMessageLoopForNSSHttpIO(); |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 409 | #endif // defined(USE_NSS) |
[email protected] | 70b9234 | 2010-10-12 05:54:06 | [diff] [blame] | 410 | |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 411 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 412 | |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 413 | DCHECK(!globals_); |
| 414 | globals_ = new Globals; |
| 415 | |
[email protected] | 58bc704 | 2010-07-07 18:04:14 | [diff] [blame] | 416 | // Add an observer that will emit network change events to the ChromeNetLog. |
| 417 | // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be |
| 418 | // logging the network change before other IO thread consumers respond to it. |
| 419 | network_change_observer_.reset( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 420 | new LoggingNetworkChangeObserver(net_log_)); |
[email protected] | 58bc704 | 2010-07-07 18:04:14 | [diff] [blame] | 421 | |
[email protected] | cde8b3c | 2012-08-13 19:20:52 | [diff] [blame] | 422 | // Setup the HistogramWatcher to run on the IO thread. |
| 423 | net::NetworkChangeNotifier::InitHistogramWatcher(); |
| 424 | |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 425 | globals_->extension_event_router_forwarder = |
| 426 | extension_event_router_forwarder_; |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 427 | ChromeNetworkDelegate* network_delegate = new ChromeNetworkDelegate( |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 428 | extension_event_router_forwarder_, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 429 | NULL, |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 430 | NULL, |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 431 | NULL, |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 432 | NULL, |
[email protected] | e9c41d2 | 2012-08-17 00:08:15 | [diff] [blame] | 433 | NULL, |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 434 | &system_enable_referrers_, |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 435 | NULL, |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 436 | NULL); |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 437 | if (command_line.HasSwitch(switches::kDisableExtensionsHttpThrottling)) |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 438 | network_delegate->NeverThrottleRequests(); |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 439 | globals_->system_network_delegate.reset(network_delegate); |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 440 | globals_->host_resolver = CreateGlobalHostResolver(net_log_); |
[email protected] | 9f59fac | 2012-03-21 23:18:11 | [diff] [blame] | 441 | globals_->cert_verifier.reset(net::CertVerifier::CreateDefault()); |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 442 | globals_->transport_security_state.reset(new net::TransportSecurityState()); |
[email protected] | 4d45a6de | 2011-05-13 05:20:18 | [diff] [blame] | 443 | globals_->ssl_config_service = GetSSLConfigService(); |
[email protected] | ec44ee0 | 2012-09-28 21:31:51 | [diff] [blame] | 444 | if (command_line.HasSwitch(switches::kSpdyProxyOrigin)) { |
| 445 | spdyproxy_origin_ = |
| 446 | command_line.GetSwitchValueASCII(switches::kSpdyProxyOrigin); |
| 447 | } |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 448 | globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 449 | globals_->host_resolver.get())); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 450 | globals_->http_server_properties.reset(new net::HttpServerPropertiesImpl); |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 451 | // For the ProxyScriptFetcher, we use a direct ProxyService. |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 452 | globals_->proxy_script_fetcher_proxy_service.reset( |
| 453 | net::ProxyService::CreateDirectWithNetLog(net_log_)); |
[email protected] | 273e37d | 2011-08-11 01:49:12 | [diff] [blame] | 454 | // In-memory cookie store. |
| 455 | globals_->system_cookie_store = new net::CookieMonster(NULL, NULL); |
[email protected] | 9c4eff2 | 2012-03-20 22:42:29 | [diff] [blame] | 456 | // In-memory server bound cert store. |
| 457 | globals_->system_server_bound_cert_service.reset( |
| 458 | new net::ServerBoundCertService( |
[email protected] | 5bab49ec | 2012-05-04 21:13:19 | [diff] [blame] | 459 | new net::DefaultServerBoundCertStore(NULL), |
| 460 | base::WorkerPool::GetTaskRunner(true))); |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 461 | globals_->load_time_stats.reset(new chrome_browser_net::LoadTimeStats()); |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 462 | globals_->host_mapping_rules.reset(new net::HostMappingRules()); |
| 463 | if (command_line.HasSwitch(switches::kHostRules)) { |
| 464 | globals_->host_mapping_rules->SetRulesFromString( |
| 465 | command_line.GetSwitchValueASCII(switches::kHostRules)); |
| 466 | } |
| 467 | if (command_line.HasSwitch(switches::kIgnoreCertificateErrors)) |
| 468 | globals_->ignore_certificate_errors = true; |
| 469 | if (command_line.HasSwitch(switches::kEnableHttpPipelining)) |
| 470 | globals_->http_pipelining_enabled = true; |
| 471 | if (command_line.HasSwitch(switches::kTestingFixedHttpPort)) { |
| 472 | int value; |
| 473 | base::StringToInt( |
| 474 | command_line.GetSwitchValueASCII( |
| 475 | switches::kTestingFixedHttpPort), |
| 476 | &value); |
| 477 | globals_->testing_fixed_http_port = value; |
| 478 | } |
| 479 | if (command_line.HasSwitch(switches::kTestingFixedHttpsPort)) { |
| 480 | int value; |
| 481 | base::StringToInt( |
| 482 | command_line.GetSwitchValueASCII( |
| 483 | switches::kTestingFixedHttpsPort), |
| 484 | &value); |
| 485 | globals_->testing_fixed_https_port = value; |
| 486 | } |
| 487 | |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 488 | net::HttpNetworkSession::Params session_params; |
| 489 | session_params.host_resolver = globals_->host_resolver.get(); |
| 490 | session_params.cert_verifier = globals_->cert_verifier.get(); |
[email protected] | 9c4eff2 | 2012-03-20 22:42:29 | [diff] [blame] | 491 | session_params.server_bound_cert_service = |
| 492 | globals_->system_server_bound_cert_service.get(); |
[email protected] | a2a4197 | 2011-12-07 17:47:27 | [diff] [blame] | 493 | session_params.transport_security_state = |
| 494 | globals_->transport_security_state.get(); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 495 | session_params.proxy_service = |
| 496 | globals_->proxy_script_fetcher_proxy_service.get(); |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 497 | session_params.ssl_config_service = globals_->ssl_config_service.get(); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 498 | session_params.http_auth_handler_factory = |
| 499 | globals_->http_auth_handler_factory.get(); |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 500 | session_params.http_server_properties = |
| 501 | globals_->http_server_properties.get(); |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 502 | session_params.network_delegate = globals_->system_network_delegate.get(); |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 503 | // TODO(rtenneti): We should probably use HttpServerPropertiesManager for the |
| 504 | // system URLRequestContext too. There's no reason this should be tied to a |
| 505 | // profile. |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 506 | session_params.net_log = net_log_; |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 507 | session_params.host_mapping_rules = globals_->host_mapping_rules.get(); |
| 508 | session_params.ignore_certificate_errors = |
| 509 | globals_->ignore_certificate_errors; |
| 510 | session_params.http_pipelining_enabled = globals_->http_pipelining_enabled; |
| 511 | session_params.testing_fixed_http_port = globals_->testing_fixed_http_port; |
[email protected] | 84ed2976 | 2012-09-16 04:00:58 | [diff] [blame] | 512 | session_params.testing_fixed_https_port = globals_->testing_fixed_https_port; |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 513 | |
[email protected] | 57cb0f7 | 2011-01-28 06:33:58 | [diff] [blame] | 514 | scoped_refptr<net::HttpNetworkSession> network_session( |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 515 | new net::HttpNetworkSession(session_params)); |
[email protected] | 57cb0f7 | 2011-01-28 06:33:58 | [diff] [blame] | 516 | globals_->proxy_script_fetcher_http_transaction_factory.reset( |
| 517 | new net::HttpNetworkLayer(network_session)); |
[email protected] | 933bc5c6 | 2011-04-12 19:08:02 | [diff] [blame] | 518 | globals_->proxy_script_fetcher_ftp_transaction_factory.reset( |
| 519 | new net::FtpNetworkLayer(globals_->host_resolver.get())); |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 520 | |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 521 | globals_->throttler_manager.reset(new net::URLRequestThrottlerManager()); |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 522 | globals_->throttler_manager->set_net_log(net_log_); |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 523 | // Always done in production, disabled only for unit tests. |
| 524 | globals_->throttler_manager->set_enable_thread_checks(true); |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 525 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 526 | globals_->proxy_script_fetcher_context.reset( |
| 527 | ConstructProxyScriptFetcherContext(globals_, net_log_)); |
[email protected] | 4a10949 | 2011-09-24 21:00:12 | [diff] [blame] | 528 | |
| 529 | sdch_manager_ = new net::SdchManager(); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 530 | |
[email protected] | 11f5e3a | 2012-09-27 00:30:13 | [diff] [blame] | 531 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 532 | // Start observing Keychain events. This needs to be done on the UI thread, |
| 533 | // as Keychain services requires a CFRunLoop. |
| 534 | BrowserThread::PostTask(BrowserThread::UI, |
| 535 | FROM_HERE, |
| 536 | base::Bind(&ObserveKeychainEvents)); |
| 537 | #endif |
| 538 | |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 539 | // InitSystemRequestContext turns right around and posts a task back |
| 540 | // to the IO thread, so we can't let it run until we know the IO |
| 541 | // thread has started. |
| 542 | // |
| 543 | // Note that since we are at BrowserThread::Init time, the UI thread |
| 544 | // is blocked waiting for the thread to start. Therefore, posting |
| 545 | // this task to the main thread's message loop here is guaranteed to |
| 546 | // get it onto the message loop while the IOThread object still |
| 547 | // exists. However, the message might not be processed on the UI |
| 548 | // thread until after IOThread is gone, so use a weak pointer. |
| 549 | BrowserThread::PostTask(BrowserThread::UI, |
| 550 | FROM_HERE, |
| 551 | base::Bind(&IOThread::InitSystemRequestContext, |
| 552 | weak_factory_.GetWeakPtr())); |
| 553 | |
| 554 | // We constructed the weak pointer on the IO thread but it will be |
| 555 | // used on the UI thread. Call this to avoid a thread checker |
| 556 | // error. |
| 557 | weak_factory_.DetachFromThread(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 558 | } |
| 559 | |
[email protected] | 2a92cd9 | 2010-04-27 00:01:41 | [diff] [blame] | 560 | void IOThread::CleanUp() { |
[email protected] | 075c032 | 2012-02-14 00:56:44 | [diff] [blame] | 561 | base::debug::LeakTracker<SafeBrowsingURLRequestContext>::CheckForLeaks(); |
| 562 | |
[email protected] | 4a10949 | 2011-09-24 21:00:12 | [diff] [blame] | 563 | delete sdch_manager_; |
| 564 | sdch_manager_ = NULL; |
| 565 | |
[email protected] | 59a3b36 | 2010-10-21 21:52:41 | [diff] [blame] | 566 | #if defined(USE_NSS) |
[email protected] | 8c434cbc | 2012-03-14 14:25:09 | [diff] [blame] | 567 | net::ShutdownNSSHttpIO(); |
[email protected] | 59a3b36 | 2010-10-21 21:52:41 | [diff] [blame] | 568 | #endif // defined(USE_NSS) |
| 569 | |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 570 | system_url_request_context_getter_ = NULL; |
| 571 | |
[email protected] | af66993 | 2012-01-17 19:26:58 | [diff] [blame] | 572 | // Release objects that the net::URLRequestContext could have been pointing |
| 573 | // to. |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 574 | |
| 575 | // This must be reset before the ChromeNetLog is destroyed. |
| 576 | network_change_observer_.reset(); |
| 577 | |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 578 | system_proxy_config_service_.reset(); |
| 579 | |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 580 | delete globals_; |
| 581 | globals_ = NULL; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 582 | |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 583 | base::debug::LeakTracker<SystemURLRequestContextGetter>::CheckForLeaks(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 584 | } |
| 585 | |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 586 | // static |
| 587 | void IOThread::RegisterPrefs(PrefService* local_state) { |
| 588 | local_state->RegisterStringPref(prefs::kAuthSchemes, |
[email protected] | ec44ee0 | 2012-09-28 21:31:51 | [diff] [blame] | 589 | "basic,digest,ntlm,negotiate," |
| 590 | "spdyproxy"); |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 591 | local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, |
| 592 | false); |
| 593 | local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); |
| 594 | local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); |
| 595 | local_state->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist, ""); |
[email protected] | ac7f3fdb | 2010-11-12 12:47:05 | [diff] [blame] | 596 | local_state->RegisterStringPref(prefs::kGSSAPILibraryName, ""); |
[email protected] | ec44ee0 | 2012-09-28 21:31:51 | [diff] [blame] | 597 | local_state->RegisterStringPref(prefs::kSpdyProxyOrigin, ""); |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 598 | local_state->RegisterBooleanPref(prefs::kEnableReferrers, true); |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame^] | 599 | local_state->RegisterInt64Pref(prefs::kHttpReceivedContentLength, 0); |
| 600 | local_state->RegisterInt64Pref(prefs::kHttpOriginalContentLength, 0); |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 601 | } |
| 602 | |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 603 | net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory( |
| 604 | net::HostResolver* resolver) { |
[email protected] | 9030a63 | 2010-11-19 20:12:09 | [diff] [blame] | 605 | net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL; |
| 606 | if (!auth_server_whitelist_.empty()) { |
| 607 | auth_filter_default_credentials = |
| 608 | new net::HttpAuthFilterWhitelist(auth_server_whitelist_); |
| 609 | } |
| 610 | net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL; |
| 611 | if (!auth_delegate_whitelist_.empty()) { |
| 612 | auth_filter_delegate = |
| 613 | new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_); |
| 614 | } |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 615 | globals_->url_security_manager.reset( |
[email protected] | d201b200e | 2010-08-27 17:35:02 | [diff] [blame] | 616 | net::URLSecurityManager::Create(auth_filter_default_credentials, |
| 617 | auth_filter_delegate)); |
[email protected] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 618 | std::vector<std::string> supported_schemes; |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 619 | base::SplitString(auth_schemes_, ',', &supported_schemes); |
[email protected] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 620 | |
[email protected] | ec44ee0 | 2012-09-28 21:31:51 | [diff] [blame] | 621 | scoped_ptr<net::HttpAuthHandlerRegistryFactory> registry_factory( |
| 622 | net::HttpAuthHandlerRegistryFactory::Create( |
| 623 | supported_schemes, globals_->url_security_manager.get(), |
| 624 | resolver, gssapi_library_name_, negotiate_disable_cname_lookup_, |
| 625 | negotiate_enable_port_)); |
| 626 | |
| 627 | if (!spdyproxy_origin_.empty()) { |
| 628 | GURL origin_url(spdyproxy_origin_); |
| 629 | if (origin_url.is_valid()) { |
| 630 | registry_factory->RegisterSchemeFactory( |
| 631 | "spdyproxy", |
| 632 | new spdyproxy::HttpAuthHandlerSpdyProxy::Factory(origin_url)); |
| 633 | } else { |
| 634 | LOG(WARNING) << "Skipping creation of SpdyProxy auth handler since " |
| 635 | << "authorized origin is invalid: " |
| 636 | << spdyproxy_origin_; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | return registry_factory.release(); |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 641 | } |
| 642 | |
[email protected] | d6f37fc | 2011-02-13 23:58:41 | [diff] [blame] | 643 | void IOThread::ClearHostCache() { |
| 644 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 645 | |
[email protected] | 489d1a8 | 2011-10-12 03:09:11 | [diff] [blame] | 646 | net::HostCache* host_cache = globals_->host_resolver->GetHostCache(); |
| 647 | if (host_cache) |
| 648 | host_cache->clear(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 649 | } |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 650 | |
[email protected] | 4d45a6de | 2011-05-13 05:20:18 | [diff] [blame] | 651 | net::SSLConfigService* IOThread::GetSSLConfigService() { |
| 652 | return ssl_config_service_manager_->Get(); |
| 653 | } |
| 654 | |
[email protected] | b09f76d6 | 2011-12-07 01:51:06 | [diff] [blame] | 655 | void IOThread::ChangedToOnTheRecordOnIOThread() { |
| 656 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 657 | |
| 658 | // Clear the host cache to avoid showing entries from the OTR session |
| 659 | // in about:net-internals. |
| 660 | ClearHostCache(); |
[email protected] | b09f76d6 | 2011-12-07 01:51:06 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 663 | void IOThread::InitSystemRequestContext() { |
[email protected] | addb324 | 2011-06-13 21:39:16 | [diff] [blame] | 664 | if (system_url_request_context_getter_) |
| 665 | return; |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 666 | // If we're in unit_tests, IOThread may not be run. |
[email protected] | dd48370 | 2011-12-02 14:47:42 | [diff] [blame] | 667 | if (!BrowserThread::IsMessageLoopValid(BrowserThread::IO)) |
[email protected] | 63e2682 | 2011-07-16 19:07:35 | [diff] [blame] | 668 | return; |
[email protected] | 71fdf77 | 2011-12-18 16:28:38 | [diff] [blame] | 669 | bool wait_for_first_update = (pref_proxy_config_tracker_.get() != NULL); |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 670 | ChromeProxyConfigService* proxy_config_service = |
[email protected] | 71fdf77 | 2011-12-18 16:28:38 | [diff] [blame] | 671 | ProxyServiceFactory::CreateProxyConfigService(wait_for_first_update); |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 672 | system_proxy_config_service_.reset(proxy_config_service); |
| 673 | if (pref_proxy_config_tracker_.get()) { |
| 674 | pref_proxy_config_tracker_->SetChromeProxyConfigService( |
| 675 | proxy_config_service); |
| 676 | } |
[email protected] | addb324 | 2011-06-13 21:39:16 | [diff] [blame] | 677 | system_url_request_context_getter_ = |
| 678 | new SystemURLRequestContextGetter(this); |
[email protected] | 2e5b60a2 | 2011-11-28 15:56:41 | [diff] [blame] | 679 | // Safe to post an unretained this pointer, since IOThread is |
| 680 | // guaranteed to outlive the IO BrowserThread. |
[email protected] | dd48370 | 2011-12-02 14:47:42 | [diff] [blame] | 681 | BrowserThread::PostTask( |
| 682 | BrowserThread::IO, |
| 683 | FROM_HERE, |
| 684 | base::Bind(&IOThread::InitSystemRequestContextOnIOThread, |
| 685 | base::Unretained(this))); |
[email protected] | addb324 | 2011-06-13 21:39:16 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | void IOThread::InitSystemRequestContextOnIOThread() { |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 689 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 690 | DCHECK(!globals_->system_proxy_service.get()); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 691 | DCHECK(system_proxy_config_service_.get()); |
| 692 | |
| 693 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 694 | globals_->system_proxy_service.reset( |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 695 | ProxyServiceFactory::CreateProxyService( |
| 696 | net_log_, |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 697 | globals_->proxy_script_fetcher_context.get(), |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 698 | system_proxy_config_service_.release(), |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 699 | command_line)); |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 700 | |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 701 | net::HttpNetworkSession::Params system_params; |
| 702 | system_params.host_resolver = globals_->host_resolver.get(); |
| 703 | system_params.cert_verifier = globals_->cert_verifier.get(); |
[email protected] | 9c4eff2 | 2012-03-20 22:42:29 | [diff] [blame] | 704 | system_params.server_bound_cert_service = |
| 705 | globals_->system_server_bound_cert_service.get(); |
[email protected] | a2a4197 | 2011-12-07 17:47:27 | [diff] [blame] | 706 | system_params.transport_security_state = |
| 707 | globals_->transport_security_state.get(); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 708 | system_params.proxy_service = globals_->system_proxy_service.get(); |
| 709 | system_params.ssl_config_service = globals_->ssl_config_service.get(); |
| 710 | system_params.http_auth_handler_factory = |
| 711 | globals_->http_auth_handler_factory.get(); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 712 | system_params.http_server_properties = globals_->http_server_properties.get(); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 713 | system_params.network_delegate = globals_->system_network_delegate.get(); |
| 714 | system_params.net_log = net_log_; |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 715 | system_params.host_mapping_rules = globals_->host_mapping_rules.get(); |
| 716 | system_params.ignore_certificate_errors = globals_->ignore_certificate_errors; |
| 717 | system_params.http_pipelining_enabled = globals_->http_pipelining_enabled; |
| 718 | system_params.testing_fixed_http_port = globals_->testing_fixed_http_port; |
| 719 | system_params.testing_fixed_https_port = globals_->testing_fixed_https_port; |
| 720 | |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 721 | globals_->system_http_transaction_factory.reset( |
| 722 | new net::HttpNetworkLayer( |
| 723 | new net::HttpNetworkSession(system_params))); |
[email protected] | 933bc5c6 | 2011-04-12 19:08:02 | [diff] [blame] | 724 | globals_->system_ftp_transaction_factory.reset( |
| 725 | new net::FtpNetworkLayer(globals_->host_resolver.get())); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 726 | globals_->system_request_context.reset( |
| 727 | ConstructSystemRequestContext(globals_, net_log_)); |
[email protected] | d24fc3a0 | 2012-02-11 02:08:34 | [diff] [blame] | 728 | |
| 729 | sdch_manager_->set_sdch_fetcher( |
| 730 | new SdchDictionaryFetcher(system_url_request_context_getter_.get())); |
[email protected] | db0e86dd | 2011-03-16 14:47:21 | [diff] [blame] | 731 | } |