Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(517)

Side by Side Diff: net/http/http_server_properties_manager.h

Issue 378823002: Move http_server_properties_manager from chrome/browser/net to net/http. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ 5 #ifndef CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_
6 #define CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ 6 #define CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/prefs/pref_change_registrar.h" 14 #include "base/prefs/pref_change_registrar.h"
15 #include "base/timer/timer.h" 15 #include "base/timer/timer.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "net/base/host_port_pair.h" 17 #include "net/base/host_port_pair.h"
18 #include "net/http/http_server_properties.h" 18 #include "net/http/http_server_properties.h"
19 #include "net/http/http_server_properties_impl.h" 19 #include "net/http/http_server_properties_impl.h"
20 20
21 class PrefService; 21 class PrefService;
22 22
23 namespace user_prefs { 23 namespace base {
24 class PrefRegistrySyncable; 24 class SingleThreadTaskRunner;
Ryan Sleevi 2014/07/08 18:19:26 Do you really need an STTR? A SequencedTaskRunner
mef 2014/07/09 12:12:04 Done.
25 } 25 }
26 26
27 namespace chrome_browser_net { 27 namespace net {
28 28
29 //////////////////////////////////////////////////////////////////////////////// 29 ////////////////////////////////////////////////////////////////////////////////
30 // HttpServerPropertiesManager 30 // HttpServerPropertiesManager
31 31
32 // The manager for creating and updating an HttpServerProperties (for example it 32 // The manager for creating and updating an HttpServerProperties (for example it
33 // tracks if a server supports SPDY or not). 33 // tracks if a server supports SPDY or not).
34 // 34 //
35 // This class interacts with both the UI thread, where notifications of pref 35 // This class interacts with both the pref thread, where notifications of pref
36 // changes are received from, and the IO thread, which owns it (in the 36 // changes are received from, and the IO thread, which owns it (e.g. in the
Ryan Sleevi 2014/07/08 18:19:26 nit: usually called network task runner.
37 // ProfileIOData) and it persists the changes from network stack that if a 37 // ProfileIOData) and it persists the changes from network stack that if a
Ryan Sleevi 2014/07/08 18:19:26 ProfileIOData = abstraction leak
mef 2014/07/09 12:12:04 Done.
38 // server supports SPDY or not. 38 // server supports SPDY or not.
39 // 39 //
40 // It must be constructed on the UI thread, to set up |ui_method_factory_| and 40 // It must be constructed on the pref thread, to set up |pref_task_runner_| and
41 // the prefs listeners. 41 // the prefs listeners.
42 // 42 //
43 // ShutdownOnUIThread must be called from UI before destruction, to release 43 // ShutdownOnPrefThread must be called from pref thread before destruction, to
44 // the prefs listeners on the UI thread. This is done from ProfileIOData. 44 // release the prefs listeners on the pref thread.
45 // 45
46 // Update tasks from the UI thread can post safely to the IO thread, since the 46 // Class expects that update tasks from the Pref thread can post safely to the
47 // destruction order of Profile and ProfileIOData guarantees that if this 47 // IO thread, since the destruction order guarantees that if this
48 // exists in UI, then a potential destruction on IO will come after any task 48 // exists in pref, then a potential destruction on IO will come after any task
49 // posted to IO from that method on UI. This is used to go through IO before 49 // posted to IO from that method on pref thread. This is used to go through IO
50 // the actual update starts, and grab a WeakPtr. 50 // before the actual update starts, and grab a WeakPtr.
Ryan Sleevi 2014/07/08 18:19:26 1) extra space in "before the" 2) This destructi
mef 2014/07/09 12:12:04 Done.
51 class HttpServerPropertiesManager 51 class NET_EXPORT HttpServerPropertiesManager : public HttpServerProperties {
52 : public net::HttpServerProperties {
53 public: 52 public:
54 // Create an instance of the HttpServerPropertiesManager. The lifetime of the 53 // Create an instance of the HttpServerPropertiesManager. The lifetime of the
55 // PrefService objects must be longer than that of the 54 // PrefService objects must be longer than that of the
56 // HttpServerPropertiesManager object. Must be constructed on the UI thread. 55 // HttpServerPropertiesManager object. Must be constructed on the Pref thread.
57 explicit HttpServerPropertiesManager(PrefService* pref_service); 56 HttpServerPropertiesManager(
57 PrefService* pref_service,
58 const char* pref_path,
59 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
58 virtual ~HttpServerPropertiesManager(); 60 virtual ~HttpServerPropertiesManager();
59 61
60 // Initialize |http_server_properties_impl_| and |io_method_factory_| on IO 62 // Initialize |http_server_properties_impl_| and |io_task_runner_| on IO
61 // thread. It also posts a task to UI thread to get SPDY Server preferences 63 // thread. It also posts a task to Pref thread to get SPDY Server preferences
62 // from |pref_service_|. 64 // from |pref_service_|.
Ryan Sleevi 2014/07/08 18:19:26 This last sentence seems to unnecessarily describe
mef 2014/07/09 12:12:04 Done.
63 void InitializeOnIOThread(); 65 void InitializeOnIOThread();
64 66
65 // Prepare for shutdown. Must be called on the UI thread, before destruction. 67 // Prepare for shutdown. Must be called on the Pref thread before destruction.
66 void ShutdownOnUIThread(); 68 void ShutdownOnPrefThread();
Ryan Sleevi 2014/07/08 18:19:26 And what happens if they don't?
Bernhard Bauer 2014/07/08 21:47:21 A DCHECK will trigger when this object (with its U
Ryan Sleevi 2014/07/08 22:54:35 Well, this is more of a "long term design" issue,
mef 2014/07/09 12:12:04 Ack. I'm fine with abandoning this CL in favor or
67
68 // Register |prefs| for properties managed here.
69 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
70 69
71 // Helper function for unit tests to set the version in the dictionary. 70 // Helper function for unit tests to set the version in the dictionary.
72 static void SetVersion(base::DictionaryValue* http_server_properties_dict, 71 static void SetVersion(base::DictionaryValue* http_server_properties_dict,
73 int version_number); 72 int version_number);
74 73
75 // Deletes all data. Works asynchronously, but if a |completion| callback is 74 // Deletes all data. Works asynchronously, but if a |completion| callback is
76 // provided, it will be fired on the UI thread when everything is done. 75 // provided, it will be fired on the pref thread when everything is done.
77 void Clear(const base::Closure& completion); 76 void Clear(const base::Closure& completion);
78 77
79 // ---------------------------------- 78 // ----------------------------------
80 // net::HttpServerProperties methods: 79 // HttpServerProperties methods:
81 // ---------------------------------- 80 // ----------------------------------
82 81
83 // Gets a weak pointer for this object. 82 // Gets a weak pointer for this object.
84 virtual base::WeakPtr<net::HttpServerProperties> GetWeakPtr() OVERRIDE; 83 virtual base::WeakPtr<HttpServerProperties> GetWeakPtr() OVERRIDE;
Ryan Sleevi 2014/07/08 18:19:26 Hrm. Long-term, I think we will want to remove thi
mef 2014/07/09 12:12:04 Roger that.
85 84
86 // Deletes all data. Works asynchronously. 85 // Deletes all data. Works asynchronously.
87 virtual void Clear() OVERRIDE; 86 virtual void Clear() OVERRIDE;
88 87
89 // Returns true if |server| supports SPDY. Should only be called from IO 88 // Returns true if |server| supports SPDY. Should only be called from IO
90 // thread. 89 // thread.
91 virtual bool SupportsSpdy(const net::HostPortPair& server) OVERRIDE; 90 virtual bool SupportsSpdy(const HostPortPair& server) OVERRIDE;
92 91
93 // Add |server| as the SPDY server which supports SPDY protocol into the 92 // Add |server| as the SPDY server which supports SPDY protocol into the
94 // persisitent store. Should only be called from IO thread. 93 // persisitent store. Should only be called from IO thread.
95 virtual void SetSupportsSpdy(const net::HostPortPair& server, 94 virtual void SetSupportsSpdy(const HostPortPair& server,
96 bool support_spdy) OVERRIDE; 95 bool support_spdy) OVERRIDE;
97 96
98 // Returns true if |server| has an Alternate-Protocol header. 97 // Returns true if |server| has an Alternate-Protocol header.
99 virtual bool HasAlternateProtocol(const net::HostPortPair& server) OVERRIDE; 98 virtual bool HasAlternateProtocol(const HostPortPair& server) OVERRIDE;
100 99
101 // Returns the Alternate-Protocol and port for |server|. 100 // Returns the Alternate-Protocol and port for |server|.
102 // HasAlternateProtocol(server) must be true. 101 // HasAlternateProtocol(server) must be true.
103 virtual net::PortAlternateProtocolPair GetAlternateProtocol( 102 virtual PortAlternateProtocolPair GetAlternateProtocol(
104 const net::HostPortPair& server) OVERRIDE; 103 const HostPortPair& server) OVERRIDE;
105 104
106 // Sets the Alternate-Protocol for |server|. 105 // Sets the Alternate-Protocol for |server|.
107 virtual void SetAlternateProtocol( 106 virtual void SetAlternateProtocol(
108 const net::HostPortPair& server, 107 const HostPortPair& server,
109 uint16 alternate_port, 108 uint16 alternate_port,
110 net::AlternateProtocol alternate_protocol) OVERRIDE; 109 AlternateProtocol alternate_protocol) OVERRIDE;
111 110
112 // Sets the Alternate-Protocol for |server| to be BROKEN. 111 // Sets the Alternate-Protocol for |server| to be BROKEN.
113 virtual void SetBrokenAlternateProtocol( 112 virtual void SetBrokenAlternateProtocol(const HostPortPair& server) OVERRIDE;
114 const net::HostPortPair& server) OVERRIDE;
115 113
116 // Returns true if Alternate-Protocol for |server| was recently BROKEN. 114 // Returns true if Alternate-Protocol for |server| was recently BROKEN.
117 virtual bool WasAlternateProtocolRecentlyBroken( 115 virtual bool WasAlternateProtocolRecentlyBroken(
118 const net::HostPortPair& server) OVERRIDE; 116 const HostPortPair& server) OVERRIDE;
119 117
120 // Confirms that Alternate-Protocol for |server| is working. 118 // Confirms that Alternate-Protocol for |server| is working.
121 virtual void ConfirmAlternateProtocol( 119 virtual void ConfirmAlternateProtocol(const HostPortPair& server) OVERRIDE;
122 const net::HostPortPair& server) OVERRIDE;
123 120
124 // Clears the Alternate-Protocol for |server|. 121 // Clears the Alternate-Protocol for |server|.
125 virtual void ClearAlternateProtocol(const net::HostPortPair& server) OVERRIDE; 122 virtual void ClearAlternateProtocol(const HostPortPair& server) OVERRIDE;
126 123
127 // Returns all Alternate-Protocol mappings. 124 // Returns all Alternate-Protocol mappings.
128 virtual const net::AlternateProtocolMap& 125 virtual const AlternateProtocolMap& alternate_protocol_map() const OVERRIDE;
129 alternate_protocol_map() const OVERRIDE;
130 126
131 virtual void SetAlternateProtocolExperiment( 127 virtual void SetAlternateProtocolExperiment(
132 net::AlternateProtocolExperiment experiment) OVERRIDE; 128 AlternateProtocolExperiment experiment) OVERRIDE;
133 129
134 virtual net::AlternateProtocolExperiment GetAlternateProtocolExperiment() 130 virtual AlternateProtocolExperiment GetAlternateProtocolExperiment()
135 const OVERRIDE; 131 const OVERRIDE;
136 132
137 // Gets a reference to the SettingsMap stored for a host. 133 // Gets a reference to the SettingsMap stored for a host.
138 // If no settings are stored, returns an empty SettingsMap. 134 // If no settings are stored, returns an empty SettingsMap.
139 virtual const net::SettingsMap& GetSpdySettings( 135 virtual const SettingsMap& GetSpdySettings(
140 const net::HostPortPair& host_port_pair) OVERRIDE; 136 const HostPortPair& host_port_pair) OVERRIDE;
141 137
142 // Saves an individual SPDY setting for a host. Returns true if SPDY setting 138 // Saves an individual SPDY setting for a host. Returns true if SPDY setting
143 // is to be persisted. 139 // is to be persisted.
144 virtual bool SetSpdySetting(const net::HostPortPair& host_port_pair, 140 virtual bool SetSpdySetting(const HostPortPair& host_port_pair,
145 net::SpdySettingsIds id, 141 SpdySettingsIds id,
146 net::SpdySettingsFlags flags, 142 SpdySettingsFlags flags,
147 uint32 value) OVERRIDE; 143 uint32 value) OVERRIDE;
148 144
149 // Clears all SPDY settings for a host. 145 // Clears all SPDY settings for a host.
150 virtual void ClearSpdySettings( 146 virtual void ClearSpdySettings(const HostPortPair& host_port_pair) OVERRIDE;
151 const net::HostPortPair& host_port_pair) OVERRIDE;
152 147
153 // Clears all SPDY settings for all hosts. 148 // Clears all SPDY settings for all hosts.
154 virtual void ClearAllSpdySettings() OVERRIDE; 149 virtual void ClearAllSpdySettings() OVERRIDE;
155 150
156 // Returns all SPDY persistent settings. 151 // Returns all SPDY persistent settings.
157 virtual const net::SpdySettingsMap& spdy_settings_map() const OVERRIDE; 152 virtual const SpdySettingsMap& spdy_settings_map() const OVERRIDE;
158 153
159 virtual void SetServerNetworkStats(const net::HostPortPair& host_port_pair, 154 virtual void SetServerNetworkStats(const HostPortPair& host_port_pair,
160 NetworkStats stats) OVERRIDE; 155 NetworkStats stats) OVERRIDE;
161 156
162 virtual const NetworkStats* GetServerNetworkStats( 157 virtual const NetworkStats* GetServerNetworkStats(
163 const net::HostPortPair& host_port_pair) const OVERRIDE; 158 const HostPortPair& host_port_pair) const OVERRIDE;
164 159
165 protected: 160 protected:
166 // -------------------- 161 // --------------------
167 // SPDY related methods 162 // SPDY related methods
168 163
169 // These are used to delay updating of the cached data in 164 // These are used to delay updating of the cached data in
170 // |http_server_properties_impl_| while the preferences are changing, and 165 // |http_server_properties_impl_| while the preferences are changing, and
171 // execute only one update per simultaneous prefs changes. 166 // execute only one update per simultaneous prefs changes.
172 void ScheduleUpdateCacheOnUI(); 167 void ScheduleUpdateCacheOnPref();
173 168
174 // Starts the timers to update the cached prefs. This are overridden in tests 169 // Starts the timers to update the cached prefs. This are overridden in tests
175 // to prevent the delay. 170 // to prevent the delay.
176 virtual void StartCacheUpdateTimerOnUI(base::TimeDelta delay); 171 virtual void StartCacheUpdateTimerOnPref(base::TimeDelta delay);
177 172
178 // Update cached prefs in |http_server_properties_impl_| with data from 173 // Update cached prefs in |http_server_properties_impl_| with data from
179 // preferences. It gets the data on UI thread and calls 174 // preferences. It gets the data on UI thread and calls
180 // UpdateSpdyServersFromPrefsOnIO() to perform the update on IO thread. 175 // UpdateSpdyServersFromPrefsOnIO() to perform the update on IO thread.
181 virtual void UpdateCacheFromPrefsOnUI(); 176 virtual void UpdateCacheFromPrefsOnPref();
182 177
183 // Starts the update of cached prefs in |http_server_properties_impl_| on the 178 // Starts the update of cached prefs in |http_server_properties_impl_| on the
184 // IO thread. Protected for testing. 179 // IO thread. Protected for testing.
185 void UpdateCacheFromPrefsOnIO( 180 void UpdateCacheFromPrefsOnIO(
186 std::vector<std::string>* spdy_servers, 181 std::vector<std::string>* spdy_servers,
187 net::SpdySettingsMap* spdy_settings_map, 182 SpdySettingsMap* spdy_settings_map,
188 net::AlternateProtocolMap* alternate_protocol_map, 183 AlternateProtocolMap* alternate_protocol_map,
189 net::AlternateProtocolExperiment alternate_protocol_experiment, 184 AlternateProtocolExperiment alternate_protocol_experiment,
190 bool detected_corrupted_prefs); 185 bool detected_corrupted_prefs);
191 186
192 // These are used to delay updating the preferences when cached data in 187 // These are used to delay updating the preferences when cached data in
193 // |http_server_properties_impl_| is changing, and execute only one update per 188 // |http_server_properties_impl_| is changing, and execute only one update per
194 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes. 189 // simultaneous spdy_servers or spdy_settings or alternate_protocol changes.
195 void ScheduleUpdatePrefsOnIO(); 190 void ScheduleUpdatePrefsOnIO();
196 191
197 // Starts the timers to update the prefs from cache. This are overridden in 192 // Starts the timers to update the prefs from cache. This are overridden in
198 // tests to prevent the delay. 193 // tests to prevent the delay.
199 virtual void StartPrefsUpdateTimerOnIO(base::TimeDelta delay); 194 virtual void StartPrefsUpdateTimerOnIO(base::TimeDelta delay);
200 195
201 // Update prefs::kHttpServerProperties in preferences with the cached data 196 // Update prefs::kHttpServerProperties in preferences with the cached data
202 // from |http_server_properties_impl_|. This gets the data on IO thread and 197 // from |http_server_properties_impl_|. This gets the data on IO thread and
203 // posts a task (UpdatePrefsOnUI) to update the preferences UI thread. 198 // posts a task (UpdatePrefsOnUI) to update the preferences UI thread.
204 void UpdatePrefsFromCacheOnIO(); 199 void UpdatePrefsFromCacheOnIO();
205 200
206 // Same as above, but fires an optional |completion| callback on the UI thread 201 // Same as above, but fires an optional |completion| callback on the UI thread
207 // when finished. Virtual for testing. 202 // when finished. Virtual for testing.
208 virtual void UpdatePrefsFromCacheOnIO(const base::Closure& completion); 203 virtual void UpdatePrefsFromCacheOnIO(const base::Closure& completion);
209 204
210 // Update prefs::kHttpServerProperties preferences on UI thread. Executes an 205 // Update prefs::kHttpServerProperties preferences on pref thread. Executes an
211 // optional |completion| callback when finished. Protected for testing. 206 // optional |completion| callback when finished. Protected for testing.
212 void UpdatePrefsOnUI( 207 void UpdatePrefsOnPref(base::ListValue* spdy_server_list,
Ryan Sleevi 2014/07/08 18:19:26 nit: naming could be clearer (OnPref -> OnPrefThre
mef 2014/07/09 12:12:04 Done.
213 base::ListValue* spdy_server_list, 208 SpdySettingsMap* spdy_settings_map,
214 net::SpdySettingsMap* spdy_settings_map, 209 AlternateProtocolMap* alternate_protocol_map,
215 net::AlternateProtocolMap* alternate_protocol_map, 210 const base::Closure& completion);
216 const base::Closure& completion);
217 211
218 private: 212 private:
219 void OnHttpServerPropertiesChanged(); 213 void OnHttpServerPropertiesChanged();
220 214
221 // --------- 215 // ---------
222 // UI thread 216 // UI thread
Ryan Sleevi 2014/07/08 18:19:26 out of date comment
mef 2014/07/09 12:12:04 Done.
223 // --------- 217 // ---------
224 218
219 const scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner_;
Ryan Sleevi 2014/07/08 18:19:26 STR seems fine here. Only requirement is it's the
mef 2014/07/09 12:12:04 Done.
220
225 // Used to get |weak_ptr_| to self on the UI thread. 221 // Used to get |weak_ptr_| to self on the UI thread.
226 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > 222 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
227 ui_weak_ptr_factory_; 223 pref_weak_ptr_factory_;
228 224
229 base::WeakPtr<HttpServerPropertiesManager> ui_weak_ptr_; 225 base::WeakPtr<HttpServerPropertiesManager> pref_weak_ptr_;
230 226
231 // Used to post cache update tasks. 227 // Used to post cache update tasks.
232 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> > 228 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
233 ui_cache_update_timer_; 229 pref_cache_update_timer_;
234 230
235 // Used to track the spdy servers changes. 231 // Used to track the spdy servers changes.
236 PrefChangeRegistrar pref_change_registrar_; 232 PrefChangeRegistrar pref_change_registrar_;
237 PrefService* pref_service_; // Weak. 233 PrefService* pref_service_; // Weak.
238 bool setting_prefs_; 234 bool setting_prefs_;
235 const char* path_;
239 236
240 // --------- 237 // ---------
241 // IO thread 238 // IO thread
242 // --------- 239 // ---------
243 240
241 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
242
244 // Used to get |weak_ptr_| to self on the IO thread. 243 // Used to get |weak_ptr_| to self on the IO thread.
245 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > 244 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> >
246 io_weak_ptr_factory_; 245 io_weak_ptr_factory_;
247 246
248 // Used to post |prefs::kHttpServerProperties| pref update tasks. 247 // Used to post |prefs::kHttpServerProperties| pref update tasks.
249 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> > 248 scoped_ptr<base::OneShotTimer<HttpServerPropertiesManager> >
250 io_prefs_update_timer_; 249 io_prefs_update_timer_;
251 250
252 scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_impl_; 251 scoped_ptr<HttpServerPropertiesImpl> http_server_properties_impl_;
253 252
254 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager); 253 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager);
255 }; 254 };
256 255
257 } // namespace chrome_browser_net 256 } // namespace chrome_browser_net
258 257
259 #endif // CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ 258 #endif // CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698