| OLD | NEW |
| 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> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/message_loop.h" |
| 7 #include "chrome/browser/extensions/extension_prefs.h" | 11 #include "chrome/browser/extensions/extension_prefs.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 10 | 14 |
| 11 namespace extensions { | 15 namespace extensions { |
| 12 | 16 |
| 13 Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { | 17 Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { |
| 14 blacklist_->AddObserver(this); | 18 blacklist_->AddObserver(this); |
| 15 } | 19 } |
| 16 | 20 |
| 17 Blacklist::Observer::~Observer() { | 21 Blacklist::Observer::~Observer() { |
| 18 blacklist_->RemoveObserver(this); | 22 blacklist_->RemoveObserver(this); |
| 19 } | 23 } |
| 20 | 24 |
| 21 Blacklist::Blacklist(ExtensionPrefs* prefs) : prefs_(prefs) { | 25 Blacklist::Blacklist(ExtensionPrefs* prefs) : prefs_(prefs) { |
| 22 } | 26 } |
| 23 | 27 |
| 24 Blacklist::~Blacklist() { | 28 Blacklist::~Blacklist() { |
| 25 } | 29 } |
| 26 | 30 |
| 27 bool Blacklist::IsBlacklisted(const Extension* extension) const { | 31 void Blacklist::GetBlacklistedIDs(const std::set<std::string>& ids, |
| 28 return prefs_->IsExtensionBlacklisted(extension->id()); | 32 const GetBlacklistedIDsCallback& callback) { |
| 29 } | 33 // TODO(kalman): Get the blacklisted IDs from the safebrowsing list. |
| 30 | 34 // This will require going to the IO thread and back. |
| 31 bool Blacklist::IsBlacklisted(const std::string& extension_id) const { | 35 std::set<std::string> blacklisted_ids; |
| 32 return prefs_->IsExtensionBlacklisted(extension_id); | 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)); |
| 33 } | 43 } |
| 34 | 44 |
| 35 void Blacklist::SetFromUpdater(const std::vector<std::string>& ids, | 45 void Blacklist::SetFromUpdater(const std::vector<std::string>& ids, |
| 36 const std::string& version) { | 46 const std::string& version) { |
| 37 std::set<std::string> ids_as_set; | 47 std::set<std::string> ids_as_set; |
| 38 for (std::vector<std::string>::const_iterator it = ids.begin(); | 48 for (std::vector<std::string>::const_iterator it = ids.begin(); |
| 39 it != ids.end(); ++it) { | 49 it != ids.end(); ++it) { |
| 40 if (Extension::IdIsValid(*it)) | 50 if (Extension::IdIsValid(*it)) |
| 41 ids_as_set.insert(*it); | 51 ids_as_set.insert(*it); |
| 42 else | 52 else |
| 43 LOG(WARNING) << "Got invalid extension ID \"" << *it << "\""; | 53 LOG(WARNING) << "Got invalid extension ID \"" << *it << "\""; |
| 44 } | 54 } |
| 45 | 55 |
| 46 prefs_->UpdateBlacklist(ids_as_set); | 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 |
| 47 prefs_->pref_service()->SetString(prefs::kExtensionBlacklistUpdateVersion, | 78 prefs_->pref_service()->SetString(prefs::kExtensionBlacklistUpdateVersion, |
| 48 version); | 79 version); |
| 49 | 80 |
| 50 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); | 81 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); |
| 51 } | 82 } |
| 52 | 83 |
| 53 void Blacklist::AddObserver(Observer* observer) { | 84 void Blacklist::AddObserver(Observer* observer) { |
| 54 observers_.AddObserver(observer); | 85 observers_.AddObserver(observer); |
| 55 } | 86 } |
| 56 | 87 |
| 57 void Blacklist::RemoveObserver(Observer* observer) { | 88 void Blacklist::RemoveObserver(Observer* observer) { |
| 58 observers_.RemoveObserver(observer); | 89 observers_.RemoveObserver(observer); |
| 59 } | 90 } |
| 60 | 91 |
| 61 } // namespace extensions | 92 } // namespace extensions |
| OLD | NEW |