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. 4 5 #ifndef COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ 6 #define COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ 7 8 #include <string> 9 #include <vector> 10 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" 16 17 namespace leveldb_proto { 18 namespace test { 19 20 template <typename T> 21 class FakeDB : public ProtoDatabase<T> { 22 typedef base::Callback<void(bool)> Callback; 23 24 public: 25 typedef typename base::hash_map<std::string, T> EntryMap; 26 27 explicit FakeDB(EntryMap* db); 28 virtual ~FakeDB(); 29 30 virtual void Init(const base::FilePath& database_dir, 31 typename ProtoDatabase<T>::InitCallback callback) 32 OVERRIDE; 33 34 virtual void UpdateEntries( 35 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, 36 scoped_ptr<std::vector<std::string> > keys_to_remove, 37 typename ProtoDatabase<T>::UpdateCallback callback) OVERRIDE; 38 39 virtual void LoadEntries(typename ProtoDatabase<T>::LoadCallback callback) 40 OVERRIDE; 41 base::FilePath& GetDirectory(); 42 43 void InitCallback(bool success); 44 45 void LoadCallback(bool success); 46 47 void UpdateCallback(bool success); 48 49 static base::FilePath DirectoryForTestDB(); 50 51 private: 52 static void RunLoadCallback( 53 typename ProtoDatabase<T>::LoadCallback callback, 54 scoped_ptr<typename std::vector<T> > entries, 55 bool success); 56 57 base::FilePath dir_; 58 EntryMap* db_; 59 60 Callback init_callback_; 61 Callback load_callback_; 62 Callback update_callback_; 63 }; 64 65 template <typename T> 66 FakeDB<T>::FakeDB(EntryMap* db) 67 : db_(db) {} 68 69 template <typename T> 70 FakeDB<T>::~FakeDB() {} 71 72 template <typename T> 73 void FakeDB<T>::Init(const base::FilePath& database_dir, 74 typename ProtoDatabase<T>::InitCallback callback) { 75 dir_ = database_dir; 76 init_callback_ = callback; 77 } 78 79 template <typename T> 80 void FakeDB<T>::UpdateEntries( 81 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save, 82 scoped_ptr<std::vector<std::string> > keys_to_remove, 83 typename ProtoDatabase<T>::UpdateCallback callback) { 84 for (typename ProtoDatabase<T>::KeyEntryVector::iterator it = 85 entries_to_save->begin(); 86 it != entries_to_save->end(); ++it) { 87 (*db_)[it->first] = it->second; 88 } 89 for (std::vector<std::string>::iterator it = keys_to_remove->begin(); 90 it != keys_to_remove->end(); ++it) { 91 (*db_).erase(*it); 92 } 93 update_callback_ = callback; 94 } 95 96 template <typename T> 97 void FakeDB<T>::LoadEntries(typename ProtoDatabase<T>::LoadCallback callback) { 98 scoped_ptr<std::vector<T> > entries(new std::vector<T>()); 99 for (typename EntryMap::iterator it = db_->begin(); it != db_->end(); ++it) { 100 entries->push_back(it->second); 101 } 102 load_callback_ = 103 base::Bind(RunLoadCallback, callback, base::Passed(&entries)); 104 } 105 106 template <typename T> 107 base::FilePath& FakeDB<T>::GetDirectory() { 108 return dir_; 109 } 110 111 template <typename T> 112 void FakeDB<T>::InitCallback(bool success) { 113 init_callback_.Run(success); 114 init_callback_.Reset(); 115 } 116 117 template <typename T> 118 void FakeDB<T>::LoadCallback(bool success) { 119 load_callback_.Run(success); 120 load_callback_.Reset(); 121 } 122 123 template <typename T> 124 void FakeDB<T>::UpdateCallback(bool success) { 125 update_callback_.Run(success); 126 update_callback_.Reset(); 127 } 128 129 // static 130 template <typename T> 131 void FakeDB<T>::RunLoadCallback( 132 typename ProtoDatabase<T>::LoadCallback callback, 133 scoped_ptr<typename std::vector<T> > entries, bool success) { 134 callback.Run(success, entries.Pass()); 135 } 136 137 // static 138 template <typename T> 139 base::FilePath FakeDB<T>::DirectoryForTestDB() { 140 return base::FilePath(FILE_PATH_LITERAL("/fake/path")); 141 } 142 143 } // namespace test 144 } // namespace leveldb_proto 145 146 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_ 147