// Copyright 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/extensions/blacklist.h" #include #include "base/bind.h" #include "base/message_loop.h" #include "chrome/browser/extensions/extension_prefs.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/common/pref_names.h" namespace extensions { Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { blacklist_->AddObserver(this); } Blacklist::Observer::~Observer() { blacklist_->RemoveObserver(this); } Blacklist::Blacklist(ExtensionPrefs* prefs) : prefs_(prefs) { } Blacklist::~Blacklist() { } void Blacklist::GetBlacklistedIDs(const std::set& ids, const GetBlacklistedIDsCallback& callback) { // TODO(kalman): Get the blacklisted IDs from the safebrowsing list. // This will require going to the IO thread and back. std::set blacklisted_ids; for (std::set::const_iterator it = ids.begin(); it != ids.end(); ++it) { if (prefs_->IsExtensionBlacklisted(*it)) blacklisted_ids.insert(*it); } MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, blacklisted_ids)); } void Blacklist::SetFromUpdater(const std::vector& ids, const std::string& version) { std::set ids_as_set; for (std::vector::const_iterator it = ids.begin(); it != ids.end(); ++it) { if (Extension::IdIsValid(*it)) ids_as_set.insert(*it); else LOG(WARNING) << "Got invalid extension ID \"" << *it << "\""; } std::set from_prefs = prefs_->GetBlacklistedExtensions(); std::set no_longer_blacklisted; std::set_difference(from_prefs.begin(), from_prefs.end(), ids_as_set.begin(), ids_as_set.end(), std::inserter(no_longer_blacklisted, no_longer_blacklisted.begin())); std::set not_yet_blacklisted; std::set_difference(ids_as_set.begin(), ids_as_set.end(), from_prefs.begin(), from_prefs.end(), std::inserter(not_yet_blacklisted, not_yet_blacklisted.begin())); for (std::set::iterator it = no_longer_blacklisted.begin(); it != no_longer_blacklisted.end(); ++it) { prefs_->SetExtensionBlacklisted(*it, false); } for (std::set::iterator it = not_yet_blacklisted.begin(); it != not_yet_blacklisted.end(); ++it) { prefs_->SetExtensionBlacklisted(*it, true); } prefs_->pref_service()->SetString(prefs::kExtensionBlacklistUpdateVersion, version); FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); } void Blacklist::AddObserver(Observer* observer) { observers_.AddObserver(observer); } void Blacklist::RemoveObserver(Observer* observer) { observers_.RemoveObserver(observer); } } // namespace extensions