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_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "base/timer/timer.h"
16 #include "base/values.h"
17 #include "net/base/host_port_pair.h"
18 #include "net/http/http_server_properties.h"
19 #include "net/http/http_server_properties_impl.h"
24 class SequencedTaskRunner;
29 ////////////////////////////////////////////////////////////////////////////////
30 // HttpServerPropertiesManager
32 // The manager for creating and updating an HttpServerProperties (for example it
33 // tracks if a server supports SPDY or not).
35 // This class interacts with both the pref thread, where notifications of pref
36 // changes are received from, and the network thread, which owns it, and it
37 // persists the changes from network stack whether server supports SPDY or not.
39 // It must be constructed on the pref thread, to set up |pref_task_runner_| and
40 // the prefs listeners.
42 // ShutdownOnPrefThread must be called from pref thread before destruction, to
43 // release the prefs listeners on the pref thread.
45 // Class requires that update tasks from the Pref thread can post safely to the
46 // network thread, so the destruction order must guarantee that if |this|
47 // exists in pref thread, then a potential destruction on network thread will
48 // come after any task posted to network thread from that method on pref thread.
49 // This is used to go through network thread before the actual update starts,
50 // and grab a WeakPtr.
51 class NET_EXPORT HttpServerPropertiesManager : public HttpServerProperties {
53 // Create an instance of the HttpServerPropertiesManager. The lifetime of the
54 // PrefService objects must be longer than that of the
55 // HttpServerPropertiesManager object. Must be constructed on the Pref thread.
56 HttpServerPropertiesManager(
57 PrefService* pref_service,
58 const char* pref_path,
59 scoped_refptr<base::SequencedTaskRunner> network_task_runner);
60 virtual ~HttpServerPropertiesManager();
62 // Initialize on Network thread.
63 void InitializeOnNetworkThread();
65 // Prepare for shutdown. Must be called on the Pref thread before destruction.
66 void ShutdownOnPrefThread();
68 // Helper function for unit tests to set the version in the dictionary.
69 static void SetVersion(base::DictionaryValue* http_server_properties_dict,
72 // Deletes all data. Works asynchronously, but if a |completion| callback is
73 // provided, it will be fired on the pref thread when everything is done.
74 void Clear(const base::Closure& completion);
76 // ----------------------------------
77 // HttpServerProperties methods:
78 // ----------------------------------
80 // Gets a weak pointer for this object.
81 virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() OVERRIDE;
83 // Deletes all data. Works asynchronously.
84 virtual void Clear() OVERRIDE;
86 // Returns true if |server| supports SPDY. Should only be called from IO
88 virtual bool SupportsSpdy(const HostPortPair& server) OVERRIDE;
90 // Add |server| as the SPDY server which supports SPDY protocol into the
91 // persisitent store. Should only be called from IO thread.
92 virtual void SetSupportsSpdy(const HostPortPair& server,
93 bool support_spdy) OVERRIDE;
95 // Returns true if |server| has an Alternate-Protocol header.
96 virtual bool HasAlternateProtocol(const HostPortPair& server) OVERRIDE;
98 // Returns the Alternate-Protocol and port for |server|.
99 // HasAlternateProtocol(server) must be true.
100 virtual AlternateProtocolInfo GetAlternateProtocol(
101 const HostPortPair& server) OVERRIDE;
103 // Sets the Alternate-Protocol for |server|.
104 virtual void SetAlternateProtocol(
105 const HostPortPair& server,
106 uint16 alternate_port,
107 AlternateProtocol alternate_protocol,
108 double alternate_probability) OVERRIDE;
110 // Sets the Alternate-Protocol for |server| to be BROKEN.
111 virtual void SetBrokenAlternateProtocol(const HostPortPair& server) OVERRIDE;
113 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
114 virtual bool WasAlternateProtocolRecentlyBroken(
115 const HostPortPair& server) OVERRIDE;
117 // Confirms that Alternate-Protocol for |server| is working.
118 virtual void ConfirmAlternateProtocol(const HostPortPair& server) OVERRIDE;
120 // Clears the Alternate-Protocol for |server|.
121 virtual void ClearAlternateProtocol(const HostPortPair& server) OVERRIDE;
123 // Returns all Alternate-Protocol mappings.
124 virtual const AlternateProtocolMap& alternate_protocol_map() const OVERRIDE;
126 virtual void SetAlternateProtocolExperiment(
127 AlternateProtocolExperiment experiment) OVERRIDE;
129 virtual void SetAlternateProtocolProbabilityThreshold(
130 double threshold) OVERRIDE;
132 virtual AlternateProtocolExperiment GetAlternateProtocolExperiment()
135 // Gets a reference to the SettingsMap stored for a host.
136 // If no settings are stored, returns an empty SettingsMap.
137 virtual const SettingsMap& GetSpdySettings(
138 const HostPortPair& host_port_pair) OVERRIDE;
140 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
141 // is to be persisted.
142 virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
144 SpdySettingsFlags flags,
145 uint32 value) OVERRIDE;
147 // Clears all SPDY settings for a host.
148 virtual void ClearSpdySettings(const HostPortPair& host_port_pair) OVERRIDE;
150 // Clears all SPDY settings for all hosts.
151 virtual void ClearAllSpdySettings() OVERRIDE;
153 // Returns all SPDY persistent settings.
154 virtual const SpdySettingsMap& spdy_settings_map() const OVERRIDE;
156 // Methods for SupportsQuic.
157 virtual SupportsQuic GetSupportsQuic(
158 const HostPortPair& host_port_pair) const OVERRIDE;
160 virtual void SetSupportsQuic(const HostPortPair& host_port_pair,
162 const std::string& address) OVERRIDE;
164 virtual const SupportsQuicMap& supports_quic_map() const OVERRIDE;
166 virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
167 NetworkStats stats) OVERRIDE;
169 virtual const NetworkStats* GetServerNetworkStats(
170 const HostPortPair& host_port_pair) const OVERRIDE;
173 // --------------------
174 // SPDY related methods
176 // These are used to delay updating of the cached data in
177 // |http_server_properties_impl_| while the preferences are changing, and
178 // execute only one update per simultaneous prefs changes.
179 void ScheduleUpdateCacheOnPrefThread();
181 // Starts the timers to update the cached prefs. This are overridden in tests
182 // to prevent the delay.
183 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay);
185 // Update cached prefs in |http_server_properties_impl_| with data from
186 // preferences. It gets the data on pref thread and calls
187 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on
189 virtual void UpdateCacheFromPrefsOnPrefThread();
191 // Starts the update of cached prefs in |http_server_properties_impl_| on the
192 // network thread. Protected for testing.
193 void UpdateCacheFromPrefsOnNetworkThread(
194 std::vector<std::string>* spdy_servers,
195 SpdySettingsMap* spdy_settings_map,
196 AlternateProtocolMap* alternate_protocol_map,
197 AlternateProtocolExperiment alternate_protocol_experiment,
198 SupportsQuicMap* supports_quic_map,
199 bool detected_corrupted_prefs);
201 // These are used to delay updating the preferences when cached data in
202 // |http_server_properties_impl_| is changing, and execute only one update per
203 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes.
204 void ScheduleUpdatePrefsOnNetworkThread();
206 // Starts the timers to update the prefs from cache. This are overridden in
207 // tests to prevent the delay.
208 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay);
210 // Update prefs::kHttpServerProperties in preferences with the cached data
211 // from |http_server_properties_impl_|. This gets the data on network thread
212 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref
214 void UpdatePrefsFromCacheOnNetworkThread();
216 // Same as above, but fires an optional |completion| callback on pref thread
217 // when finished. Virtual for testing.
218 virtual void UpdatePrefsFromCacheOnNetworkThread(
219 const base::Closure& completion);
221 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an
222 // optional |completion| callback when finished. Protected for testing.
223 void UpdatePrefsOnPrefThread(base::ListValue* spdy_server_list,
224 SpdySettingsMap* spdy_settings_map,
225 AlternateProtocolMap* alternate_protocol_map,
226 SupportsQuicMap* supports_quic_map,
227 const base::Closure& completion);
230 void OnHttpServerPropertiesChanged();
236 const scoped_refptr<base::SequencedTaskRunner> pref_task_runner_;
238 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_;
240 // Used to post cache update tasks.
241 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
242 pref_cache_update_timer_;
244 // Used to track the spdy servers changes.
245 PrefChangeRegistrar pref_change_registrar_;
246 PrefService* pref_service_; // Weak.
254 const scoped_refptr<base::SequencedTaskRunner> network_task_runner_;
256 // Used to post |prefs::kHttpServerProperties| pref update tasks.
257 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
258 network_prefs_update_timer_;
260 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_;
262 // Used to get |weak_ptr_| to self on the pref thread.
263 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
264 pref_weak_ptr_factory_;
266 // Used to get |weak_ptr_| to self on the network thread.
267 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
268 network_weak_ptr_factory_;
270 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager);
275 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_