[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 1 | // Copyright (c) 2010 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 "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] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 10 | #include "base/debug/leak_tracker.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 12 | #include "base/metrics/field_trial.h" |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 13 | #include "base/stl_util-inl.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 14 | #include "base/string_number_conversions.h" |
[email protected] | 4e5ae20f | 2010-09-24 04:52:11 | [diff] [blame] | 15 | #include "base/string_split.h" |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 16 | #include "base/string_util.h" |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 17 | #include "base/thread_restrictions.h" |
[email protected] | e3671727 | 2010-10-12 12:07:13 | [diff] [blame] | 18 | #include "chrome/browser/browser_thread.h" |
[email protected] | 1082b1d | 2010-03-30 00:31:22 | [diff] [blame] | 19 | #include "chrome/browser/gpu_process_host.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 20 | #include "chrome/browser/net/chrome_net_log.h" |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 21 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 1889dc1b | 2010-10-14 22:03:13 | [diff] [blame] | 22 | #include "chrome/browser/net/connect_interceptor.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 23 | #include "chrome/browser/net/passive_log_collector.h" |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 24 | #include "chrome/browser/net/predictor_api.h" |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 25 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | d077249e | 2010-12-09 02:44:58 | [diff] [blame] | 26 | #include "chrome/browser/prerender/prerender_interceptor.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 27 | #include "chrome/common/chrome_switches.h" |
[email protected] | 2258c1c9 | 2010-10-28 17:51:19 | [diff] [blame] | 28 | #include "chrome/common/net/raw_host_resolver_proc.h" |
[email protected] | 68d2a05f | 2010-05-07 21:39:55 | [diff] [blame] | 29 | #include "chrome/common/net/url_fetcher.h" |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 30 | #include "chrome/common/pref_names.h" |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 31 | #include "net/base/cert_verifier.h" |
[email protected] | 2db58053 | 2010-10-08 14:32:37 | [diff] [blame] | 32 | #include "net/base/dnsrr_resolver.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 33 | #include "net/base/host_cache.h" |
| 34 | #include "net/base/host_resolver.h" |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 35 | #include "net/base/host_resolver_impl.h" |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 36 | #include "net/base/mapped_host_resolver.h" |
[email protected] | 32eaa33 | 2010-02-08 22:15:54 | [diff] [blame] | 37 | #include "net/base/net_util.h" |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 38 | #include "net/http/http_auth_filter.h" |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 39 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 40 | #include "net/http/http_network_layer.h" |
[email protected] | 70b9234 | 2010-10-12 05:54:06 | [diff] [blame] | 41 | #if defined(USE_NSS) |
| 42 | #include "net/ocsp/nss_ocsp.h" |
| 43 | #endif // defined(USE_NSS) |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 44 | #include "net/proxy/proxy_script_fetcher_impl.h" |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 45 | #include "net/socket/client_socket_factory.h" |
| 46 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 47 | |
| 48 | namespace { |
| 49 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 50 | net::HostResolver* CreateGlobalHostResolver(net::NetLog* net_log) { |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 51 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 52 | |
| 53 | size_t parallelism = net::HostResolver::kDefaultParallelism; |
| 54 | |
| 55 | // Use the concurrency override from the command-line, if any. |
| 56 | if (command_line.HasSwitch(switches::kHostResolverParallelism)) { |
| 57 | std::string s = |
| 58 | command_line.GetSwitchValueASCII(switches::kHostResolverParallelism); |
| 59 | |
| 60 | // Parse the switch (it should be a positive integer formatted as decimal). |
| 61 | int n; |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 62 | if (base::StringToInt(s, &n) && n > 0) { |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 63 | parallelism = static_cast<size_t>(n); |
| 64 | } else { |
| 65 | LOG(ERROR) << "Invalid switch for host resolver parallelism: " << s; |
| 66 | } |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 67 | } else { |
| 68 | // Set up a field trial to see what impact the total number of concurrent |
| 69 | // resolutions have on DNS resolutions. |
| 70 | base::FieldTrial::Probability kDivisor = 1000; |
| 71 | // For each option (i.e., non-default), we have a fixed probability. |
| 72 | base::FieldTrial::Probability kProbabilityPerGroup = 100; // 10%. |
| 73 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 74 | scoped_refptr<base::FieldTrial> trial( |
| 75 | new base::FieldTrial("DnsParallelism", kDivisor)); |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 76 | |
| 77 | // List options with different counts. |
| 78 | // Firefox limits total to 8 in parallel, and default is currently 50. |
| 79 | int parallel_6 = trial->AppendGroup("parallel_6", kProbabilityPerGroup); |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 80 | int parallel_7 = trial->AppendGroup("parallel_7", kProbabilityPerGroup); |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 81 | int parallel_8 = trial->AppendGroup("parallel_8", kProbabilityPerGroup); |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 82 | int parallel_9 = trial->AppendGroup("parallel_9", kProbabilityPerGroup); |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 83 | int parallel_10 = trial->AppendGroup("parallel_10", kProbabilityPerGroup); |
| 84 | int parallel_14 = trial->AppendGroup("parallel_14", kProbabilityPerGroup); |
| 85 | int parallel_20 = trial->AppendGroup("parallel_20", kProbabilityPerGroup); |
| 86 | |
| 87 | trial->AppendGroup("parallel_default", |
| 88 | base::FieldTrial::kAllRemainingProbability); |
| 89 | |
| 90 | if (trial->group() == parallel_6) |
| 91 | parallelism = 6; |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 92 | else if (trial->group() == parallel_7) |
| 93 | parallelism = 7; |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 94 | else if (trial->group() == parallel_8) |
| 95 | parallelism = 8; |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 96 | else if (trial->group() == parallel_9) |
| 97 | parallelism = 9; |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 98 | else if (trial->group() == parallel_10) |
| 99 | parallelism = 10; |
| 100 | else if (trial->group() == parallel_14) |
| 101 | parallelism = 14; |
| 102 | else if (trial->group() == parallel_20) |
| 103 | parallelism = 20; |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 2258c1c9 | 2010-10-28 17:51:19 | [diff] [blame] | 106 | // Use the specified DNS server for doing raw resolutions if requested |
| 107 | // from the command-line. |
| 108 | scoped_refptr<net::HostResolverProc> resolver_proc; |
| 109 | if (command_line.HasSwitch(switches::kDnsServer)) { |
| 110 | std::string dns_ip_string = |
| 111 | command_line.GetSwitchValueASCII(switches::kDnsServer); |
| 112 | net::IPAddressNumber dns_ip_number; |
| 113 | if (net::ParseIPLiteralToNumber(dns_ip_string, &dns_ip_number)) { |
| 114 | resolver_proc = |
| 115 | new chrome_common_net::RawHostResolverProc(dns_ip_number, NULL); |
| 116 | } else { |
| 117 | LOG(ERROR) << "Invalid IP address specified for --dns-server: " |
| 118 | << dns_ip_string; |
| 119 | } |
| 120 | } |
| 121 | |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 122 | net::HostResolver* global_host_resolver = |
[email protected] | 2258c1c9 | 2010-10-28 17:51:19 | [diff] [blame] | 123 | net::CreateSystemHostResolver(parallelism, resolver_proc.get(), net_log); |
[email protected] | 9087aa3 | 2010-02-18 08:03:38 | [diff] [blame] | 124 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 125 | // Determine if we should disable IPv6 support. |
[email protected] | 9087aa3 | 2010-02-18 08:03:38 | [diff] [blame] | 126 | if (!command_line.HasSwitch(switches::kEnableIPv6)) { |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 127 | if (command_line.HasSwitch(switches::kDisableIPv6)) { |
[email protected] | 46f6e20 | 2010-02-26 06:07:25 | [diff] [blame] | 128 | global_host_resolver->SetDefaultAddressFamily(net::ADDRESS_FAMILY_IPV4); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 129 | } else { |
| 130 | net::HostResolverImpl* host_resolver_impl = |
| 131 | global_host_resolver->GetAsHostResolverImpl(); |
| 132 | if (host_resolver_impl != NULL) { |
[email protected] | 780f849 | 2010-09-16 04:12:15 | [diff] [blame] | 133 | // Use probe to decide if support is warranted. |
| 134 | host_resolver_impl->ProbeIPv6Support(); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 135 | } |
| 136 | } |
[email protected] | 9087aa3 | 2010-02-18 08:03:38 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 9087aa3 | 2010-02-18 08:03:38 | [diff] [blame] | 139 | // If hostname remappings were specified on the command-line, layer these |
| 140 | // rules on top of the real host resolver. This allows forwarding all requests |
| 141 | // through a designated test server. |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 142 | if (!command_line.HasSwitch(switches::kHostResolverRules)) |
| 143 | return global_host_resolver; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 144 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 145 | net::MappedHostResolver* remapped_resolver = |
| 146 | new net::MappedHostResolver(global_host_resolver); |
| 147 | remapped_resolver->SetRulesFromString( |
| 148 | command_line.GetSwitchValueASCII(switches::kHostResolverRules)); |
| 149 | return remapped_resolver; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 58bc704 | 2010-07-07 18:04:14 | [diff] [blame] | 152 | class LoggingNetworkChangeObserver |
| 153 | : public net::NetworkChangeNotifier::Observer { |
| 154 | public: |
| 155 | // |net_log| must remain valid throughout our lifetime. |
| 156 | explicit LoggingNetworkChangeObserver(net::NetLog* net_log) |
| 157 | : net_log_(net_log) { |
| 158 | net::NetworkChangeNotifier::AddObserver(this); |
| 159 | } |
| 160 | |
| 161 | ~LoggingNetworkChangeObserver() { |
| 162 | net::NetworkChangeNotifier::RemoveObserver(this); |
| 163 | } |
| 164 | |
| 165 | virtual void OnIPAddressChanged() { |
[email protected] | 8e96e50 | 2010-10-21 20:57:12 | [diff] [blame] | 166 | VLOG(1) << "Observed a change to the network IP addresses"; |
[email protected] | 58bc704 | 2010-07-07 18:04:14 | [diff] [blame] | 167 | |
[email protected] | d3aaf3f | 2010-09-02 20:45:55 | [diff] [blame] | 168 | net_log_->AddEntry(net::NetLog::TYPE_NETWORK_IP_ADDRESSES_CHANGED, |
[email protected] | 58bc704 | 2010-07-07 18:04:14 | [diff] [blame] | 169 | base::TimeTicks::Now(), |
[email protected] | d2cbfbe | 2010-08-12 17:38:16 | [diff] [blame] | 170 | net::NetLog::Source(), |
[email protected] | 58bc704 | 2010-07-07 18:04:14 | [diff] [blame] | 171 | net::NetLog::PHASE_NONE, |
| 172 | NULL); |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | net::NetLog* net_log_; |
| 177 | DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver); |
| 178 | }; |
| 179 | |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 180 | scoped_refptr<URLRequestContext> |
| 181 | ConstructProxyScriptFetcherContext(IOThread::Globals* globals, |
| 182 | net::NetLog* net_log) { |
| 183 | scoped_refptr<URLRequestContext> context(new URLRequestContext); |
| 184 | context->set_net_log(net_log); |
| 185 | context->set_host_resolver(globals->host_resolver.get()); |
| 186 | context->set_cert_verifier(globals->cert_verifier.get()); |
| 187 | context->set_dnsrr_resolver(globals->dnsrr_resolver.get()); |
| 188 | context->set_http_auth_handler_factory( |
| 189 | globals->http_auth_handler_factory.get()); |
| 190 | context->set_proxy_service(globals->proxy_script_fetcher_proxy_service.get()); |
| 191 | context->set_http_transaction_factory( |
[email protected] | 52617df | 2010-12-24 07:30:01 | [diff] [blame^] | 192 | globals->proxy_script_fetcher_http_transaction_factory.get()); |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 193 | // In-memory cookie store. |
| 194 | context->set_cookie_store(new net::CookieMonster(NULL, NULL)); |
| 195 | return context; |
| 196 | } |
| 197 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 198 | } // namespace |
| 199 | |
| 200 | // The IOThread object must outlive any tasks posted to the IO thread before the |
| 201 | // Quit task. |
[email protected] | c56428f2 | 2010-06-16 02:17:23 | [diff] [blame] | 202 | DISABLE_RUNNABLE_METHOD_REFCOUNT(IOThread); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 203 | |
[email protected] | 1889dc1b | 2010-10-14 22:03:13 | [diff] [blame] | 204 | IOThread::Globals::Globals() {} |
| 205 | |
| 206 | IOThread::Globals::~Globals() {} |
| 207 | |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 208 | // |local_state| is passed in explicitly in order to (1) reduce implicit |
| 209 | // dependencies and (2) make IOThread more flexible for testing. |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 210 | IOThread::IOThread(PrefService* local_state, ChromeNetLog* net_log) |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 211 | : BrowserProcessSubThread(BrowserThread::IO), |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 212 | net_log_(net_log), |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 213 | globals_(NULL), |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 214 | speculative_interceptor_(NULL), |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 215 | predictor_(NULL) { |
| 216 | // We call RegisterPrefs() here (instead of inside browser_prefs.cc) to make |
| 217 | // sure that everything is initialized in the right order. |
| 218 | RegisterPrefs(local_state); |
| 219 | auth_schemes_ = local_state->GetString(prefs::kAuthSchemes); |
| 220 | negotiate_disable_cname_lookup_ = local_state->GetBoolean( |
| 221 | prefs::kDisableAuthNegotiateCnameLookup); |
| 222 | negotiate_enable_port_ = local_state->GetBoolean( |
| 223 | prefs::kEnableAuthNegotiatePort); |
| 224 | auth_server_whitelist_ = local_state->GetString(prefs::kAuthServerWhitelist); |
| 225 | auth_delegate_whitelist_ = local_state->GetString( |
| 226 | prefs::kAuthNegotiateDelegateWhitelist); |
[email protected] | ac7f3fdb | 2010-11-12 12:47:05 | [diff] [blame] | 227 | gssapi_library_name_ = local_state->GetString(prefs::kGSSAPILibraryName); |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 228 | } |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 229 | |
| 230 | IOThread::~IOThread() { |
| 231 | // We cannot rely on our base class to stop the thread since we want our |
| 232 | // CleanUp function to run. |
| 233 | Stop(); |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 234 | DCHECK(!globals_); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 235 | } |
| 236 | |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 237 | IOThread::Globals* IOThread::globals() { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 238 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 239 | return globals_; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 242 | ChromeNetLog* IOThread::net_log() { |
| 243 | return net_log_; |
| 244 | } |
| 245 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 246 | void IOThread::InitNetworkPredictor( |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 247 | bool prefetching_enabled, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 248 | base::TimeDelta max_dns_queue_delay, |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 249 | size_t max_speculative_parallel_resolves, |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 250 | const chrome_common_net::UrlList& startup_urls, |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 251 | ListValue* referral_list, |
| 252 | bool preconnect_enabled) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 253 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 254 | message_loop()->PostTask( |
| 255 | FROM_HERE, |
| 256 | NewRunnableMethod( |
| 257 | this, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 258 | &IOThread::InitNetworkPredictorOnIOThread, |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 259 | prefetching_enabled, max_dns_queue_delay, |
| 260 | max_speculative_parallel_resolves, |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 261 | startup_urls, referral_list, preconnect_enabled)); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 264 | void IOThread::RegisterURLRequestContextGetter( |
| 265 | ChromeURLRequestContextGetter* url_request_context_getter) { |
| 266 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 267 | std::list<ChromeURLRequestContextGetter*>::const_iterator it = |
| 268 | std::find(url_request_context_getters_.begin(), |
| 269 | url_request_context_getters_.end(), |
| 270 | url_request_context_getter); |
| 271 | DCHECK(it == url_request_context_getters_.end()); |
| 272 | url_request_context_getters_.push_back(url_request_context_getter); |
| 273 | } |
| 274 | |
| 275 | void IOThread::UnregisterURLRequestContextGetter( |
| 276 | ChromeURLRequestContextGetter* url_request_context_getter) { |
| 277 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 278 | std::list<ChromeURLRequestContextGetter*>::iterator it = |
| 279 | std::find(url_request_context_getters_.begin(), |
| 280 | url_request_context_getters_.end(), |
| 281 | url_request_context_getter); |
| 282 | DCHECK(it != url_request_context_getters_.end()); |
| 283 | // This does not scale, but we shouldn't have many URLRequestContextGetters in |
| 284 | // the first place, so this should be fine. |
| 285 | url_request_context_getters_.erase(it); |
| 286 | } |
| 287 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 288 | void IOThread::ChangedToOnTheRecord() { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 289 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 290 | message_loop()->PostTask( |
| 291 | FROM_HERE, |
| 292 | NewRunnableMethod( |
| 293 | this, |
| 294 | &IOThread::ChangedToOnTheRecordOnIOThread)); |
| 295 | } |
| 296 | |
| 297 | void IOThread::Init() { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 298 | #if !defined(OS_CHROMEOS) |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 299 | // TODO(evan): test and enable this on all platforms. |
| 300 | // Though this thread is called the "IO" thread, it actually just routes |
| 301 | // messages around; it shouldn't be allowed to perform any blocking disk I/O. |
| 302 | base::ThreadRestrictions::SetIOAllowed(false); |
| 303 | #endif |
| 304 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 305 | BrowserProcessSubThread::Init(); |
| 306 | |
[email protected] | 70b9234 | 2010-10-12 05:54:06 | [diff] [blame] | 307 | DCHECK_EQ(MessageLoop::TYPE_IO, message_loop()->type()); |
| 308 | |
| 309 | #if defined(USE_NSS) |
| 310 | net::SetMessageLoopForOCSP(); |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 311 | #endif // defined(USE_NSS) |
[email protected] | 70b9234 | 2010-10-12 05:54:06 | [diff] [blame] | 312 | |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 313 | DCHECK(!globals_); |
| 314 | globals_ = new Globals; |
| 315 | |
[email protected] | 58bc704 | 2010-07-07 18:04:14 | [diff] [blame] | 316 | // Add an observer that will emit network change events to the ChromeNetLog. |
| 317 | // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be |
| 318 | // logging the network change before other IO thread consumers respond to it. |
| 319 | network_change_observer_.reset( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 320 | new LoggingNetworkChangeObserver(net_log_)); |
[email protected] | 58bc704 | 2010-07-07 18:04:14 | [diff] [blame] | 321 | |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 322 | globals_->client_socket_factory = |
| 323 | net::ClientSocketFactory::GetDefaultFactory(); |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 324 | globals_->host_resolver.reset( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 325 | CreateGlobalHostResolver(net_log_)); |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 326 | globals_->cert_verifier.reset(new net::CertVerifier); |
[email protected] | 2db58053 | 2010-10-08 14:32:37 | [diff] [blame] | 327 | globals_->dnsrr_resolver.reset(new net::DnsRRResolver); |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 328 | // TODO(willchan): Use the real SSLConfigService. |
| 329 | globals_->ssl_config_service = |
| 330 | net::SSLConfigService::CreateSystemSSLConfigService(); |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 331 | globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 332 | globals_->host_resolver.get())); |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 333 | // For the ProxyScriptFetcher, we use a direct ProxyService. |
| 334 | globals_->proxy_script_fetcher_proxy_service = |
| 335 | net::ProxyService::CreateDirectWithNetLog(net_log_); |
[email protected] | 52617df | 2010-12-24 07:30:01 | [diff] [blame^] | 336 | globals_->proxy_script_fetcher_http_transaction_factory.reset( |
| 337 | new net::HttpNetworkLayer( |
| 338 | globals_->client_socket_factory, |
| 339 | globals_->host_resolver.get(), |
| 340 | globals_->cert_verifier.get(), |
| 341 | globals_->dnsrr_resolver.get(), |
| 342 | NULL /* dns_cert_checker */, |
| 343 | NULL /* ssl_host_info_factory */, |
| 344 | globals_->proxy_script_fetcher_proxy_service.get(), |
| 345 | globals_->ssl_config_service.get(), |
| 346 | new net::SpdySessionPool(globals_->ssl_config_service.get()), |
| 347 | globals_->http_auth_handler_factory.get(), |
| 348 | &globals_->network_delegate, |
| 349 | net_log_)); |
[email protected] | 2fb62920 | 2010-12-23 23:52:57 | [diff] [blame] | 350 | |
| 351 | scoped_refptr<URLRequestContext> proxy_script_fetcher_context = |
| 352 | ConstructProxyScriptFetcherContext(globals_, net_log_); |
| 353 | globals_->proxy_script_fetcher_context = proxy_script_fetcher_context; |
[email protected] | 1d14ba55 | 2010-11-19 19:07:40 | [diff] [blame] | 354 | |
| 355 | if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 356 | switches::kEnablePagePrerender)) { |
[email protected] | d077249e | 2010-12-09 02:44:58 | [diff] [blame] | 357 | prerender_interceptor_.reset(new PrerenderInterceptor()); |
[email protected] | 1d14ba55 | 2010-11-19 19:07:40 | [diff] [blame] | 358 | } |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | 2a92cd9 | 2010-04-27 00:01:41 | [diff] [blame] | 361 | void IOThread::CleanUp() { |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 362 | // Step 1: Kill all things that might be holding onto |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 363 | // net::URLRequest/URLRequestContexts. |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 364 | |
[email protected] | 59a3b36 | 2010-10-21 21:52:41 | [diff] [blame] | 365 | #if defined(USE_NSS) |
| 366 | net::ShutdownOCSP(); |
| 367 | #endif // defined(USE_NSS) |
| 368 | |
[email protected] | 0b1ad31 | 2010-10-22 01:01:38 | [diff] [blame] | 369 | // Destroy all URLRequests started by URLFetchers. |
| 370 | URLFetcher::CancelAll(); |
| 371 | |
[email protected] | 325a71f | 2010-05-21 23:05:27 | [diff] [blame] | 372 | // If any child processes are still running, terminate them and |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 373 | // and delete the BrowserChildProcessHost instances to release whatever |
[email protected] | 325a71f | 2010-05-21 23:05:27 | [diff] [blame] | 374 | // IO thread only resources they are referencing. |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 375 | BrowserChildProcessHost::TerminateAll(); |
[email protected] | 325a71f | 2010-05-21 23:05:27 | [diff] [blame] | 376 | |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 377 | std::list<ChromeURLRequestContextGetter*> url_request_context_getters; |
| 378 | url_request_context_getters.swap(url_request_context_getters_); |
| 379 | for (std::list<ChromeURLRequestContextGetter*>::iterator it = |
| 380 | url_request_context_getters.begin(); |
| 381 | it != url_request_context_getters.end(); ++it) { |
| 382 | ChromeURLRequestContextGetter* getter = *it; |
[email protected] | 361fe8a | 2010-11-24 17:20:49 | [diff] [blame] | 383 | // Stop all pending certificate provenance check uploads |
| 384 | net::DnsCertProvenanceChecker* checker = |
| 385 | getter->GetURLRequestContext()->dns_cert_checker(); |
| 386 | if (checker) |
| 387 | checker->Shutdown(); |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 388 | getter->ReleaseURLRequestContext(); |
| 389 | } |
| 390 | |
| 391 | // Step 2: Release objects that the URLRequestContext could have been pointing |
| 392 | // to. |
| 393 | |
| 394 | // This must be reset before the ChromeNetLog is destroyed. |
| 395 | network_change_observer_.reset(); |
| 396 | |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 397 | // Not initialized in Init(). May not be initialized. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 398 | if (predictor_) { |
| 399 | predictor_->Shutdown(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 400 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 401 | // TODO(willchan): Stop reference counting Predictor. It's owned by |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 402 | // IOThread now. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 403 | predictor_->Release(); |
| 404 | predictor_ = NULL; |
| 405 | chrome_browser_net::FreePredictorResources(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 408 | // Deletion will unregister this interceptor. |
| 409 | delete speculative_interceptor_; |
| 410 | speculative_interceptor_ = NULL; |
| 411 | |
[email protected] | 1d14ba55 | 2010-11-19 19:07:40 | [diff] [blame] | 412 | prerender_interceptor_.reset(); |
| 413 | |
[email protected] | e4d2dd82 | 2010-02-05 20:57:33 | [diff] [blame] | 414 | // TODO(eroman): hack for http://crbug.com/15513 |
[email protected] | 970210c | 2010-02-19 20:27:02 | [diff] [blame] | 415 | if (globals_->host_resolver->GetAsHostResolverImpl()) { |
| 416 | globals_->host_resolver.get()->GetAsHostResolverImpl()->Shutdown(); |
[email protected] | e4d2dd82 | 2010-02-05 20:57:33 | [diff] [blame] | 417 | } |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 418 | |
[email protected] | d13c327 | 2010-02-04 00:24:51 | [diff] [blame] | 419 | delete globals_; |
| 420 | globals_ = NULL; |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 421 | |
[email protected] | 2a92cd9 | 2010-04-27 00:01:41 | [diff] [blame] | 422 | BrowserProcessSubThread::CleanUp(); |
| 423 | } |
| 424 | |
| 425 | void IOThread::CleanUpAfterMessageLoopDestruction() { |
[email protected] | 0ee7a3b | 2010-11-09 06:13:40 | [diff] [blame] | 426 | // This will delete the |notification_service_|. Make sure it's done after |
| 427 | // anything else can reference it. |
[email protected] | 9aa33e8 | 2010-04-15 00:15:39 | [diff] [blame] | 428 | BrowserProcessSubThread::CleanUpAfterMessageLoopDestruction(); |
[email protected] | 4411da94 | 2010-10-25 15:06:02 | [diff] [blame] | 429 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 430 | // net::URLRequest instances must NOT outlive the IO thread. |
[email protected] | 4411da94 | 2010-10-25 15:06:02 | [diff] [blame] | 431 | // |
| 432 | // To allow for URLRequests to be deleted from |
| 433 | // MessageLoop::DestructionObserver this check has to happen after CleanUp |
| 434 | // (which runs before DestructionObservers). |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 435 | base::debug::LeakTracker<net::URLRequest>::CheckForLeaks(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 438 | // static |
| 439 | void IOThread::RegisterPrefs(PrefService* local_state) { |
| 440 | local_state->RegisterStringPref(prefs::kAuthSchemes, |
| 441 | "basic,digest,ntlm,negotiate"); |
| 442 | local_state->RegisterBooleanPref(prefs::kDisableAuthNegotiateCnameLookup, |
| 443 | false); |
| 444 | local_state->RegisterBooleanPref(prefs::kEnableAuthNegotiatePort, false); |
| 445 | local_state->RegisterStringPref(prefs::kAuthServerWhitelist, ""); |
| 446 | local_state->RegisterStringPref(prefs::kAuthNegotiateDelegateWhitelist, ""); |
[email protected] | ac7f3fdb | 2010-11-12 12:47:05 | [diff] [blame] | 447 | local_state->RegisterStringPref(prefs::kGSSAPILibraryName, ""); |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 450 | net::HttpAuthHandlerFactory* IOThread::CreateDefaultAuthHandlerFactory( |
| 451 | net::HostResolver* resolver) { |
[email protected] | 9030a63 | 2010-11-19 20:12:09 | [diff] [blame] | 452 | net::HttpAuthFilterWhitelist* auth_filter_default_credentials = NULL; |
| 453 | if (!auth_server_whitelist_.empty()) { |
| 454 | auth_filter_default_credentials = |
| 455 | new net::HttpAuthFilterWhitelist(auth_server_whitelist_); |
| 456 | } |
| 457 | net::HttpAuthFilterWhitelist* auth_filter_delegate = NULL; |
| 458 | if (!auth_delegate_whitelist_.empty()) { |
| 459 | auth_filter_delegate = |
| 460 | new net::HttpAuthFilterWhitelist(auth_delegate_whitelist_); |
| 461 | } |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 462 | globals_->url_security_manager.reset( |
[email protected] | d201b200e | 2010-08-27 17:35:02 | [diff] [blame] | 463 | net::URLSecurityManager::Create(auth_filter_default_credentials, |
| 464 | auth_filter_delegate)); |
[email protected] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 465 | std::vector<std::string> supported_schemes; |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 466 | base::SplitString(auth_schemes_, ',', &supported_schemes); |
[email protected] | b730416 | 2010-08-23 17:42:29 | [diff] [blame] | 467 | |
| 468 | return net::HttpAuthHandlerRegistryFactory::Create( |
| 469 | supported_schemes, |
| 470 | globals_->url_security_manager.get(), |
| 471 | resolver, |
[email protected] | ac7f3fdb | 2010-11-12 12:47:05 | [diff] [blame] | 472 | gssapi_library_name_, |
[email protected] | bcefe0f | 2010-11-10 16:19:10 | [diff] [blame] | 473 | negotiate_disable_cname_lookup_, |
| 474 | negotiate_enable_port_); |
[email protected] | eb3cac7 | 2010-02-26 21:07:45 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 477 | void IOThread::InitNetworkPredictorOnIOThread( |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 478 | bool prefetching_enabled, |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 479 | base::TimeDelta max_dns_queue_delay, |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 480 | size_t max_speculative_parallel_resolves, |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 481 | const chrome_common_net::UrlList& startup_urls, |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 482 | ListValue* referral_list, |
| 483 | bool preconnect_enabled) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 484 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 485 | CHECK(!predictor_); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 486 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 487 | chrome_browser_net::EnablePredictor(prefetching_enabled); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 488 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 489 | predictor_ = new chrome_browser_net::Predictor( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 490 | globals_->host_resolver.get(), |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 491 | max_dns_queue_delay, |
[email protected] | 755a9335 | 2010-10-29 06:33:59 | [diff] [blame] | 492 | max_speculative_parallel_resolves, |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 493 | preconnect_enabled); |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 494 | predictor_->AddRef(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 495 | |
[email protected] | f4ef861ba | 2010-07-28 22:37:23 | [diff] [blame] | 496 | // Speculative_interceptor_ is used to predict subresource usage. |
| 497 | DCHECK(!speculative_interceptor_); |
| 498 | speculative_interceptor_ = new chrome_browser_net::ConnectInterceptor; |
| 499 | |
[email protected] | bff1f51 | 2010-08-15 15:13:49 | [diff] [blame] | 500 | FinalizePredictorInitialization(predictor_, startup_urls, referral_list); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | void IOThread::ChangedToOnTheRecordOnIOThread() { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 504 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 505 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 506 | if (predictor_) { |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 507 | // Destroy all evidence of our OTR session. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 508 | predictor_->Predictor::DiscardAllResults(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | // Clear the host cache to avoid showing entries from the OTR session |
| 512 | // in about:net-internals. |
[email protected] | 970210c | 2010-02-19 20:27:02 | [diff] [blame] | 513 | if (globals_->host_resolver->GetAsHostResolverImpl()) { |
| 514 | net::HostCache* host_cache = |
| 515 | globals_->host_resolver.get()->GetAsHostResolverImpl()->cache(); |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 516 | if (host_cache) |
| 517 | host_cache->clear(); |
| 518 | } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 519 | // Clear all of the passively logged data. |
| 520 | // TODO(eroman): this is a bit heavy handed, really all we need to do is |
| 521 | // clear the data pertaining to off the record context. |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 522 | net_log_->ClearAllPassivelyCapturedEvents(); |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 523 | } |