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
) 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 AlternateProtocolMap
& alternate_protocol_map() const override
;
105 void SetAlternateProtocolProbabilityThreshold(double threshold
) override
;
106 const SettingsMap
& GetSpdySettings(
107 const HostPortPair
& host_port_pair
) override
;
108 bool SetSpdySetting(const HostPortPair
& host_port_pair
,
110 SpdySettingsFlags flags
,
111 uint32 value
) override
;
112 void ClearSpdySettings(const HostPortPair
& host_port_pair
) override
;
113 void ClearAllSpdySettings() override
;
114 const SpdySettingsMap
& spdy_settings_map() const override
;
115 bool GetSupportsQuic(IPAddressNumber
* last_address
) const override
;
116 void SetSupportsQuic(bool used_quic
,
117 const IPAddressNumber
& last_address
) override
;
118 void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
119 ServerNetworkStats stats
) override
;
120 const ServerNetworkStats
* GetServerNetworkStats(
121 const HostPortPair
& host_port_pair
) override
;
122 const ServerNetworkStatsMap
& server_network_stats_map() const override
;
125 // --------------------
126 // SPDY related methods
128 // These are used to delay updating of the cached data in
129 // |http_server_properties_impl_| while the preferences are changing, and
130 // execute only one update per simultaneous prefs changes.
131 void ScheduleUpdateCacheOnPrefThread();
133 // Starts the timers to update the cached prefs. This are overridden in tests
134 // to prevent the delay.
135 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay
);
137 // Update cached prefs in |http_server_properties_impl_| with data from
138 // preferences. It gets the data on pref thread and calls
139 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on
141 virtual void UpdateCacheFromPrefsOnPrefThread();
143 // Starts the update of cached prefs in |http_server_properties_impl_| on the
144 // network thread. Protected for testing.
145 void UpdateCacheFromPrefsOnNetworkThread(
146 std::vector
<std::string
>* spdy_servers
,
147 SpdySettingsMap
* spdy_settings_map
,
148 AlternateProtocolMap
* alternate_protocol_map
,
149 IPAddressNumber
* last_quic_address
,
150 ServerNetworkStatsMap
* server_network_stats_map
,
151 bool detected_corrupted_prefs
);
153 // These are used to delay updating the preferences when cached data in
154 // |http_server_properties_impl_| is changing, and execute only one update per
155 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes.
156 void ScheduleUpdatePrefsOnNetworkThread();
158 // Starts the timers to update the prefs from cache. This are overridden in
159 // tests to prevent the delay.
160 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay
);
162 // Update prefs::kHttpServerProperties in preferences with the cached data
163 // from |http_server_properties_impl_|. This gets the data on network thread
164 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref
166 void UpdatePrefsFromCacheOnNetworkThread();
168 // Same as above, but fires an optional |completion| callback on pref thread
169 // when finished. Virtual for testing.
170 virtual void UpdatePrefsFromCacheOnNetworkThread(
171 const base::Closure
& completion
);
173 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an
174 // optional |completion| callback when finished. Protected for testing.
175 void UpdatePrefsOnPrefThread(base::ListValue
* spdy_server_list
,
176 SpdySettingsMap
* spdy_settings_map
,
177 AlternateProtocolMap
* alternate_protocol_map
,
178 IPAddressNumber
* last_quic_address
,
179 ServerNetworkStatsMap
* server_network_stats_map
,
180 const base::Closure
& completion
);
183 void OnHttpServerPropertiesChanged();
185 bool ReadSupportsQuic(const base::DictionaryValue
& server_dict
,
186 IPAddressNumber
* last_quic_address
);
187 void AddToSpdySettingsMap(const HostPortPair
& server
,
188 const base::DictionaryValue
& server_dict
,
189 SpdySettingsMap
* spdy_settings_map
);
190 AlternateProtocolInfo
ParseAlternateProtocolDict(
191 const base::DictionaryValue
& alternate_protocol_dict
,
192 const std::string
& server_str
);
193 bool AddToAlternateProtocolMap(const HostPortPair
& server
,
194 const base::DictionaryValue
& server_dict
,
195 AlternateProtocolMap
* alternate_protocol_map
);
196 bool AddToNetworkStatsMap(const HostPortPair
& server
,
197 const base::DictionaryValue
& server_dict
,
198 ServerNetworkStatsMap
* network_stats_map
);
200 void SaveSpdySettingsToServerPrefs(const SettingsMap
* spdy_settings_map
,
201 base::DictionaryValue
* server_pref_dict
);
202 void SaveAlternateProtocolToServerPrefs(
203 const AlternateProtocolInfo
* port_alternate_protocol
,
204 base::DictionaryValue
* server_pref_dict
);
205 void SaveNetworkStatsToServerPrefs(
206 const ServerNetworkStats
* server_network_stats
,
207 base::DictionaryValue
* server_pref_dict
);
209 void SaveSupportsQuicToPrefs(
210 const IPAddressNumber
* last_quic_address
,
211 base::DictionaryValue
* http_server_properties_dict
);
217 const scoped_refptr
<base::SequencedTaskRunner
> pref_task_runner_
;
219 base::WeakPtr
<HttpServerPropertiesManager
> pref_weak_ptr_
;
221 // Used to post cache update tasks.
222 scoped_ptr
<base::OneShotTimer
<HttpServerPropertiesManager
> >
223 pref_cache_update_timer_
;
225 // Used to track the spdy servers changes.
226 PrefChangeRegistrar pref_change_registrar_
;
227 PrefService
* pref_service_
; // Weak.
235 const scoped_refptr
<base::SequencedTaskRunner
> network_task_runner_
;
237 // Used to post |prefs::kHttpServerProperties| pref update tasks.
238 scoped_ptr
<base::OneShotTimer
<HttpServerPropertiesManager
> >
239 network_prefs_update_timer_
;
241 scoped_ptr
<HttpServerPropertiesImpl
> http_server_properties_impl_
;
243 // Used to get |weak_ptr_| to self on the pref thread.
244 scoped_ptr
<base::WeakPtrFactory
<HttpServerPropertiesManager
> >
245 pref_weak_ptr_factory_
;
247 // Used to get |weak_ptr_| to self on the network thread.
248 scoped_ptr
<base::WeakPtrFactory
<HttpServerPropertiesManager
> >
249 network_weak_ptr_factory_
;
251 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager
);
256 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_