| Index: components/safe_browsing_db/v4_local_database_manager.cc
|
| diff --git a/components/safe_browsing_db/v4_local_database_manager.cc b/components/safe_browsing_db/v4_local_database_manager.cc
|
| index 376d26edac729bd59e0727bb0a9588be447cd9e2..06fb383160cf1241e55ac56991c384927e95ff4e 100644
|
| --- a/components/safe_browsing_db/v4_local_database_manager.cc
|
| +++ b/components/safe_browsing_db/v4_local_database_manager.cc
|
| @@ -6,6 +6,7 @@
|
|
|
| #include <vector>
|
|
|
| +#include "base/callback.h"
|
| #include "components/safe_browsing_db/safebrowsing.pb.h"
|
| #include "content/public/browser/browser_thread.h"
|
|
|
| @@ -13,7 +14,12 @@
|
|
|
| namespace safe_browsing {
|
|
|
| -V4LocalDatabaseManager::V4LocalDatabaseManager() : enabled_(false) {}
|
| +V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path)
|
| + : base_path_(base_path), enabled_(false) {
|
| + DCHECK(!base_path_.empty());
|
| + DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: "
|
| + << "base_path_: " << base_path_.AsUTF8Unsafe();
|
| +}
|
|
|
| V4LocalDatabaseManager::~V4LocalDatabaseManager() {
|
| DCHECK(!enabled_);
|
| @@ -123,8 +129,9 @@
|
| bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) {
|
| // TODO(vakh): Implement this skeleton.
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| - if (!enabled_)
|
| + if (!enabled_) {
|
| return true;
|
| + }
|
|
|
| // Don't defer the resource load.
|
| return true;
|
| @@ -141,13 +148,23 @@
|
| const V4ProtocolConfig& config) {
|
| SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config);
|
|
|
| -#if defined(OS_WIN) || defined (OS_LINUX) || defined (OS_MACOSX)
|
| + SetupUpdateProtocolManager(request_context_getter, config);
|
| +
|
| + SetupDatabase();
|
| +
|
| + enabled_ = true;
|
| +}
|
| +
|
| +void V4LocalDatabaseManager::SetupUpdateProtocolManager(
|
| + net::URLRequestContextGetter* request_context_getter,
|
| + const V4ProtocolConfig& config) {
|
| +#if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX)
|
| // TODO(vakh): Remove this if/endif block when the V4Database is implemented.
|
| // Filed as http://crbug.com/608075
|
| UpdateListIdentifier update_list_identifier;
|
| #if defined(OS_WIN)
|
| update_list_identifier.platform_type = WINDOWS_PLATFORM;
|
| -#elif defined (OS_LINUX)
|
| +#elif defined(OS_LINUX)
|
| update_list_identifier.platform_type = LINUX_PLATFORM;
|
| #else
|
| update_list_identifier.platform_type = OSX_PLATFORM;
|
| @@ -159,28 +176,75 @@
|
|
|
| V4UpdateCallback callback = base::Bind(
|
| &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this));
|
| +
|
| v4_update_protocol_manager_ = V4UpdateProtocolManager::Create(
|
| request_context_getter, config, current_list_states_, callback);
|
| -
|
| - enabled_ = true;
|
| +}
|
| +
|
| +void V4LocalDatabaseManager::SetupDatabase() {
|
| + DCHECK(!base_path_.empty());
|
| + DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| +
|
| + // Only get a new task runner if there isn't one already. If the service has
|
| + // previously been started and stopped, a task runner could already exist.
|
| + if (!task_runner_) {
|
| + base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
|
| + task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior(
|
| + pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
|
| + }
|
| +
|
| + // TODO(vakh): list_info_map should probably be a hard-coded map.
|
| + ListInfoMap list_info_map;
|
| +
|
| + // Do not create the database on the IO thread since this may be an expensive
|
| + // operation. Instead, do that on the task_runner and when the new database
|
| + // has been created, swap it out on the IO thread.
|
| + NewDatabaseReadyCallback db_ready_callback = base::Bind(
|
| + &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this));
|
| + V4Database::Create(task_runner_, base_path_, list_info_map,
|
| + db_ready_callback);
|
| +}
|
| +
|
| +void V4LocalDatabaseManager::DatabaseReady(
|
| + std::unique_ptr<V4Database> v4_database) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| +
|
| + // The following check is needed because it is possible that by the time the
|
| + // database is ready, StopOnIOThread has been called.
|
| + if (enabled_) {
|
| + v4_database_ = std::move(v4_database);
|
| +
|
| + // The database is in place. Start fetching updates now.
|
| + v4_update_protocol_manager_->ScheduleNextUpdate();
|
| + } else {
|
| + // Schedule the deletion of v4_database off IO thread.
|
| + V4Database::Destroy(std::move(v4_database));
|
| + }
|
| }
|
|
|
| void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| +
|
| + enabled_ = false;
|
| +
|
| + // Delete the V4Database. Any pending writes to disk are completed.
|
| + // This operation happens on the task_runner on which v4_database_ operates
|
| + // and doesn't block the IO thread.
|
| + V4Database::Destroy(std::move(v4_database_));
|
|
|
| // Delete the V4UpdateProtocolManager.
|
| // This cancels any in-flight update request.
|
| - if (v4_update_protocol_manager_.get()) {
|
| - v4_update_protocol_manager_.reset();
|
| - }
|
| -
|
| - enabled_ = false;
|
| + v4_update_protocol_manager_.reset();
|
| +
|
| SafeBrowsingDatabaseManager::StopOnIOThread(shutdown);
|
| }
|
|
|
| void V4LocalDatabaseManager::UpdateRequestCompleted(
|
| const std::vector<ListUpdateResponse>& responses) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| +
|
| // TODO(vakh): Updates downloaded. Store them on disk and record new state.
|
| + v4_update_protocol_manager_->ScheduleNextUpdate();
|
| }
|
|
|
| } // namespace safe_browsing
|
|
|