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