Don't preload rarely seen large images
[chromium-blink-merge.git] / components / leveldb_proto / testing / fake_db.h
blob2009e1061d79c3d0fdc44a1548756c115b2ac384
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 typedef base::Callback<void(bool)> Callback;
24 public:
25 typedef typename base::hash_map<std::string, T> EntryMap;
27 explicit FakeDB(EntryMap* db);
28 ~FakeDB() override;
30 void Init(const base::FilePath& database_dir,
31 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 typename ProtoDatabase<T>::UpdateCallback callback) override;
38 void LoadEntries(typename ProtoDatabase<T>::LoadCallback callback) override;
39 base::FilePath& GetDirectory();
41 void InitCallback(bool success);
43 void LoadCallback(bool success);
45 void UpdateCallback(bool success);
47 static base::FilePath DirectoryForTestDB();
49 private:
50 static void RunLoadCallback(
51 typename ProtoDatabase<T>::LoadCallback callback,
52 scoped_ptr<typename std::vector<T> > entries,
53 bool success);
55 base::FilePath dir_;
56 EntryMap* db_;
58 Callback init_callback_;
59 Callback load_callback_;
60 Callback update_callback_;
63 template <typename T>
64 FakeDB<T>::FakeDB(EntryMap* db)
65 : db_(db) {}
67 template <typename T>
68 FakeDB<T>::~FakeDB() {}
70 template <typename T>
71 void FakeDB<T>::Init(const base::FilePath& database_dir,
72 typename ProtoDatabase<T>::InitCallback callback) {
73 dir_ = database_dir;
74 init_callback_ = callback;
77 template <typename T>
78 void FakeDB<T>::UpdateEntries(
79 scoped_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save,
80 scoped_ptr<std::vector<std::string> > keys_to_remove,
81 typename ProtoDatabase<T>::UpdateCallback callback) {
82 for (typename ProtoDatabase<T>::KeyEntryVector::iterator it =
83 entries_to_save->begin();
84 it != entries_to_save->end(); ++it) {
85 (*db_)[it->first] = it->second;
87 for (std::vector<std::string>::iterator it = keys_to_remove->begin();
88 it != keys_to_remove->end(); ++it) {
89 (*db_).erase(*it);
91 update_callback_ = callback;
94 template <typename T>
95 void FakeDB<T>::LoadEntries(typename ProtoDatabase<T>::LoadCallback callback) {
96 scoped_ptr<std::vector<T> > entries(new std::vector<T>());
97 for (typename EntryMap::iterator it = db_->begin(); it != db_->end(); ++it) {
98 entries->push_back(it->second);
100 load_callback_ =
101 base::Bind(RunLoadCallback, callback, base::Passed(&entries));
104 template <typename T>
105 base::FilePath& FakeDB<T>::GetDirectory() {
106 return dir_;
109 template <typename T>
110 void FakeDB<T>::InitCallback(bool success) {
111 init_callback_.Run(success);
112 init_callback_.Reset();
115 template <typename T>
116 void FakeDB<T>::LoadCallback(bool success) {
117 load_callback_.Run(success);
118 load_callback_.Reset();
121 template <typename T>
122 void FakeDB<T>::UpdateCallback(bool success) {
123 update_callback_.Run(success);
124 update_callback_.Reset();
127 // static
128 template <typename T>
129 void FakeDB<T>::RunLoadCallback(
130 typename ProtoDatabase<T>::LoadCallback callback,
131 scoped_ptr<typename std::vector<T> > entries, 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_