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

Side by Side Diff: chrome/browser/extensions/blacklist.cc

Issue 49253005: Fetch extension blacklist states from SafeBrowsing server (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add unit tests, fix bugs. Created 7 years, 1 month 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
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/blacklist.h" 5 #include "chrome/browser/extensions/blacklist.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "chrome/browser/browser_process.h" 15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/extensions/blacklist_fetcher.h"
17 #include "chrome/browser/extensions/extension_prefs.h" 18 #include "chrome/browser/extensions/extension_prefs.h"
18 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
19 #include "chrome/browser/safe_browsing/safe_browsing_util.h" 20 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
20 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
21 #include "content/public/browser/notification_details.h" 22 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_source.h" 23 #include "content/public/browser/notification_source.h"
23 24
24 using content::BrowserThread; 25 using content::BrowserThread;
25 26
26 namespace extensions { 27 namespace extensions {
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 Blacklist::ScopedDatabaseManagerForTest::ScopedDatabaseManagerForTest( 148 Blacklist::ScopedDatabaseManagerForTest::ScopedDatabaseManagerForTest(
148 scoped_refptr<SafeBrowsingDatabaseManager> database_manager) 149 scoped_refptr<SafeBrowsingDatabaseManager> database_manager)
149 : original_(GetDatabaseManager()) { 150 : original_(GetDatabaseManager()) {
150 SetDatabaseManager(database_manager); 151 SetDatabaseManager(database_manager);
151 } 152 }
152 153
153 Blacklist::ScopedDatabaseManagerForTest::~ScopedDatabaseManagerForTest() { 154 Blacklist::ScopedDatabaseManagerForTest::~ScopedDatabaseManagerForTest() {
154 SetDatabaseManager(original_); 155 SetDatabaseManager(original_);
155 } 156 }
156 157
157 Blacklist::Blacklist(ExtensionPrefs* prefs) { 158 Blacklist::Blacklist(ExtensionPrefs* prefs) : fetcher_() {
not at google - send to devlin 2013/11/18 17:03:49 fetcher_() is implicit?
Oleg Eterevsky 2013/11/20 12:58:45 Removed.
158 scoped_refptr<SafeBrowsingDatabaseManager> database_manager = 159 scoped_refptr<SafeBrowsingDatabaseManager> database_manager =
159 g_database_manager.Get().get(); 160 g_database_manager.Get().get();
160 if (database_manager) { 161 if (database_manager) {
161 registrar_.Add( 162 registrar_.Add(
162 this, 163 this,
163 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, 164 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE,
164 content::Source<SafeBrowsingDatabaseManager>(database_manager.get())); 165 content::Source<SafeBrowsingDatabaseManager>(database_manager.get()));
165 } 166 }
166 167
167 // Clear out the old prefs-backed blacklist, stored as empty extension entries 168 // Clear out the old prefs-backed blacklist, stored as empty extension entries
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 const GetBlacklistedIDsCallback& callback, 217 const GetBlacklistedIDsCallback& callback,
217 const std::set<std::string>& blacklisted_ids) { 218 const std::set<std::string>& blacklisted_ids) {
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
219 220
220 std::set<std::string> ids_unknown_state; 221 std::set<std::string> ids_unknown_state;
221 BlacklistStateMap extensions_state; 222 BlacklistStateMap extensions_state;
222 for (std::set<std::string>::const_iterator it = blacklisted_ids.begin(); 223 for (std::set<std::string>::const_iterator it = blacklisted_ids.begin();
223 it != blacklisted_ids.end(); ++it) { 224 it != blacklisted_ids.end(); ++it) {
224 BlacklistStateMap::const_iterator cache_it = 225 BlacklistStateMap::const_iterator cache_it =
225 blacklist_state_cache_.find(*it); 226 blacklist_state_cache_.find(*it);
226 if (cache_it == blacklist_state_cache_.end()) 227 if (cache_it == blacklist_state_cache_.end() ||
228 cache_it->second == BLACKLISTED_UNKNOWN) // Do not return UNKNOWN
229 // from cache, retry request.
227 ids_unknown_state.insert(*it); 230 ids_unknown_state.insert(*it);
228 else 231 else
229 extensions_state[*it] = cache_it->second; 232 extensions_state[*it] = cache_it->second;
230 } 233 }
231 234
232 if (ids_unknown_state.empty()) { 235 if (ids_unknown_state.empty()) {
233 callback.Run(extensions_state); 236 callback.Run(extensions_state);
234 } else { 237 } else {
235 // After the extension blacklist states have been downloaded, call this 238 // After the extension blacklist states have been downloaded, call this
236 // functions again, but prevent infinite cycle in case server is offline 239 // functions again, but prevent infinite cycle in case server is offline
237 // or some other reason prevents us from receiving the blacklist state for 240 // or some other reason prevents us from receiving the blacklist state for
238 // these extensions. 241 // these extensions.
239 RequestExtensionsBlacklistState( 242 RequestExtensionsBlacklistState(
240 ids_unknown_state, 243 ids_unknown_state,
241 base::Bind(&Blacklist::ReturnBlacklistStateMap, AsWeakPtr(), 244 base::Bind(&Blacklist::ReturnBlacklistStateMap, AsWeakPtr(),
242 callback, blacklisted_ids)); 245 callback, blacklisted_ids));
243 } 246 }
244 } 247 }
245 248
246 void Blacklist::ReturnBlacklistStateMap( 249 void Blacklist::ReturnBlacklistStateMap(
247 const GetBlacklistedIDsCallback& callback, 250 const GetBlacklistedIDsCallback& callback,
248 const std::set<std::string>& blacklisted_ids) { 251 const std::set<std::string>& blacklisted_ids) {
249 BlacklistStateMap extensions_state; 252 BlacklistStateMap extensions_state;
250 for (std::set<std::string>::const_iterator it = blacklisted_ids.begin(); 253 for (std::set<std::string>::const_iterator it = blacklisted_ids.begin();
251 it != blacklisted_ids.end(); ++it) { 254 it != blacklisted_ids.end(); ++it) {
252 BlacklistStateMap::const_iterator cache_it = 255 BlacklistStateMap::const_iterator cache_it =
253 blacklist_state_cache_.find(*it); 256 blacklist_state_cache_.find(*it);
254 if (cache_it != blacklist_state_cache_.end()) 257 if (cache_it != blacklist_state_cache_.end()) {
255 extensions_state[*it] = cache_it->second; 258 extensions_state[*it] = cache_it->second;
259 }
not at google - send to devlin 2013/11/18 17:03:49 preferred the way it was
Oleg Eterevsky 2013/11/20 12:58:45 Must have added something and then removed. Change
256 // If for some reason we still haven't cached the state of this extension, 260 // If for some reason we still haven't cached the state of this extension,
257 // we silently skip it. 261 // we silently skip it.
258 } 262 }
259 263
260 callback.Run(extensions_state); 264 callback.Run(extensions_state);
261 } 265 }
262 266
263 void Blacklist::RequestExtensionsBlacklistState( 267 void Blacklist::RequestExtensionsBlacklistState(
264 const std::set<std::string> ids, base::Callback<void()> callback) { 268 const std::set<std::string> ids, base::Callback<void()> callback) {
not at google - send to devlin 2013/11/18 17:03:49 Both of these should have been const&; in the latt
Oleg Eterevsky 2013/11/20 12:58:45 Done.
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266 // This is a stub. The request will be made here, but the server is not up 270 if (!fetcher_) {
267 // yet. For compatibility with current blacklist logic, mark all extensions 271 fetcher_.reset(new BlacklistFetcher());
268 // as malicious. 272 }
not at google - send to devlin 2013/11/18 17:03:49 no {} around single-line if bodies
Oleg Eterevsky 2013/11/20 12:58:45 Done.
273
274 state_requests_.push_back(
275 make_pair(std::vector<std::string>(ids.begin(), ids.end()), callback));
269 for (std::set<std::string>::const_iterator it = ids.begin(); 276 for (std::set<std::string>::const_iterator it = ids.begin();
270 it != ids.end(); 277 it != ids.end();
271 ++it) { 278 ++it) {
272 blacklist_state_cache_[*it] = BLACKLISTED_MALWARE; 279 fetcher_->Request(*it, base::Bind(&Blacklist::OnBlacklistStateReceived,
280 AsWeakPtr(), *it));
273 } 281 }
274 callback.Run(); 282 }
283
284 void Blacklist::OnBlacklistStateReceived(std::string id, BlacklistState state) {
not at google - send to devlin 2013/11/18 17:03:49 const std::string& base::Bind can handle it.
Oleg Eterevsky 2013/11/20 12:58:45 Done.
285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
286 blacklist_state_cache_[id] = state;
287
288 StateRequestsList::iterator requests_it = state_requests_.begin();
not at google - send to devlin 2013/11/18 17:03:49 I'm not sure you need state_requests_ at all. Blac
Oleg Eterevsky 2013/11/20 12:58:45 I'm a bit reluctant about it. To do this we'll hav
not at google - send to devlin 2013/11/20 23:44:48 You would pass around a pointer with base::Owned()
Oleg Eterevsky 2013/11/21 16:53:13 base::Owned won't work here, since OnBlacklistStat
not at google - send to devlin 2013/11/21 18:42:57 You're right, and now that my memory is working co
289 while (requests_it != state_requests_.end()) {
290 const std::vector<std::string>& ids = requests_it->first;
291
292 bool have_all_in_cache = true;
293 for (std::vector<std::string>::const_iterator ids_it = ids.begin();
294 ids_it != ids.end();
295 ++ids_it) {
296 if (blacklist_state_cache_.find(*ids_it) ==
297 blacklist_state_cache_.end()) {
not at google - send to devlin 2013/11/18 17:03:49 since you're not holding onto the iterator result
Oleg Eterevsky 2013/11/20 12:58:45 I just googled "find vs count stl" and people tend
not at google - send to devlin 2013/11/20 23:44:48 I'm told you should use ContainsKey: https://code
Oleg Eterevsky 2013/11/21 16:53:13 Replaced it here and in other places. On 2013/11/
298 have_all_in_cache = false;
299 break;
300 }
301 }
302
303 if (have_all_in_cache) {
304 requests_it->second.Run();
305 requests_it = state_requests_.erase(requests_it);
306 } else {
307 ++requests_it;
308 }
309 }
310 }
311
312 void Blacklist::SetBlacklistFetcherForTest(BlacklistFetcher* fetcher) {
313 fetcher_.reset(fetcher);
275 } 314 }
276 315
277 void Blacklist::AddObserver(Observer* observer) { 316 void Blacklist::AddObserver(Observer* observer) {
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
279 observers_.AddObserver(observer); 318 observers_.AddObserver(observer);
280 } 319 }
281 320
282 void Blacklist::RemoveObserver(Observer* observer) { 321 void Blacklist::RemoveObserver(Observer* observer) {
283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
284 observers_.RemoveObserver(observer); 323 observers_.RemoveObserver(observer);
(...skipping 11 matching lines...) Expand all
296 } 335 }
297 336
298 void Blacklist::Observe(int type, 337 void Blacklist::Observe(int type,
299 const content::NotificationSource& source, 338 const content::NotificationSource& source,
300 const content::NotificationDetails& details) { 339 const content::NotificationDetails& details) {
301 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, type); 340 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, type);
302 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); 341 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated());
303 } 342 }
304 343
305 } // namespace extensions 344 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698