1 // Copyright 2016 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_DATABASE_IMPL_H_
6 #define CONTENT_BROWSER_INDEXED_DB_DATABASE_IMPL_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/sequence_checker.h"
14 #include "base/strings/string16.h"
15 #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
16 #include "mojo/public/cpp/bindings/pending_associated_remote.h"
17 #include "third_party/blink/public/common/indexeddb/indexeddb_key.h"
18 #include "third_party/blink/public/common/indexeddb/indexeddb_key_path.h"
19 #include "third_party/blink/public/mojom/indexeddb/indexeddb.mojom.h"
20 #include "url/origin.h"
21 
22 namespace base {
23 class SequencedTaskRunner;
24 }
25 
26 namespace blink {
27 class IndexedDBKeyRange;
28 }
29 
30 namespace content {
31 class IndexedDBConnection;
32 class IndexedDBContextImpl;
33 class IndexedDBDispatcherHost;
34 
35 class DatabaseImpl : public blink::mojom::IDBDatabase {
36  public:
37   explicit DatabaseImpl(std::unique_ptr<IndexedDBConnection> connection,
38                         const url::Origin& origin,
39                         IndexedDBDispatcherHost* dispatcher_host,
40                         scoped_refptr<base::SequencedTaskRunner> idb_runner);
41   ~DatabaseImpl() override;
42 
43   // blink::mojom::IDBDatabase implementation
44   void RenameObjectStore(int64_t transaction_id,
45                          int64_t object_store_id,
46                          const base::string16& new_name) override;
47   void CreateTransaction(
48       mojo::PendingAssociatedReceiver<blink::mojom::IDBTransaction>
49           transaction_receiver,
50       int64_t transaction_id,
51       const std::vector<int64_t>& object_store_ids,
52       blink::mojom::IDBTransactionMode mode,
53       blink::mojom::IDBTransactionDurability durability) override;
54   void Close() override;
55   void VersionChangeIgnored() override;
56   void AddObserver(int64_t transaction_id,
57                    int32_t observer_id,
58                    bool include_transaction,
59                    bool no_records,
60                    bool values,
61                    uint32_t operation_types) override;
62   void RemoveObservers(const std::vector<int32_t>& observers) override;
63   void Get(int64_t transaction_id,
64            int64_t object_store_id,
65            int64_t index_id,
66            const blink::IndexedDBKeyRange& key_range,
67            bool key_only,
68            blink::mojom::IDBDatabase::GetCallback callback) override;
69   void GetAll(int64_t transaction_id,
70               int64_t object_store_id,
71               int64_t index_id,
72               const blink::IndexedDBKeyRange& key_range,
73               bool key_only,
74               int64_t max_count,
75               blink::mojom::IDBDatabase::GetAllCallback callback) override;
76   void SetIndexKeys(
77       int64_t transaction_id,
78       int64_t object_store_id,
79       const blink::IndexedDBKey& primary_key,
80       const std::vector<blink::IndexedDBIndexKeys>& index_keys) override;
81   void SetIndexesReady(int64_t transaction_id,
82                        int64_t object_store_id,
83                        const std::vector<int64_t>& index_ids) override;
84   void OpenCursor(
85       int64_t transaction_id,
86       int64_t object_store_id,
87       int64_t index_id,
88       const blink::IndexedDBKeyRange& key_range,
89       blink::mojom::IDBCursorDirection direction,
90       bool key_only,
91       blink::mojom::IDBTaskType task_type,
92       blink::mojom::IDBDatabase::OpenCursorCallback callback) override;
93   void Count(int64_t transaction_id,
94              int64_t object_store_id,
95              int64_t index_id,
96              const blink::IndexedDBKeyRange& key_range,
97              mojo::PendingAssociatedRemote<blink::mojom::IDBCallbacks>
98                  pending_callbacks) override;
99   void DeleteRange(int64_t transaction_id,
100                    int64_t object_store_id,
101                    const blink::IndexedDBKeyRange& key_range,
102                    mojo::PendingAssociatedRemote<blink::mojom::IDBCallbacks>
103                        pending_callbacks) override;
104   void GetKeyGeneratorCurrentNumber(
105       int64_t transaction_id,
106       int64_t object_store_id,
107       mojo::PendingAssociatedRemote<blink::mojom::IDBCallbacks>
108           pending_callbacks) override;
109   void Clear(int64_t transaction_id,
110              int64_t object_store_id,
111              mojo::PendingAssociatedRemote<blink::mojom::IDBCallbacks>
112                  pending_callbacks) override;
113   void CreateIndex(int64_t transaction_id,
114                    int64_t object_store_id,
115                    int64_t index_id,
116                    const base::string16& name,
117                    const blink::IndexedDBKeyPath& key_path,
118                    bool unique,
119                    bool multi_entry) override;
120   void DeleteIndex(int64_t transaction_id,
121                    int64_t object_store_id,
122                    int64_t index_id) override;
123   void RenameIndex(int64_t transaction_id,
124                    int64_t object_store_id,
125                    int64_t index_id,
126                    const base::string16& new_name) override;
127   void Abort(int64_t transaction_id) override;
128 
129  private:
130   // This raw pointer is safe because all DatabaseImpl instances are owned by
131   // an IndexedDBDispatcherHost.
132   IndexedDBDispatcherHost* dispatcher_host_;
133   scoped_refptr<IndexedDBContextImpl> indexed_db_context_;
134   std::unique_ptr<IndexedDBConnection> connection_;
135   const url::Origin origin_;
136   scoped_refptr<base::SequencedTaskRunner> idb_runner_;
137 
138   SEQUENCE_CHECKER(sequence_checker_);
139 
140   DISALLOW_COPY_AND_ASSIGN(DatabaseImpl);
141 };
142 
143 }  // namespace content
144 
145 #endif  // CONTENT_BROWSER_INDEXED_DB_DATABASE_IMPL_H_
146