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" | 9 #include "base/callback.h" |
10 #include "components/safe_browsing_db/safebrowsing.pb.h" | 10 #include "components/safe_browsing_db/safebrowsing.pb.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 SetupUpdateProtocolManager(request_context_getter, config); | 145 SetupUpdateProtocolManager(request_context_getter, config); |
146 | 146 |
147 SetupDatabase(); | 147 SetupDatabase(); |
148 | 148 |
149 enabled_ = true; | 149 enabled_ = true; |
150 } | 150 } |
151 | 151 |
152 void V4LocalDatabaseManager::SetupUpdateProtocolManager( | 152 void V4LocalDatabaseManager::SetupUpdateProtocolManager( |
153 net::URLRequestContextGetter* request_context_getter, | 153 net::URLRequestContextGetter* request_context_getter, |
154 const V4ProtocolConfig& config) { | 154 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( | 155 V4UpdateCallback callback = base::Bind( |
172 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); | 156 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); |
173 | 157 |
174 v4_update_protocol_manager_ = V4UpdateProtocolManager::Create( | 158 v4_update_protocol_manager_ = |
175 request_context_getter, config, current_list_states_, callback); | 159 V4UpdateProtocolManager::Create(request_context_getter, config, callback); |
176 } | 160 } |
177 | 161 |
178 void V4LocalDatabaseManager::SetupDatabase() { | 162 void V4LocalDatabaseManager::SetupDatabase() { |
179 DCHECK(!base_path_.empty()); | 163 DCHECK(!base_path_.empty()); |
180 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 164 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
181 | 165 |
182 // Only get a new task runner if there isn't one already. If the service has | 166 // 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. | 167 // previously been started and stopped, a task runner could already exist. |
184 if (!task_runner_) { | 168 if (!task_runner_) { |
185 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | 169 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
186 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | 170 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
187 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 171 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
188 } | 172 } |
189 | 173 |
190 // TODO(vakh): list_info_map should probably be a hard-coded map. | 174 StoreFileNameMap store_file_name_map; |
191 ListInfoMap list_info_map; | 175 PopulateStoreFileNameMap(&store_file_name_map); |
Scott Hess - ex-Googler
2016/06/17 22:53:43
Could PopulateStoreFileNameMap() just return a map
vakh (use Gerrit instead)
2016/06/20 22:28:42
Done.
| |
192 | 176 |
193 // Do not create the database on the IO thread since this may be an expensive | 177 // 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 | 178 // operation. Instead, do that on the task_runner and when the new database |
195 // has been created, swap it out on the IO thread. | 179 // has been created, swap it out on the IO thread. |
180 DCHECK(!store_file_name_map.empty()); | |
181 DatabaseUpdatedCallback db_updated_callback = base::Bind( | |
182 &V4LocalDatabaseManager::DatabaseUpdated, base::Unretained(this)); | |
196 NewDatabaseReadyCallback db_ready_callback = base::Bind( | 183 NewDatabaseReadyCallback db_ready_callback = base::Bind( |
197 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); | 184 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); |
198 V4Database::Create(task_runner_, base_path_, list_info_map, | 185 V4Database::Create(task_runner_, base_path_, store_file_name_map, |
199 db_ready_callback); | 186 db_updated_callback, db_ready_callback); |
187 } | |
188 | |
189 void V4LocalDatabaseManager::PopulateStoreFileNameMap( | |
190 StoreFileNameMap* store_file_name_map) { | |
191 // TODO(vakh): Implement this to populate the map appopriately. | |
192 // Filed as http://crbug.com/608075 | |
193 #if defined(OS_WIN) | |
194 PlatformType platform_type = WINDOWS_PLATFORM; | |
195 #elif defined(OS_LINUX) | |
196 PlatformType platform_type = LINUX_PLATFORM; | |
197 #else | |
Scott Hess - ex-Googler
2016/06/17 22:53:43
I think there should be no default,
#elif defined(
vakh (use Gerrit instead)
2016/06/20 22:28:42
This code is getting compiled on Android for an un
Scott Hess - ex-Googler
2016/06/21 21:03:44
Suggest having an explicit case for OS_OSX, plus a
vakh (use Gerrit instead)
2016/06/21 23:19:35
Done.
| |
198 PlatformType platform_type = OSX_PLATFORM; | |
199 #endif | |
200 // This is designed to fail compilation for other platforms because | |
201 // |platform_type| won't be declared. | |
Scott Hess - ex-Googler
2016/06/17 22:53:43
I vote for no comment. I think anyone debugging w
vakh (use Gerrit instead)
2016/06/20 22:28:42
Done.
| |
202 UpdateListIdentifier update_list_identifier(platform_type, URL, | |
203 MALWARE_THREAT); | |
204 (*store_file_name_map)[update_list_identifier] = "os_url_malware.store"; | |
205 | |
206 update_list_identifier.threat_type = SOCIAL_ENGINEERING_PUBLIC; | |
Scott Hess - ex-Googler
2016/06/17 22:53:43
I am not entirely comfortable with this, because i
vakh (use Gerrit instead)
2016/06/20 22:28:42
Done.
| |
207 (*store_file_name_map)[update_list_identifier] = "os_url_soceng.store"; | |
200 } | 208 } |
201 | 209 |
202 void V4LocalDatabaseManager::DatabaseReady( | 210 void V4LocalDatabaseManager::DatabaseReady( |
203 std::unique_ptr<V4Database> v4_database) { | 211 std::unique_ptr<V4Database> v4_database) { |
204 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 212 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
205 | 213 |
206 // The following check is needed because it is possible that by the time the | 214 // The following check is needed because it is possible that by the time the |
207 // database is ready, StopOnIOThread has been called. | 215 // database is ready, StopOnIOThread has been called. |
208 if (enabled_) { | 216 if (enabled_) { |
209 v4_database_ = std::move(v4_database); | 217 v4_database_ = std::move(v4_database); |
210 | 218 |
219 v4_update_protocol_manager_->SetStoreStateMap( | |
220 v4_database_->store_state_map()); | |
221 | |
211 // The database is in place. Start fetching updates now. | 222 // The database is in place. Start fetching updates now. |
212 v4_update_protocol_manager_->ScheduleNextUpdate(); | 223 v4_update_protocol_manager_->ScheduleNextUpdate(); |
213 } else { | 224 } else { |
214 // Schedule the deletion of v4_database off IO thread. | 225 // Schedule the deletion of v4_database off IO thread. |
215 V4Database::Destroy(std::move(v4_database)); | 226 V4Database::Destroy(std::move(v4_database)); |
216 } | 227 } |
217 } | 228 } |
218 | 229 |
219 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { | 230 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { |
220 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 231 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
221 | 232 |
222 enabled_ = false; | 233 enabled_ = false; |
223 | 234 |
224 // Delete the V4Database. Any pending writes to disk are completed. | 235 // Delete the V4Database. Any pending writes to disk are completed. |
225 // This operation happens on the task_runner on which v4_database_ operates | 236 // This operation happens on the task_runner on which v4_database_ operates |
226 // and doesn't block the IO thread. | 237 // and doesn't block the IO thread. |
227 V4Database::Destroy(std::move(v4_database_)); | 238 V4Database::Destroy(std::move(v4_database_)); |
228 | 239 |
229 // Delete the V4UpdateProtocolManager. | 240 // Delete the V4UpdateProtocolManager. |
230 // This cancels any in-flight update request. | 241 // This cancels any in-flight update request. |
231 v4_update_protocol_manager_.reset(); | 242 v4_update_protocol_manager_.reset(); |
232 | 243 |
233 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); | 244 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); |
234 } | 245 } |
235 | 246 |
236 void V4LocalDatabaseManager::UpdateRequestCompleted( | 247 void V4LocalDatabaseManager::UpdateRequestCompleted( |
237 const std::vector<ListUpdateResponse>& responses) { | 248 const std::vector<ListUpdateResponse>& responses) { |
238 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 249 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
250 v4_database_->ApplyUpdate(responses); | |
Scott Hess - ex-Googler
2016/06/17 22:53:43
WRT my comment elsewhere with many store tasks ver
vakh (use Gerrit instead)
2016/06/20 22:28:43
Still not sure about updating all stores at once,
Scott Hess - ex-Googler
2016/06/21 21:03:44
It's more efficient, probably. But a call like th
vakh (use Gerrit instead)
2016/06/21 23:19:35
The other callbacks in this class also work the sa
Scott Hess - ex-Googler
2016/06/24 23:01:19
AFAICT there's only that one callback which is sto
vakh (use Gerrit instead)
2016/06/27 19:38:26
Your reading is right. What I wrote is different t
Scott Hess - ex-Googler
2016/06/27 22:19:17
OK, I guess I'll leave this to you. I find the st
vakh (use Gerrit instead)
2016/06/27 23:58:52
Done.
| |
251 } | |
239 | 252 |
240 // TODO(vakh): Updates downloaded. Store them on disk and record new state. | 253 void V4LocalDatabaseManager::DatabaseUpdated() { |
241 v4_update_protocol_manager_->ScheduleNextUpdate(); | 254 v4_update_protocol_manager_->ScheduleNextUpdate(); |
242 } | 255 } |
243 | 256 |
244 } // namespace safe_browsing | 257 } // namespace safe_browsing |
OLD | NEW |