| OLD | NEW |
| (Empty) |
| 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 | |
| 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" | |
| 21 | |
| 22 class PrefService; | |
| 23 | |
| 24 namespace base { | |
| 25 class SequencedTaskRunner; | |
| 26 } | |
| 27 | |
| 28 namespace net { | |
| 29 | |
| 30 //////////////////////////////////////////////////////////////////////////////// | |
| 31 // HttpServerPropertiesManager | |
| 32 | |
| 33 // The manager for creating and updating an HttpServerProperties (for example it | |
| 34 // tracks if a server supports SPDY or not). | |
| 35 // | |
| 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. | |
| 39 // | |
| 40 // It must be constructed on the pref thread, to set up |pref_task_runner_| and | |
| 41 // the prefs listeners. | |
| 42 // | |
| 43 // ShutdownOnPrefThread must be called from pref thread before destruction, to | |
| 44 // release the prefs listeners on the pref thread. | |
| 45 // | |
| 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; | |
| 62 | |
| 63 // Initialize on Network thread. | |
| 64 void InitializeOnNetworkThread(); | |
| 65 | |
| 66 // Prepare for shutdown. Must be called on the Pref thread before destruction. | |
| 67 void ShutdownOnPrefThread(); | |
| 68 | |
| 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); | |
| 72 | |
| 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); | |
| 76 | |
| 77 // ---------------------------------- | |
| 78 // HttpServerProperties methods: | |
| 79 // ---------------------------------- | |
| 80 | |
| 81 base::WeakPtr<HttpServerProperties> GetWeakPtr() override; | |
| 82 void Clear() override; | |
| 83 bool SupportsRequestPriority(const HostPortPair& server) override; | |
| 84 void SetSupportsSpdy(const HostPortPair& server, bool support_spdy) override; | |
| 85 bool RequiresHTTP11(const HostPortPair& server) override; | |
| 86 void SetHTTP11Required(const HostPortPair& server) override; | |
| 87 void MaybeForceHTTP11(const HostPortPair& server, | |
| 88 SSLConfig* ssl_config) override; | |
| 89 AlternateProtocolInfo GetAlternateProtocol( | |
| 90 const HostPortPair& server) override; | |
| 91 void SetAlternateProtocol(const HostPortPair& server, | |
| 92 uint16 alternate_port, | |
| 93 AlternateProtocol alternate_protocol, | |
| 94 double alternate_probability) override; | |
| 95 void SetBrokenAlternateProtocol(const HostPortPair& server) override; | |
| 96 bool WasAlternateProtocolRecentlyBroken(const HostPortPair& server) override; | |
| 97 void ConfirmAlternateProtocol(const HostPortPair& server) override; | |
| 98 void ClearAlternateProtocol(const HostPortPair& server) override; | |
| 99 const AlternateProtocolMap& alternate_protocol_map() const override; | |
| 100 void SetAlternateProtocolProbabilityThreshold(double threshold) override; | |
| 101 const SettingsMap& GetSpdySettings( | |
| 102 const HostPortPair& host_port_pair) override; | |
| 103 bool SetSpdySetting(const HostPortPair& host_port_pair, | |
| 104 SpdySettingsIds id, | |
| 105 SpdySettingsFlags flags, | |
| 106 uint32 value) override; | |
| 107 void ClearSpdySettings(const HostPortPair& host_port_pair) override; | |
| 108 void ClearAllSpdySettings() override; | |
| 109 const SpdySettingsMap& spdy_settings_map() const override; | |
| 110 bool GetSupportsQuic(IPAddressNumber* last_address) const override; | |
| 111 void SetSupportsQuic(bool used_quic, | |
| 112 const IPAddressNumber& last_address) override; | |
| 113 void SetServerNetworkStats(const HostPortPair& host_port_pair, | |
| 114 ServerNetworkStats stats) override; | |
| 115 const ServerNetworkStats* GetServerNetworkStats( | |
| 116 const HostPortPair& host_port_pair) override; | |
| 117 const ServerNetworkStatsMap& server_network_stats_map() const override; | |
| 118 | |
| 119 protected: | |
| 120 // -------------------- | |
| 121 // SPDY related methods | |
| 122 | |
| 123 // These are used to delay updating of the cached data in | |
| 124 // |http_server_properties_impl_| while the preferences are changing, and | |
| 125 // execute only one update per simultaneous prefs changes. | |
| 126 void ScheduleUpdateCacheOnPrefThread(); | |
| 127 | |
| 128 // Starts the timers to update the cached prefs. This are overridden in tests | |
| 129 // to prevent the delay. | |
| 130 virtual void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay); | |
| 131 | |
| 132 // Update cached prefs in |http_server_properties_impl_| with data from | |
| 133 // preferences. It gets the data on pref thread and calls | |
| 134 // UpdateSpdyServersFromPrefsOnNetworkThread() to perform the update on | |
| 135 // network thread. | |
| 136 virtual void UpdateCacheFromPrefsOnPrefThread(); | |
| 137 | |
| 138 // Starts the update of cached prefs in |http_server_properties_impl_| on the | |
| 139 // network thread. Protected for testing. | |
| 140 void UpdateCacheFromPrefsOnNetworkThread( | |
| 141 std::vector<std::string>* spdy_servers, | |
| 142 SpdySettingsMap* spdy_settings_map, | |
| 143 AlternateProtocolMap* alternate_protocol_map, | |
| 144 IPAddressNumber* last_quic_address, | |
| 145 ServerNetworkStatsMap* server_network_stats_map, | |
| 146 bool detected_corrupted_prefs); | |
| 147 | |
| 148 // These are used to delay updating the preferences when cached data in | |
| 149 // |http_server_properties_impl_| is changing, and execute only one update per | |
| 150 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes. | |
| 151 void ScheduleUpdatePrefsOnNetworkThread(); | |
| 152 | |
| 153 // Starts the timers to update the prefs from cache. This are overridden in | |
| 154 // tests to prevent the delay. | |
| 155 virtual void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay); | |
| 156 | |
| 157 // Update prefs::kHttpServerProperties in preferences with the cached data | |
| 158 // from |http_server_properties_impl_|. This gets the data on network thread | |
| 159 // and posts a task (UpdatePrefsOnPrefThread) to update preferences on pref | |
| 160 // thread. | |
| 161 void UpdatePrefsFromCacheOnNetworkThread(); | |
| 162 | |
| 163 // Same as above, but fires an optional |completion| callback on pref thread | |
| 164 // when finished. Virtual for testing. | |
| 165 virtual void UpdatePrefsFromCacheOnNetworkThread( | |
| 166 const base::Closure& completion); | |
| 167 | |
| 168 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an | |
| 169 // optional |completion| callback when finished. Protected for testing. | |
| 170 void UpdatePrefsOnPrefThread(base::ListValue* spdy_server_list, | |
| 171 SpdySettingsMap* spdy_settings_map, | |
| 172 AlternateProtocolMap* alternate_protocol_map, | |
| 173 IPAddressNumber* last_quic_address, | |
| 174 ServerNetworkStatsMap* server_network_stats_map, | |
| 175 const base::Closure& completion); | |
| 176 | |
| 177 private: | |
| 178 void OnHttpServerPropertiesChanged(); | |
| 179 | |
| 180 bool ReadSupportsQuic(const base::DictionaryValue& server_dict, | |
| 181 IPAddressNumber* last_quic_address); | |
| 182 void AddToSpdySettingsMap(const HostPortPair& server, | |
| 183 const base::DictionaryValue& server_dict, | |
| 184 SpdySettingsMap* spdy_settings_map); | |
| 185 AlternateProtocolInfo ParseAlternateProtocolDict( | |
| 186 const base::DictionaryValue& alternate_protocol_dict, | |
| 187 const std::string& server_str); | |
| 188 bool AddToAlternateProtocolMap(const HostPortPair& server, | |
| 189 const base::DictionaryValue& server_dict, | |
| 190 AlternateProtocolMap* alternate_protocol_map); | |
| 191 bool AddToNetworkStatsMap(const HostPortPair& server, | |
| 192 const base::DictionaryValue& server_dict, | |
| 193 ServerNetworkStatsMap* network_stats_map); | |
| 194 | |
| 195 void SaveSpdySettingsToServerPrefs(const SettingsMap* spdy_settings_map, | |
| 196 base::DictionaryValue* server_pref_dict); | |
| 197 void SaveAlternateProtocolToServerPrefs( | |
| 198 const AlternateProtocolInfo* port_alternate_protocol, | |
| 199 base::DictionaryValue* server_pref_dict); | |
| 200 void SaveNetworkStatsToServerPrefs( | |
| 201 const ServerNetworkStats* server_network_stats, | |
| 202 base::DictionaryValue* server_pref_dict); | |
| 203 | |
| 204 void SaveSupportsQuicToPrefs( | |
| 205 const IPAddressNumber* last_quic_address, | |
| 206 base::DictionaryValue* http_server_properties_dict); | |
| 207 | |
| 208 // ----------- | |
| 209 // Pref thread | |
| 210 // ----------- | |
| 211 | |
| 212 const scoped_refptr<base::SequencedTaskRunner> pref_task_runner_; | |
| 213 | |
| 214 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_; | |
| 215 | |
| 216 // Used to post cache update tasks. | |
| 217 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> > | |
| 218 pref_cache_update_timer_; | |
| 219 | |
| 220 // Used to track the spdy servers changes. | |
| 221 PrefChangeRegistrar pref_change_registrar_; | |
| 222 PrefService* pref_service_; // Weak. | |
| 223 bool setting_prefs_; | |
| 224 const char* path_; | |
| 225 | |
| 226 // -------------- | |
| 227 // Network thread | |
| 228 // -------------- | |
| 229 | |
| 230 const scoped_refptr<base::SequencedTaskRunner> network_task_runner_; | |
| 231 | |
| 232 // Used to post |prefs::kHttpServerProperties| pref update tasks. | |
| 233 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> > | |
| 234 network_prefs_update_timer_; | |
| 235 | |
| 236 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_; | |
| 237 | |
| 238 // Used to get |weak_ptr_| to self on the pref thread. | |
| 239 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > | |
| 240 pref_weak_ptr_factory_; | |
| 241 | |
| 242 // Used to get |weak_ptr_| to self on the network thread. | |
| 243 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > | |
| 244 network_weak_ptr_factory_; | |
| 245 | |
| 246 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager); | |
| 247 }; | |
| 248 | |
| 249 } // namespace net | |
| 250 | |
| 251 #endif // NET_HTTP_HTTP_SERVER_PROPERTIES_MANAGER_H_ | |
| OLD | NEW |