[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/blacklist.h" |
| 6 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame^] | 7 | #include <algorithm> |
| 8 | |
| 9 | #include "base/bind.h" |
| 10 | #include "base/message_loop.h" |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_prefs.h" |
| 12 | #include "chrome/browser/prefs/pref_service.h" |
| 13 | #include "chrome/common/pref_names.h" |
| 14 | |
| 15 | namespace extensions { |
| 16 | |
| 17 | Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { |
| 18 | blacklist_->AddObserver(this); |
| 19 | } |
| 20 | |
| 21 | Blacklist::Observer::~Observer() { |
| 22 | blacklist_->RemoveObserver(this); |
| 23 | } |
| 24 | |
| 25 | Blacklist::Blacklist(ExtensionPrefs* prefs) : prefs_(prefs) { |
| 26 | } |
| 27 | |
| 28 | Blacklist::~Blacklist() { |
| 29 | } |
| 30 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame^] | 31 | void Blacklist::GetBlacklistedIDs(const std::set<std::string>& ids, |
| 32 | const GetBlacklistedIDsCallback& callback) { |
| 33 | // TODO(kalman): Get the blacklisted IDs from the safebrowsing list. |
| 34 | // This will require going to the IO thread and back. |
| 35 | std::set<std::string> blacklisted_ids; |
| 36 | for (std::set<std::string>::const_iterator it = ids.begin(); |
| 37 | it != ids.end(); ++it) { |
| 38 | if (prefs_->IsExtensionBlacklisted(*it)) |
| 39 | blacklisted_ids.insert(*it); |
| 40 | } |
| 41 | MessageLoop::current()->PostTask(FROM_HERE, |
| 42 | base::Bind(callback, blacklisted_ids)); |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void Blacklist::SetFromUpdater(const std::vector<std::string>& ids, |
| 46 | const std::string& version) { |
| 47 | std::set<std::string> ids_as_set; |
| 48 | for (std::vector<std::string>::const_iterator it = ids.begin(); |
| 49 | it != ids.end(); ++it) { |
| 50 | if (Extension::IdIsValid(*it)) |
| 51 | ids_as_set.insert(*it); |
| 52 | else |
| 53 | LOG(WARNING) << "Got invalid extension ID \"" << *it << "\""; |
| 54 | } |
| 55 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame^] | 56 | std::set<std::string> from_prefs = prefs_->GetBlacklistedExtensions(); |
| 57 | |
| 58 | std::set<std::string> no_longer_blacklisted; |
| 59 | std::set_difference(from_prefs.begin(), from_prefs.end(), |
| 60 | ids_as_set.begin(), ids_as_set.end(), |
| 61 | std::inserter(no_longer_blacklisted, |
| 62 | no_longer_blacklisted.begin())); |
| 63 | std::set<std::string> not_yet_blacklisted; |
| 64 | std::set_difference(ids_as_set.begin(), ids_as_set.end(), |
| 65 | from_prefs.begin(), from_prefs.end(), |
| 66 | std::inserter(not_yet_blacklisted, |
| 67 | not_yet_blacklisted.begin())); |
| 68 | |
| 69 | for (std::set<std::string>::iterator it = no_longer_blacklisted.begin(); |
| 70 | it != no_longer_blacklisted.end(); ++it) { |
| 71 | prefs_->SetExtensionBlacklisted(*it, false); |
| 72 | } |
| 73 | for (std::set<std::string>::iterator it = not_yet_blacklisted.begin(); |
| 74 | it != not_yet_blacklisted.end(); ++it) { |
| 75 | prefs_->SetExtensionBlacklisted(*it, true); |
| 76 | } |
| 77 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 78 | prefs_->pref_service()->SetString(prefs::kExtensionBlacklistUpdateVersion, |
| 79 | version); |
| 80 | |
| 81 | FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); |
| 82 | } |
| 83 | |
| 84 | void Blacklist::AddObserver(Observer* observer) { |
| 85 | observers_.AddObserver(observer); |
| 86 | } |
| 87 | |
| 88 | void Blacklist::RemoveObserver(Observer* observer) { |
| 89 | observers_.RemoveObserver(observer); |
| 90 | } |
| 91 | |
| 92 | } // namespace extensions |