Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 return kLeastSeverity; | 58 return kLeastSeverity; |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 V4LocalDatabaseManager::PendingCheck::PendingCheck( | 64 V4LocalDatabaseManager::PendingCheck::PendingCheck( |
| 65 Client* client, | 65 Client* client, |
| 66 ClientCallbackType client_callback_type, | 66 ClientCallbackType client_callback_type, |
| 67 const StoresToCheck& stores_to_check, | 67 const StoresToCheck& stores_to_check, |
| 68 const GURL& url) | 68 const std::vector<GURL>& urls) |
| 69 : client(client), | 69 : client(client), |
| 70 client_callback_type(client_callback_type), | 70 client_callback_type(client_callback_type), |
| 71 result_threat_type(SB_THREAT_TYPE_SAFE), | 71 result_threat_type(SB_THREAT_TYPE_SAFE), |
| 72 stores_to_check(stores_to_check), | 72 stores_to_check(stores_to_check), |
| 73 url(url) { | 73 urls(urls) { |
| 74 DCHECK_GT(ClientCallbackType::CHECK_MAX, client_callback_type); | 74 DCHECK_GT(ClientCallbackType::CHECK_MAX, client_callback_type); |
| 75 } | 75 } |
| 76 | 76 |
| 77 V4LocalDatabaseManager::PendingCheck::~PendingCheck() {} | 77 V4LocalDatabaseManager::PendingCheck::~PendingCheck() {} |
| 78 | 78 |
| 79 // static | 79 // static |
| 80 scoped_refptr<V4LocalDatabaseManager> V4LocalDatabaseManager::Create( | 80 scoped_refptr<V4LocalDatabaseManager> V4LocalDatabaseManager::Create( |
| 81 const base::FilePath& base_path) { | 81 const base::FilePath& base_path) { |
| 82 if (!V4FeatureList::IsLocalDatabaseManagerEnabled()) { | 82 if (!V4FeatureList::IsLocalDatabaseManagerEnabled()) { |
| 83 return nullptr; | 83 return nullptr; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 | 142 |
| 143 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { | 143 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { |
| 144 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 144 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 145 | 145 |
| 146 if (!enabled_ || !CanCheckUrl(url)) { | 146 if (!enabled_ || !CanCheckUrl(url)) { |
| 147 return true; | 147 return true; |
| 148 } | 148 } |
| 149 | 149 |
| 150 std::unique_ptr<PendingCheck> check = base::MakeUnique<PendingCheck>( | 150 std::unique_ptr<PendingCheck> check = base::MakeUnique<PendingCheck>( |
| 151 client, ClientCallbackType::CHECK_BROWSE_URL, | 151 client, ClientCallbackType::CHECK_BROWSE_URL, |
| 152 StoresToCheck({GetUrlMalwareId(), GetUrlSocEngId(), GetUrlUwsId()}), url); | 152 StoresToCheck({GetUrlMalwareId(), GetUrlSocEngId(), GetUrlUwsId()}), |
| 153 if (!v4_database_) { | 153 std::vector<GURL>(1, url)); |
|
Nathan Parker
2016/10/26 00:45:45
nit: Does {url} work here? I'm not sure if that wi
vakh (use Gerrit instead)
2016/10/26 19:53:21
Tried it. MakeUnique doesn't like it very much.
| |
| 154 queued_checks_.push_back(std::move(check)); | |
| 155 return false; | |
| 156 } | |
| 157 | 154 |
| 158 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; | 155 return HandleCheck(std::move(check)); |
| 159 if (!GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes)) { | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 163 // Post the task to check full hashes back on the IO thread to follow the | |
| 164 // documented behavior of CheckBrowseUrl. | |
| 165 BrowserThread::PostTask( | |
| 166 BrowserThread::IO, FROM_HERE, | |
| 167 base::Bind(&V4LocalDatabaseManager::PerformFullHashCheck, this, | |
| 168 base::Passed(std::move(check)), | |
| 169 full_hash_to_store_and_hash_prefixes)); | |
| 170 return false; | |
| 171 } | 156 } |
| 172 | 157 |
| 173 bool V4LocalDatabaseManager::CheckDownloadUrl( | 158 bool V4LocalDatabaseManager::CheckDownloadUrl( |
| 174 const std::vector<GURL>& url_chain, | 159 const std::vector<GURL>& url_chain, |
| 175 Client* client) { | 160 Client* client) { |
| 176 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 177 // TODO(vakh): Implement this skeleton. | 162 |
| 178 return true; | 163 if (!enabled_ || url_chain.empty()) { |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 std::unique_ptr<PendingCheck> check = base::MakeUnique<PendingCheck>( | |
| 168 client, ClientCallbackType::CHECK_DOWNLOAD_URLS, | |
| 169 StoresToCheck({GetUrlMalBinId()}), url_chain); | |
| 170 | |
| 171 return HandleCheck(std::move(check)); | |
| 179 } | 172 } |
| 180 | 173 |
| 181 bool V4LocalDatabaseManager::CheckExtensionIDs( | 174 bool V4LocalDatabaseManager::CheckExtensionIDs( |
| 182 const std::set<std::string>& extension_ids, | 175 const std::set<std::string>& extension_ids, |
| 183 Client* client) { | 176 Client* client) { |
| 184 // TODO(vakh): Implement this skeleton. | 177 // TODO(vakh): Implement this skeleton. |
| 185 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 186 return true; | 179 return true; |
| 187 } | 180 } |
| 188 | 181 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 const std::unique_ptr<PendingCheck>& check, | 330 const std::unique_ptr<PendingCheck>& check, |
| 338 FullHashToStoreAndHashPrefixesMap* full_hash_to_store_and_hash_prefixes) { | 331 FullHashToStoreAndHashPrefixesMap* full_hash_to_store_and_hash_prefixes) { |
| 339 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 332 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 340 | 333 |
| 341 DCHECK(enabled_); | 334 DCHECK(enabled_); |
| 342 DCHECK(v4_database_); | 335 DCHECK(v4_database_); |
| 343 DCHECK_GT(ClientCallbackType::CHECK_MAX, check->client_callback_type); | 336 DCHECK_GT(ClientCallbackType::CHECK_MAX, check->client_callback_type); |
| 344 full_hash_to_store_and_hash_prefixes->clear(); | 337 full_hash_to_store_and_hash_prefixes->clear(); |
| 345 | 338 |
| 346 const base::TimeTicks before = TimeTicks::Now(); | 339 const base::TimeTicks before = TimeTicks::Now(); |
| 347 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL) { | 340 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL || |
| 341 check->client_callback_type == ClientCallbackType::CHECK_DOWNLOAD_URLS) { | |
| 348 std::unordered_set<FullHash> full_hashes; | 342 std::unordered_set<FullHash> full_hashes; |
| 349 V4ProtocolManagerUtil::UrlToFullHashes(check->url, &full_hashes); | 343 for (const auto& url : check->urls) { |
| 344 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); | |
| 345 } | |
| 350 | 346 |
| 351 StoreAndHashPrefixes matched_store_and_hash_prefixes; | 347 StoreAndHashPrefixes matched_store_and_hash_prefixes; |
| 352 for (const auto& full_hash : full_hashes) { | 348 for (const auto& full_hash : full_hashes) { |
| 353 matched_store_and_hash_prefixes.clear(); | 349 matched_store_and_hash_prefixes.clear(); |
| 354 v4_database_->GetStoresMatchingFullHash(full_hash, check->stores_to_check, | 350 v4_database_->GetStoresMatchingFullHash(full_hash, check->stores_to_check, |
| 355 &matched_store_and_hash_prefixes); | 351 &matched_store_and_hash_prefixes); |
| 356 if (!matched_store_and_hash_prefixes.empty()) { | 352 if (!matched_store_and_hash_prefixes.empty()) { |
| 357 (*full_hash_to_store_and_hash_prefixes)[full_hash] = | 353 (*full_hash_to_store_and_hash_prefixes)[full_hash] = |
| 358 matched_store_and_hash_prefixes; | 354 matched_store_and_hash_prefixes; |
| 359 } | 355 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 SBThreatType V4LocalDatabaseManager::GetSBThreatTypeForList( | 401 SBThreatType V4LocalDatabaseManager::GetSBThreatTypeForList( |
| 406 const ListIdentifier& list_id) { | 402 const ListIdentifier& list_id) { |
| 407 auto it = std::find_if( | 403 auto it = std::find_if( |
| 408 std::begin(list_infos_), std::end(list_infos_), | 404 std::begin(list_infos_), std::end(list_infos_), |
| 409 [&list_id](ListInfo const& li) { return li.list_id() == list_id; }); | 405 [&list_id](ListInfo const& li) { return li.list_id() == list_id; }); |
| 410 DCHECK(list_infos_.end() != it); | 406 DCHECK(list_infos_.end() != it); |
| 411 DCHECK_NE(SB_THREAT_TYPE_SAFE, it->sb_threat_type()); | 407 DCHECK_NE(SB_THREAT_TYPE_SAFE, it->sb_threat_type()); |
| 412 return it->sb_threat_type(); | 408 return it->sb_threat_type(); |
| 413 } | 409 } |
| 414 | 410 |
| 411 bool V4LocalDatabaseManager::HandleCheck(std::unique_ptr<PendingCheck> check) { | |
| 412 if (!v4_database_) { | |
| 413 queued_checks_.push_back(std::move(check)); | |
| 414 return false; | |
| 415 } | |
| 416 | |
| 417 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; | |
| 418 if (!GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes)) { | |
| 419 return true; | |
| 420 } | |
| 421 | |
| 422 // Post on the IO thread to enforce async behavior. | |
| 423 BrowserThread::PostTask( | |
| 424 BrowserThread::IO, FROM_HERE, | |
| 425 base::Bind(&V4LocalDatabaseManager::PerformFullHashCheck, this, | |
| 426 base::Passed(std::move(check)), | |
| 427 full_hash_to_store_and_hash_prefixes)); | |
| 428 return false; | |
| 429 } | |
| 430 | |
| 415 void V4LocalDatabaseManager::OnFullHashResponse( | 431 void V4LocalDatabaseManager::OnFullHashResponse( |
| 416 std::unique_ptr<PendingCheck> pending_check, | 432 std::unique_ptr<PendingCheck> pending_check, |
| 417 const std::vector<FullHashInfo>& full_hash_infos) { | 433 const std::vector<FullHashInfo>& full_hash_infos) { |
| 418 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 434 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 419 | 435 |
| 420 if (!enabled_) { | 436 if (!enabled_) { |
| 421 DCHECK(pending_clients_.empty()); | 437 DCHECK(pending_clients_.empty()); |
| 422 return; | 438 return; |
| 423 } | 439 } |
| 424 | 440 |
| 425 auto it = pending_clients_.find(pending_check->client); | 441 const auto it = pending_clients_.find(pending_check->client); |
| 426 if (it == pending_clients_.end()) { | 442 if (it == pending_clients_.end()) { |
| 427 // The check has since been cancelled. | 443 // The check has since been cancelled. |
| 428 return; | 444 return; |
| 429 } | 445 } |
| 430 | 446 |
| 431 // Find out the most severe threat, if any, to report to the client. | 447 // Find out the most severe threat, if any, to report to the client. |
| 432 GetSeverestThreatTypeAndMetadata(&pending_check->result_threat_type, | 448 GetSeverestThreatTypeAndMetadata(&pending_check->result_threat_type, |
| 433 &pending_check->url_metadata, | 449 &pending_check->url_metadata, |
| 434 full_hash_infos); | 450 full_hash_infos); |
| 435 pending_clients_.erase(it); | 451 pending_clients_.erase(it); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 473 } | 489 } |
| 474 queued_checks_.clear(); | 490 queued_checks_.clear(); |
| 475 } | 491 } |
| 476 | 492 |
| 477 void V4LocalDatabaseManager::RespondToClient( | 493 void V4LocalDatabaseManager::RespondToClient( |
| 478 std::unique_ptr<PendingCheck> check) { | 494 std::unique_ptr<PendingCheck> check) { |
| 479 DCHECK(check.get()); | 495 DCHECK(check.get()); |
| 480 DCHECK_GT(ClientCallbackType::CHECK_MAX, check->client_callback_type); | 496 DCHECK_GT(ClientCallbackType::CHECK_MAX, check->client_callback_type); |
| 481 | 497 |
| 482 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL) { | 498 if (check->client_callback_type == ClientCallbackType::CHECK_BROWSE_URL) { |
| 483 check->client->OnCheckBrowseUrlResult(check->url, check->result_threat_type, | 499 DCHECK_EQ(1u, check->urls.size()); |
| 484 check->url_metadata); | 500 check->client->OnCheckBrowseUrlResult( |
| 501 check->urls[0], check->result_threat_type, check->url_metadata); | |
| 502 } else if (check->client_callback_type == | |
| 503 ClientCallbackType::CHECK_DOWNLOAD_URLS) { | |
| 504 check->client->OnCheckDownloadUrlResult(check->urls, | |
| 505 check->result_threat_type); | |
| 485 } else { | 506 } else { |
| 486 NOTREACHED() << "Unexpected client_callback_type encountered"; | 507 NOTREACHED() << "Unexpected client_callback_type encountered"; |
| 487 } | 508 } |
| 488 } | 509 } |
| 489 | 510 |
| 490 void V4LocalDatabaseManager::SetupDatabase() { | 511 void V4LocalDatabaseManager::SetupDatabase() { |
| 491 DCHECK(!base_path_.empty()); | 512 DCHECK(!base_path_.empty()); |
| 492 DCHECK(!list_infos_.empty()); | 513 DCHECK(!list_infos_.empty()); |
| 493 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 514 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 494 | 515 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 521 } | 542 } |
| 522 | 543 |
| 523 void V4LocalDatabaseManager::UpdateRequestCompleted( | 544 void V4LocalDatabaseManager::UpdateRequestCompleted( |
| 524 std::unique_ptr<ParsedServerResponse> parsed_server_response) { | 545 std::unique_ptr<ParsedServerResponse> parsed_server_response) { |
| 525 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 546 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 526 v4_database_->ApplyUpdate(std::move(parsed_server_response), | 547 v4_database_->ApplyUpdate(std::move(parsed_server_response), |
| 527 db_updated_callback_); | 548 db_updated_callback_); |
| 528 } | 549 } |
| 529 | 550 |
| 530 } // namespace safe_browsing | 551 } // namespace safe_browsing |
| OLD | NEW |