[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> |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 10 | #include <set> |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 14 | #include "base/callback.h" |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 17 | #include "base/observer_list.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 18 | #include "chrome/browser/safe_browsing/database_manager.h" |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame^] | 19 | #include "components/keyed_service/core/keyed_service.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 20 | #include "content/public/browser/notification_observer.h" |
| 21 | #include "content/public/browser/notification_registrar.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 Extension; |
| 32 | class ExtensionPrefs; |
| 33 | |
[email protected] | 3f2a2fa | 2013-09-24 02:55:25 | [diff] [blame] | 34 | // The blacklist of extensions backed by safe browsing. |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame^] | 35 | class Blacklist : public KeyedService, |
| 36 | public content::NotificationObserver, |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 37 | public base::SupportsWeakPtr<Blacklist> { |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 38 | public: |
| 39 | class Observer { |
| 40 | public: |
| 41 | // Observes |blacklist| on construction and unobserves on destruction. |
| 42 | explicit Observer(Blacklist* blacklist); |
| 43 | |
| 44 | virtual void OnBlacklistUpdated() = 0; |
| 45 | |
| 46 | protected: |
| 47 | virtual ~Observer(); |
| 48 | |
| 49 | private: |
| 50 | Blacklist* blacklist_; |
| 51 | }; |
| 52 | |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 53 | class ScopedDatabaseManagerForTest { |
| 54 | public: |
| 55 | explicit ScopedDatabaseManagerForTest( |
| 56 | scoped_refptr<SafeBrowsingDatabaseManager> database_manager); |
| 57 | |
| 58 | ~ScopedDatabaseManagerForTest(); |
| 59 | |
| 60 | private: |
| 61 | scoped_refptr<SafeBrowsingDatabaseManager> original_; |
| 62 | |
| 63 | DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest); |
| 64 | }; |
| 65 | |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 66 | typedef std::map<std::string, BlacklistState> BlacklistStateMap; |
| 67 | |
| 68 | typedef base::Callback<void(const BlacklistStateMap&)> |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 69 | GetBlacklistedIDsCallback; |
| 70 | |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 71 | typedef base::Callback<void(const std::set<std::string>&)> |
| 72 | GetMalwareIDsCallback; |
| 73 | |
[email protected] | 9f3c853 | 2013-07-31 19:52:07 | [diff] [blame] | 74 | typedef base::Callback<void(BlacklistState)> IsBlacklistedCallback; |
[email protected] | bc151cf9 | 2013-02-12 04:57:26 | [diff] [blame] | 75 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 76 | explicit Blacklist(ExtensionPrefs* prefs); |
| 77 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 78 | ~Blacklist() override; |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 79 | |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame^] | 80 | static Blacklist* Get(content::BrowserContext* context); |
| 81 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 82 | // From the set of extension IDs passed in via |ids|, asynchronously checks |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 83 | // which are blacklisted and includes them in the resulting map passed |
| 84 | // via |callback|, which will be sent on the caller's message loop. The values |
| 85 | // of the map are the blacklist state for each extension. Extensions with |
| 86 | // a BlacklistState of NOT_BLACKLISTED are not included in the result. |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 87 | // |
| 88 | // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS |
| 89 | // see ExtensionPrefs::IsExtensionBlacklisted. |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 90 | void GetBlacklistedIDs(const std::set<std::string>& ids, |
| 91 | const GetBlacklistedIDsCallback& callback); |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 92 | |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 93 | // From the subset of extension IDs passed in via |ids|, select the ones |
| 94 | // marked in the blacklist as BLACKLISTED_MALWARE and asynchronously pass |
| 95 | // to |callback|. Basically, will call GetBlacklistedIDs and filter its |
| 96 | // results. |
| 97 | void GetMalwareIDs(const std::set<std::string>& ids, |
| 98 | const GetMalwareIDsCallback& callback); |
| 99 | |
[email protected] | bc151cf9 | 2013-02-12 04:57:26 | [diff] [blame] | 100 | // More convenient form of GetBlacklistedIDs for checking a single extension. |
| 101 | void IsBlacklisted(const std::string& extension_id, |
| 102 | const IsBlacklistedCallback& callback); |
| 103 | |
[email protected] | f71b582c | 2014-01-10 17:03:15 | [diff] [blame] | 104 | // Used to mock BlacklistStateFetcher in unit tests. Blacklist owns the |
| 105 | // |fetcher|. |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 106 | void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher); |
| 107 | |
[email protected] | f71b582c | 2014-01-10 17:03:15 | [diff] [blame] | 108 | // Reset the owned BlacklistStateFetcher to null and return the current |
| 109 | // BlacklistStateFetcher. |
| 110 | BlacklistStateFetcher* ResetBlacklistStateFetcherForTest(); |
| 111 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 112 | // Adds/removes an observer to the blacklist. |
| 113 | void AddObserver(Observer* observer); |
| 114 | void RemoveObserver(Observer* observer); |
| 115 | |
| 116 | private: |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 117 | // Use via ScopedDatabaseManagerForTest. |
| 118 | static void SetDatabaseManager( |
| 119 | scoped_refptr<SafeBrowsingDatabaseManager> database_manager); |
| 120 | static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager(); |
| 121 | |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 122 | // content::NotificationObserver |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 123 | void Observe(int type, |
| 124 | const content::NotificationSource& source, |
| 125 | const content::NotificationDetails& details) override; |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 126 | |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 127 | void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback, |
| 128 | const std::set<std::string>& blacklisted_ids); |
| 129 | |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 130 | void RequestExtensionsBlacklistState(const std::set<std::string>& ids, |
| 131 | const base::Callback<void()>& callback); |
| 132 | |
| 133 | void OnBlacklistStateReceived(const std::string& id, BlacklistState state); |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 134 | |
| 135 | void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback, |
| 136 | const std::set<std::string>& blacklisted_ids); |
| 137 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 138 | ObserverList<Observer> observers_; |
| 139 | |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 140 | content::NotificationRegistrar registrar_; |
| 141 | |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 142 | // The cached BlacklistState's, received from BlacklistStateFetcher. |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 143 | BlacklistStateMap blacklist_state_cache_; |
| 144 | |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 145 | scoped_ptr<BlacklistStateFetcher> state_fetcher_; |
| 146 | |
| 147 | typedef std::list<std::pair<std::vector<std::string>, |
| 148 | base::Callback<void()> > > |
| 149 | StateRequestsList; |
| 150 | |
| 151 | // The list of ongoing requests for blacklist states that couldn't be |
| 152 | // served directly from the cache. A new request is created in |
| 153 | // GetBlacklistedIDs and deleted when the callback is called from |
| 154 | // OnBlacklistStateReceived. |
| 155 | StateRequestsList state_requests_; |
| 156 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 157 | DISALLOW_COPY_AND_ASSIGN(Blacklist); |
| 158 | }; |
| 159 | |
| 160 | } // namespace extensions |
| 161 | |
| 162 | #endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_ |