| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/safe_browsing/database_manager.h" | 5 #include "chrome/browser/safe_browsing/database_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 if (!MakeDatabaseAvailable()) { | 329 if (!MakeDatabaseAvailable()) { |
| 330 QueuedCheck queued_check(safe_browsing_util::MALWARE, // or PHISH | 330 QueuedCheck queued_check(safe_browsing_util::MALWARE, // or PHISH |
| 331 client, | 331 client, |
| 332 url, | 332 url, |
| 333 expected_threats, | 333 expected_threats, |
| 334 start); | 334 start); |
| 335 queued_checks_.push_back(queued_check); | 335 queued_checks_.push_back(queued_check); |
| 336 return false; | 336 return false; |
| 337 } | 337 } |
| 338 | 338 |
| 339 std::string list; | |
| 340 std::vector<SBPrefix> prefix_hits; | 339 std::vector<SBPrefix> prefix_hits; |
| 341 std::vector<SBFullHashResult> full_hits; | 340 std::vector<SBFullHashResult> cache_hits; |
| 342 | 341 |
| 343 bool prefix_match = | 342 bool prefix_match = |
| 344 database_->ContainsBrowseUrl(url, &list, &prefix_hits, &full_hits, | 343 database_->ContainsBrowseUrl(url, &prefix_hits, &cache_hits); |
| 345 sb_service_->protocol_manager()->last_update()); | |
| 346 | 344 |
| 347 UMA_HISTOGRAM_TIMES("SB2.FilterCheck", base::TimeTicks::Now() - start); | 345 UMA_HISTOGRAM_TIMES("SB2.FilterCheck", base::TimeTicks::Now() - start); |
| 348 | 346 |
| 349 if (!prefix_match) | 347 if (!prefix_match) |
| 350 return true; // URL is okay. | 348 return true; // URL is okay. |
| 351 | 349 |
| 352 // Needs to be asynchronous, since we could be in the constructor of a | 350 // Needs to be asynchronous, since we could be in the constructor of a |
| 353 // ResourceDispatcherHost event handler which can't pause there. | 351 // ResourceDispatcherHost event handler which can't pause there. |
| 354 SafeBrowsingCheck* check = new SafeBrowsingCheck(std::vector<GURL>(1, url), | 352 SafeBrowsingCheck* check = new SafeBrowsingCheck(std::vector<GURL>(1, url), |
| 355 std::vector<SBFullHash>(), | 353 std::vector<SBFullHash>(), |
| 356 client, | 354 client, |
| 357 safe_browsing_util::MALWARE, | 355 safe_browsing_util::MALWARE, |
| 358 expected_threats); | 356 expected_threats); |
| 359 check->need_get_hash = full_hits.empty(); | 357 check->need_get_hash = cache_hits.empty(); |
| 360 check->prefix_hits.swap(prefix_hits); | 358 check->prefix_hits.swap(prefix_hits); |
| 361 check->full_hits.swap(full_hits); | 359 check->cache_hits.swap(cache_hits); |
| 362 checks_.insert(check); | 360 checks_.insert(check); |
| 363 | 361 |
| 364 BrowserThread::PostTask( | 362 BrowserThread::PostTask( |
| 365 BrowserThread::IO, FROM_HERE, | 363 BrowserThread::IO, FROM_HERE, |
| 366 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); | 364 base::Bind(&SafeBrowsingDatabaseManager::OnCheckDone, this, check)); |
| 367 | 365 |
| 368 return false; | 366 return false; |
| 369 } | 367 } |
| 370 | 368 |
| 371 void SafeBrowsingDatabaseManager::CancelCheck(Client* client) { | 369 void SafeBrowsingDatabaseManager::CancelCheck(Client* client) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 387 if (it->client == client) | 385 if (it->client == client) |
| 388 it = queued_checks_.erase(it); | 386 it = queued_checks_.erase(it); |
| 389 else | 387 else |
| 390 ++it; | 388 ++it; |
| 391 } | 389 } |
| 392 } | 390 } |
| 393 | 391 |
| 394 void SafeBrowsingDatabaseManager::HandleGetHashResults( | 392 void SafeBrowsingDatabaseManager::HandleGetHashResults( |
| 395 SafeBrowsingCheck* check, | 393 SafeBrowsingCheck* check, |
| 396 const std::vector<SBFullHashResult>& full_hashes, | 394 const std::vector<SBFullHashResult>& full_hashes, |
| 397 bool can_cache) { | 395 const base::TimeDelta& cache_lifetime) { |
| 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 399 | 397 |
| 400 if (!enabled_) | 398 if (!enabled_) |
| 401 return; | 399 return; |
| 402 | 400 |
| 403 // If the service has been shut down, |check| should have been deleted. | 401 // If the service has been shut down, |check| should have been deleted. |
| 404 DCHECK(checks_.find(check) != checks_.end()); | 402 DCHECK(checks_.find(check) != checks_.end()); |
| 405 | 403 |
| 406 // |start| is set before calling |GetFullHash()|, which should be | 404 // |start| is set before calling |GetFullHash()|, which should be |
| 407 // the only path which gets to here. | 405 // the only path which gets to here. |
| 408 DCHECK(!check->start.is_null()); | 406 DCHECK(!check->start.is_null()); |
| 409 UMA_HISTOGRAM_LONG_TIMES("SB2.Network", | 407 UMA_HISTOGRAM_LONG_TIMES("SB2.Network", |
| 410 base::TimeTicks::Now() - check->start); | 408 base::TimeTicks::Now() - check->start); |
| 411 | 409 |
| 412 std::vector<SBPrefix> prefixes = check->prefix_hits; | 410 std::vector<SBPrefix> prefixes = check->prefix_hits; |
| 413 OnHandleGetHashResults(check, full_hashes); // 'check' is deleted here. | 411 OnHandleGetHashResults(check, full_hashes); // 'check' is deleted here. |
| 414 | 412 |
| 415 if (can_cache && MakeDatabaseAvailable()) { | 413 if (cache_lifetime != base::TimeDelta() && MakeDatabaseAvailable()) { |
| 416 // Cache the GetHash results in memory: | 414 // Cache the GetHash results in memory: |
| 417 database_->CacheHashResults(prefixes, full_hashes); | 415 database_->CacheHashResults(prefixes, full_hashes, cache_lifetime); |
| 418 } | 416 } |
| 419 } | 417 } |
| 420 | 418 |
| 421 void SafeBrowsingDatabaseManager::GetChunks(GetChunksCallback callback) { | 419 void SafeBrowsingDatabaseManager::GetChunks(GetChunksCallback callback) { |
| 422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 423 DCHECK(enabled_); | 421 DCHECK(enabled_); |
| 424 DCHECK(!callback.is_null()); | 422 DCHECK(!callback.is_null()); |
| 425 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind( | 423 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, base::Bind( |
| 426 &SafeBrowsingDatabaseManager::GetAllChunksFromDatabase, this, callback)); | 424 &SafeBrowsingDatabaseManager::GetAllChunksFromDatabase, this, callback)); |
| 427 } | 425 } |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 bool is_download = check->check_type == safe_browsing_util::BINURL; | 678 bool is_download = check->check_type == safe_browsing_util::BINURL; |
| 681 sb_service_->protocol_manager()->GetFullHash( | 679 sb_service_->protocol_manager()->GetFullHash( |
| 682 check->prefix_hits, | 680 check->prefix_hits, |
| 683 base::Bind(&SafeBrowsingDatabaseManager::HandleGetHashResults, | 681 base::Bind(&SafeBrowsingDatabaseManager::HandleGetHashResults, |
| 684 base::Unretained(this), | 682 base::Unretained(this), |
| 685 check), | 683 check), |
| 686 is_download); | 684 is_download); |
| 687 } else { | 685 } else { |
| 688 // We may have cached results for previous GetHash queries. Since | 686 // We may have cached results for previous GetHash queries. Since |
| 689 // this data comes from cache, don't histogram hits. | 687 // this data comes from cache, don't histogram hits. |
| 690 HandleOneCheck(check, check->full_hits); | 688 bool is_threat = HandleOneCheck(check, check->cache_hits); |
| 689 // cache_hits should only contain hits for a fullhash we searched for, so if |
| 690 // we got to this point it should always result in a threat match. |
| 691 DCHECK(is_threat); |
| 691 } | 692 } |
| 692 } | 693 } |
| 693 | 694 |
| 694 void SafeBrowsingDatabaseManager::GetAllChunksFromDatabase( | 695 void SafeBrowsingDatabaseManager::GetAllChunksFromDatabase( |
| 695 GetChunksCallback callback) { | 696 GetChunksCallback callback) { |
| 696 DCHECK_EQ(base::MessageLoop::current(), | 697 DCHECK_EQ(base::MessageLoop::current(), |
| 697 safe_browsing_thread_->message_loop()); | 698 safe_browsing_thread_->message_loop()); |
| 698 | 699 |
| 699 bool database_error = true; | 700 bool database_error = true; |
| 700 std::vector<SBListChunkRanges> lists; | 701 std::vector<SBListChunkRanges> lists; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 closing_database_ = false; | 838 closing_database_ = false; |
| 838 } | 839 } |
| 839 | 840 |
| 840 void SafeBrowsingDatabaseManager::OnResetDatabase() { | 841 void SafeBrowsingDatabaseManager::OnResetDatabase() { |
| 841 DCHECK_EQ(base::MessageLoop::current(), | 842 DCHECK_EQ(base::MessageLoop::current(), |
| 842 safe_browsing_thread_->message_loop()); | 843 safe_browsing_thread_->message_loop()); |
| 843 GetDatabase()->ResetDatabase(); | 844 GetDatabase()->ResetDatabase(); |
| 844 } | 845 } |
| 845 | 846 |
| 846 void SafeBrowsingDatabaseManager::CacheHashResults( | 847 void SafeBrowsingDatabaseManager::CacheHashResults( |
| 847 const std::vector<SBPrefix>& prefixes, | 848 const std::vector<SBPrefix>& prefixes, |
| 848 const std::vector<SBFullHashResult>& full_hashes) { | 849 const std::vector<SBFullHashResult>& full_hashes, |
| 850 const base::TimeDelta& cache_lifetime) { |
| 849 DCHECK_EQ(base::MessageLoop::current(), | 851 DCHECK_EQ(base::MessageLoop::current(), |
| 850 safe_browsing_thread_->message_loop()); | 852 safe_browsing_thread_->message_loop()); |
| 851 GetDatabase()->CacheHashResults(prefixes, full_hashes); | 853 GetDatabase()->CacheHashResults(prefixes, full_hashes, cache_lifetime); |
| 852 } | 854 } |
| 853 | 855 |
| 854 void SafeBrowsingDatabaseManager::OnHandleGetHashResults( | 856 void SafeBrowsingDatabaseManager::OnHandleGetHashResults( |
| 855 SafeBrowsingCheck* check, | 857 SafeBrowsingCheck* check, |
| 856 const std::vector<SBFullHashResult>& full_hashes) { | 858 const std::vector<SBFullHashResult>& full_hashes) { |
| 857 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 859 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 858 safe_browsing_util::ListType check_type = check->check_type; | 860 safe_browsing_util::ListType check_type = check->check_type; |
| 859 SBPrefix prefix = check->prefix_hits[0]; | 861 SBPrefix prefix = check->prefix_hits[0]; |
| 860 GetHashRequests::iterator it = gethash_requests_.find(prefix); | 862 GetHashRequests::iterator it = gethash_requests_.find(prefix); |
| 861 if (check->prefix_hits.size() > 1 || it == gethash_requests_.end()) { | 863 if (check->prefix_hits.size() > 1 || it == gethash_requests_.end()) { |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this)); | 1017 new base::WeakPtrFactory<SafeBrowsingDatabaseManager>(this)); |
| 1016 checks_.insert(check); | 1018 checks_.insert(check); |
| 1017 | 1019 |
| 1018 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task); | 1020 safe_browsing_thread_->message_loop()->PostTask(FROM_HERE, task); |
| 1019 | 1021 |
| 1020 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 1022 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 1021 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback, | 1023 base::Bind(&SafeBrowsingDatabaseManager::TimeoutCallback, |
| 1022 check->timeout_factory_->GetWeakPtr(), check), | 1024 check->timeout_factory_->GetWeakPtr(), check), |
| 1023 check_timeout_); | 1025 check_timeout_); |
| 1024 } | 1026 } |
| OLD | NEW |