| 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 <string> | 9 #include <string> |
| 9 #include <utility> | 10 #include <utility> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "components/leveldb_proto/proto_database.h" | 16 #include "components/leveldb_proto/proto_database.h" |
| 17 | 17 |
| 18 namespace leveldb_proto { | 18 namespace leveldb_proto { |
| 19 namespace test { | 19 namespace test { |
| 20 | 20 |
| 21 template <typename T> | 21 template <typename T> |
| 22 class FakeDB : public ProtoDatabase<T> { | 22 class FakeDB : public ProtoDatabase<T> { |
| 23 using Callback = base::Callback<void(bool)>; | 23 using Callback = base::Callback<void(bool)>; |
| 24 | 24 |
| 25 public: | 25 public: |
| 26 using EntryMap = typename base::hash_map<std::string, T>; | 26 using EntryMap = typename base::hash_map<std::string, T>; |
| 27 | 27 |
| 28 explicit FakeDB(EntryMap* db); | 28 explicit FakeDB(EntryMap* db); |
| 29 ~FakeDB() override; | 29 ~FakeDB() override; |
| 30 | 30 |
| 31 // ProtoDatabase implementation. | 31 // ProtoDatabase implementation. |
| 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 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | 36 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> |
| 37 scoped_ptr<std::vector<std::string>> keys_to_remove, | 37 entries_to_save, |
| 38 std::unique_ptr<std::vector<std::string>> keys_to_remove, |
| 38 const typename ProtoDatabase<T>::UpdateCallback& callback) override; | 39 const typename ProtoDatabase<T>::UpdateCallback& callback) override; |
| 39 void LoadEntries( | 40 void LoadEntries( |
| 40 const typename ProtoDatabase<T>::LoadCallback& callback) override; | 41 const typename ProtoDatabase<T>::LoadCallback& callback) override; |
| 41 void Destroy( | 42 void Destroy( |
| 42 const typename ProtoDatabase<T>::DestroyCallback& callback) override; | 43 const typename ProtoDatabase<T>::DestroyCallback& callback) override; |
| 43 | 44 |
| 44 base::FilePath& GetDirectory(); | 45 base::FilePath& GetDirectory(); |
| 45 | 46 |
| 46 void InitCallback(bool success); | 47 void InitCallback(bool success); |
| 47 | 48 |
| 48 void LoadCallback(bool success); | 49 void LoadCallback(bool success); |
| 49 | 50 |
| 50 void UpdateCallback(bool success); | 51 void UpdateCallback(bool success); |
| 51 | 52 |
| 52 static base::FilePath DirectoryForTestDB(); | 53 static base::FilePath DirectoryForTestDB(); |
| 53 | 54 |
| 54 private: | 55 private: |
| 55 static void RunLoadCallback( | 56 static void RunLoadCallback( |
| 56 const typename ProtoDatabase<T>::LoadCallback& callback, | 57 const typename ProtoDatabase<T>::LoadCallback& callback, |
| 57 scoped_ptr<typename std::vector<T>> entries, | 58 std::unique_ptr<typename std::vector<T>> entries, |
| 58 bool success); | 59 bool success); |
| 59 | 60 |
| 60 base::FilePath dir_; | 61 base::FilePath dir_; |
| 61 EntryMap* db_; | 62 EntryMap* db_; |
| 62 | 63 |
| 63 Callback init_callback_; | 64 Callback init_callback_; |
| 64 Callback load_callback_; | 65 Callback load_callback_; |
| 65 Callback update_callback_; | 66 Callback update_callback_; |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 template <typename T> | 69 template <typename T> |
| 69 FakeDB<T>::FakeDB(EntryMap* db) | 70 FakeDB<T>::FakeDB(EntryMap* db) |
| 70 : db_(db) {} | 71 : db_(db) {} |
| 71 | 72 |
| 72 template <typename T> | 73 template <typename T> |
| 73 FakeDB<T>::~FakeDB() {} | 74 FakeDB<T>::~FakeDB() {} |
| 74 | 75 |
| 75 template <typename T> | 76 template <typename T> |
| 76 void FakeDB<T>::Init(const char* client_name, | 77 void FakeDB<T>::Init(const char* client_name, |
| 77 const base::FilePath& database_dir, | 78 const base::FilePath& database_dir, |
| 78 const typename ProtoDatabase<T>::InitCallback& callback) { | 79 const typename ProtoDatabase<T>::InitCallback& callback) { |
| 79 dir_ = database_dir; | 80 dir_ = database_dir; |
| 80 init_callback_ = callback; | 81 init_callback_ = callback; |
| 81 } | 82 } |
| 82 | 83 |
| 83 template <typename T> | 84 template <typename T> |
| 84 void FakeDB<T>::UpdateEntries( | 85 void FakeDB<T>::UpdateEntries( |
| 85 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, | 86 std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, |
| 86 scoped_ptr<std::vector<std::string>> keys_to_remove, | 87 std::unique_ptr<std::vector<std::string>> keys_to_remove, |
| 87 const typename ProtoDatabase<T>::UpdateCallback& callback) { | 88 const typename ProtoDatabase<T>::UpdateCallback& callback) { |
| 88 for (const auto& pair : *entries_to_save) | 89 for (const auto& pair : *entries_to_save) |
| 89 (*db_)[pair.first] = pair.second; | 90 (*db_)[pair.first] = pair.second; |
| 90 | 91 |
| 91 for (const auto& key : *keys_to_remove) | 92 for (const auto& key : *keys_to_remove) |
| 92 db_->erase(key); | 93 db_->erase(key); |
| 93 | 94 |
| 94 update_callback_ = callback; | 95 update_callback_ = callback; |
| 95 } | 96 } |
| 96 | 97 |
| 97 template <typename T> | 98 template <typename T> |
| 98 void FakeDB<T>::LoadEntries( | 99 void FakeDB<T>::LoadEntries( |
| 99 const typename ProtoDatabase<T>::LoadCallback& callback) { | 100 const typename ProtoDatabase<T>::LoadCallback& callback) { |
| 100 scoped_ptr<std::vector<T>> entries(new std::vector<T>()); | 101 std::unique_ptr<std::vector<T>> entries(new std::vector<T>()); |
| 101 for (const auto& pair : *db_) | 102 for (const auto& pair : *db_) |
| 102 entries->push_back(pair.second); | 103 entries->push_back(pair.second); |
| 103 | 104 |
| 104 load_callback_ = | 105 load_callback_ = |
| 105 base::Bind(RunLoadCallback, callback, base::Passed(&entries)); | 106 base::Bind(RunLoadCallback, callback, base::Passed(&entries)); |
| 106 } | 107 } |
| 107 | 108 |
| 108 template <typename T> | 109 template <typename T> |
| 109 void FakeDB<T>::Destroy( | 110 void FakeDB<T>::Destroy( |
| 110 const typename ProtoDatabase<T>::DestroyCallback& callback) { | 111 const typename ProtoDatabase<T>::DestroyCallback& callback) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 130 template <typename T> | 131 template <typename T> |
| 131 void FakeDB<T>::UpdateCallback(bool success) { | 132 void FakeDB<T>::UpdateCallback(bool success) { |
| 132 update_callback_.Run(success); | 133 update_callback_.Run(success); |
| 133 update_callback_.Reset(); | 134 update_callback_.Reset(); |
| 134 } | 135 } |
| 135 | 136 |
| 136 // static | 137 // static |
| 137 template <typename T> | 138 template <typename T> |
| 138 void FakeDB<T>::RunLoadCallback( | 139 void FakeDB<T>::RunLoadCallback( |
| 139 const typename ProtoDatabase<T>::LoadCallback& callback, | 140 const typename ProtoDatabase<T>::LoadCallback& callback, |
| 140 scoped_ptr<typename std::vector<T>> entries, | 141 std::unique_ptr<typename std::vector<T>> entries, |
| 141 bool success) { | 142 bool success) { |
| 142 callback.Run(success, std::move(entries)); | 143 callback.Run(success, std::move(entries)); |
| 143 } | 144 } |
| 144 | 145 |
| 145 // static | 146 // static |
| 146 template <typename T> | 147 template <typename T> |
| 147 base::FilePath FakeDB<T>::DirectoryForTestDB() { | 148 base::FilePath FakeDB<T>::DirectoryForTestDB() { |
| 148 return base::FilePath(FILE_PATH_LITERAL("/fake/path")); | 149 return base::FilePath(FILE_PATH_LITERAL("/fake/path")); |
| 149 } | 150 } |
| 150 | 151 |
| 151 } // namespace test | 152 } // namespace test |
| 152 } // namespace leveldb_proto | 153 } // namespace leveldb_proto |
| 153 | 154 |
| 154 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ | 155 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ |
| OLD | NEW |