[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 2 | // 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] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 8 | #include <list> |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 9 | #include <map> |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 11 | #include <set> |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 15 | #include "base/callback.h" |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 16 | #include "base/callback_list.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 17 | #include "base/macros.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 19 | #include "base/observer_list.h" |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame] | 20 | #include "components/keyed_service/core/keyed_service.h" |
Tim Volodine | e4593847 | 2017-09-21 10:08:22 | [diff] [blame] | 21 | #include "components/safe_browsing/db/database_manager.h" |
[email protected] | 2d19eb6e | 2014-01-27 17:30:00 | [diff] [blame] | 22 | #include "extensions/browser/blacklist_state.h" |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 23 | |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame] | 24 | namespace content { |
| 25 | class BrowserContext; |
| 26 | } |
| 27 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 28 | namespace extensions { |
| 29 | |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 30 | class BlacklistStateFetcher; |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 31 | class ExtensionPrefs; |
| 32 | |
[email protected] | 3f2a2fa | 2013-09-24 02:55:25 | [diff] [blame] | 33 | // The blacklist of extensions backed by safe browsing. |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame] | 34 | class Blacklist : public KeyedService, |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 35 | public base::SupportsWeakPtr<Blacklist> { |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 36 | 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] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 51 | class ScopedDatabaseManagerForTest { |
| 52 | public: |
| 53 | explicit ScopedDatabaseManagerForTest( |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 54 | scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> |
| 55 | database_manager); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 56 | |
| 57 | ~ScopedDatabaseManagerForTest(); |
| 58 | |
| 59 | private: |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 60 | scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> original_; |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest); |
| 63 | }; |
| 64 | |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 65 | using BlacklistStateMap = std::map<std::string, BlacklistState>; |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 66 | |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 67 | using GetBlacklistedIDsCallback = |
| 68 | base::Callback<void(const BlacklistStateMap&)>; |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 69 | |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 70 | using GetMalwareIDsCallback = |
| 71 | base::Callback<void(const std::set<std::string>&)>; |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 72 | |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 73 | using IsBlacklistedCallback = base::Callback<void(BlacklistState)>; |
[email protected] | bc151cf9 | 2013-02-12 04:57:26 | [diff] [blame] | 74 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 75 | explicit Blacklist(ExtensionPrefs* prefs); |
| 76 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 77 | ~Blacklist() override; |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 78 | |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame] | 79 | static Blacklist* Get(content::BrowserContext* context); |
| 80 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 81 | // From the set of extension IDs passed in via |ids|, asynchronously checks |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 82 | // 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] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 86 | // |
| 87 | // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS |
| 88 | // see ExtensionPrefs::IsExtensionBlacklisted. |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 89 | void GetBlacklistedIDs(const std::set<std::string>& ids, |
| 90 | const GetBlacklistedIDsCallback& callback); |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 91 | |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 92 | // 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] | bc151cf9 | 2013-02-12 04:57:26 | [diff] [blame] | 99 | // 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] | f71b582c | 2014-01-10 17:03:15 | [diff] [blame] | 103 | // Used to mock BlacklistStateFetcher in unit tests. Blacklist owns the |
| 104 | // |fetcher|. |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 105 | void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher); |
| 106 | |
[email protected] | f71b582c | 2014-01-10 17:03:15 | [diff] [blame] | 107 | // Reset the owned BlacklistStateFetcher to null and return the current |
| 108 | // BlacklistStateFetcher. |
| 109 | BlacklistStateFetcher* ResetBlacklistStateFetcherForTest(); |
| 110 | |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 111 | // Reset the listening for an updated database. |
| 112 | void ResetDatabaseUpdatedListenerForTest(); |
| 113 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 114 | // Adds/removes an observer to the blacklist. |
| 115 | void AddObserver(Observer* observer); |
| 116 | void RemoveObserver(Observer* observer); |
| 117 | |
| 118 | private: |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 119 | // Use via ScopedDatabaseManagerForTest. |
| 120 | static void SetDatabaseManager( |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 121 | scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> |
| 122 | database_manager); |
| 123 | static scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> |
| 124 | GetDatabaseManager(); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 125 | |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 126 | void ObserveNewDatabase(); |
| 127 | |
| 128 | void NotifyObservers(); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 129 | |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 130 | void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback, |
| 131 | const std::set<std::string>& blacklisted_ids); |
| 132 | |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 133 | void RequestExtensionsBlacklistState(const std::set<std::string>& ids, |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 134 | base::OnceClosure callback); |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 135 | |
| 136 | void OnBlacklistStateReceived(const std::string& id, BlacklistState state); |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 137 | |
| 138 | void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback, |
| 139 | const std::set<std::string>& blacklisted_ids); |
| 140 | |
brettw | d195c95 | 2015-06-02 17:31:12 | [diff] [blame] | 141 | base::ObserverList<Observer> observers_; |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 142 | |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 143 | 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] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 147 | |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 148 | // The cached BlacklistState's, received from BlacklistStateFetcher. |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 149 | BlacklistStateMap blacklist_state_cache_; |
| 150 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 151 | std::unique_ptr<BlacklistStateFetcher> state_fetcher_; |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 152 | |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 153 | // 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 Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 157 | // |
| 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] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 162 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 163 | DISALLOW_COPY_AND_ASSIGN(Blacklist); |
| 164 | }; |
| 165 | |
| 166 | } // namespace extensions |
| 167 | |
| 168 | #endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_ |