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 <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" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "components/safe_browsing_db/v4_database.h" | 12 #include "components/safe_browsing_db/v4_database.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 // static | 19 // static |
20 V4StoreFactory* V4Database::factory_ = NULL; | 20 V4StoreFactory* V4Database::factory_ = NULL; |
21 | 21 |
22 // static | 22 // static |
23 void V4Database::Create( | 23 void V4Database::Create( |
24 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 24 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
25 const base::FilePath& base_path, | 25 const base::FilePath& base_path, |
26 const StoreFileNameMap& store_file_name_map, | 26 const StoreIdAndFileNames& store_id_file_names, |
27 NewDatabaseReadyCallback new_db_callback) { | 27 NewDatabaseReadyCallback new_db_callback) { |
28 DCHECK(base_path.IsAbsolute()); | 28 DCHECK(base_path.IsAbsolute()); |
29 DCHECK(!store_file_name_map.empty()); | 29 DCHECK(!store_id_file_names.empty()); |
30 | 30 |
31 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner = | 31 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner = |
32 base::MessageLoop::current()->task_runner(); | 32 base::MessageLoop::current()->task_runner(); |
33 db_task_runner->PostTask( | 33 db_task_runner->PostTask( |
34 FROM_HERE, | 34 FROM_HERE, |
35 base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, base_path, | 35 base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, base_path, |
36 store_file_name_map, callback_task_runner, new_db_callback)); | 36 store_id_file_names, callback_task_runner, new_db_callback)); |
37 } | 37 } |
38 | 38 |
39 // static | 39 // static |
40 void V4Database::CreateOnTaskRunner( | 40 void V4Database::CreateOnTaskRunner( |
41 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 41 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
42 const base::FilePath& base_path, | 42 const base::FilePath& base_path, |
43 const StoreFileNameMap& store_file_name_map, | 43 const StoreIdAndFileNames& store_id_file_names, |
44 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 44 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
45 NewDatabaseReadyCallback new_db_callback) { | 45 NewDatabaseReadyCallback new_db_callback) { |
46 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); | 46 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); |
47 | 47 |
48 if (!factory_) { | 48 if (!factory_) { |
49 factory_ = new V4StoreFactory(); | 49 factory_ = new V4StoreFactory(); |
50 ANNOTATE_LEAKING_OBJECT_PTR(factory_); | 50 ANNOTATE_LEAKING_OBJECT_PTR(factory_); |
51 } | 51 } |
52 | 52 |
53 if (!base::CreateDirectory(base_path)) { | 53 if (!base::CreateDirectory(base_path)) { |
54 NOTREACHED(); | 54 NOTREACHED(); |
55 } | 55 } |
56 | 56 |
57 std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>(); | 57 std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>(); |
58 for (const auto& store_info : store_file_name_map) { | 58 for (const auto& it : store_id_file_names) { |
59 const UpdateListIdentifier& update_list_identifier = store_info.first; | 59 const base::FilePath store_path = base_path.AppendASCII(it.filename); |
60 const base::FilePath store_path = base_path.AppendASCII(store_info.second); | 60 (*store_map)[it.list_id].reset( |
61 (*store_map)[update_list_identifier].reset( | |
62 factory_->CreateV4Store(db_task_runner, store_path)); | 61 factory_->CreateV4Store(db_task_runner, store_path)); |
63 } | 62 } |
64 std::unique_ptr<V4Database> v4_database( | 63 std::unique_ptr<V4Database> v4_database( |
65 new V4Database(db_task_runner, std::move(store_map))); | 64 new V4Database(db_task_runner, std::move(store_map))); |
66 | 65 |
67 // Database is done loading, pass it to the new_db_callback on the caller's | 66 // Database is done loading, pass it to the new_db_callback on the caller's |
68 // thread. | 67 // thread. |
69 callback_task_runner->PostTask( | 68 callback_task_runner->PostTask( |
70 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database))); | 69 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database))); |
71 } | 70 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 std::unique_ptr<StoreStateMap> store_state_map = | 162 std::unique_ptr<StoreStateMap> store_state_map = |
164 base::MakeUnique<StoreStateMap>(); | 163 base::MakeUnique<StoreStateMap>(); |
165 for (const auto& store_map_iter : *store_map_) { | 164 for (const auto& store_map_iter : *store_map_) { |
166 (*store_state_map)[store_map_iter.first] = store_map_iter.second->state(); | 165 (*store_state_map)[store_map_iter.first] = store_map_iter.second->state(); |
167 } | 166 } |
168 return store_state_map; | 167 return store_state_map; |
169 } | 168 } |
170 | 169 |
171 void V4Database::GetStoresMatchingFullHash( | 170 void V4Database::GetStoresMatchingFullHash( |
172 const FullHash& full_hash, | 171 const FullHash& full_hash, |
173 const base::hash_set<UpdateListIdentifier>& stores_to_look, | 172 const std::unordered_set<UpdateListIdentifier>& stores_to_look, |
174 StoreAndHashPrefixes* matched_store_and_hash_prefixes) { | 173 StoreAndHashPrefixes* matched_store_and_hash_prefixes) { |
175 matched_store_and_hash_prefixes->clear(); | 174 matched_store_and_hash_prefixes->clear(); |
176 for (const UpdateListIdentifier& identifier : stores_to_look) { | 175 for (const UpdateListIdentifier& identifier : stores_to_look) { |
177 const auto& store_pair = store_map_->find(identifier); | 176 const auto& store_pair = store_map_->find(identifier); |
178 DCHECK(store_pair != store_map_->end()); | 177 DCHECK(store_pair != store_map_->end()); |
179 const std::unique_ptr<V4Store>& store = store_pair->second; | 178 const std::unique_ptr<V4Store>& store = store_pair->second; |
180 HashPrefix hash_prefix = store->GetMatchingHashPrefix(full_hash); | 179 HashPrefix hash_prefix = store->GetMatchingHashPrefix(full_hash); |
181 if (!hash_prefix.empty()) { | 180 if (!hash_prefix.empty()) { |
182 matched_store_and_hash_prefixes->emplace_back(identifier, hash_prefix); | 181 matched_store_and_hash_prefixes->emplace_back(identifier, hash_prefix); |
183 } | 182 } |
184 } | 183 } |
185 } | 184 } |
186 | 185 |
| 186 StoreIdAndFileName::StoreIdAndFileName(const UpdateListIdentifier& list_id, |
| 187 const std::string& filename) |
| 188 : list_id(list_id), filename(filename) { |
| 189 DCHECK(!filename.empty()); |
| 190 } |
| 191 |
| 192 StoreIdAndFileName::~StoreIdAndFileName() {} |
| 193 |
187 } // namespace safe_browsing | 194 } // namespace safe_browsing |
OLD | NEW |