Chromium Code Reviews| 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::IsBlacklisted(const std::set<std::string>& ids, |
| 28 return prefs_->IsExtensionBlacklisted(extension->id()); | 32 const IsBlacklistedCallback& 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)); | |
| 29 } | 43 } |
| 30 | 44 |
| 31 bool Blacklist::IsBlacklisted(const std::string& extension_id) const { | 45 void Blacklist::IsBlacklisted(const std::string& id, |
| 32 return prefs_->IsExtensionBlacklisted(extension_id); | 46 const IsBlacklistedCallback& callback) { |
| 47 std::set<std::string> id_set; | |
| 48 id_set.insert(id); | |
| 49 IsBlacklisted(id_set, callback); | |
| 33 } | 50 } |
| 34 | 51 |
| 35 void Blacklist::SetFromUpdater(const std::vector<std::string>& ids, | 52 void Blacklist::SetFromUpdater(const std::vector<std::string>& ids, |
| 36 const std::string& version) { | 53 const std::string& version) { |
| 37 std::set<std::string> ids_as_set; | 54 std::set<std::string> ids_as_set; |
| 38 for (std::vector<std::string>::const_iterator it = ids.begin(); | 55 for (std::vector<std::string>::const_iterator it = ids.begin(); |
| 39 it != ids.end(); ++it) { | 56 it != ids.end(); ++it) { |
| 40 if (Extension::IdIsValid(*it)) | 57 if (Extension::IdIsValid(*it)) |
| 41 ids_as_set.insert(*it); | 58 ids_as_set.insert(*it); |
| 42 else | 59 else |
| 43 LOG(WARNING) << "Got invalid extension ID \"" << *it << "\""; | 60 LOG(WARNING) << "Got invalid extension ID \"" << *it << "\""; |
| 44 } | 61 } |
| 45 | 62 |
| 46 prefs_->UpdateBlacklist(ids_as_set); | 63 std::set<std::string> from_prefs = prefs_->GetBlacklistedExtensions(); |
| 64 | |
| 65 std::set<std::string> no_longer_blacklisted; | |
| 66 std::set_difference(from_prefs.begin(), from_prefs.end(), | |
| 67 ids_as_set.begin(), ids_as_set.end(), | |
| 68 std::inserter(no_longer_blacklisted, | |
| 69 no_longer_blacklisted.begin())); | |
| 70 std::set<std::string> not_yet_blacklisted; | |
| 71 std::set_difference(ids_as_set.begin(), ids_as_set.end(), | |
| 72 from_prefs.begin(), from_prefs.end(), | |
| 73 std::inserter(not_yet_blacklisted, | |
| 74 not_yet_blacklisted.begin())); | |
|
asargent_no_longer_on_chrome
2012/11/30 21:44:22
It would be sort of cool if we had a SetDifference
not at google - send to devlin
2012/11/30 23:09:54
Sounds good to me.
Here we go: https://codereview
| |
| 75 | |
| 76 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin(); | |
| 77 it != no_longer_blacklisted.end(); ++it) { | |
| 78 prefs_->SetExtensionBlacklisted(*it, false); | |
| 79 } | |
| 80 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin(); | |
| 81 it != not_yet_blacklisted.end(); ++it) { | |
| 82 prefs_->SetExtensionBlacklisted(*it, true); | |
| 83 } | |
|
not at google - send to devlin
2012/11/30 18:57:20
I'm thinking about moving the prefs modification s
asargent_no_longer_on_chrome
2012/11/30 21:49:59
Sorry, I missed this comment when going through th
not at google - send to devlin
2012/11/30 22:06:39
The nice thing about the safebrowsing databases is
not at google - send to devlin
2012/11/30 23:09:54
As you say, I think this change would make more se
Yoyo Zhou
2012/11/30 23:44:07
Just as a general comment, there should be less st
not at google - send to devlin
2012/12/01 02:51:30
My plan was to have Blacklist be essentially an in
| |
| 84 | |
| 47 prefs_->pref_service()->SetString(prefs::kExtensionBlacklistUpdateVersion, | 85 prefs_->pref_service()->SetString(prefs::kExtensionBlacklistUpdateVersion, |
| 48 version); | 86 version); |
| 49 | 87 |
| 50 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); | 88 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); |
| 51 } | 89 } |
| 52 | 90 |
| 53 void Blacklist::AddObserver(Observer* observer) { | 91 void Blacklist::AddObserver(Observer* observer) { |
| 54 observers_.AddObserver(observer); | 92 observers_.AddObserver(observer); |
| 55 } | 93 } |
| 56 | 94 |
| 57 void Blacklist::RemoveObserver(Observer* observer) { | 95 void Blacklist::RemoveObserver(Observer* observer) { |
| 58 observers_.RemoveObserver(observer); | 96 observers_.RemoveObserver(observer); |
| 59 } | 97 } |
| 60 | 98 |
| 61 } // namespace extensions | 99 } // namespace extensions |
| OLD | NEW |