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 void SetSupportsSpdy(const HostPortPair
& server
, bool support_spdy
) override
;
85 bool RequiresHTTP11(const HostPortPair
& server
) override
;
86 void SetHTTP11Required(const HostPortPair
& server
) override
;
87 void MaybeForceHTTP11(const HostPortPair
& server
,
88 SSLConfig
* ssl_config
) override
;
89 AlternativeService
GetAlternativeService(const HostPortPair
& origin
) override
;
90 void SetAlternativeService(const HostPortPair
& origin
,
91 const AlternativeService
& alternative_service
,
92 double alternative_probability
) override
;
93 void MarkAlternativeServiceBroken(
94 const AlternativeService
& alternative_service
) override
;
95 void MarkAlternativeServiceRecentlyBroken(
96 const AlternativeService
& alternative_service
) override
;
97 bool IsAlternativeServiceBroken(
98 const AlternativeService
& alternative_service
) const override
;
99 bool WasAlternativeServiceRecentlyBroken(
100 const AlternativeService
& alternative_service
) override
;
101 void ConfirmAlternativeService(
102 const AlternativeService
& alternative_service
) override
;
103 void ClearAlternativeService(const HostPortPair
& origin
) override
;
104 const AlternativeServiceMap
& alternative_service_map() const override
;
105 base::Value
* GetAlternativeServiceInfoAsValue() const override
;
106 void SetAlternativeServiceProbabilityThreshold(double threshold
) override
;
107 const SettingsMap
& GetSpdySettings(
108 const HostPortPair
& host_port_pair
) override
;
109 bool SetSpdySetting(const HostPortPair
& host_port_pair
,
111 SpdySettingsFlags flags
,
112 uint32 value
) override
;
113 void ClearSpdySettings(const HostPortPair
& host_port_pair
) override
;
114 void ClearAllSpdySettings() override
;
115 const SpdySettingsMap
& spdy_settings_map() const override
;
116 bool GetSupportsQuic(IPAddressNumber
* last_address
) const override
;
117 void SetSupportsQuic(bool used_quic
,
118 const IPAddressNumber
& last_address
) override
;
119 void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
120 ServerNetworkStats stats
) override
;
121 const ServerNetworkStats
* GetServerNetworkStats(
122 const HostPortPair
& host_port_pair
) override
;
123 const ServerNetworkStatsMap
& server_network_stats_map() const override
;
126 // --------------------
127 // SPDY related methods
129 // These are used to delay updating of the cached data in
130 // |http_server_properties_impl_| while the preferences are changing, and
131 // execute only one update per simultaneous prefs changes.
132 void ScheduleUpdateCacheOnPrefThread();
134 // Starts the timers to update the cached prefs. This are overridden in tests
135 // to prevent the delay.
136 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay
);
138 // Update cached prefs in |http_server_properties_impl_| with data from
139 // preferences. It gets the data on pref thread and calls
140 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on
142 virtual void UpdateCacheFromPrefsOnPrefThread();
144 // Starts the update of cached prefs in |http_server_properties_impl_| on the
145 // network thread. Protected for testing.
146 void UpdateCacheFromPrefsOnNetworkThread(
147 std::vector
<std::string
>* spdy_servers
,
148 SpdySettingsMap
* spdy_settings_map
,
149 AlternativeServiceMap
* alternative_service_map
,
150 IPAddressNumber
* last_quic_address
,
151 ServerNetworkStatsMap
* server_network_stats_map
,
152 bool detected_corrupted_prefs
);
154 // These are used to delay updating the preferences when cached data in
155 // |http_server_properties_impl_| is changing, and execute only one update per
156 // simultaneous spdy_servers or spdy_settings or alternative_service changes.
157 void ScheduleUpdatePrefsOnNetworkThread();
159 // Starts the timers to update the prefs from cache. This are overridden in
160 // tests to prevent the delay.
161 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay
);
163 // Update prefs::kHttpServerProperties in preferences with the cached data
164 // from |http_server_properties_impl_|. This gets the data on network thread
165 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref
167 void UpdatePrefsFromCacheOnNetworkThread();
169 // Same as above, but fires an optional |completion| callback on pref thread
170 // when finished. Virtual for testing.
171 virtual void UpdatePrefsFromCacheOnNetworkThread(
172 const base::Closure
& completion
);
174 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an
175 // optional |completion| callback when finished. Protected for testing.
176 void UpdatePrefsOnPrefThread(base::ListValue
* spdy_server_list
,
177 SpdySettingsMap
* spdy_settings_map
,
178 AlternativeServiceMap
* alternative_service_map
,
179 IPAddressNumber
* last_quic_address
,
180 ServerNetworkStatsMap
* server_network_stats_map
,
181 const base::Closure
& completion
);
184 void OnHttpServerPropertiesChanged();
186 bool ReadSupportsQuic(const base::DictionaryValue
& server_dict
,
187 IPAddressNumber
* last_quic_address
);
188 void AddToSpdySettingsMap(const HostPortPair
& server
,
189 const base::DictionaryValue
& server_dict
,
190 SpdySettingsMap
* spdy_settings_map
);
191 AlternativeServiceInfo
ParseAlternativeServiceDict(
192 const base::DictionaryValue
& alternative_service_dict
,
193 const std::string
& server_str
);
194 bool AddToAlternativeServiceMap(
195 const HostPortPair
& server
,
196 const base::DictionaryValue
& server_dict
,
197 AlternativeServiceMap
* alternative_service_map
);
198 bool AddToNetworkStatsMap(const HostPortPair
& server
,
199 const base::DictionaryValue
& server_dict
,
200 ServerNetworkStatsMap
* network_stats_map
);
202 void SaveSpdySettingsToServerPrefs(const SettingsMap
* spdy_settings_map
,
203 base::DictionaryValue
* server_pref_dict
);
204 void SaveAlternativeServiceToServerPrefs(
205 const AlternativeServiceInfo
* alternative_service_info
,
206 base::DictionaryValue
* server_pref_dict
);
207 void SaveNetworkStatsToServerPrefs(
208 const ServerNetworkStats
* server_network_stats
,
209 base::DictionaryValue
* server_pref_dict
);
211 void SaveSupportsQuicToPrefs(
212 const IPAddressNumber
* last_quic_address
,
213 base::DictionaryValue
* http_server_properties_dict
);
219 const scoped_refptr
<base::SequencedTaskRunner
> pref_task_runner_
;
221 base::WeakPtr
<HttpServerPropertiesManager
> pref_weak_ptr_
;
223 // Used to post cache update tasks.
224 scoped_ptr
<base::OneShotTimer
<HttpServerPropertiesManager
> >
225 pref_cache_update_timer_
;
227 // Used to track the spdy servers changes.
228 PrefChangeRegistrar pref_change_registrar_
;
229 PrefService
* pref_service_
; // Weak.
237 const scoped_refptr
<base::SequencedTaskRunner
> network_task_runner_
;
239 // Used to post |prefs::kHttpServerProperties| pref update tasks.
240 scoped_ptr
<base::OneShotTimer
<HttpServerPropertiesManager
> >
241 network_prefs_update_timer_
;
243 scoped_ptr
<HttpServerPropertiesImpl
> http_server_properties_impl_
;
245 // Used to get |weak_ptr_| to self on the pref thread.
246 scoped_ptr
<base::WeakPtrFactory
<HttpServerPropertiesManager
> >
247 pref_weak_ptr_factory_
;
249 // Used to get |weak_ptr_| to self on the network thread.
250 scoped_ptr
<base::WeakPtrFactory
<HttpServerPropertiesManager
> >
251 network_weak_ptr_factory_
;
253 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager
);
258 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_