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

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

Powered by Google App Engine
This is Rietveld 408576698