1 // Copyright 2013 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 CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FAKE_BACKING_STORE_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FAKE_BACKING_STORE_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "base/macros.h"
14 #include "content/browser/indexed_db/indexed_db_backing_store.h"
15 #include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
16 
17 namespace base {
18 class SequencedTaskRunner;
19 }
20 
21 namespace blink {
22 class IndexedDBKeyRange;
23 }
24 
25 namespace content {
26 
27 class IndexedDBFakeBackingStore : public IndexedDBBackingStore {
28  public:
29   IndexedDBFakeBackingStore();
30   IndexedDBFakeBackingStore(
31       BlobFilesCleanedCallback blob_files_cleaned,
32       ReportOutstandingBlobsCallback report_outstanding_blobs,
33       scoped_refptr<base::SequencedTaskRunner> task_runner);
34   ~IndexedDBFakeBackingStore() override;
35 
36   leveldb::Status DeleteDatabase(
37       const base::string16& name,
38       TransactionalLevelDBTransaction* transaction) override;
39 
40   leveldb::Status PutRecord(IndexedDBBackingStore::Transaction* transaction,
41                             int64_t database_id,
42                             int64_t object_store_id,
43                             const blink::IndexedDBKey& key,
44                             IndexedDBValue* value,
45                             RecordIdentifier* record) override;
46 
47   leveldb::Status ClearObjectStore(Transaction*,
48                                    int64_t database_id,
49                                    int64_t object_store_id) override;
50   leveldb::Status DeleteRecord(Transaction*,
51                                int64_t database_id,
52                                int64_t object_store_id,
53                                const RecordIdentifier&) override;
54   leveldb::Status GetKeyGeneratorCurrentNumber(
55       Transaction*,
56       int64_t database_id,
57       int64_t object_store_id,
58       int64_t* current_number) override;
59   leveldb::Status MaybeUpdateKeyGeneratorCurrentNumber(
60       Transaction*,
61       int64_t database_id,
62       int64_t object_store_id,
63       int64_t new_number,
64       bool check_current) override;
65   leveldb::Status KeyExistsInObjectStore(
66       Transaction*,
67       int64_t database_id,
68       int64_t object_store_id,
69       const blink::IndexedDBKey&,
70       RecordIdentifier* found_record_identifier,
71       bool* found) override;
72 
73   leveldb::Status ClearIndex(Transaction*,
74                              int64_t database_id,
75                              int64_t object_store_id,
76                              int64_t index_id) override;
77   leveldb::Status PutIndexDataForRecord(Transaction*,
78                                         int64_t database_id,
79                                         int64_t object_store_id,
80                                         int64_t index_id,
81                                         const blink::IndexedDBKey&,
82                                         const RecordIdentifier&) override;
83   void ReportBlobUnused(int64_t database_id, int64_t blob_number) override;
84   std::unique_ptr<Cursor> OpenObjectStoreKeyCursor(
85       Transaction* transaction,
86       int64_t database_id,
87       int64_t object_store_id,
88       const blink::IndexedDBKeyRange& key_range,
89       blink::mojom::IDBCursorDirection,
90       leveldb::Status*) override;
91   std::unique_ptr<Cursor> OpenObjectStoreCursor(
92       Transaction* transaction,
93       int64_t database_id,
94       int64_t object_store_id,
95       const blink::IndexedDBKeyRange& key_range,
96       blink::mojom::IDBCursorDirection,
97       leveldb::Status*) override;
98   std::unique_ptr<Cursor> OpenIndexKeyCursor(
99       Transaction* transaction,
100       int64_t database_id,
101       int64_t object_store_id,
102       int64_t index_id,
103       const blink::IndexedDBKeyRange& key_range,
104       blink::mojom::IDBCursorDirection,
105       leveldb::Status*) override;
106   std::unique_ptr<Cursor> OpenIndexCursor(
107       Transaction* transaction,
108       int64_t database_id,
109       int64_t object_store_id,
110       int64_t index_id,
111       const blink::IndexedDBKeyRange& key_range,
112       blink::mojom::IDBCursorDirection,
113       leveldb::Status*) override;
114 
115   class FakeTransaction : public IndexedDBBackingStore::Transaction {
116    public:
117     FakeTransaction(leveldb::Status phase_two_result,
118                     blink::mojom::IDBTransactionMode mode);
119     explicit FakeTransaction(leveldb::Status phase_two_result);
120     void Begin(std::vector<ScopeLock> locks) override;
121     leveldb::Status CommitPhaseOne(BlobWriteCallback) override;
122     leveldb::Status CommitPhaseTwo() override;
123     uint64_t GetTransactionSize() override;
124     leveldb::Status Rollback() override;
125 
126    private:
127     leveldb::Status result_;
128 
129     DISALLOW_COPY_AND_ASSIGN(FakeTransaction);
130   };
131 
132   std::unique_ptr<IndexedDBBackingStore::Transaction> CreateTransaction(
133       blink::mojom::IDBTransactionDurability durability,
134       blink::mojom::IDBTransactionMode mode) override;
135 
136  protected:
137  private:
138   DISALLOW_COPY_AND_ASSIGN(IndexedDBFakeBackingStore);
139 };
140 
141 }  // namespace content
142 
143 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_FAKE_BACKING_STORE_H_
144