OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ | 5 #ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
6 #define CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ | 6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/prefs/pref_change_registrar.h" | 14 #include "base/prefs/pref_change_registrar.h" |
15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "net/base/host_port_pair.h" | 17 #include "net/base/host_port_pair.h" |
18 #include "net/http/http_server_properties.h" | 18 #include "net/http/http_server_properties.h" |
19 #include "net/http/http_server_properties_impl.h" | 19 #include "net/http/http_server_properties_impl.h" |
20 | 20 |
21 class PrefService; | 21 class PrefService; |
22 | 22 |
23 namespace user_prefs { | 23 namespace base { |
24 class PrefRegistrySyncable; | 24 class SequencedTaskRunner; |
25 } | 25 } |
26 | 26 |
27 namespace chrome_browser_net { | 27 namespace net { |
28 | 28 |
29 //////////////////////////////////////////////////////////////////////////////// | 29 //////////////////////////////////////////////////////////////////////////////// |
30 // HttpServerPropertiesManager | 30 // HttpServerPropertiesManager |
31 | 31 |
32 // The manager for creating and updating an HttpServerProperties (for example it | 32 // The manager for creating and updating an HttpServerProperties (for example it |
33 // tracks if a server supports SPDY or not). | 33 // tracks if a server supports SPDY or not). |
34 // | 34 // |
35 // This class interacts with both the UI thread, where notifications of pref | 35 // This class interacts with both the pref thread, where notifications of pref |
36 // changes are received from, and the IO thread, which owns it (in the | 36 // changes are received from, and the network thread, which owns it, and it |
37 // ProfileIOData) and it persists the changes from network stack that if a | 37 // persists the changes from network stack whether server supports SPDY or not. |
38 // server supports SPDY or not. | |
39 // | 38 // |
40 // It must be constructed on the UI thread, to set up |ui_method_factory_| and | 39 // It must be constructed on the pref thread, to set up |pref_task_runner_| and |
41 // the prefs listeners. | 40 // the prefs listeners. |
42 // | 41 // |
43 // ShutdownOnUIThread must be called from UI before destruction, to release | 42 // ShutdownOnPrefThread must be called from pref thread before destruction, to |
44 // the prefs listeners on the UI thread. This is done from ProfileIOData. | 43 // release the prefs listeners on the pref thread. |
45 // | 44 // |
46 // Update tasks from the UI thread can post safely to the IO thread, since the | 45 // Class requires that update tasks from the Pref thread can post safely to the |
47 // destruction order of Profile and ProfileIOData guarantees that if this | 46 // network thread, so the destruction order must guarantee that if |this| |
48 // exists in UI, then a potential destruction on IO will come after any task | 47 // exists in pref thread, then a potential destruction on network thread will |
49 // posted to IO from that method on UI. This is used to go through IO before | 48 // come after any task posted to network thread from that method on pref thread. |
50 // the actual update starts, and grab a WeakPtr. | 49 // This is used to go through network thread before the actual update starts, |
51 class HttpServerPropertiesManager | 50 // and grab a WeakPtr. |
52 : public net::HttpServerProperties { | 51 class NET_EXPORT HttpServerPropertiesManager : public HttpServerProperties { |
53 public: | 52 public: |
54 // Create an instance of the HttpServerPropertiesManager. The lifetime of the | 53 // Create an instance of the HttpServerPropertiesManager. The lifetime of the |
55 // PrefService objects must be longer than that of the | 54 // PrefService objects must be longer than that of the |
56 // HttpServerPropertiesManager object. Must be constructed on the UI thread. | 55 // HttpServerPropertiesManager object. Must be constructed on the Pref thread. |
57 explicit HttpServerPropertiesManager(PrefService* pref_service); | 56 HttpServerPropertiesManager( |
| 57 PrefService* pref_service, |
| 58 const char* pref_path, |
| 59 scoped_refptr<base::SequencedTaskRunner> network_task_runner); |
58 virtual ~HttpServerPropertiesManager(); | 60 virtual ~HttpServerPropertiesManager(); |
59 | 61 |
60 // Initialize |http_server_properties_impl_| and |io_method_factory_| on IO | 62 // Initialize on Network thread. |
61 // thread. It also posts a task to UI thread to get SPDY Server preferences | 63 void InitializeOnNetworkThread(); |
62 // from |pref_service_|. | |
63 void InitializeOnIOThread(); | |
64 | 64 |
65 // Prepare for shutdown. Must be called on the UI thread, before destruction. | 65 // Prepare for shutdown. Must be called on the Pref thread before destruction. |
66 void ShutdownOnUIThread(); | 66 void ShutdownOnPrefThread(); |
67 | |
68 // Register |prefs| for properties managed here. | |
69 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
70 | 67 |
71 // Helper function for unit tests to set the version in the dictionary. | 68 // Helper function for unit tests to set the version in the dictionary. |
72 static void SetVersion(base::DictionaryValue* http_server_properties_dict, | 69 static void SetVersion(base::DictionaryValue* http_server_properties_dict, |
73 int version_number); | 70 int version_number); |
74 | 71 |
75 // Deletes all data. Works asynchronously, but if a |completion| callback is | 72 // Deletes all data. Works asynchronously, but if a |completion| callback is |
76 // provided, it will be fired on the UI thread when everything is done. | 73 // provided, it will be fired on the pref thread when everything is done. |
77 void Clear(const base::Closure& completion); | 74 void Clear(const base::Closure& completion); |
78 | 75 |
79 // ---------------------------------- | 76 // ---------------------------------- |
80 // net::HttpServerProperties methods: | 77 // HttpServerProperties methods: |
81 // ---------------------------------- | 78 // ---------------------------------- |
82 | 79 |
83 // Gets a weak pointer for this object. | 80 // Gets a weak pointer for this object. |
84 virtual base::WeakPtr<net::HttpServerProperties> GetWeakPtr() OVERRIDE; | 81 virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() OVERRIDE; |
85 | 82 |
86 // Deletes all data. Works asynchronously. | 83 // Deletes all data. Works asynchronously. |
87 virtual void Clear() OVERRIDE; | 84 virtual void Clear() OVERRIDE; |
88 | 85 |
89 // Returns true if |server| supports SPDY. Should only be called from IO | 86 // Returns true if |server| supports SPDY. Should only be called from IO |
90 // thread. | 87 // thread. |
91 virtual bool SupportsSpdy(const net::HostPortPair& server) OVERRIDE; | 88 virtual bool SupportsSpdy(const HostPortPair& server) OVERRIDE; |
92 | 89 |
93 // Add |server| as the SPDY server which supports SPDY protocol into the | 90 // Add |server| as the SPDY server which supports SPDY protocol into the |
94 // persisitent store. Should only be called from IO thread. | 91 // persisitent store. Should only be called from IO thread. |
95 virtual void SetSupportsSpdy(const net::HostPortPair& server, | 92 virtual void SetSupportsSpdy(const HostPortPair& server, |
96 bool support_spdy) OVERRIDE; | 93 bool support_spdy) OVERRIDE; |
97 | 94 |
98 // Returns true if |server| has an Alternate-Protocol header. | 95 // Returns true if |server| has an Alternate-Protocol header. |
99 virtual bool HasAlternateProtocol(const net::HostPortPair& server) OVERRIDE; | 96 virtual bool HasAlternateProtocol(const HostPortPair& server) OVERRIDE; |
100 | 97 |
101 // Returns the Alternate-Protocol and port for |server|. | 98 // Returns the Alternate-Protocol and port for |server|. |
102 // HasAlternateProtocol(server) must be true. | 99 // HasAlternateProtocol(server) must be true. |
103 virtual net::AlternateProtocolInfo GetAlternateProtocol( | 100 virtual AlternateProtocolInfo GetAlternateProtocol( |
104 const net::HostPortPair& server) OVERRIDE; | 101 const HostPortPair& server) OVERRIDE; |
105 | 102 |
106 // Sets the Alternate-Protocol for |server|. | 103 // Sets the Alternate-Protocol for |server|. |
107 virtual void SetAlternateProtocol( | 104 virtual void SetAlternateProtocol( |
108 const net::HostPortPair& server, | 105 const HostPortPair& server, |
109 uint16 alternate_port, | 106 uint16 alternate_port, |
110 net::AlternateProtocol alternate_protocol, | 107 AlternateProtocol alternate_protocol, |
111 double alternate_probability) OVERRIDE; | 108 double alternate_probability) OVERRIDE; |
112 | 109 |
113 // Sets the Alternate-Protocol for |server| to be BROKEN. | 110 // Sets the Alternate-Protocol for |server| to be BROKEN. |
114 virtual void SetBrokenAlternateProtocol( | 111 virtual void SetBrokenAlternateProtocol(const HostPortPair& server) OVERRIDE; |
115 const net::HostPortPair& server) OVERRIDE; | |
116 | 112 |
117 // Returns true if Alternate-Protocol for |server| was recently BROKEN. | 113 // Returns true if Alternate-Protocol for |server| was recently BROKEN. |
118 virtual bool WasAlternateProtocolRecentlyBroken( | 114 virtual bool WasAlternateProtocolRecentlyBroken( |
119 const net::HostPortPair& server) OVERRIDE; | 115 const HostPortPair& server) OVERRIDE; |
120 | 116 |
121 // Confirms that Alternate-Protocol for |server| is working. | 117 // Confirms that Alternate-Protocol for |server| is working. |
122 virtual void ConfirmAlternateProtocol( | 118 virtual void ConfirmAlternateProtocol(const HostPortPair& server) OVERRIDE; |
123 const net::HostPortPair& server) OVERRIDE; | |
124 | 119 |
125 // Clears the Alternate-Protocol for |server|. | 120 // Clears the Alternate-Protocol for |server|. |
126 virtual void ClearAlternateProtocol(const net::HostPortPair& server) OVERRIDE; | 121 virtual void ClearAlternateProtocol(const HostPortPair& server) OVERRIDE; |
127 | 122 |
128 // Returns all Alternate-Protocol mappings. | 123 // Returns all Alternate-Protocol mappings. |
129 virtual const net::AlternateProtocolMap& | 124 virtual const AlternateProtocolMap& alternate_protocol_map() const OVERRIDE; |
130 alternate_protocol_map() const OVERRIDE; | |
131 | 125 |
132 virtual void SetAlternateProtocolExperiment( | 126 virtual void SetAlternateProtocolExperiment( |
133 net::AlternateProtocolExperiment experiment) OVERRIDE; | 127 AlternateProtocolExperiment experiment) OVERRIDE; |
134 | 128 |
135 virtual void SetAlternateProtocolProbabilityThreshold( | 129 virtual void SetAlternateProtocolProbabilityThreshold( |
136 double threshold) OVERRIDE; | 130 double threshold) OVERRIDE; |
137 | 131 |
138 virtual net::AlternateProtocolExperiment GetAlternateProtocolExperiment() | 132 virtual AlternateProtocolExperiment GetAlternateProtocolExperiment() |
139 const OVERRIDE; | 133 const OVERRIDE; |
140 | 134 |
141 // Gets a reference to the SettingsMap stored for a host. | 135 // Gets a reference to the SettingsMap stored for a host. |
142 // If no settings are stored, returns an empty SettingsMap. | 136 // If no settings are stored, returns an empty SettingsMap. |
143 virtual const net::SettingsMap& GetSpdySettings( | 137 virtual const SettingsMap& GetSpdySettings( |
144 const net::HostPortPair& host_port_pair) OVERRIDE; | 138 const HostPortPair& host_port_pair) OVERRIDE; |
145 | 139 |
146 // Saves an individual SPDY setting for a host. Returns true if SPDY setting | 140 // Saves an individual SPDY setting for a host. Returns true if SPDY setting |
147 // is to be persisted. | 141 // is to be persisted. |
148 virtual bool SetSpdySetting(const net::HostPortPair& host_port_pair, | 142 virtual bool SetSpdySetting(const HostPortPair& host_port_pair, |
149 net::SpdySettingsIds id, | 143 SpdySettingsIds id, |
150 net::SpdySettingsFlags flags, | 144 SpdySettingsFlags flags, |
151 uint32 value) OVERRIDE; | 145 uint32 value) OVERRIDE; |
152 | 146 |
153 // Clears all SPDY settings for a host. | 147 // Clears all SPDY settings for a host. |
154 virtual void ClearSpdySettings( | 148 virtual void ClearSpdySettings(const HostPortPair& host_port_pair) OVERRIDE; |
155 const net::HostPortPair& host_port_pair) OVERRIDE; | |
156 | 149 |
157 // Clears all SPDY settings for all hosts. | 150 // Clears all SPDY settings for all hosts. |
158 virtual void ClearAllSpdySettings() OVERRIDE; | 151 virtual void ClearAllSpdySettings() OVERRIDE; |
159 | 152 |
160 // Returns all SPDY persistent settings. | 153 // Returns all SPDY persistent settings. |
161 virtual const net::SpdySettingsMap& spdy_settings_map() const OVERRIDE; | 154 virtual const SpdySettingsMap& spdy_settings_map() const OVERRIDE; |
162 | 155 |
163 virtual void SetServerNetworkStats(const net::HostPortPair& host_port_pair, | 156 virtual void SetServerNetworkStats(const HostPortPair& host_port_pair, |
164 NetworkStats stats) OVERRIDE; | 157 NetworkStats stats) OVERRIDE; |
165 | 158 |
166 virtual const NetworkStats* GetServerNetworkStats( | 159 virtual const NetworkStats* GetServerNetworkStats( |
167 const net::HostPortPair& host_port_pair) const OVERRIDE; | 160 const HostPortPair& host_port_pair) const OVERRIDE; |
168 | 161 |
169 protected: | 162 protected: |
170 // -------------------- | 163 // -------------------- |
171 // SPDY related methods | 164 // SPDY related methods |
172 | 165 |
173 // These are used to delay updating of the cached data in | 166 // These are used to delay updating of the cached data in |
174 // |http_server_properties_impl_| while the preferences are changing, and | 167 // |http_server_properties_impl_| while the preferences are changing, and |
175 // execute only one update per simultaneous prefs changes. | 168 // execute only one update per simultaneous prefs changes. |
176 void ScheduleUpdateCacheOnUI(); | 169 void ScheduleUpdateCacheOnPrefThread(); |
177 | 170 |
178 // Starts the timers to update the cached prefs. This are overridden in tests | 171 // Starts the timers to update the cached prefs. This are overridden in tests |
179 // to prevent the delay. | 172 // to prevent the delay. |
180 virtual void StartCacheUpdateTimerOnUI(base::TimeDelta delay); | 173 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay); |
181 | 174 |
182 // Update cached prefs in |http_server_properties_impl_| with data from | 175 // Update cached prefs in |http_server_properties_impl_| with data from |
183 // preferences. It gets the data on UI thread and calls | 176 // preferences. It gets the data on pref thread and calls |
184 // UpdateSpdyServersFromPrefsOnIO() to perform the update on IO thread. | 177 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on |
185 virtual void UpdateCacheFromPrefsOnUI(); | 178 // network thread. |
| 179 virtual void UpdateCacheFromPrefsOnPrefThread(); |
186 | 180 |
187 // Starts the update of cached prefs in |http_server_properties_impl_| on the | 181 // Starts the update of cached prefs in |http_server_properties_impl_| on the |
188 // IO thread. Protected for testing. | 182 // network thread. Protected for testing. |
189 void UpdateCacheFromPrefsOnIO( | 183 void UpdateCacheFromPrefsOnNetworkThread( |
190 std::vector<std::string>* spdy_servers, | 184 std::vector<std::string>* spdy_servers, |
191 net::SpdySettingsMap* spdy_settings_map, | 185 SpdySettingsMap* spdy_settings_map, |
192 net::AlternateProtocolMap* alternate_protocol_map, | 186 AlternateProtocolMap* alternate_protocol_map, |
193 net::AlternateProtocolExperiment alternate_protocol_experiment, | 187 AlternateProtocolExperiment alternate_protocol_experiment, |
194 bool detected_corrupted_prefs); | 188 bool detected_corrupted_prefs); |
195 | 189 |
196 // These are used to delay updating the preferences when cached data in | 190 // These are used to delay updating the preferences when cached data in |
197 // |http_server_properties_impl_| is changing, and execute only one update per | 191 // |http_server_properties_impl_| is changing, and execute only one update per |
198 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes. | 192 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes. |
199 void ScheduleUpdatePrefsOnIO(); | 193 void ScheduleUpdatePrefsOnNetworkThread(); |
200 | 194 |
201 // Starts the timers to update the prefs from cache. This are overridden in | 195 // Starts the timers to update the prefs from cache. This are overridden in |
202 // tests to prevent the delay. | 196 // tests to prevent the delay. |
203 virtual void StartPrefsUpdateTimerOnIO(base::TimeDelta delay); | 197 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay); |
204 | 198 |
205 // Update prefs::kHttpServerProperties in preferences with the cached data | 199 // Update prefs::kHttpServerProperties in preferences with the cached data |
206 // from |http_server_properties_impl_|. This gets the data on IO thread and | 200 // from |http_server_properties_impl_|. This gets the data on network thread |
207 // posts a task (UpdatePrefsOnUI) to update the preferences UI thread. | 201 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref |
208 void UpdatePrefsFromCacheOnIO(); | 202 // thread. |
| 203 void UpdatePrefsFromCacheOnNetworkThread(); |
209 | 204 |
210 // Same as above, but fires an optional |completion| callback on the UI thread | 205 // Same as above, but fires an optional |completion| callback on pref thread |
211 // when finished. Virtual for testing. | 206 // when finished. Virtual for testing. |
212 virtual void UpdatePrefsFromCacheOnIO(const base::Closure& completion); | 207 virtual void UpdatePrefsFromCacheOnNetworkThread( |
| 208 const base::Closure& completion); |
213 | 209 |
214 // Update prefs::kHttpServerProperties preferences on UI thread. Executes an | 210 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an |
215 // optional |completion| callback when finished. Protected for testing. | 211 // optional |completion| callback when finished. Protected for testing. |
216 void UpdatePrefsOnUI( | 212 void UpdatePrefsOnPrefThread(base::ListValue* spdy_server_list, |
217 base::ListValue* spdy_server_list, | 213 SpdySettingsMap* spdy_settings_map, |
218 net::SpdySettingsMap* spdy_settings_map, | 214 AlternateProtocolMap* alternate_protocol_map, |
219 net::AlternateProtocolMap* alternate_protocol_map, | 215 const base::Closure& completion); |
220 const base::Closure& completion); | |
221 | 216 |
222 private: | 217 private: |
223 void OnHttpServerPropertiesChanged(); | 218 void OnHttpServerPropertiesChanged(); |
224 | 219 |
225 // --------- | 220 // ----------- |
226 // UI thread | 221 // Pref thread |
227 // --------- | 222 // ----------- |
228 | 223 |
229 // Used to get |weak_ptr_| to self on the UI thread. | 224 const scoped_refptr<base::SequencedTaskRunner> pref_task_runner_; |
| 225 |
| 226 // Used to get |weak_ptr_| to self on the pref thread. |
230 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > | 227 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > |
231 ui_weak_ptr_factory_; | 228 pref_weak_ptr_factory_; |
232 | 229 |
233 base::WeakPtr<HttpServerPropertiesManager> ui_weak_ptr_; | 230 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_; |
234 | 231 |
235 // Used to post cache update tasks. | 232 // Used to post cache update tasks. |
236 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> > | 233 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> > |
237 ui_cache_update_timer_; | 234 pref_cache_update_timer_; |
238 | 235 |
239 // Used to track the spdy servers changes. | 236 // Used to track the spdy servers changes. |
240 PrefChangeRegistrar pref_change_registrar_; | 237 PrefChangeRegistrar pref_change_registrar_; |
241 PrefService* pref_service_; // Weak. | 238 PrefService* pref_service_; // Weak. |
242 bool setting_prefs_; | 239 bool setting_prefs_; |
| 240 const char* path_; |
243 | 241 |
244 // --------- | 242 // -------------- |
245 // IO thread | 243 // Network thread |
246 // --------- | 244 // -------------- |
247 | 245 |
248 // Used to get |weak_ptr_| to self on the IO thread. | 246 const scoped_refptr<base::SequencedTaskRunner> network_task_runner_; |
| 247 |
| 248 // Used to get |weak_ptr_| to self on the network thread. |
249 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > | 249 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > |
250 io_weak_ptr_factory_; | 250 network_weak_ptr_factory_; |
251 | 251 |
252 // Used to post |prefs::kHttpServerProperties| pref update tasks. | 252 // Used to post |prefs::kHttpServerProperties| pref update tasks. |
253 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> > | 253 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> > |
254 io_prefs_update_timer_; | 254 network_prefs_update_timer_; |
255 | 255 |
256 scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_impl_; | 256 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_; |
257 | 257 |
258 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager); | 258 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager); |
259 }; | 259 }; |
260 | 260 |
261 } // namespace chrome_browser_net | 261 } // namespace net |
262 | 262 |
263 #endif // CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ | 263 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
OLD | NEW |