| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // This file should not be build on Android but is currently getting built. | 5 // This file should not be build on Android but is currently getting built. |
| 6 // TODO(vakh): Fix that: http://crbug.com/621647 | 6 // TODO(vakh): Fix that: http://crbug.com/621647 |
| 7 | 7 |
| 8 #include "components/safe_browsing_db/v4_local_database_manager.h" | 8 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 return kLeastSeverity; | 56 return kLeastSeverity; |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 } // namespace | 60 } // namespace |
| 61 | 61 |
| 62 V4LocalDatabaseManager::PendingCheck::PendingCheck( | 62 V4LocalDatabaseManager::PendingCheck::PendingCheck( |
| 63 Client* client, | 63 Client* client, |
| 64 ClientCallbackType client_callback_type, | 64 ClientCallbackType client_callback_type, |
| 65 const StoresToCheck& stores_to_check, | 65 const StoresToCheck& stores_to_check, |
| 66 const GURL& url) | 66 const std::vector<GURL>& urls) |
| 67 : client(client), | 67 : client(client), |
| 68 client_callback_type(client_callback_type), | 68 client_callback_type(client_callback_type), |
| 69 result_threat_type(SB_THREAT_TYPE_SAFE), | 69 result_threat_type(SB_THREAT_TYPE_SAFE), |
| 70 stores_to_check(stores_to_check), | 70 stores_to_check(stores_to_check), |
| 71 url(url) { | 71 urls(urls) {} |
| 72 DCHECK_GT(ClientCallbackType::CHECK_MAX, client_callback_type); | |
| 73 } | |
| 74 | 72 |
| 75 V4LocalDatabaseManager::PendingCheck::~PendingCheck() {} | 73 V4LocalDatabaseManager::PendingCheck::~PendingCheck() {} |
| 76 | 74 |
| 77 // static | 75 // static |
| 78 scoped_refptr<V4LocalDatabaseManager> V4LocalDatabaseManager::Create( | 76 scoped_refptr<V4LocalDatabaseManager> V4LocalDatabaseManager::Create( |
| 79 const base::FilePath& base_path) { | 77 const base::FilePath& base_path) { |
| 80 if (!V4FeatureList::IsLocalDatabaseManagerEnabled()) { | 78 if (!V4FeatureList::IsLocalDatabaseManagerEnabled()) { |
| 81 return nullptr; | 79 return nullptr; |
| 82 } | 80 } |
| 83 | 81 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 138 |
| 141 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { | 139 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 143 | 141 |
| 144 if (!enabled_ || !CanCheckUrl(url)) { | 142 if (!enabled_ || !CanCheckUrl(url)) { |
| 145 return true; | 143 return true; |
| 146 } | 144 } |
| 147 | 145 |
| 148 std::unique_ptr<PendingCheck> check = base::MakeUnique<PendingCheck>( | 146 std::unique_ptr<PendingCheck> check = base::MakeUnique<PendingCheck>( |
| 149 client, ClientCallbackType::CHECK_BROWSE_URL, | 147 client, ClientCallbackType::CHECK_BROWSE_URL, |
| 150 StoresToCheck({GetUrlMalwareId(), GetUrlSocEngId(), GetUrlUwsId()}), url); | 148 StoresToCheck({GetUrlMalwareId(), GetUrlSocEngId(), GetUrlUwsId()}), |
| 151 if (!v4_database_) { | 149 std::vector<GURL>(1, url)); |
| 152 queued_checks_.push_back(std::move(check)); | |
| 153 return false; | |
| 154 } | |
| 155 | 150 |
| 156 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; | 151 return HandleCheck(std::move(check)); |
| 157 if (!GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes)) { | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 // Post the task to check full hashes back on the IO thread to follow the | |
| 162 // documented behavior of CheckBrowseUrl. | |
| 163 BrowserThread::PostTask( | |
| 164 BrowserThread::IO, FROM_HERE, | |
| 165 base::Bind(&V4LocalDatabaseManager::PerformFullHashCheck, this, | |
| 166 base::Passed(std::move(check)), | |
| 167 full_hash_to_store_and_hash_prefixes)); | |
| 168 return false; | |
| 169 } | 152 } |
| 170 | 153 |
| 171 bool V4LocalDatabaseManager::CheckDownloadUrl( | 154 bool V4LocalDatabaseManager::CheckDownloadUrl( |
| 172 const std::vector<GURL>& url_chain, | 155 const std::vector<GURL>& url_chain, |
| 173 Client* client) { | 156 Client* client) { |
| 174 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 157 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 175 // TODO(vakh): Implement this skeleton. | 158 |
| 176 return true; | 159 if (!enabled_ || url_chain.empty()) { |
| 160 return true; |
| 161 } |
| 162 |
| 163 std::unique_ptr<PendingCheck> check = base::MakeUnique<PendingCheck>( |
| 164 client, ClientCallbackType::CHECK_DOWNLOAD_URLS, |
| 165 StoresToCheck({GetUrlMalBinId()}), url_chain); |
| 166 |
| 167 return HandleCheck(std::move(check)); |
| 177 } | 168 } |
| 178 | 169 |
| 179 bool V4LocalDatabaseManager::CheckExtensionIDs( | 170 bool V4LocalDatabaseManager::CheckExtensionIDs( |
| 180 const std::set<std::string>& extension_ids, | 171 const std::set<std::string>& extension_ids, |
| 181 Client* client) { | 172 Client* client) { |
| 182 // TODO(vakh): Implement this skeleton. | 173 // TODO(vakh): Implement this skeleton. |
| 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 174 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 184 return true; | 175 return true; |
| 185 } | 176 } |
| 186 | 177 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 } | 322 } |
| 332 } | 323 } |
| 333 | 324 |
| 334 bool V4LocalDatabaseManager::GetPrefixMatches( | 325 bool V4LocalDatabaseManager::GetPrefixMatches( |
| 335 const std::unique_ptr<PendingCheck>& check, | 326 const std::unique_ptr<PendingCheck>& check, |
| 336 FullHashToStoreAndHashPrefixesMap* full_hash_to_store_and_hash_prefixes) { | 327 FullHashToStoreAndHashPrefixesMap* full_hash_to_store_and_hash_prefixes) { |
| 337 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 328 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 338 | 329 |
| 339 DCHECK(enabled_); | 330 DCHECK(enabled_); |
| 340 DCHECK(v4_database_); | 331 DCHECK(v4_database_); |
| 341 DCHECK_GT(ClientCallbackType::CHECK_MAX, check->client_callback_type); | |
| 342 full_hash_to_store_and_hash_prefixes->clear(); | 332 full_hash_to_store_and_hash_prefixes->clear(); |
| 343 | 333 |
| 344 const base::TimeTicks before = TimeTicks::Now(); | 334 const base::TimeTicks before = TimeTicks::Now(); |
| 345 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL) { | 335 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL || |
| 336 check->client_callback_type == ClientCallbackType::CHECK_DOWNLOAD_URLS) { |
| 346 std::unordered_set<FullHash> full_hashes; | 337 std::unordered_set<FullHash> full_hashes; |
| 347 V4ProtocolManagerUtil::UrlToFullHashes(check->url, &full_hashes); | 338 for (const auto& url : check->urls) { |
| 339 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); |
| 340 } |
| 348 | 341 |
| 349 StoreAndHashPrefixes matched_store_and_hash_prefixes; | 342 StoreAndHashPrefixes matched_store_and_hash_prefixes; |
| 350 for (const auto& full_hash : full_hashes) { | 343 for (const auto& full_hash : full_hashes) { |
| 351 matched_store_and_hash_prefixes.clear(); | 344 matched_store_and_hash_prefixes.clear(); |
| 352 v4_database_->GetStoresMatchingFullHash(full_hash, check->stores_to_check, | 345 v4_database_->GetStoresMatchingFullHash(full_hash, check->stores_to_check, |
| 353 &matched_store_and_hash_prefixes); | 346 &matched_store_and_hash_prefixes); |
| 354 if (!matched_store_and_hash_prefixes.empty()) { | 347 if (!matched_store_and_hash_prefixes.empty()) { |
| 355 (*full_hash_to_store_and_hash_prefixes)[full_hash] = | 348 (*full_hash_to_store_and_hash_prefixes)[full_hash] = |
| 356 matched_store_and_hash_prefixes; | 349 matched_store_and_hash_prefixes; |
| 357 } | 350 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 SBThreatType V4LocalDatabaseManager::GetSBThreatTypeForList( | 396 SBThreatType V4LocalDatabaseManager::GetSBThreatTypeForList( |
| 404 const ListIdentifier& list_id) { | 397 const ListIdentifier& list_id) { |
| 405 auto it = std::find_if( | 398 auto it = std::find_if( |
| 406 std::begin(list_infos_), std::end(list_infos_), | 399 std::begin(list_infos_), std::end(list_infos_), |
| 407 [&list_id](ListInfo const& li) { return li.list_id() == list_id; }); | 400 [&list_id](ListInfo const& li) { return li.list_id() == list_id; }); |
| 408 DCHECK(list_infos_.end() != it); | 401 DCHECK(list_infos_.end() != it); |
| 409 DCHECK_NE(SB_THREAT_TYPE_SAFE, it->sb_threat_type()); | 402 DCHECK_NE(SB_THREAT_TYPE_SAFE, it->sb_threat_type()); |
| 410 return it->sb_threat_type(); | 403 return it->sb_threat_type(); |
| 411 } | 404 } |
| 412 | 405 |
| 406 bool V4LocalDatabaseManager::HandleCheck(std::unique_ptr<PendingCheck> check) { |
| 407 if (!v4_database_) { |
| 408 queued_checks_.push_back(std::move(check)); |
| 409 return false; |
| 410 } |
| 411 |
| 412 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; |
| 413 if (!GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes)) { |
| 414 return true; |
| 415 } |
| 416 |
| 417 // Post on the IO thread to enforce async behavior. |
| 418 BrowserThread::PostTask( |
| 419 BrowserThread::IO, FROM_HERE, |
| 420 base::Bind(&V4LocalDatabaseManager::PerformFullHashCheck, this, |
| 421 base::Passed(std::move(check)), |
| 422 full_hash_to_store_and_hash_prefixes)); |
| 423 return false; |
| 424 } |
| 425 |
| 413 void V4LocalDatabaseManager::OnFullHashResponse( | 426 void V4LocalDatabaseManager::OnFullHashResponse( |
| 414 std::unique_ptr<PendingCheck> pending_check, | 427 std::unique_ptr<PendingCheck> pending_check, |
| 415 const std::vector<FullHashInfo>& full_hash_infos) { | 428 const std::vector<FullHashInfo>& full_hash_infos) { |
| 416 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 429 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 417 | 430 |
| 418 if (!enabled_) { | 431 if (!enabled_) { |
| 419 DCHECK(pending_clients_.empty()); | 432 DCHECK(pending_clients_.empty()); |
| 420 return; | 433 return; |
| 421 } | 434 } |
| 422 | 435 |
| 423 auto it = pending_clients_.find(pending_check->client); | 436 const auto it = pending_clients_.find(pending_check->client); |
| 424 if (it == pending_clients_.end()) { | 437 if (it == pending_clients_.end()) { |
| 425 // The check has since been cancelled. | 438 // The check has since been cancelled. |
| 426 return; | 439 return; |
| 427 } | 440 } |
| 428 | 441 |
| 429 // Find out the most severe threat, if any, to report to the client. | 442 // Find out the most severe threat, if any, to report to the client. |
| 430 GetSeverestThreatTypeAndMetadata(&pending_check->result_threat_type, | 443 GetSeverestThreatTypeAndMetadata(&pending_check->result_threat_type, |
| 431 &pending_check->url_metadata, | 444 &pending_check->url_metadata, |
| 432 full_hash_infos); | 445 full_hash_infos); |
| 433 pending_clients_.erase(it); | 446 pending_clients_.erase(it); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 481 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 469 for (std::unique_ptr<PendingCheck>& it : queued_checks_) { | 482 for (std::unique_ptr<PendingCheck>& it : queued_checks_) { |
| 470 RespondToClient(std::move(it)); | 483 RespondToClient(std::move(it)); |
| 471 } | 484 } |
| 472 queued_checks_.clear(); | 485 queued_checks_.clear(); |
| 473 } | 486 } |
| 474 | 487 |
| 475 void V4LocalDatabaseManager::RespondToClient( | 488 void V4LocalDatabaseManager::RespondToClient( |
| 476 std::unique_ptr<PendingCheck> check) { | 489 std::unique_ptr<PendingCheck> check) { |
| 477 DCHECK(check.get()); | 490 DCHECK(check.get()); |
| 478 DCHECK_GT(ClientCallbackType::CHECK_MAX, check->client_callback_type); | |
| 479 | 491 |
| 480 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL) { | 492 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL) { |
| 481 check->client->OnCheckBrowseUrlResult(check->url, check->result_threat_type, | 493 DCHECK_EQ(1u, check->urls.size()); |
| 482 check->url_metadata); | 494 check->client->OnCheckBrowseUrlResult( |
| 495 check->urls[0], check->result_threat_type, check->url_metadata); |
| 496 } else if (check->client_callback_type == |
| 497 ClientCallbackType::CHECK_DOWNLOAD_URLS) { |
| 498 check->client->OnCheckDownloadUrlResult(check->urls, |
| 499 check->result_threat_type); |
| 483 } else { | 500 } else { |
| 484 NOTREACHED() << "Unexpected client_callback_type encountered"; | 501 NOTREACHED() << "Unexpected client_callback_type encountered"; |
| 485 } | 502 } |
| 486 } | 503 } |
| 487 | 504 |
| 488 void V4LocalDatabaseManager::SetupDatabase() { | 505 void V4LocalDatabaseManager::SetupDatabase() { |
| 489 DCHECK(!base_path_.empty()); | 506 DCHECK(!base_path_.empty()); |
| 490 DCHECK(!list_infos_.empty()); | 507 DCHECK(!list_infos_.empty()); |
| 491 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 508 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 492 | 509 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 519 } | 536 } |
| 520 | 537 |
| 521 void V4LocalDatabaseManager::UpdateRequestCompleted( | 538 void V4LocalDatabaseManager::UpdateRequestCompleted( |
| 522 std::unique_ptr<ParsedServerResponse> parsed_server_response) { | 539 std::unique_ptr<ParsedServerResponse> parsed_server_response) { |
| 523 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 540 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 524 v4_database_->ApplyUpdate(std::move(parsed_server_response), | 541 v4_database_->ApplyUpdate(std::move(parsed_server_response), |
| 525 db_updated_callback_); | 542 db_updated_callback_); |
| 526 } | 543 } |
| 527 | 544 |
| 528 } // namespace safe_browsing | 545 } // namespace safe_browsing |
| OLD | NEW |