Chromium Code Reviews| 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..4f8a4564446d857b9a2ec2277cde39238fbe7c63 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 @@ using content::BrowserThread; |
| 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::IsCsdWhitelistKillSwitchOn() { |
| bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { |
| // TODO(vakh): Implement this skeleton. |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - if (!enabled_) |
| + if (!enabled_) { |
|
Nathan Parker
2016/05/14 00:43:41
I assume that if v4_database_ == null, we'll queue
vakh (use Gerrit instead)
2016/05/16 22:04:12
Yes, will add that logic later.
|
| return true; |
| + } |
| // Don't defer the resource load. |
| return true; |
| @@ -141,13 +148,23 @@ void V4LocalDatabaseManager::StartOnIOThread( |
| 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,67 @@ void V4LocalDatabaseManager::StartOnIOThread( |
| 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); |
| + |
| + v4_database_ = std::move(v4_database); |
| + |
| + // The database is in place. Start fetching updates now. |
| + v4_update_protocol_manager_->ScheduleNextUpdate(); |
| } |
| void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + enabled_ = false; |
| + |
| + // Delete the V4Database. |
| + // Any pending writes to disk are completed. |
| + v4_database_.reset(); |
| + |
| // Delete the V4UpdateProtocolManager. |
| // This cancels any in-flight update request. |
| - if (v4_update_protocol_manager_.get()) { |
| - v4_update_protocol_manager_.reset(); |
| - } |
| + v4_update_protocol_manager_.reset(); |
| - enabled_ = false; |
| 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 |