| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef IOS_CHROME_BROWSER_IOS_CHROME_IO_THREAD_H_ | 5 #ifndef IOS_CHROME_BROWSER_IOS_CHROME_IO_THREAD_H_ |
| 6 #define IOS_CHROME_BROWSER_IOS_CHROME_IO_THREAD_H_ | 6 #define IOS_CHROME_BROWSER_IOS_CHROME_IO_THREAD_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <memory> |
| 9 #include <stdint.h> | |
| 10 | 9 |
| 11 #include <map> | 10 #include "ios/components/io_thread/ios_io_thread.h" |
| 12 #include <memory> | |
| 13 #include <set> | |
| 14 #include <string> | |
| 15 #include <vector> | |
| 16 | 11 |
| 17 #include "base/compiler_specific.h" | |
| 18 #include "base/macros.h" | |
| 19 #include "base/memory/ref_counted.h" | |
| 20 #include "base/memory/weak_ptr.h" | |
| 21 #include "base/time/time.h" | |
| 22 #include "components/prefs/pref_member.h" | |
| 23 #include "components/ssl_config/ssl_config_service_manager.h" | |
| 24 #include "ios/web/public/web_thread_delegate.h" | |
| 25 #include "net/base/network_change_notifier.h" | |
| 26 #include "net/http/http_network_session.h" | |
| 27 | |
| 28 class PrefProxyConfigTracker; | |
| 29 class PrefService; | 12 class PrefService; |
| 30 class SystemURLRequestContextGetter; | |
| 31 | |
| 32 namespace net { | |
| 33 class CTPolicyEnforcer; | |
| 34 class CertVerifier; | |
| 35 class ChannelIDService; | |
| 36 class CookieStore; | |
| 37 class CTVerifier; | |
| 38 class HostResolver; | |
| 39 class HttpAuthHandlerFactory; | |
| 40 class HttpAuthPreferences; | |
| 41 class HttpServerProperties; | |
| 42 class HttpTransactionFactory; | |
| 43 class HttpUserAgentSettings; | |
| 44 class NetworkDelegate; | |
| 45 class NetworkQualityEstimator; | |
| 46 class ProxyConfigService; | |
| 47 class ProxyService; | |
| 48 class SSLConfigService; | |
| 49 class TransportSecurityState; | |
| 50 class URLRequestContext; | |
| 51 class URLRequestContextGetter; | |
| 52 class URLRequestJobFactory; | |
| 53 } // namespace net | |
| 54 | 13 |
| 55 namespace net_log { | 14 namespace net_log { |
| 56 class ChromeNetLog; | 15 class ChromeNetLog; |
| 57 } // namespace net_log | 16 } // namespace net_log |
| 58 | 17 |
| 59 class SystemURLRequestContextGetter; | |
| 60 | |
| 61 // Contains state associated with, initialized and cleaned up on, and | 18 // Contains state associated with, initialized and cleaned up on, and |
| 62 // primarily used on, the IO thread. | 19 // primarily used on, the IO thread. |
| 63 // | 20 class IOSChromeIOThread : public io_thread::IOSIOThread { |
| 64 // If you are looking to interact with the IO thread (e.g. post tasks | |
| 65 // to it or check if it is the current thread), see web::WebThread. | |
| 66 class IOSChromeIOThread : public web::WebThreadDelegate { | |
| 67 public: | 21 public: |
| 68 struct Globals { | |
| 69 template <typename T> | |
| 70 class Optional { | |
| 71 public: | |
| 72 Optional() : set_(false) {} | |
| 73 | |
| 74 void set(T value) { | |
| 75 set_ = true; | |
| 76 value_ = value; | |
| 77 } | |
| 78 void CopyToIfSet(T* value) const { | |
| 79 if (set_) { | |
| 80 *value = value_; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 bool set_; | |
| 86 T value_; | |
| 87 }; | |
| 88 | |
| 89 class SystemRequestContextLeakChecker { | |
| 90 public: | |
| 91 explicit SystemRequestContextLeakChecker(Globals* globals); | |
| 92 ~SystemRequestContextLeakChecker(); | |
| 93 | |
| 94 private: | |
| 95 Globals* const globals_; | |
| 96 }; | |
| 97 | |
| 98 Globals(); | |
| 99 ~Globals(); | |
| 100 | |
| 101 // The "system" NetworkDelegate, used for BrowserState-agnostic network | |
| 102 // events. | |
| 103 std::unique_ptr<net::NetworkDelegate> system_network_delegate; | |
| 104 std::unique_ptr<net::HostResolver> host_resolver; | |
| 105 std::unique_ptr<net::CertVerifier> cert_verifier; | |
| 106 // The ChannelIDService must outlive the HttpTransactionFactory. | |
| 107 std::unique_ptr<net::ChannelIDService> system_channel_id_service; | |
| 108 // This TransportSecurityState doesn't load or save any state. It's only | |
| 109 // used to enforce pinning for system requests and will only use built-in | |
| 110 // pins. | |
| 111 std::unique_ptr<net::TransportSecurityState> transport_security_state; | |
| 112 std::unique_ptr<net::CTVerifier> cert_transparency_verifier; | |
| 113 scoped_refptr<net::SSLConfigService> ssl_config_service; | |
| 114 std::unique_ptr<net::HttpAuthPreferences> http_auth_preferences; | |
| 115 std::unique_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory; | |
| 116 std::unique_ptr<net::HttpServerProperties> http_server_properties; | |
| 117 std::unique_ptr<net::ProxyService> system_proxy_service; | |
| 118 std::unique_ptr<net::HttpNetworkSession> system_http_network_session; | |
| 119 std::unique_ptr<net::HttpTransactionFactory> | |
| 120 system_http_transaction_factory; | |
| 121 std::unique_ptr<net::URLRequestJobFactory> system_url_request_job_factory; | |
| 122 std::unique_ptr<net::URLRequestContext> system_request_context; | |
| 123 SystemRequestContextLeakChecker system_request_context_leak_checker; | |
| 124 std::unique_ptr<net::CookieStore> system_cookie_store; | |
| 125 std::unique_ptr<net::HttpUserAgentSettings> http_user_agent_settings; | |
| 126 std::unique_ptr<net::NetworkQualityEstimator> network_quality_estimator; | |
| 127 std::unique_ptr<net::CTPolicyEnforcer> ct_policy_enforcer; | |
| 128 }; | |
| 129 | |
| 130 // |net_log| must either outlive the IOSChromeIOThread or be NULL. | |
| 131 IOSChromeIOThread(PrefService* local_state, net_log::ChromeNetLog* net_log); | 22 IOSChromeIOThread(PrefService* local_state, net_log::ChromeNetLog* net_log); |
| 132 | |
| 133 ~IOSChromeIOThread() override; | 23 ~IOSChromeIOThread() override; |
| 134 | 24 |
| 135 // Can only be called on the IO thread. | 25 protected: |
| 136 Globals* globals(); | 26 // io_thread::IOSIOThread overrides |
| 137 | 27 std::unique_ptr<net::NetworkDelegate> CreateSystemNetworkDelegate() override; |
| 138 // Allows overriding Globals in tests where IOSChromeIOThread::Init() and | 28 std::string GetChannelString() const override; |
| 139 // IOSChromeIOThread::CleanUp() are not called. This allows for injecting | |
| 140 // mocks into IOSChromeIOThread global objects. | |
| 141 void SetGlobalsForTesting(Globals* globals); | |
| 142 | |
| 143 net_log::ChromeNetLog* net_log(); | |
| 144 | |
| 145 // Handles changing to On The Record mode, discarding confidential data. | |
| 146 void ChangedToOnTheRecord(); | |
| 147 | |
| 148 // Returns a getter for the URLRequestContext. Only called on the UI thread. | |
| 149 net::URLRequestContextGetter* system_url_request_context_getter(); | |
| 150 | |
| 151 // Clears the host cache. Intended to be used to prevent exposing recently | |
| 152 // visited sites on about:net-internals/#dns and about:dns pages. Must be | |
| 153 // called on the IO thread. | |
| 154 void ClearHostCache(); | |
| 155 | |
| 156 const net::HttpNetworkSession::Params& NetworkSessionParams() const; | |
| 157 | |
| 158 base::TimeTicks creation_time() const; | |
| 159 | 29 |
| 160 private: | 30 private: |
| 161 // Provide SystemURLRequestContextGetter with access to | |
| 162 // InitSystemRequestContext(). | |
| 163 friend class SystemURLRequestContextGetter; | |
| 164 | |
| 165 // WebThreadDelegate implementation, runs on the IO thread. | |
| 166 // This handles initialization and destruction of state that must | |
| 167 // live on the IO thread. | |
| 168 void Init() override; | |
| 169 void CleanUp() override; | |
| 170 | |
| 171 // Global state must be initialized on the IO thread, then this | |
| 172 // method must be invoked on the UI thread. | |
| 173 void InitSystemRequestContext(); | |
| 174 | |
| 175 // Lazy initialization of system request context for | |
| 176 // SystemURLRequestContextGetter. To be called on IO thread only | |
| 177 // after global state has been initialized on the IO thread, and | |
| 178 // SystemRequestContext state has been initialized on the UI thread. | |
| 179 void InitSystemRequestContextOnIOThread(); | |
| 180 | |
| 181 void CreateDefaultAuthHandlerFactory(); | |
| 182 | |
| 183 // Returns an SSLConfigService instance. | |
| 184 net::SSLConfigService* GetSSLConfigService(); | |
| 185 | |
| 186 void ChangedToOnTheRecordOnIOThread(); | |
| 187 | |
| 188 static net::URLRequestContext* ConstructSystemRequestContext( | |
| 189 Globals* globals, | |
| 190 const net::HttpNetworkSession::Params& params, | |
| 191 net::NetLog* net_log); | |
| 192 | |
| 193 // The NetLog is owned by the application context, to allow logging from other | |
| 194 // threads during shutdown, but is used most frequently on the IO thread. | |
| 195 net_log::ChromeNetLog* net_log_; | |
| 196 | |
| 197 // These member variables are basically global, but their lifetimes are tied | |
| 198 // to the IOSChromeIOThread. IOSChromeIOThread owns them all, despite not | |
| 199 // using scoped_ptr. This is because the destructor of IOSChromeIOThread runs | |
| 200 // on the wrong thread. All member variables should be deleted in CleanUp(). | |
| 201 | |
| 202 // These member variables are initialized in Init() and do not change for the | |
| 203 // lifetime of the IO thread. | |
| 204 | |
| 205 Globals* globals_; | |
| 206 | |
| 207 net::HttpNetworkSession::Params params_; | |
| 208 | |
| 209 // Observer that logs network changes to the ChromeNetLog. | |
| 210 class LoggingNetworkChangeObserver; | |
| 211 std::unique_ptr<LoggingNetworkChangeObserver> network_change_observer_; | |
| 212 | |
| 213 // This is an instance of the default SSLConfigServiceManager for the current | |
| 214 // platform and it gets SSL preferences from local_state object. | |
| 215 std::unique_ptr<ssl_config::SSLConfigServiceManager> | |
| 216 ssl_config_service_manager_; | |
| 217 | |
| 218 // These member variables are initialized by a task posted to the IO thread, | |
| 219 // which gets posted by calling certain member functions of IOSChromeIOThread. | |
| 220 std::unique_ptr<net::ProxyConfigService> system_proxy_config_service_; | |
| 221 | |
| 222 std::unique_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_; | |
| 223 | |
| 224 scoped_refptr<SystemURLRequestContextGetter> | |
| 225 system_url_request_context_getter_; | |
| 226 | |
| 227 const base::TimeTicks creation_time_; | |
| 228 | |
| 229 base::WeakPtrFactory<IOSChromeIOThread> weak_factory_; | |
| 230 | |
| 231 DISALLOW_COPY_AND_ASSIGN(IOSChromeIOThread); | 31 DISALLOW_COPY_AND_ASSIGN(IOSChromeIOThread); |
| 232 }; | 32 }; |
| 233 | 33 |
| 234 #endif // IOS_CHROME_BROWSER_IOS_CHROME_IO_THREAD_H_ | 34 #endif // IOS_CHROME_BROWSER_IOS_CHROME_IO_THREAD_H_ |
| OLD | NEW |