1 // Copyright 2013 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/fake_safe_browsing_database_manager.h" 6 7 #include <algorithm> 8 #include <iterator> 9 #include <vector> 10 11 #include "base/bind_helpers.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/message_loop/message_loop_proxy.h" 14 #include "base/run_loop.h" 15 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 16 #include "chrome/browser/safe_browsing/safe_browsing_util.h" 17 18 namespace extensions { 19 20 FakeSafeBrowsingDatabaseManager::FakeSafeBrowsingDatabaseManager(bool enabled) 21 : SafeBrowsingDatabaseManager( 22 make_scoped_refptr(SafeBrowsingService::CreateSafeBrowsingService())), 23 enabled_(enabled) { 24 } 25 26 FakeSafeBrowsingDatabaseManager::~FakeSafeBrowsingDatabaseManager() { 27 } 28 29 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::Enable() { 30 enabled_ = true; 31 return *this; 32 } 33 34 FakeSafeBrowsingDatabaseManager& 35 FakeSafeBrowsingDatabaseManager::ClearUnsafe() { 36 unsafe_ids_.clear(); 37 return *this; 38 } 39 40 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe( 41 const std::string& a) { 42 ClearUnsafe(); 43 unsafe_ids_.insert(a); 44 return *this; 45 } 46 47 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe( 48 const std::string& a, const std::string& b) { 49 SetUnsafe(a); 50 unsafe_ids_.insert(b); 51 return *this; 52 } 53 54 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe( 55 const std::string& a, const std::string& b, const std::string& c) { 56 SetUnsafe(a, b); 57 unsafe_ids_.insert(c); 58 return *this; 59 } 60 61 void FakeSafeBrowsingDatabaseManager::NotifyUpdate() { 62 SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished(true); 63 } 64 65 bool FakeSafeBrowsingDatabaseManager::CheckExtensionIDs( 66 const std::set<std::string>& extension_ids, 67 Client* client) { 68 if (!enabled_) 69 return true; 70 71 // Need to construct the full SafeBrowsingCheck rather than calling 72 // OnCheckExtensionsResult directly because it's protected. Grr! 73 std::vector<std::string> extension_ids_vector(extension_ids.begin(), 74 extension_ids.end()); 75 std::vector<SBFullHash> extension_id_hashes; 76 std::transform(extension_ids_vector.begin(), extension_ids_vector.end(), 77 std::back_inserter(extension_id_hashes), 78 safe_browsing_util::StringToSBFullHash); 79 80 scoped_ptr<SafeBrowsingCheck> safe_browsing_check( 81 new SafeBrowsingCheck( 82 std::vector<GURL>(), 83 extension_id_hashes, 84 client, 85 safe_browsing_util::EXTENSIONBLACKLIST, 86 std::vector<SBThreatType>(1, SB_THREAT_TYPE_EXTENSION))); 87 88 for (size_t i = 0; i < extension_ids_vector.size(); ++i) { 89 const std::string& extension_id = extension_ids_vector[i]; 90 if (unsafe_ids_.count(extension_id)) 91 safe_browsing_check->full_hash_results[i] = SB_THREAT_TYPE_EXTENSION; 92 } 93 94 base::MessageLoopProxy::current()->PostTask( 95 FROM_HERE, 96 base::Bind(&FakeSafeBrowsingDatabaseManager::OnSafeBrowsingResult, 97 this, 98 base::Passed(&safe_browsing_check), 99 client)); 100 return false; 101 } 102 103 void FakeSafeBrowsingDatabaseManager::OnSafeBrowsingResult( 104 scoped_ptr<SafeBrowsingCheck> result, 105 Client* client) { 106 client->OnSafeBrowsingResult(*result); 107 } 108 109 } // namespace extensions 110