3a050f3572dc88cfbc3ad3eba3ea58281a947b9e
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / leveldb / IDBDatabaseBackendLevelDB.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 IDBDatabaseBackendLevelDB_h
27 #define IDBDatabaseBackendLevelDB_h
28
29 #include "IDBCallbacks.h"
30 #include "IDBMetadata.h"
31 #include <stdint.h>
32 #include <wtf/Deque.h>
33 #include <wtf/HashMap.h>
34 #include <wtf/ListHashSet.h>
35
36 #if ENABLE(INDEXED_DATABASE) && USE(LEVELDB)
37
38 namespace WebCore {
39
40 class IDBBackingStoreInterface;
41 class IDBDatabase;
42 class IDBFactoryBackendInterface;
43 class IDBTransactionBackendInterface;
44 class IDBTransactionCoordinatorLevelDB;
45
46 class IDBDatabaseBackendLevelDB : public IDBDatabaseBackendInterface {
47 public:
48     static PassRefPtr<IDBDatabaseBackendLevelDB> create(const String& name, IDBBackingStoreInterface*, IDBFactoryBackendInterface*, const String& uniqueIdentifier);
49     virtual ~IDBDatabaseBackendLevelDB();
50
51     IDBBackingStoreInterface* backingStore() const;
52
53     static const int64_t InvalidId = 0;
54     int64_t id() const { return m_metadata.id; }
55     void addObjectStore(const IDBObjectStoreMetadata&, int64_t newMaxObjectStoreId);
56     void removeObjectStore(int64_t objectStoreId);
57     void addIndex(int64_t objectStoreId, const IDBIndexMetadata&, int64_t newMaxIndexId);
58     void removeIndex(int64_t objectStoreId, int64_t indexId);
59
60     void openConnection(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, int64_t version);
61     void deleteDatabase(PassRefPtr<IDBCallbacks>);
62     const IDBDatabaseMetadata& metadata() const { return m_metadata; }
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     IDBTransactionCoordinatorLevelDB* transactionCoordinator() const { return m_transactionCoordinator.get(); }
78     void transactionStarted(IDBTransactionBackendInterface*);
79     void transactionFinished(IDBTransactionBackendInterface*);
80     void transactionFinishedAndCompleteFired(IDBTransactionBackendInterface*);
81     void transactionFinishedAndAbortFired(IDBTransactionBackendInterface*);
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     class VersionChangeOperation;
93     class VersionChangeAbortOperation;
94
95 private:
96     IDBDatabaseBackendLevelDB(const String& name, IDBBackingStoreInterface*, IDBFactoryBackendInterface*, const String& uniqueIdentifier);
97
98     bool openInternal();
99     void runIntVersionChangeTransaction(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, int64_t requestedVersion);
100     size_t connectionCount();
101     void processPendingCalls();
102
103     bool isDeleteDatabaseBlocked();
104     void deleteDatabaseFinal(PassRefPtr<IDBCallbacks>);
105
106     RefPtr<IDBBackingStoreInterface> m_backingStore;
107     IDBDatabaseMetadata m_metadata;
108
109     String m_identifier;
110     // 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.
111     RefPtr<IDBFactoryBackendInterface> m_factory;
112
113     OwnPtr<IDBTransactionCoordinatorLevelDB> m_transactionCoordinator;
114     RefPtr<IDBTransactionBackendInterface> m_runningVersionChangeTransaction;
115
116     typedef HashMap<int64_t, IDBTransactionBackendInterface*> TransactionMap;
117     TransactionMap m_transactions;
118
119     class PendingOpenCall {
120     public:
121         static PassOwnPtr<PendingOpenCall> create(PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBDatabaseCallbacks> databaseCallbacks, int64_t transactionId, int64_t version)
122         {
123             return adoptPtr(new PendingOpenCall(callbacks, databaseCallbacks, transactionId, version));
124         }
125         IDBCallbacks* callbacks() { return m_callbacks.get(); }
126         IDBDatabaseCallbacks* databaseCallbacks() { return m_databaseCallbacks.get(); }
127         int64_t version() { return m_version; }
128         int64_t transactionId() const { return m_transactionId; }
129
130     private:
131         PendingOpenCall(PassRefPtr<IDBCallbacks> callbacks, PassRefPtr<IDBDatabaseCallbacks> databaseCallbacks, int64_t transactionId, int64_t version)
132             : m_callbacks(callbacks)
133             , m_databaseCallbacks(databaseCallbacks)
134             , m_version(version)
135             , m_transactionId(transactionId)
136         {
137         }
138         RefPtr<IDBCallbacks> m_callbacks;
139         RefPtr<IDBDatabaseCallbacks> m_databaseCallbacks;
140         int64_t m_version;
141         const int64_t m_transactionId;
142     };
143
144     Deque<OwnPtr<PendingOpenCall>> m_pendingOpenCalls;
145     OwnPtr<PendingOpenCall> m_pendingSecondHalfOpen;
146
147     class PendingDeleteCall {
148     public:
149         static PassOwnPtr<PendingDeleteCall> create(PassRefPtr<IDBCallbacks> callbacks)
150         {
151             return adoptPtr(new PendingDeleteCall(callbacks));
152         }
153         IDBCallbacks* callbacks() { return m_callbacks.get(); }
154
155     private:
156         PendingDeleteCall(PassRefPtr<IDBCallbacks> callbacks)
157             : m_callbacks(callbacks)
158         {
159         }
160         RefPtr<IDBCallbacks> m_callbacks;
161     };
162
163     Deque<OwnPtr<PendingDeleteCall>> m_pendingDeleteCalls;
164
165     typedef ListHashSet<RefPtr<IDBDatabaseCallbacks>> DatabaseCallbacksSet;
166     DatabaseCallbacksSet m_databaseCallbacksSet;
167
168     bool m_closingConnection;
169 };
170
171 } // namespace WebCore
172
173 #endif // ENABLE(INDEXED_DATABASE) && USE(LEVELDB)
174
175 #endif // IDBDatabaseBackendLevelDB_h