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

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

Issue 2384893002: PVer4: Test checksum on startup outside the hotpath of DB load (Closed)
Patch Set: go: design-doc-v4store-verifychecksum -- VerifyChecksum in a way that avoids race conditions betwee… Created 4 years, 2 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 #include <memory> 5 #include <memory>
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/debug/leak_annotations.h" 8 #include "base/debug/leak_annotations.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 } 62 }
63 63
64 const base::FilePath store_path = base_path.AppendASCII(it.filename()); 64 const base::FilePath store_path = base_path.AppendASCII(it.filename());
65 (*store_map)[it.list_id()].reset( 65 (*store_map)[it.list_id()].reset(
66 factory_->CreateV4Store(db_task_runner, store_path)); 66 factory_->CreateV4Store(db_task_runner, store_path));
67 } 67 }
68 std::unique_ptr<V4Database> v4_database( 68 std::unique_ptr<V4Database> v4_database(
69 new V4Database(db_task_runner, std::move(store_map))); 69 new V4Database(db_task_runner, std::move(store_map)));
70 70
71 // Database is done loading, pass it to the new_db_callback on the caller's 71 // Database is done loading, pass it to the new_db_callback on the caller's
72 // thread. 72 // thread. This would unblock URL navigation.
73 callback_task_runner->PostTask( 73 callback_task_runner->PostTask(
74 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database))); 74 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database)));
75 } 75 }
76 76
77 V4Database::V4Database( 77 V4Database::V4Database(
78 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 78 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
79 std::unique_ptr<StoreMap> store_map) 79 std::unique_ptr<StoreMap> store_map)
80 : db_task_runner_(db_task_runner), 80 : db_task_runner_(db_task_runner),
81 store_map_(std::move(store_map)), 81 store_map_(std::move(store_map)),
82 pending_store_updates_(0) { 82 pending_store_updates_(0) {
83 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); 83 DCHECK(db_task_runner->RunsTasksOnCurrentThread());
84 // TODO(vakh): Implement skeleton
85 } 84 }
86 85
87 // static 86 // static
88 void V4Database::Destroy(std::unique_ptr<V4Database> v4_database) { 87 void V4Database::Destroy(std::unique_ptr<V4Database> v4_database) {
89 V4Database* v4_database_raw = v4_database.release(); 88 V4Database* v4_database_raw = v4_database.release();
90 if (v4_database_raw) { 89 if (v4_database_raw) {
91 v4_database_raw->db_task_runner_->DeleteSoon(FROM_HERE, v4_database_raw); 90 v4_database_raw->db_task_runner_->DeleteSoon(FROM_HERE, v4_database_raw);
92 } 91 }
93 } 92 }
94 93
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 (*store_map_)[identifier] = std::move(new_store); 144 (*store_map_)[identifier] = std::move(new_store);
146 } 145 }
147 146
148 pending_store_updates_--; 147 pending_store_updates_--;
149 if (!pending_store_updates_) { 148 if (!pending_store_updates_) {
150 db_updated_callback_.Run(); 149 db_updated_callback_.Run();
151 db_updated_callback_.Reset(); 150 db_updated_callback_.Reset();
152 } 151 }
153 } 152 }
154 153
155 bool V4Database::ResetDatabase() {
156 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
157 bool reset_success = true;
158 for (const auto& store_map_iter : *store_map_) {
159 if (!store_map_iter.second->Reset()) {
160 reset_success = false;
161 }
162 }
163 return reset_success;
164 }
165
166 std::unique_ptr<StoreStateMap> V4Database::GetStoreStateMap() { 154 std::unique_ptr<StoreStateMap> V4Database::GetStoreStateMap() {
167 std::unique_ptr<StoreStateMap> store_state_map = 155 std::unique_ptr<StoreStateMap> store_state_map =
168 base::MakeUnique<StoreStateMap>(); 156 base::MakeUnique<StoreStateMap>();
169 for (const auto& store_map_iter : *store_map_) { 157 for (const auto& store_map_iter : *store_map_) {
170 (*store_state_map)[store_map_iter.first] = store_map_iter.second->state(); 158 (*store_state_map)[store_map_iter.first] = store_map_iter.second->state();
171 } 159 }
172 return store_state_map; 160 return store_state_map;
173 } 161 }
174 162
175 void V4Database::GetStoresMatchingFullHash( 163 void V4Database::GetStoresMatchingFullHash(
176 const FullHash& full_hash, 164 const FullHash& full_hash,
177 const StoresToCheck& stores_to_check, 165 const StoresToCheck& stores_to_check,
178 StoreAndHashPrefixes* matched_store_and_hash_prefixes) { 166 StoreAndHashPrefixes* matched_store_and_hash_prefixes) {
179 DCHECK_CURRENTLY_ON(BrowserThread::IO); 167 DCHECK_CURRENTLY_ON(BrowserThread::IO);
180 matched_store_and_hash_prefixes->clear(); 168 matched_store_and_hash_prefixes->clear();
181 for (const ListIdentifier& identifier : stores_to_check) { 169 for (const ListIdentifier& identifier : stores_to_check) {
182 const auto& store_pair = store_map_->find(identifier); 170 const auto& store_pair = store_map_->find(identifier);
183 DCHECK(store_pair != store_map_->end()); 171 DCHECK(store_pair != store_map_->end());
184 const std::unique_ptr<V4Store>& store = store_pair->second; 172 const std::unique_ptr<V4Store>& store = store_pair->second;
185 HashPrefix hash_prefix = store->GetMatchingHashPrefix(full_hash); 173 HashPrefix hash_prefix = store->GetMatchingHashPrefix(full_hash);
186 if (!hash_prefix.empty()) { 174 if (!hash_prefix.empty()) {
187 matched_store_and_hash_prefixes->emplace_back(identifier, hash_prefix); 175 matched_store_and_hash_prefixes->emplace_back(identifier, hash_prefix);
188 } 176 }
189 } 177 }
190 } 178 }
191 179
180 void V4Database::ResetStores(
181 const std::unordered_set<ListIdentifier>& stores_to_reset) {
182 DCHECK_CURRENTLY_ON(BrowserThread::IO);
183 for (const ListIdentifier& identifier : stores_to_reset) {
184 const auto& store_pair = store_map_->find(identifier);
185 DCHECK(store_pair != store_map_->end());
186 store_pair->second->Reset();
187 }
188 }
189
190 void V4Database::VerifyChecksum(
191 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback) {
192 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner =
193 base::MessageLoop::current()->task_runner();
194 db_task_runner_->PostTask(
195 FROM_HERE, base::Bind(&V4Database::VerifyChecksumOnTaskRunner,
196 base::Unretained(this), callback_task_runner,
197 db_ready_for_updates_callback));
198 }
199
200 void V4Database::VerifyChecksumOnTaskRunner(
201 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
202 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback) {
203 std::unordered_set<ListIdentifier> stores_to_reset;
204 for (const auto& store_map_iter : *store_map_) {
205 if (!store_map_iter.second->VerifyChecksum()) {
206 stores_to_reset.insert(store_map_iter.first);
207 }
208 }
209
210 callback_task_runner->PostTask(
211 FROM_HERE, base::Bind(db_ready_for_updates_callback, stores_to_reset));
212 }
213
192 ListInfo::ListInfo(const bool fetch_updates, 214 ListInfo::ListInfo(const bool fetch_updates,
193 const std::string& filename, 215 const std::string& filename,
194 const ListIdentifier& list_id, 216 const ListIdentifier& list_id,
195 const SBThreatType sb_threat_type) 217 const SBThreatType sb_threat_type)
196 : fetch_updates_(fetch_updates), 218 : fetch_updates_(fetch_updates),
197 filename_(filename), 219 filename_(filename),
198 list_id_(list_id), 220 list_id_(list_id),
199 sb_threat_type_(sb_threat_type) { 221 sb_threat_type_(sb_threat_type) {
200 DCHECK(!fetch_updates_ || !filename_.empty()); 222 DCHECK(!fetch_updates_ || !filename_.empty());
201 DCHECK_NE(SB_THREAT_TYPE_SAFE, sb_threat_type_); 223 DCHECK_NE(SB_THREAT_TYPE_SAFE, sb_threat_type_);
202 } 224 }
203 225
204 ListInfo::~ListInfo() {} 226 ListInfo::~ListInfo() {}
205 227
206 } // namespace safe_browsing 228 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698