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

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 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
60 DCHECK(pref_service);
61 ui_weak_ptr_factory_.reset(
62 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
63 ui_weak_ptr_ = ui_weak_ptr_factory_->GetWeakPtr();
64 pref_change_registrar_.Init(pref_service_);
65 pref_change_registrar_.Add(prefs::kSpdyServers, this);
66 }
67
68 HttpServerPropertiesManager::~HttpServerPropertiesManager() {
69 }
70
71 void HttpServerPropertiesManager::InitializeOnIOThread() {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
73 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl());
74
75 io_method_factory_.reset(
76 new ScopedRunnableMethodFactory<HttpServerPropertiesManager>(this));
77
78 BrowserThread::PostTask(
79 BrowserThread::UI,
80 FROM_HERE,
81 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefs,
82 ui_weak_ptr_));
83 }
84
85 void HttpServerPropertiesManager::ShutdownOnUIThread() {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
87 // Cancel any pending updates, and stop listening for pref change updates.
88 ui_method_factory_.RevokeAll();
89 ui_weak_ptr_factory_.reset();
90 pref_change_registrar_.RemoveAll();
91 }
92
93 bool HttpServerPropertiesManager::SupportsSpdy(
94 const net::HostPortPair& server) const {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
96 return http_server_properties_impl_->SupportsSpdy(server);
97 }
98
99 void HttpServerPropertiesManager::SetSupportsSpdy(
100 const net::HostPortPair& server,
101 bool support_spdy) {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
103
104 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy);
105 ScheduleUpdatePrefsOnIO();
106 }
107
108 void HttpServerPropertiesManager::DeleteAll() {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
110
111 http_server_properties_impl_->DeleteAll();
112 ScheduleUpdatePrefsOnIO();
113 }
114
115 // static
116 void HttpServerPropertiesManager::RegisterPrefs(PrefService* prefs) {
117 prefs->RegisterListPref(prefs::kSpdyServers, PrefService::UNSYNCABLE_PREF);
118 }
119
120 //
121 // Update spdy_servers (the cached data) with data from preferences.
122 //
123 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
125 // Cancel pending updates, if any.
126 ui_method_factory_.RevokeAll();
127 PostUpdateTaskOnUI(
128 ui_method_factory_.NewRunnableMethod(
129 &HttpServerPropertiesManager::UpdateCacheFromPrefs));
130 }
131
132 void HttpServerPropertiesManager::PostUpdateTaskOnUI(Task* task) {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
134 // This is overridden in tests to post the task without the delay.
135 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdateCacheDelayMs);
136 }
137
138 void HttpServerPropertiesManager::UpdateCacheFromPrefs() {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
140
141 if (!pref_service_->HasPrefPath(prefs::kSpdyServers))
142 return;
143
144 // The preferences can only be read on the UI thread.
145 StringVector* spdy_servers =
146 ListValueToStringVector(pref_service_->GetList(prefs::kSpdyServers));
147
148 // Go through the IO thread to grab a WeakPtr to |this|. This is safe from
149 // here, since this task will always execute before a potential deletion of
150 // ProfileIOData on IO.
151 BrowserThread::PostTask(
152 BrowserThread::IO,
153 FROM_HERE,
154 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO,
155 base::Unretained(this), spdy_servers, true));
156 }
157
158 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO(
159 StringVector* spdy_servers,
160 bool support_spdy) {
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
162 // Preferences have the master data because admins might have pushed new
163 // preferences. Clear the cached data and use the new spdy server list from
164 // preferences.
165 scoped_ptr<StringVector> scoped_spdy_servers(spdy_servers);
166 http_server_properties_impl_->Initialize(spdy_servers, support_spdy);
167 }
168
169 //
170 // Update Preferences with data from spdy_servers (the cached data).
171 //
172 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
174 // Cancel pending updates, if any.
175 io_method_factory_->RevokeAll();
176 PostUpdateTaskOnIO(
177 io_method_factory_->NewRunnableMethod(
178 &HttpServerPropertiesManager::UpdatePrefsFromCache));
179 }
180
181 void HttpServerPropertiesManager::PostUpdateTaskOnIO(Task* task) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
183 // This is overridden in tests to post the task without the delay.
184 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdatePrefsDelayMs);
185 }
186
187 void HttpServerPropertiesManager::UpdatePrefsFromCache() {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
189
190 base::ListValue* spdy_server_list = new ListValue;
191
192 http_server_properties_impl_->GetSpdyServerList(spdy_server_list);
193
194 // Update the preferences on the UI thread.
willchan no longer on Chromium 2011/10/07 19:17:47 Ah yes, this task is not getting run, so this is g
ramant (doing other things) 2011/10/08 21:51:40 Done.
195 BrowserThread::PostTask(
196 BrowserThread::UI,
197 FROM_HERE,
198 base::Bind(&HttpServerPropertiesManager::SetSpdyServersInPrefsOnUI,
199 ui_weak_ptr_, spdy_server_list));
200 }
201
202 void HttpServerPropertiesManager::SetSpdyServersInPrefsOnUI(
203 base::ListValue* spdy_server_list) {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
205 scoped_ptr<base::ListValue> scoped_spdy_servers_list(spdy_server_list);
206 pref_service_->Set(prefs::kSpdyServers, *spdy_server_list);
207 }
208
209 void HttpServerPropertiesManager::Observe(int type,
210 const NotificationSource& source,
211 const NotificationDetails& details) {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
213 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED);
214 PrefService* prefs = Source<PrefService>(source).ptr();
215 DCHECK(prefs == pref_service_);
216 std::string* pref_name = Details<std::string>(details).ptr();
217 DCHECK(*pref_name == prefs::kSpdyServers);
218 ScheduleUpdateCacheOnUI();
219 }
220
221 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698