1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
6 #define CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
13 #include "base/callback.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/safe_browsing/database_manager.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
20 namespace extensions {
25 // The blacklist of extensions backed by safe browsing.
26 class Blacklist : public content::NotificationObserver,
27 public base::SupportsWeakPtr<Blacklist> {
31 // Observes |blacklist| on construction and unobserves on destruction.
32 explicit Observer(Blacklist* blacklist);
34 virtual void OnBlacklistUpdated() = 0;
40 Blacklist* blacklist_;
43 class ScopedDatabaseManagerForTest {
45 explicit ScopedDatabaseManagerForTest(
46 scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
48 ~ScopedDatabaseManagerForTest();
51 scoped_refptr<SafeBrowsingDatabaseManager> original_;
53 DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest);
56 // The numeric values here match the values of the respective enum in proto
57 // received from SafeBrowsing server.
60 BLACKLISTED_MALWARE = 1,
61 BLACKLISTED_SECURITY_VULNERABILITY = 2,
62 BLACKLISTED_CWS_POLICY_VIOLATION = 3,
63 BLACKLISTED_POTENTIALLY_UNWANTED = 4
66 typedef std::map<std::string, BlacklistState> BlacklistStateMap;
68 typedef base::Callback<void(const BlacklistStateMap&)>
69 GetBlacklistedIDsCallback;
71 typedef base::Callback<void(const std::set<std::string>&)>
72 GetMalwareIDsCallback;
74 typedef base::Callback<void(BlacklistState)> IsBlacklistedCallback;
76 explicit Blacklist(ExtensionPrefs* prefs);
80 // From the set of extension IDs passed in via |ids|, asynchronously checks
81 // which are blacklisted and includes them in the resulting map passed
82 // via |callback|, which will be sent on the caller's message loop. The values
83 // of the map are the blacklist state for each extension. Extensions with
84 // a BlacklistState of NOT_BLACKLISTED are not included in the result.
86 // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS
87 // see ExtensionPrefs::IsExtensionBlacklisted.
88 void GetBlacklistedIDs(const std::set<std::string>& ids,
89 const GetBlacklistedIDsCallback& callback);
91 // From the subset of extension IDs passed in via |ids|, select the ones
92 // marked in the blacklist as BLACKLISTED_MALWARE and asynchronously pass
93 // to |callback|. Basically, will call GetBlacklistedIDs and filter its
95 void GetMalwareIDs(const std::set<std::string>& ids,
96 const GetMalwareIDsCallback& callback);
98 // More convenient form of GetBlacklistedIDs for checking a single extension.
99 void IsBlacklisted(const std::string& extension_id,
100 const IsBlacklistedCallback& callback);
102 // Adds/removes an observer to the blacklist.
103 void AddObserver(Observer* observer);
104 void RemoveObserver(Observer* observer);
107 // Use via ScopedDatabaseManagerForTest.
108 static void SetDatabaseManager(
109 scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
110 static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager();
112 // content::NotificationObserver
113 virtual void Observe(int type,
114 const content::NotificationSource& source,
115 const content::NotificationDetails& details) OVERRIDE;
117 void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback,
118 const std::set<std::string>& blacklisted_ids);
120 void RequestExtensionsBlacklistState(const std::set<std::string> ids,
121 base::Callback<void()> callback);
123 void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback,
124 const std::set<std::string>& blacklisted_ids);
126 ObserverList<Observer> observers_;
128 content::NotificationRegistrar registrar_;
130 BlacklistStateMap blacklist_state_cache_;
132 DISALLOW_COPY_AND_ASSIGN(Blacklist);
135 } // namespace extensions
137 #endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_