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