OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/io_thread.h" | 5 #include "chrome/browser/io_thread.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/leak_tracker.h" | 10 #include "base/debug/leak_tracker.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
31 #include "net/base/cert_verifier.h" | 31 #include "net/base/cert_verifier.h" |
32 #include "net/base/dnsrr_resolver.h" | 32 #include "net/base/dnsrr_resolver.h" |
33 #include "net/base/host_cache.h" | 33 #include "net/base/host_cache.h" |
34 #include "net/base/host_resolver.h" | 34 #include "net/base/host_resolver.h" |
35 #include "net/base/host_resolver_impl.h" | 35 #include "net/base/host_resolver_impl.h" |
36 #include "net/base/mapped_host_resolver.h" | 36 #include "net/base/mapped_host_resolver.h" |
37 #include "net/base/net_util.h" | 37 #include "net/base/net_util.h" |
38 #include "net/http/http_auth_filter.h" | 38 #include "net/http/http_auth_filter.h" |
39 #include "net/http/http_auth_handler_factory.h" | 39 #include "net/http/http_auth_handler_factory.h" |
| 40 #include "net/http/http_network_layer.h" |
40 #if defined(USE_NSS) | 41 #if defined(USE_NSS) |
41 #include "net/ocsp/nss_ocsp.h" | 42 #include "net/ocsp/nss_ocsp.h" |
42 #endif // defined(USE_NSS) | 43 #endif // defined(USE_NSS) |
43 #include "net/proxy/proxy_script_fetcher_impl.h" | 44 #include "net/proxy/proxy_script_fetcher_impl.h" |
| 45 #include "net/socket/client_socket_factory.h" |
| 46 #include "net/spdy/spdy_session_pool.h" |
44 | 47 |
45 namespace { | 48 namespace { |
46 | 49 |
47 net::HostResolver* CreateGlobalHostResolver(net::NetLog* net_log) { | 50 net::HostResolver* CreateGlobalHostResolver(net::NetLog* net_log) { |
48 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 51 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
49 | 52 |
50 size_t parallelism = net::HostResolver::kDefaultParallelism; | 53 size_t parallelism = net::HostResolver::kDefaultParallelism; |
51 | 54 |
52 // Use the concurrency override from the command-line, if any. | 55 // Use the concurrency override from the command-line, if any. |
53 if (command_line.HasSwitch(switches::kHostResolverParallelism)) { | 56 if (command_line.HasSwitch(switches::kHostResolverParallelism)) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 net::NetLog::Source(), | 170 net::NetLog::Source(), |
168 net::NetLog::PHASE_NONE, | 171 net::NetLog::PHASE_NONE, |
169 NULL); | 172 NULL); |
170 } | 173 } |
171 | 174 |
172 private: | 175 private: |
173 net::NetLog* net_log_; | 176 net::NetLog* net_log_; |
174 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver); | 177 DISALLOW_COPY_AND_ASSIGN(LoggingNetworkChangeObserver); |
175 }; | 178 }; |
176 | 179 |
| 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( |
| 192 new net::HttpNetworkLayer( |
| 193 globals->client_socket_factory, |
| 194 globals->host_resolver.get(), |
| 195 globals->cert_verifier.get(), |
| 196 globals->dnsrr_resolver.get(), |
| 197 NULL /* dns_cert_checker */, |
| 198 NULL /* ssl_host_info_factory */, |
| 199 globals->proxy_script_fetcher_proxy_service.get(), |
| 200 globals->ssl_config_service.get(), |
| 201 new net::SpdySessionPool(globals->ssl_config_service.get()), |
| 202 globals->http_auth_handler_factory.get(), |
| 203 &globals->network_delegate, |
| 204 net_log)); |
| 205 // In-memory cookie store. |
| 206 context->set_cookie_store(new net::CookieMonster(NULL, NULL)); |
| 207 return context; |
| 208 } |
| 209 |
177 } // namespace | 210 } // namespace |
178 | 211 |
179 // This is a wrapper class around ProxyScriptFetcherImpl that will | |
180 // keep track of live instances. | |
181 class IOThread::ManagedProxyScriptFetcher | |
182 : public net::ProxyScriptFetcherImpl { | |
183 public: | |
184 ManagedProxyScriptFetcher(URLRequestContext* context, | |
185 IOThread* io_thread) | |
186 : net::ProxyScriptFetcherImpl(context), | |
187 io_thread_(io_thread) { | |
188 DCHECK(!ContainsKey(*fetchers(), this)); | |
189 fetchers()->insert(this); | |
190 } | |
191 | |
192 virtual ~ManagedProxyScriptFetcher() { | |
193 DCHECK(ContainsKey(*fetchers(), this)); | |
194 fetchers()->erase(this); | |
195 } | |
196 | |
197 private: | |
198 ProxyScriptFetchers* fetchers() { | |
199 return &io_thread_->fetchers_; | |
200 } | |
201 | |
202 IOThread* io_thread_; | |
203 | |
204 DISALLOW_COPY_AND_ASSIGN(ManagedProxyScriptFetcher); | |
205 }; | |
206 | |
207 // The IOThread object must outlive any tasks posted to the IO thread before the | 212 // The IOThread object must outlive any tasks posted to the IO thread before the |
208 // Quit task. | 213 // Quit task. |
209 DISABLE_RUNNABLE_METHOD_REFCOUNT(IOThread); | 214 DISABLE_RUNNABLE_METHOD_REFCOUNT(IOThread); |
210 | 215 |
211 IOThread::Globals::Globals() {} | 216 IOThread::Globals::Globals() {} |
212 | 217 |
213 IOThread::Globals::~Globals() {} | 218 IOThread::Globals::~Globals() {} |
214 | 219 |
215 // |local_state| is passed in explicitly in order to (1) reduce implicit | 220 // |local_state| is passed in explicitly in order to (1) reduce implicit |
216 // dependencies and (2) make IOThread more flexible for testing. | 221 // dependencies and (2) make IOThread more flexible for testing. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 | 299 |
295 void IOThread::ChangedToOnTheRecord() { | 300 void IOThread::ChangedToOnTheRecord() { |
296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
297 message_loop()->PostTask( | 302 message_loop()->PostTask( |
298 FROM_HERE, | 303 FROM_HERE, |
299 NewRunnableMethod( | 304 NewRunnableMethod( |
300 this, | 305 this, |
301 &IOThread::ChangedToOnTheRecordOnIOThread)); | 306 &IOThread::ChangedToOnTheRecordOnIOThread)); |
302 } | 307 } |
303 | 308 |
304 net::ProxyScriptFetcher* IOThread::CreateAndRegisterProxyScriptFetcher( | |
305 URLRequestContext* url_request_context) { | |
306 return new ManagedProxyScriptFetcher(url_request_context, this); | |
307 } | |
308 | |
309 void IOThread::Init() { | 309 void IOThread::Init() { |
310 #if !defined(OS_CHROMEOS) | 310 #if !defined(OS_CHROMEOS) |
311 // TODO(evan): test and enable this on all platforms. | 311 // TODO(evan): test and enable this on all platforms. |
312 // Though this thread is called the "IO" thread, it actually just routes | 312 // Though this thread is called the "IO" thread, it actually just routes |
313 // messages around; it shouldn't be allowed to perform any blocking disk I/O. | 313 // messages around; it shouldn't be allowed to perform any blocking disk I/O. |
314 base::ThreadRestrictions::SetIOAllowed(false); | 314 base::ThreadRestrictions::SetIOAllowed(false); |
315 #endif | 315 #endif |
316 | 316 |
317 BrowserProcessSubThread::Init(); | 317 BrowserProcessSubThread::Init(); |
318 | 318 |
319 DCHECK_EQ(MessageLoop::TYPE_IO, message_loop()->type()); | 319 DCHECK_EQ(MessageLoop::TYPE_IO, message_loop()->type()); |
320 | 320 |
321 #if defined(USE_NSS) | 321 #if defined(USE_NSS) |
322 net::SetMessageLoopForOCSP(); | 322 net::SetMessageLoopForOCSP(); |
323 #endif // defined(USE_NSS) | 323 #endif // defined(USE_NSS) |
324 | 324 |
325 DCHECK(!globals_); | 325 DCHECK(!globals_); |
326 globals_ = new Globals; | 326 globals_ = new Globals; |
327 | 327 |
328 // Add an observer that will emit network change events to the ChromeNetLog. | 328 // Add an observer that will emit network change events to the ChromeNetLog. |
329 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be | 329 // Assuming NetworkChangeNotifier dispatches in FIFO order, we should be |
330 // logging the network change before other IO thread consumers respond to it. | 330 // logging the network change before other IO thread consumers respond to it. |
331 network_change_observer_.reset( | 331 network_change_observer_.reset( |
332 new LoggingNetworkChangeObserver(net_log_)); | 332 new LoggingNetworkChangeObserver(net_log_)); |
333 | 333 |
| 334 globals_->client_socket_factory = |
| 335 net::ClientSocketFactory::GetDefaultFactory(); |
334 globals_->host_resolver.reset( | 336 globals_->host_resolver.reset( |
335 CreateGlobalHostResolver(net_log_)); | 337 CreateGlobalHostResolver(net_log_)); |
336 globals_->cert_verifier.reset(new net::CertVerifier); | 338 globals_->cert_verifier.reset(new net::CertVerifier); |
337 globals_->dnsrr_resolver.reset(new net::DnsRRResolver); | 339 globals_->dnsrr_resolver.reset(new net::DnsRRResolver); |
| 340 // TODO(willchan): Use the real SSLConfigService. |
| 341 globals_->ssl_config_service = |
| 342 net::SSLConfigService::CreateSystemSSLConfigService(); |
338 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory( | 343 globals_->http_auth_handler_factory.reset(CreateDefaultAuthHandlerFactory( |
339 globals_->host_resolver.get())); | 344 globals_->host_resolver.get())); |
| 345 // For the ProxyScriptFetcher, we use a direct ProxyService. |
| 346 globals_->proxy_script_fetcher_proxy_service = |
| 347 net::ProxyService::CreateDirectWithNetLog(net_log_); |
| 348 |
| 349 scoped_refptr<URLRequestContext> proxy_script_fetcher_context = |
| 350 ConstructProxyScriptFetcherContext(globals_, net_log_); |
| 351 globals_->proxy_script_fetcher_context = proxy_script_fetcher_context; |
340 | 352 |
341 if (CommandLine::ForCurrentProcess()->HasSwitch( | 353 if (CommandLine::ForCurrentProcess()->HasSwitch( |
342 switches::kEnablePagePrerender)) { | 354 switches::kEnablePagePrerender)) { |
343 prerender_interceptor_.reset(new PrerenderInterceptor()); | 355 prerender_interceptor_.reset(new PrerenderInterceptor()); |
344 } | 356 } |
345 } | 357 } |
346 | 358 |
347 void IOThread::CleanUp() { | 359 void IOThread::CleanUp() { |
348 // Step 1: Kill all things that might be holding onto | 360 // Step 1: Kill all things that might be holding onto |
349 // net::URLRequest/URLRequestContexts. | 361 // net::URLRequest/URLRequestContexts. |
350 | 362 |
351 #if defined(USE_NSS) | 363 #if defined(USE_NSS) |
352 net::ShutdownOCSP(); | 364 net::ShutdownOCSP(); |
353 #endif // defined(USE_NSS) | 365 #endif // defined(USE_NSS) |
354 | 366 |
355 // Destroy all URLRequests started by URLFetchers. | 367 // Destroy all URLRequests started by URLFetchers. |
356 URLFetcher::CancelAll(); | 368 URLFetcher::CancelAll(); |
357 | 369 |
358 // Break any cycles between the ProxyScriptFetcher and URLRequestContext. | |
359 for (ProxyScriptFetchers::const_iterator it = fetchers_.begin(); | |
360 it != fetchers_.end();) { | |
361 ManagedProxyScriptFetcher* fetcher = *it; | |
362 { | |
363 // Hang on to the context while cancelling to avoid problems | |
364 // with the cancellation causing the context to be destroyed | |
365 // (see http://crbug.com/63796 ). Ideally, the IOThread would | |
366 // own the URLRequestContexts. | |
367 scoped_refptr<URLRequestContext> context(fetcher->GetRequestContext()); | |
368 fetcher->Cancel(); | |
369 } | |
370 // Any number of fetchers may have been deleted at this point, so | |
371 // use upper_bound instead of a simple increment. | |
372 it = fetchers_.upper_bound(fetcher); | |
373 } | |
374 | |
375 // If any child processes are still running, terminate them and | 370 // If any child processes are still running, terminate them and |
376 // and delete the BrowserChildProcessHost instances to release whatever | 371 // and delete the BrowserChildProcessHost instances to release whatever |
377 // IO thread only resources they are referencing. | 372 // IO thread only resources they are referencing. |
378 BrowserChildProcessHost::TerminateAll(); | 373 BrowserChildProcessHost::TerminateAll(); |
379 | 374 |
380 std::list<ChromeURLRequestContextGetter*> url_request_context_getters; | 375 std::list<ChromeURLRequestContextGetter*> url_request_context_getters; |
381 url_request_context_getters.swap(url_request_context_getters_); | 376 url_request_context_getters.swap(url_request_context_getters_); |
382 for (std::list<ChromeURLRequestContextGetter*>::iterator it = | 377 for (std::list<ChromeURLRequestContextGetter*>::iterator it = |
383 url_request_context_getters.begin(); | 378 url_request_context_getters.begin(); |
384 it != url_request_context_getters.end(); ++it) { | 379 it != url_request_context_getters.end(); ++it) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 net::HostCache* host_cache = | 512 net::HostCache* host_cache = |
518 globals_->host_resolver.get()->GetAsHostResolverImpl()->cache(); | 513 globals_->host_resolver.get()->GetAsHostResolverImpl()->cache(); |
519 if (host_cache) | 514 if (host_cache) |
520 host_cache->clear(); | 515 host_cache->clear(); |
521 } | 516 } |
522 // Clear all of the passively logged data. | 517 // Clear all of the passively logged data. |
523 // TODO(eroman): this is a bit heavy handed, really all we need to do is | 518 // TODO(eroman): this is a bit heavy handed, really all we need to do is |
524 // clear the data pertaining to off the record context. | 519 // clear the data pertaining to off the record context. |
525 net_log_->ClearAllPassivelyCapturedEvents(); | 520 net_log_->ClearAllPassivelyCapturedEvents(); |
526 } | 521 } |
OLD | NEW |