Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / http / http_server_properties_manager.h
blobedd52b4a8e770fbdec4e14c51cedac09dd25b849
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>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/prefs/pref_change_registrar.h"
16 #include "base/timer/timer.h"
17 #include "base/values.h"
18 #include "net/base/host_port_pair.h"
19 #include "net/http/http_server_properties.h"
20 #include "net/http/http_server_properties_impl.h"
22 class PrefService;
24 namespace base {
25 class SequencedTaskRunner;
28 namespace net {
30 ////////////////////////////////////////////////////////////////////////////////
31 // HttpServerPropertiesManager
33 // The manager for creating and updating an HttpServerProperties (for example it
34 // tracks if a server supports SPDY or not).
36 // This class interacts with both the pref thread, where notifications of pref
37 // changes are received from, and the network thread, which owns it, and it
38 // persists the changes from network stack whether server supports SPDY or not.
40 // It must be constructed on the pref thread, to set up |pref_task_runner_| and
41 // the prefs listeners.
43 // ShutdownOnPrefThread must be called from pref thread before destruction, to
44 // release the prefs listeners on the pref thread.
46 // Class requires that update tasks from the Pref thread can post safely to the
47 // network thread, so the destruction order must guarantee that if |this|
48 // exists in pref thread, then a potential destruction on network thread will
49 // come after any task posted to network thread from that method on pref thread.
50 // This is used to go through network thread before the actual update starts,
51 // and grab a WeakPtr.
52 class NET_EXPORT HttpServerPropertiesManager : public HttpServerProperties {
53 public:
54 // Create an instance of the HttpServerPropertiesManager. The lifetime of the
55 // PrefService objects must be longer than that of the
56 // HttpServerPropertiesManager object. Must be constructed on the Pref thread.
57 HttpServerPropertiesManager(
58 PrefService* pref_service,
59 const char* pref_path,
60 scoped_refptr<base::SequencedTaskRunner> network_task_runner);
61 ~HttpServerPropertiesManager() override;
63 // Initialize on Network thread.
64 void InitializeOnNetworkThread();
66 // Prepare for shutdown. Must be called on the Pref thread before destruction.
67 void ShutdownOnPrefThread();
69 // Helper function for unit tests to set the version in the dictionary.
70 static void SetVersion(base::DictionaryValue* http_server_properties_dict,
71 int version_number);
73 // Deletes all data. Works asynchronously, but if a |completion| callback is
74 // provided, it will be fired on the pref thread when everything is done.
75 void Clear(const base::Closure& completion);
77 // ----------------------------------
78 // HttpServerProperties methods:
79 // ----------------------------------
81 base::WeakPtr<HttpServerProperties> GetWeakPtr() override;
82 void Clear() override;
83 bool SupportsRequestPriority(const HostPortPair& server) override;
84 bool GetSupportsSpdy(const HostPortPair& server) override;
85 void SetSupportsSpdy(const HostPortPair& server, bool support_spdy) override;
86 bool RequiresHTTP11(const HostPortPair& server) override;
87 void SetHTTP11Required(const HostPortPair& server) override;
88 void MaybeForceHTTP11(const HostPortPair& server,
89 SSLConfig* ssl_config) override;
90 AlternativeServiceVector GetAlternativeServices(
91 const HostPortPair& origin) override;
92 bool SetAlternativeService(const HostPortPair& origin,
93 const AlternativeService& alternative_service,
94 double alternative_probability,
95 base::Time expiration) override;
96 bool SetAlternativeServices(const HostPortPair& origin,
97 const AlternativeServiceInfoVector&
98 alternative_service_info_vector) override;
99 void MarkAlternativeServiceBroken(
100 const AlternativeService& alternative_service) override;
101 void MarkAlternativeServiceRecentlyBroken(
102 const AlternativeService& alternative_service) override;
103 bool IsAlternativeServiceBroken(
104 const AlternativeService& alternative_service) const override;
105 bool WasAlternativeServiceRecentlyBroken(
106 const AlternativeService& alternative_service) override;
107 void ConfirmAlternativeService(
108 const AlternativeService& alternative_service) override;
109 void ClearAlternativeServices(const HostPortPair& origin) override;
110 const AlternativeServiceMap& alternative_service_map() const override;
111 scoped_ptr<base::Value> GetAlternativeServiceInfoAsValue() const override;
112 void SetAlternativeServiceProbabilityThreshold(double threshold) override;
113 const SettingsMap& GetSpdySettings(
114 const HostPortPair& host_port_pair) override;
115 bool SetSpdySetting(const HostPortPair& host_port_pair,
116 SpdySettingsIds id,
117 SpdySettingsFlags flags,
118 uint32 value) override;
119 void ClearSpdySettings(const HostPortPair& host_port_pair) override;
120 void ClearAllSpdySettings() override;
121 const SpdySettingsMap& spdy_settings_map() const override;
122 bool GetSupportsQuic(IPAddressNumber* last_address) const override;
123 void SetSupportsQuic(bool used_quic,
124 const IPAddressNumber& last_address) override;
125 void SetServerNetworkStats(const HostPortPair& host_port_pair,
126 ServerNetworkStats stats) override;
127 const ServerNetworkStats* GetServerNetworkStats(
128 const HostPortPair& host_port_pair) override;
129 const ServerNetworkStatsMap& server_network_stats_map() const override;
131 protected:
132 // The location where ScheduleUpdatePrefsOnNetworkThread was called.
133 enum Location {
134 SUPPORTS_SPDY = 0,
135 HTTP_11_REQUIRED = 1,
136 SET_ALTERNATIVE_SERVICES = 2,
137 MARK_ALTERNATIVE_SERVICE_BROKEN = 3,
138 MARK_ALTERNATIVE_SERVICE_RECENTLY_BROKEN = 4,
139 CONFIRM_ALTERNATIVE_SERVICE = 5,
140 CLEAR_ALTERNATIVE_SERVICE = 6,
141 SET_SPDY_SETTING = 7,
142 CLEAR_SPDY_SETTINGS = 8,
143 CLEAR_ALL_SPDY_SETTINGS = 9,
144 SET_SUPPORTS_QUIC = 10,
145 SET_SERVER_NETWORK_STATS = 11,
146 DETECTED_CORRUPTED_PREFS = 12,
147 NUM_LOCATIONS = 13,
150 // --------------------
151 // SPDY related methods
153 // These are used to delay updating of the cached data in
154 // |http_server_properties_impl_| while the preferences are changing, and
155 // execute only one update per simultaneous prefs changes.
156 void ScheduleUpdateCacheOnPrefThread();
158 // Starts the timers to update the cached prefs. This are overridden in tests
159 // to prevent the delay.
160 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay);
162 // Update cached prefs in |http_server_properties_impl_| with data from
163 // preferences. It gets the data on pref thread and calls
164 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on
165 // network thread.
166 virtual void UpdateCacheFromPrefsOnPrefThread();
168 // Starts the update of cached prefs in |http_server_properties_impl_| on the
169 // network thread. Protected for testing.
170 void UpdateCacheFromPrefsOnNetworkThread(
171 std::vector<std::string>* spdy_servers,
172 SpdySettingsMap* spdy_settings_map,
173 AlternativeServiceMap* alternative_service_map,
174 IPAddressNumber* last_quic_address,
175 ServerNetworkStatsMap* server_network_stats_map,
176 bool detected_corrupted_prefs);
178 // These are used to delay updating the preferences when cached data in
179 // |http_server_properties_impl_| is changing, and execute only one update per
180 // simultaneous spdy_servers or spdy_settings or alternative_service changes.
181 // |location| specifies where this method is called from. Virtual for testing.
182 virtual void ScheduleUpdatePrefsOnNetworkThread(Location location);
184 // Starts the timers to update the prefs from cache. This are overridden in
185 // tests to prevent the delay.
186 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay);
188 // Update prefs::kHttpServerProperties in preferences with the cached data
189 // from |http_server_properties_impl_|. This gets the data on network thread
190 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref
191 // thread.
192 void UpdatePrefsFromCacheOnNetworkThread();
194 // Same as above, but fires an optional |completion| callback on pref thread
195 // when finished. Virtual for testing.
196 virtual void UpdatePrefsFromCacheOnNetworkThread(
197 const base::Closure& completion);
199 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an
200 // optional |completion| callback when finished. Protected for testing.
201 void UpdatePrefsOnPrefThread(base::ListValue* spdy_server_list,
202 SpdySettingsMap* spdy_settings_map,
203 AlternativeServiceMap* alternative_service_map,
204 IPAddressNumber* last_quic_address,
205 ServerNetworkStatsMap* server_network_stats_map,
206 const base::Closure& completion);
208 private:
209 FRIEND_TEST_ALL_PREFIXES(HttpServerPropertiesManagerTest,
210 AddToAlternativeServiceMap);
211 FRIEND_TEST_ALL_PREFIXES(HttpServerPropertiesManagerTest,
212 AlternativeServiceExpirationDouble);
213 void OnHttpServerPropertiesChanged();
215 bool ReadSupportsQuic(const base::DictionaryValue& server_dict,
216 IPAddressNumber* last_quic_address);
217 void AddToSpdySettingsMap(const HostPortPair& server,
218 const base::DictionaryValue& server_dict,
219 SpdySettingsMap* spdy_settings_map);
220 bool ParseAlternativeServiceDict(
221 const base::DictionaryValue& alternative_service_dict,
222 const std::string& server_str,
223 AlternativeServiceInfo* alternative_service_info);
224 bool AddToAlternativeServiceMap(
225 const HostPortPair& server,
226 const base::DictionaryValue& server_dict,
227 AlternativeServiceMap* alternative_service_map);
228 bool AddToNetworkStatsMap(const HostPortPair& server,
229 const base::DictionaryValue& server_dict,
230 ServerNetworkStatsMap* network_stats_map);
232 void SaveSpdySettingsToServerPrefs(const SettingsMap* spdy_settings_map,
233 base::DictionaryValue* server_pref_dict);
234 void SaveAlternativeServiceToServerPrefs(
235 const AlternativeServiceInfoVector* alternative_service_info_vector,
236 base::DictionaryValue* server_pref_dict);
237 void SaveNetworkStatsToServerPrefs(
238 const ServerNetworkStats* server_network_stats,
239 base::DictionaryValue* server_pref_dict);
241 void SaveSupportsQuicToPrefs(
242 const IPAddressNumber* last_quic_address,
243 base::DictionaryValue* http_server_properties_dict);
245 // -----------
246 // Pref thread
247 // -----------
249 const scoped_refptr<base::SequencedTaskRunner> pref_task_runner_;
251 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_;
253 // Used to post cache update tasks.
254 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
255 pref_cache_update_timer_;
257 // Used to track the spdy servers changes.
258 PrefChangeRegistrar pref_change_registrar_;
259 PrefService* pref_service_; // Weak.
260 bool setting_prefs_;
261 const char* path_;
263 // --------------
264 // Network thread
265 // --------------
267 const scoped_refptr<base::SequencedTaskRunner> network_task_runner_;
269 // Used to post |prefs::kHttpServerProperties| pref update tasks.
270 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
271 network_prefs_update_timer_;
273 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_;
275 // Used to get |weak_ptr_| to self on the pref thread.
276 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
277 pref_weak_ptr_factory_;
279 // Used to get |weak_ptr_| to self on the network thread.
280 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
281 network_weak_ptr_factory_;
283 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager);
286 } // namespace net
288 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_