Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / blacklist.h
blobc39214e7f394a77aeffa45c0dca1adad71b56ca8
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
6 #define CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
14 #include "base/callback.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "chrome/browser/safe_browsing/database_manager.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "extensions/browser/blacklist_state.h"
24 namespace content {
25 class BrowserContext;
28 namespace extensions {
30 class BlacklistStateFetcher;
31 class Extension;
32 class ExtensionPrefs;
34 // The blacklist of extensions backed by safe browsing.
35 class Blacklist : public KeyedService,
36 public content::NotificationObserver,
37 public base::SupportsWeakPtr<Blacklist> {
38 public:
39 class Observer {
40 public:
41 // Observes |blacklist| on construction and unobserves on destruction.
42 explicit Observer(Blacklist* blacklist);
44 virtual void OnBlacklistUpdated() = 0;
46 protected:
47 virtual ~Observer();
49 private:
50 Blacklist* blacklist_;
53 class ScopedDatabaseManagerForTest {
54 public:
55 explicit ScopedDatabaseManagerForTest(
56 scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
58 ~ScopedDatabaseManagerForTest();
60 private:
61 scoped_refptr<SafeBrowsingDatabaseManager> original_;
63 DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest);
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);
78 ~Blacklist() override;
80 static Blacklist* Get(content::BrowserContext* context);
82 // From the set of extension IDs passed in via |ids|, asynchronously checks
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.
88 // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS
89 // see ExtensionPrefs::IsExtensionBlacklisted.
90 void GetBlacklistedIDs(const std::set<std::string>& ids,
91 const GetBlacklistedIDsCallback& callback);
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);
100 // More convenient form of GetBlacklistedIDs for checking a single extension.
101 void IsBlacklisted(const std::string& extension_id,
102 const IsBlacklistedCallback& callback);
104 // Used to mock BlacklistStateFetcher in unit tests. Blacklist owns the
105 // |fetcher|.
106 void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher);
108 // Reset the owned BlacklistStateFetcher to null and return the current
109 // BlacklistStateFetcher.
110 BlacklistStateFetcher* ResetBlacklistStateFetcherForTest();
112 // Adds/removes an observer to the blacklist.
113 void AddObserver(Observer* observer);
114 void RemoveObserver(Observer* observer);
116 private:
117 // Use via ScopedDatabaseManagerForTest.
118 static void SetDatabaseManager(
119 scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
120 static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager();
122 // content::NotificationObserver
123 void Observe(int type,
124 const content::NotificationSource& source,
125 const content::NotificationDetails& details) override;
127 void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback,
128 const std::set<std::string>& blacklisted_ids);
130 void RequestExtensionsBlacklistState(const std::set<std::string>& ids,
131 const base::Callback<void()>& callback);
133 void OnBlacklistStateReceived(const std::string& id, BlacklistState state);
135 void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback,
136 const std::set<std::string>& blacklisted_ids);
138 base::ObserverList<Observer> observers_;
140 content::NotificationRegistrar registrar_;
142 // The cached BlacklistState's, received from BlacklistStateFetcher.
143 BlacklistStateMap blacklist_state_cache_;
145 scoped_ptr<BlacklistStateFetcher> state_fetcher_;
147 typedef std::list<std::pair<std::vector<std::string>,
148 base::Callback<void()> > >
149 StateRequestsList;
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_;
157 DISALLOW_COPY_AND_ASSIGN(Blacklist);
160 } // namespace extensions
162 #endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_