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 ~HttpServerPropertiesManager() override;
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 base::WeakPtr<HttpServerProperties> GetWeakPtr() override;
83 // Deletes all data. Works asynchronously.
84 void Clear() override;
86 // Returns true if |server| supports SPDY. Should only be called from IO
88 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 void SetSupportsSpdy(const HostPortPair& server, bool support_spdy) override;
94 // Returns true if |server| has an Alternate-Protocol header.
95 bool HasAlternateProtocol(const HostPortPair& server) override;
97 // Returns the Alternate-Protocol and port for |server|.
98 // HasAlternateProtocol(server) must be true.
99 AlternateProtocolInfo GetAlternateProtocol(
100 const HostPortPair& server) override;
102 // Sets the Alternate-Protocol for |server|.
103 void SetAlternateProtocol(const HostPortPair& server,
104 uint16 alternate_port,
105 AlternateProtocol alternate_protocol,
106 double alternate_probability) override;
108 // Sets the Alternate-Protocol for |server| to be BROKEN.
109 void SetBrokenAlternateProtocol(const HostPortPair& server) override;
111 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
112 bool WasAlternateProtocolRecentlyBroken(const HostPortPair& server) override;
114 // Confirms that Alternate-Protocol for |server| is working.
115 void ConfirmAlternateProtocol(const HostPortPair& server) override;
117 // Clears the Alternate-Protocol for |server|.
118 void ClearAlternateProtocol(const HostPortPair& server) override;
120 // Returns all Alternate-Protocol mappings.
121 const AlternateProtocolMap& alternate_protocol_map() const override;
123 void SetAlternateProtocolProbabilityThreshold(double threshold) override;
125 // Gets a reference to the SettingsMap stored for a host.
126 // If no settings are stored, returns an empty SettingsMap.
127 const SettingsMap& GetSpdySettings(
128 const HostPortPair& host_port_pair) override;
130 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
131 // is to be persisted.
132 bool SetSpdySetting(const HostPortPair& host_port_pair,
134 SpdySettingsFlags flags,
135 uint32 value) override;
137 // Clears all SPDY settings for a host.
138 void ClearSpdySettings(const HostPortPair& host_port_pair) override;
140 // Clears all SPDY settings for all hosts.
141 void ClearAllSpdySettings() override;
143 // Returns all SPDY persistent settings.
144 const SpdySettingsMap& spdy_settings_map() const override;
146 // Methods for SupportsQuic.
147 SupportsQuic GetSupportsQuic(
148 const HostPortPair& host_port_pair) const override;
150 void SetSupportsQuic(const HostPortPair& host_port_pair,
152 const std::string& address) override;
154 const SupportsQuicMap& supports_quic_map() const override;
156 void SetServerNetworkStats(const HostPortPair& host_port_pair,
157 NetworkStats stats) override;
159 const NetworkStats* GetServerNetworkStats(
160 const HostPortPair& host_port_pair) const override;
163 // --------------------
164 // SPDY related methods
166 // These are used to delay updating of the cached data in
167 // |http_server_properties_impl_| while the preferences are changing, and
168 // execute only one update per simultaneous prefs changes.
169 void ScheduleUpdateCacheOnPrefThread();
171 // Starts the timers to update the cached prefs. This are overridden in tests
172 // to prevent the delay.
173 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay);
175 // Update cached prefs in |http_server_properties_impl_| with data from
176 // preferences. It gets the data on pref thread and calls
177 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on
179 virtual void UpdateCacheFromPrefsOnPrefThread();
181 // Starts the update of cached prefs in |http_server_properties_impl_| on the
182 // network thread. Protected for testing.
183 void UpdateCacheFromPrefsOnNetworkThread(
184 std::vector<std::string>* spdy_servers,
185 SpdySettingsMap* spdy_settings_map,
186 AlternateProtocolMap* alternate_protocol_map,
187 SupportsQuicMap* supports_quic_map,
188 bool detected_corrupted_prefs);
190 // These are used to delay updating the preferences when cached data in
191 // |http_server_properties_impl_| is changing, and execute only one update per
192 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes.
193 void ScheduleUpdatePrefsOnNetworkThread();
195 // Starts the timers to update the prefs from cache. This are overridden in
196 // tests to prevent the delay.
197 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay);
199 // Update prefs::kHttpServerProperties in preferences with the cached data
200 // from |http_server_properties_impl_|. This gets the data on network thread
201 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref
203 void UpdatePrefsFromCacheOnNetworkThread();
205 // Same as above, but fires an optional |completion| callback on pref thread
206 // when finished. Virtual for testing.
207 virtual void UpdatePrefsFromCacheOnNetworkThread(
208 const base::Closure& completion);
210 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an
211 // optional |completion| callback when finished. Protected for testing.
212 void UpdatePrefsOnPrefThread(base::ListValue* spdy_server_list,
213 SpdySettingsMap* spdy_settings_map,
214 AlternateProtocolMap* alternate_protocol_map,
215 SupportsQuicMap* supports_quic_map,
216 const base::Closure& completion);
219 void OnHttpServerPropertiesChanged();
225 const scoped_refptr<base::SequencedTaskRunner> pref_task_runner_;
227 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_;
229 // Used to post cache update tasks.
230 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
231 pref_cache_update_timer_;
233 // Used to track the spdy servers changes.
234 PrefChangeRegistrar pref_change_registrar_;
235 PrefService* pref_service_; // Weak.
243 const scoped_refptr<base::SequencedTaskRunner> network_task_runner_;
245 // Used to post |prefs::kHttpServerProperties| pref update tasks.
246 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
247 network_prefs_update_timer_;
249 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_;
251 // Used to get |weak_ptr_| to self on the pref thread.
252 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
253 pref_weak_ptr_factory_;
255 // Used to get |weak_ptr_| to self on the network thread.
256 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
257 network_weak_ptr_factory_;
259 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager);
264 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_