| 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 #include "components/safe_browsing_db/v4_local_database_manager.h" | 5 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/callback.h" | |
| 10 #include "components/safe_browsing_db/safebrowsing.pb.h" | 9 #include "components/safe_browsing_db/safebrowsing.pb.h" |
| 11 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 12 | 11 |
| 13 using content::BrowserThread; | 12 using content::BrowserThread; |
| 14 | 13 |
| 15 namespace safe_browsing { | 14 namespace safe_browsing { |
| 16 | 15 |
| 17 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) | 16 V4LocalDatabaseManager::V4LocalDatabaseManager() : enabled_(false) {} |
| 18 : base_path_(base_path), enabled_(false) { | |
| 19 DCHECK(!base_path_.empty()); | |
| 20 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " | |
| 21 << "base_path_: " << base_path_.AsUTF8Unsafe(); | |
| 22 } | |
| 23 | 17 |
| 24 V4LocalDatabaseManager::~V4LocalDatabaseManager() { | 18 V4LocalDatabaseManager::~V4LocalDatabaseManager() { |
| 25 DCHECK(!enabled_); | 19 DCHECK(!enabled_); |
| 26 } | 20 } |
| 27 | 21 |
| 28 bool V4LocalDatabaseManager::IsSupported() const { | 22 bool V4LocalDatabaseManager::IsSupported() const { |
| 29 return true; | 23 return true; |
| 30 } | 24 } |
| 31 | 25 |
| 32 safe_browsing::ThreatSource V4LocalDatabaseManager::GetThreatSource() const { | 26 safe_browsing::ThreatSource V4LocalDatabaseManager::GetThreatSource() const { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 116 |
| 123 bool V4LocalDatabaseManager::IsCsdWhitelistKillSwitchOn() { | 117 bool V4LocalDatabaseManager::IsCsdWhitelistKillSwitchOn() { |
| 124 // TODO(vakh): Implement this skeleton. | 118 // TODO(vakh): Implement this skeleton. |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 126 return true; | 120 return true; |
| 127 } | 121 } |
| 128 | 122 |
| 129 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { | 123 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { |
| 130 // TODO(vakh): Implement this skeleton. | 124 // TODO(vakh): Implement this skeleton. |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 132 if (!enabled_) { | 126 if (!enabled_) |
| 133 return true; | 127 return true; |
| 134 } | |
| 135 | 128 |
| 136 // Don't defer the resource load. | 129 // Don't defer the resource load. |
| 137 return true; | 130 return true; |
| 138 } | 131 } |
| 139 | 132 |
| 140 void V4LocalDatabaseManager::CancelCheck(Client* client) { | 133 void V4LocalDatabaseManager::CancelCheck(Client* client) { |
| 141 // TODO(vakh): Implement this skeleton. | 134 // TODO(vakh): Implement this skeleton. |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 135 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 143 DCHECK(enabled_); | 136 DCHECK(enabled_); |
| 144 } | 137 } |
| 145 | 138 |
| 146 void V4LocalDatabaseManager::StartOnIOThread( | 139 void V4LocalDatabaseManager::StartOnIOThread( |
| 147 net::URLRequestContextGetter* request_context_getter, | 140 net::URLRequestContextGetter* request_context_getter, |
| 148 const V4ProtocolConfig& config) { | 141 const V4ProtocolConfig& config) { |
| 149 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); | 142 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); |
| 150 | 143 |
| 151 SetupUpdateProtocolManager(request_context_getter, config); | 144 #if defined(OS_WIN) || defined (OS_LINUX) || defined (OS_MACOSX) |
| 152 | |
| 153 SetupDatabase(); | |
| 154 | |
| 155 enabled_ = true; | |
| 156 } | |
| 157 | |
| 158 void V4LocalDatabaseManager::SetupUpdateProtocolManager( | |
| 159 net::URLRequestContextGetter* request_context_getter, | |
| 160 const V4ProtocolConfig& config) { | |
| 161 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX) | |
| 162 // TODO(vakh): Remove this if/endif block when the V4Database is implemented. | 145 // TODO(vakh): Remove this if/endif block when the V4Database is implemented. |
| 163 // Filed as http://crbug.com/608075 | 146 // Filed as http://crbug.com/608075 |
| 164 UpdateListIdentifier update_list_identifier; | 147 UpdateListIdentifier update_list_identifier; |
| 165 #if defined(OS_WIN) | 148 #if defined(OS_WIN) |
| 166 update_list_identifier.platform_type = WINDOWS_PLATFORM; | 149 update_list_identifier.platform_type = WINDOWS_PLATFORM; |
| 167 #elif defined(OS_LINUX) | 150 #elif defined (OS_LINUX) |
| 168 update_list_identifier.platform_type = LINUX_PLATFORM; | 151 update_list_identifier.platform_type = LINUX_PLATFORM; |
| 169 #else | 152 #else |
| 170 update_list_identifier.platform_type = OSX_PLATFORM; | 153 update_list_identifier.platform_type = OSX_PLATFORM; |
| 171 #endif | 154 #endif |
| 172 update_list_identifier.threat_entry_type = URL_EXPRESSION; | 155 update_list_identifier.threat_entry_type = URL_EXPRESSION; |
| 173 update_list_identifier.threat_type = MALWARE_THREAT; | 156 update_list_identifier.threat_type = MALWARE_THREAT; |
| 174 current_list_states_[update_list_identifier] = ""; | 157 current_list_states_[update_list_identifier] = ""; |
| 175 #endif | 158 #endif |
| 176 | 159 |
| 177 V4UpdateCallback callback = base::Bind( | 160 V4UpdateCallback callback = base::Bind( |
| 178 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); | 161 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); |
| 179 | |
| 180 v4_update_protocol_manager_ = V4UpdateProtocolManager::Create( | 162 v4_update_protocol_manager_ = V4UpdateProtocolManager::Create( |
| 181 request_context_getter, config, current_list_states_, callback); | 163 request_context_getter, config, current_list_states_, callback); |
| 182 } | |
| 183 | 164 |
| 184 void V4LocalDatabaseManager::SetupDatabase() { | 165 enabled_ = true; |
| 185 DCHECK(!base_path_.empty()); | |
| 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 187 | |
| 188 // Only get a new task runner if there isn't one already. If the service has | |
| 189 // previously been started and stopped, a task runner could already exist. | |
| 190 if (!task_runner_) { | |
| 191 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
| 192 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 193 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 194 } | |
| 195 | |
| 196 // TODO(vakh): list_info_map should probably be a hard-coded map. | |
| 197 ListInfoMap list_info_map; | |
| 198 | |
| 199 // Do not create the database on the IO thread since this may be an expensive | |
| 200 // operation. Instead, do that on the task_runner and when the new database | |
| 201 // has been created, swap it out on the IO thread. | |
| 202 NewDatabaseReadyCallback db_ready_callback = base::Bind( | |
| 203 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); | |
| 204 V4Database::Create(task_runner_, base_path_, list_info_map, | |
| 205 db_ready_callback); | |
| 206 } | |
| 207 | |
| 208 void V4LocalDatabaseManager::DatabaseReady( | |
| 209 std::unique_ptr<V4Database> v4_database) { | |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 211 | |
| 212 // The following check is needed because it is possible that by the time the | |
| 213 // database is ready, StopOnIOThread has been called. | |
| 214 if (enabled_) { | |
| 215 v4_database_ = std::move(v4_database); | |
| 216 | |
| 217 // The database is in place. Start fetching updates now. | |
| 218 v4_update_protocol_manager_->ScheduleNextUpdate(); | |
| 219 } else { | |
| 220 // Schedule the deletion of v4_database off IO thread. | |
| 221 V4Database::Destroy(std::move(v4_database)); | |
| 222 } | |
| 223 } | 166 } |
| 224 | 167 |
| 225 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 168 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
| 226 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 169 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 227 | 170 |
| 228 enabled_ = false; | |
| 229 | |
| 230 // Delete the V4Database. Any pending writes to disk are completed. | |
| 231 // This operation happens on the task_runner on which v4_database_ operates | |
| 232 // and doesn't block the IO thread. | |
| 233 V4Database::Destroy(std::move(v4_database_)); | |
| 234 | |
| 235 // Delete the V4UpdateProtocolManager. | 171 // Delete the V4UpdateProtocolManager. |
| 236 // This cancels any in-flight update request. | 172 // This cancels any in-flight update request. |
| 237 v4_update_protocol_manager_.reset(); | 173 if (v4_update_protocol_manager_.get()) { |
| 174 v4_update_protocol_manager_.reset(); |
| 175 } |
| 238 | 176 |
| 177 enabled_ = false; |
| 239 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); | 178 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); |
| 240 } | 179 } |
| 241 | 180 |
| 242 void V4LocalDatabaseManager::UpdateRequestCompleted( | 181 void V4LocalDatabaseManager::UpdateRequestCompleted( |
| 243 const std::vector<ListUpdateResponse>& responses) { | 182 const std::vector<ListUpdateResponse>& responses) { |
| 244 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 245 | |
| 246 // TODO(vakh): Updates downloaded. Store them on disk and record new state. | 183 // TODO(vakh): Updates downloaded. Store them on disk and record new state. |
| 247 v4_update_protocol_manager_->ScheduleNextUpdate(); | |
| 248 } | 184 } |
| 249 | 185 |
| 250 } // namespace safe_browsing | 186 } // namespace safe_browsing |
| OLD | NEW |