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& FakeSafeBrowsingDatabaseManager::Disable() { 35 enabled_ = false; 36 return *this; 37 } 38 39 FakeSafeBrowsingDatabaseManager& 40 FakeSafeBrowsingDatabaseManager::ClearUnsafe() { 41 unsafe_ids_.clear(); 42 return *this; 43 } 44 45 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe( 46 const std::string& a) { 47 ClearUnsafe(); 48 unsafe_ids_.insert(a); 49 return *this; 50 } 51 52 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe( 53 const std::string& a, const std::string& b) { 54 SetUnsafe(a); 55 unsafe_ids_.insert(b); 56 return *this; 57 } 58 59 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe( 60 const std::string& a, const std::string& b, const std::string& c) { 61 SetUnsafe(a, b); 62 unsafe_ids_.insert(c); 63 return *this; 64 } 65 66 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::SetUnsafe( 67 const std::string& a, const std::string& b, const std::string& c, 68 const std::string& d) { 69 SetUnsafe(a, b, c); 70 unsafe_ids_.insert(d); 71 return *this; 72 } 73 74 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::AddUnsafe( 75 const std::string& a) { 76 unsafe_ids_.insert(a); 77 return *this; 78 } 79 80 FakeSafeBrowsingDatabaseManager& FakeSafeBrowsingDatabaseManager::RemoveUnsafe( 81 const std::string& a) { 82 unsafe_ids_.erase(a); 83 return *this; 84 } 85 86 void FakeSafeBrowsingDatabaseManager::NotifyUpdate() { 87 SafeBrowsingDatabaseManager::NotifyDatabaseUpdateFinished(true); 88 } 89 90 bool FakeSafeBrowsingDatabaseManager::CheckExtensionIDs( 91 const std::set<std::string>& extension_ids, 92 Client* client) { 93 if (!enabled_) 94 return true; 95 96 // Need to construct the full SafeBrowsingCheck rather than calling 97 // OnCheckExtensionsResult directly because it's protected. Grr! 98 std::vector<std::string> extension_ids_vector(extension_ids.begin(), 99 extension_ids.end()); 100 std::vector<SBFullHash> extension_id_hashes; 101 std::transform(extension_ids_vector.begin(), extension_ids_vector.end(), 102 std::back_inserter(extension_id_hashes), 103 safe_browsing_util::StringToSBFullHash); 104 105 scoped_ptr<SafeBrowsingCheck> safe_browsing_check( 106 new SafeBrowsingCheck( 107 std::vector<GURL>(), 108 extension_id_hashes, 109 client, 110 safe_browsing_util::EXTENSIONBLACKLIST, 111 std::vector<SBThreatType>(1, SB_THREAT_TYPE_EXTENSION))); 112 113 for (size_t i = 0; i < extension_ids_vector.size(); ++i) { 114 const std::string& extension_id = extension_ids_vector[i]; 115 if (unsafe_ids_.count(extension_id)) 116 safe_browsing_check->full_hash_results[i] = SB_THREAT_TYPE_EXTENSION; 117 } 118 119 base::MessageLoopProxy::current()->PostTask( 120 FROM_HERE, 121 base::Bind(&FakeSafeBrowsingDatabaseManager::OnSafeBrowsingResult, 122 this, 123 base::Passed(&safe_browsing_check), 124 client)); 125 return false; 126 } 127 128 void FakeSafeBrowsingDatabaseManager::OnSafeBrowsingResult( 129 scoped_ptr<SafeBrowsingCheck> result, 130 Client* client) { 131 client->OnSafeBrowsingResult(*result); 132 } 133 134 } // namespace extensions 135