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 IDBTransaction_h
27#define IDBTransaction_h
28
29#if ENABLE(INDEXED_DATABASE)
30
31#include "ActiveDOMObject.h"
32#include "DOMStringList.h"
33#include "Event.h"
34#include "EventListener.h"
35#include "EventNames.h"
36#include "EventTarget.h"
37#include "IDBTransactionBackendInterface.h"
38#include "IDBTransactionCallbacks.h"
39#include <wtf/RefCounted.h>
40
41namespace WebCore {
42
43class IDBDatabase;
44class IDBObjectStore;
45
46class IDBTransaction : public IDBTransactionCallbacks, public EventTarget, public ActiveDOMObject {
47public:
48    static PassRefPtr<IDBTransaction> create(ScriptExecutionContext*, PassRefPtr<IDBTransactionBackendInterface>, IDBDatabase*);
49    virtual ~IDBTransaction();
50
51    enum Mode {
52        READ_WRITE = 0,
53        READ_ONLY = 1,
54        VERSION_CHANGE = 2
55    };
56
57    IDBTransactionBackendInterface* backend() const;
58    bool finished() const;
59
60    unsigned short mode() const;
61    IDBDatabase* db() const;
62    PassRefPtr<IDBObjectStore> objectStore(const String& name, ExceptionCode&);
63    void abort();
64
65    void registerRequest(IDBRequest*);
66    void unregisterRequest(IDBRequest*);
67
68    DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
69    DEFINE_ATTRIBUTE_EVENT_LISTENER(complete);
70    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
71
72    // IDBTransactionCallbacks
73    virtual void onAbort();
74    virtual void onComplete();
75
76    // EventTarget
77    virtual IDBTransaction* toIDBTransaction() { return this; }
78    virtual ScriptExecutionContext* scriptExecutionContext() const;
79    virtual bool dispatchEvent(PassRefPtr<Event>);
80    bool dispatchEvent(PassRefPtr<Event> event, ExceptionCode& ec) { return EventTarget::dispatchEvent(event, ec); }
81
82    // ActiveDOMObject
83    virtual bool hasPendingActivity() const;
84    virtual bool canSuspend() const;
85    virtual void contextDestroyed();
86
87    using RefCounted<IDBTransactionCallbacks>::ref;
88    using RefCounted<IDBTransactionCallbacks>::deref;
89
90private:
91    IDBTransaction(ScriptExecutionContext*, PassRefPtr<IDBTransactionBackendInterface>, IDBDatabase*);
92
93    void enqueueEvent(PassRefPtr<Event>);
94
95    // EventTarget
96    virtual void refEventTarget() { ref(); }
97    virtual void derefEventTarget() { deref(); }
98    virtual EventTargetData* eventTargetData();
99    virtual EventTargetData* ensureEventTargetData();
100
101    RefPtr<IDBTransactionBackendInterface> m_backend;
102    RefPtr<IDBDatabase> m_database;
103    unsigned short m_mode;
104    bool m_finished; // Is it possible that we'll fire any more events or allow any new transactions? If not, we're finished.
105
106    ListHashSet<IDBRequest*> m_childRequests;
107
108    EventTargetData m_eventTargetData;
109};
110
111} // namespace WebCore
112
113#endif // ENABLE(INDEXED_DATABASE)
114
115#endif // IDBTransaction_h
116