| 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> |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 5 #include "components/safe_browsing_db/v4_database.h" | 9 #include "components/safe_browsing_db/v4_database.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 |
| 12 using content::BrowserThread; |
| 6 | 13 |
| 7 namespace safe_browsing { | 14 namespace safe_browsing { |
| 8 | 15 |
| 16 namespace { |
| 17 |
| 18 V4Store* CreateStore( |
| 19 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 20 const base::FilePath& store_path) { |
| 21 return new V4Store(task_runner, store_path); |
| 22 } |
| 23 |
| 24 } // namespace |
| 25 |
| 9 // static | 26 // static |
| 10 V4DatabaseFactory* V4Database::factory_ = NULL; | 27 V4DatabaseFactory* V4Database::factory_ = NULL; |
| 11 | 28 |
| 12 // static | 29 // static |
| 13 // Factory method, should be called on the Safe Browsing sequenced task runner, | 30 void V4Database::Create( |
| 14 // which is also passed to the function as |db_task_runner|. | |
| 15 V4Database* V4Database::Create( | |
| 16 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 31 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 17 const base::FilePath& base_path, | 32 const base::FilePath& base_path, |
| 18 ListInfoMap list_info_map) { | 33 ListInfoMap list_info_map, |
| 34 NewDatabaseReadyCallback callback) { |
| 35 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner = |
| 36 base::MessageLoop::current()->task_runner(); |
| 37 db_task_runner->PostTask( |
| 38 FROM_HERE, |
| 39 base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, base_path, |
| 40 list_info_map, callback_task_runner, callback)); |
| 41 } |
| 42 |
| 43 // static |
| 44 void V4Database::CreateOnTaskRunner( |
| 45 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 46 const base::FilePath& base_path, |
| 47 ListInfoMap list_info_map, |
| 48 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
| 49 NewDatabaseReadyCallback callback) { |
| 19 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); | 50 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); |
| 51 DCHECK(!base_path.empty()); |
| 52 |
| 53 std::unique_ptr<V4Database> v4_database; |
| 20 if (!factory_) { | 54 if (!factory_) { |
| 21 StoreMap store_map; | 55 StoreMap store_map; |
| 22 // TODO(vakh): Populate the store_map using list_suffix_map. | 56 |
| 23 return new V4Database(db_task_runner, std::move(store_map)); | 57 for (const auto& list_info : list_info_map) { |
| 58 UpdateListIdentifier update_list_identifier = list_info.first; |
| 59 const base::FilePath::CharType suffix = list_info.second; |
| 60 |
| 61 const base::FilePath store_path = |
| 62 base::FilePath(base_path.value() + suffix); |
| 63 (*store_map)[update_list_identifier].reset( |
| 64 CreateStore(db_task_runner, store_path)); |
| 65 } |
| 66 |
| 67 v4_database.reset(new V4Database(db_task_runner, std::move(store_map))); |
| 24 } else { | 68 } else { |
| 25 return factory_->CreateV4Database(db_task_runner, base_path, list_info_map); | 69 v4_database.reset( |
| 70 factory_->CreateV4Database(db_task_runner, base_path, list_info_map)); |
| 26 } | 71 } |
| 72 callback_task_runner->PostTask( |
| 73 FROM_HERE, base::Bind(callback, base::Passed(&v4_database))); |
| 27 } | 74 } |
| 28 | 75 |
| 29 V4Database::V4Database( | 76 V4Database::V4Database( |
| 30 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 77 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 31 StoreMap store_map) { | 78 StoreMap store_map) |
| 79 : db_task_runner_(db_task_runner), store_map_(std::move(store_map)) { |
| 80 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); |
| 32 // TODO(vakh): Implement skeleton | 81 // TODO(vakh): Implement skeleton |
| 33 } | 82 } |
| 34 | 83 |
| 84 // static |
| 85 void V4Database::Destroy(std::unique_ptr<V4Database> v4_database) { |
| 86 if (v4_database.get()) { |
| 87 v4_database->db_task_runner_->DeleteSoon(FROM_HERE, v4_database.release()); |
| 88 } |
| 89 } |
| 90 |
| 35 V4Database::~V4Database() {} | 91 V4Database::~V4Database() {} |
| 36 | 92 |
| 37 bool V4Database::ResetDatabase() { | 93 bool V4Database::ResetDatabase() { |
| 38 // TODO(vakh): Delete the stores. Delete the backing files. | 94 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); |
| 39 return true; | 95 bool reset_success = true; |
| 96 for (const auto& store_id_and_store : *store_map_) { |
| 97 if (!store_id_and_store.second->Reset()) { |
| 98 reset_success = false; |
| 99 } |
| 100 } |
| 101 return reset_success; |
| 40 } | 102 } |
| 41 | 103 |
| 42 } // namespace safe_browsing | 104 } // namespace safe_browsing |
| OLD | NEW |