| 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_TESTING_FAKE_DB_H_ | 5 #ifndef COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ |
| 6 #define COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ | 6 #define COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 void Init(const char* client_name, | 32 void Init(const char* client_name, |
| 33 const base::FilePath& database_dir, | 33 const base::FilePath& database_dir, |
| 34 const typename ProtoDatabase<T>::InitCallback& callback) override; | 34 const typename ProtoDatabase<T>::InitCallback& callback) override; |
| 35 void UpdateEntries( | 35 void UpdateEntries( |
| 36 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> | 36 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> |
| 37 entries_to_save, | 37 entries_to_save, |
| 38 std::unique_ptr<std::vector<std::string>> keys_to_remove, | 38 std::unique_ptr<std::vector<std::string>> keys_to_remove, |
| 39 const typename ProtoDatabase<T>::UpdateCallback& callback) override; | 39 const typename ProtoDatabase<T>::UpdateCallback& callback) override; |
| 40 void LoadEntries( | 40 void LoadEntries( |
| 41 const typename ProtoDatabase<T>::LoadCallback& callback) override; | 41 const typename ProtoDatabase<T>::LoadCallback& callback) override; |
| 42 void LoadKeys( |
| 43 const typename ProtoDatabase<T>::LoadKeysCallback& callback) override; |
| 42 void GetEntry( | 44 void GetEntry( |
| 43 const std::string& key, | 45 const std::string& key, |
| 44 const typename ProtoDatabase<T>::GetCallback& callback) override; | 46 const typename ProtoDatabase<T>::GetCallback& callback) override; |
| 45 void Destroy( | 47 void Destroy( |
| 46 const typename ProtoDatabase<T>::DestroyCallback& callback) override; | 48 const typename ProtoDatabase<T>::DestroyCallback& callback) override; |
| 47 | 49 |
| 48 base::FilePath& GetDirectory(); | 50 base::FilePath& GetDirectory(); |
| 49 | 51 |
| 50 void InitCallback(bool success); | 52 void InitCallback(bool success); |
| 51 | 53 |
| 52 void LoadCallback(bool success); | 54 void LoadCallback(bool success); |
| 53 | 55 |
| 56 void LoadKeysCallback(bool success); |
| 57 |
| 54 void GetCallback(bool success); | 58 void GetCallback(bool success); |
| 55 | 59 |
| 56 void UpdateCallback(bool success); | 60 void UpdateCallback(bool success); |
| 57 | 61 |
| 58 static base::FilePath DirectoryForTestDB(); | 62 static base::FilePath DirectoryForTestDB(); |
| 59 | 63 |
| 60 private: | 64 private: |
| 61 static void RunLoadCallback( | 65 static void RunLoadCallback( |
| 62 const typename ProtoDatabase<T>::LoadCallback& callback, | 66 const typename ProtoDatabase<T>::LoadCallback& callback, |
| 63 std::unique_ptr<typename std::vector<T>> entries, | 67 std::unique_ptr<typename std::vector<T>> entries, |
| 64 bool success); | 68 bool success); |
| 65 | 69 |
| 70 static void RunLoadKeysCallback( |
| 71 const typename ProtoDatabase<T>::LoadKeysCallback& callback, |
| 72 std::unique_ptr<std::vector<std::string>> keys, |
| 73 bool success); |
| 74 |
| 66 static void RunGetCallback( | 75 static void RunGetCallback( |
| 67 const typename ProtoDatabase<T>::GetCallback& callback, | 76 const typename ProtoDatabase<T>::GetCallback& callback, |
| 68 std::unique_ptr<T> entry, | 77 std::unique_ptr<T> entry, |
| 69 bool success); | 78 bool success); |
| 70 | 79 |
| 71 base::FilePath dir_; | 80 base::FilePath dir_; |
| 72 EntryMap* db_; | 81 EntryMap* db_; |
| 73 | 82 |
| 74 Callback init_callback_; | 83 Callback init_callback_; |
| 75 Callback load_callback_; | 84 Callback load_callback_; |
| 85 Callback load_keys_callback_; |
| 76 Callback get_callback_; | 86 Callback get_callback_; |
| 77 Callback update_callback_; | 87 Callback update_callback_; |
| 78 }; | 88 }; |
| 79 | 89 |
| 80 template <typename T> | 90 template <typename T> |
| 81 FakeDB<T>::FakeDB(EntryMap* db) | 91 FakeDB<T>::FakeDB(EntryMap* db) |
| 82 : db_(db) {} | 92 : db_(db) {} |
| 83 | 93 |
| 84 template <typename T> | 94 template <typename T> |
| 85 FakeDB<T>::~FakeDB() {} | 95 FakeDB<T>::~FakeDB() {} |
| (...skipping 25 matching lines...) Expand all Loading... |
| 111 const typename ProtoDatabase<T>::LoadCallback& callback) { | 121 const typename ProtoDatabase<T>::LoadCallback& callback) { |
| 112 std::unique_ptr<std::vector<T>> entries(new std::vector<T>()); | 122 std::unique_ptr<std::vector<T>> entries(new std::vector<T>()); |
| 113 for (const auto& pair : *db_) | 123 for (const auto& pair : *db_) |
| 114 entries->push_back(pair.second); | 124 entries->push_back(pair.second); |
| 115 | 125 |
| 116 load_callback_ = | 126 load_callback_ = |
| 117 base::Bind(RunLoadCallback, callback, base::Passed(&entries)); | 127 base::Bind(RunLoadCallback, callback, base::Passed(&entries)); |
| 118 } | 128 } |
| 119 | 129 |
| 120 template <typename T> | 130 template <typename T> |
| 131 void FakeDB<T>::LoadKeys( |
| 132 const typename ProtoDatabase<T>::LoadKeysCallback& callback) { |
| 133 std::unique_ptr<std::vector<std::string>> keys( |
| 134 new std::vector<std::string>()); |
| 135 for (const auto& pair : *db_) |
| 136 keys->push_back(pair.first); |
| 137 |
| 138 load_keys_callback_ = |
| 139 base::Bind(RunLoadKeysCallback, callback, base::Passed(&keys)); |
| 140 } |
| 141 |
| 142 template <typename T> |
| 121 void FakeDB<T>::GetEntry( | 143 void FakeDB<T>::GetEntry( |
| 122 const std::string& key, | 144 const std::string& key, |
| 123 const typename ProtoDatabase<T>::GetCallback& callback) { | 145 const typename ProtoDatabase<T>::GetCallback& callback) { |
| 124 std::unique_ptr<T> entry; | 146 std::unique_ptr<T> entry; |
| 125 auto it = db_->find(key); | 147 auto it = db_->find(key); |
| 126 if (it != db_->end()) | 148 if (it != db_->end()) |
| 127 entry.reset(new T(it->second)); | 149 entry.reset(new T(it->second)); |
| 128 | 150 |
| 129 get_callback_ = base::Bind(RunGetCallback, callback, base::Passed(&entry)); | 151 get_callback_ = base::Bind(RunGetCallback, callback, base::Passed(&entry)); |
| 130 } | 152 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 145 init_callback_.Reset(); | 167 init_callback_.Reset(); |
| 146 } | 168 } |
| 147 | 169 |
| 148 template <typename T> | 170 template <typename T> |
| 149 void FakeDB<T>::LoadCallback(bool success) { | 171 void FakeDB<T>::LoadCallback(bool success) { |
| 150 load_callback_.Run(success); | 172 load_callback_.Run(success); |
| 151 load_callback_.Reset(); | 173 load_callback_.Reset(); |
| 152 } | 174 } |
| 153 | 175 |
| 154 template <typename T> | 176 template <typename T> |
| 177 void FakeDB<T>::LoadKeysCallback(bool success) { |
| 178 load_keys_callback_.Run(success); |
| 179 load_keys_callback_.Reset(); |
| 180 } |
| 181 |
| 182 template <typename T> |
| 155 void FakeDB<T>::GetCallback(bool success) { | 183 void FakeDB<T>::GetCallback(bool success) { |
| 156 get_callback_.Run(success); | 184 get_callback_.Run(success); |
| 157 get_callback_.Reset(); | 185 get_callback_.Reset(); |
| 158 } | 186 } |
| 159 | 187 |
| 160 template <typename T> | 188 template <typename T> |
| 161 void FakeDB<T>::UpdateCallback(bool success) { | 189 void FakeDB<T>::UpdateCallback(bool success) { |
| 162 update_callback_.Run(success); | 190 update_callback_.Run(success); |
| 163 update_callback_.Reset(); | 191 update_callback_.Reset(); |
| 164 } | 192 } |
| 165 | 193 |
| 166 // static | 194 // static |
| 167 template <typename T> | 195 template <typename T> |
| 168 void FakeDB<T>::RunLoadCallback( | 196 void FakeDB<T>::RunLoadCallback( |
| 169 const typename ProtoDatabase<T>::LoadCallback& callback, | 197 const typename ProtoDatabase<T>::LoadCallback& callback, |
| 170 std::unique_ptr<typename std::vector<T>> entries, | 198 std::unique_ptr<typename std::vector<T>> entries, |
| 171 bool success) { | 199 bool success) { |
| 172 callback.Run(success, std::move(entries)); | 200 callback.Run(success, std::move(entries)); |
| 173 } | 201 } |
| 174 | 202 |
| 175 // static | 203 // static |
| 176 template <typename T> | 204 template <typename T> |
| 205 void FakeDB<T>::RunLoadKeysCallback( |
| 206 const typename ProtoDatabase<T>::LoadKeysCallback& callback, |
| 207 std::unique_ptr<std::vector<std::string>> keys, |
| 208 bool success) { |
| 209 callback.Run(success, std::move(keys)); |
| 210 } |
| 211 |
| 212 // static |
| 213 template <typename T> |
| 177 void FakeDB<T>::RunGetCallback( | 214 void FakeDB<T>::RunGetCallback( |
| 178 const typename ProtoDatabase<T>::GetCallback& callback, | 215 const typename ProtoDatabase<T>::GetCallback& callback, |
| 179 std::unique_ptr<T> entry, | 216 std::unique_ptr<T> entry, |
| 180 bool success) { | 217 bool success) { |
| 181 callback.Run(success, std::move(entry)); | 218 callback.Run(success, std::move(entry)); |
| 182 } | 219 } |
| 183 | 220 |
| 184 // static | 221 // static |
| 185 template <typename T> | 222 template <typename T> |
| 186 base::FilePath FakeDB<T>::DirectoryForTestDB() { | 223 base::FilePath FakeDB<T>::DirectoryForTestDB() { |
| 187 return base::FilePath(FILE_PATH_LITERAL("/fake/path")); | 224 return base::FilePath(FILE_PATH_LITERAL("/fake/path")); |
| 188 } | 225 } |
| 189 | 226 |
| 190 } // namespace test | 227 } // namespace test |
| 191 } // namespace leveldb_proto | 228 } // namespace leveldb_proto |
| 192 | 229 |
| 193 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ | 230 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ |
| OLD | NEW |