Merge IDBTransactionBackendInterface and IDBTransactionBackendImpl
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / IDBDatabaseBackendImpl.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 IDBDatabaseBackendImpl_h
27 #define IDBDatabaseBackendImpl_h
28
29 #include "IDBDatabaseBackendInterface.h"
30 #include "IDBMetadata.h"
31 #include "IDBPendingDeleteCall.h"
32 #include <stdint.h>
33 #include <wtf/Deque.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/ListHashSet.h>
36
37 #if ENABLE(INDEXED_DATABASE)
38
39 namespace WebCore {
40
41 class IDBBackingStoreInterface;
42 class IDBDatabase;
43 class IDBFactoryBackendInterface;
44 class IDBTransactionBackend;
45 class IDBTransactionCoordinator;
46
47 class IDBDatabaseBackendImpl FINAL : public IDBDatabaseBackendInterface {
48 public:
49     static PassRefPtr<IDBDatabaseBackendImpl> create(const String& name, IDBBackingStoreInterface*, IDBFactoryBackendInterface*, const String& uniqueIdentifier);
50     virtual ~IDBDatabaseBackendImpl();
51
52     virtual IDBBackingStoreInterface* backingStore() const OVERRIDE;
53
54     static const int64_t InvalidId = 0;
55     virtual int64_t id() const OVERRIDE { return m_metadata.id; }
56     virtual void addObjectStore(const IDBObjectStoreMetadata&, int64_t newMaxObjectStoreId) OVERRIDE;
57     virtual void removeObjectStore(int64_t objectStoreId) OVERRIDE;
58     virtual void addIndex(int64_t objectStoreId, const IDBIndexMetadata&, int64_t newMaxIndexId) OVERRIDE;
59     virtual void removeIndex(int64_t objectStoreId, int64_t indexId) OVERRIDE;
60
61     void openConnection(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, uint64_t version);
62     void deleteDatabase(PassRefPtr<IDBCallbacks>);
63
64     // IDBDatabaseBackendInterface
65     virtual void createObjectStore(int64_t transactionId, int64_t objectStoreId, const String& name, const IDBKeyPath&, bool autoIncrement);
66     virtual void deleteObjectStore(int64_t transactionId, int64_t objectStoreId);
67     virtual void createTransaction(int64_t transactionId, PassRefPtr<IDBDatabaseCallbacks>, const Vector<int64_t>& objectStoreIds, unsigned short mode);
68     virtual void close(PassRefPtr<IDBDatabaseCallbacks>);
69
70     virtual void commit(int64_t transactionId);
71     virtual void abort(int64_t transactionId);
72     virtual void abort(int64_t transactionId, PassRefPtr<IDBDatabaseError>);
73
74     virtual void createIndex(int64_t transactionId, int64_t objectStoreId, int64_t indexId, const String& name, const IDBKeyPath&, bool unique, bool multiEntry);
75     virtual void deleteIndex(int64_t transactionId, int64_t objectStoreId, int64_t indexId);
76
77     IDBTransactionCoordinator* transactionCoordinator() const { return m_transactionCoordinator.get(); }
78     void transactionStarted(IDBTransactionBackend*);
79     void transactionFinished(IDBTransactionBackend*);
80     void transactionFinishedAndCompleteFired(IDBTransactionBackend*);
81     void transactionFinishedAndAbortFired(IDBTransactionBackend*);
82
83     virtual void get(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, bool keyOnly, PassRefPtr<IDBCallbacks>) OVERRIDE;
84     virtual void put(int64_t transactionId, int64_t objectStoreId, PassRefPtr<SharedBuffer> value, PassRefPtr<IDBKey>, PutMode, PassRefPtr<IDBCallbacks>, const Vector<int64_t>& indexIds, const Vector<IndexKeys>&) OVERRIDE;
85     virtual void setIndexKeys(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBKey> prpPrimaryKey, const Vector<int64_t>& indexIds, const Vector<IndexKeys>&) OVERRIDE;
86     virtual void setIndexesReady(int64_t transactionId, int64_t objectStoreId, const Vector<int64_t>& indexIds) OVERRIDE;
87     virtual void openCursor(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, IndexedDB::CursorDirection, bool keyOnly, TaskType, PassRefPtr<IDBCallbacks>) OVERRIDE;
88     virtual void count(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>) OVERRIDE;
89     virtual void deleteRange(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>) OVERRIDE;
90     virtual void clear(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBCallbacks>) OVERRIDE;
91
92     virtual const IDBDatabaseMetadata& metadata() const OVERRIDE { return m_metadata; }
93     virtual void setCurrentVersion(uint64_t version) OVERRIDE { m_metadata.version = version; }
94
95     virtual bool isIDBDatabaseBackendImpl() OVERRIDE { return true; }
96
97     virtual bool hasPendingSecondHalfOpen() OVERRIDE { return m_pendingSecondHalfOpen; }
98     virtual void setPendingSecondHalfOpen(PassOwnPtr<IDBPendingOpenCall> pendingOpenCall) OVERRIDE { m_pendingSecondHalfOpen = pendingOpenCall; }
99
100     virtual IDBFactoryBackendInterface& factoryBackend() OVERRIDE { return *m_factory; }
101
102     class VersionChangeOperation;
103     class VersionChangeAbortOperation;
104
105 private:
106     IDBDatabaseBackendImpl(const String& name, IDBBackingStoreInterface*, IDBFactoryBackendInterface*, const String& uniqueIdentifier);
107
108     void openConnectionInternal(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, uint64_t version);
109
110     void openInternalAsync();
111     void didOpenInternalAsync(const IDBDatabaseMetadata&, bool success);
112
113     void runIntVersionChangeTransaction(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, int64_t requestedVersion);
114     size_t connectionCount();
115     void processPendingCalls();
116     void processPendingOpenCalls(bool success);
117
118     bool isDeleteDatabaseBlocked();
119     void deleteDatabaseAsync(PassRefPtr<IDBCallbacks>);
120
121     RefPtr<IDBBackingStoreInterface> m_backingStore;
122     IDBDatabaseMetadata m_metadata;
123
124     String m_identifier;
125     // This might not need to be a RefPtr since the factory's lifetime is that of the page group, but it's better to be conservative than sorry.
126     RefPtr<IDBFactoryBackendInterface> m_factory;
127
128     OwnPtr<IDBTransactionCoordinator> m_transactionCoordinator;
129     RefPtr<IDBTransactionBackend> m_runningVersionChangeTransaction;
130
131     typedef HashMap<int64_t, IDBTransactionBackend*> TransactionMap;
132     TransactionMap m_transactions;
133
134     Deque<OwnPtr<IDBPendingOpenCall>> m_pendingOpenCalls;
135     OwnPtr<IDBPendingOpenCall> m_pendingSecondHalfOpen;
136
137     Deque<OwnPtr<IDBPendingDeleteCall>> m_pendingDeleteCalls;
138     HashSet<RefPtr<IDBCallbacks>> m_deleteCallbacksWaitingCompletion;
139
140     typedef ListHashSet<RefPtr<IDBDatabaseCallbacks>> DatabaseCallbacksSet;
141     DatabaseCallbacksSet m_databaseCallbacksSet;
142
143     bool m_closingConnection;
144 };
145
146 } // namespace WebCore
147
148 #endif // ENABLE(INDEXED_DATABASE)
149
150 #endif // IDBDatabaseBackendImpl_h