Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / http / http_server_properties_manager.h
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
21 class PrefService;
22
23 namespace base {
24 class SequencedTaskRunner;
25 }
26
27 namespace 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.
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   ~HttpServerPropertiesManager() override;
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   base::WeakPtr<HttpServerProperties> GetWeakPtr() override;
82
83   // Deletes all data. Works asynchronously.
84   void Clear() override;
85
86   // Returns true if |server| supports SPDY. Should only be called from IO
87   // thread.
88   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   void SetSupportsSpdy(const HostPortPair& server, bool support_spdy) override;
93
94   // Returns true if |server| has an Alternate-Protocol header.
95   bool HasAlternateProtocol(const HostPortPair& server) override;
96
97   // Returns the Alternate-Protocol and port for |server|.
98   // HasAlternateProtocol(server) must be true.
99   AlternateProtocolInfo GetAlternateProtocol(
100       const HostPortPair& server) override;
101
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;
107
108   // Sets the Alternate-Protocol for |server| to be BROKEN.
109   void SetBrokenAlternateProtocol(const HostPortPair& server) override;
110
111   // Returns true if Alternate-Protocol for |server| was recently BROKEN.
112   bool WasAlternateProtocolRecentlyBroken(const HostPortPair& server) override;
113
114   // Confirms that Alternate-Protocol for |server| is working.
115   void ConfirmAlternateProtocol(const HostPortPair& server) override;
116
117   // Clears the Alternate-Protocol for |server|.
118   void ClearAlternateProtocol(const HostPortPair& server) override;
119
120   // Returns all Alternate-Protocol mappings.
121   const AlternateProtocolMap& alternate_protocol_map() const override;
122
123   void SetAlternateProtocolProbabilityThreshold(double threshold) override;
124
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;
129
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,
133                       SpdySettingsIds id,
134                       SpdySettingsFlags flags,
135                       uint32 value) override;
136
137   // Clears all SPDY settings for a host.
138   void ClearSpdySettings(const HostPortPair& host_port_pair) override;
139
140   // Clears all SPDY settings for all hosts.
141   void ClearAllSpdySettings() override;
142
143   // Returns all SPDY persistent settings.
144   const SpdySettingsMap& spdy_settings_map() const override;
145
146   // Methods for SupportsQuic.
147   SupportsQuic GetSupportsQuic(
148       const HostPortPair& host_port_pair) const override;
149
150   void SetSupportsQuic(const HostPortPair& host_port_pair,
151                        bool used_quic,
152                        const std::string& address) override;
153
154   const SupportsQuicMap& supports_quic_map() const override;
155
156   void SetServerNetworkStats(const HostPortPair& host_port_pair,
157                              NetworkStats stats) override;
158
159   const NetworkStats* GetServerNetworkStats(
160       const HostPortPair& host_port_pair) const override;
161
162  protected:
163   // --------------------
164   // SPDY related methods
165
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();
170
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);
174
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();
180
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);
189
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();
194
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);
198
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();
204
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);
209
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);
217
218  private:
219   void OnHttpServerPropertiesChanged();
220
221   // -----------
222   // Pref thread
223   // -----------
224
225   const scoped_refptr<base::SequencedTaskRunner> pref_task_runner_;
226
227   base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_;
228
229   // Used to post cache update tasks.
230   scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
231       pref_cache_update_timer_;
232
233   // Used to track the spdy servers changes.
234   PrefChangeRegistrar pref_change_registrar_;
235   PrefService* pref_service_;  // Weak.
236   bool setting_prefs_;
237   const char* path_;
238
239   // --------------
240   // Network thread
241   // --------------
242
243   const scoped_refptr<base::SequencedTaskRunner> network_task_runner_;
244
245   // Used to post |prefs::kHttpServerProperties| pref update tasks.
246   scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
247       network_prefs_update_timer_;
248
249   scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_;
250
251   // Used to get |weak_ptr_| to self on the pref thread.
252   scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
253       pref_weak_ptr_factory_;
254
255   // Used to get |weak_ptr_| to self on the network thread.
256   scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
257       network_weak_ptr_factory_;
258
259   DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager);
260 };
261
262 }  // namespace net
263
264 #endif  // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_