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

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

Issue 2345573002: Each DatabaseManager gets to decide which stores to track (Closed)
Patch Set: Incorporated nparker@ feedback Created 4 years, 3 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. 5 // This file should not be build on Android but is currently getting built.
6 // TODO(vakh): Fix that: http://crbug.com/621647 6 // TODO(vakh): Fix that: http://crbug.com/621647
7 7
8 #include "components/safe_browsing_db/v4_local_database_manager.h" 8 #include "components/safe_browsing_db/v4_local_database_manager.h"
9 9
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 14
15 using content::BrowserThread; 15 using content::BrowserThread;
16 16
17 namespace safe_browsing { 17 namespace safe_browsing {
18 18
19 namespace { 19 namespace {
20 20
21 // TODO(vakh): Implement this to populate the map appopriately. 21 // TODO(vakh): Implement this to populate the vector appopriately.
22 // Filed as http://crbug.com/608075 22 // Filed as http://crbug.com/608075
23 StoreFileNameMap GetStoreFileNameMap() { 23 StoreIdAndFileNames GetStoreIdAndFileNames() {
24 return StoreFileNameMap({{GetUrlMalwareId(), "UrlMalware.store"}, 24 return StoreIdAndFileNames(
25 {GetUrlSocEngId(), "UrlSoceng.store"}}); 25 {StoreIdAndFileName(GetUrlMalwareId(), "UrlMalware.store"),
26 StoreIdAndFileName(GetUrlSocEngId(), "UrlSoceng.store")});
26 } 27 }
27 28
28 } // namespace 29 } // namespace
29 30
30 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path) 31 V4LocalDatabaseManager::V4LocalDatabaseManager(const base::FilePath& base_path)
31 : base_path_(base_path), enabled_(false) { 32 : base_path_(base_path),
33 enabled_(false),
34 store_id_file_names_(GetStoreIdAndFileNames()) {
32 DCHECK(!base_path_.empty()); 35 DCHECK(!base_path_.empty());
36 DCHECK(!store_id_file_names_.empty());
37
33 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: " 38 DVLOG(1) << "V4LocalDatabaseManager::V4LocalDatabaseManager: "
34 << "base_path_: " << base_path_.AsUTF8Unsafe(); 39 << "base_path_: " << base_path_.AsUTF8Unsafe();
35 } 40 }
36 41
37 V4LocalDatabaseManager::~V4LocalDatabaseManager() { 42 V4LocalDatabaseManager::~V4LocalDatabaseManager() {
38 DCHECK(!enabled_); 43 DCHECK(!enabled_);
39 } 44 }
40 45
41 bool V4LocalDatabaseManager::IsSupported() const { 46 bool V4LocalDatabaseManager::IsSupported() const {
42 return true; 47 return true;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 return true; 138 return true;
134 } 139 }
135 140
136 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) { 141 bool V4LocalDatabaseManager::CheckBrowseUrl(const GURL& url, Client* client) {
137 DCHECK_CURRENTLY_ON(BrowserThread::IO); 142 DCHECK_CURRENTLY_ON(BrowserThread::IO);
138 if (!enabled_ || !CanCheckUrl(url)) { 143 if (!enabled_ || !CanCheckUrl(url)) {
139 return true; 144 return true;
140 } 145 }
141 146
142 if (v4_database_) { 147 if (v4_database_) {
143 base::hash_set<FullHash> full_hashes; 148 std::unordered_set<FullHash> full_hashes;
144 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes); 149 V4ProtocolManagerUtil::UrlToFullHashes(url, &full_hashes);
145 150
146 base::hash_set<UpdateListIdentifier> stores_to_look( 151 std::unordered_set<UpdateListIdentifier> stores_to_look(
147 {GetUrlMalwareId(), GetUrlSocEngId()}); 152 {GetUrlMalwareId(), GetUrlSocEngId()});
148 base::hash_set<HashPrefix> matched_hash_prefixes; 153 std::unordered_set<HashPrefix> matched_hash_prefixes;
149 base::hash_set<UpdateListIdentifier> matched_stores; 154 std::unordered_set<UpdateListIdentifier> matched_stores;
150 StoreAndHashPrefixes matched_store_and_full_hashes; 155 StoreAndHashPrefixes matched_store_and_full_hashes;
151 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes; 156 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
152 for (const auto& full_hash : full_hashes) { 157 for (const auto& full_hash : full_hashes) {
153 matched_store_and_full_hashes.clear(); 158 matched_store_and_full_hashes.clear();
154 v4_database_->GetStoresMatchingFullHash(full_hash, stores_to_look, 159 v4_database_->GetStoresMatchingFullHash(full_hash, stores_to_look,
155 &matched_store_and_full_hashes); 160 &matched_store_and_full_hashes);
156 if (!matched_store_and_full_hashes.empty()) { 161 if (!matched_store_and_full_hashes.empty()) {
157 full_hash_to_store_and_hash_prefixes[full_hash] = 162 full_hash_to_store_and_hash_prefixes[full_hash] =
158 matched_store_and_full_hashes; 163 matched_store_and_full_hashes;
159 } 164 }
(...skipping 30 matching lines...) Expand all
190 195
191 void V4LocalDatabaseManager::StartOnIOThread( 196 void V4LocalDatabaseManager::StartOnIOThread(
192 net::URLRequestContextGetter* request_context_getter, 197 net::URLRequestContextGetter* request_context_getter,
193 const V4ProtocolConfig& config) { 198 const V4ProtocolConfig& config) {
194 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config); 199 SafeBrowsingDatabaseManager::StartOnIOThread(request_context_getter, config);
195 200
196 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated, 201 db_updated_callback_ = base::Bind(&V4LocalDatabaseManager::DatabaseUpdated,
197 base::Unretained(this)); 202 base::Unretained(this));
198 203
199 SetupUpdateProtocolManager(request_context_getter, config); 204 SetupUpdateProtocolManager(request_context_getter, config);
200
201 SetupDatabase(); 205 SetupDatabase();
202 206
203 enabled_ = true; 207 enabled_ = true;
204 } 208 }
205 209
206 void V4LocalDatabaseManager::SetupUpdateProtocolManager( 210 void V4LocalDatabaseManager::SetupUpdateProtocolManager(
207 net::URLRequestContextGetter* request_context_getter, 211 net::URLRequestContextGetter* request_context_getter,
208 const V4ProtocolConfig& config) { 212 const V4ProtocolConfig& config) {
209 V4UpdateCallback callback = base::Bind( 213 V4UpdateCallback callback = base::Bind(
210 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this)); 214 &V4LocalDatabaseManager::UpdateRequestCompleted, base::Unretained(this));
211 215
212 v4_update_protocol_manager_ = 216 v4_update_protocol_manager_ =
213 V4UpdateProtocolManager::Create(request_context_getter, config, callback); 217 V4UpdateProtocolManager::Create(request_context_getter, config, callback);
214 } 218 }
215 219
216 void V4LocalDatabaseManager::SetupDatabase() { 220 void V4LocalDatabaseManager::SetupDatabase() {
217 DCHECK(!base_path_.empty()); 221 DCHECK(!base_path_.empty());
222 DCHECK(!store_id_file_names_.empty());
kcarattini 2016/09/18 12:20:46 Just checking that this list includes API checks a
vakh (use Gerrit instead) 2016/09/19 17:03:05 SafeBrowsingDatabaseManager includes API checks. V
kcarattini 2016/09/26 00:23:34 So that means that until it's added to V$, API che
vakh (use Gerrit instead) 2016/09/26 06:09:55 That's OK. Other things are sill using PVer3 but I
218 DCHECK_CURRENTLY_ON(BrowserThread::IO); 223 DCHECK_CURRENTLY_ON(BrowserThread::IO);
219 224
220 // Only get a new task runner if there isn't one already. If the service has 225 // Only get a new task runner if there isn't one already. If the service has
221 // previously been started and stopped, a task runner could already exist. 226 // previously been started and stopped, a task runner could already exist.
222 if (!task_runner_) { 227 if (!task_runner_) {
223 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); 228 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
224 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( 229 task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior(
225 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 230 pool->GetSequenceToken(), base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
226 } 231 }
227 232
228 // Do not create the database on the IO thread since this may be an expensive 233 // Do not create the database on the IO thread since this may be an expensive
229 // operation. Instead, do that on the task_runner and when the new database 234 // operation. Instead, do that on the task_runner and when the new database
230 // has been created, swap it out on the IO thread. 235 // has been created, swap it out on the IO thread.
231 StoreFileNameMap store_file_name_map = GetStoreFileNameMap();
232 DCHECK(!store_file_name_map.empty());
233 NewDatabaseReadyCallback db_ready_callback = base::Bind( 236 NewDatabaseReadyCallback db_ready_callback = base::Bind(
234 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this)); 237 &V4LocalDatabaseManager::DatabaseReady, base::Unretained(this));
235 V4Database::Create(task_runner_, base_path_, store_file_name_map, 238 V4Database::Create(task_runner_, base_path_, store_id_file_names_,
236 db_ready_callback); 239 db_ready_callback);
237 } 240 }
238 241
239 void V4LocalDatabaseManager::DatabaseReady( 242 void V4LocalDatabaseManager::DatabaseReady(
240 std::unique_ptr<V4Database> v4_database) { 243 std::unique_ptr<V4Database> v4_database) {
241 DCHECK_CURRENTLY_ON(BrowserThread::IO); 244 DCHECK_CURRENTLY_ON(BrowserThread::IO);
242 245
243 // The following check is needed because it is possible that by the time the 246 // The following check is needed because it is possible that by the time the
244 // database is ready, StopOnIOThread has been called. 247 // database is ready, StopOnIOThread has been called.
245 if (enabled_) { 248 if (enabled_) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 db_updated_callback_); 283 db_updated_callback_);
281 } 284 }
282 285
283 void V4LocalDatabaseManager::DatabaseUpdated() { 286 void V4LocalDatabaseManager::DatabaseUpdated() {
284 if (enabled_) { 287 if (enabled_) {
285 v4_update_protocol_manager_->ScheduleNextUpdate( 288 v4_update_protocol_manager_->ScheduleNextUpdate(
286 v4_database_->GetStoreStateMap()); 289 v4_database_->GetStoreStateMap());
287 } 290 }
288 } 291 }
289 292
293 std::unordered_set<UpdateListIdentifier>
294 V4LocalDatabaseManager::GetStoresForFullHashRequests() {
295 std::unordered_set<UpdateListIdentifier> stores_for_full_hash;
296 for (auto it : store_id_file_names_) {
297 stores_for_full_hash.insert(it.list_id);
298 }
299 return stores_for_full_hash;
300 }
301
290 } // namespace safe_browsing 302 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698