Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / legacy / IDBDatabaseBackend.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef IDBDatabaseBackend_h
27 #define IDBDatabaseBackend_h
28
29 #include "IDBDatabaseCallbacks.h"
30 #include "IDBDatabaseMetadata.h"
31 #include "IDBKeyRange.h"
32 #include "IDBPendingDeleteCall.h"
33 #include "IDBPendingOpenCall.h"
34
35 #include <stdint.h>
36 #include <wtf/Deque.h>
37 #include <wtf/HashMap.h>
38 #include <wtf/ListHashSet.h>
39
40 #if ENABLE(INDEXED_DATABASE)
41
42 namespace WebCore {
43
44 class IDBDatabase;
45 class IDBFactoryBackendInterface;
46 class IDBKey;
47 class IDBKeyPath;
48 class IDBServerConnection;
49 class IDBTransactionBackend;
50 class IDBTransactionCoordinator;
51 class SharedBuffer;
52
53 struct IDBDatabaseMetadata;
54 struct IDBIndexMetadata;
55 struct IDBObjectStoreMetadata;
56
57 typedef Vector<RefPtr<IDBKey>> IndexKeys;
58 typedef int ExceptionCode;
59
60 class IDBDatabaseBackend : public RefCounted<IDBDatabaseBackend> {
61 public:
62     WEBCORE_EXPORT static Ref<IDBDatabaseBackend> create(const String& name, const String& uniqueIdentifier, IDBFactoryBackendInterface*, IDBServerConnection&);
63     WEBCORE_EXPORT ~IDBDatabaseBackend();
64
65     IDBServerConnection& serverConnection() { return m_serverConnection.get(); }
66
67     static const int64_t InvalidId = 0;
68     int64_t id() const { return m_metadata.id; }
69     void addObjectStore(const IDBObjectStoreMetadata&, int64_t newMaxObjectStoreId);
70     void removeObjectStore(int64_t objectStoreId);
71     void addIndex(int64_t objectStoreId, const IDBIndexMetadata&, int64_t newMaxIndexId);
72     void removeIndex(int64_t objectStoreId, int64_t indexId);
73
74     WEBCORE_EXPORT void openConnection(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, uint64_t version);
75     WEBCORE_EXPORT void deleteDatabase(PassRefPtr<IDBCallbacks>);
76
77     // IDBDatabaseBackend
78     void createObjectStore(int64_t transactionId, int64_t objectStoreId, const String& name, const IDBKeyPath&, bool autoIncrement);
79     void deleteObjectStore(int64_t transactionId, int64_t objectStoreId);
80     void createTransaction(int64_t transactionId, PassRefPtr<IDBDatabaseCallbacks>, const Vector<int64_t>& objectStoreIds, IndexedDB::TransactionMode);
81     void close(PassRefPtr<IDBDatabaseCallbacks>);
82
83     void commit(int64_t transactionId);
84     void abort(int64_t transactionId);
85     void abort(int64_t transactionId, PassRefPtr<IDBDatabaseError>);
86
87     void createIndex(int64_t transactionId, int64_t objectStoreId, int64_t indexId, const String& name, const IDBKeyPath&, bool unique, bool multiEntry);
88     void deleteIndex(int64_t transactionId, int64_t objectStoreId, int64_t indexId);
89
90     IDBTransactionCoordinator* transactionCoordinator() const { return m_transactionCoordinator.get(); }
91     void transactionStarted(IDBTransactionBackend*);
92     void transactionFinished(IDBTransactionBackend*);
93     void transactionFinishedAndCompleteFired(IDBTransactionBackend*);
94     void transactionFinishedAndAbortFired(IDBTransactionBackend*);
95
96     enum TaskType {
97         NormalTask = 0,
98         PreemptiveTask
99     };
100
101     enum PutMode {
102         AddOrUpdate,
103         AddOnly,
104         CursorUpdate
105     };
106
107     static const int64_t MinimumIndexId = 30;
108
109     void get(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, bool keyOnly, PassRefPtr<IDBCallbacks>);
110     void put(int64_t transactionId, int64_t objectStoreId, PassRefPtr<SharedBuffer> value, PassRefPtr<IDBKey>, PutMode, PassRefPtr<IDBCallbacks>, const Vector<int64_t>& indexIds, const Vector<IndexKeys>&);
111     void setIndexKeys(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBKey> prpPrimaryKey, const Vector<int64_t, 1>& indexIds, const Vector<IndexKeys, 1>&);
112     void setIndexesReady(int64_t transactionId, int64_t objectStoreId, const Vector<int64_t, 1>& indexIds);
113     void openCursor(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, IndexedDB::CursorDirection, bool keyOnly, TaskType, PassRefPtr<IDBCallbacks>);
114     void count(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>);
115     void deleteRange(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>);
116     void clearObjectStore(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBCallbacks>);
117
118     const IDBDatabaseMetadata& metadata() const { return m_metadata; }
119     void setCurrentVersion(uint64_t version) { m_metadata.version = version; }
120
121     bool hasPendingSecondHalfOpen() { return m_pendingSecondHalfOpen.get(); }
122     void setPendingSecondHalfOpen(std::unique_ptr<IDBPendingOpenCall> pendingOpenCall) { m_pendingSecondHalfOpen = WTFMove(pendingOpenCall); }
123
124     IDBFactoryBackendInterface& factoryBackend() { return *m_factory; }
125
126     class VersionChangeOperation;
127     class VersionChangeAbortOperation;
128
129 private:
130     IDBDatabaseBackend(const String& name, const String& uniqueIdentifier, IDBFactoryBackendInterface*, IDBServerConnection&);
131
132     void openConnectionInternal(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, uint64_t version);
133
134     void openInternalAsync();
135     void didOpenInternalAsync(const IDBDatabaseMetadata&, bool success);
136
137     void runIntVersionChangeTransaction(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, int64_t requestedVersion);
138     size_t connectionCount();
139     void processPendingCalls();
140     void processPendingOpenCalls(bool success);
141
142     bool isDeleteDatabaseBlocked();
143     void deleteDatabaseAsync(PassRefPtr<IDBCallbacks>);
144
145     IDBDatabaseMetadata m_metadata;
146
147     String m_identifier;
148
149     RefPtr<IDBFactoryBackendInterface> m_factory;
150     Ref<IDBServerConnection> m_serverConnection;
151
152     std::unique_ptr<IDBTransactionCoordinator> m_transactionCoordinator;
153     RefPtr<IDBTransactionBackend> m_runningVersionChangeTransaction;
154
155     typedef HashMap<int64_t, IDBTransactionBackend*> TransactionMap;
156     TransactionMap m_transactions;
157
158     Deque<std::unique_ptr<IDBPendingOpenCall>> m_pendingOpenCalls;
159     std::unique_ptr<IDBPendingOpenCall> m_pendingSecondHalfOpen;
160
161     Deque<std::unique_ptr<IDBPendingDeleteCall>> m_pendingDeleteCalls;
162     HashSet<RefPtr<IDBCallbacks>> m_deleteCallbacksWaitingCompletion;
163
164     typedef ListHashSet<RefPtr<IDBDatabaseCallbacks>> DatabaseCallbacksSet;
165     DatabaseCallbacksSet m_databaseCallbacksSet;
166
167     bool m_closingConnection;
168     bool m_didOpenInternal;
169 };
170
171 } // namespace WebCore
172
173 #endif // ENABLE(INDEXED_DATABASE)
174
175 #endif // IDBDatabaseBackend_h