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