| 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 StoreIdAndFileNames& store_id_file_names, | 26 const ListInfos& list_infos, |
| 27 NewDatabaseReadyCallback new_db_callback) { | 27 NewDatabaseReadyCallback new_db_callback) { |
| 28 DCHECK(base_path.IsAbsolute()); | 28 DCHECK(base_path.IsAbsolute()); |
| 29 DCHECK(!store_id_file_names.empty()); | 29 DCHECK(!list_infos.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_id_file_names, callback_task_runner, new_db_callback)); | 36 list_infos, 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 StoreIdAndFileNames& store_id_file_names, | 43 const ListInfos& list_infos, |
| 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& it : store_id_file_names) { | 58 for (const auto& it : list_infos) { |
| 59 const base::FilePath store_path = base_path.AppendASCII(it.filename); | 59 if (!it.fetch_updates()) { |
| 60 (*store_map)[it.list_id].reset( | 60 // This list doesn't need to be fetched or stored on disk. |
| 61 continue; |
| 62 } |
| 63 |
| 64 const base::FilePath store_path = base_path.AppendASCII(it.filename()); |
| 65 (*store_map)[it.list_id()].reset( |
| 61 factory_->CreateV4Store(db_task_runner, store_path)); | 66 factory_->CreateV4Store(db_task_runner, store_path)); |
| 62 } | 67 } |
| 63 std::unique_ptr<V4Database> v4_database( | 68 std::unique_ptr<V4Database> v4_database( |
| 64 new V4Database(db_task_runner, std::move(store_map))); | 69 new V4Database(db_task_runner, std::move(store_map))); |
| 65 | 70 |
| 66 // 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 |
| 67 // thread. | 72 // thread. |
| 68 callback_task_runner->PostTask( | 73 callback_task_runner->PostTask( |
| 69 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database))); | 74 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database))); |
| 70 } | 75 } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 const auto& store_pair = store_map_->find(identifier); | 182 const auto& store_pair = store_map_->find(identifier); |
| 178 DCHECK(store_pair != store_map_->end()); | 183 DCHECK(store_pair != store_map_->end()); |
| 179 const std::unique_ptr<V4Store>& store = store_pair->second; | 184 const std::unique_ptr<V4Store>& store = store_pair->second; |
| 180 HashPrefix hash_prefix = store->GetMatchingHashPrefix(full_hash); | 185 HashPrefix hash_prefix = store->GetMatchingHashPrefix(full_hash); |
| 181 if (!hash_prefix.empty()) { | 186 if (!hash_prefix.empty()) { |
| 182 matched_store_and_hash_prefixes->emplace_back(identifier, hash_prefix); | 187 matched_store_and_hash_prefixes->emplace_back(identifier, hash_prefix); |
| 183 } | 188 } |
| 184 } | 189 } |
| 185 } | 190 } |
| 186 | 191 |
| 187 StoreIdAndFileName::StoreIdAndFileName(const ListIdentifier& list_id, | 192 ListInfo::ListInfo(const bool fetch_updates, |
| 188 const std::string& filename) | 193 const std::string& filename, |
| 189 : list_id(list_id), filename(filename) { | 194 const ListIdentifier& list_id, |
| 190 DCHECK(!filename.empty()); | 195 const SBThreatType sb_threat_type) |
| 196 : fetch_updates_(fetch_updates), |
| 197 filename_(filename), |
| 198 list_id_(list_id), |
| 199 sb_threat_type_(sb_threat_type) { |
| 200 DCHECK(!fetch_updates_ || !filename_.empty()); |
| 201 DCHECK_NE(SB_THREAT_TYPE_SAFE, sb_threat_type_); |
| 191 } | 202 } |
| 192 | 203 |
| 193 StoreIdAndFileName::~StoreIdAndFileName() {} | 204 ListInfo::~ListInfo() {} |
| 194 | 205 |
| 195 } // namespace safe_browsing | 206 } // namespace safe_browsing |
| OLD | NEW |