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

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 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(URLBlacklist* 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 URLBlacklist, and passes it to the URLBlacklistManager
62 // on the IO thread, if the URLBlacklistManager still exists.
63 void SetBlacklistOnIO(base::WeakPtr<URLBlacklistManager> blacklist_manager,
64 URLBlacklist* blacklist) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
66 if (blacklist_manager)
67 blacklist_manager->SetBlacklist(blacklist);
68 else
69 delete blacklist;
70 }
71
72 } // namespace
73
74 URLBlacklist::URLBlacklist() {
75 }
76
77 URLBlacklist::~URLBlacklist() {
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 URLBlacklist) {
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 }
106
107 void URLBlacklistManager::Observe(int type,
108 const NotificationSource& source,
109 const NotificationDetails& details) {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
111 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED);
112 PrefService* prefs = Source<PrefService>(source).ptr();
113 DCHECK(prefs == pref_service_);
114 std::string* pref_name = Details<std::string>(details).ptr();
115 DCHECK(*pref_name == prefs::kUrlBlacklist ||
116 *pref_name == prefs::kUrlWhitelist);
117 ScheduleUpdate();
118 }
119
120 void URLBlacklistManager::ScheduleUpdate() {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
122 // Cancel pending updates, if any.
123 ui_method_factory_.RevokeAll();
124 PostUpdateTask(
125 ui_method_factory_.NewRunnableMethod(&URLBlacklistManager::Update));
126 }
127
128 void URLBlacklistManager::PostUpdateTask(Task* task) {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
130 // This is overridden in tests to post the task without the delay.
131 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdateDelayMs);
132 }
133
134 void URLBlacklistManager::Update() {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136
137 // The preferences can only be read on the UI thread.
138 StringVector* block =
139 ListValueToStringVector(pref_service_->GetList(prefs::kUrlBlacklist));
140 StringVector* allow =
141 ListValueToStringVector(pref_service_->GetList(prefs::kUrlWhitelist));
142
143 // Go through the IO thread to grab a WeakPtr to |this|. This is safe from
144 // here, since this task will always execute before a potential deletion of
145 // ProfileIOData on IO.
146 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
147 base::Bind(&URLBlacklistManager::UpdateOnIO,
148 base::Unretained(this), block, allow));
149 }
150
151 void URLBlacklistManager::UpdateOnIO(StringVector* block, StringVector* allow) {
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
153 URLBlacklist* blacklist = new URLBlacklist;
154 // The URLBlacklist is built on the FILE thread. Once it's ready, it is passed
155 // to the URLBlacklistManager on IO.
156 // |blacklist|, |block| and |allow| can leak on the unlikely event of a
157 // policy update and shutdown happening at the same time.
158 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE,
159 base::Bind(&BuildBlacklist,
160 blacklist, block, allow),
161 base::Bind(&SetBlacklistOnIO,
162 io_weak_ptr_factory_.GetWeakPtr(),
163 blacklist));
164 }
165
166 void URLBlacklistManager::SetBlacklist(URLBlacklist* blacklist) {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
168 blacklist_.reset(blacklist);
169 }
170
171 bool URLBlacklistManager::IsURLBlocked(const GURL& url) const {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
173
174 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612
175 return false;
176 }
177
178 // static
179 void URLBlacklistManager::RegisterPrefs(PrefService* pref_service) {
180 pref_service->RegisterListPref(prefs::kUrlBlacklist,
181 PrefService::UNSYNCABLE_PREF);
182 pref_service->RegisterListPref(prefs::kUrlWhitelist,
183 PrefService::UNSYNCABLE_PREF);
184 }
185
186 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/url_blacklist_manager.h ('k') | chrome/browser/policy/url_blacklist_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698