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.
5 #include "chrome/browser/extensions/blacklist.h"
10 #include "base/bind.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/stl_util.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/extensions/blacklist_factory.h"
18 #include "chrome/browser/extensions/blacklist_state_fetcher.h"
19 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
20 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_source.h"
23 #include "extensions/browser/extension_prefs.h"
25 using content::BrowserThread
;
27 namespace extensions
{
31 // The safe browsing database manager to use. Make this a global/static variable
32 // rather than a member of Blacklist because Blacklist accesses the real
33 // database manager before it has a chance to get a fake one.
34 class LazySafeBrowsingDatabaseManager
{
36 LazySafeBrowsingDatabaseManager() {
37 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
38 if (g_browser_process
&& g_browser_process
->safe_browsing_service()) {
40 g_browser_process
->safe_browsing_service()->database_manager();
45 scoped_refptr
<SafeBrowsingDatabaseManager
> get() {
49 void set(scoped_refptr
<SafeBrowsingDatabaseManager
> instance
) {
54 scoped_refptr
<SafeBrowsingDatabaseManager
> instance_
;
57 static base::LazyInstance
<LazySafeBrowsingDatabaseManager
> g_database_manager
=
58 LAZY_INSTANCE_INITIALIZER
;
60 // Implementation of SafeBrowsingDatabaseManager::Client, the class which is
61 // called back from safebrowsing queries.
63 // Constructed on any thread but lives on the IO from then on.
64 class SafeBrowsingClientImpl
65 : public SafeBrowsingDatabaseManager::Client
,
66 public base::RefCountedThreadSafe
<SafeBrowsingClientImpl
> {
68 typedef base::Callback
<void(const std::set
<std::string
>&)> OnResultCallback
;
70 // Constructs a client to query the database manager for |extension_ids| and
71 // run |callback| with the IDs of those which have been blacklisted.
72 SafeBrowsingClientImpl(
73 const std::set
<std::string
>& extension_ids
,
74 const OnResultCallback
& callback
)
75 : callback_message_loop_(base::MessageLoopProxy::current()),
77 BrowserThread::PostTask(
80 base::Bind(&SafeBrowsingClientImpl::StartCheck
, this,
81 g_database_manager
.Get().get(),
86 friend class base::RefCountedThreadSafe
<SafeBrowsingClientImpl
>;
87 ~SafeBrowsingClientImpl() override
{}
89 // Pass |database_manager| as a parameter to avoid touching
90 // SafeBrowsingService on the IO thread.
91 void StartCheck(scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager
,
92 const std::set
<std::string
>& extension_ids
) {
93 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
94 if (database_manager
->CheckExtensionIDs(extension_ids
, this)) {
95 // Definitely not blacklisted. Callback immediately.
96 callback_message_loop_
->PostTask(
98 base::Bind(callback_
, std::set
<std::string
>()));
101 // Something might be blacklisted, response will come in
102 // OnCheckExtensionsResult.
103 AddRef(); // Balanced in OnCheckExtensionsResult
106 void OnCheckExtensionsResult(const std::set
<std::string
>& hits
) override
{
107 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
108 callback_message_loop_
->PostTask(FROM_HERE
, base::Bind(callback_
, hits
));
109 Release(); // Balanced in StartCheck.
112 scoped_refptr
<base::MessageLoopProxy
> callback_message_loop_
;
113 OnResultCallback callback_
;
115 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingClientImpl
);
118 void CheckOneExtensionState(
119 const Blacklist::IsBlacklistedCallback
& callback
,
120 const Blacklist::BlacklistStateMap
& state_map
) {
121 callback
.Run(state_map
.empty() ? NOT_BLACKLISTED
: state_map
.begin()->second
);
124 void GetMalwareFromBlacklistStateMap(
125 const Blacklist::GetMalwareIDsCallback
& callback
,
126 const Blacklist::BlacklistStateMap
& state_map
) {
127 std::set
<std::string
> malware
;
128 for (Blacklist::BlacklistStateMap::const_iterator it
= state_map
.begin();
129 it
!= state_map
.end(); ++it
) {
130 // TODO(oleg): UNKNOWN is treated as MALWARE for backwards compatibility.
131 // In future GetMalwareIDs will be removed and the caller will have to
132 // deal with BLACKLISTED_UNKNOWN state returned from GetBlacklistedIDs.
133 if (it
->second
== BLACKLISTED_MALWARE
|| it
->second
== BLACKLISTED_UNKNOWN
)
134 malware
.insert(it
->first
);
136 callback
.Run(malware
);
141 Blacklist::Observer::Observer(Blacklist
* blacklist
) : blacklist_(blacklist
) {
142 blacklist_
->AddObserver(this);
145 Blacklist::Observer::~Observer() {
146 blacklist_
->RemoveObserver(this);
149 Blacklist::ScopedDatabaseManagerForTest::ScopedDatabaseManagerForTest(
150 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager
)
151 : original_(GetDatabaseManager()) {
152 SetDatabaseManager(database_manager
);
155 Blacklist::ScopedDatabaseManagerForTest::~ScopedDatabaseManagerForTest() {
156 SetDatabaseManager(original_
);
159 Blacklist::Blacklist(ExtensionPrefs
* prefs
) {
160 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager
=
161 g_database_manager
.Get().get();
162 if (database_manager
.get()) {
165 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE
,
166 content::Source
<SafeBrowsingDatabaseManager
>(database_manager
.get()));
169 // Clear out the old prefs-backed blacklist, stored as empty extension entries
170 // with just a "blacklisted" property.
172 // TODO(kalman): Delete this block of code, see http://crbug.com/295882.
173 std::set
<std::string
> blacklisted
= prefs
->GetBlacklistedExtensions();
174 for (std::set
<std::string
>::iterator it
= blacklisted
.begin();
175 it
!= blacklisted
.end(); ++it
) {
176 if (!prefs
->GetInstalledExtensionInfo(*it
))
177 prefs
->DeleteExtensionPrefs(*it
);
181 Blacklist::~Blacklist() {
185 Blacklist
* Blacklist::Get(content::BrowserContext
* context
) {
186 return BlacklistFactory::GetForBrowserContext(context
);
189 void Blacklist::GetBlacklistedIDs(const std::set
<std::string
>& ids
,
190 const GetBlacklistedIDsCallback
& callback
) {
191 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
193 if (ids
.empty() || !g_database_manager
.Get().get().get()) {
194 base::MessageLoopProxy::current()->PostTask(
195 FROM_HERE
, base::Bind(callback
, BlacklistStateMap()));
199 // Constructing the SafeBrowsingClientImpl begins the process of asking
200 // safebrowsing for the blacklisted extensions. The set of blacklisted
201 // extensions returned by SafeBrowsing will then be passed to
202 // GetBlacklistStateIDs to get the particular BlacklistState for each id.
203 new SafeBrowsingClientImpl(
204 ids
, base::Bind(&Blacklist::GetBlacklistStateForIDs
, AsWeakPtr(),
208 void Blacklist::GetMalwareIDs(const std::set
<std::string
>& ids
,
209 const GetMalwareIDsCallback
& callback
) {
210 GetBlacklistedIDs(ids
, base::Bind(&GetMalwareFromBlacklistStateMap
,
215 void Blacklist::IsBlacklisted(const std::string
& extension_id
,
216 const IsBlacklistedCallback
& callback
) {
217 std::set
<std::string
> check
;
218 check
.insert(extension_id
);
219 GetBlacklistedIDs(check
, base::Bind(&CheckOneExtensionState
, callback
));
222 void Blacklist::GetBlacklistStateForIDs(
223 const GetBlacklistedIDsCallback
& callback
,
224 const std::set
<std::string
>& blacklisted_ids
) {
225 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
227 std::set
<std::string
> ids_unknown_state
;
228 BlacklistStateMap extensions_state
;
229 for (std::set
<std::string
>::const_iterator it
= blacklisted_ids
.begin();
230 it
!= blacklisted_ids
.end(); ++it
) {
231 BlacklistStateMap::const_iterator cache_it
=
232 blacklist_state_cache_
.find(*it
);
233 if (cache_it
== blacklist_state_cache_
.end() ||
234 cache_it
->second
== BLACKLISTED_UNKNOWN
) // Do not return UNKNOWN
235 // from cache, retry request.
236 ids_unknown_state
.insert(*it
);
238 extensions_state
[*it
] = cache_it
->second
;
241 if (ids_unknown_state
.empty()) {
242 callback
.Run(extensions_state
);
244 // After the extension blacklist states have been downloaded, call this
245 // functions again, but prevent infinite cycle in case server is offline
246 // or some other reason prevents us from receiving the blacklist state for
248 RequestExtensionsBlacklistState(
250 base::Bind(&Blacklist::ReturnBlacklistStateMap
, AsWeakPtr(),
251 callback
, blacklisted_ids
));
255 void Blacklist::ReturnBlacklistStateMap(
256 const GetBlacklistedIDsCallback
& callback
,
257 const std::set
<std::string
>& blacklisted_ids
) {
258 BlacklistStateMap extensions_state
;
259 for (std::set
<std::string
>::const_iterator it
= blacklisted_ids
.begin();
260 it
!= blacklisted_ids
.end(); ++it
) {
261 BlacklistStateMap::const_iterator cache_it
=
262 blacklist_state_cache_
.find(*it
);
263 if (cache_it
!= blacklist_state_cache_
.end())
264 extensions_state
[*it
] = cache_it
->second
;
265 // If for some reason we still haven't cached the state of this extension,
266 // we silently skip it.
269 callback
.Run(extensions_state
);
272 void Blacklist::RequestExtensionsBlacklistState(
273 const std::set
<std::string
>& ids
, const base::Callback
<void()>& callback
) {
274 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
276 state_fetcher_
.reset(new BlacklistStateFetcher());
278 state_requests_
.push_back(
279 make_pair(std::vector
<std::string
>(ids
.begin(), ids
.end()), callback
));
280 for (std::set
<std::string
>::const_iterator it
= ids
.begin();
283 state_fetcher_
->Request(
285 base::Bind(&Blacklist::OnBlacklistStateReceived
, AsWeakPtr(), *it
));
289 void Blacklist::OnBlacklistStateReceived(const std::string
& id
,
290 BlacklistState state
) {
291 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
292 blacklist_state_cache_
[id
] = state
;
294 // Go through the opened requests and call the callbacks for those requests
295 // for which we already got all the required blacklist states.
296 StateRequestsList::iterator requests_it
= state_requests_
.begin();
297 while (requests_it
!= state_requests_
.end()) {
298 const std::vector
<std::string
>& ids
= requests_it
->first
;
300 bool have_all_in_cache
= true;
301 for (std::vector
<std::string
>::const_iterator ids_it
= ids
.begin();
304 if (!ContainsKey(blacklist_state_cache_
, *ids_it
)) {
305 have_all_in_cache
= false;
310 if (have_all_in_cache
) {
311 requests_it
->second
.Run();
312 requests_it
= state_requests_
.erase(requests_it
); // returns next element
319 void Blacklist::SetBlacklistStateFetcherForTest(
320 BlacklistStateFetcher
* fetcher
) {
321 state_fetcher_
.reset(fetcher
);
324 BlacklistStateFetcher
* Blacklist::ResetBlacklistStateFetcherForTest() {
325 return state_fetcher_
.release();
328 void Blacklist::AddObserver(Observer
* observer
) {
329 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
330 observers_
.AddObserver(observer
);
333 void Blacklist::RemoveObserver(Observer
* observer
) {
334 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
335 observers_
.RemoveObserver(observer
);
339 void Blacklist::SetDatabaseManager(
340 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager
) {
341 g_database_manager
.Get().set(database_manager
);
345 scoped_refptr
<SafeBrowsingDatabaseManager
> Blacklist::GetDatabaseManager() {
346 return g_database_manager
.Get().get();
349 void Blacklist::Observe(int type
,
350 const content::NotificationSource
& source
,
351 const content::NotificationDetails
& details
) {
352 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE
, type
);
353 FOR_EACH_OBSERVER(Observer
, observers_
, OnBlacklistUpdated());
356 } // namespace extensions