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_
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/extensions/blacklist_state.h"
19 #include "chrome/browser/safe_browsing/database_manager.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
23 namespace extensions
{
25 class BlacklistStateFetcher
;
29 // The blacklist of extensions backed by safe browsing.
30 class Blacklist
: public content::NotificationObserver
,
31 public base::SupportsWeakPtr
<Blacklist
> {
35 // Observes |blacklist| on construction and unobserves on destruction.
36 explicit Observer(Blacklist
* blacklist
);
38 virtual void OnBlacklistUpdated() = 0;
44 Blacklist
* blacklist_
;
47 class ScopedDatabaseManagerForTest
{
49 explicit ScopedDatabaseManagerForTest(
50 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager
);
52 ~ScopedDatabaseManagerForTest();
55 scoped_refptr
<SafeBrowsingDatabaseManager
> original_
;
57 DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest
);
60 typedef std::map
<std::string
, BlacklistState
> BlacklistStateMap
;
62 typedef base::Callback
<void(const BlacklistStateMap
&)>
63 GetBlacklistedIDsCallback
;
65 typedef base::Callback
<void(const std::set
<std::string
>&)>
66 GetMalwareIDsCallback
;
68 typedef base::Callback
<void(BlacklistState
)> IsBlacklistedCallback
;
70 explicit Blacklist(ExtensionPrefs
* prefs
);
74 // From the set of extension IDs passed in via |ids|, asynchronously checks
75 // 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.
80 // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS
81 // see ExtensionPrefs::IsExtensionBlacklisted.
82 void GetBlacklistedIDs(const std::set
<std::string
>& ids
,
83 const GetBlacklistedIDsCallback
& callback
);
85 // 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
89 void GetMalwareIDs(const std::set
<std::string
>& ids
,
90 const GetMalwareIDsCallback
& callback
);
92 // More convenient form of GetBlacklistedIDs for checking a single extension.
93 void IsBlacklisted(const std::string
& extension_id
,
94 const IsBlacklistedCallback
& callback
);
96 // Used to mock BlacklistStateFetcher in unit tests. Blacklist owns the
98 void SetBlacklistStateFetcherForTest(BlacklistStateFetcher
* fetcher
);
100 // Reset the owned BlacklistStateFetcher to null and return the current
101 // BlacklistStateFetcher.
102 BlacklistStateFetcher
* ResetBlacklistStateFetcherForTest();
104 // Adds/removes an observer to the blacklist.
105 void AddObserver(Observer
* observer
);
106 void RemoveObserver(Observer
* observer
);
109 // Use via ScopedDatabaseManagerForTest.
110 static void SetDatabaseManager(
111 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager
);
112 static scoped_refptr
<SafeBrowsingDatabaseManager
> GetDatabaseManager();
114 // content::NotificationObserver
115 virtual void Observe(int type
,
116 const content::NotificationSource
& source
,
117 const content::NotificationDetails
& details
) OVERRIDE
;
119 void GetBlacklistStateForIDs(const GetBlacklistedIDsCallback
& callback
,
120 const std::set
<std::string
>& blacklisted_ids
);
122 void RequestExtensionsBlacklistState(const std::set
<std::string
>& ids
,
123 const base::Callback
<void()>& callback
);
125 void OnBlacklistStateReceived(const std::string
& id
, BlacklistState state
);
127 void ReturnBlacklistStateMap(const GetBlacklistedIDsCallback
& callback
,
128 const std::set
<std::string
>& blacklisted_ids
);
130 ObserverList
<Observer
> observers_
;
132 content::NotificationRegistrar registrar_
;
134 // The cached BlacklistState's, received from BlacklistStateFetcher.
135 BlacklistStateMap blacklist_state_cache_
;
137 scoped_ptr
<BlacklistStateFetcher
> state_fetcher_
;
139 typedef std::list
<std::pair
<std::vector
<std::string
>,
140 base::Callback
<void()> > >
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_
;
149 DISALLOW_COPY_AND_ASSIGN(Blacklist
);
152 } // namespace extensions
154 #endif // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_