blob: 15f16f8ebac3f430c619cfb19655dfd0b5f8a8d4 [file] [log] [blame]
[email protected]8e289f0b2013-12-17 17:49:071// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]fdd679b2012-11-15 20:49:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
6#define CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
7
[email protected]8e289f0b2013-12-17 17:49:078#include <list>
[email protected]48a359342013-10-30 00:22:009#include <map>
dchengc963c7142016-04-08 03:55:2210#include <memory>
[email protected]695b5712012-12-06 23:55:2811#include <set>
[email protected]fdd679b2012-11-15 20:49:3912#include <string>
13#include <vector>
14
[email protected]695b5712012-12-06 23:55:2815#include "base/callback.h"
Avi Drissmane222a562018-03-27 03:25:4816#include "base/callback_list.h"
avia2f4804a2015-12-24 23:11:1317#include "base/macros.h"
[email protected]3e72ed752013-02-02 00:47:4718#include "base/memory/weak_ptr.h"
[email protected]fdd679b2012-11-15 20:49:3919#include "base/observer_list.h"
reillyg121e8892014-11-03 22:12:5920#include "components/keyed_service/core/keyed_service.h"
Tim Volodinee45938472017-09-21 10:08:2221#include "components/safe_browsing/db/database_manager.h"
[email protected]2d19eb6e2014-01-27 17:30:0022#include "extensions/browser/blacklist_state.h"
[email protected]fdd679b2012-11-15 20:49:3923
reillyg121e8892014-11-03 22:12:5924namespace content {
25class BrowserContext;
26}
27
[email protected]fdd679b2012-11-15 20:49:3928namespace extensions {
29
[email protected]8e289f0b2013-12-17 17:49:0730class BlacklistStateFetcher;
[email protected]fdd679b2012-11-15 20:49:3931class ExtensionPrefs;
32
[email protected]3f2a2fa2013-09-24 02:55:2533// The blacklist of extensions backed by safe browsing.
reillyg121e8892014-11-03 22:12:5934class Blacklist : public KeyedService,
[email protected]3e72ed752013-02-02 00:47:4735 public base::SupportsWeakPtr<Blacklist> {
[email protected]fdd679b2012-11-15 20:49:3936 public:
37 class Observer {
38 public:
39 // Observes |blacklist| on construction and unobserves on destruction.
40 explicit Observer(Blacklist* blacklist);
41
42 virtual void OnBlacklistUpdated() = 0;
43
44 protected:
45 virtual ~Observer();
46
47 private:
48 Blacklist* blacklist_;
49 };
50
[email protected]3e72ed752013-02-02 00:47:4751 class ScopedDatabaseManagerForTest {
52 public:
53 explicit ScopedDatabaseManagerForTest(
vakh9a474d832015-11-13 01:43:0954 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
55 database_manager);
[email protected]3e72ed752013-02-02 00:47:4756
57 ~ScopedDatabaseManagerForTest();
58
59 private:
vakh9a474d832015-11-13 01:43:0960 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> original_;
[email protected]3e72ed752013-02-02 00:47:4761
62 DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest);
63 };
64
Avi Drissman6ee0692f2018-03-26 17:08:0165 using BlacklistStateMap = std::map<std::string, BlacklistState>;
[email protected]48a359342013-10-30 00:22:0066
Avi Drissman6ee0692f2018-03-26 17:08:0167 using GetBlacklistedIDsCallback =
68 base::Callback<void(const BlacklistStateMap&)>;
[email protected]695b5712012-12-06 23:55:2869
Avi Drissman6ee0692f2018-03-26 17:08:0170 using GetMalwareIDsCallback =
71 base::Callback<void(const std::set<std::string>&)>;
[email protected]48a359342013-10-30 00:22:0072
Avi Drissman6ee0692f2018-03-26 17:08:0173 using IsBlacklistedCallback = base::Callback<void(BlacklistState)>;
[email protected]bc151cf92013-02-12 04:57:2674
[email protected]fdd679b2012-11-15 20:49:3975 explicit Blacklist(ExtensionPrefs* prefs);
76
dchengae36a4a2014-10-21 12:36:3677 ~Blacklist() override;
[email protected]fdd679b2012-11-15 20:49:3978
reillyg121e8892014-11-03 22:12:5979 static Blacklist* Get(content::BrowserContext* context);
80
[email protected]695b5712012-12-06 23:55:2881 // From the set of extension IDs passed in via |ids|, asynchronously checks
[email protected]48a359342013-10-30 00:22:0082 // which are blacklisted and includes them in the resulting map passed
83 // via |callback|, which will be sent on the caller's message loop. The values
84 // of the map are the blacklist state for each extension. Extensions with
85 // a BlacklistState of NOT_BLACKLISTED are not included in the result.
[email protected]3e72ed752013-02-02 00:47:4786 //
87 // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS
88 // see ExtensionPrefs::IsExtensionBlacklisted.
[email protected]695b5712012-12-06 23:55:2889 void GetBlacklistedIDs(const std::set<std::string>& ids,
90 const GetBlacklistedIDsCallback& callback);
[email protected]fdd679b2012-11-15 20:49:3991
[email protected]48a359342013-10-30 00:22:0092 // From the subset of extension IDs passed in via |ids|, select the ones
93 // marked in the blacklist as BLACKLISTED_MALWARE and asynchronously pass
94 // to |callback|. Basically, will call GetBlacklistedIDs and filter its
95 // results.
96 void GetMalwareIDs(const std::set<std::string>& ids,
97 const GetMalwareIDsCallback& callback);
98
[email protected]bc151cf92013-02-12 04:57:2699 // More convenient form of GetBlacklistedIDs for checking a single extension.
100 void IsBlacklisted(const std::string& extension_id,
101 const IsBlacklistedCallback& callback);
102
[email protected]f71b582c2014-01-10 17:03:15103 // Used to mock BlacklistStateFetcher in unit tests. Blacklist owns the
104 // |fetcher|.
[email protected]8e289f0b2013-12-17 17:49:07105 void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher);
106
[email protected]f71b582c2014-01-10 17:03:15107 // Reset the owned BlacklistStateFetcher to null and return the current
108 // BlacklistStateFetcher.
109 BlacklistStateFetcher* ResetBlacklistStateFetcherForTest();
110
Avi Drissmane222a562018-03-27 03:25:48111 // Reset the listening for an updated database.
112 void ResetDatabaseUpdatedListenerForTest();
113
[email protected]fdd679b2012-11-15 20:49:39114 // Adds/removes an observer to the blacklist.
115 void AddObserver(Observer* observer);
116 void RemoveObserver(Observer* observer);
117
118 private:
[email protected]3e72ed752013-02-02 00:47:47119 // Use via ScopedDatabaseManagerForTest.
120 static void SetDatabaseManager(
vakh9a474d832015-11-13 01:43:09121 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
122 database_manager);
123 static scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
124 GetDatabaseManager();
[email protected]3e72ed752013-02-02 00:47:47125
Avi Drissmane222a562018-03-27 03:25:48126 void ObserveNewDatabase();
127
128 void NotifyObservers();
[email protected]3e72ed752013-02-02 00:47:47129
[email protected]48a359342013-10-30 00:22:00130 void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback,
131 const std::set<std::string>& blacklisted_ids);
132
[email protected]8e289f0b2013-12-17 17:49:07133 void RequestExtensionsBlacklistState(const std::set<std::string>& ids,
Avi Drissman6ee0692f2018-03-26 17:08:01134 base::OnceClosure callback);
[email protected]8e289f0b2013-12-17 17:49:07135
136 void OnBlacklistStateReceived(const std::string& id, BlacklistState state);
[email protected]48a359342013-10-30 00:22:00137
138 void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback,
139 const std::set<std::string>& blacklisted_ids);
140
brettwd195c952015-06-02 17:31:12141 base::ObserverList<Observer> observers_;
[email protected]fdd679b2012-11-15 20:49:39142
Avi Drissmane222a562018-03-27 03:25:48143 std::unique_ptr<base::CallbackList<void()>::Subscription>
144 database_updated_subscription_;
145 std::unique_ptr<base::CallbackList<void()>::Subscription>
146 database_changed_subscription_;
[email protected]3e72ed752013-02-02 00:47:47147
[email protected]8e289f0b2013-12-17 17:49:07148 // The cached BlacklistState's, received from BlacklistStateFetcher.
[email protected]48a359342013-10-30 00:22:00149 BlacklistStateMap blacklist_state_cache_;
150
dchengc963c7142016-04-08 03:55:22151 std::unique_ptr<BlacklistStateFetcher> state_fetcher_;
[email protected]8e289f0b2013-12-17 17:49:07152
[email protected]8e289f0b2013-12-17 17:49:07153 // The list of ongoing requests for blacklist states that couldn't be
154 // served directly from the cache. A new request is created in
155 // GetBlacklistedIDs and deleted when the callback is called from
156 // OnBlacklistStateReceived.
Avi Drissman6ee0692f2018-03-26 17:08:01157 //
158 // This is a list of requests. Each item in the list is a request. A request
159 // is a pair of [vector of string ids to check, response closure].
160 std::list<std::pair<std::vector<std::string>, base::OnceClosure>>
161 state_requests_;
[email protected]8e289f0b2013-12-17 17:49:07162
[email protected]fdd679b2012-11-15 20:49:39163 DISALLOW_COPY_AND_ASSIGN(Blacklist);
164};
165
166} // namespace extensions
167
168#endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_