io_thread.h revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
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#ifndef CHROME_BROWSER_IO_THREAD_H_
6#define CHROME_BROWSER_IO_THREAD_H_
7#pragma once
8
9#include <list>
10#include <set>
11#include <string>
12#include "base/basictypes.h"
13#include "base/ref_counted.h"
14#include "base/scoped_ptr.h"
15#include "chrome/browser/browser_process_sub_thread.h"
16#include "chrome/browser/net/chrome_network_delegate.h"
17#include "chrome/common/net/predictor_common.h"
18#include "net/base/network_change_notifier.h"
19
20class ChromeNetLog;
21class ChromeURLRequestContextGetter;
22class ListValue;
23class PrefService;
24class URLRequestContext;
25
26namespace chrome_browser_net {
27class ConnectInterceptor;
28class Predictor;
29class PrerenderInterceptor;
30}  // namespace chrome_browser_net
31
32namespace net {
33class DnsRRResolver;
34class HostResolver;
35class HttpAuthHandlerFactory;
36class ProxyScriptFetcher;
37class URLSecurityManager;
38}  // namespace net
39
40class IOThread : public BrowserProcessSubThread {
41 public:
42  struct Globals {
43    Globals();
44    ~Globals();
45
46    scoped_ptr<ChromeNetLog> net_log;
47    scoped_ptr<net::HostResolver> host_resolver;
48    scoped_ptr<net::DnsRRResolver> dnsrr_resolver;
49    scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory;
50    scoped_ptr<net::URLSecurityManager> url_security_manager;
51    ChromeNetworkDelegate network_delegate;
52  };
53
54  explicit IOThread(PrefService* local_state);
55
56  virtual ~IOThread();
57
58  // Can only be called on the IO thread.
59  Globals* globals();
60
61  // Initializes the network predictor, which induces DNS pre-resolution and/or
62  // TCP/IP preconnections.  |prefetching_enabled| indicates whether or not DNS
63  // prefetching should be enabled, and |preconnect_enabled| controls whether
64  // TCP/IP preconnection is enabled.  This should be called by the UI thread.
65  // It will post a task to the IO thread to perform the actual initialization.
66  void InitNetworkPredictor(bool prefetching_enabled,
67                            base::TimeDelta max_dns_queue_delay,
68                            size_t max_speculative_parallel_resolves,
69                            const chrome_common_net::UrlList& startup_urls,
70                            ListValue* referral_list,
71                            bool preconnect_enabled);
72
73  // Registers |url_request_context_getter| into the IO thread.  During
74  // IOThread::CleanUp(), IOThread will iterate through known getters and
75  // release their URLRequestContexts.  Only called on the IO thread.  It does
76  // not acquire a refcount for |url_request_context_getter|.  If
77  // |url_request_context_getter| is being deleted before IOThread::CleanUp() is
78  // invoked, then this needs to be balanced with a call to
79  // UnregisterURLRequestContextGetter().
80  void RegisterURLRequestContextGetter(
81      ChromeURLRequestContextGetter* url_request_context_getter);
82
83  // Unregisters |url_request_context_getter| from the IO thread.  Only called
84  // on the IO thread.
85  void UnregisterURLRequestContextGetter(
86      ChromeURLRequestContextGetter* url_request_context_getter);
87
88  // Handles changing to On The Record mode.  Posts a task for this onto the
89  // IOThread's message loop.
90  void ChangedToOnTheRecord();
91
92  // Creates a ProxyScriptFetcherImpl which will be automatically aborted
93  // during shutdown.
94  // This is used to avoid cycles between the ProxyScriptFetcher and the
95  // URLRequestContext that owns it (indirectly via the ProxyService).
96  net::ProxyScriptFetcher* CreateAndRegisterProxyScriptFetcher(
97      URLRequestContext* url_request_context);
98
99 protected:
100  virtual void Init();
101  virtual void CleanUp();
102  virtual void CleanUpAfterMessageLoopDestruction();
103
104 private:
105  class ManagedProxyScriptFetcher;
106  typedef std::set<ManagedProxyScriptFetcher*> ProxyScriptFetchers;
107
108  static void RegisterPrefs(PrefService* local_state);
109
110  net::HttpAuthHandlerFactory* CreateDefaultAuthHandlerFactory(
111      net::HostResolver* resolver);
112
113  void InitNetworkPredictorOnIOThread(
114      bool prefetching_enabled,
115      base::TimeDelta max_dns_queue_delay,
116      size_t max_speculative_parallel_resolves,
117      const chrome_common_net::UrlList& startup_urls,
118      ListValue* referral_list,
119      bool preconnect_enabled);
120
121  void ChangedToOnTheRecordOnIOThread();
122
123  // These member variables are basically global, but their lifetimes are tied
124  // to the IOThread.  IOThread owns them all, despite not using scoped_ptr.
125  // This is because the destructor of IOThread runs on the wrong thread.  All
126  // member variables should be deleted in CleanUp(), except ChromeNetLog
127  // which is deleted later in CleanUpAfterMessageLoopDestruction().
128
129  // These member variables are initialized in Init() and do not change for the
130  // lifetime of the IO thread.
131
132  Globals* globals_;
133
134  // This variable is only meaningful during shutdown. It is used to defer
135  // deletion of the NetLog to CleanUpAfterMessageLoopDestruction() even
136  // though |globals_| is reset by CleanUp().
137  scoped_ptr<ChromeNetLog> deferred_net_log_to_delete_;
138
139  // Observer that logs network changes to the ChromeNetLog.
140  scoped_ptr<net::NetworkChangeNotifier::Observer> network_change_observer_;
141
142  // Store HTTP Auth-related policies in this thread.
143  std::string auth_schemes_;
144  bool negotiate_disable_cname_lookup_;
145  bool negotiate_enable_port_;
146  std::string auth_server_whitelist_;
147  std::string auth_delegate_whitelist_;
148  std::string gssapi_library_name_;
149
150  // These member variables are initialized by a task posted to the IO thread,
151  // which gets posted by calling certain member functions of IOThread.
152
153  // Note: we user explicit pointers rather than smart pointers to be more
154  // explicit about destruction order, and ensure that there is no chance that
155  // these observers would be used accidentally after we have begun to tear
156  // down.
157  chrome_browser_net::ConnectInterceptor* speculative_interceptor_;
158  chrome_browser_net::Predictor* predictor_;
159  scoped_ptr<chrome_browser_net::PrerenderInterceptor>
160    prerender_interceptor_;
161
162  // List of live ProxyScriptFetchers.
163  ProxyScriptFetchers fetchers_;
164
165  // Keeps track of all live ChromeURLRequestContextGetters, so the
166  // ChromeURLRequestContexts can be released during
167  // IOThread::CleanUpAfterMessageLoopDestruction().
168  std::list<ChromeURLRequestContextGetter*> url_request_context_getters_;
169
170  DISALLOW_COPY_AND_ASSIGN(IOThread);
171};
172
173#endif  // CHROME_BROWSER_IO_THREAD_H_
174