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 "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)
37
38namespace WebCore {
39
40class IDBBackingStore;
41class IDBDatabase;
42class IDBFactoryBackendImpl;
43class IDBTransactionBackendImpl;
44class IDBTransactionCoordinator;
45
46class IDBDatabaseBackendImpl : public IDBDatabaseBackendInterface {
47public:
48    static PassRefPtr<IDBDatabaseBackendImpl> create(const String& name, IDBBackingStore* database, IDBFactoryBackendImpl*, const String& uniqueIdentifier);
49    virtual ~IDBDatabaseBackendImpl();
50
51    PassRefPtr<IDBBackingStore> 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    IDBTransactionCoordinator* transactionCoordinator() const { return m_transactionCoordinator.get(); }
78    void transactionStarted(PassRefPtr<IDBTransactionBackendImpl>);
79    void transactionFinished(PassRefPtr<IDBTransactionBackendImpl>);
80    void transactionFinishedAndCompleteFired(PassRefPtr<IDBTransactionBackendImpl>);
81    void transactionFinishedAndAbortFired(PassRefPtr<IDBTransactionBackendImpl>);
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
92private:
93    IDBDatabaseBackendImpl(const String& name, IDBBackingStore* database, IDBFactoryBackendImpl*, const String& uniqueIdentifier);
94
95    bool openInternal();
96    void runIntVersionChangeTransaction(PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, int64_t transactionId, int64_t requestedVersion);
97    size_t connectionCount();
98    void processPendingCalls();
99
100    bool isDeleteDatabaseBlocked();
101    void deleteDatabaseFinal(PassRefPtr<IDBCallbacks>);
102
103    class VersionChangeOperation;
104
105    // When a "versionchange" transaction aborts, these restore the back-end object hierarchy.
106    class VersionChangeAbortOperation;
107
108    RefPtr<IDBBackingStore> m_backingStore;
109    IDBDatabaseMetadata m_metadata;
110
111    String m_identifier;
112    // 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.
113    RefPtr<IDBFactoryBackendImpl> m_factory;
114
115    OwnPtr<IDBTransactionCoordinator> m_transactionCoordinator;
116    RefPtr<IDBTransactionBackendImpl> m_runningVersionChangeTransaction;
117
118    typedef HashMap<int64_t, IDBTransactionBackendImpl*> TransactionMap;
119    TransactionMap m_transactions;
120
121    class PendingOpenCall;
122    Deque<OwnPtr<PendingOpenCall> > m_pendingOpenCalls;
123    OwnPtr<PendingOpenCall> m_pendingSecondHalfOpen;
124
125    class PendingDeleteCall;
126    Deque<OwnPtr<PendingDeleteCall> > m_pendingDeleteCalls;
127
128    typedef ListHashSet<RefPtr<IDBDatabaseCallbacks> > DatabaseCallbacksSet;
129    DatabaseCallbacksSet m_databaseCallbacksSet;
130
131    bool m_closingConnection;
132};
133
134} // namespace WebCore
135
136#endif
137
138#endif // IDBDatabaseBackendImpl_h
139