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

Side by Side Diff: chrome/browser/net/http_server_properties_manager.cc

Issue 7827033: Introduce net::HttpServerPropertiesManager to manage server-specific properties. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 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 | Annotate | Revision Log
OLDNEW
(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 #include "chrome/browser/net/http_server_properties_manager.h"
5
6 #include "base/bind.h"
7 #include "base/stl_util.h"
8 #include "base/values.h"
9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/common/chrome_notification_types.h"
11 #include "chrome/common/pref_names.h"
12 #include "content/browser/browser_thread.h"
13 #include "content/common/notification_details.h"
14 #include "content/common/notification_source.h"
15
16 namespace chrome_browser_net {
17
18 namespace {
19
20 // Time to wait before starting an update the spdy_servers_table_ cache from
21 // preferences. Scheduling another update during this period will reset the
22 // timer.
23 const int64 kUpdateCacheDelayMs = 1000;
24
25 // Time to wait before starting an update the preferences from the
26 // spdy_servers cache. Scheduling another update during this period will
27 // reset the timer.
28 const int64 kUpdatePrefsDelayMs = 5000;
29
30 typedef std::vector<std::string> StringVector;
31
32 // String is host/port pair of spdy server.
33 StringVector* ListValueToStringVector(const base::ListValue* list) {
34 StringVector* vector = new StringVector;
35
36 if (!list)
37 return vector;
38
39 vector->reserve(list->GetSize());
40 std::string s;
41 for (base::ListValue::const_iterator it = list->begin();
42 it != list->end(); ++it) {
43 if ((*it)->GetAsString(&s))
44 vector->push_back(s);
45 }
46
47 return vector;
48 }
49
50 } // namespace
51
52 ////////////////////////////////////////////////////////////////////////////////
53 // HttpServerPropertiesManager
54
55 HttpServerPropertiesManager::HttpServerPropertiesManager(
56 PrefService* pref_service)
57 : ALLOW_THIS_IN_INITIALIZER_LIST(ui_method_factory_(this)),
58 pref_service_(pref_service),
59 ALLOW_THIS_IN_INITIALIZER_LIST(io_method_factory_(this)),
60 ALLOW_THIS_IN_INITIALIZER_LIST(io_weak_ptr_factory_(this)) {
willchan no longer on Chromium 2011/10/02 18:33:13 For the io_method_factory_ and io_weak_ptr_factory
ramant (doing other things) 2011/10/04 05:23:24 Done.
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62 DCHECK(pref_service);
63
64 pref_change_registrar_.Init(pref_service_);
65 pref_change_registrar_.Add(prefs::kSpdyServers, this);
66
67 if (pref_service_->HasPrefPath(prefs::kSpdyServers))
68 UpdateCacheFromPrefs();
69 }
70
71 HttpServerPropertiesManager::~HttpServerPropertiesManager() {
72 }
73
74 void HttpServerPropertiesManager::ShutdownOnUIThread() {
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
76 // Cancel any pending updates, and stop listening for pref change updates.
77 ui_method_factory_.RevokeAll();
78 io_method_factory_.RevokeAll();
willchan no longer on Chromium 2011/10/02 18:33:13 ScopedRunnableMethodFactory isn't threadsafe. You
ramant (doing other things) 2011/10/04 05:23:24 Done.
79 pref_change_registrar_.RemoveAll();
80 }
81
82 bool HttpServerPropertiesManager::SupportsSpdy(
83 const net::HostPortPair& host_port_pair) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
85 return HttpServerProperties::SupportsSpdy(host_port_pair);
86 }
87
88 void HttpServerPropertiesManager::SetSupportsSpdy(
89 const net::HostPortPair& host_port_pair,
90 bool support_spdy) {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
92
93 HttpServerProperties::SetSupportsSpdy(host_port_pair, support_spdy);
94 ScheduleUpdateOnIO();
95 }
96
97 // static
98 void HttpServerPropertiesManager::RegisterPrefs(PrefService* prefs) {
willchan no longer on Chromium 2011/10/02 18:33:13 You should assert that this is only called from th
ramant (doing other things) 2011/10/04 05:23:24 Hi willchan, Some of the unit tests are register
99 prefs->RegisterListPref(prefs::kSpdyServers, PrefService::UNSYNCABLE_PREF);
100 }
101
102 //
103 // Update spdy_servers (the cached data) with data from preferences.
104 //
105 void HttpServerPropertiesManager::ScheduleUpdateOnUI() {
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
107 // Cancel pending updates, if any.
108 ui_method_factory_.RevokeAll();
109 PostUpdateTaskOnUI(
110 ui_method_factory_.NewRunnableMethod(
111 &HttpServerPropertiesManager::UpdateCacheFromPrefs));
112 }
113
114 void HttpServerPropertiesManager::PostUpdateTaskOnUI(Task* task) {
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
116 // This is overridden in tests to post the task without the delay.
117 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdateCacheDelayMs);
118 }
119
120 void HttpServerPropertiesManager::UpdateCacheFromPrefs() {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
122
123 // The preferences can only be read on the UI thread.
124 StringVector* spdy_servers =
125 ListValueToStringVector(pref_service_->GetList(prefs::kSpdyServers));
126
127 // Go through the IO thread to grab a WeakPtr to |this|. This is safe from
128 // here, since this task will always execute before a potential deletion of
129 // ProfileIOData on IO.
130 BrowserThread::PostTask(
131 BrowserThread::IO,
132 FROM_HERE,
133 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO,
134 base::Unretained(this), spdy_servers, true));
135 }
136
137 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO(
138 StringVector* spdy_servers,
139 bool support_spdy) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
141 // Preferences have the master data because admins might have pushed new
142 // preferences. Clear the cached data and use the new spdy server list from
143 // preferences.
144 Initialize(spdy_servers, support_spdy);
145 }
146
147 //
148 // Update Preferences with data from spdy_servers (the cached data).
149 //
150 void HttpServerPropertiesManager::ScheduleUpdateOnIO() {
151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
152 // Cancel pending updates, if any.
153 io_method_factory_.RevokeAll();
154 PostUpdateTaskOnIO(
155 io_method_factory_.NewRunnableMethod(
156 &HttpServerPropertiesManager::UpdatePrefsFromCache));
157 }
158
159 void HttpServerPropertiesManager::PostUpdateTaskOnIO(Task* task) {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
161 // This is overridden in tests to post the task without the delay.
162 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdatePrefsDelayMs);
163 }
164
165 void HttpServerPropertiesManager::UpdatePrefsFromCache() {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
167
168 base::ListValue* spdy_server_list = new ListValue;
169
170 GetSpdyServerList(spdy_server_list);
171
172 // Update the preferences on the UI thread.
173 BrowserThread::PostTask(
174 BrowserThread::UI,
175 FROM_HERE,
176 base::Bind(&HttpServerPropertiesManager::SetSpdyServersInPrefsOnUI,
177 base::Unretained(this), spdy_server_list));
willchan no longer on Chromium 2011/10/02 18:33:13 From IO=>UI thread, this is where you can have a l
ramant (doing other things) 2011/10/04 05:23:24 Done.
178 }
179
180 void HttpServerPropertiesManager::SetSpdyServersInPrefsOnUI(
181 base::ListValue* spdy_server_list) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
183 pref_service_->Set(prefs::kSpdyServers, *spdy_server_list);
184 }
185
186 void HttpServerPropertiesManager::Observe(int type,
187 const NotificationSource& source,
188 const NotificationDetails& details) {
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
190 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED);
191 PrefService* prefs = Source<PrefService>(source).ptr();
192 DCHECK(prefs == pref_service_);
193 std::string* pref_name = Details<std::string>(details).ptr();
194 DCHECK(*pref_name == prefs::kSpdyServers);
195 ScheduleUpdateOnUI();
196 }
197
198 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698