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/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "components/safe_browsing_db/v4_database.h" | 13 #include "components/safe_browsing_db/v4_database.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 | 15 |
16 using content::BrowserThread; | 16 using content::BrowserThread; |
17 using base::TimeTicks; | 17 using base::TimeTicks; |
18 | 18 |
19 namespace safe_browsing { | 19 namespace safe_browsing { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 const char kV4DatabaseSizeMetric[] = "SafeBrowsing.V4Database.Size"; | 23 const char kV4DatabaseSizeMetric[] = "SafeBrowsing.V4Database.Size"; |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 // static | 27 // static |
28 V4StoreFactory* V4Database::factory_ = NULL; | 28 V4DatabaseFactory* V4Database::db_factory_ = NULL; |
| 29 |
| 30 // static |
| 31 V4StoreFactory* V4Database::store_factory_ = NULL; |
| 32 |
| 33 V4Database* V4DatabaseFactory::Create( |
| 34 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 35 std::unique_ptr<StoreMap> store_map) { |
| 36 return new V4Database(db_task_runner, std::move(store_map)); |
| 37 } |
29 | 38 |
30 // static | 39 // static |
31 void V4Database::Create( | 40 void V4Database::Create( |
32 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 41 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
33 const base::FilePath& base_path, | 42 const base::FilePath& base_path, |
34 const ListInfos& list_infos, | 43 const ListInfos& list_infos, |
35 NewDatabaseReadyCallback new_db_callback) { | 44 NewDatabaseReadyCallback new_db_callback) { |
36 DCHECK(base_path.IsAbsolute()); | 45 DCHECK(base_path.IsAbsolute()); |
37 DCHECK(!list_infos.empty()); | 46 DCHECK(!list_infos.empty()); |
38 | 47 |
39 const scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner = | 48 const scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner = |
40 base::ThreadTaskRunnerHandle::Get(); | 49 base::ThreadTaskRunnerHandle::Get(); |
41 db_task_runner->PostTask( | 50 db_task_runner->PostTask( |
42 FROM_HERE, base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, | 51 FROM_HERE, base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, |
43 base_path, list_infos, callback_task_runner, | 52 base_path, list_infos, callback_task_runner, |
44 new_db_callback, TimeTicks::Now())); | 53 new_db_callback, TimeTicks::Now())); |
45 } | 54 } |
46 | 55 |
47 // static | 56 // static |
48 void V4Database::CreateOnTaskRunner( | 57 void V4Database::CreateOnTaskRunner( |
49 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 58 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
50 const base::FilePath& base_path, | 59 const base::FilePath& base_path, |
51 const ListInfos& list_infos, | 60 const ListInfos& list_infos, |
52 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 61 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
53 NewDatabaseReadyCallback new_db_callback, | 62 NewDatabaseReadyCallback new_db_callback, |
54 const TimeTicks create_start_time) { | 63 const TimeTicks create_start_time) { |
55 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); | 64 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); |
56 | 65 |
57 if (!factory_) { | 66 if (!store_factory_) { |
58 factory_ = new V4StoreFactory(); | 67 store_factory_ = new V4StoreFactory(); |
59 ANNOTATE_LEAKING_OBJECT_PTR(factory_); | 68 ANNOTATE_LEAKING_OBJECT_PTR(store_factory_); |
60 } | 69 } |
61 | 70 |
62 if (!base::CreateDirectory(base_path)) { | 71 if (!base::CreateDirectory(base_path)) { |
63 NOTREACHED(); | 72 NOTREACHED(); |
64 } | 73 } |
65 | 74 |
66 std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>(); | 75 std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>(); |
67 for (const auto& it : list_infos) { | 76 for (const auto& it : list_infos) { |
68 if (!it.fetch_updates()) { | 77 if (!it.fetch_updates()) { |
69 // This list doesn't need to be fetched or stored on disk. | 78 // This list doesn't need to be fetched or stored on disk. |
70 continue; | 79 continue; |
71 } | 80 } |
72 | 81 |
73 const base::FilePath store_path = base_path.AppendASCII(it.filename()); | 82 const base::FilePath store_path = base_path.AppendASCII(it.filename()); |
74 (*store_map)[it.list_id()].reset( | 83 (*store_map)[it.list_id()].reset( |
75 factory_->CreateV4Store(db_task_runner, store_path)); | 84 store_factory_->CreateV4Store(db_task_runner, store_path)); |
| 85 } |
| 86 |
| 87 if (!db_factory_) { |
| 88 db_factory_ = new V4DatabaseFactory(); |
| 89 ANNOTATE_LEAKING_OBJECT_PTR(db_factory_); |
76 } | 90 } |
77 std::unique_ptr<V4Database> v4_database( | 91 std::unique_ptr<V4Database> v4_database( |
78 new V4Database(db_task_runner, std::move(store_map))); | 92 db_factory_->Create(db_task_runner, std::move(store_map))); |
79 | 93 |
80 // Database is done loading, pass it to the new_db_callback on the caller's | 94 // Database is done loading, pass it to the new_db_callback on the caller's |
81 // thread. This would unblock resource loads. | 95 // thread. This would unblock resource loads. |
82 callback_task_runner->PostTask( | 96 callback_task_runner->PostTask( |
83 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database))); | 97 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database))); |
84 | 98 |
85 UMA_HISTOGRAM_TIMES("SafeBrowsing.V4DatabaseOpen.Time", | 99 UMA_HISTOGRAM_TIMES("SafeBrowsing.V4DatabaseOpen.Time", |
86 TimeTicks::Now() - create_start_time); | 100 TimeTicks::Now() - create_start_time); |
87 } | 101 } |
88 | 102 |
89 V4Database::V4Database( | 103 V4Database::V4Database( |
90 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 104 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
91 std::unique_ptr<StoreMap> store_map) | 105 std::unique_ptr<StoreMap> store_map) |
92 : db_task_runner_(db_task_runner), | 106 : store_map_(std::move(store_map)), |
93 store_map_(std::move(store_map)), | 107 db_task_runner_(db_task_runner), |
94 pending_store_updates_(0), | 108 pending_store_updates_(0), |
95 weak_factory_on_io_(this) { | 109 weak_factory_on_io_(this) { |
96 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); | 110 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); |
97 } | 111 } |
98 | 112 |
99 // static | 113 // static |
100 void V4Database::Destroy(std::unique_ptr<V4Database> v4_database) { | 114 void V4Database::Destroy(std::unique_ptr<V4Database> v4_database) { |
101 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 115 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
102 V4Database* v4_database_raw = v4_database.release(); | 116 V4Database* v4_database_raw = v4_database.release(); |
103 if (v4_database_raw) { | 117 if (v4_database_raw) { |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 filename_(filename), | 276 filename_(filename), |
263 list_id_(list_id), | 277 list_id_(list_id), |
264 sb_threat_type_(sb_threat_type) { | 278 sb_threat_type_(sb_threat_type) { |
265 DCHECK(!fetch_updates_ || !filename_.empty()); | 279 DCHECK(!fetch_updates_ || !filename_.empty()); |
266 DCHECK_NE(SB_THREAT_TYPE_SAFE, sb_threat_type_); | 280 DCHECK_NE(SB_THREAT_TYPE_SAFE, sb_threat_type_); |
267 } | 281 } |
268 | 282 |
269 ListInfo::~ListInfo() {} | 283 ListInfo::~ListInfo() {} |
270 | 284 |
271 } // namespace safe_browsing | 285 } // namespace safe_browsing |
OLD | NEW |