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 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 SetupUpdateProtocolManager(request_context_getter, config); | 148 SetupUpdateProtocolManager(request_context_getter, config); |
146 | 149 |
147 SetupDatabase(); | 150 SetupDatabase(); |
148 | 151 |
149 enabled_ = true; | 152 enabled_ = true; |
150 } | 153 } |
151 | 154 |
152 void V4LocalDatabaseManager::SetupUpdateProtocolManager( | 155 void V4LocalDatabaseManager::SetupUpdateProtocolManager( |
153 net::URLRequestContextGetter* request_context_getter, | 156 net::URLRequestContextGetter* request_context_getter, |
154 const V4ProtocolConfig& config) { | 157 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( | 158 V4UpdateCallback callback = base::Bind( |
172 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); | 159 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); |
173 | 160 |
174 v4_update_protocol_manager_ = V4UpdateProtocolManager::Create( | 161 v4_update_protocol_manager_ = |
175 request_context_getter, config, current_list_states_, callback); | 162 V4UpdateProtocolManager::Create(request_context_getter, config, callback); |
176 } | 163 } |
177 | 164 |
178 void V4LocalDatabaseManager::SetupDatabase() { | 165 void V4LocalDatabaseManager::SetupDatabase() { |
179 DCHECK(!base_path_.empty()); | 166 DCHECK(!base_path_.empty()); |
180 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 167 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
181 | 168 |
182 // Only get a new task runner if there isn't one already. If the service has | 169 // 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. | 170 // previously been started and stopped, a task runner could already exist. |
184 if (!task_runner_) { | 171 if (!task_runner_) { |
185 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 172 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
186 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | 173 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
187 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 174 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
188 } | 175 } |
189 | 176 |
190 // TODO(vakh): store_file_name_map should probably be a hard-coded map. | 177 StoreFileNameMap store_file_name_map = CreateStoreFileNameMap(); |
191 StoreFileNameMap store_file_name_map; | |
192 | 178 |
193 // Do not create the database on the IO thread since this may be an expensive | 179 // 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 | 180 // operation. Instead, do that on the task_runner and when the new database |
195 // has been created, swap it out on the IO thread. | 181 // has been created, swap it out on the IO thread. |
| 182 DCHECK(!store_file_name_map.empty()); |
| 183 DatabaseUpdatedCallback db_updated_callback = base::Bind( |
| 184 &V4LocalDatabaseManager::DatabaseUpdated, base::Unretained(this)); |
196 NewDatabaseReadyCallback db_ready_callback = base::Bind( | 185 NewDatabaseReadyCallback db_ready_callback = base::Bind( |
197 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); | 186 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); |
198 V4Database::Create(task_runner_, base_path_, store_file_name_map, | 187 V4Database::Create(task_runner_, base_path_, store_file_name_map, |
199 db_ready_callback); | 188 db_updated_callback, db_ready_callback); |
| 189 } |
| 190 |
| 191 StoreFileNameMap V4LocalDatabaseManager::CreateStoreFileNameMap() { |
| 192 StoreFileNameMap store_file_name_map; |
| 193 // TODO(vakh): Implement this to populate the map appopriately. |
| 194 // Filed as http://crbug.com/608075 |
| 195 #if defined(OS_WIN) |
| 196 PlatformType platform_type = WINDOWS_PLATFORM; |
| 197 #elif defined(OS_LINUX) |
| 198 PlatformType platform_type = LINUX_PLATFORM; |
| 199 #else |
| 200 PlatformType platform_type = OSX_PLATFORM; |
| 201 #endif |
| 202 |
| 203 UpdateListIdentifier malware_id(platform_type, URL, MALWARE_THREAT); |
| 204 store_file_name_map[malware_id] = "os_url_malware.store"; |
| 205 UpdateListIdentifier soceng_id(platform_type, URL, SOCIAL_ENGINEERING_PUBLIC); |
| 206 store_file_name_map[soceng_id] = "os_url_soceng.store"; |
| 207 |
| 208 return store_file_name_map; |
200 } | 209 } |
201 | 210 |
202 void V4LocalDatabaseManager::DatabaseReady( | 211 void V4LocalDatabaseManager::DatabaseReady( |
203 std::unique_ptr<V4Database> v4_database) { | 212 std::unique_ptr<V4Database> v4_database) { |
204 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 213 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
205 | 214 |
206 // The following check is needed because it is possible that by the time the | 215 // The following check is needed because it is possible that by the time the |
207 // database is ready, StopOnIOThread has been called. | 216 // database is ready, StopOnIOThread has been called. |
208 if (enabled_) { | 217 if (enabled_) { |
209 v4_database_ = std::move(v4_database); | 218 v4_database_ = std::move(v4_database); |
210 | 219 |
211 // The database is in place. Start fetching updates now. | 220 // The database is in place. Start fetching updates now. |
212 v4_update_protocol_manager_->ScheduleNextUpdate(); | 221 v4_update_protocol_manager_->ScheduleNextUpdate( |
| 222 v4_database_->GetStoreStateMap()); |
213 } else { | 223 } else { |
214 // Schedule the deletion of v4_database off IO thread. | 224 // Schedule the deletion of v4_database off IO thread. |
215 V4Database::Destroy(std::move(v4_database)); | 225 V4Database::Destroy(std::move(v4_database)); |
216 } | 226 } |
217 } | 227 } |
218 | 228 |
219 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 229 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
220 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 230 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
221 | 231 |
222 enabled_ = false; | 232 enabled_ = false; |
223 | 233 |
224 // Delete the V4Database. Any pending writes to disk are completed. | 234 // Delete the V4Database. Any pending writes to disk are completed. |
225 // This operation happens on the task_runner on which v4_database_ operates | 235 // This operation happens on the task_runner on which v4_database_ operates |
226 // and doesn't block the IO thread. | 236 // and doesn't block the IO thread. |
227 V4Database::Destroy(std::move(v4_database_)); | 237 V4Database::Destroy(std::move(v4_database_)); |
228 | 238 |
229 // Delete the V4UpdateProtocolManager. | 239 // Delete the V4UpdateProtocolManager. |
230 // This cancels any in-flight update request. | 240 // This cancels any in-flight update request. |
231 v4_update_protocol_manager_.reset(); | 241 v4_update_protocol_manager_.reset(); |
232 | 242 |
233 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); | 243 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); |
234 } | 244 } |
235 | 245 |
236 void V4LocalDatabaseManager::UpdateRequestCompleted( | 246 void V4LocalDatabaseManager::UpdateRequestCompleted( |
237 const std::vector<ListUpdateResponse>& responses) { | 247 const std::vector<ListUpdateResponse>& responses) { |
238 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 248 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 249 v4_database_->ApplyUpdate(responses); |
| 250 } |
239 | 251 |
240 // TODO(vakh): Updates downloaded. Store them on disk and record new state. | 252 void V4LocalDatabaseManager::DatabaseUpdated() { |
241 v4_update_protocol_manager_->ScheduleNextUpdate(); | 253 v4_update_protocol_manager_->ScheduleNextUpdate( |
| 254 v4_database_->GetStoreStateMap()); |
242 } | 255 } |
243 | 256 |
244 } // namespace safe_browsing | 257 } // namespace safe_browsing |
OLD | NEW |