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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 scoped_refptr<V4LocalDatabaseManager> V4LocalDatabaseManager::Create( | 72 scoped_refptr<V4LocalDatabaseManager> V4LocalDatabaseManager::Create( |
| 73 const base::FilePath& base_path) { | 73 const base::FilePath& base_path) { |
| 74 if (!V4FeatureList::IsLocalDatabaseManagerEnabled()) { | 74 if (!V4FeatureList::IsLocalDatabaseManagerEnabled()) { |
| 75 return nullptr; | 75 return nullptr; |
| 76 } | 76 } |
| 77 | 77 |
| 78 return make_scoped_refptr(new V4LocalDatabaseManager(base_path)); | 78 return make_scoped_refptr(new V4LocalDatabaseManager(base_path)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) | 81 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) |
| 82 : base_path_(base_path), enabled_(false), list_infos_(GetListInfos()) { | 82 : base_path_(base_path), |
| 83 enabled_(false), | |
| 84 list_infos_(GetListInfos()), | |
| 85 weak_factory_(this) { | |
| 83 DCHECK(!base_path_.empty()); | 86 DCHECK(!base_path_.empty()); |
| 84 DCHECK(!list_infos_.empty()); | 87 DCHECK(!list_infos_.empty()); |
| 85 | 88 |
| 86 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " | 89 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " |
| 87 << "base_path_: " << base_path_.AsUTF8Unsafe(); | 90 << "base_path_: " << base_path_.AsUTF8Unsafe(); |
| 88 } | 91 } |
| 89 | 92 |
| 90 V4LocalDatabaseManager::~V4LocalDatabaseManager() { | 93 V4LocalDatabaseManager::~V4LocalDatabaseManager() { |
| 91 DCHECK(!enabled_); | 94 DCHECK(!enabled_); |
| 92 } | 95 } |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 bool V4LocalDatabaseManager::IsSupported() const { | 239 bool V4LocalDatabaseManager::IsSupported() const { |
| 237 return true; | 240 return true; |
| 238 } | 241 } |
| 239 | 242 |
| 240 void V4LocalDatabaseManager::StartOnIOThread( | 243 void V4LocalDatabaseManager::StartOnIOThread( |
| 241 net::URLRequestContextGetter* request_context_getter, | 244 net::URLRequestContextGetter* request_context_getter, |
| 242 const V4ProtocolConfig& config) { | 245 const V4ProtocolConfig& config) { |
| 243 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); | 246 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); |
| 244 | 247 |
| 245 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated, | 248 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated, |
| 246 base::Unretained(this)); | 249 weak_factory_.GetWeakPtr()); |
| 247 | 250 |
| 248 SetupUpdateProtocolManager(request_context_getter, config); | 251 SetupUpdateProtocolManager(request_context_getter, config); |
| 249 SetupDatabase(); | 252 SetupDatabase(); |
| 250 | 253 |
| 251 enabled_ = true; | 254 enabled_ = true; |
| 252 } | 255 } |
| 253 | 256 |
| 254 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 257 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
| 255 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 258 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 256 | 259 |
| 257 enabled_ = false; | 260 enabled_ = false; |
| 258 | 261 |
| 259 pending_clients_.clear(); | 262 pending_clients_.clear(); |
| 260 | 263 |
| 261 RespondSafeToQueuedChecks(); | 264 RespondSafeToQueuedChecks(); |
| 262 | 265 |
| 263 // Delete the V4Database. Any pending writes to disk are completed. | 266 // Delete the V4Database. Any pending writes to disk are completed. |
| 264 // This operation happens on the task_runner on which v4_database_ operates | 267 // This operation happens on the task_runner on which v4_database_ operates |
| 265 // and doesn't block the IO thread. | 268 // and doesn't block the IO thread. |
| 266 V4Database::Destroy(std::move(v4_database_)); | 269 V4Database::Destroy(std::move(v4_database_)); |
| 267 | 270 |
| 268 // Delete the V4UpdateProtocolManager. | 271 // Delete the V4UpdateProtocolManager. |
| 269 // This cancels any in-flight update request. | 272 // This cancels any in-flight update request. |
| 270 v4_update_protocol_manager_.reset(); | 273 v4_update_protocol_manager_.reset(); |
|
Nathan Parker
2016/10/18 05:06:37
Does this mean that any calls to the protocol_mana
vakh (use Gerrit instead)
2016/10/18 17:37:44
Not sure exactly what you mean but all callbacks i
| |
| 271 | 274 |
| 272 db_updated_callback_.Reset(); | 275 db_updated_callback_.Reset(); |
| 273 | 276 |
| 274 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); | 277 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); |
| 275 } | 278 } |
| 276 | 279 |
| 277 // | 280 // |
| 278 // End: SafeBrowsingDatabaseManager implementation | 281 // End: SafeBrowsingDatabaseManager implementation |
| 279 // | 282 // |
| 280 | 283 |
| 281 void V4LocalDatabaseManager::DatabaseReadyForChecks( | 284 void V4LocalDatabaseManager::DatabaseReadyForChecks( |
| 282 std::unique_ptr<V4Database> v4_database) { | 285 std::unique_ptr<V4Database> v4_database) { |
| 283 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 286 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 284 | 287 |
| 285 // The following check is needed because it is possible that by the time the | 288 // The following check is needed because it is possible that by the time the |
| 286 // database is ready, StopOnIOThread has been called. | 289 // database is ready, StopOnIOThread has been called. |
| 287 if (enabled_) { | 290 if (enabled_) { |
| 288 v4_database_ = std::move(v4_database); | 291 v4_database_ = std::move(v4_database); |
| 289 | 292 |
| 290 // The consistency of the stores read from the disk needs to verified. Post | 293 // The consistency of the stores read from the disk needs to verified. Post |
| 291 // that task on the task runner. It calls |DatabaseReadyForUpdates| | 294 // that task on the task runner. It calls |DatabaseReadyForUpdates| |
| 292 // callback with the stores to reset, if any, and then we can schedule the | 295 // callback with the stores to reset, if any, and then we can schedule the |
| 293 // database updates. | 296 // database updates. |
| 294 v4_database_->VerifyChecksum( | 297 v4_database_->VerifyChecksum( |
| 295 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForUpdates, | 298 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForUpdates, |
| 296 base::Unretained(this))); | 299 weak_factory_.GetWeakPtr())); |
| 297 | 300 |
| 298 ProcessQueuedChecks(); | 301 ProcessQueuedChecks(); |
| 299 } else { | 302 } else { |
| 300 // Schedule the deletion of v4_database off IO thread. | 303 // Schedule the deletion of v4_database off IO thread. |
| 301 V4Database::Destroy(std::move(v4_database)); | 304 V4Database::Destroy(std::move(v4_database)); |
| 302 } | 305 } |
| 303 } | 306 } |
| 304 | 307 |
| 305 void V4LocalDatabaseManager::DatabaseReadyForUpdates( | 308 void V4LocalDatabaseManager::DatabaseReadyForUpdates( |
| 306 const std::vector<ListIdentifier>& stores_to_reset) { | 309 const std::vector<ListIdentifier>& stores_to_reset) { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 421 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 424 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 422 | 425 |
| 423 DCHECK(enabled_); | 426 DCHECK(enabled_); |
| 424 DCHECK(!full_hash_to_store_and_hash_prefixes.empty()); | 427 DCHECK(!full_hash_to_store_and_hash_prefixes.empty()); |
| 425 | 428 |
| 426 pending_clients_.insert(check->client); | 429 pending_clients_.insert(check->client); |
| 427 | 430 |
| 428 v4_get_hash_protocol_manager_->GetFullHashes( | 431 v4_get_hash_protocol_manager_->GetFullHashes( |
| 429 full_hash_to_store_and_hash_prefixes, | 432 full_hash_to_store_and_hash_prefixes, |
| 430 base::Bind(&V4LocalDatabaseManager::OnFullHashResponse, | 433 base::Bind(&V4LocalDatabaseManager::OnFullHashResponse, |
| 431 base::Unretained(this), base::Passed(std::move(check)))); | 434 weak_factory_.GetWeakPtr(), base::Passed(std::move(check)))); |
| 432 } | 435 } |
| 433 | 436 |
| 434 void V4LocalDatabaseManager::ProcessQueuedChecks() { | 437 void V4LocalDatabaseManager::ProcessQueuedChecks() { |
| 435 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 438 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 436 for (auto& it : queued_checks_) { | 439 for (auto& it : queued_checks_) { |
| 437 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; | 440 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; |
| 438 if (!GetPrefixMatches(it, &full_hash_to_store_and_hash_prefixes)) { | 441 if (!GetPrefixMatches(it, &full_hash_to_store_and_hash_prefixes)) { |
| 439 RespondToClient(std::move(it)); | 442 RespondToClient(std::move(it)); |
| 440 } else { | 443 } else { |
| 441 PerformFullHashCheck(std::move(it), full_hash_to_store_and_hash_prefixes); | 444 PerformFullHashCheck(std::move(it), full_hash_to_store_and_hash_prefixes); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 469 // previously been started and stopped, a task runner could already exist. | 472 // previously been started and stopped, a task runner could already exist. |
| 470 if (!task_runner_) { | 473 if (!task_runner_) { |
| 471 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 474 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 472 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | 475 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 473 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 476 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 474 } | 477 } |
| 475 | 478 |
| 476 // Do not create the database on the IO thread since this may be an expensive | 479 // Do not create the database on the IO thread since this may be an expensive |
| 477 // operation. Instead, do that on the task_runner and when the new database | 480 // operation. Instead, do that on the task_runner and when the new database |
| 478 // has been created, swap it out on the IO thread. | 481 // has been created, swap it out on the IO thread. |
| 479 NewDatabaseReadyCallback db_ready_callback = base::Bind( | 482 NewDatabaseReadyCallback db_ready_callback = |
| 480 &V4LocalDatabaseManager::DatabaseReadyForChecks, base::Unretained(this)); | 483 base::Bind(&V4LocalDatabaseManager::DatabaseReadyForChecks, |
| 484 weak_factory_.GetWeakPtr()); | |
| 481 V4Database::Create(task_runner_, base_path_, list_infos_, db_ready_callback); | 485 V4Database::Create(task_runner_, base_path_, list_infos_, db_ready_callback); |
| 482 } | 486 } |
| 483 | 487 |
| 484 void V4LocalDatabaseManager::SetupUpdateProtocolManager( | 488 void V4LocalDatabaseManager::SetupUpdateProtocolManager( |
| 485 net::URLRequestContextGetter* request_context_getter, | 489 net::URLRequestContextGetter* request_context_getter, |
| 486 const V4ProtocolConfig& config) { | 490 const V4ProtocolConfig& config) { |
| 487 V4UpdateCallback callback = base::Bind( | 491 V4UpdateCallback callback = |
| 488 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); | 492 base::Bind(&V4LocalDatabaseManager::UpdateRequestCompleted, |
| 493 weak_factory_.GetWeakPtr()); | |
| 489 | 494 |
| 490 v4_update_protocol_manager_ = | 495 v4_update_protocol_manager_ = |
| 491 V4UpdateProtocolManager::Create(request_context_getter, config, callback); | 496 V4UpdateProtocolManager::Create(request_context_getter, config, callback); |
| 492 } | 497 } |
| 493 | 498 |
| 494 void V4LocalDatabaseManager::UpdateRequestCompleted( | 499 void V4LocalDatabaseManager::UpdateRequestCompleted( |
| 495 std::unique_ptr<ParsedServerResponse> parsed_server_response) { | 500 std::unique_ptr<ParsedServerResponse> parsed_server_response) { |
| 496 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 501 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 497 v4_database_->ApplyUpdate(std::move(parsed_server_response), | 502 v4_database_->ApplyUpdate(std::move(parsed_server_response), |
| 498 db_updated_callback_); | 503 db_updated_callback_); |
| 499 } | 504 } |
| 500 | 505 |
| 501 } // namespace safe_browsing | 506 } // namespace safe_browsing |
| OLD | NEW |