- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / blacklist.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
6 #define CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
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"
19
20 namespace extensions {
21
22 class Extension;
23 class ExtensionPrefs;
24
25 // The blacklist of extensions backed by safe browsing.
26 class Blacklist : public content::NotificationObserver,
27                   public base::SupportsWeakPtr<Blacklist> {
28  public:
29   class Observer {
30    public:
31     // Observes |blacklist| on construction and unobserves on destruction.
32     explicit Observer(Blacklist* blacklist);
33
34     virtual void OnBlacklistUpdated() = 0;
35
36    protected:
37     virtual ~Observer();
38
39    private:
40     Blacklist* blacklist_;
41   };
42
43   class ScopedDatabaseManagerForTest {
44    public:
45     explicit ScopedDatabaseManagerForTest(
46         scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
47
48     ~ScopedDatabaseManagerForTest();
49
50    private:
51     scoped_refptr<SafeBrowsingDatabaseManager> original_;
52
53     DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest);
54   };
55
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;
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   virtual ~Blacklist();
79
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.
85   //
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);
90
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
94   // results.
95   void GetMalwareIDs(const std::set<std::string>& ids,
96                      const GetMalwareIDsCallback& callback);
97
98   // More convenient form of GetBlacklistedIDs for checking a single extension.
99   void IsBlacklisted(const std::string& extension_id,
100                      const IsBlacklistedCallback& callback);
101
102   // Adds/removes an observer to the blacklist.
103   void AddObserver(Observer* observer);
104   void RemoveObserver(Observer* observer);
105
106  private:
107   // Use via ScopedDatabaseManagerForTest.
108   static void SetDatabaseManager(
109       scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
110   static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager();
111
112   // content::NotificationObserver
113   virtual void Observe(int type,
114                        const content::NotificationSource& source,
115                        const content::NotificationDetails& details) OVERRIDE;
116
117   void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback& callback,
118                                const std::set<std::string>& blacklisted_ids);
119
120   void RequestExtensionsBlacklistState(const std::set<std::string> ids,
121                                        base::Callback<void()> callback);
122
123   void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback& callback,
124                                const std::set<std::string>& blacklisted_ids);
125
126   ObserverList<Observer> observers_;
127
128   content::NotificationRegistrar registrar_;
129
130   BlacklistStateMap blacklist_state_cache_;
131
132   DISALLOW_COPY_AND_ASSIGN(Blacklist);
133 };
134
135 }  // namespace extensions
136
137 #endif  // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_