OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ | 5 #ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ | 6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/time/default_tick_clock.h" |
18 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
19 #include "base/values.h" | 20 #include "base/values.h" |
20 #include "net/base/host_port_pair.h" | 21 #include "net/base/host_port_pair.h" |
21 #include "net/base/net_export.h" | 22 #include "net/base/net_export.h" |
22 #include "net/http/http_server_properties.h" | 23 #include "net/http/http_server_properties.h" |
23 #include "net/http/http_server_properties_impl.h" | 24 #include "net/http/http_server_properties_impl.h" |
24 | 25 |
25 namespace base { | 26 namespace base { |
26 class SingleThreadTaskRunner; | 27 class SingleThreadTaskRunner; |
| 28 class TickClock; |
27 } | 29 } |
28 | 30 |
29 namespace net { | 31 namespace net { |
30 | 32 |
31 class IPAddress; | 33 class IPAddress; |
32 | 34 |
33 //////////////////////////////////////////////////////////////////////////////// | 35 //////////////////////////////////////////////////////////////////////////////// |
34 // HttpServerPropertiesManager | 36 // HttpServerPropertiesManager |
35 | 37 |
36 // The manager for creating and updating an HttpServerProperties (for example it | 38 // The manager for creating and updating an HttpServerProperties (for example it |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 // | 91 // |
90 // There are two SingleThreadTaskRunners: | 92 // There are two SingleThreadTaskRunners: |
91 // |pref_task_runner| should be bound with the pref thread and is used to post | 93 // |pref_task_runner| should be bound with the pref thread and is used to post |
92 // cache update to the pref thread; | 94 // cache update to the pref thread; |
93 // |network_task_runner| should be bound with the network thread and is used | 95 // |network_task_runner| should be bound with the network thread and is used |
94 // to post pref update to the cache thread. | 96 // to post pref update to the cache thread. |
95 HttpServerPropertiesManager( | 97 HttpServerPropertiesManager( |
96 PrefDelegate* pref_delegate, | 98 PrefDelegate* pref_delegate, |
97 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner, | 99 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner, |
98 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner); | 100 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner); |
| 101 |
99 ~HttpServerPropertiesManager() override; | 102 ~HttpServerPropertiesManager() override; |
100 | 103 |
101 // Initialize on Network thread. | 104 // Initialize on Network thread. |
102 void InitializeOnNetworkSequence(); | 105 void InitializeOnNetworkSequence(); |
103 | 106 |
104 // Prepare for shutdown. Must be called on the Pref thread before destruction. | 107 // Prepare for shutdown. Must be called on the Pref thread before destruction. |
105 void ShutdownOnPrefSequence(); | 108 void ShutdownOnPrefSequence(); |
106 | 109 |
107 // Helper function for unit tests to set the version in the dictionary. | 110 // Helper function for unit tests to set the version in the dictionary. |
108 static void SetVersion(base::DictionaryValue* http_server_properties_dict, | 111 static void SetVersion(base::DictionaryValue* http_server_properties_dict, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 const std::string* GetQuicServerInfo(const QuicServerId& server_id) override; | 162 const std::string* GetQuicServerInfo(const QuicServerId& server_id) override; |
160 const QuicServerInfoMap& quic_server_info_map() const override; | 163 const QuicServerInfoMap& quic_server_info_map() const override; |
161 size_t max_server_configs_stored_in_properties() const override; | 164 size_t max_server_configs_stored_in_properties() const override; |
162 void SetMaxServerConfigsStoredInProperties( | 165 void SetMaxServerConfigsStoredInProperties( |
163 size_t max_server_configs_stored_in_properties) override; | 166 size_t max_server_configs_stored_in_properties) override; |
164 bool IsInitialized() const override; | 167 bool IsInitialized() const override; |
165 | 168 |
166 static base::TimeDelta GetUpdateCacheDelayForTesting(); | 169 static base::TimeDelta GetUpdateCacheDelayForTesting(); |
167 static base::TimeDelta GetUpdatePrefsDelayForTesting(); | 170 static base::TimeDelta GetUpdatePrefsDelayForTesting(); |
168 | 171 |
| 172 // |clock| is unowned. |
| 173 void SetTickClockForTesting(base::TickClock* clock); |
| 174 |
169 protected: | 175 protected: |
170 // The location where ScheduleUpdatePrefsOnNetworkSequence was called. | 176 // The location where ScheduleUpdatePrefsOnNetworkSequence was called. |
171 // Must be kept up to date with HttpServerPropertiesUpdatePrefsLocation in | 177 // Must be kept up to date with HttpServerPropertiesUpdatePrefsLocation in |
172 // histograms.xml. | 178 // histograms.xml. |
173 enum Location { | 179 enum Location { |
174 SUPPORTS_SPDY = 0, | 180 SUPPORTS_SPDY = 0, |
175 HTTP_11_REQUIRED = 1, | 181 HTTP_11_REQUIRED = 1, |
176 SET_ALTERNATIVE_SERVICES = 2, | 182 SET_ALTERNATIVE_SERVICES = 2, |
177 MARK_ALTERNATIVE_SERVICE_BROKEN = 3, | 183 MARK_ALTERNATIVE_SERVICE_BROKEN = 3, |
178 MARK_ALTERNATIVE_SERVICE_RECENTLY_BROKEN = 4, | 184 MARK_ALTERNATIVE_SERVICE_RECENTLY_BROKEN = 4, |
(...skipping 25 matching lines...) Expand all Loading... |
204 virtual void UpdateCacheFromPrefsOnPrefSequence(); | 210 virtual void UpdateCacheFromPrefsOnPrefSequence(); |
205 | 211 |
206 // Starts the update of cached prefs in |http_server_properties_impl_| on the | 212 // Starts the update of cached prefs in |http_server_properties_impl_| on the |
207 // network thread. Protected for testing. | 213 // network thread. Protected for testing. |
208 void UpdateCacheFromPrefsOnNetworkSequence( | 214 void UpdateCacheFromPrefsOnNetworkSequence( |
209 std::vector<std::string>* spdy_servers, | 215 std::vector<std::string>* spdy_servers, |
210 AlternativeServiceMap* alternative_service_map, | 216 AlternativeServiceMap* alternative_service_map, |
211 IPAddress* last_quic_address, | 217 IPAddress* last_quic_address, |
212 ServerNetworkStatsMap* server_network_stats_map, | 218 ServerNetworkStatsMap* server_network_stats_map, |
213 QuicServerInfoMap* quic_server_info_map, | 219 QuicServerInfoMap* quic_server_info_map, |
| 220 std::unique_ptr<BrokenAlternativeServiceList> |
| 221 broken_alternative_service_list, |
| 222 std::unique_ptr<RecentlyBrokenAlternativeServices> |
| 223 recently_broken_alternative_services, |
214 bool detected_corrupted_prefs); | 224 bool detected_corrupted_prefs); |
215 | 225 |
216 // These are used to delay updating the preferences when cached data in | 226 // These are used to delay updating the preferences when cached data in |
217 // |http_server_properties_impl_| is changing, and execute only one update per | 227 // |http_server_properties_impl_| is changing, and execute only one update per |
218 // simultaneous spdy_servers or spdy_settings or alternative_service changes. | 228 // simultaneous spdy_servers or spdy_settings or alternative_service changes. |
219 // |location| specifies where this method is called from. Virtual for testing. | 229 // |location| specifies where this method is called from. Virtual for testing. |
220 virtual void ScheduleUpdatePrefsOnNetworkSequence(Location location); | 230 virtual void ScheduleUpdatePrefsOnNetworkSequence(Location location); |
221 | 231 |
222 // Update prefs::kHttpServerProperties in preferences with the cached data | 232 // Update prefs::kHttpServerProperties in preferences with the cached data |
223 // from |http_server_properties_impl_|. This gets the data on network thread | 233 // from |http_server_properties_impl_|. This gets the data on network thread |
224 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref | 234 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref |
225 // thread. | 235 // thread. |
226 void UpdatePrefsFromCacheOnNetworkSequence(); | 236 void UpdatePrefsFromCacheOnNetworkSequence(); |
227 | 237 |
228 // Same as above, but fires an optional |completion| callback on pref thread | 238 // Same as above, but fires an optional |completion| callback on pref thread |
229 // when finished. Virtual for testing. | 239 // when finished. Virtual for testing. |
230 virtual void UpdatePrefsFromCacheOnNetworkSequence( | 240 virtual void UpdatePrefsFromCacheOnNetworkSequence( |
231 const base::Closure& completion); | 241 const base::Closure& completion); |
232 | 242 |
233 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an | 243 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an |
234 // optional |completion| callback when finished. Protected for testing. | 244 // optional |completion| callback when finished. Protected for testing. |
235 void UpdatePrefsOnPrefThread(base::ListValue* spdy_server_list, | 245 void UpdatePrefsOnPrefThread( |
236 AlternativeServiceMap* alternative_service_map, | 246 base::ListValue* spdy_server_list, |
237 IPAddress* last_quic_address, | 247 AlternativeServiceMap* alternative_service_map, |
238 ServerNetworkStatsMap* server_network_stats_map, | 248 IPAddress* last_quic_address, |
239 QuicServerInfoMap* quic_server_info_map, | 249 ServerNetworkStatsMap* server_network_stats_map, |
240 const base::Closure& completion); | 250 QuicServerInfoMap* quic_server_info_map, |
| 251 BrokenAlternativeServiceList* broken_alternative_service_list, |
| 252 RecentlyBrokenAlternativeServices* recently_broken_alternative_services, |
| 253 const base::Closure& completion); |
241 | 254 |
242 private: | 255 private: |
243 typedef std::vector<std::string> ServerList; | 256 typedef std::vector<std::string> ServerList; |
244 | 257 |
245 FRIEND_TEST_ALL_PREFIXES(HttpServerPropertiesManagerTest, | 258 FRIEND_TEST_ALL_PREFIXES(HttpServerPropertiesManagerTest, |
246 AddToAlternativeServiceMap); | 259 AddToAlternativeServiceMap); |
247 FRIEND_TEST_ALL_PREFIXES(HttpServerPropertiesManagerTest, | 260 FRIEND_TEST_ALL_PREFIXES(HttpServerPropertiesManagerTest, |
248 DoNotLoadAltSvcForInsecureOrigins); | 261 DoNotLoadAltSvcForInsecureOrigins); |
249 FRIEND_TEST_ALL_PREFIXES(HttpServerPropertiesManagerTest, | 262 FRIEND_TEST_ALL_PREFIXES(HttpServerPropertiesManagerTest, |
250 DoNotLoadExpiredAlternativeService); | 263 DoNotLoadExpiredAlternativeService); |
(...skipping 12 matching lines...) Expand all Loading... |
263 const url::SchemeHostPort& server, | 276 const url::SchemeHostPort& server, |
264 const base::DictionaryValue& server_dict, | 277 const base::DictionaryValue& server_dict, |
265 AlternativeServiceMap* alternative_service_map); | 278 AlternativeServiceMap* alternative_service_map); |
266 bool ReadSupportsQuic(const base::DictionaryValue& server_dict, | 279 bool ReadSupportsQuic(const base::DictionaryValue& server_dict, |
267 IPAddress* last_quic_address); | 280 IPAddress* last_quic_address); |
268 bool AddToNetworkStatsMap(const url::SchemeHostPort& server, | 281 bool AddToNetworkStatsMap(const url::SchemeHostPort& server, |
269 const base::DictionaryValue& server_dict, | 282 const base::DictionaryValue& server_dict, |
270 ServerNetworkStatsMap* network_stats_map); | 283 ServerNetworkStatsMap* network_stats_map); |
271 bool AddToQuicServerInfoMap(const base::DictionaryValue& server_dict, | 284 bool AddToQuicServerInfoMap(const base::DictionaryValue& server_dict, |
272 QuicServerInfoMap* quic_server_info_map); | 285 QuicServerInfoMap* quic_server_info_map); |
| 286 bool ExtractBrokenAlternativeServiceData( |
| 287 const base::DictionaryValue* broken_alternative_service_dict, |
| 288 AlternativeService* alternative_service, |
| 289 base::TimeTicks* expiration_time_ticks); |
| 290 bool AddRecentlyBrokenAlternativeServiceData( |
| 291 const base::DictionaryValue* recently_broken_alternative_service_dict, |
| 292 RecentlyBrokenAlternativeServices* recently_broken_alternative_services); |
273 | 293 |
274 void SaveAlternativeServiceToServerPrefs( | 294 void SaveAlternativeServiceToServerPrefs( |
275 const AlternativeServiceInfoVector* alternative_service_info_vector, | 295 const AlternativeServiceInfoVector* alternative_service_info_vector, |
276 base::DictionaryValue* server_pref_dict); | 296 base::DictionaryValue* server_pref_dict); |
277 void SaveSupportsQuicToPrefs( | 297 void SaveSupportsQuicToPrefs( |
278 const IPAddress* last_quic_address, | 298 const IPAddress* last_quic_address, |
279 base::DictionaryValue* http_server_properties_dict); | 299 base::DictionaryValue* http_server_properties_dict); |
280 void SaveNetworkStatsToServerPrefs( | 300 void SaveNetworkStatsToServerPrefs( |
281 const ServerNetworkStats* server_network_stats, | 301 const ServerNetworkStats* server_network_stats, |
282 base::DictionaryValue* server_pref_dict); | 302 base::DictionaryValue* server_pref_dict); |
283 void SaveQuicServerInfoMapToServerPrefs( | 303 void SaveQuicServerInfoMapToServerPrefs( |
284 QuicServerInfoMap* quic_server_info_map, | 304 QuicServerInfoMap* quic_server_info_map, |
285 base::DictionaryValue* http_server_properties_dict); | 305 base::DictionaryValue* http_server_properties_dict); |
| 306 void SaveBrokenAlternativeServiceListToPrefs( |
| 307 const BrokenAlternativeServiceList* broken_alternative_service_list, |
| 308 base::DictionaryValue* http_server_properties_dict); |
| 309 void SaveRecentlyBrokenAlternativeServicesToPrefs( |
| 310 const RecentlyBrokenAlternativeServices* |
| 311 recently_broken_alternative_services, |
| 312 base::DictionaryValue* http_server_properties_dict); |
286 void SetInitialized(); | 313 void SetInitialized(); |
287 | 314 |
288 // ----------- | 315 // ----------- |
289 // Pref thread | 316 // Pref thread |
290 // ----------- | 317 // ----------- |
291 | 318 |
292 const scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner_; | 319 const scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner_; |
293 | 320 |
294 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_; | 321 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_; |
295 | 322 |
296 // Used to post cache update tasks. | 323 // Used to post cache update tasks. |
297 std::unique_ptr<base::OneShotTimer> pref_cache_update_timer_; | 324 std::unique_ptr<base::OneShotTimer> pref_cache_update_timer_; |
298 | 325 |
299 std::unique_ptr<PrefDelegate> pref_delegate_; | 326 std::unique_ptr<PrefDelegate> pref_delegate_; |
300 bool setting_prefs_; | 327 bool setting_prefs_; |
301 | 328 |
| 329 base::TickClock* clock_; // Unowned if not pointing to |default_clock_|. |
| 330 base::DefaultTickClock default_clock_; |
| 331 |
302 // -------------- | 332 // -------------- |
303 // Network thread | 333 // Network thread |
304 // -------------- | 334 // -------------- |
305 | 335 |
306 // Whether InitializeOnNetworkSequence() has completed. | 336 // Whether InitializeOnNetworkSequence() has completed. |
307 bool is_initialized_; | 337 bool is_initialized_; |
308 | 338 |
309 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 339 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
310 | 340 |
311 // Used to post |prefs::kHttpServerProperties| pref update tasks. | 341 // Used to post |prefs::kHttpServerProperties| pref update tasks. |
312 std::unique_ptr<base::OneShotTimer> network_prefs_update_timer_; | 342 std::unique_ptr<base::OneShotTimer> network_prefs_update_timer_; |
313 | 343 |
314 std::unique_ptr<HttpServerPropertiesImpl> http_server_properties_impl_; | 344 std::unique_ptr<HttpServerPropertiesImpl> http_server_properties_impl_; |
315 | 345 |
316 // Used to get |weak_ptr_| to self on the pref thread. | 346 // Used to get |weak_ptr_| to self on the pref thread. |
317 std::unique_ptr<base::WeakPtrFactory<HttpServerPropertiesManager>> | 347 std::unique_ptr<base::WeakPtrFactory<HttpServerPropertiesManager>> |
318 pref_weak_ptr_factory_; | 348 pref_weak_ptr_factory_; |
319 | 349 |
320 // Used to get |weak_ptr_| to self on the network thread. | 350 // Used to get |weak_ptr_| to self on the network thread. |
321 std::unique_ptr<base::WeakPtrFactory<HttpServerPropertiesManager>> | 351 std::unique_ptr<base::WeakPtrFactory<HttpServerPropertiesManager>> |
322 network_weak_ptr_factory_; | 352 network_weak_ptr_factory_; |
323 | 353 |
324 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager); | 354 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager); |
325 }; | 355 }; |
326 | 356 |
327 } // namespace net | 357 } // namespace net |
328 | 358 |
329 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ | 359 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
OLD | NEW |