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.
5 #include "chrome/browser/extensions/test_blacklist.h"
10 #include "base/run_loop.h"
11 #include "base/stl_util.h"
12 #include "base/thread_task_runner_handle.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"
17 namespace extensions
{
21 void Assign(BlacklistState
*out
, BlacklistState in
) {
27 BlacklistStateFetcherMock::BlacklistStateFetcherMock() : request_count_(0) {}
29 BlacklistStateFetcherMock::~BlacklistStateFetcherMock() {}
31 void BlacklistStateFetcherMock::Request(const std::string
& id
,
32 const RequestCallback
& callback
) {
35 BlacklistState result
= NOT_BLACKLISTED
;
36 if (ContainsKey(states_
, id
))
39 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
40 base::Bind(callback
, result
));
43 void BlacklistStateFetcherMock::SetState(const std::string
& id
,
44 BlacklistState state
) {
48 void BlacklistStateFetcherMock::Clear() {
53 TestBlacklist::TestBlacklist()
55 blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)),
56 scoped_blacklist_db_(blacklist_db_
) {
59 TestBlacklist::TestBlacklist(Blacklist
* blacklist
)
61 blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)),
62 scoped_blacklist_db_(blacklist_db_
) {
66 TestBlacklist::~TestBlacklist() {
70 void TestBlacklist::Attach(Blacklist
* blacklist
) {
74 blacklist_
= blacklist
;
75 blacklist_
->SetBlacklistStateFetcherForTest(&state_fetcher_mock_
);
78 void TestBlacklist::Detach() {
79 blacklist_
->ResetBlacklistStateFetcherForTest();
82 void TestBlacklist::SetBlacklistState(const std::string
& extension_id
,
85 state_fetcher_mock_
.SetState(extension_id
, state
);
89 blacklist_db_
->RemoveUnsafe(extension_id
);
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
);
104 blacklist_db_
->NotifyUpdate();
107 void TestBlacklist::Clear(bool notify
) {
108 state_fetcher_mock_
.Clear();
109 blacklist_db_
->ClearUnsafe();
111 blacklist_db_
->NotifyUpdate();
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
;
123 void TestBlacklist::DisableSafeBrowsing() {
124 blacklist_db_
->Disable();
127 void TestBlacklist::EnableSafeBrowsing() {
128 blacklist_db_
->Enable();
131 void TestBlacklist::NotifyUpdate() {
132 blacklist_db_
->NotifyUpdate();
135 } // namespace extensions