| 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> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 Blacklist::ScopedDatabaseManagerForTest::ScopedDatabaseManagerForTest( | 134 Blacklist::ScopedDatabaseManagerForTest::ScopedDatabaseManagerForTest( |
| 135 scoped_refptr<SafeBrowsingDatabaseManager> database_manager) | 135 scoped_refptr<SafeBrowsingDatabaseManager> database_manager) |
| 136 : original_(GetDatabaseManager()) { | 136 : original_(GetDatabaseManager()) { |
| 137 SetDatabaseManager(database_manager); | 137 SetDatabaseManager(database_manager); |
| 138 } | 138 } |
| 139 | 139 |
| 140 Blacklist::ScopedDatabaseManagerForTest::~ScopedDatabaseManagerForTest() { | 140 Blacklist::ScopedDatabaseManagerForTest::~ScopedDatabaseManagerForTest() { |
| 141 SetDatabaseManager(original_); | 141 SetDatabaseManager(original_); |
| 142 } | 142 } |
| 143 | 143 |
| 144 Blacklist::Blacklist(ExtensionPrefs* prefs) : prefs_(prefs) { | 144 Blacklist::Blacklist(ExtensionPrefs* prefs) { |
| 145 scoped_refptr<SafeBrowsingDatabaseManager> database_manager = | 145 scoped_refptr<SafeBrowsingDatabaseManager> database_manager = |
| 146 g_database_manager.Get().get(); | 146 g_database_manager.Get().get(); |
| 147 if (database_manager.get()) { | 147 if (database_manager) { |
| 148 registrar_.Add( | 148 registrar_.Add( |
| 149 this, | 149 this, |
| 150 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 150 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
| 151 content::Source<SafeBrowsingDatabaseManager>(database_manager.get())); | 151 content::Source<SafeBrowsingDatabaseManager>(database_manager.get())); |
| 152 } | 152 } |
| 153 | 153 |
| 154 // TODO(kalman): Delete anything from the pref blacklist that is in the | 154 // Clear out the old prefs-backed blacklist, stored as empty extension entries |
| 155 // safebrowsing blacklist (of course, only entries for which the extension | 155 // with just a "blacklisted" property. |
| 156 // hasn't been installed). | |
| 157 // | 156 // |
| 158 // Or maybe just wait until we're able to delete the pref blacklist | 157 // TODO(kalman): Delete this block of code, see http://crbug.com/295882. |
| 159 // altogether (when we're sure it's a strict subset of the safebrowsing one). | 158 std::set<std::string> blacklisted = prefs->GetBlacklistedExtensions(); |
| 159 for (std::set<std::string>::iterator it = blacklisted.begin(); |
| 160 it != blacklisted.end(); ++it) { |
| 161 if (!prefs->GetInstalledExtensionInfo(*it)) |
| 162 prefs->DeleteExtensionPrefs(*it); |
| 163 } |
| 160 } | 164 } |
| 161 | 165 |
| 162 Blacklist::~Blacklist() { | 166 Blacklist::~Blacklist() { |
| 163 } | 167 } |
| 164 | 168 |
| 165 void Blacklist::GetBlacklistedIDs(const std::set<std::string>& ids, | 169 void Blacklist::GetBlacklistedIDs(const std::set<std::string>& ids, |
| 166 const GetBlacklistedIDsCallback& callback) { | 170 const GetBlacklistedIDsCallback& callback) { |
| 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 168 | 172 |
| 169 if (ids.empty()) { | 173 if (ids.empty() || !g_database_manager.Get().get().get()) { |
| 170 base::MessageLoopProxy::current()->PostTask( | 174 base::MessageLoopProxy::current()->PostTask( |
| 171 FROM_HERE, | 175 FROM_HERE, base::Bind(callback, std::set<std::string>())); |
| 172 base::Bind(callback, std::set<std::string>())); | |
| 173 return; | |
| 174 } | |
| 175 | |
| 176 // The blacklisted IDs are the union of those blacklisted in prefs and | |
| 177 // those blacklisted from safe browsing. | |
| 178 std::set<std::string> pref_blacklisted_ids; | |
| 179 for (std::set<std::string>::const_iterator it = ids.begin(); | |
| 180 it != ids.end(); ++it) { | |
| 181 if (prefs_->IsExtensionBlacklisted(*it)) | |
| 182 pref_blacklisted_ids.insert(*it); | |
| 183 } | |
| 184 | |
| 185 if (!g_database_manager.Get().get().get()) { | |
| 186 base::MessageLoopProxy::current()->PostTask( | |
| 187 FROM_HERE, base::Bind(callback, pref_blacklisted_ids)); | |
| 188 return; | 176 return; |
| 189 } | 177 } |
| 190 | 178 |
| 191 // Constructing the SafeBrowsingClientImpl begins the process of asking | 179 // Constructing the SafeBrowsingClientImpl begins the process of asking |
| 192 // safebrowsing for the blacklisted extensions. | 180 // safebrowsing for the blacklisted extensions. |
| 193 new SafeBrowsingClientImpl( | 181 new SafeBrowsingClientImpl(ids, callback); |
| 194 ids, | |
| 195 base::Bind(&Blacklist::OnSafeBrowsingResponse, AsWeakPtr(), | |
| 196 pref_blacklisted_ids, | |
| 197 callback)); | |
| 198 } | 182 } |
| 199 | 183 |
| 200 void Blacklist::IsBlacklisted(const std::string& extension_id, | 184 void Blacklist::IsBlacklisted(const std::string& extension_id, |
| 201 const IsBlacklistedCallback& callback) { | 185 const IsBlacklistedCallback& callback) { |
| 202 std::set<std::string> check; | 186 std::set<std::string> check; |
| 203 check.insert(extension_id); | 187 check.insert(extension_id); |
| 204 GetBlacklistedIDs(check, base::Bind(&IsNotEmpty, callback)); | 188 GetBlacklistedIDs(check, base::Bind(&IsNotEmpty, callback)); |
| 205 } | 189 } |
| 206 | 190 |
| 207 void Blacklist::SetFromUpdater(const std::vector<std::string>& ids, | |
| 208 const std::string& version) { | |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 210 | |
| 211 std::set<std::string> ids_as_set; | |
| 212 for (std::vector<std::string>::const_iterator it = ids.begin(); | |
| 213 it != ids.end(); ++it) { | |
| 214 if (Extension::IdIsValid(*it)) | |
| 215 ids_as_set.insert(*it); | |
| 216 else | |
| 217 LOG(WARNING) << "Got invalid extension ID \"" << *it << "\""; | |
| 218 } | |
| 219 | |
| 220 std::set<std::string> from_prefs = prefs_->GetBlacklistedExtensions(); | |
| 221 | |
| 222 std::set<std::string> no_longer_blacklisted = | |
| 223 base::STLSetDifference<std::set<std::string> >(from_prefs, | |
| 224 ids_as_set); | |
| 225 std::set<std::string> not_yet_blacklisted = | |
| 226 base::STLSetDifference<std::set<std::string> >(ids_as_set, | |
| 227 from_prefs); | |
| 228 | |
| 229 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin(); | |
| 230 it != no_longer_blacklisted.end(); ++it) { | |
| 231 prefs_->SetExtensionBlacklisted(*it, false); | |
| 232 } | |
| 233 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin(); | |
| 234 it != not_yet_blacklisted.end(); ++it) { | |
| 235 prefs_->SetExtensionBlacklisted(*it, true); | |
| 236 } | |
| 237 | |
| 238 prefs_->pref_service()->SetString(prefs::kExtensionBlacklistUpdateVersion, | |
| 239 version); | |
| 240 | |
| 241 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); | |
| 242 } | |
| 243 | |
| 244 void Blacklist::AddObserver(Observer* observer) { | 191 void Blacklist::AddObserver(Observer* observer) { |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 246 observers_.AddObserver(observer); | 193 observers_.AddObserver(observer); |
| 247 } | 194 } |
| 248 | 195 |
| 249 void Blacklist::RemoveObserver(Observer* observer) { | 196 void Blacklist::RemoveObserver(Observer* observer) { |
| 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 251 observers_.RemoveObserver(observer); | 198 observers_.RemoveObserver(observer); |
| 252 } | 199 } |
| 253 | 200 |
| 254 // static | 201 // static |
| 255 void Blacklist::SetDatabaseManager( | 202 void Blacklist::SetDatabaseManager( |
| 256 scoped_refptr<SafeBrowsingDatabaseManager> database_manager) { | 203 scoped_refptr<SafeBrowsingDatabaseManager> database_manager) { |
| 257 g_database_manager.Get().set(database_manager); | 204 g_database_manager.Get().set(database_manager); |
| 258 } | 205 } |
| 259 | 206 |
| 260 // static | 207 // static |
| 261 scoped_refptr<SafeBrowsingDatabaseManager> Blacklist::GetDatabaseManager() { | 208 scoped_refptr<SafeBrowsingDatabaseManager> Blacklist::GetDatabaseManager() { |
| 262 return g_database_manager.Get().get(); | 209 return g_database_manager.Get().get(); |
| 263 } | 210 } |
| 264 | 211 |
| 265 void Blacklist::OnSafeBrowsingResponse( | |
| 266 const std::set<std::string>& pref_blacklisted_ids, | |
| 267 const GetBlacklistedIDsCallback& callback, | |
| 268 const std::set<std::string>& safebrowsing_blacklisted_ids) { | |
| 269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 270 | |
| 271 std::set<std::string> blacklist = pref_blacklisted_ids; | |
| 272 blacklist.insert(safebrowsing_blacklisted_ids.begin(), | |
| 273 safebrowsing_blacklisted_ids.end()); | |
| 274 | |
| 275 callback.Run(blacklist); | |
| 276 } | |
| 277 | |
| 278 void Blacklist::Observe(int type, | 212 void Blacklist::Observe(int type, |
| 279 const content::NotificationSource& source, | 213 const content::NotificationSource& source, |
| 280 const content::NotificationDetails& details) { | 214 const content::NotificationDetails& details) { |
| 281 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, type); | 215 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, type); |
| 282 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); | 216 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); |
| 283 } | 217 } |
| 284 | 218 |
| 285 } // namespace extensions | 219 } // namespace extensions |
| OLD | NEW |