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 // Gets a weak pointer for this object.
82 base::WeakPtr
<HttpServerProperties
> GetWeakPtr() override
;
84 // Deletes all data. Works asynchronously.
85 void Clear() override
;
87 // Returns true if |server| supports SPDY. Should only be called from IO
89 bool SupportsSpdy(const HostPortPair
& server
) override
;
91 // Add |server| as the SPDY server which supports SPDY protocol into the
92 // persisitent store. Should only be called from IO thread.
93 void SetSupportsSpdy(const HostPortPair
& server
, bool support_spdy
) override
;
95 // Returns true if |server| has an Alternate-Protocol header.
96 bool HasAlternateProtocol(const HostPortPair
& server
) override
;
98 // Returns the Alternate-Protocol and port for |server|.
99 // HasAlternateProtocol(server) must be true.
100 AlternateProtocolInfo
GetAlternateProtocol(
101 const HostPortPair
& server
) override
;
103 // Sets the Alternate-Protocol for |server|.
104 void SetAlternateProtocol(const HostPortPair
& server
,
105 uint16 alternate_port
,
106 AlternateProtocol alternate_protocol
,
107 double alternate_probability
) override
;
109 // Sets the Alternate-Protocol for |server| to be BROKEN.
110 void SetBrokenAlternateProtocol(const HostPortPair
& server
) override
;
112 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
113 bool WasAlternateProtocolRecentlyBroken(const HostPortPair
& server
) override
;
115 // Confirms that Alternate-Protocol for |server| is working.
116 void ConfirmAlternateProtocol(const HostPortPair
& server
) override
;
118 // Clears the Alternate-Protocol for |server|.
119 void ClearAlternateProtocol(const HostPortPair
& server
) override
;
121 // Returns all Alternate-Protocol mappings.
122 const AlternateProtocolMap
& alternate_protocol_map() const override
;
124 void SetAlternateProtocolProbabilityThreshold(double threshold
) override
;
126 // Gets a reference to the SettingsMap stored for a host.
127 // If no settings are stored, returns an empty SettingsMap.
128 const SettingsMap
& GetSpdySettings(
129 const HostPortPair
& host_port_pair
) override
;
131 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
132 // is to be persisted.
133 bool SetSpdySetting(const HostPortPair
& host_port_pair
,
135 SpdySettingsFlags flags
,
136 uint32 value
) override
;
138 // Clears all SPDY settings for a host.
139 void ClearSpdySettings(const HostPortPair
& host_port_pair
) override
;
141 // Clears all SPDY settings for all hosts.
142 void ClearAllSpdySettings() override
;
144 // Returns all SPDY persistent settings.
145 const SpdySettingsMap
& spdy_settings_map() const override
;
147 // Methods for SupportsQuic.
148 SupportsQuic
GetSupportsQuic(
149 const HostPortPair
& host_port_pair
) const override
;
151 void SetSupportsQuic(const HostPortPair
& host_port_pair
,
153 const std::string
& address
) override
;
155 const SupportsQuicMap
& supports_quic_map() const override
;
157 void SetServerNetworkStats(const HostPortPair
& host_port_pair
,
158 NetworkStats stats
) override
;
160 const NetworkStats
* GetServerNetworkStats(
161 const HostPortPair
& host_port_pair
) const override
;
164 // --------------------
165 // SPDY related methods
167 // These are used to delay updating of the cached data in
168 // |http_server_properties_impl_| while the preferences are changing, and
169 // execute only one update per simultaneous prefs changes.
170 void ScheduleUpdateCacheOnPrefThread();
172 // Starts the timers to update the cached prefs. This are overridden in tests
173 // to prevent the delay.
174 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay
);
176 // Update cached prefs in |http_server_properties_impl_| with data from
177 // preferences. It gets the data on pref thread and calls
178 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on
180 virtual void UpdateCacheFromPrefsOnPrefThread();
182 // Starts the update of cached prefs in |http_server_properties_impl_| on the
183 // network thread. Protected for testing.
184 void UpdateCacheFromPrefsOnNetworkThread(
185 std::vector
<std::string
>* spdy_servers
,
186 SpdySettingsMap
* spdy_settings_map
,
187 AlternateProtocolMap
* alternate_protocol_map
,
188 SupportsQuicMap
* supports_quic_map
,
189 bool detected_corrupted_prefs
);
191 // These are used to delay updating the preferences when cached data in
192 // |http_server_properties_impl_| is changing, and execute only one update per
193 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes.
194 void ScheduleUpdatePrefsOnNetworkThread();
196 // Starts the timers to update the prefs from cache. This are overridden in
197 // tests to prevent the delay.
198 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay
);
200 // Update prefs::kHttpServerProperties in preferences with the cached data
201 // from |http_server_properties_impl_|. This gets the data on network thread
202 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref
204 void UpdatePrefsFromCacheOnNetworkThread();
206 // Same as above, but fires an optional |completion| callback on pref thread
207 // when finished. Virtual for testing.
208 virtual void UpdatePrefsFromCacheOnNetworkThread(
209 const base::Closure
& completion
);
211 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an
212 // optional |completion| callback when finished. Protected for testing.
213 void UpdatePrefsOnPrefThread(base::ListValue
* spdy_server_list
,
214 SpdySettingsMap
* spdy_settings_map
,
215 AlternateProtocolMap
* alternate_protocol_map
,
216 SupportsQuicMap
* supports_quic_map
,
217 const base::Closure
& completion
);
220 void OnHttpServerPropertiesChanged();
226 const scoped_refptr
<base::SequencedTaskRunner
> pref_task_runner_
;
228 base::WeakPtr
<HttpServerPropertiesManager
> pref_weak_ptr_
;
230 // Used to post cache update tasks.
231 scoped_ptr
<base::OneShotTimer
<HttpServerPropertiesManager
> >
232 pref_cache_update_timer_
;
234 // Used to track the spdy servers changes.
235 PrefChangeRegistrar pref_change_registrar_
;
236 PrefService
* pref_service_
; // Weak.
244 const scoped_refptr
<base::SequencedTaskRunner
> network_task_runner_
;
246 // Used to post |prefs::kHttpServerProperties| pref update tasks.
247 scoped_ptr
<base::OneShotTimer
<HttpServerPropertiesManager
> >
248 network_prefs_update_timer_
;
250 scoped_ptr
<HttpServerPropertiesImpl
> http_server_properties_impl_
;
252 // Used to get |weak_ptr_| to self on the pref thread.
253 scoped_ptr
<base::WeakPtrFactory
<HttpServerPropertiesManager
> >
254 pref_weak_ptr_factory_
;
256 // Used to get |weak_ptr_| to self on the network thread.
257 scoped_ptr
<base::WeakPtrFactory
<HttpServerPropertiesManager
> >
258 network_weak_ptr_factory_
;
260 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager
);
265 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_