Misc clean up in browser_shutdown.cc.
[chromium-blink-merge.git] / components / leveldb_proto / testing / fake_db.h
blobe5cd177e85625d85f2a62cb63bcdf37275547b40
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
6 #define COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "components/leveldb_proto/proto_database.h"
17 namespace leveldb_proto {
18 namespace test {
20 template <typename T>
21 class FakeDB : public ProtoDatabase<T> {
22 using Callback = base::Callback<void(bool)>;
24 public:
25 using EntryMap = typename base::hash_map<std::string, T>;
27 explicit FakeDB(EntryMap* db);
28 ~FakeDB() override;
30 // ProtoDatabase implementation.
31 void Init(const base::FilePath& database_dir,
32 const typename ProtoDatabase<T>::InitCallback& callback) override;
33 void UpdateEntries(
34 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save,
35 scoped_ptr<std::vector<std::string>> keys_to_remove,
36 const typename ProtoDatabase<T>::UpdateCallback& callback) override;
37 void LoadEntries(
38 const typename ProtoDatabase<T>::LoadCallback& callback) override;
40 base::FilePath& GetDirectory();
42 void InitCallback(bool success);
44 void LoadCallback(bool success);
46 void UpdateCallback(bool success);
48 static base::FilePath DirectoryForTestDB();
50 private:
51 static void RunLoadCallback(
52 const typename ProtoDatabase<T>::LoadCallback& callback,
53 scoped_ptr<typename std::vector<T>> entries,
54 bool success);
56 base::FilePath dir_;
57 EntryMap* db_;
59 Callback init_callback_;
60 Callback load_callback_;
61 Callback update_callback_;
64 template <typename T>
65 FakeDB<T>::FakeDB(EntryMap* db)
66 : db_(db) {}
68 template <typename T>
69 FakeDB<T>::~FakeDB() {}
71 template <typename T>
72 void FakeDB<T>::Init(const base::FilePath& database_dir,
73 const typename ProtoDatabase<T>::InitCallback& callback) {
74 dir_ = database_dir;
75 init_callback_ = callback;
78 template <typename T>
79 void FakeDB<T>::UpdateEntries(
80 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save,
81 scoped_ptr<std::vector<std::string>> keys_to_remove,
82 const typename ProtoDatabase<T>::UpdateCallback& callback) {
83 for (const auto& pair : *entries_to_save)
84 (*db_)[pair.first] = pair.second;
86 for (const auto& key : *keys_to_remove)
87 db_->erase(key);
89 update_callback_ = callback;
92 template <typename T>
93 void FakeDB<T>::LoadEntries(
94 const typename ProtoDatabase<T>::LoadCallback& callback) {
95 scoped_ptr<std::vector<T>> entries(new std::vector<T>());
96 for (const auto& pair : *db_)
97 entries->push_back(pair.second);
99 load_callback_ =
100 base::Bind(RunLoadCallback, callback, base::Passed(&entries));
103 template <typename T>
104 base::FilePath& FakeDB<T>::GetDirectory() {
105 return dir_;
108 template <typename T>
109 void FakeDB<T>::InitCallback(bool success) {
110 init_callback_.Run(success);
111 init_callback_.Reset();
114 template <typename T>
115 void FakeDB<T>::LoadCallback(bool success) {
116 load_callback_.Run(success);
117 load_callback_.Reset();
120 template <typename T>
121 void FakeDB<T>::UpdateCallback(bool success) {
122 update_callback_.Run(success);
123 update_callback_.Reset();
126 // static
127 template <typename T>
128 void FakeDB<T>::RunLoadCallback(
129 const typename ProtoDatabase<T>::LoadCallback& callback,
130 scoped_ptr<typename std::vector<T>> entries,
131 bool success) {
132 callback.Run(success, entries.Pass());
135 // static
136 template <typename T>
137 base::FilePath FakeDB<T>::DirectoryForTestDB() {
138 return base::FilePath(FILE_PATH_LITERAL("/fake/path"));
141 } // namespace test
142 } // namespace leveldb_proto
144 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_