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
3 // found in the LICENSE file.
5 #ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_
6 #define NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/prefs/pref_change_registrar.h"
16 #include "base/timer/timer.h"
17 #include "base/values.h"
18 #include "net/base/host_port_pair.h"
19 #include "net/http/http_server_properties.h"
20 #include "net/http/http_server_properties_impl.h"
25 class SequencedTaskRunner
;
30 ////////////////////////////////////////////////////////////////////////////////
31 // HttpServerPropertiesManager
33 // The manager for creating and updating an HttpServerProperties (for example it
34 // tracks if a server supports SPDY or not).
36 // This class interacts with both the pref thread, where notifications of pref
37 // changes are received from, and the network thread, which owns it, and it
38 // persists the changes from network stack whether server supports SPDY or not.
40 // It must be constructed on the pref thread, to set up |pref_task_runner_| and
41 // the prefs listeners.
43 // ShutdownOnPrefThread must be called from pref thread before destruction, to
44 // release the prefs listeners on the pref thread.
46 // Class requires that update tasks from the Pref thread can post safely to the
47 // network thread, so the destruction order must guarantee that if |this|
48 // exists in pref thread, then a potential destruction on network thread will
49 // come after any task posted to network thread from that method on pref thread.
50 // This is used to go through network thread before the actual update starts,
51 // and grab a WeakPtr.
52 class NET_EXPORT HttpServerPropertiesManager
: public HttpServerProperties
{
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 Pref thread.
57 HttpServerPropertiesManager(
58 PrefService
* pref_service
,
59 const char* pref_path
,
60 scoped_refptr
<base::SequencedTaskRunner
> network_task_runner
);
61 ~HttpServerPropertiesManager() override
;
63 // Initialize on Network thread.
64 void InitializeOnNetworkThread();
66 // Prepare for shutdown. Must be called on the Pref thread before destruction.
67 void ShutdownOnPrefThread();
69 // Helper function for unit tests to set the version in the dictionary.
70 static void SetVersion(base::DictionaryValue
* http_server_properties_dict
,
73 // Deletes all data. Works asynchronously, but if a |completion| callback is
74 // provided, it will be fired on the pref thread when everything is done.
75 void Clear(const base::Closure
& completion
);
77 // ----------------------------------
78 // HttpServerProperties methods:
79 // ----------------------------------
81 base::WeakPtr
<HttpServerProperties
> GetWeakPtr() override
;
82 void Clear() override
;
83 bool SupportsRequestPriority(const HostPortPair
& server
) override
;
84 bool GetSupportsSpdy(const HostPortPair
& server
) override
;
85 void SetSupportsSpdy(const HostPortPair
& server
, bool support_spdy
) override
;
86 bool RequiresHTTP11(const HostPortPair
& server
) override
;
87 void SetHTTP11Required(const HostPortPair
& server
) override
;
88 void MaybeForceHTTP11(const HostPortPair
& server
,
89 SSLConfig
* ssl_config
) override
;
90 AlternativeService
GetAlternativeService(const HostPortPair
& origin
) override
;
91 void SetAlternativeService(const HostPortPair
& origin
,
92 const AlternativeService
& alternative_service
,
93 double alternative_probability
) override
;
94 void MarkAlternativeServiceBroken(
95 const AlternativeService
& alternative_service
) override
;
96 void MarkAlternativeServiceRecentlyBroken(
97 const AlternativeService
& alternative_service
) override
;
98 bool IsAlternativeServiceBroken(
99 const AlternativeService
& alternative_service
) const override
;
100 bool WasAlternativeServiceRecentlyBroken(
101 const AlternativeService
& alternative_service
) override
;
102 void ConfirmAlternativeService(
103 const AlternativeService
& alternative_service
) override
;
104 void ClearAlternativeService(const HostPortPair
& origin
) override
;
105 const AlternativeServiceMap
& alternative_service_map() const override
;
106 scoped_ptr
<base::Value
> GetAlternativeServiceInfoAsValue() const override
;
107 void SetAlternativeServiceProbabilityThreshold(double threshold
) override
;
108 const SettingsMap
& GetSpdySettings(
109 const HostPortPair
& host_port_pair
) override
;
110 bool SetSpdySetting(const HostPortPair
& host_port_pair
,
112 SpdySettingsFlags flags
,
113 uint32 value
) override
;
114 void ClearSpdySettings(const HostPortPair
& host_port_pair
) override
;
115 void ClearAllSpdySettings() override
;
116 const SpdySettingsMap
& spdy_settings_map() const override
;
117 bool GetSupportsQuic(IPAddressNumber
* last_address
) const override
;
118 void SetSupportsQuic(bool used_quic
,
119 const IPAddressNumber
& last_address
) override
;
120 void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
121 ServerNetworkStats stats
) override
;
122 const ServerNetworkStats
* GetServerNetworkStats(
123 const HostPortPair
& host_port_pair
) override
;
124 const ServerNetworkStatsMap
& server_network_stats_map() const override
;
127 // The location where ScheduleUpdatePrefsOnNetworkThread was called.
130 HTTP_11_REQUIRED
= 1,
131 SET_ALTERNATIVE_SERVICE
= 2,
132 MARK_ALTERNATIVE_SERVICE_BROKEN
= 3,
133 MARK_ALTERNATIVE_SERVICE_RECENTLY_BROKEN
= 4,
134 CONFIRM_ALTERNATIVE_SERVICE
= 5,
135 CLEAR_ALTERNATIVE_SERVICE
= 6,
136 SET_SPDY_SETTING
= 7,
137 CLEAR_SPDY_SETTINGS
= 8,
138 CLEAR_ALL_SPDY_SETTINGS
= 9,
139 SET_SUPPORTS_QUIC
= 10,
140 SET_SERVER_NETWORK_STATS
= 11,
141 DETECTED_CORRUPTED_PREFS
= 12,
145 // --------------------
146 // SPDY related methods
148 // These are used to delay updating of the cached data in
149 // |http_server_properties_impl_| while the preferences are changing, and
150 // execute only one update per simultaneous prefs changes.
151 void ScheduleUpdateCacheOnPrefThread();
153 // Starts the timers to update the cached prefs. This are overridden in tests
154 // to prevent the delay.
155 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay
);
157 // Update cached prefs in |http_server_properties_impl_| with data from
158 // preferences. It gets the data on pref thread and calls
159 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on
161 virtual void UpdateCacheFromPrefsOnPrefThread();
163 // Starts the update of cached prefs in |http_server_properties_impl_| on the
164 // network thread. Protected for testing.
165 void UpdateCacheFromPrefsOnNetworkThread(
166 std::vector
<std::string
>* spdy_servers
,
167 SpdySettingsMap
* spdy_settings_map
,
168 AlternativeServiceMap
* alternative_service_map
,
169 IPAddressNumber
* last_quic_address
,
170 ServerNetworkStatsMap
* server_network_stats_map
,
171 bool detected_corrupted_prefs
);
173 // These are used to delay updating the preferences when cached data in
174 // |http_server_properties_impl_| is changing, and execute only one update per
175 // simultaneous spdy_servers or spdy_settings or alternative_service changes.
176 // |location| specifies where this method is called from. Virtual for testing.
177 virtual void ScheduleUpdatePrefsOnNetworkThread(Location location
);
179 // Starts the timers to update the prefs from cache. This are overridden in
180 // tests to prevent the delay.
181 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay
);
183 // Update prefs::kHttpServerProperties in preferences with the cached data
184 // from |http_server_properties_impl_|. This gets the data on network thread
185 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref
187 void UpdatePrefsFromCacheOnNetworkThread();
189 // Same as above, but fires an optional |completion| callback on pref thread
190 // when finished. Virtual for testing.
191 virtual void UpdatePrefsFromCacheOnNetworkThread(
192 const base::Closure
& completion
);
194 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an
195 // optional |completion| callback when finished. Protected for testing.
196 void UpdatePrefsOnPrefThread(base::ListValue
* spdy_server_list
,
197 SpdySettingsMap
* spdy_settings_map
,
198 AlternativeServiceMap
* alternative_service_map
,
199 IPAddressNumber
* last_quic_address
,
200 ServerNetworkStatsMap
* server_network_stats_map
,
201 const base::Closure
& completion
);
204 void OnHttpServerPropertiesChanged();
206 bool ReadSupportsQuic(const base::DictionaryValue
& server_dict
,
207 IPAddressNumber
* last_quic_address
);
208 void AddToSpdySettingsMap(const HostPortPair
& server
,
209 const base::DictionaryValue
& server_dict
,
210 SpdySettingsMap
* spdy_settings_map
);
211 AlternativeServiceInfo
ParseAlternativeServiceDict(
212 const base::DictionaryValue
& alternative_service_dict
,
213 const std::string
& server_str
);
214 bool AddToAlternativeServiceMap(
215 const HostPortPair
& server
,
216 const base::DictionaryValue
& server_dict
,
217 AlternativeServiceMap
* alternative_service_map
);
218 bool AddToNetworkStatsMap(const HostPortPair
& server
,
219 const base::DictionaryValue
& server_dict
,
220 ServerNetworkStatsMap
* network_stats_map
);
222 void SaveSpdySettingsToServerPrefs(const SettingsMap
* spdy_settings_map
,
223 base::DictionaryValue
* server_pref_dict
);
224 void SaveAlternativeServiceToServerPrefs(
225 const AlternativeServiceInfo
* alternative_service_info
,
226 base::DictionaryValue
* server_pref_dict
);
227 void SaveNetworkStatsToServerPrefs(
228 const ServerNetworkStats
* server_network_stats
,
229 base::DictionaryValue
* server_pref_dict
);
231 void SaveSupportsQuicToPrefs(
232 const IPAddressNumber
* last_quic_address
,
233 base::DictionaryValue
* http_server_properties_dict
);
239 const scoped_refptr
<base::SequencedTaskRunner
> pref_task_runner_
;
241 base::WeakPtr
<HttpServerPropertiesManager
> pref_weak_ptr_
;
243 // Used to post cache update tasks.
244 scoped_ptr
<base::OneShotTimer
<HttpServerPropertiesManager
> >
245 pref_cache_update_timer_
;
247 // Used to track the spdy servers changes.
248 PrefChangeRegistrar pref_change_registrar_
;
249 PrefService
* pref_service_
; // Weak.
257 const scoped_refptr
<base::SequencedTaskRunner
> network_task_runner_
;
259 // Used to post |prefs::kHttpServerProperties| pref update tasks.
260 scoped_ptr
<base::OneShotTimer
<HttpServerPropertiesManager
> >
261 network_prefs_update_timer_
;
263 scoped_ptr
<HttpServerPropertiesImpl
> http_server_properties_impl_
;
265 // Used to get |weak_ptr_| to self on the pref thread.
266 scoped_ptr
<base::WeakPtrFactory
<HttpServerPropertiesManager
> >
267 pref_weak_ptr_factory_
;
269 // Used to get |weak_ptr_| to self on the network thread.
270 scoped_ptr
<base::WeakPtrFactory
<HttpServerPropertiesManager
> >
271 network_weak_ptr_factory_
;
273 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager
);
278 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_