Make IDBDatabaseBackendLevelDB.cpp be cross platform
[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 "IDBCallbacks.h"
30 #include "IDBDatabaseCallbacks.h"
31 #include "IDBMetadata.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 IDBTransactionBackendInterface;
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     IDBBackingStoreInterface* backingStore() const;
53
54     static const int64_t InvalidId = 0;
55     int64_t id() const { return m_metadata.id; }
56     void addObjectStore(const IDBObjectStoreMetadata&, int64_t newMaxObjectStoreId);
57     void removeObjectStore(int64_t objectStoreId);
58     void addIndex(int64_t objectStoreId, const IDBIndexMetadata&, int64_t newMaxIndexId);
59     void removeIndex(int64_t objectStoreId, int64_t indexId);
60
61     void openConnection(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, int64_t version);
62     void deleteDatabase(PassRefPtr<IDBCallbacks>);
63     const IDBDatabaseMetadata& metadata() const { return m_metadata; }
64
65     // IDBDatabaseBackendInterface
66     virtual void createObjectStore(int64_t transactionId, int64_t objectStoreId, const String& name, const IDBKeyPath&, bool autoIncrement);
67     virtual void deleteObjectStore(int64_t transactionId, int64_t objectStoreId);
68     virtual void createTransaction(int64_t transactionId, PassRefPtr<IDBDatabaseCallbacks>, const Vector<int64_t>& objectStoreIds, unsigned short mode);
69     virtual void close(PassRefPtr<IDBDatabaseCallbacks>);
70
71     virtual void commit(int64_t transactionId);
72     virtual void abort(int64_t transactionId);
73     virtual void abort(int64_t transactionId, PassRefPtr<IDBDatabaseError>);
74
75     virtual void createIndex(int64_t transactionId, int64_t objectStoreId, int64_t indexId, const String& name, const IDBKeyPath&, bool unique, bool multiEntry);
76     virtual void deleteIndex(int64_t transactionId, int64_t objectStoreId, int64_t indexId);
77
78     IDBTransactionCoordinator* transactionCoordinator() const { return m_transactionCoordinator.get(); }
79     void transactionStarted(IDBTransactionBackendInterface*);
80     void transactionFinished(IDBTransactionBackendInterface*);
81     void transactionFinishedAndCompleteFired(IDBTransactionBackendInterface*);
82     void transactionFinishedAndAbortFired(IDBTransactionBackendInterface*);
83
84     virtual void get(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, bool keyOnly, PassRefPtr<IDBCallbacks>) OVERRIDE;
85     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;
86     virtual void setIndexKeys(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBKey> prpPrimaryKey, const Vector<int64_t>& indexIds, const Vector<IndexKeys>&) OVERRIDE;
87     virtual void setIndexesReady(int64_t transactionId, int64_t objectStoreId, const Vector<int64_t>& indexIds) OVERRIDE;
88     virtual void openCursor(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, IndexedDB::CursorDirection, bool keyOnly, TaskType, PassRefPtr<IDBCallbacks>) OVERRIDE;
89     virtual void count(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>) OVERRIDE;
90     virtual void deleteRange(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>) OVERRIDE;
91     virtual void clear(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBCallbacks>) OVERRIDE;
92
93     virtual bool isIDBDatabaseBackendImpl() OVERRIDE { return true; }
94
95     class VersionChangeOperation;
96     class VersionChangeAbortOperation;
97
98 private:
99     IDBDatabaseBackendImpl(const String& name, IDBBackingStoreInterface*, IDBFactoryBackendInterface*, const String& uniqueIdentifier);
100
101     bool openInternal();
102     void runIntVersionChangeTransaction(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, int64_t requestedVersion);
103     size_t connectionCount();
104     void processPendingCalls();
105
106     bool isDeleteDatabaseBlocked();
107     void deleteDatabaseFinal(PassRefPtr<IDBCallbacks>);
108
109     RefPtr<IDBBackingStoreInterface> m_backingStore;
110     IDBDatabaseMetadata m_metadata;
111
112     String m_identifier;
113     // 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 conservitive than sorry.
114     RefPtr<IDBFactoryBackendInterface> m_factory;
115
116     OwnPtr<IDBTransactionCoordinator> m_transactionCoordinator;
117     RefPtr<IDBTransactionBackendInterface> m_runningVersionChangeTransaction;
118
119     typedef HashMap<int64_t, IDBTransactionBackendInterface*> TransactionMap;
120     TransactionMap m_transactions;
121
122     class PendingOpenCall {
123     public:
124         static PassOwnPtr<PendingOpenCall> create(PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBDatabaseCallbacks> databaseCallbacks, int64_t transactionId, int64_t version)
125         {
126             return adoptPtr(new PendingOpenCall(callbacks, databaseCallbacks, transactionId, version));
127         }
128         IDBCallbacks* callbacks() { return m_callbacks.get(); }
129         IDBDatabaseCallbacks* databaseCallbacks() { return m_databaseCallbacks.get(); }
130         int64_t version() { return m_version; }
131         int64_t transactionId() const { return m_transactionId; }
132
133     private:
134         PendingOpenCall(PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBDatabaseCallbacks> databaseCallbacks, int64_t transactionId, int64_t version)
135             : m_callbacks(callbacks)
136             , m_databaseCallbacks(databaseCallbacks)
137             , m_version(version)
138             , m_transactionId(transactionId)
139         {
140         }
141         RefPtr<IDBCallbacks> m_callbacks;
142         RefPtr<IDBDatabaseCallbacks> m_databaseCallbacks;
143         int64_t m_version;
144         const int64_t m_transactionId;
145     };
146
147     Deque<OwnPtr<PendingOpenCall>> m_pendingOpenCalls;
148     OwnPtr<PendingOpenCall> m_pendingSecondHalfOpen;
149
150     class PendingDeleteCall {
151     public:
152         static PassOwnPtr<PendingDeleteCall> create(PassRefPtr<IDBCallbacks> callbacks)
153         {
154             return adoptPtr(new PendingDeleteCall(callbacks));
155         }
156         IDBCallbacks* callbacks() { return m_callbacks.get(); }
157
158     private:
159         PendingDeleteCall(PassRefPtr<IDBCallbacks> callbacks)
160             : m_callbacks(callbacks)
161         {
162         }
163         RefPtr<IDBCallbacks> m_callbacks;
164     };
165
166     Deque<OwnPtr<PendingDeleteCall>> m_pendingDeleteCalls;
167
168     typedef ListHashSet<RefPtr<IDBDatabaseCallbacks>> DatabaseCallbacksSet;
169     DatabaseCallbacksSet m_databaseCallbacksSet;
170
171     bool m_closingConnection;
172 };
173
174 } // namespace WebCore
175
176 #endif // ENABLE(INDEXED_DATABASE)
177
178 #endif // IDBDatabaseBackendImpl_h