| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_ |
| 7 | 7 |
| 8 #include <list> |
| 8 #include <map> | 9 #include <map> |
| 9 #include <set> | 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 15 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "chrome/browser/extensions/blacklist_state.h" |
| 16 #include "chrome/browser/safe_browsing/database_manager.h" | 19 #include "chrome/browser/safe_browsing/database_manager.h" |
| 17 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
| 19 | 22 |
| 20 namespace extensions { | 23 namespace extensions { |
| 21 | 24 |
| 25 class BlacklistStateFetcher; |
| 22 class Extension; | 26 class Extension; |
| 23 class ExtensionPrefs; | 27 class ExtensionPrefs; |
| 24 | 28 |
| 25 // The blacklist of extensions backed by safe browsing. | 29 // The blacklist of extensions backed by safe browsing. |
| 26 class Blacklist : public content::NotificationObserver, | 30 class Blacklist : public content::NotificationObserver, |
| 27 public base::SupportsWeakPtr<Blacklist> { | 31 public base::SupportsWeakPtr<Blacklist> { |
| 28 public: | 32 public: |
| 29 class Observer { | 33 class Observer { |
| 30 public: | 34 public: |
| 31 // Observes |blacklist| on construction and unobserves on destruction. | 35 // Observes |blacklist| on construction and unobserves on destruction. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 46 scoped_refptr<SafeBrowsingDatabaseManager> database_manager); | 50 scoped_refptr<SafeBrowsingDatabaseManager> database_manager); |
| 47 | 51 |
| 48 ~ScopedDatabaseManagerForTest(); | 52 ~ScopedDatabaseManagerForTest(); |
| 49 | 53 |
| 50 private: | 54 private: |
| 51 scoped_refptr<SafeBrowsingDatabaseManager> original_; | 55 scoped_refptr<SafeBrowsingDatabaseManager> original_; |
| 52 | 56 |
| 53 DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest); | 57 DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest); |
| 54 }; | 58 }; |
| 55 | 59 |
| 56 // The numeric values here match the values of the respective enum in proto | |
| 57 // received from SafeBrowsing server. | |
| 58 enum BlacklistState { | |
| 59 NOT_BLACKLISTED = 0, | |
| 60 BLACKLISTED_MALWARE = 1, | |
| 61 BLACKLISTED_SECURITY_VULNERABILITY = 2, | |
| 62 BLACKLISTED_CWS_POLICY_VIOLATION = 3, | |
| 63 BLACKLISTED_POTENTIALLY_UNWANTED = 4 | |
| 64 }; | |
| 65 | |
| 66 typedef std::map<std::string, BlacklistState> BlacklistStateMap; | 60 typedef std::map<std::string, BlacklistState> BlacklistStateMap; |
| 67 | 61 |
| 68 typedef base::Callback<void(const BlacklistStateMap&)> | 62 typedef base::Callback<void(const BlacklistStateMap&)> |
| 69 GetBlacklistedIDsCallback; | 63 GetBlacklistedIDsCallback; |
| 70 | 64 |
| 71 typedef base::Callback<void(const std::set<std::string>&)> | 65 typedef base::Callback<void(const std::set<std::string>&)> |
| 72 GetMalwareIDsCallback; | 66 GetMalwareIDsCallback; |
| 73 | 67 |
| 74 typedef base::Callback<void(BlacklistState)> IsBlacklistedCallback; | 68 typedef base::Callback<void(BlacklistState)> IsBlacklistedCallback; |
| 75 | 69 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 92 // marked in the blacklist as BLACKLISTED_MALWARE and asynchronously pass | 86 // marked in the blacklist as BLACKLISTED_MALWARE and asynchronously pass |
| 93 // to |callback|. Basically, will call GetBlacklistedIDs and filter its | 87 // to |callback|. Basically, will call GetBlacklistedIDs and filter its |
| 94 // results. | 88 // results. |
| 95 void GetMalwareIDs(const std::set<std::string>& ids, | 89 void GetMalwareIDs(const std::set<std::string>& ids, |
| 96 const GetMalwareIDsCallback& callback); | 90 const GetMalwareIDsCallback& callback); |
| 97 | 91 |
| 98 // More convenient form of GetBlacklistedIDs for checking a single extension. | 92 // More convenient form of GetBlacklistedIDs for checking a single extension. |
| 99 void IsBlacklisted(const std::string& extension_id, | 93 void IsBlacklisted(const std::string& extension_id, |
| 100 const IsBlacklistedCallback& callback); | 94 const IsBlacklistedCallback& callback); |
| 101 | 95 |
| 96 // Used to mock BlacklistStateFetcher in unit tests. |
| 97 void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher); |
| 98 |
| 102 // Adds/removes an observer to the blacklist. | 99 // Adds/removes an observer to the blacklist. |
| 103 void AddObserver(Observer* observer); | 100 void AddObserver(Observer* observer); |
| 104 void RemoveObserver(Observer* observer); | 101 void RemoveObserver(Observer* observer); |
| 105 | 102 |
| 106 private: | 103 private: |
| 107 // Use via ScopedDatabaseManagerForTest. | 104 // Use via ScopedDatabaseManagerForTest. |
| 108 static void SetDatabaseManager( | 105 static void SetDatabaseManager( |
| 109 scoped_refptr<SafeBrowsingDatabaseManager> database_manager); | 106 scoped_refptr<SafeBrowsingDatabaseManager> database_manager); |
| 110 static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager(); | 107 static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager(); |
| 111 | 108 |
| 112 // content::NotificationObserver | 109 // content::NotificationObserver |
| 113 virtual void Observe(int type, | 110 virtual void Observe(int type, |
| 114 const content::NotificationSource& source, | 111 const content::NotificationSource& source, |
| 115 const content::NotificationDetails& details) OVERRIDE; | 112 const content::NotificationDetails& details) OVERRIDE; |
| 116 | 113 |
| 117 void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback, | 114 void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback, |
| 118 const std::set<std::string>& blacklisted_ids); | 115 const std::set<std::string>& blacklisted_ids); |
| 119 | 116 |
| 120 void RequestExtensionsBlacklistState(const std::set<std::string> ids, | 117 void RequestExtensionsBlacklistState(const std::set<std::string>& ids, |
| 121 base::Callback<void()> callback); | 118 const base::Callback<void()>& callback); |
| 119 |
| 120 void OnBlacklistStateReceived(const std::string& id, BlacklistState state); |
| 122 | 121 |
| 123 void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback, | 122 void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback, |
| 124 const std::set<std::string>& blacklisted_ids); | 123 const std::set<std::string>& blacklisted_ids); |
| 125 | 124 |
| 126 ObserverList<Observer> observers_; | 125 ObserverList<Observer> observers_; |
| 127 | 126 |
| 128 content::NotificationRegistrar registrar_; | 127 content::NotificationRegistrar registrar_; |
| 129 | 128 |
| 129 // The cached BlacklistState's, received from BlacklistStateFetcher. |
| 130 BlacklistStateMap blacklist_state_cache_; | 130 BlacklistStateMap blacklist_state_cache_; |
| 131 | 131 |
| 132 scoped_ptr<BlacklistStateFetcher> state_fetcher_; |
| 133 |
| 134 typedef std::list<std::pair<std::vector<std::string>, |
| 135 base::Callback<void()> > > |
| 136 StateRequestsList; |
| 137 |
| 138 // The list of ongoing requests for blacklist states that couldn't be |
| 139 // served directly from the cache. A new request is created in |
| 140 // GetBlacklistedIDs and deleted when the callback is called from |
| 141 // OnBlacklistStateReceived. |
| 142 StateRequestsList state_requests_; |
| 143 |
| 132 DISALLOW_COPY_AND_ASSIGN(Blacklist); | 144 DISALLOW_COPY_AND_ASSIGN(Blacklist); |
| 133 }; | 145 }; |
| 134 | 146 |
| 135 } // namespace extensions | 147 } // namespace extensions |
| 136 | 148 |
| 137 #endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_ | 149 #endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_ |
| OLD | NEW |