| 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> |
| 11 | 11 |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "components/safe_browsing_db/v4_feature_list.h" | 16 #include "components/safe_browsing_db/v4_feature_list.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 | 18 |
| 19 using content::BrowserThread; | 19 using content::BrowserThread; |
| 20 | 20 |
| 21 namespace safe_browsing { | 21 namespace safe_browsing { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const ThreatSeverity kLeastSeverity = | 25 const ThreatSeverity kLeastSeverity = |
| 26 std::numeric_limits<ThreatSeverity>::max(); | 26 std::numeric_limits<ThreatSeverity>::max(); |
| 27 | 27 |
| 28 ListInfos GetListInfos() { | 28 ListInfos GetListInfos() { |
| 29 // NOTE(vakh): When adding a store here, add the corresponding store-specific |
| 30 // histograms also. |
| 29 return ListInfos( | 31 return ListInfos( |
| 30 {ListInfo(true, "UrlMalware.store", GetUrlMalwareId(), | 32 {ListInfo(true, "UrlMalware.store", GetUrlMalwareId(), |
| 31 SB_THREAT_TYPE_URL_MALWARE), | 33 SB_THREAT_TYPE_URL_MALWARE), |
| 32 ListInfo(true, "UrlSoceng.store", GetUrlSocEngId(), | 34 ListInfo(true, "UrlSoceng.store", GetUrlSocEngId(), |
| 33 SB_THREAT_TYPE_URL_PHISHING), | 35 SB_THREAT_TYPE_URL_PHISHING), |
| 34 ListInfo(false, "", GetChromeUrlApiId(), SB_THREAT_TYPE_API_ABUSE), | 36 ListInfo(false, "", GetChromeUrlApiId(), SB_THREAT_TYPE_API_ABUSE), |
| 35 ListInfo(true, "UrlUws.store", GetUrlUwsId(), | 37 ListInfo(true, "UrlUws.store", GetUrlUwsId(), |
| 36 SB_THREAT_TYPE_URL_UNWANTED)}); | 38 SB_THREAT_TYPE_URL_UNWANTED)}); |
| 37 } | 39 } |
| 38 | 40 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 scoped_refptr<V4LocalDatabaseManager> V4LocalDatabaseManager::Create( | 74 scoped_refptr<V4LocalDatabaseManager> V4LocalDatabaseManager::Create( |
| 73 const base::FilePath& base_path) { | 75 const base::FilePath& base_path) { |
| 74 if (!V4FeatureList::IsLocalDatabaseManagerEnabled()) { | 76 if (!V4FeatureList::IsLocalDatabaseManagerEnabled()) { |
| 75 return nullptr; | 77 return nullptr; |
| 76 } | 78 } |
| 77 | 79 |
| 78 return make_scoped_refptr(new V4LocalDatabaseManager(base_path)); | 80 return make_scoped_refptr(new V4LocalDatabaseManager(base_path)); |
| 79 } | 81 } |
| 80 | 82 |
| 81 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) | 83 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) |
| 82 : base_path_(base_path), enabled_(false), list_infos_(GetListInfos()) { | 84 : base_path_(base_path), |
| 85 enabled_(false), |
| 86 list_infos_(GetListInfos()), |
| 87 weak_factory_(this) { |
| 83 DCHECK(!base_path_.empty()); | 88 DCHECK(!base_path_.empty()); |
| 84 DCHECK(!list_infos_.empty()); | 89 DCHECK(!list_infos_.empty()); |
| 85 | 90 |
| 86 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " | 91 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " |
| 87 << "base_path_: " << base_path_.AsUTF8Unsafe(); | 92 << "base_path_: " << base_path_.AsUTF8Unsafe(); |
| 88 } | 93 } |
| 89 | 94 |
| 90 V4LocalDatabaseManager::~V4LocalDatabaseManager() { | 95 V4LocalDatabaseManager::~V4LocalDatabaseManager() { |
| 91 DCHECK(!enabled_); | 96 DCHECK(!enabled_); |
| 92 } | 97 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 if (!v4_database_) { | 147 if (!v4_database_) { |
| 143 queued_checks_.push_back(std::move(check)); | 148 queued_checks_.push_back(std::move(check)); |
| 144 return false; | 149 return false; |
| 145 } | 150 } |
| 146 | 151 |
| 147 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; | 152 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; |
| 148 if (!GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes)) { | 153 if (!GetPrefixMatches(check, &full_hash_to_store_and_hash_prefixes)) { |
| 149 return true; | 154 return true; |
| 150 } | 155 } |
| 151 | 156 |
| 152 PerformFullHashCheck(std::move(check), full_hash_to_store_and_hash_prefixes); | 157 // Post the task to check full hashes back on the IO thread to follow the |
| 158 // documented behavior of CheckBrowseUrl. |
| 159 BrowserThread::PostTask( |
| 160 BrowserThread::IO, FROM_HERE, |
| 161 base::Bind(&V4LocalDatabaseManager::PerformFullHashCheck, this, |
| 162 base::Passed(std::move(check)), |
| 163 full_hash_to_store_and_hash_prefixes)); |
| 153 return false; | 164 return false; |
| 154 } | 165 } |
| 155 | 166 |
| 156 bool V4LocalDatabaseManager::CheckDownloadUrl( | 167 bool V4LocalDatabaseManager::CheckDownloadUrl( |
| 157 const std::vector<GURL>& url_chain, | 168 const std::vector<GURL>& url_chain, |
| 158 Client* client) { | 169 Client* client) { |
| 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 170 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 160 // TODO(vakh): Implement this skeleton. | 171 // TODO(vakh): Implement this skeleton. |
| 161 return true; | 172 return true; |
| 162 } | 173 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 bool V4LocalDatabaseManager::IsSupported() const { | 243 bool V4LocalDatabaseManager::IsSupported() const { |
| 233 return true; | 244 return true; |
| 234 } | 245 } |
| 235 | 246 |
| 236 void V4LocalDatabaseManager::StartOnIOThread( | 247 void V4LocalDatabaseManager::StartOnIOThread( |
| 237 net::URLRequestContextGetter* request_context_getter, | 248 net::URLRequestContextGetter* request_context_getter, |
| 238 const V4ProtocolConfig& config) { | 249 const V4ProtocolConfig& config) { |
| 239 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); | 250 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); |
| 240 | 251 |
| 241 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated, | 252 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated, |
| 242 base::Unretained(this)); | 253 weak_factory_.GetWeakPtr()); |
| 243 | 254 |
| 244 SetupUpdateProtocolManager(request_context_getter, config); | 255 SetupUpdateProtocolManager(request_context_getter, config); |
| 245 SetupDatabase(); | 256 SetupDatabase(); |
| 246 | 257 |
| 247 enabled_ = true; | 258 enabled_ = true; |
| 248 } | 259 } |
| 249 | 260 |
| 250 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 261 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
| 251 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 262 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 252 | 263 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 282 // database is ready, StopOnIOThread has been called. | 293 // database is ready, StopOnIOThread has been called. |
| 283 if (enabled_) { | 294 if (enabled_) { |
| 284 v4_database_ = std::move(v4_database); | 295 v4_database_ = std::move(v4_database); |
| 285 | 296 |
| 286 // The consistency of the stores read from the disk needs to verified. Post | 297 // The consistency of the stores read from the disk needs to verified. Post |
| 287 // that task on the task runner. It calls |DatabaseReadyForUpdates| | 298 // that task on the task runner. It calls |DatabaseReadyForUpdates| |
| 288 // callback with the stores to reset, if any, and then we can schedule the | 299 // callback with the stores to reset, if any, and then we can schedule the |
| 289 // database updates. | 300 // database updates. |
| 290 v4_database_->VerifyChecksum( | 301 v4_database_->VerifyChecksum( |
| 291 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForUpdates, | 302 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForUpdates, |
| 292 base::Unretained(this))); | 303 weak_factory_.GetWeakPtr())); |
| 293 | 304 |
| 294 ProcessQueuedChecks(); | 305 ProcessQueuedChecks(); |
| 295 } else { | 306 } else { |
| 296 // Schedule the deletion of v4_database off IO thread. | 307 // Schedule the deletion of v4_database off IO thread. |
| 297 V4Database::Destroy(std::move(v4_database)); | 308 V4Database::Destroy(std::move(v4_database)); |
| 298 } | 309 } |
| 299 } | 310 } |
| 300 | 311 |
| 301 void V4LocalDatabaseManager::DatabaseReadyForUpdates( | 312 void V4LocalDatabaseManager::DatabaseReadyForUpdates( |
| 302 const std::vector<ListIdentifier>& stores_to_reset) { | 313 const std::vector<ListIdentifier>& stores_to_reset) { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 428 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 418 | 429 |
| 419 DCHECK(enabled_); | 430 DCHECK(enabled_); |
| 420 DCHECK(!full_hash_to_store_and_hash_prefixes.empty()); | 431 DCHECK(!full_hash_to_store_and_hash_prefixes.empty()); |
| 421 | 432 |
| 422 pending_clients_.insert(check->client); | 433 pending_clients_.insert(check->client); |
| 423 | 434 |
| 424 v4_get_hash_protocol_manager_->GetFullHashes( | 435 v4_get_hash_protocol_manager_->GetFullHashes( |
| 425 full_hash_to_store_and_hash_prefixes, | 436 full_hash_to_store_and_hash_prefixes, |
| 426 base::Bind(&V4LocalDatabaseManager::OnFullHashResponse, | 437 base::Bind(&V4LocalDatabaseManager::OnFullHashResponse, |
| 427 base::Unretained(this), base::Passed(std::move(check)))); | 438 weak_factory_.GetWeakPtr(), base::Passed(std::move(check)))); |
| 428 } | 439 } |
| 429 | 440 |
| 430 void V4LocalDatabaseManager::ProcessQueuedChecks() { | 441 void V4LocalDatabaseManager::ProcessQueuedChecks() { |
| 431 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 442 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 432 for (auto& it : queued_checks_) { | 443 for (auto& it : queued_checks_) { |
| 433 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; | 444 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; |
| 434 if (!GetPrefixMatches(it, &full_hash_to_store_and_hash_prefixes)) { | 445 if (!GetPrefixMatches(it, &full_hash_to_store_and_hash_prefixes)) { |
| 435 RespondToClient(std::move(it)); | 446 RespondToClient(std::move(it)); |
| 436 } else { | 447 } else { |
| 437 PerformFullHashCheck(std::move(it), full_hash_to_store_and_hash_prefixes); | 448 PerformFullHashCheck(std::move(it), full_hash_to_store_and_hash_prefixes); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 465 // previously been started and stopped, a task runner could already exist. | 476 // previously been started and stopped, a task runner could already exist. |
| 466 if (!task_runner_) { | 477 if (!task_runner_) { |
| 467 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 478 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 468 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | 479 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 469 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 480 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 470 } | 481 } |
| 471 | 482 |
| 472 // Do not create the database on the IO thread since this may be an expensive | 483 // Do not create the database on the IO thread since this may be an expensive |
| 473 // operation. Instead, do that on the task_runner and when the new database | 484 // operation. Instead, do that on the task_runner and when the new database |
| 474 // has been created, swap it out on the IO thread. | 485 // has been created, swap it out on the IO thread. |
| 475 NewDatabaseReadyCallback db_ready_callback = base::Bind( | 486 NewDatabaseReadyCallback db_ready_callback = |
| 476 &V4LocalDatabaseManager::DatabaseReadyForChecks, base::Unretained(this)); | 487 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForChecks, |
| 488 weak_factory_.GetWeakPtr()); |
| 477 V4Database::Create(task_runner_, base_path_, list_infos_, db_ready_callback); | 489 V4Database::Create(task_runner_, base_path_, list_infos_, db_ready_callback); |
| 478 } | 490 } |
| 479 | 491 |
| 480 void V4LocalDatabaseManager::SetupUpdateProtocolManager( | 492 void V4LocalDatabaseManager::SetupUpdateProtocolManager( |
| 481 net::URLRequestContextGetter* request_context_getter, | 493 net::URLRequestContextGetter* request_context_getter, |
| 482 const V4ProtocolConfig& config) { | 494 const V4ProtocolConfig& config) { |
| 483 V4UpdateCallback callback = base::Bind( | 495 V4UpdateCallback callback = |
| 484 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); | 496 base::Bind(&V4LocalDatabaseManager::UpdateRequestCompleted, |
| 497 weak_factory_.GetWeakPtr()); |
| 485 | 498 |
| 486 v4_update_protocol_manager_ = | 499 v4_update_protocol_manager_ = |
| 487 V4UpdateProtocolManager::Create(request_context_getter, config, callback); | 500 V4UpdateProtocolManager::Create(request_context_getter, config, callback); |
| 488 } | 501 } |
| 489 | 502 |
| 490 void V4LocalDatabaseManager::UpdateRequestCompleted( | 503 void V4LocalDatabaseManager::UpdateRequestCompleted( |
| 491 std::unique_ptr<ParsedServerResponse> parsed_server_response) { | 504 std::unique_ptr<ParsedServerResponse> parsed_server_response) { |
| 492 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 505 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 493 v4_database_->ApplyUpdate(std::move(parsed_server_response), | 506 v4_database_->ApplyUpdate(std::move(parsed_server_response), |
| 494 db_updated_callback_); | 507 db_updated_callback_); |
| 495 } | 508 } |
| 496 | 509 |
| 497 } // namespace safe_browsing | 510 } // namespace safe_browsing |
| OLD | NEW |