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

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

Issue 2675063002: Browser tests for using the new SafeBrowsing protocol (v4) (Closed)
Patch Set: Nit: Add comments in tests Created 3 years, 10 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"
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 std::unique_ptr<V4DatabaseFactory> V4Database::db_factory_;
29
30 // static
31 std::unique_ptr<V4StoreFactory> V4Database::store_factory_;
32
33 std::unique_ptr<V4Database> V4DatabaseFactory::Create(
34 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
35 std::unique_ptr<StoreMap> store_map) {
36 // Not using MakeUnique since the constructor of V4Database is protected.
37 return std::unique_ptr<V4Database>(
38 new V4Database(db_task_runner, std::move(store_map)));
39 }
29 40
30 // static 41 // static
31 void V4Database::Create( 42 void V4Database::Create(
32 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 43 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
33 const base::FilePath& base_path, 44 const base::FilePath& base_path,
34 const ListInfos& list_infos, 45 const ListInfos& list_infos,
35 NewDatabaseReadyCallback new_db_callback) { 46 NewDatabaseReadyCallback new_db_callback) {
36 DCHECK(base_path.IsAbsolute()); 47 DCHECK(base_path.IsAbsolute());
37 DCHECK(!list_infos.empty()); 48 DCHECK(!list_infos.empty());
38 49
39 const scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner = 50 const scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner =
40 base::ThreadTaskRunnerHandle::Get(); 51 base::ThreadTaskRunnerHandle::Get();
41 db_task_runner->PostTask( 52 db_task_runner->PostTask(
42 FROM_HERE, base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, 53 FROM_HERE, base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner,
43 base_path, list_infos, callback_task_runner, 54 base_path, list_infos, callback_task_runner,
44 new_db_callback, TimeTicks::Now())); 55 new_db_callback, TimeTicks::Now()));
45 } 56 }
46 57
47 // static 58 // static
48 void V4Database::CreateOnTaskRunner( 59 void V4Database::CreateOnTaskRunner(
49 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 60 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
50 const base::FilePath& base_path, 61 const base::FilePath& base_path,
51 const ListInfos& list_infos, 62 const ListInfos& list_infos,
52 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, 63 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
53 NewDatabaseReadyCallback new_db_callback, 64 NewDatabaseReadyCallback new_db_callback,
54 const TimeTicks create_start_time) { 65 const TimeTicks create_start_time) {
55 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); 66 DCHECK(db_task_runner->RunsTasksOnCurrentThread());
56 67
57 if (!factory_) { 68 if (!store_factory_) {
58 factory_ = new V4StoreFactory(); 69 store_factory_.reset(new V4StoreFactory());
59 ANNOTATE_LEAKING_OBJECT_PTR(factory_);
60 } 70 }
61 71
62 if (!base::CreateDirectory(base_path)) { 72 if (!base::CreateDirectory(base_path)) {
63 NOTREACHED(); 73 NOTREACHED();
64 } 74 }
65 75
66 std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>(); 76 std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>();
67 for (const auto& it : list_infos) { 77 for (const auto& it : list_infos) {
68 if (!it.fetch_updates()) { 78 if (!it.fetch_updates()) {
69 // This list doesn't need to be fetched or stored on disk. 79 // This list doesn't need to be fetched or stored on disk.
70 continue; 80 continue;
71 } 81 }
72 82
73 const base::FilePath store_path = base_path.AppendASCII(it.filename()); 83 const base::FilePath store_path = base_path.AppendASCII(it.filename());
74 (*store_map)[it.list_id()].reset( 84 (*store_map)[it.list_id()].reset(
75 factory_->CreateV4Store(db_task_runner, store_path)); 85 store_factory_->CreateV4Store(db_task_runner, store_path));
86 }
87
88 if (!db_factory_) {
89 db_factory_.reset(new V4DatabaseFactory());
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698