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 IDBTransactionBackendImpl_h
27#define IDBTransactionBackendImpl_h
28
29#if ENABLE(INDEXED_DATABASE)
30
31#include "IDBBackingStore.h"
32#include "IDBDatabaseBackendInterface.h"
33#include "IDBDatabaseError.h"
34#include "Timer.h"
35#include <wtf/Deque.h>
36#include <wtf/HashSet.h>
37#include <wtf/RefPtr.h>
38
39namespace WebCore {
40
41class IDBDatabaseBackendImpl;
42class IDBCursorBackendImpl;
43class IDBDatabaseCallbacks;
44
45class IDBTransactionBackendImpl : public RefCounted<IDBTransactionBackendImpl> {
46public:
47    static PassRefPtr<IDBTransactionBackendImpl> create(int64_t transactionId, PassRefPtr<IDBDatabaseCallbacks>, const Vector<int64_t>&, IndexedDB::TransactionMode, IDBDatabaseBackendImpl*);
48    virtual ~IDBTransactionBackendImpl();
49
50    virtual void abort();
51    void commit();
52
53    class Operation {
54    public:
55        virtual ~Operation() { }
56        virtual void perform(IDBTransactionBackendImpl*) = 0;
57    };
58
59    void abort(PassRefPtr<IDBDatabaseError>);
60    void run();
61    IndexedDB::TransactionMode mode() const { return m_mode; }
62    const HashSet<int64_t>& scope() const { return m_objectStoreIds; }
63    void scheduleTask(PassOwnPtr<Operation> task, PassOwnPtr<Operation> abortTask = nullptr) { scheduleTask(IDBDatabaseBackendInterface::NormalTask, task, abortTask); }
64    void scheduleTask(IDBDatabaseBackendInterface::TaskType, PassOwnPtr<Operation>, PassOwnPtr<Operation> abortTask = nullptr);
65    void registerOpenCursor(IDBCursorBackendImpl*);
66    void unregisterOpenCursor(IDBCursorBackendImpl*);
67    void addPreemptiveEvent() { m_pendingPreemptiveEvents++; }
68    void didCompletePreemptiveEvent() { m_pendingPreemptiveEvents--; ASSERT(m_pendingPreemptiveEvents >= 0); }
69    IDBBackingStore::Transaction* backingStoreTransaction() { return &m_transaction; }
70    int64_t id() const { return m_id; }
71
72    IDBDatabaseBackendImpl* database() const { return m_database.get(); }
73
74private:
75    IDBTransactionBackendImpl(int64_t id, PassRefPtr<IDBDatabaseCallbacks>, const HashSet<int64_t>& objectStoreIds, IndexedDB::TransactionMode, IDBDatabaseBackendImpl*);
76
77    enum State {
78        Unused, // Created, but no tasks yet.
79        StartPending, // Enqueued tasks, but backing store transaction not yet started.
80        Running, // Backing store transaction started but not yet finished.
81        Finished, // Either aborted or committed.
82    };
83
84    void start();
85
86    bool isTaskQueueEmpty() const;
87    bool hasPendingTasks() const;
88
89    void taskTimerFired(Timer<IDBTransactionBackendImpl>*);
90    void closeOpenCursors();
91
92    const int64_t m_id;
93    const HashSet<int64_t> m_objectStoreIds;
94    const IndexedDB::TransactionMode m_mode;
95
96    State m_state;
97    bool m_commitPending;
98    RefPtr<IDBDatabaseCallbacks> m_callbacks;
99    RefPtr<IDBDatabaseBackendImpl> m_database;
100
101    typedef Deque<OwnPtr<Operation> > TaskQueue;
102    TaskQueue m_taskQueue;
103    TaskQueue m_preemptiveTaskQueue;
104    TaskQueue m_abortTaskQueue;
105
106    IDBBackingStore::Transaction m_transaction;
107
108    // FIXME: delete the timer once we have threads instead.
109    Timer<IDBTransactionBackendImpl> m_taskTimer;
110    int m_pendingPreemptiveEvents;
111
112    HashSet<IDBCursorBackendImpl*> m_openCursors;
113};
114
115} // namespace WebCore
116
117#endif // ENABLE(INDEXED_DATABASE)
118
119#endif // IDBTransactionBackendImpl_h
120