| 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 GetEntry( |
| 43 const std::string& key, |
| 44 const typename ProtoDatabase<T>::GetCallback& callback) override; |
| 42 void Destroy( | 45 void Destroy( |
| 43 const typename ProtoDatabase<T>::DestroyCallback& callback) override; | 46 const typename ProtoDatabase<T>::DestroyCallback& callback) override; |
| 44 | 47 |
| 45 base::FilePath& GetDirectory(); | 48 base::FilePath& GetDirectory(); |
| 46 | 49 |
| 47 void InitCallback(bool success); | 50 void InitCallback(bool success); |
| 48 | 51 |
| 49 void LoadCallback(bool success); | 52 void LoadCallback(bool success); |
| 50 | 53 |
| 54 void GetCallback(bool success); |
| 55 |
| 51 void UpdateCallback(bool success); | 56 void UpdateCallback(bool success); |
| 52 | 57 |
| 53 static base::FilePath DirectoryForTestDB(); | 58 static base::FilePath DirectoryForTestDB(); |
| 54 | 59 |
| 55 private: | 60 private: |
| 56 static void RunLoadCallback( | 61 static void RunLoadCallback( |
| 57 const typename ProtoDatabase<T>::LoadCallback& callback, | 62 const typename ProtoDatabase<T>::LoadCallback& callback, |
| 58 std::unique_ptr<typename std::vector<T>> entries, | 63 std::unique_ptr<typename std::vector<T>> entries, |
| 59 bool success); | 64 bool success); |
| 60 | 65 |
| 66 static void RunGetCallback( |
| 67 const typename ProtoDatabase<T>::GetCallback& callback, |
| 68 std::unique_ptr<T> entry, |
| 69 bool success); |
| 70 |
| 61 base::FilePath dir_; | 71 base::FilePath dir_; |
| 62 EntryMap* db_; | 72 EntryMap* db_; |
| 63 | 73 |
| 64 Callback init_callback_; | 74 Callback init_callback_; |
| 65 Callback load_callback_; | 75 Callback load_callback_; |
| 76 Callback get_callback_; |
| 66 Callback update_callback_; | 77 Callback update_callback_; |
| 67 }; | 78 }; |
| 68 | 79 |
| 69 template <typename T> | 80 template <typename T> |
| 70 FakeDB<T>::FakeDB(EntryMap* db) | 81 FakeDB<T>::FakeDB(EntryMap* db) |
| 71 : db_(db) {} | 82 : db_(db) {} |
| 72 | 83 |
| 73 template <typename T> | 84 template <typename T> |
| 74 FakeDB<T>::~FakeDB() {} | 85 FakeDB<T>::~FakeDB() {} |
| 75 | 86 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 100 const typename ProtoDatabase<T>::LoadCallback& callback) { | 111 const typename ProtoDatabase<T>::LoadCallback& callback) { |
| 101 std::unique_ptr<std::vector<T>> entries(new std::vector<T>()); | 112 std::unique_ptr<std::vector<T>> entries(new std::vector<T>()); |
| 102 for (const auto& pair : *db_) | 113 for (const auto& pair : *db_) |
| 103 entries->push_back(pair.second); | 114 entries->push_back(pair.second); |
| 104 | 115 |
| 105 load_callback_ = | 116 load_callback_ = |
| 106 base::Bind(RunLoadCallback, callback, base::Passed(&entries)); | 117 base::Bind(RunLoadCallback, callback, base::Passed(&entries)); |
| 107 } | 118 } |
| 108 | 119 |
| 109 template <typename T> | 120 template <typename T> |
| 121 void FakeDB<T>::GetEntry( |
| 122 const std::string& key, |
| 123 const typename ProtoDatabase<T>::GetCallback& callback) { |
| 124 std::unique_ptr<T> entry; |
| 125 auto it = db_->find(key); |
| 126 if (it != db_->end()) |
| 127 entry.reset(new T(it->second)); |
| 128 |
| 129 get_callback_ = |
| 130 base::Bind(RunGetCallback, callback, base::Passed(&entry)); |
| 131 } |
| 132 |
| 133 template <typename T> |
| 110 void FakeDB<T>::Destroy( | 134 void FakeDB<T>::Destroy( |
| 111 const typename ProtoDatabase<T>::DestroyCallback& callback) { | 135 const typename ProtoDatabase<T>::DestroyCallback& callback) { |
| 112 } | 136 } |
| 113 | 137 |
| 114 template <typename T> | 138 template <typename T> |
| 115 base::FilePath& FakeDB<T>::GetDirectory() { | 139 base::FilePath& FakeDB<T>::GetDirectory() { |
| 116 return dir_; | 140 return dir_; |
| 117 } | 141 } |
| 118 | 142 |
| 119 template <typename T> | 143 template <typename T> |
| 120 void FakeDB<T>::InitCallback(bool success) { | 144 void FakeDB<T>::InitCallback(bool success) { |
| 121 init_callback_.Run(success); | 145 init_callback_.Run(success); |
| 122 init_callback_.Reset(); | 146 init_callback_.Reset(); |
| 123 } | 147 } |
| 124 | 148 |
| 125 template <typename T> | 149 template <typename T> |
| 126 void FakeDB<T>::LoadCallback(bool success) { | 150 void FakeDB<T>::LoadCallback(bool success) { |
| 127 load_callback_.Run(success); | 151 load_callback_.Run(success); |
| 128 load_callback_.Reset(); | 152 load_callback_.Reset(); |
| 129 } | 153 } |
| 130 | 154 |
| 131 template <typename T> | 155 template <typename T> |
| 156 void FakeDB<T>::GetCallback(bool success) { |
| 157 get_callback_.Run(success); |
| 158 get_callback_.Reset(); |
| 159 } |
| 160 |
| 161 template <typename T> |
| 132 void FakeDB<T>::UpdateCallback(bool success) { | 162 void FakeDB<T>::UpdateCallback(bool success) { |
| 133 update_callback_.Run(success); | 163 update_callback_.Run(success); |
| 134 update_callback_.Reset(); | 164 update_callback_.Reset(); |
| 135 } | 165 } |
| 136 | 166 |
| 137 // static | 167 // static |
| 138 template <typename T> | 168 template <typename T> |
| 139 void FakeDB<T>::RunLoadCallback( | 169 void FakeDB<T>::RunLoadCallback( |
| 140 const typename ProtoDatabase<T>::LoadCallback& callback, | 170 const typename ProtoDatabase<T>::LoadCallback& callback, |
| 141 std::unique_ptr<typename std::vector<T>> entries, | 171 std::unique_ptr<typename std::vector<T>> entries, |
| 142 bool success) { | 172 bool success) { |
| 143 callback.Run(success, std::move(entries)); | 173 callback.Run(success, std::move(entries)); |
| 144 } | 174 } |
| 145 | 175 |
| 146 // static | 176 // static |
| 147 template <typename T> | 177 template <typename T> |
| 178 void FakeDB<T>::RunGetCallback( |
| 179 const typename ProtoDatabase<T>::GetCallback& callback, |
| 180 std::unique_ptr<T> entry, |
| 181 bool success) { |
| 182 callback.Run(success, std::move(entry)); |
| 183 } |
| 184 |
| 185 // static |
| 186 template <typename T> |
| 148 base::FilePath FakeDB<T>::DirectoryForTestDB() { | 187 base::FilePath FakeDB<T>::DirectoryForTestDB() { |
| 149 return base::FilePath(FILE_PATH_LITERAL("/fake/path")); | 188 return base::FilePath(FILE_PATH_LITERAL("/fake/path")); |
| 150 } | 189 } |
| 151 | 190 |
| 152 } // namespace test | 191 } // namespace test |
| 153 } // namespace leveldb_proto | 192 } // namespace leveldb_proto |
| 154 | 193 |
| 155 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ | 194 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ |
| OLD | NEW |