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

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

Issue 2062013002: Fetch incremental updates. Store new state in V4Store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: git fetch && git pull Created 4 years, 6 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/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "components/safe_browsing_db/v4_database.h" 11 #include "components/safe_browsing_db/v4_database.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 13
14 using content::BrowserThread; 14 using content::BrowserThread;
15 15
16 namespace safe_browsing { 16 namespace safe_browsing {
17 17
18 // static 18 // static
19 V4StoreFactory* V4Database::factory_ = NULL; 19 V4StoreFactory* V4Database::factory_ = NULL;
20 20
21 // static 21 // static
22 void V4Database::Create( 22 void V4Database::Create(
23 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 23 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
24 const base::FilePath& base_path, 24 const base::FilePath& base_path,
25 const StoreFileNameMap& store_file_name_map, 25 const StoreFileNameMap& store_file_name_map,
26 NewDatabaseReadyCallback callback) { 26 DatabaseUpdatedCallback db_updated_callback,
27 NewDatabaseReadyCallback new_db_callback) {
27 // Create the database, which may be a lengthy operation, on the 28 // Create the database, which may be a lengthy operation, on the
28 // db_task_runner, but once that is done, call the caller back on this 29 // db_task_runner, but once that is done, call the caller back on this
29 // thread. 30 // thread.
Scott Hess - ex-Googler 2016/06/21 22:23:25 Did this comment float away from the code it refer
vakh (use Gerrit instead) 2016/06/23 06:23:50 This is a comment about what this method does, so
Scott Hess - ex-Googler 2016/06/24 23:01:19 I'd put it on the method itself, since inline like
vakh (use Gerrit instead) 2016/06/27 19:38:27 Done.
31 DCHECK(base_path.IsAbsolute());
32 DCHECK(!store_file_name_map.empty());
33
30 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner = 34 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner =
31 base::MessageLoop::current()->task_runner(); 35 base::MessageLoop::current()->task_runner();
32 db_task_runner->PostTask( 36 db_task_runner->PostTask(
33 FROM_HERE, 37 FROM_HERE, base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner,
34 base::Bind(&V4Database::CreateOnTaskRunner, db_task_runner, base_path, 38 base_path, store_file_name_map, db_updated_callback,
35 store_file_name_map, callback_task_runner, callback)); 39 callback_task_runner, new_db_callback));
36 } 40 }
37 41
38 // static 42 // static
39 void V4Database::CreateOnTaskRunner( 43 void V4Database::CreateOnTaskRunner(
40 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 44 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
41 const base::FilePath& base_path, 45 const base::FilePath& base_path,
42 const StoreFileNameMap& store_file_name_map, 46 const StoreFileNameMap& store_file_name_map,
47 DatabaseUpdatedCallback db_updated_callback,
43 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, 48 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
44 NewDatabaseReadyCallback callback) { 49 NewDatabaseReadyCallback new_db_callback) {
45 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); 50 DCHECK(db_task_runner->RunsTasksOnCurrentThread());
46 DCHECK(base_path.IsAbsolute());
47 51
48 if (!factory_) { 52 if (!factory_) {
49 factory_ = new V4StoreFactory(); 53 factory_ = new V4StoreFactory();
50 ANNOTATE_LEAKING_OBJECT_PTR(factory_); 54 ANNOTATE_LEAKING_OBJECT_PTR(factory_);
51 } 55 }
52 auto store_map = base::MakeUnique<StoreMap>(); 56
57 std::unique_ptr<StoreMap> store_map = base::MakeUnique<StoreMap>();
53 for (const auto& store_info : store_file_name_map) { 58 for (const auto& store_info : store_file_name_map) {
54 const UpdateListIdentifier& update_list_identifier = store_info.first; 59 const UpdateListIdentifier& update_list_identifier = store_info.first;
55 const base::FilePath store_path = base_path.AppendASCII(store_info.second); 60 const base::FilePath store_path = base_path.AppendASCII(store_info.second);
56 (*store_map)[update_list_identifier].reset( 61 (*store_map)[update_list_identifier].reset(
57 factory_->CreateV4Store(db_task_runner, store_path)); 62 factory_->CreateV4Store(db_task_runner, store_path));
58 } 63 }
59 std::unique_ptr<V4Database> v4_database( 64 std::unique_ptr<V4Database> v4_database(new V4Database(
60 new V4Database(db_task_runner, std::move(store_map))); 65 db_task_runner, std::move(store_map), db_updated_callback));
61 66
62 // Database is done loading, pass it to the callback on the caller's thread. 67 // Database is done loading, pass it to the new_db_callback on the caller's
68 // thread.
63 callback_task_runner->PostTask( 69 callback_task_runner->PostTask(
64 FROM_HERE, base::Bind(callback, base::Passed(&v4_database))); 70 FROM_HERE, base::Bind(new_db_callback, base::Passed(&v4_database)));
65 } 71 }
66 72
67 V4Database::V4Database( 73 V4Database::V4Database(
68 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 74 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
69 std::unique_ptr<StoreMap> store_map) 75 std::unique_ptr<StoreMap> store_map,
70 : db_task_runner_(db_task_runner), store_map_(std::move(store_map)) { 76 DatabaseUpdatedCallback db_updated_callback)
77 : db_task_runner_(db_task_runner),
78 store_map_(std::move(store_map)),
79 db_updated_callback_(db_updated_callback),
80 pending_store_updates_(0) {
71 DCHECK(db_task_runner->RunsTasksOnCurrentThread()); 81 DCHECK(db_task_runner->RunsTasksOnCurrentThread());
72 // TODO(vakh): Implement skeleton 82 // TODO(vakh): Implement skeleton
73 } 83 }
74 84
75 // static 85 // static
76 void V4Database::Destroy(std::unique_ptr<V4Database> v4_database) { 86 void V4Database::Destroy(std::unique_ptr<V4Database> v4_database) {
77 V4Database* v4_database_raw = v4_database.release(); 87 V4Database* v4_database_raw = v4_database.release();
78 if (v4_database_raw) { 88 if (v4_database_raw) {
79 v4_database_raw->db_task_runner_->DeleteSoon(FROM_HERE, v4_database_raw); 89 v4_database_raw->db_task_runner_->DeleteSoon(FROM_HERE, v4_database_raw);
80 } 90 }
81 } 91 }
82 92
83 V4Database::~V4Database() { 93 V4Database::~V4Database() {
84 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 94 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
85 } 95 }
86 96
97 void V4Database::ApplyUpdate(const std::vector<ListUpdateResponse>& responses) {
98 DCHECK_CURRENTLY_ON(BrowserThread::IO);
99 DCHECK(!pending_store_updates_);
100
101 // Post the V4Store update task on the task runner but get the callback on the
102 // current thread (IO thread).
Scott Hess - ex-Googler 2016/06/21 22:23:25 IO thread is implied two lines up. I think "curre
vakh (use Gerrit instead) 2016/06/23 06:23:50 Done.
103 const scoped_refptr<base::SingleThreadTaskRunner>& current_task_runner =
104 base::MessageLoop::current()->task_runner();
105 for (const auto& response : responses) {
106 UpdateListIdentifier identifier(response);
107 StoreMap::const_iterator iter = store_map_->find(identifier);
108 if (iter != store_map_->end()) {
109 const std::unique_ptr<V4Store>& old_store = iter->second;
110 if (old_store->state() != response.new_client_state()) {
111 // A different state implies there are updates to process.
112 pending_store_updates_++;
113 UpdatedStoreReadyCallback store_ready_callback = base::Bind(
114 &V4Database::UpdatedStoreReady, base::Unretained(this), identifier);
115 db_task_runner_->PostTask(
116 FROM_HERE,
117 base::Bind(&V4Store::ApplyUpdate, base::Unretained(old_store.get()),
118 response, current_task_runner, store_ready_callback));
119 }
120 } else {
121 NOTREACHED() << "Got update for unexpected identifier: " << identifier;
122 }
123 }
124
125 if (!pending_store_updates_) {
126 current_task_runner->PostTask(FROM_HERE, db_updated_callback_);
127 }
128 }
129
130 void V4Database::UpdatedStoreReady(UpdateListIdentifier identifier,
131 std::unique_ptr<V4Store> new_store) {
132 DCHECK_CURRENTLY_ON(BrowserThread::IO);
133 DCHECK(pending_store_updates_);
134 (*store_map_)[identifier] = std::move(new_store);
135
136 pending_store_updates_--;
137 if (!pending_store_updates_) {
138 db_updated_callback_.Run();
139 }
140 }
141
87 bool V4Database::ResetDatabase() { 142 bool V4Database::ResetDatabase() {
88 DCHECK(db_task_runner_->RunsTasksOnCurrentThread()); 143 DCHECK(db_task_runner_->RunsTasksOnCurrentThread());
89 bool reset_success = true; 144 bool reset_success = true;
90 for (const auto& store_id_and_store : *store_map_) { 145 for (const auto& store_map_iter : *store_map_) {
91 if (!store_id_and_store.second->Reset()) { 146 if (!store_map_iter.second->Reset()) {
92 reset_success = false; 147 reset_success = false;
93 } 148 }
94 } 149 }
95 return reset_success; 150 return reset_success;
96 } 151 }
97 152
153 std::unique_ptr<StoreStateMap> V4Database::GetStoreStateMap() {
154 std::unique_ptr<StoreStateMap> store_state_map =
155 base::MakeUnique<StoreStateMap>();
156 for (const auto& store_map_iter : *store_map_) {
157 (*store_state_map)[store_map_iter.first] = store_map_iter.second->state();
158 }
159 return store_state_map;
160 }
161
98 } // namespace safe_browsing 162 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698