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

Side by Side Diff: chrome/browser/policy/host_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, rebased Created 9 years, 4 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/host_blacklist_manager.h"
6
7 #include "base/task.h"
8 #include "base/values.h"
9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/chrome_notification_types.h"
12 #include "chrome/common/pref_names.h"
13 #include "content/browser/browser_thread.h"
14 #include "content/common/notification_details.h"
15 #include "content/common/notification_source.h"
16 #include "googleurl/src/gurl.h"
17
18 // A note on ownership of HostBlacklistManager:
19 // HostBlacklistManager is held mainly by ProfileImpl and its ProfileIOData.
20 // Its pending tasks can also hold references to it during destruction of the
21 // profile thought; however, those tasks only affect the internal state of
22 // HostBlacklistManager, and won't attempt to touch the profile or anything
23 // else. So in those cases, the HostBlacklistManager will be gone when the
24 // tasks are deleted.
25 //
26 // Its only client is the ChromeNetworkDelegate, which is also owned by the
27 // ProfileIOData. If the Profile is gone before the ChromeNetworkDelegate,
28 // the HostBlacklistManager won't see any more changes to the preferences,
29 // but it can still serve the ChromeNetworkDelegate until the ProfileIOData is
30 // deleted too.
31
32 namespace policy {
33
34 namespace {
35
36 // Time to wait before starting an update of the blacklist. Scheduling another
37 // update during this period will reset the timer.
38 const int64 kUpdateDelayMs = 5000;
39
40 // Maximum host filter per policy. Filters over this index are ignored.
41 const size_t kMaxHostsPerPolicy = 100;
42
43 typedef std::vector<std::string> StringVector;
44
45 StringVector* ListValueToStringVector(const base::ListValue* list) {
46 StringVector* vector = new StringVector;
47
48 if (!list)
49 return vector;
50
51 vector->reserve(list->GetSize());
52 std::string s;
53 for (base::ListValue::const_iterator it = list->begin();
54 it != list->end() && vector->size() < kMaxHostsPerPolicy; ++it) {
55 if ((*it)->GetAsString(&s))
56 vector->push_back(s);
57 }
58
59 return vector;
60 }
61
62 } // namespace
63
64 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612
65 class HostBlacklistManager::Blacklist {
66 public:
67 Blacklist() {
68 }
69
70 private:
71 DISALLOW_COPY_AND_ASSIGN(Blacklist);
72 };
73
74 // A task that also owns the StringVectors.
75 class BuildBlacklistTask : public Task {
76 public:
77 BuildBlacklistTask(
78 scoped_refptr<HostBlacklistManager> host_blacklist_manager,
79 StringVector* blacklist,
80 StringVector* whitelist)
81 : host_blacklist_manager_(host_blacklist_manager),
82 blacklist_(blacklist),
83 whitelist_(whitelist) {
84 }
85
86 virtual void Run() {
87 host_blacklist_manager_->BuildBlacklist(blacklist_.get(), whitelist_.get());
88 }
89
90 private:
91 scoped_refptr<HostBlacklistManager> host_blacklist_manager_;
92 scoped_ptr<StringVector> blacklist_;
93 scoped_ptr<StringVector> whitelist_;
94 DISALLOW_COPY_AND_ASSIGN(BuildBlacklistTask);
95 };
96
97 // A task that also owns the Blacklist.
98 class SetBlacklistTask : public Task {
99 public:
100 SetBlacklistTask(
101 scoped_refptr<HostBlacklistManager> host_blacklist_manager,
102 HostBlacklistManager::Blacklist* blacklist)
103 : host_blacklist_manager_(host_blacklist_manager),
104 blacklist_(blacklist) {
105 }
106
107 virtual void Run() {
108 host_blacklist_manager_->SetBlacklist(blacklist_.release());
109 }
110
111 private:
112 scoped_refptr<HostBlacklistManager> host_blacklist_manager_;
113 scoped_ptr<HostBlacklistManager::Blacklist> blacklist_;
114 DISALLOW_COPY_AND_ASSIGN(SetBlacklistTask);
115 };
116
117 HostBlacklistManager::HostBlacklistManager(Profile* profile)
118 : update_task_(NULL),
119 pref_service_(profile->GetPrefs()) {
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
121
122 pref_change_registrar_.Init(pref_service_);
123 pref_change_registrar_.Add(prefs::kHostBlacklist, this);
124 pref_change_registrar_.Add(prefs::kHostWhitelist, this);
125 }
126
127 void HostBlacklistManager::Initialize() {
128 // Start enforcing the policies without a delay when they are present at
129 // startup.
130 if (pref_service_->HasPrefPath(prefs::kHostBlacklist))
131 Update();
132 }
133
134 void HostBlacklistManager::ShutdownOnUIThread() {
135 pref_change_registrar_.RemoveAll();
136 }
137
138 HostBlacklistManager::~HostBlacklistManager() {
139 }
140
141 void HostBlacklistManager::Observe(int type,
142 const NotificationSource& source,
143 const NotificationDetails& details) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
145 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED);
146 PrefService* prefs = Source<PrefService>(source).ptr();
147 DCHECK(prefs == pref_service_);
148 std::string* pref_name = Details<std::string>(details).ptr();
149 DCHECK(*pref_name == prefs::kHostBlacklist ||
150 *pref_name == prefs::kHostWhitelist);
151 ScheduleUpdate();
152 }
153
154 void HostBlacklistManager::ScheduleUpdate() {
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
156 if (update_task_)
157 update_task_->Cancel();
158 update_task_ = NewRunnableMethod(this, &HostBlacklistManager::Update);
159 PostUpdateTask();
160 }
161
162 void HostBlacklistManager::PostUpdateTask() {
163 // This is overriden in tests to post the task without the delay.
164 MessageLoop::current()->PostDelayedTask(FROM_HERE, update_task_,
165 kUpdateDelayMs);
166 }
167
168 void HostBlacklistManager::Update() {
169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
170 update_task_ = NULL;
171
172 StringVector* blacklist = ListValueToStringVector(
173 pref_service_->GetList(prefs::kHostBlacklist));
174 StringVector* whitelist = ListValueToStringVector(
175 pref_service_->GetList(prefs::kHostWhitelist));
176
177 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
178 new BuildBlacklistTask(this, blacklist, whitelist));
179 }
180
181 void HostBlacklistManager::BuildBlacklist(const StringVector* host_blacklist,
182 const StringVector* host_whitelist) {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
184
185 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612
186
187 if (host_blacklist->empty()) {
188 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
189 new SetBlacklistTask(this, NULL));
190 return;
191 }
192
193 Blacklist* blacklist = new Blacklist;
194 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
195 new SetBlacklistTask(this, blacklist));
196 }
197
198 void HostBlacklistManager::SetBlacklist(Blacklist* blacklist) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
200 blacklist_.reset(blacklist);
201 }
202
203 bool HostBlacklistManager::ShouldBlockRequest(const GURL& url) const {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
205
206 if (!blacklist_.get())
207 return false;
208
209 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612
210 return false;
211 }
212
213 // static
214 void HostBlacklistManager::RegisterPrefs(PrefService* pref_service) {
215 pref_service->RegisterListPref(prefs::kHostBlacklist,
216 PrefService::UNSYNCABLE_PREF);
217 pref_service->RegisterListPref(prefs::kHostWhitelist,
218 PrefService::UNSYNCABLE_PREF);
219 }
220
221 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698