blob: 4b9d0bfdea6c44666ace81ca9429278e68b496f2 [file] [log] [blame]
[email protected]8e289f0b2013-12-17 17:49:071// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]fdd679b2012-11-15 20:49:392// 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]8e289f0b2013-12-17 17:49:078#include <list>
[email protected]48a359342013-10-30 00:22:009#include <map>
[email protected]695b5712012-12-06 23:55:2810#include <set>
[email protected]fdd679b2012-11-15 20:49:3911#include <string>
12#include <vector>
13
[email protected]695b5712012-12-06 23:55:2814#include "base/callback.h"
[email protected]8e289f0b2013-12-17 17:49:0715#include "base/memory/scoped_ptr.h"
[email protected]3e72ed752013-02-02 00:47:4716#include "base/memory/weak_ptr.h"
[email protected]fdd679b2012-11-15 20:49:3917#include "base/observer_list.h"
[email protected]3e72ed752013-02-02 00:47:4718#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]2d19eb6e2014-01-27 17:30:0021#include "extensions/browser/blacklist_state.h"
[email protected]fdd679b2012-11-15 20:49:3922
23namespace extensions {
24
[email protected]8e289f0b2013-12-17 17:49:0725class BlacklistStateFetcher;
[email protected]fdd679b2012-11-15 20:49:3926class Extension;
27class ExtensionPrefs;
28
[email protected]3f2a2fa2013-09-24 02:55:2529// The blacklist of extensions backed by safe browsing.
[email protected]3e72ed752013-02-02 00:47:4730class Blacklist : public content::NotificationObserver,
31 public base::SupportsWeakPtr<Blacklist> {
[email protected]fdd679b2012-11-15 20:49:3932 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]3e72ed752013-02-02 00:47:4747 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]48a359342013-10-30 00:22:0060 typedef std::map<std::string, BlacklistState> BlacklistStateMap;
61
62 typedef base::Callback<void(const BlacklistStateMap&)>
[email protected]695b5712012-12-06 23:55:2863 GetBlacklistedIDsCallback;
64
[email protected]48a359342013-10-30 00:22:0065 typedef base::Callback<void(const std::set<std::string>&)>
66 GetMalwareIDsCallback;
67
[email protected]9f3c8532013-07-31 19:52:0768 typedef base::Callback<void(BlacklistState)> IsBlacklistedCallback;
[email protected]bc151cf92013-02-12 04:57:2669
[email protected]fdd679b2012-11-15 20:49:3970 explicit Blacklist(ExtensionPrefs* prefs);
71
dchengae36a4a2014-10-21 12:36:3672 ~Blacklist() override;
[email protected]fdd679b2012-11-15 20:49:3973
[email protected]695b5712012-12-06 23:55:2874 // From the set of extension IDs passed in via |ids|, asynchronously checks
[email protected]48a359342013-10-30 00:22:0075 // 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]3e72ed752013-02-02 00:47:4779 //
80 // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS
81 // see ExtensionPrefs::IsExtensionBlacklisted.
[email protected]695b5712012-12-06 23:55:2882 void GetBlacklistedIDs(const std::set<std::string>& ids,
83 const GetBlacklistedIDsCallback& callback);
[email protected]fdd679b2012-11-15 20:49:3984
[email protected]48a359342013-10-30 00:22:0085 // 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]bc151cf92013-02-12 04:57:2692 // 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]f71b582c2014-01-10 17:03:1596 // Used to mock BlacklistStateFetcher in unit tests. Blacklist owns the
97 // |fetcher|.
[email protected]8e289f0b2013-12-17 17:49:0798 void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher);
99
[email protected]f71b582c2014-01-10 17:03:15100 // Reset the owned BlacklistStateFetcher to null and return the current
101 // BlacklistStateFetcher.
102 BlacklistStateFetcher* ResetBlacklistStateFetcherForTest();
103
[email protected]fdd679b2012-11-15 20:49:39104 // Adds/removes an observer to the blacklist.
105 void AddObserver(Observer* observer);
106 void RemoveObserver(Observer* observer);
107
108 private:
[email protected]3e72ed752013-02-02 00:47:47109 // Use via ScopedDatabaseManagerForTest.
110 static void SetDatabaseManager(
111 scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
112 static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager();
113
[email protected]3e72ed752013-02-02 00:47:47114 // content::NotificationObserver
dchengae36a4a2014-10-21 12:36:36115 void Observe(int type,
116 const content::NotificationSource& source,
117 const content::NotificationDetails& details) override;
[email protected]3e72ed752013-02-02 00:47:47118
[email protected]48a359342013-10-30 00:22:00119 void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback,
120 const std::set<std::string>& blacklisted_ids);
121
[email protected]8e289f0b2013-12-17 17:49:07122 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]48a359342013-10-30 00:22:00126
127 void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback,
128 const std::set<std::string>& blacklisted_ids);
129
[email protected]fdd679b2012-11-15 20:49:39130 ObserverList<Observer> observers_;
131
[email protected]3e72ed752013-02-02 00:47:47132 content::NotificationRegistrar registrar_;
133
[email protected]8e289f0b2013-12-17 17:49:07134 // The cached BlacklistState's, received from BlacklistStateFetcher.
[email protected]48a359342013-10-30 00:22:00135 BlacklistStateMap blacklist_state_cache_;
136
[email protected]8e289f0b2013-12-17 17:49:07137 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]fdd679b2012-11-15 20:49:39149 DISALLOW_COPY_AND_ASSIGN(Blacklist);
150};
151
152} // namespace extensions
153
154#endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_