OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ | 5 #ifndef COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ |
6 #define COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ | 6 #define COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 // part of the constructor | 42 // part of the constructor |
43 void Init(const char* client_name, | 43 void Init(const char* client_name, |
44 const base::FilePath& database_dir, | 44 const base::FilePath& database_dir, |
45 const typename ProtoDatabase<T>::InitCallback& callback) override; | 45 const typename ProtoDatabase<T>::InitCallback& callback) override; |
46 void UpdateEntries( | 46 void UpdateEntries( |
47 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | 47 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, |
48 scoped_ptr<KeyVector> keys_to_remove, | 48 scoped_ptr<KeyVector> keys_to_remove, |
49 const typename ProtoDatabase<T>::UpdateCallback& callback) override; | 49 const typename ProtoDatabase<T>::UpdateCallback& callback) override; |
50 void LoadEntries( | 50 void LoadEntries( |
51 const typename ProtoDatabase<T>::LoadCallback& callback) override; | 51 const typename ProtoDatabase<T>::LoadCallback& callback) override; |
| 52 void Destroy( |
| 53 const typename ProtoDatabase<T>::DestroyCallback& callback) override; |
52 | 54 |
53 // Allow callers to provide their own Database implementation. | 55 // Allow callers to provide their own Database implementation. |
54 void InitWithDatabase( | 56 void InitWithDatabase( |
55 scoped_ptr<LevelDB> database, | 57 scoped_ptr<LevelDB> database, |
56 const base::FilePath& database_dir, | 58 const base::FilePath& database_dir, |
57 const typename ProtoDatabase<T>::InitCallback& callback); | 59 const typename ProtoDatabase<T>::InitCallback& callback); |
58 | 60 |
59 private: | 61 private: |
60 base::ThreadChecker thread_checker_; | 62 base::ThreadChecker thread_checker_; |
61 | 63 |
62 // Used to run blocking tasks in-order. | 64 // Used to run blocking tasks in-order. |
63 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 65 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
64 | 66 |
65 scoped_ptr<LevelDB> db_; | 67 scoped_ptr<LevelDB> db_; |
| 68 base::FilePath database_dir_; |
66 | 69 |
67 DISALLOW_COPY_AND_ASSIGN(ProtoDatabaseImpl); | 70 DISALLOW_COPY_AND_ASSIGN(ProtoDatabaseImpl); |
68 }; | 71 }; |
69 | 72 |
70 namespace { | 73 namespace { |
71 | 74 |
72 template <typename T> | 75 template <typename T> |
73 void RunInitCallback(const typename ProtoDatabase<T>::InitCallback& callback, | 76 void RunInitCallback(const typename ProtoDatabase<T>::InitCallback& callback, |
74 const bool* success) { | 77 const bool* success) { |
75 callback.Run(*success); | 78 callback.Run(*success); |
76 } | 79 } |
77 | 80 |
78 template <typename T> | 81 template <typename T> |
79 void RunUpdateCallback( | 82 void RunUpdateCallback( |
80 const typename ProtoDatabase<T>::UpdateCallback& callback, | 83 const typename ProtoDatabase<T>::UpdateCallback& callback, |
81 const bool* success) { | 84 const bool* success) { |
82 callback.Run(*success); | 85 callback.Run(*success); |
83 } | 86 } |
84 | 87 |
85 template <typename T> | 88 template <typename T> |
86 void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback, | 89 void RunLoadCallback(const typename ProtoDatabase<T>::LoadCallback& callback, |
87 const bool* success, | 90 const bool* success, |
88 scoped_ptr<std::vector<T>> entries) { | 91 scoped_ptr<std::vector<T>> entries) { |
89 callback.Run(*success, entries.Pass()); | 92 callback.Run(*success, entries.Pass()); |
90 } | 93 } |
91 | 94 |
| 95 template <typename T> |
| 96 void RunDestroyCallback( |
| 97 const typename ProtoDatabase<T>::DestroyCallback& callback, |
| 98 const bool* success) { |
| 99 callback.Run(*success); |
| 100 } |
| 101 |
92 void InitFromTaskRunner(LevelDB* database, const base::FilePath& database_dir, | 102 void InitFromTaskRunner(LevelDB* database, const base::FilePath& database_dir, |
93 bool* success) { | 103 bool* success) { |
94 DCHECK(success); | 104 DCHECK(success); |
95 | 105 |
96 // TODO(cjhopman): Histogram for database size. | 106 // TODO(cjhopman): Histogram for database size. |
97 *success = database->Init(database_dir); | 107 *success = database->Init(database_dir); |
98 } | 108 } |
99 | 109 |
| 110 void DestroyFromTaskRunner(const base::FilePath& database_dir, bool* success) { |
| 111 CHECK(success); |
| 112 |
| 113 *success = LevelDB::Destroy(database_dir); |
| 114 } |
| 115 |
100 template <typename T> | 116 template <typename T> |
101 void UpdateEntriesFromTaskRunner( | 117 void UpdateEntriesFromTaskRunner( |
102 LevelDB* database, | 118 LevelDB* database, |
103 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | 119 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, |
104 scoped_ptr<KeyVector> keys_to_remove, | 120 scoped_ptr<KeyVector> keys_to_remove, |
105 bool* success) { | 121 bool* success) { |
106 DCHECK(success); | 122 DCHECK(success); |
107 | 123 |
108 // Serialize the values from Proto to string before passing on to database. | 124 // Serialize the values from Proto to string before passing on to database. |
109 KeyValueVector pairs_to_save; | 125 KeyValueVector pairs_to_save; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 if (!task_runner_->DeleteSoon(FROM_HERE, db_.release())) | 167 if (!task_runner_->DeleteSoon(FROM_HERE, db_.release())) |
152 DLOG(WARNING) << "Proto database will not be deleted."; | 168 DLOG(WARNING) << "Proto database will not be deleted."; |
153 } | 169 } |
154 | 170 |
155 template <typename T> | 171 template <typename T> |
156 void ProtoDatabaseImpl<T>::Init( | 172 void ProtoDatabaseImpl<T>::Init( |
157 const char* client_name, | 173 const char* client_name, |
158 const base::FilePath& database_dir, | 174 const base::FilePath& database_dir, |
159 const typename ProtoDatabase<T>::InitCallback& callback) { | 175 const typename ProtoDatabase<T>::InitCallback& callback) { |
160 DCHECK(thread_checker_.CalledOnValidThread()); | 176 DCHECK(thread_checker_.CalledOnValidThread()); |
| 177 database_dir_ = database_dir; |
161 InitWithDatabase(make_scoped_ptr(new LevelDB(client_name)), database_dir, | 178 InitWithDatabase(make_scoped_ptr(new LevelDB(client_name)), database_dir, |
162 callback); | 179 callback); |
163 } | 180 } |
164 | 181 |
165 template <typename T> | 182 template <typename T> |
| 183 void ProtoDatabaseImpl<T>::Destroy( |
| 184 const typename ProtoDatabase<T>::DestroyCallback& callback) { |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); |
| 186 DCHECK(db_); |
| 187 DCHECK(!database_dir_.empty()); |
| 188 db_.reset(); |
| 189 bool* success = new bool(false); |
| 190 task_runner_->PostTaskAndReply( |
| 191 FROM_HERE, base::Bind(DestroyFromTaskRunner, database_dir_, success), |
| 192 base::Bind(RunDestroyCallback<T>, callback, base::Owned(success))); |
| 193 } |
| 194 |
| 195 template <typename T> |
166 void ProtoDatabaseImpl<T>::InitWithDatabase( | 196 void ProtoDatabaseImpl<T>::InitWithDatabase( |
167 scoped_ptr<LevelDB> database, | 197 scoped_ptr<LevelDB> database, |
168 const base::FilePath& database_dir, | 198 const base::FilePath& database_dir, |
169 const typename ProtoDatabase<T>::InitCallback& callback) { | 199 const typename ProtoDatabase<T>::InitCallback& callback) { |
170 DCHECK(thread_checker_.CalledOnValidThread()); | 200 DCHECK(thread_checker_.CalledOnValidThread()); |
171 DCHECK(!db_); | 201 DCHECK(!db_); |
172 DCHECK(database); | 202 DCHECK(database); |
173 db_.reset(database.release()); | 203 db_.reset(database.release()); |
174 bool* success = new bool(false); | 204 bool* success = new bool(false); |
175 task_runner_->PostTaskAndReply( | 205 task_runner_->PostTaskAndReply( |
(...skipping 30 matching lines...) Expand all Loading... |
206 task_runner_->PostTaskAndReply( | 236 task_runner_->PostTaskAndReply( |
207 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, | 237 FROM_HERE, base::Bind(LoadEntriesFromTaskRunner<T>, |
208 base::Unretained(db_.get()), entries_ptr, success), | 238 base::Unretained(db_.get()), entries_ptr, success), |
209 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), | 239 base::Bind(RunLoadCallback<T>, callback, base::Owned(success), |
210 base::Passed(&entries))); | 240 base::Passed(&entries))); |
211 } | 241 } |
212 | 242 |
213 } // namespace leveldb_proto | 243 } // namespace leveldb_proto |
214 | 244 |
215 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ | 245 #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_IMPL_H_ |
OLD | NEW |