Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / blacklist.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
6 #define CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
7
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
13
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"
23
24 namespace content {
25 class BrowserContext;
26 }
27
28 namespace extensions {
29
30 class BlacklistStateFetcher;
31 class Extension;
32 class ExtensionPrefs;
33
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);
43
44     virtual void OnBlacklistUpdated() = 0;
45
46    protected:
47     virtual ~Observer();
48
49    private:
50     Blacklist* blacklist_;
51   };
52
53   class ScopedDatabaseManagerForTest {
54    public:
55     explicit ScopedDatabaseManagerForTest(
56         scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
57
58     ~ScopedDatabaseManagerForTest();
59
60    private:
61     scoped_refptr<SafeBrowsingDatabaseManager> original_;
62
63     DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest);
64   };
65
66   typedef std::map<std::string, BlacklistState> BlacklistStateMap;
67
68   typedef base::Callback<void(const BlacklistStateMap&)>
69       GetBlacklistedIDsCallback;
70
71   typedef base::Callback<void(const std::set<std::string>&)>
72       GetMalwareIDsCallback;
73
74   typedef base::Callback<void(BlacklistState)> IsBlacklistedCallback;
75
76   explicit Blacklist(ExtensionPrefs* prefs);
77
78   ~Blacklist() override;
79
80   static Blacklist* Get(content::BrowserContext* context);
81
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.
87   //
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);
92
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);
99
100   // More convenient form of GetBlacklistedIDs for checking a single extension.
101   void IsBlacklisted(const std::string& extension_id,
102                      const IsBlacklistedCallback& callback);
103
104   // Used to mock BlacklistStateFetcher in unit tests. Blacklist owns the
105   // |fetcher|.
106   void SetBlacklistStateFetcherForTest(BlacklistStateFetcher* fetcher);
107
108   // Reset the owned BlacklistStateFetcher to null and return the current
109   // BlacklistStateFetcher.
110   BlacklistStateFetcher* ResetBlacklistStateFetcherForTest();
111
112   // Adds/removes an observer to the blacklist.
113   void AddObserver(Observer* observer);
114   void RemoveObserver(Observer* observer);
115
116  private:
117   // Use via ScopedDatabaseManagerForTest.
118   static void SetDatabaseManager(
119       scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
120   static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager();
121
122   // content::NotificationObserver
123   void Observe(int type,
124                const content::NotificationSource& source,
125                const content::NotificationDetails& details) override;
126
127   void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback,
128                                const std::set<std::string>& blacklisted_ids);
129
130   void RequestExtensionsBlacklistState(const std::set<std::string>& ids,
131                                        const base::Callback<void()>& callback);
132
133   void OnBlacklistStateReceived(const std::string& id, BlacklistState state);
134
135   void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback,
136                                const std::set<std::string>& blacklisted_ids);
137
138   ObserverList<Observer> observers_;
139
140   content::NotificationRegistrar registrar_;
141
142   // The cached BlacklistState's, received from BlacklistStateFetcher.
143   BlacklistStateMap blacklist_state_cache_;
144
145   scoped_ptr<BlacklistStateFetcher> state_fetcher_;
146
147   typedef std::list<std::pair<std::vector<std::string>,
148                               base::Callback<void()> > >
149       StateRequestsList;
150
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_;
156
157   DISALLOW_COPY_AND_ASSIGN(Blacklist);
158 };
159
160 }  // namespace extensions
161
162 #endif  // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_