Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / test_blacklist.cc
blob254dc7b3ee03b47bf3984b842c39c99ff98d9714
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"
7 #include <set>
9 #include "base/bind.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 {
19 namespace {
21 void Assign(BlacklistState *out, BlacklistState in) {
22 *out = in;
25 } // namespace
27 BlacklistStateFetcherMock::BlacklistStateFetcherMock() : request_count_(0) {}
29 BlacklistStateFetcherMock::~BlacklistStateFetcherMock() {}
31 void BlacklistStateFetcherMock::Request(const std::string& id,
32 const RequestCallback& callback) {
33 ++request_count_;
35 BlacklistState result = NOT_BLACKLISTED;
36 if (ContainsKey(states_, id))
37 result = 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) {
45 states_[id] = state;
48 void BlacklistStateFetcherMock::Clear() {
49 states_.clear();
53 TestBlacklist::TestBlacklist()
54 : blacklist_(NULL),
55 blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)),
56 scoped_blacklist_db_(blacklist_db_) {
59 TestBlacklist::TestBlacklist(Blacklist* blacklist)
60 : blacklist_(NULL),
61 blacklist_db_(new FakeSafeBrowsingDatabaseManager(true)),
62 scoped_blacklist_db_(blacklist_db_) {
63 Attach(blacklist);
66 TestBlacklist::~TestBlacklist() {
67 Detach();
70 void TestBlacklist::Attach(Blacklist* blacklist) {
71 if (blacklist_)
72 Detach();
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,
83 BlacklistState state,
84 bool notify) {
85 state_fetcher_mock_.SetState(extension_id, state);
87 switch (state) {
88 case NOT_BLACKLISTED:
89 blacklist_db_->RemoveUnsafe(extension_id);
90 break;
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;
99 default:
100 break;
103 if (notify)
104 blacklist_db_->NotifyUpdate();
107 void TestBlacklist::Clear(bool notify) {
108 state_fetcher_mock_.Clear();
109 blacklist_db_->ClearUnsafe();
110 if (notify)
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