Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(421)

Side by Side Diff: components/safe_browsing_db/v4_local_database_manager.cc

Issue 2062013002: Fetch incremental updates. Store new state in V4Store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: git fetch && git pull && gclient sync Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 fixes, this should be removed. If we leave
Scott Hess - ex-Googler 2016/06/24 23:01:19 s/fixes/fixed/
vakh (use Gerrit instead) 2016/06/27 19:38:27 Done.
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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 SetupUpdateProtocolManager(request_context_getter, config); 174 SetupUpdateProtocolManager(request_context_getter, config);
146 175
147 SetupDatabase(); 176 SetupDatabase();
148 177
149 enabled_ = true; 178 enabled_ = true;
150 } 179 }
151 180
152 void V4LocalDatabaseManager::SetupUpdateProtocolManager( 181 void V4LocalDatabaseManager::SetupUpdateProtocolManager(
153 net::URLRequestContextGetter* request_context_getter, 182 net::URLRequestContextGetter* request_context_getter,
154 const V4ProtocolConfig& config) { 183 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( 184 V4UpdateCallback callback = base::Bind(
172 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); 185 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this));
173 186
174 v4_update_protocol_manager_ = V4UpdateProtocolManager::Create( 187 v4_update_protocol_manager_ =
175 request_context_getter, config, current_list_states_, callback); 188 V4UpdateProtocolManager::Create(request_context_getter, config, callback);
176 } 189 }
177 190
178 void V4LocalDatabaseManager::SetupDatabase() { 191 void V4LocalDatabaseManager::SetupDatabase() {
179 DCHECK(!base_path_.empty()); 192 DCHECK(!base_path_.empty());
180 DCHECK_CURRENTLY_ON(BrowserThread::IO); 193 DCHECK_CURRENTLY_ON(BrowserThread::IO);
181 194
182 // Only get a new task runner if there isn't one already. If the service has 195 // 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. 196 // previously been started and stopped, a task runner could already exist.
184 if (!task_runner_) { 197 if (!task_runner_) {
185 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); 198 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
186 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( 199 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior(
187 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 200 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
188 } 201 }
189 202
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 203 // 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 204 // operation. Instead, do that on the task_runner and when the new database
195 // has been created, swap it out on the IO thread. 205 // has been created, swap it out on the IO thread.
206 DCHECK(!store_file_name_map.empty());
207 DatabaseUpdatedCallback db_updated_callback = base::Bind(
208 &V4LocalDatabaseManager::DatabaseUpdated, base::Unretained(this));
196 NewDatabaseReadyCallback db_ready_callback = base::Bind( 209 NewDatabaseReadyCallback db_ready_callback = base::Bind(
197 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); 210 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this));
198 V4Database::Create(task_runner_, base_path_, store_file_name_map, 211 V4Database::Create(task_runner_, base_path_, store_file_name_map,
199 db_ready_callback); 212 db_updated_callback, db_ready_callback);
200 } 213 }
201 214
202 void V4LocalDatabaseManager::DatabaseReady( 215 void V4LocalDatabaseManager::DatabaseReady(
203 std::unique_ptr<V4Database> v4_database) { 216 std::unique_ptr<V4Database> v4_database) {
204 DCHECK_CURRENTLY_ON(BrowserThread::IO); 217 DCHECK_CURRENTLY_ON(BrowserThread::IO);
205 218
206 // The following check is needed because it is possible that by the time the 219 // The following check is needed because it is possible that by the time the
207 // database is ready, StopOnIOThread has been called. 220 // database is ready, StopOnIOThread has been called.
208 if (enabled_) { 221 if (enabled_) {
209 v4_database_ = std::move(v4_database); 222 v4_database_ = std::move(v4_database);
210 223
211 // The database is in place. Start fetching updates now. 224 // The database is in place. Start fetching updates now.
212 v4_update_protocol_manager_->ScheduleNextUpdate(); 225 v4_update_protocol_manager_->ScheduleNextUpdate(
226 v4_database_->GetStoreStateMap());
213 } else { 227 } else {
214 // Schedule the deletion of v4_database off IO thread. 228 // Schedule the deletion of v4_database off IO thread.
215 V4Database::Destroy(std::move(v4_database)); 229 V4Database::Destroy(std::move(v4_database));
216 } 230 }
217 } 231 }
218 232
219 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) { 233 void V4LocalDatabaseManager::StopOnIOThread(bool shutdown) {
220 DCHECK_CURRENTLY_ON(BrowserThread::IO); 234 DCHECK_CURRENTLY_ON(BrowserThread::IO);
221 235
222 enabled_ = false; 236 enabled_ = false;
223 237
224 // Delete the V4Database. Any pending writes to disk are completed. 238 // Delete the V4Database. Any pending writes to disk are completed.
225 // This operation happens on the task_runner on which v4_database_ operates 239 // This operation happens on the task_runner on which v4_database_ operates
226 // and doesn't block the IO thread. 240 // and doesn't block the IO thread.
227 V4Database::Destroy(std::move(v4_database_)); 241 V4Database::Destroy(std::move(v4_database_));
228 242
229 // Delete the V4UpdateProtocolManager. 243 // Delete the V4UpdateProtocolManager.
230 // This cancels any in-flight update request. 244 // This cancels any in-flight update request.
231 v4_update_protocol_manager_.reset(); 245 v4_update_protocol_manager_.reset();
232 246
233 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown); 247 SafeBrowsingDatabaseManager::StopOnIOThread(shutdown);
234 } 248 }
235 249
236 void V4LocalDatabaseManager::UpdateRequestCompleted( 250 void V4LocalDatabaseManager::UpdateRequestCompleted(
237 const std::vector<ListUpdateResponse>& responses) { 251 const std::vector<ListUpdateResponse>& responses) {
238 DCHECK_CURRENTLY_ON(BrowserThread::IO); 252 DCHECK_CURRENTLY_ON(BrowserThread::IO);
253 v4_database_->ApplyUpdate(responses);
254 }
239 255
240 // TODO(vakh): Updates downloaded. Store them on disk and record new state. 256 void V4LocalDatabaseManager::DatabaseUpdated() {
241 v4_update_protocol_manager_->ScheduleNextUpdate(); 257 v4_update_protocol_manager_->ScheduleNextUpdate(
258 v4_database_->GetStoreStateMap());
242 } 259 }
243 260
244 } // namespace safe_browsing 261 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698