OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ | |
6 #define CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ | |
7 #pragma once | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/compiler_specific.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/task.h" | |
14 #include "chrome/browser/prefs/pref_change_registrar.h" | |
15 #include "content/common/notification_observer.h" | |
16 #include "net/base/host_port_pair.h" | |
17 #include "net/http/http_server_properties.h" | |
18 #include "net/http/http_server_properties_impl.h" | |
19 | |
20 class NotificationDetails; | |
21 class NotificationSource; | |
22 class PrefService; | |
23 | |
24 namespace base { | |
25 class ListValue; | |
26 } | |
27 | |
28 namespace chrome_browser_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 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. | |
40 // | |
41 // It must be constructed on the UI thread, to set up |ui_method_factory_| and | |
42 // the prefs listeners. | |
43 // | |
44 // ShutdownOnUIThread must be called from UI before destruction, to release | |
45 // the prefs listeners on the UI thread. This is done from ProfileIOData. | |
46 // | |
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 NotificationObserver { | |
55 public: | |
56 // Create an instance of the HttpServerPropertiesManager. The lifetime of the | |
57 // PrefService objects must be longer than that of the | |
58 // HttpServerPropertiesManager object. Must be constructed on the UI thread. | |
59 explicit HttpServerPropertiesManager(PrefService* pref_service); | |
60 virtual ~HttpServerPropertiesManager(); | |
61 | |
62 // Initialize |http_server_properties_impl_| and |io_method_factory_| on IO | |
63 // thread. It also posts a task to UI thread to get SPDY Server preferences | |
64 // from |pref_service_|. | |
65 void InitializeOnIOThread(); | |
66 | |
67 // Prepare for shutdown. Must be called on the UI thread, before destruction. | |
68 void ShutdownOnUIThread(); | |
69 | |
70 // Returns true if |server| supports SPDY. Should only be called from IO | |
71 // thread. | |
72 virtual bool SupportsSpdy(const net::HostPortPair& server) const OVERRIDE; | |
73 | |
74 // Add |server| as the SPDY server which supports SPDY protocol into the | |
75 // persisitent store. Should only be called from IO thread. | |
76 virtual void SetSupportsSpdy(const net::HostPortPair& server, | |
77 bool support_spdy) OVERRIDE; | |
78 | |
79 // Deletes all data. | |
80 virtual void DeleteAll() OVERRIDE; | |
81 | |
82 // Register |prefs| SPDY preferences. | |
83 static void RegisterPrefs(PrefService* prefs); | |
84 | |
85 protected: | |
86 // These are used to delay updating the spdy servers in | |
87 // |http_server_properties_impl_| while the preferences are changing, and | |
88 // execute only one update per simultaneous prefs changes. | |
89 void ScheduleUpdateCacheOnUI(); | |
90 virtual void PostUpdateTaskOnUI(Task* task); // Virtual for testing. | |
willchan no longer on Chromium
2011/10/07 01:45:15
Do these virtuals need to be protected? Are they a
ramant (doing other things)
2011/10/07 02:01:12
In the tests, we overwrite PostUpdateTaskOn* metho
| |
91 | |
92 // Update spdy servers (the cached data in |http_server_properties_impl_|) | |
93 // with data from preferences. Virtual for testing. | |
94 virtual void UpdateCacheFromPrefs(); | |
95 | |
96 // Starts the |spdy_servers| update on the IO thread. Protected for testing. | |
97 void UpdateCacheFromPrefsOnIO(StringVector* spdy_servers, bool support_spdy); | |
98 | |
99 // These are used to delay updating the preferences when spdy servers_ are | |
100 // changing, and execute only one update per simultaneous spdy server changes. | |
101 void ScheduleUpdatePrefsOnIO(); | |
102 virtual void PostUpdateTaskOnIO(Task* task); // Virtual for testing. | |
103 | |
104 // Update spdy servers in preferences with the cached data from | |
105 // |http_server_properties_impl_|. Virtual for testing. | |
106 virtual void UpdatePrefsFromCache(); // Virtual for testing. | |
107 | |
108 // Update |prefs::kSpdyServers| preferences with |spdy_server_list| on UI | |
109 // thread. Protected for testing. | |
110 void SetSpdyServersInPrefsOnUI(base::ListValue* spdy_server_list); | |
111 | |
112 private: | |
113 // Callback for preference changes. | |
114 virtual void Observe(int type, | |
115 const NotificationSource& source, | |
116 const NotificationDetails& details); | |
117 | |
118 // --------- | |
119 // UI thread | |
120 // --------- | |
121 | |
122 // Used to post update tasks to the UI thread. | |
123 ScopedRunnableMethodFactory<HttpServerPropertiesManager> ui_method_factory_; | |
124 | |
125 // Used to get |weak_ptr_| to self on the UI thread. | |
126 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > | |
127 ui_weak_ptr_factory_; | |
128 | |
129 base::WeakPtr<HttpServerPropertiesManager> ui_weak_ptr_; | |
130 | |
131 // Used to track the spdy servers changes. | |
132 PrefChangeRegistrar pref_change_registrar_; | |
133 PrefService* pref_service_; // Weak. | |
134 | |
135 // --------- | |
136 // IO thread | |
137 // --------- | |
138 | |
139 // Used to post update tasks to the IO thread. | |
140 scoped_ptr<ScopedRunnableMethodFactory<HttpServerPropertiesManager> > | |
141 io_method_factory_; | |
142 | |
143 scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_impl_; | |
144 | |
145 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager); | |
146 }; | |
147 | |
148 } // namespace chrome_browser_net | |
149 | |
150 #endif // CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ | |
OLD | NEW |