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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/test_blacklist.h" 6 7 #include <set> 8 9 #include "base/bind.h" 10 #include "base/message_loop/message_loop.h" 11 #include "base/run_loop.h" 12 #include "base/stl_util.h" 13 #include "chrome/browser/extensions/blacklist.h" 14 #include "chrome/browser/extensions/blacklist_state_fetcher.h" 15 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" 16 17 namespace extensions { 18 19 namespace { 20 21 void Assign(BlacklistState *out, BlacklistState in) { 22 *out = in; 23 } 24 25 } // namespace 26 27 BlacklistStateFetcherMock::BlacklistStateFetcherMock() : request_count_(0) {} 28 29 BlacklistStateFetcherMock::~BlacklistStateFetcherMock() {} 30 31 void BlacklistStateFetcherMock::Request(const std::string& id, 32 const RequestCallback& callback) { 33 ++request_count_; 34 35 BlacklistState result = NOT_BLACKLISTED; 36 if (ContainsKey(states_, id)) 37 result = states_[id]; 38 39 base::MessageLoopProxy::current()->PostTask(FROM_HERE, 40 base::Bind(callback, result)); 41 } 42 43 void BlacklistStateFetcherMock::SetState(const std::string& id, 44 BlacklistState state) { 45 states_[id] = state; 46 } 47 48 void BlacklistStateFetcherMock::Clear() { 49 states_.clear(); 50 } 51 52 53 TestBlacklist::TestBlacklist() 54 : blacklist_(NULL), 55 blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)), 56 scoped_blacklist_db_(blacklist_db_) { 57 } 58 59 TestBlacklist::TestBlacklist(Blacklist* blacklist) 60 : blacklist_(NULL), 61 blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)), 62 scoped_blacklist_db_(blacklist_db_) { 63 Attach(blacklist); 64 } 65 66 TestBlacklist::~TestBlacklist() { 67 Detach(); 68 } 69 70 void TestBlacklist::Attach(Blacklist* blacklist) { 71 if (blacklist_) 72 Detach(); 73 74 blacklist_ = blacklist; 75 blacklist_->SetBlacklistStateFetcherForTest(&state_fetcher_mock_); 76 } 77 78 void TestBlacklist::Detach() { 79 blacklist_->ResetBlacklistStateFetcherForTest(); 80 } 81 82 void TestBlacklist::SetBlacklistState(const std::string& extension_id, 83 BlacklistState state, 84 bool notify) { 85 state_fetcher_mock_.SetState(extension_id, state); 86 87 switch (state) { 88 case NOT_BLACKLISTED: 89 blacklist_db_->RemoveUnsafe(extension_id); 90 break; 91 92 case BLACKLISTED_MALWARE: 93 case BLACKLISTED_SECURITY_VULNERABILITY: 94 case BLACKLISTED_CWS_POLICY_VIOLATION: 95 case BLACKLISTED_POTENTIALLY_UNWANTED: 96 blacklist_db_->AddUnsafe(extension_id); 97 break; 98 99 default: 100 break; 101 } 102 103 if (notify) 104 blacklist_db_->NotifyUpdate(); 105 } 106 107 void TestBlacklist::Clear(bool notify) { 108 state_fetcher_mock_.Clear(); 109 blacklist_db_->ClearUnsafe(); 110 if (notify) 111 blacklist_db_->NotifyUpdate(); 112 } 113 114 BlacklistState TestBlacklist::GetBlacklistState( 115 const std::string& extension_id) { 116 BlacklistState blacklist_state; 117 blacklist_->IsBlacklisted(extension_id, 118 base::Bind(&Assign, &blacklist_state)); 119 base::RunLoop().RunUntilIdle(); 120 return blacklist_state; 121 } 122 123 void TestBlacklist::DisableSafeBrowsing() { 124 blacklist_db_->Disable(); 125 } 126 127 void TestBlacklist::EnableSafeBrowsing() { 128 blacklist_db_->Enable(); 129 } 130 131 void TestBlacklist::NotifyUpdate() { 132 blacklist_db_->NotifyUpdate(); 133 } 134 135 } // namespace extensions 136