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

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: Rebased. Reviewed: don't use linked_ptrs 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/bind.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 #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 // A task that builds the blacklist on the FILE thread. Takes ownership
48 // of |block| and |allow| but not of |blacklist|.
49 void BuildBlacklist(Blacklist* blacklist,
50 StringVector* block,
51 StringVector* allow) {
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
53
54 scoped_ptr<StringVector> scoped_block(block);
55 scoped_ptr<StringVector> scoped_allow(allow);
56
57 // TODO(joaodasilva): This is a work in progress. http://crbug.com/49612
58 // Builds |blacklist| using the filters in |block| and |allow|.
59 }
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 void SetBlacklistOnIO(base::WeakPtr<URLBlacklistManager> url_blacklist_manager,
64 Blacklist* blacklist) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
66 if (url_blacklist_manager)
67 url_blacklist_manager->SetBlacklist(blacklist);
68 else
69 delete blacklist;
70 }
71
72 } // namespace
73
74 Blacklist::Blacklist() {
75 }
76
77 Blacklist::~Blacklist() {
78 }
79
80 URLBlacklistManager::URLBlacklistManager(PrefService* pref_service)
81 : ALLOW_THIS_IN_INITIALIZER_LIST(ui_method_factory_(this)),
82 pref_service_(pref_service),
83 ALLOW_THIS_IN_INITIALIZER_LIST(io_weak_ptr_factory_(this)),
84 blacklist_(new Blacklist) {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86
87 pref_change_registrar_.Init(pref_service_);
88 pref_change_registrar_.Add(prefs::kUrlBlacklist, this);
89 pref_change_registrar_.Add(prefs::kUrlWhitelist, this);
90
91 // Start enforcing the policies without a delay when they are present at
92 // startup.
93 if (pref_service_->HasPrefPath(prefs::kUrlBlacklist))
94 Update();
95 }
96
97 void URLBlacklistManager::ShutdownOnUIThread() {
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
99 // Cancel any pending updates, and stop listening for pref change updates.
100 ui_method_factory_.RevokeAll();
101 pref_change_registrar_.RemoveAll();
102 }
103
104 URLBlacklistManager::~URLBlacklistManager() {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
willchan no longer on Chromium 2011/09/03 20:28:04 OIC, this is causing you crashes on the linux_chro
Joao da Silva 2011/09/04 17:35:52 Done.
106 }
107
108 void URLBlacklistManager::Observe(int type,
109 const NotificationSource& source,
110 const NotificationDetails& details) {
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
112 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED);
113 PrefService* prefs = Source<PrefService>(source).ptr();
114 DCHECK(prefs == pref_service_);
115 std::string* pref_name = Details<std::string>(details).ptr();
116 DCHECK(*pref_name == prefs::kUrlBlacklist ||
117 *pref_name == prefs::kUrlWhitelist);
118 ScheduleUpdate();
119 }
120
121 void URLBlacklistManager::ScheduleUpdate() {
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
123 // Cancel pending updates, if any.
124 ui_method_factory_.RevokeAll();
125 PostUpdateTask(
126 ui_method_factory_.NewRunnableMethod(&URLBlacklistManager::Update));
127 }
128
129 void URLBlacklistManager::PostUpdateTask(Task* task) {
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
131 // This is overridden in tests to post the task without the delay.
132 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdateDelayMs);
133 }
134
135 void URLBlacklistManager::Update() {
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
137
138 // The preferences can only be read on the UI thread.
139 StringVector* block =
140 ListValueToStringVector(pref_service_->GetList(prefs::kUrlBlacklist));
141 StringVector* allow =
142 ListValueToStringVector(pref_service_->GetList(prefs::kUrlWhitelist));
143
144 // Go through the IO thread to grab a WeakPtr to |this|. This is safe from
145 // here, since this task will always execute before a potential deletion of
146 // ProfileIOData on IO.
147 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
willchan no longer on Chromium 2011/09/03 20:28:04 Please format like the style guide says: http://go
Joao da Silva 2011/09/04 17:35:52 Done.
148 base::Bind(&URLBlacklistManager::UpdateOnIO, base::Unretained(this),
149 block, allow));
150 }
151
152 void URLBlacklistManager::UpdateOnIO(StringVector* block, StringVector* allow) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
154 Blacklist* blacklist = new Blacklist;
155 // The Blacklist is built on the FILE thread. Once it's ready, it is passed
156 // to the URLBlacklistManager on IO.
157 // |blacklist|, |block| and |allow| can leak on the unlikely event of a
158 // policy update and shutdown happening at the same time.
159 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
willchan no longer on Chromium 2011/09/03 20:28:04 Ditto
Joao da Silva 2011/09/04 17:35:52 Done.
160 base::Bind(&BuildBlacklist, blacklist, block, allow),
161 base::Bind(&SetBlacklistOnIO, io_weak_ptr_factory_.GetWeakPtr(),
162 blacklist));
163 }
164
165 void URLBlacklistManager::SetBlacklist(Blacklist* blacklist) {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
167 blacklist_.reset(blacklist);
168 }
169
170 bool URLBlacklistManager::IsURLBlocked(const GURL& url) const {
171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
172
173 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612
174 return false;
175 }
176
177 // static
178 void URLBlacklistManager::RegisterPrefs(PrefService* pref_service) {
179 pref_service->RegisterListPref(prefs::kUrlBlacklist,
180 PrefService::UNSYNCABLE_PREF);
181 pref_service->RegisterListPref(prefs::kUrlWhitelist,
182 PrefService::UNSYNCABLE_PREF);
183 }
184
185 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698