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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 112 |
112 scoped_refptr<base::MessageLoopProxy> callback_message_loop_; | 113 scoped_refptr<base::MessageLoopProxy> callback_message_loop_; |
113 OnResultCallback callback_; | 114 OnResultCallback callback_; |
114 | 115 |
115 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingClientImpl); | 116 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingClientImpl); |
116 }; | 117 }; |
117 | 118 |
118 void CheckOneExtensionState( | 119 void CheckOneExtensionState( |
119 const Blacklist::IsBlacklistedCallback& callback, | 120 const Blacklist::IsBlacklistedCallback& callback, |
120 const Blacklist::BlacklistStateMap& state_map) { | 121 const Blacklist::BlacklistStateMap& state_map) { |
121 callback.Run(state_map.empty() ? Blacklist::NOT_BLACKLISTED | 122 callback.Run(state_map.empty() ? NOT_BLACKLISTED : state_map.begin()->second); |
122 : state_map.begin()->second); | |
123 } | 123 } |
124 | 124 |
125 void GetMalwareFromBlacklistStateMap( | 125 void GetMalwareFromBlacklistStateMap( |
126 const Blacklist::GetMalwareIDsCallback& callback, | 126 const Blacklist::GetMalwareIDsCallback& callback, |
127 const Blacklist::BlacklistStateMap& state_map) { | 127 const Blacklist::BlacklistStateMap& state_map) { |
128 std::set<std::string> malware; | 128 std::set<std::string> malware; |
129 for (Blacklist::BlacklistStateMap::const_iterator it = state_map.begin(); | 129 for (Blacklist::BlacklistStateMap::const_iterator it = state_map.begin(); |
130 it != state_map.end(); ++it) { | 130 it != state_map.end(); ++it) { |
131 if (it->second == Blacklist::BLACKLISTED_MALWARE) | 131 // TODO(oleg): UNKNOWN is treated as MALWARE for backwards compatibility. |
| 132 // In future GetMalwareIDs will be removed and the caller will have to |
| 133 // deal with BLACKLISTED_UNKNOWN state returned from GetBlacklistedIDs. |
| 134 if (it->second == BLACKLISTED_MALWARE || it->second == BLACKLISTED_UNKNOWN) |
132 malware.insert(it->first); | 135 malware.insert(it->first); |
133 } | 136 } |
134 callback.Run(malware); | 137 callback.Run(malware); |
135 } | 138 } |
136 | 139 |
137 } // namespace | 140 } // namespace |
138 | 141 |
139 Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { | 142 Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { |
140 blacklist_->AddObserver(this); | 143 blacklist_->AddObserver(this); |
141 } | 144 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 const GetBlacklistedIDsCallback& callback, | 219 const GetBlacklistedIDsCallback& callback, |
217 const std::set<std::string>& blacklisted_ids) { | 220 const std::set<std::string>& blacklisted_ids) { |
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
219 | 222 |
220 std::set<std::string> ids_unknown_state; | 223 std::set<std::string> ids_unknown_state; |
221 BlacklistStateMap extensions_state; | 224 BlacklistStateMap extensions_state; |
222 for (std::set<std::string>::const_iterator it = blacklisted_ids.begin(); | 225 for (std::set<std::string>::const_iterator it = blacklisted_ids.begin(); |
223 it != blacklisted_ids.end(); ++it) { | 226 it != blacklisted_ids.end(); ++it) { |
224 BlacklistStateMap::const_iterator cache_it = | 227 BlacklistStateMap::const_iterator cache_it = |
225 blacklist_state_cache_.find(*it); | 228 blacklist_state_cache_.find(*it); |
226 if (cache_it == blacklist_state_cache_.end()) | 229 if (cache_it == blacklist_state_cache_.end() || |
| 230 cache_it->second == BLACKLISTED_UNKNOWN) // Do not return UNKNOWN |
| 231 // from cache, retry request. |
227 ids_unknown_state.insert(*it); | 232 ids_unknown_state.insert(*it); |
228 else | 233 else |
229 extensions_state[*it] = cache_it->second; | 234 extensions_state[*it] = cache_it->second; |
230 } | 235 } |
231 | 236 |
232 if (ids_unknown_state.empty()) { | 237 if (ids_unknown_state.empty()) { |
233 callback.Run(extensions_state); | 238 callback.Run(extensions_state); |
234 } else { | 239 } else { |
235 // After the extension blacklist states have been downloaded, call this | 240 // After the extension blacklist states have been downloaded, call this |
236 // functions again, but prevent infinite cycle in case server is offline | 241 // 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()) | 259 if (cache_it != blacklist_state_cache_.end()) |
255 extensions_state[*it] = cache_it->second; | 260 extensions_state[*it] = cache_it->second; |
256 // If for some reason we still haven't cached the state of this extension, | 261 // If for some reason we still haven't cached the state of this extension, |
257 // we silently skip it. | 262 // we silently skip it. |
258 } | 263 } |
259 | 264 |
260 callback.Run(extensions_state); | 265 callback.Run(extensions_state); |
261 } | 266 } |
262 | 267 |
263 void Blacklist::RequestExtensionsBlacklistState( | 268 void Blacklist::RequestExtensionsBlacklistState( |
264 const std::set<std::string> ids, base::Callback<void()> callback) { | 269 const std::set<std::string>& ids, const base::Callback<void()>& callback) { |
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
266 // This is a stub. The request will be made here, but the server is not up | 271 if (!state_fetcher_) |
267 // yet. For compatibility with current blacklist logic, mark all extensions | 272 state_fetcher_.reset(new BlacklistStateFetcher()); |
268 // as malicious. | 273 |
| 274 state_requests_.push_back( |
| 275 make_pair(std::vector<std::string>(ids.begin(), ids.end()), callback)); |
269 for (std::set<std::string>::const_iterator it = ids.begin(); | 276 for (std::set<std::string>::const_iterator it = ids.begin(); |
270 it != ids.end(); | 277 it != ids.end(); |
271 ++it) { | 278 ++it) { |
272 blacklist_state_cache_[*it] = BLACKLISTED_MALWARE; | 279 state_fetcher_->Request( |
| 280 *it, |
| 281 base::Bind(&Blacklist::OnBlacklistStateReceived, AsWeakPtr(), *it)); |
273 } | 282 } |
274 callback.Run(); | 283 } |
| 284 |
| 285 void Blacklist::OnBlacklistStateReceived(const std::string& id, |
| 286 BlacklistState state) { |
| 287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 288 blacklist_state_cache_[id] = state; |
| 289 |
| 290 // Go through the opened requests and call the callbacks for those requests |
| 291 // for which we already got all the required blacklist states. |
| 292 StateRequestsList::iterator requests_it = state_requests_.begin(); |
| 293 while (requests_it != state_requests_.end()) { |
| 294 const std::vector<std::string>& ids = requests_it->first; |
| 295 |
| 296 bool have_all_in_cache = true; |
| 297 for (std::vector<std::string>::const_iterator ids_it = ids.begin(); |
| 298 ids_it != ids.end(); |
| 299 ++ids_it) { |
| 300 if (!ContainsKey(blacklist_state_cache_, *ids_it)) { |
| 301 have_all_in_cache = false; |
| 302 break; |
| 303 } |
| 304 } |
| 305 |
| 306 if (have_all_in_cache) { |
| 307 requests_it->second.Run(); |
| 308 requests_it = state_requests_.erase(requests_it); // returns next element |
| 309 } else { |
| 310 ++requests_it; |
| 311 } |
| 312 } |
| 313 } |
| 314 |
| 315 void Blacklist::SetBlacklistStateFetcherForTest( |
| 316 BlacklistStateFetcher* fetcher) { |
| 317 state_fetcher_.reset(fetcher); |
275 } | 318 } |
276 | 319 |
277 void Blacklist::AddObserver(Observer* observer) { | 320 void Blacklist::AddObserver(Observer* observer) { |
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
279 observers_.AddObserver(observer); | 322 observers_.AddObserver(observer); |
280 } | 323 } |
281 | 324 |
282 void Blacklist::RemoveObserver(Observer* observer) { | 325 void Blacklist::RemoveObserver(Observer* observer) { |
283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
284 observers_.RemoveObserver(observer); | 327 observers_.RemoveObserver(observer); |
(...skipping 11 matching lines...) Expand all Loading... |
296 } | 339 } |
297 | 340 |
298 void Blacklist::Observe(int type, | 341 void Blacklist::Observe(int type, |
299 const content::NotificationSource& source, | 342 const content::NotificationSource& source, |
300 const content::NotificationDetails& details) { | 343 const content::NotificationDetails& details) { |
301 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, type); | 344 DCHECK_EQ(chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, type); |
302 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); | 345 FOR_EACH_OBSERVER(Observer, observers_, OnBlacklistUpdated()); |
303 } | 346 } |
304 | 347 |
305 } // namespace extensions | 348 } // namespace extensions |
OLD | NEW |