Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / http / http_server_properties_manager.h
blobd332aa8fc546e8696b77c8e96f92481d4c1a214b
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_
8 #include <string>
9 #include <vector>
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"
21 class PrefService;
23 namespace base {
24 class SequencedTaskRunner;
27 namespace net {
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 {
52 public:
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,
70 int version_number);
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
87 // thread.
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()
133 const OVERRIDE;
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,
143 SpdySettingsIds id,
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 virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
157 NetworkStats stats) OVERRIDE;
159 virtual const NetworkStats* GetServerNetworkStats(
160 const HostPortPair& host_port_pair) const OVERRIDE;
162 protected:
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
178 // network thread.
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 AlternateProtocolExperiment alternate_protocol_experiment,
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
202 // thread.
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 const base::Closure& completion);
217 private:
218 void OnHttpServerPropertiesChanged();
220 // -----------
221 // Pref thread
222 // -----------
224 const scoped_refptr<base::SequencedTaskRunner> pref_task_runner_;
226 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_;
228 // Used to post cache update tasks.
229 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
230 pref_cache_update_timer_;
232 // Used to track the spdy servers changes.
233 PrefChangeRegistrar pref_change_registrar_;
234 PrefService* pref_service_; // Weak.
235 bool setting_prefs_;
236 const char* path_;
238 // --------------
239 // Network thread
240 // --------------
242 const scoped_refptr<base::SequencedTaskRunner> network_task_runner_;
244 // Used to post |prefs::kHttpServerProperties| pref update tasks.
245 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
246 network_prefs_update_timer_;
248 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_;
250 // Used to get |weak_ptr_| to self on the pref thread.
251 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
252 pref_weak_ptr_factory_;
254 // Used to get |weak_ptr_| to self on the network thread.
255 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
256 network_weak_ptr_factory_;
258 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager);
261 } // namespace net
263 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_