[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / net / http_server_properties_manager.h
blobb27c17ae27582ff18cc08f074b14ef2e37b54a07
1 // Copyright (c) 2012 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 CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_
6 #define CHROME_BROWSER_NET_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_pipelined_host_capability.h"
19 #include "net/http/http_server_properties.h"
20 #include "net/http/http_server_properties_impl.h"
22 class PrefService;
24 namespace user_prefs {
25 class PrefRegistrySyncable;
28 namespace chrome_browser_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 UI thread, where notifications of pref
37 // changes are received from, and the IO thread, which owns it (in the
38 // ProfileIOData) and it persists the changes from network stack that if a
39 // server supports SPDY or not.
41 // It must be constructed on the UI thread, to set up |ui_method_factory_| and
42 // the prefs listeners.
44 // ShutdownOnUIThread must be called from UI before destruction, to release
45 // the prefs listeners on the UI thread. This is done from ProfileIOData.
47 // Update tasks from the UI thread can post safely to the IO thread, since the
48 // destruction order of Profile and ProfileIOData guarantees that if this
49 // exists in UI, then a potential destruction on IO will come after any task
50 // posted to IO from that method on UI. This is used to go through IO before
51 // the actual update starts, and grab a WeakPtr.
52 class HttpServerPropertiesManager
53 : public net::HttpServerProperties {
54 public:
55 // Create an instance of the HttpServerPropertiesManager. The lifetime of the
56 // PrefService objects must be longer than that of the
57 // HttpServerPropertiesManager object. Must be constructed on the UI thread.
58 explicit HttpServerPropertiesManager(PrefService* pref_service);
59 virtual ~HttpServerPropertiesManager();
61 // Initialize |http_server_properties_impl_| and |io_method_factory_| on IO
62 // thread. It also posts a task to UI thread to get SPDY Server preferences
63 // from |pref_service_|.
64 void InitializeOnIOThread();
66 // Prepare for shutdown. Must be called on the UI thread, before destruction.
67 void ShutdownOnUIThread();
69 // Register |prefs| for properties managed here.
70 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
72 // Helper function for unit tests to set the version in the dictionary.
73 static void SetVersion(base::DictionaryValue* http_server_properties_dict,
74 int version_number);
76 // Deletes all data. Works asynchronously, but if a |completion| callback is
77 // provided, it will be fired on the UI thread when everything is done.
78 void Clear(const base::Closure& completion);
80 // ----------------------------------
81 // net::HttpServerProperties methods:
82 // ----------------------------------
84 // Gets a weak pointer for this object.
85 virtual base::WeakPtr<net::HttpServerProperties> GetWeakPtr() OVERRIDE;
87 // Deletes all data. Works asynchronously.
88 virtual void Clear() OVERRIDE;
90 // Returns true if |server| supports SPDY. Should only be called from IO
91 // thread.
92 virtual bool SupportsSpdy(const net::HostPortPair& server) OVERRIDE;
94 // Add |server| as the SPDY server which supports SPDY protocol into the
95 // persisitent store. Should only be called from IO thread.
96 virtual void SetSupportsSpdy(const net::HostPortPair& server,
97 bool support_spdy) OVERRIDE;
99 // Returns true if |server| has an Alternate-Protocol header.
100 virtual bool HasAlternateProtocol(const net::HostPortPair& server) OVERRIDE;
102 // Returns the Alternate-Protocol and port for |server|.
103 // HasAlternateProtocol(server) must be true.
104 virtual net::PortAlternateProtocolPair GetAlternateProtocol(
105 const net::HostPortPair& server) OVERRIDE;
107 // Sets the Alternate-Protocol for |server|.
108 virtual void SetAlternateProtocol(
109 const net::HostPortPair& server,
110 uint16 alternate_port,
111 net::AlternateProtocol alternate_protocol) OVERRIDE;
113 // Sets the Alternate-Protocol for |server| to be BROKEN.
114 virtual void SetBrokenAlternateProtocol(
115 const net::HostPortPair& server) OVERRIDE;
117 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
118 virtual bool WasAlternateProtocolRecentlyBroken(
119 const net::HostPortPair& server) OVERRIDE;
121 // Confirms that Alternate-Protocol for |server| is working.
122 virtual void ConfirmAlternateProtocol(
123 const net::HostPortPair& server) OVERRIDE;
125 // Clears the Alternate-Protocol for |server|.
126 virtual void ClearAlternateProtocol(const net::HostPortPair& server) OVERRIDE;
128 // Returns all Alternate-Protocol mappings.
129 virtual const net::AlternateProtocolMap&
130 alternate_protocol_map() const OVERRIDE;
132 // Gets a reference to the SettingsMap stored for a host.
133 // If no settings are stored, returns an empty SettingsMap.
134 virtual const net::SettingsMap& GetSpdySettings(
135 const net::HostPortPair& host_port_pair) OVERRIDE;
137 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
138 // is to be persisted.
139 virtual bool SetSpdySetting(const net::HostPortPair& host_port_pair,
140 net::SpdySettingsIds id,
141 net::SpdySettingsFlags flags,
142 uint32 value) OVERRIDE;
144 // Clears all SPDY settings for a host.
145 virtual void ClearSpdySettings(
146 const net::HostPortPair& host_port_pair) OVERRIDE;
148 // Clears all SPDY settings for all hosts.
149 virtual void ClearAllSpdySettings() OVERRIDE;
151 // Returns all SPDY persistent settings.
152 virtual const net::SpdySettingsMap& spdy_settings_map() const OVERRIDE;
154 virtual void SetServerNetworkStats(const net::HostPortPair& host_port_pair,
155 NetworkStats stats) OVERRIDE;
157 virtual const NetworkStats* GetServerNetworkStats(
158 const net::HostPortPair& host_port_pair) const OVERRIDE;
160 virtual net::HttpPipelinedHostCapability GetPipelineCapability(
161 const net::HostPortPair& origin) OVERRIDE;
163 virtual void SetPipelineCapability(
164 const net::HostPortPair& origin,
165 net::HttpPipelinedHostCapability capability) OVERRIDE;
167 virtual void ClearPipelineCapabilities() OVERRIDE;
169 virtual net::PipelineCapabilityMap GetPipelineCapabilityMap() const OVERRIDE;
171 protected:
172 // --------------------
173 // SPDY related methods
175 // These are used to delay updating of the cached data in
176 // |http_server_properties_impl_| while the preferences are changing, and
177 // execute only one update per simultaneous prefs changes.
178 void ScheduleUpdateCacheOnUI();
180 // Starts the timers to update the cached prefs. This are overridden in tests
181 // to prevent the delay.
182 virtual void StartCacheUpdateTimerOnUI(base::TimeDelta delay);
184 // Update cached prefs in |http_server_properties_impl_| with data from
185 // preferences. It gets the data on UI thread and calls
186 // UpdateSpdyServersFromPrefsOnIO() to perform the update on IO thread.
187 virtual void UpdateCacheFromPrefsOnUI();
189 // Starts the update of cached prefs in |http_server_properties_impl_| on the
190 // IO thread. Protected for testing.
191 void UpdateCacheFromPrefsOnIO(
192 std::vector<std::string>* spdy_servers,
193 net::SpdySettingsMap* spdy_settings_map,
194 net::AlternateProtocolMap* alternate_protocol_map,
195 net::PipelineCapabilityMap* pipeline_capability_map,
196 bool detected_corrupted_prefs);
198 // These are used to delay updating the preferences when cached data in
199 // |http_server_properties_impl_| is changing, and execute only one update per
200 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes.
201 void ScheduleUpdatePrefsOnIO();
203 // Starts the timers to update the prefs from cache. This are overridden in
204 // tests to prevent the delay.
205 virtual void StartPrefsUpdateTimerOnIO(base::TimeDelta delay);
207 // Update prefs::kHttpServerProperties in preferences with the cached data
208 // from |http_server_properties_impl_|. This gets the data on IO thread and
209 // posts a task (UpdatePrefsOnUI) to update the preferences UI thread.
210 void UpdatePrefsFromCacheOnIO();
212 // Same as above, but fires an optional |completion| callback on the UI thread
213 // when finished. Virtual for testing.
214 virtual void UpdatePrefsFromCacheOnIO(const base::Closure& completion);
216 // Update prefs::kHttpServerProperties preferences on UI thread. Executes an
217 // optional |completion| callback when finished. Protected for testing.
218 void UpdatePrefsOnUI(
219 base::ListValue* spdy_server_list,
220 net::SpdySettingsMap* spdy_settings_map,
221 net::AlternateProtocolMap* alternate_protocol_map,
222 net::PipelineCapabilityMap* pipeline_capability_map,
223 const base::Closure& completion);
225 private:
226 void OnHttpServerPropertiesChanged();
228 // ---------
229 // UI thread
230 // ---------
232 // Used to get |weak_ptr_| to self on the UI thread.
233 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
234 ui_weak_ptr_factory_;
236 base::WeakPtr<HttpServerPropertiesManager> ui_weak_ptr_;
238 // Used to post cache update tasks.
239 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
240 ui_cache_update_timer_;
242 // Used to track the spdy servers changes.
243 PrefChangeRegistrar pref_change_registrar_;
244 PrefService* pref_service_; // Weak.
245 bool setting_prefs_;
247 // ---------
248 // IO thread
249 // ---------
251 // Used to get |weak_ptr_| to self on the IO thread.
252 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
253 io_weak_ptr_factory_;
255 // Used to post |prefs::kHttpServerProperties| pref update tasks.
256 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
257 io_prefs_update_timer_;
259 scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_impl_;
261 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager);
264 } // namespace chrome_browser_net
266 #endif // CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_