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. | |
| 6 // TODO(vakh): Fix that: http://crbug.com/621647 | |
| 7 | |
| 5 #include "components/safe_browsing_db/v4_local_database_manager.h" | 8 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 6 | 9 |
| 7 #include <vector> | 10 #include <vector> |
| 8 | 11 |
| 9 #include "base/callback.h" | 12 #include "base/callback.h" |
| 10 #include "components/safe_browsing_db/safebrowsing.pb.h" | 13 #include "components/safe_browsing_db/safebrowsing.pb.h" |
| 11 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 12 | 15 |
| 13 using content::BrowserThread; | 16 using content::BrowserThread; |
| 14 | 17 |
| 15 namespace safe_browsing { | 18 namespace safe_browsing { |
| 16 | 19 |
| 20 namespace { | |
| 21 #if defined(OS_WIN) | |
| 22 #define PLATFORM_TYPE WINDOWS_PLATFORM | |
| 23 #elif defined(OS_LINUX) | |
| 24 #define PLATFORM_TYPE LINUX_PLATFORM | |
| 25 #elif defined(OS_MACOSX) | |
| 26 #define PLATFORM_TYPE OSX_PLATFORM | |
| 27 #else | |
| 28 // This should ideally never compile but it is getting compiled on Android. | |
| 29 // See: https://bugs.chromium.org/p/chromium/issues/detail?id=621647 | |
| 30 // TODO(vakh): Once that bug is fixed, this should be removed. If we leave | |
| 31 // the platform_type empty, the server won't recognize the request and | |
| 32 // return an error response which will pollute our UMA metrics. | |
| 33 #define PLATFORM_TYPE LINUX_PLATFORM | |
| 34 #endif | |
| 35 | |
| 36 // TODO(vakh): Implement this to populate the map appopriately. | |
| 37 // Filed as http://crbug.com/608075 | |
| 38 StoreFileNameMap store_file_name_map{ | |
| 39 {UpdateListIdentifier(PLATFORM_TYPE, URL, MALWARE_THREAT), | |
| 40 "UrlMalware.store"}, | |
| 41 {UpdateListIdentifier(PLATFORM_TYPE, URL, SOCIAL_ENGINEERING_PUBLIC), | |
| 42 "UrlSoceng.store"}}; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 17 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) | 46 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) |
| 18 : base_path_(base_path), enabled_(false) { | 47 : base_path_(base_path), enabled_(false) { |
| 19 DCHECK(!base_path_.empty()); | 48 DCHECK(!base_path_.empty()); |
| 20 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " | 49 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " |
| 21 << "base_path_: " << base_path_.AsUTF8Unsafe(); | 50 << "base_path_: " << base_path_.AsUTF8Unsafe(); |
| 22 } | 51 } |
| 23 | 52 |
| 24 V4LocalDatabaseManager::~V4LocalDatabaseManager() { | 53 V4LocalDatabaseManager::~V4LocalDatabaseManager() { |
| 25 DCHECK(!enabled_); | 54 DCHECK(!enabled_); |
| 26 } | 55 } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 // TODO(vakh): Implement this skeleton. | 164 // TODO(vakh): Implement this skeleton. |
| 136 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 137 DCHECK(enabled_); | 166 DCHECK(enabled_); |
| 138 } | 167 } |
| 139 | 168 |
| 140 void V4LocalDatabaseManager::StartOnIOThread( | 169 void V4LocalDatabaseManager::StartOnIOThread( |
| 141 net::URLRequestContextGetter* request_context_getter, | 170 net::URLRequestContextGetter* request_context_getter, |
| 142 const V4ProtocolConfig& config) { | 171 const V4ProtocolConfig& config) { |
| 143 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); | 172 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); |
| 144 | 173 |
| 174 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated, | |
| 175 base::Unretained(this)); | |
| 176 | |
| 145 SetupUpdateProtocolManager(request_context_getter, config); | 177 SetupUpdateProtocolManager(request_context_getter, config); |
| 146 | 178 |
| 147 SetupDatabase(); | 179 SetupDatabase(); |
| 148 | 180 |
| 149 enabled_ = true; | 181 enabled_ = true; |
| 150 } | 182 } |
| 151 | 183 |
| 152 void V4LocalDatabaseManager::SetupUpdateProtocolManager( | 184 void V4LocalDatabaseManager::SetupUpdateProtocolManager( |
| 153 net::URLRequestContextGetter* request_context_getter, | 185 net::URLRequestContextGetter* request_context_getter, |
| 154 const V4ProtocolConfig& config) { | 186 const V4ProtocolConfig& config) { |
| 155 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) | |
| 156 // TODO(vakh): Remove this if/endif block when the V4Database is implemented. | |
| 157 // Filed as http://crbug.com/608075 | |
| 158 UpdateListIdentifier update_list_identifier; | |
| 159 #if defined(OS_WIN) | |
| 160 update_list_identifier.platform_type = WINDOWS_PLATFORM; | |
| 161 #elif defined(OS_LINUX) | |
| 162 update_list_identifier.platform_type = LINUX_PLATFORM; | |
| 163 #else | |
| 164 update_list_identifier.platform_type = OSX_PLATFORM; | |
| 165 #endif | |
| 166 update_list_identifier.threat_entry_type = URL; | |
| 167 update_list_identifier.threat_type = MALWARE_THREAT; | |
| 168 current_list_states_[update_list_identifier] = ""; | |
| 169 #endif | |
| 170 | |
| 171 V4UpdateCallback callback = base::Bind( | 187 V4UpdateCallback callback = base::Bind( |
| 172 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); | 188 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); |
| 173 | 189 |
| 174 v4_update_protocol_manager_ = V4UpdateProtocolManager::Create( | 190 v4_update_protocol_manager_ = |
| 175 request_context_getter, config, current_list_states_, callback); | 191 V4UpdateProtocolManager::Create(request_context_getter, config, callback); |
| 176 } | 192 } |
| 177 | 193 |
| 178 void V4LocalDatabaseManager::SetupDatabase() { | 194 void V4LocalDatabaseManager::SetupDatabase() { |
| 179 DCHECK(!base_path_.empty()); | 195 DCHECK(!base_path_.empty()); |
| 180 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 196 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 181 | 197 |
| 182 // Only get a new task runner if there isn't one already. If the service has | 198 // Only get a new task runner if there isn't one already. If the service has |
| 183 // previously been started and stopped, a task runner could already exist. | 199 // previously been started and stopped, a task runner could already exist. |
| 184 if (!task_runner_) { | 200 if (!task_runner_) { |
| 185 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 201 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 186 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | 202 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 187 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 203 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 188 } | 204 } |
| 189 | 205 |
| 190 // TODO(vakh): store_file_name_map should probably be a hard-coded map. | |
| 191 StoreFileNameMap store_file_name_map; | |
| 192 | |
| 193 // Do not create the database on the IO thread since this may be an expensive | 206 // Do not create the database on the IO thread since this may be an expensive |
| 194 // operation. Instead, do that on the task_runner and when the new database | 207 // operation. Instead, do that on the task_runner and when the new database |
| 195 // has been created, swap it out on the IO thread. | 208 // has been created, swap it out on the IO thread. |
| 209 DCHECK(!store_file_name_map.empty()); | |
| 196 NewDatabaseReadyCallback db_ready_callback = base::Bind( | 210 NewDatabaseReadyCallback db_ready_callback = base::Bind( |
| 197 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); | 211 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); |
| 198 V4Database::Create(task_runner_, base_path_, store_file_name_map, | 212 V4Database::Create(task_runner_, base_path_, store_file_name_map, |
| 199 db_ready_callback); | 213 db_ready_callback); |
| 200 } | 214 } |
| 201 | 215 |
| 202 void V4LocalDatabaseManager::DatabaseReady( | 216 void V4LocalDatabaseManager::DatabaseReady( |
| 203 std::unique_ptr<V4Database> v4_database) { | 217 std::unique_ptr<V4Database> v4_database) { |
| 204 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 218 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 205 | 219 |
| 206 // The following check is needed because it is possible that by the time the | 220 // The following check is needed because it is possible that by the time the |
| 207 // database is ready, StopOnIOThread has been called. | 221 // database is ready, StopOnIOThread has been called. |
| 208 if (enabled_) { | 222 if (enabled_) { |
| 209 v4_database_ = std::move(v4_database); | 223 v4_database_ = std::move(v4_database); |
| 210 | 224 |
| 211 // The database is in place. Start fetching updates now. | 225 // The database is in place. Start fetching updates now. |
| 212 v4_update_protocol_manager_->ScheduleNextUpdate(); | 226 v4_update_protocol_manager_->ScheduleNextUpdate( |
| 227 v4_database_->GetStoreStateMap()); | |
| 213 } else { | 228 } else { |
| 214 // Schedule the deletion of v4_database off IO thread. | 229 // Schedule the deletion of v4_database off IO thread. |
| 215 V4Database::Destroy(std::move(v4_database)); | 230 V4Database::Destroy(std::move(v4_database)); |
| 216 } | 231 } |
| 217 } | 232 } |
| 218 | 233 |
| 219 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 234 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
| 220 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 235 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 221 | 236 |
| 222 enabled_ = false; | 237 enabled_ = false; |
| 223 | 238 |
| 224 // Delete the V4Database. Any pending writes to disk are completed. | 239 // Delete the V4Database. Any pending writes to disk are completed. |
| 225 // This operation happens on the task_runner on which v4_database_ operates | 240 // This operation happens on the task_runner on which v4_database_ operates |
| 226 // and doesn't block the IO thread. | 241 // and doesn't block the IO thread. |
| 227 V4Database::Destroy(std::move(v4_database_)); | 242 V4Database::Destroy(std::move(v4_database_)); |
| 228 | 243 |
| 229 // Delete the V4UpdateProtocolManager. | 244 // Delete the V4UpdateProtocolManager. |
| 230 // This cancels any in-flight update request. | 245 // This cancels any in-flight update request. |
| 231 v4_update_protocol_manager_.reset(); | 246 v4_update_protocol_manager_.reset(); |
| 232 | 247 |
| 248 db_updated_callback_.Reset(); | |
| 249 | |
| 233 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); | 250 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); |
| 234 } | 251 } |
| 235 | 252 |
| 236 void V4LocalDatabaseManager::UpdateRequestCompleted( | 253 void V4LocalDatabaseManager::UpdateRequestCompleted( |
| 237 const std::vector<ListUpdateResponse>& responses) { | 254 const std::vector<ListUpdateResponse>& responses) { |
| 238 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 255 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 256 v4_database_->ApplyUpdate(responses, db_updated_callback_); | |
|
Scott Hess - ex-Googler
2016/06/28 18:18:06
Is there any possibility of receiving this call af
vakh (use Gerrit instead)
2016/06/28 19:02:03
That's my understanding too with the use of unique
| |
| 257 } | |
| 239 | 258 |
| 240 // TODO(vakh): Updates downloaded. Store them on disk and record new state. | 259 void V4LocalDatabaseManager::DatabaseUpdated() { |
| 241 v4_update_protocol_manager_->ScheduleNextUpdate(); | 260 v4_update_protocol_manager_->ScheduleNextUpdate( |
| 261 v4_database_->GetStoreStateMap()); | |
| 242 } | 262 } |
| 243 | 263 |
| 244 } // namespace safe_browsing | 264 } // namespace safe_browsing |
| OLD | NEW |