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.
4
5#ifndef NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_
6#define NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_
7
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"
20
21class PrefService;
22
23namespace base {
24class SequencedTaskRunner;
25}
26
27namespace net {
28
29////////////////////////////////////////////////////////////////////////////////
30// HttpServerPropertiesManager
31
32// The manager for creating and updating an HttpServerProperties (for example it
33// tracks if a server supports SPDY or not).
34//
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.
38//
39// It must be constructed on the pref thread, to set up |pref_task_runner_| and
40// the prefs listeners.
41//
42// ShutdownOnPrefThread must be called from pref thread before destruction, to
43// release the prefs listeners on the pref thread.
44//
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.
51class 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();
61
62  // Initialize on Network thread.
63  void InitializeOnNetworkThread();
64
65  // Prepare for shutdown. Must be called on the Pref thread before destruction.
66  void ShutdownOnPrefThread();
67
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);
71
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);
75
76  // ----------------------------------
77  // HttpServerProperties methods:
78  // ----------------------------------
79
80  // Gets a weak pointer for this object.
81  virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() OVERRIDE;
82
83  // Deletes all data. Works asynchronously.
84  virtual void Clear() OVERRIDE;
85
86  // Returns true if |server| supports SPDY. Should only be called from IO
87  // thread.
88  virtual bool SupportsSpdy(const HostPortPair& server) OVERRIDE;
89
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;
94
95  // Returns true if |server| has an Alternate-Protocol header.
96  virtual bool HasAlternateProtocol(const HostPortPair& server) OVERRIDE;
97
98  // Returns the Alternate-Protocol and port for |server|.
99  // HasAlternateProtocol(server) must be true.
100  virtual AlternateProtocolInfo GetAlternateProtocol(
101      const HostPortPair& server) OVERRIDE;
102
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;
109
110  // Sets the Alternate-Protocol for |server| to be BROKEN.
111  virtual void SetBrokenAlternateProtocol(const HostPortPair& server) OVERRIDE;
112
113  // Returns true if Alternate-Protocol for |server| was recently BROKEN.
114  virtual bool WasAlternateProtocolRecentlyBroken(
115      const HostPortPair& server) OVERRIDE;
116
117  // Confirms that Alternate-Protocol for |server| is working.
118  virtual void ConfirmAlternateProtocol(const HostPortPair& server) OVERRIDE;
119
120  // Clears the Alternate-Protocol for |server|.
121  virtual void ClearAlternateProtocol(const HostPortPair& server) OVERRIDE;
122
123  // Returns all Alternate-Protocol mappings.
124  virtual const AlternateProtocolMap& alternate_protocol_map() const OVERRIDE;
125
126  virtual void SetAlternateProtocolExperiment(
127      AlternateProtocolExperiment experiment) OVERRIDE;
128
129  virtual void SetAlternateProtocolProbabilityThreshold(
130      double threshold) OVERRIDE;
131
132  virtual AlternateProtocolExperiment GetAlternateProtocolExperiment()
133      const OVERRIDE;
134
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;
139
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;
146
147  // Clears all SPDY settings for a host.
148  virtual void ClearSpdySettings(const HostPortPair& host_port_pair) OVERRIDE;
149
150  // Clears all SPDY settings for all hosts.
151  virtual void ClearAllSpdySettings() OVERRIDE;
152
153  // Returns all SPDY persistent settings.
154  virtual const SpdySettingsMap& spdy_settings_map() const OVERRIDE;
155
156  // Methods for SupportsQuic.
157  virtual SupportsQuic GetSupportsQuic(
158      const HostPortPair& host_port_pair) const OVERRIDE;
159
160  virtual void SetSupportsQuic(const HostPortPair& host_port_pair,
161                               bool used_quic,
162                               const std::string& address) OVERRIDE;
163
164  virtual const SupportsQuicMap& supports_quic_map() const OVERRIDE;
165
166  virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
167                                     NetworkStats stats) OVERRIDE;
168
169  virtual const NetworkStats* GetServerNetworkStats(
170      const HostPortPair& host_port_pair) const OVERRIDE;
171
172 protected:
173  // --------------------
174  // SPDY related methods
175
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();
180
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);
184
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
188  // network thread.
189  virtual void UpdateCacheFromPrefsOnPrefThread();
190
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);
200
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();
205
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);
209
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
213  // thread.
214  void UpdatePrefsFromCacheOnNetworkThread();
215
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);
220
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);
228
229 private:
230  void OnHttpServerPropertiesChanged();
231
232  // -----------
233  // Pref thread
234  // -----------
235
236  const scoped_refptr<base::SequencedTaskRunner> pref_task_runner_;
237
238  base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_;
239
240  // Used to post cache update tasks.
241  scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
242      pref_cache_update_timer_;
243
244  // Used to track the spdy servers changes.
245  PrefChangeRegistrar pref_change_registrar_;
246  PrefService* pref_service_;  // Weak.
247  bool setting_prefs_;
248  const char* path_;
249
250  // --------------
251  // Network thread
252  // --------------
253
254  const scoped_refptr<base::SequencedTaskRunner> network_task_runner_;
255
256  // Used to post |prefs::kHttpServerProperties| pref update tasks.
257  scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
258      network_prefs_update_timer_;
259
260  scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_;
261
262  // Used to get |weak_ptr_| to self on the pref thread.
263  scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
264      pref_weak_ptr_factory_;
265
266  // Used to get |weak_ptr_| to self on the network thread.
267  scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
268      network_weak_ptr_factory_;
269
270  DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager);
271};
272
273}  // namespace net
274
275#endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_
276