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

Side by Side Diff: chrome/browser/policy/url_blacklist_manager.cc

Issue 7747018: Introduced the URLBlacklistManager, and wired it to various places. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reviewed, added netlog Created 9 years, 3 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
5 #include "chrome/browser/policy/url_blacklist_manager.h"
6
7 #include "base/values.h"
8 #include "chrome/browser/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.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 #include "googleurl/src/gurl.h"
16
17 namespace policy {
18
19 namespace {
20
21 // Time to wait before starting an update of the blacklist. Scheduling another
22 // update during this period will reset the timer.
23 const int64 kUpdateDelayMs = 1000;
24
25 // Maximum filters per policy. Filters over this index are ignored.
26 const size_t kMaxFiltersPerPolicy = 100;
27
28 typedef std::vector<std::string> StringVector;
29
30 StringVector* ListValueToStringVector(const base::ListValue* list) {
31 StringVector* vector = new StringVector;
32
33 if (!list)
34 return vector;
35
36 vector->reserve(list->GetSize());
37 std::string s;
38 for (base::ListValue::const_iterator it = list->begin();
39 it != list->end() && vector->size() < kMaxFiltersPerPolicy; ++it) {
40 if ((*it)->GetAsString(&s))
41 vector->push_back(s);
42 }
43
44 return vector;
45 }
46
47 } // namespace
48
49 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612
50 class Blacklist {
51 public:
52 Blacklist() {
53 }
54
55 private:
56 DISALLOW_COPY_AND_ASSIGN(Blacklist);
57 };
58
59 namespace {
60
61 // A task that owns the Blacklist, and passes it to the URLBlacklistManager
62 // on the IO thread, if the URLBlacklistManager still exists.
63 class SetBlacklistTask : public Task {
willchan no longer on Chromium 2011/09/01 17:05:04 Rather than write a bunch of Tasks, you should pro
Joao da Silva 2011/09/02 14:31:02 The purpose of the tasks was to hold ownership of
64 public:
65 SetBlacklistTask(
66 base::WeakPtr<URLBlacklistManager> url_blacklist_manager,
67 Blacklist* blacklist)
68 : url_blacklist_manager_(url_blacklist_manager),
69 blacklist_(blacklist) {
70 }
71
72 virtual void Run() OVERRIDE {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
74 if (url_blacklist_manager_)
75 url_blacklist_manager_->SetBlacklist(blacklist_.release());
76 }
77
78 private:
79 base::WeakPtr<URLBlacklistManager> url_blacklist_manager_;
80 scoped_ptr<Blacklist> blacklist_;
81 DISALLOW_COPY_AND_ASSIGN(SetBlacklistTask);
82 };
83
84 // A task that builds the blacklist on the FILE thread, and then posts another
85 // task to pass it to the URLBlacklistManager on the IO thread.
86 class BuildBlacklistTask : public Task {
87 public:
88 BuildBlacklistTask(
89 base::WeakPtr<URLBlacklistManager> url_blacklist_manager,
90 StringVector* blacklist,
91 StringVector* whitelist)
92 : url_blacklist_manager_(url_blacklist_manager),
93 blacklist_(blacklist),
94 whitelist_(whitelist) {
95 }
96
97 virtual void Run() OVERRIDE {
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
99
100 Blacklist* blacklist = NULL;
101 if (!blacklist_->empty()) {
102 blacklist = new Blacklist;
103 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612
104 }
105
106 BrowserThread::PostTask(
107 BrowserThread::IO, FROM_HERE,
108 new SetBlacklistTask(url_blacklist_manager_, blacklist));
109 }
110
111 private:
112 base::WeakPtr<URLBlacklistManager> url_blacklist_manager_;
113 scoped_ptr<StringVector> blacklist_;
114 scoped_ptr<StringVector> whitelist_;
115 DISALLOW_COPY_AND_ASSIGN(BuildBlacklistTask);
116 };
117
118 } // namespace
119
120 URLBlacklistManager::URLBlacklistManager(Profile* profile)
121 : ALLOW_THIS_IN_INITIALIZER_LIST(ui_method_factory_(this)),
122 ALLOW_THIS_IN_INITIALIZER_LIST(io_weak_ptr_factory_(this)),
123 pref_service_(profile->GetPrefs()) {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
125 // This has to be created on the UI thread, but is only posted later, after
126 // the initialization on the IO thread is complete.
127 // Posting a task to invoke InitializeOnIOThread from here isn't safe, since
128 // we can't get weak ptrs for the IO thread yet.
129 initialize_on_ui_task_ = ui_method_factory_.NewRunnableMethod(
130 &URLBlacklistManager::InitializeOnUIThread);
131 }
132
133 void URLBlacklistManager::InitializeOnIOThread() {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
135 DCHECK(initialize_on_ui_task_ != NULL);
136 io_weak_ptr_factory_.DetachFromThread();
137 weak_ptr_ = io_weak_ptr_factory_.GetWeakPtr();
138
139 // It is now safe to resume initialization on the UI thread, since it is now
140 // possible to get weak refs to self to use on the IO thread.
141 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, initialize_on_ui_task_);
142 }
143
144 void URLBlacklistManager::InitializeOnUIThread() {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
146 DCHECK(initialize_on_ui_task_ != NULL);
147 initialize_on_ui_task_ = NULL;
148
149 pref_change_registrar_.Init(pref_service_);
150 pref_change_registrar_.Add(prefs::kUrlBlacklist, this);
151 pref_change_registrar_.Add(prefs::kUrlWhitelist, this);
152
153 // Start enforcing the policies without a delay when they are present at
154 // startup.
155 if (pref_service_->HasPrefPath(prefs::kUrlBlacklist))
156 Update();
157 }
158
159 void URLBlacklistManager::ShutdownOnUIThread() {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
161 // Cancel any pending updates, and stop listening for pref change updates.
162 // This also cancels a pending InitializeOnUIThread, if it hasn't executed
163 // yet.
164 ui_method_factory_.RevokeAll();
165 pref_change_registrar_.RemoveAll();
166 }
167
168 URLBlacklistManager::~URLBlacklistManager() {
169 // Cancel any weak ptrs on the IO thread. Pending tasks won't execute their
170 // updates anymore.
171 io_weak_ptr_factory_.InvalidateWeakPtrs();
willchan no longer on Chromium 2011/09/01 17:05:04 Unnecessary, the destructor for the factory will d
Joao da Silva 2011/09/02 14:31:02 Done.
172 }
173
174 void URLBlacklistManager::Observe(int type,
175 const NotificationSource& source,
176 const NotificationDetails& details) {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
178 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED);
179 PrefService* prefs = Source<PrefService>(source).ptr();
180 DCHECK(prefs == pref_service_);
181 std::string* pref_name = Details<std::string>(details).ptr();
182 DCHECK(*pref_name == prefs::kUrlBlacklist ||
183 *pref_name == prefs::kUrlWhitelist);
184 ScheduleUpdate();
185 }
186
187 void URLBlacklistManager::ScheduleUpdate() {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
189 // Cancel pending updates, if any.
190 ui_method_factory_.RevokeAll();
191 PostUpdateTask(
192 ui_method_factory_.NewRunnableMethod(&URLBlacklistManager::Update));
193 }
194
195 void URLBlacklistManager::PostUpdateTask(Task* task) {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
197 // This is overriden in tests to post the task without the delay.
198 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdateDelayMs);
199 }
200
201 void URLBlacklistManager::Update() {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
203
204 // The preferences can only be read on the UI thread.
205 StringVector* blacklist = ListValueToStringVector(
206 pref_service_->GetList(prefs::kUrlBlacklist));
207 StringVector* whitelist = ListValueToStringVector(
208 pref_service_->GetList(prefs::kUrlWhitelist));
209
210 BrowserThread::PostTask(
willchan no longer on Chromium 2011/09/01 17:05:04 Rather than going directly to the FILE thread, ple
Joao da Silva 2011/09/02 14:31:02 PostTaskAndReply turned out to be very cool for th
211 BrowserThread::FILE, FROM_HERE,
212 new BuildBlacklistTask(weak_ptr_, blacklist, whitelist));
213 }
214
215 void URLBlacklistManager::SetBlacklist(Blacklist* blacklist) {
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
217 blacklist_.reset(blacklist);
218 }
219
220 bool URLBlacklistManager::IsURLBlocked(const GURL& url) const {
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
222
223 if (!blacklist_.get())
willchan no longer on Chromium 2011/09/01 17:05:04 I prefer not to let variables be NULL if reasonabl
Joao da Silva 2011/09/02 14:31:02 Done.
224 return false;
225
226 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612
227 return false;
228 }
229
230 // static
231 void URLBlacklistManager::RegisterPrefs(PrefService* pref_service) {
232 pref_service->RegisterListPref(prefs::kUrlBlacklist,
233 PrefService::UNSYNCABLE_PREF);
234 pref_service->RegisterListPref(prefs::kUrlWhitelist,
235 PrefService::UNSYNCABLE_PREF);
236 }
237
238 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698