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