OLD | NEW |
---|---|
(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. | |
48 void BuildBlacklist(linked_ptr<Blacklist> blacklist, | |
49 linked_ptr<StringVector> block, | |
50 linked_ptr<StringVector> allow) { | |
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
52 | |
53 // TODO(joaodasilva): This is a work in progress. http://crbug.com/49612 | |
54 // Builds |blacklist| using the filters in |block| and |allow|. | |
55 } | |
56 | |
57 // A task that owns the Blacklist, and passes it to the URLBlacklistManager | |
58 // on the IO thread, if the URLBlacklistManager still exists. | |
59 void SetBlacklistOnIO(base::WeakPtr<URLBlacklistManager> url_blacklist_manager, | |
60 linked_ptr<Blacklist> blacklist) { | |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
62 if (url_blacklist_manager) | |
63 url_blacklist_manager->SetBlacklist(blacklist); | |
64 } | |
65 | |
66 } // namespace | |
67 | |
68 URLBlacklistManager::URLBlacklistManager(PrefService* pref_service) | |
69 : ALLOW_THIS_IN_INITIALIZER_LIST(ui_method_factory_(this)), | |
70 pref_service_(pref_service), | |
71 ALLOW_THIS_IN_INITIALIZER_LIST(io_weak_ptr_factory_(this)), | |
72 blacklist_(new Blacklist) { | |
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
74 | |
75 pref_change_registrar_.Init(pref_service_); | |
76 pref_change_registrar_.Add(prefs::kUrlBlacklist, this); | |
77 pref_change_registrar_.Add(prefs::kUrlWhitelist, this); | |
78 | |
79 // Start enforcing the policies without a delay when they are present at | |
80 // startup. | |
81 if (pref_service_->HasPrefPath(prefs::kUrlBlacklist)) | |
82 Update(); | |
83 } | |
84 | |
85 void URLBlacklistManager::ShutdownOnUIThread() { | |
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
87 // Cancel any pending updates, and stop listening for pref change updates. | |
88 ui_method_factory_.RevokeAll(); | |
89 pref_change_registrar_.RemoveAll(); | |
90 } | |
91 | |
92 URLBlacklistManager::~URLBlacklistManager() { | |
willchan no longer on Chromium
2011/09/02 16:39:26
Can you add a DCHECK here for BrowserThread::IO?
Joao da Silva
2011/09/02 17:35:20
Done.
| |
93 } | |
94 | |
95 void URLBlacklistManager::Observe(int type, | |
96 const NotificationSource& source, | |
97 const NotificationDetails& details) { | |
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
99 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); | |
100 PrefService* prefs = Source<PrefService>(source).ptr(); | |
101 DCHECK(prefs == pref_service_); | |
102 std::string* pref_name = Details<std::string>(details).ptr(); | |
103 DCHECK(*pref_name == prefs::kUrlBlacklist || | |
104 *pref_name == prefs::kUrlWhitelist); | |
105 ScheduleUpdate(); | |
106 } | |
107 | |
108 void URLBlacklistManager::ScheduleUpdate() { | |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
110 // Cancel pending updates, if any. | |
111 ui_method_factory_.RevokeAll(); | |
112 PostUpdateTask( | |
113 ui_method_factory_.NewRunnableMethod(&URLBlacklistManager::Update)); | |
114 } | |
115 | |
116 void URLBlacklistManager::PostUpdateTask(Task* task) { | |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
118 // This is overriden in tests to post the task without the delay. | |
willchan no longer on Chromium
2011/09/02 16:39:26
overridden
Joao da Silva
2011/09/02 17:35:20
Done.
| |
119 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdateDelayMs); | |
willchan no longer on Chromium
2011/09/02 16:39:26
I had toyed earlier with replacing this with base:
| |
120 } | |
121 | |
122 void URLBlacklistManager::Update() { | |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
124 | |
125 // The preferences can only be read on the UI thread. The linked_ptrs manage | |
126 // the memory while the task is in flight, and release it in case the task | |
127 // in cancelled (e.g. during shutdown). | |
128 linked_ptr<StringVector> block( | |
willchan no longer on Chromium
2011/09/02 16:39:26
linked_ptr doesn't work because it isn't threadsaf
Joao da Silva
2011/09/02 17:35:20
I'd really like to make sure there is always an ow
| |
129 ListValueToStringVector(pref_service_->GetList(prefs::kUrlBlacklist))); | |
130 linked_ptr<StringVector> allow( | |
131 ListValueToStringVector(pref_service_->GetList(prefs::kUrlWhitelist))); | |
132 | |
133 // Go through the IO thread to grab a WeakPtr to |this|. This is safe from | |
134 // here, since this task will always execute before a potential deletion of | |
135 // ProfileIOData on IO. | |
136 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
137 base::Bind(&URLBlacklistManager::UpdateOnIO, base::Unretained(this), | |
138 block, allow)); | |
139 } | |
140 | |
141 void URLBlacklistManager::UpdateOnIO(linked_ptr<StringVector> block, | |
142 linked_ptr<StringVector> allow) { | |
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
144 linked_ptr<Blacklist> blacklist(new Blacklist); | |
145 // The Blacklist is built on the FILE thread. Once it's ready, it is passed | |
146 // to the URLBlacklistManager on IO. | |
147 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | |
148 base::Bind(&BuildBlacklist, blacklist, block, allow), | |
149 base::Bind(&SetBlacklistOnIO, io_weak_ptr_factory_.GetWeakPtr(), | |
150 blacklist)); | |
151 } | |
152 | |
153 void URLBlacklistManager::SetBlacklist(linked_ptr<Blacklist> blacklist) { | |
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
155 blacklist_ = blacklist; | |
156 } | |
157 | |
158 bool URLBlacklistManager::IsURLBlocked(const GURL& url) const { | |
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
160 | |
161 // TODO(joaodasilva): this is a work in progress. http://crbug.com/49612 | |
162 return false; | |
163 } | |
164 | |
165 // static | |
166 void URLBlacklistManager::RegisterPrefs(PrefService* pref_service) { | |
167 pref_service->RegisterListPref(prefs::kUrlBlacklist, | |
168 PrefService::UNSYNCABLE_PREF); | |
169 pref_service->RegisterListPref(prefs::kUrlWhitelist, | |
170 PrefService::UNSYNCABLE_PREF); | |
171 } | |
172 | |
173 } // namespace policy | |
OLD | NEW |