OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/blacklist.h" | 5 #include "chrome/browser/extensions/blacklist.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/extensions/blacklist_state_fetcher.h" | |
17 #include "chrome/browser/extensions/extension_prefs.h" | 18 #include "chrome/browser/extensions/extension_prefs.h" |
18 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
19 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
21 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
22 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
23 | 24 |
24 using content::BrowserThread; | 25 using content::BrowserThread; |
25 | 26 |
26 namespace extensions { | 27 namespace extensions { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 callback.Run(state_map.empty() ? Blacklist::NOT_BLACKLISTED | 122 callback.Run(state_map.empty() ? Blacklist::NOT_BLACKLISTED |
122 : state_map.begin()->second); | 123 : state_map.begin()->second); |
123 } | 124 } |
124 | 125 |
125 void GetMalwareFromBlacklistStateMap( | 126 void GetMalwareFromBlacklistStateMap( |
126 const Blacklist::GetMalwareIDsCallback& callback, | 127 const Blacklist::GetMalwareIDsCallback& callback, |
127 const Blacklist::BlacklistStateMap& state_map) { | 128 const Blacklist::BlacklistStateMap& state_map) { |
128 std::set<std::string> malware; | 129 std::set<std::string> malware; |
129 for (Blacklist::BlacklistStateMap::const_iterator it = state_map.begin(); | 130 for (Blacklist::BlacklistStateMap::const_iterator it = state_map.begin(); |
130 it != state_map.end(); ++it) { | 131 it != state_map.end(); ++it) { |
131 if (it->second == Blacklist::BLACKLISTED_MALWARE) | 132 // TODO(oleg): UNKNOWN is treated as MALWARE for backwards compatibility. |
133 // In future GetMalwareIDs will be removed and the caller will have to | |
134 // deal with BLACKLISTED_UNKNOWN state returned from GetBlacklistedIDs. | |
135 if (it->second == Blacklist::BLACKLISTED_MALWARE || | |
136 it->second == Blacklist::BLACKLISTED_UNKNOWN) | |
132 malware.insert(it->first); | 137 malware.insert(it->first); |
133 } | 138 } |
134 callback.Run(malware); | 139 callback.Run(malware); |
135 } | 140 } |
136 | 141 |
137 } // namespace | 142 } // namespace |
138 | 143 |
139 Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { | 144 Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { |
140 blacklist_->AddObserver(this); | 145 blacklist_->AddObserver(this); |
141 } | 146 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 const GetBlacklistedIDsCallback& callback, | 221 const GetBlacklistedIDsCallback& callback, |
217 const std::set<std::string>& blacklisted_ids) { | 222 const std::set<std::string>& blacklisted_ids) { |
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
219 | 224 |
220 std::set<std::string> ids_unknown_state; | 225 std::set<std::string> ids_unknown_state; |
221 BlacklistStateMap extensions_state; | 226 BlacklistStateMap extensions_state; |
222 for (std::set<std::string>::const_iterator it = blacklisted_ids.begin(); | 227 for (std::set<std::string>::const_iterator it = blacklisted_ids.begin(); |
223 it != blacklisted_ids.end(); ++it) { | 228 it != blacklisted_ids.end(); ++it) { |
224 BlacklistStateMap::const_iterator cache_it = | 229 BlacklistStateMap::const_iterator cache_it = |
225 blacklist_state_cache_.find(*it); | 230 blacklist_state_cache_.find(*it); |
226 if (cache_it == blacklist_state_cache_.end()) | 231 if (cache_it == blacklist_state_cache_.end() || |
232 cache_it->second == BLACKLISTED_UNKNOWN) // Do not return UNKNOWN | |
233 // from cache, retry request. | |
227 ids_unknown_state.insert(*it); | 234 ids_unknown_state.insert(*it); |
228 else | 235 else |
229 extensions_state[*it] = cache_it->second; | 236 extensions_state[*it] = cache_it->second; |
230 } | 237 } |
231 | 238 |
232 if (ids_unknown_state.empty()) { | 239 if (ids_unknown_state.empty()) { |
233 callback.Run(extensions_state); | 240 callback.Run(extensions_state); |
234 } else { | 241 } else { |
235 // After the extension blacklist states have been downloaded, call this | 242 // After the extension blacklist states have been downloaded, call this |
236 // functions again, but prevent infinite cycle in case server is offline | 243 // functions again, but prevent infinite cycle in case server is offline |
(...skipping 17 matching lines...) Expand all Loading... | |
254 if (cache_it != blacklist_state_cache_.end()) | 261 if (cache_it != blacklist_state_cache_.end()) |
255 extensions_state[*it] = cache_it->second; | 262 extensions_state[*it] = cache_it->second; |
256 // If for some reason we still haven't cached the state of this extension, | 263 // If for some reason we still haven't cached the state of this extension, |
257 // we silently skip it. | 264 // we silently skip it. |
258 } | 265 } |
259 | 266 |
260 callback.Run(extensions_state); | 267 callback.Run(extensions_state); |
261 } | 268 } |
262 | 269 |
263 void Blacklist::RequestExtensionsBlacklistState( | 270 void Blacklist::RequestExtensionsBlacklistState( |
264 const std::set<std::string> ids, base::Callback<void()> callback) { | 271 const std::set<std::string>& ids, const base::Callback<void()>& callback) { |
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
266 // This is a stub. The request will be made here, but the server is not up | 273 if (!state_fetcher_) |
267 // yet. For compatibility with current blacklist logic, mark all extensions | 274 state_fetcher_.reset(new BlacklistStateFetcher()); |
268 // as malicious. | 275 |
276 state_requests_.push_back( | |
277 make_pair(std::vector<std::string>(ids.begin(), ids.end()), callback)); | |
269 for (std::set<std::string>::const_iterator it = ids.begin(); | 278 for (std::set<std::string>::const_iterator it = ids.begin(); |
270 it != ids.end(); | 279 it != ids.end(); |
271 ++it) { | 280 ++it) { |
272 blacklist_state_cache_[*it] = BLACKLISTED_MALWARE; | 281 state_fetcher_->Request( |
mattm
2013/11/25 21:48:55
Have you tested this in a debug build? This functi
Oleg Eterevsky
2013/11/27 14:07:38
Done.
| |
282 *it, | |
283 base::Bind(&Blacklist::OnBlacklistStateReceived, AsWeakPtr(), *it)); | |
273 } | 284 } |
274 callback.Run(); | 285 } |
286 | |
287 void Blacklist::OnBlacklistStateReceived(const std::string& id, | |
288 BlacklistState state) { | |
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
290 blacklist_state_cache_[id] = state; | |
291 | |
292 // Go through the opened requests and call the callbacks for those requests | |
293 // for which we already got all the required blacklist states. | |
294 StateRequestsList::iterator requests_it = state_requests_.begin(); | |
295 while (requests_it != state_requests_.end()) { | |
296 const std::vector<std::string>& ids = requests_it->first; | |
297 | |
298 bool have_all_in_cache = true; | |
299 for (std::vector<std::string>::const_iterator ids_it = ids.begin(); | |
300 ids_it != ids.end(); | |
301 ++ids_it) { | |
302 if (!ContainsKey(blacklist_state_cache_, *ids_it)) { | |
303 have_all_in_cache = false; | |
304 break; | |
305 } | |
306 } | |
307 | |
308 // When erase an element from std::list, the iterator for the next element | |
309 // is returned, so no need to increment it. | |
not at google - send to devlin
2013/11/25 18:48:24
a lot of words to explain some C++ library behavio
Oleg Eterevsky
2013/11/27 14:07:38
Done.
| |
310 if (have_all_in_cache) { | |
311 requests_it->second.Run(); | |
312 requests_it = state_requests_.erase(requests_it); | |
313 } else { | |
314 ++requests_it; | |
315 } | |
316 } | |
317 } | |
318 | |
319 void Blacklist::SetBlacklistStateFetcherForTest( | |
320 BlacklistStateFetcher* fetcher) { | |
321 state_fetcher_.reset(fetcher); | |
275 } | 322 } |
276 | 323 |
277 void Blacklist::AddObserver(Observer* observer) { | 324 void Blacklist::AddObserver(Observer* observer) { |
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
279 observers_.AddObserver(observer); | 326 observers_.AddObserver(observer); |
280 } | 327 } |
281 | 328 |
282 void Blacklist::RemoveObserver(Observer* observer) { | 329 void Blacklist::RemoveObserver(Observer* observer) { |
283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
284 observers_.RemoveObserver(observer); | 331 observers_.RemoveObserver(observer); |
(...skipping 11 matching lines...) Expand all Loading... | |
296 } | 343 } |
297 | 344 |
298 void Blacklist::Observe(int type, | 345 void Blacklist::Observe(int type, |
299 const content::NotificationSource& source, | 346 const content::NotificationSource& source, |
300 const content::NotificationDetails& details) { | 347 const content::NotificationDetails& details) { |
301 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, type); | 348 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, type); |
302 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); | 349 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); |
303 } | 350 } |
304 | 351 |
305 } // namespace extensions | 352 } // namespace extensions |
OLD | NEW |