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#include "bindings/core/v8/ScriptState.h"
30#include "core/dom/ActiveDOMObject.h"
31#include "core/dom/DOMError.h"
32#include "core/events/EventListener.h"
33#include "modules/EventModules.h"
34#include "modules/EventTargetModules.h"
35#include "modules/indexeddb/IDBMetadata.h"
36#include "modules/indexeddb/IndexedDB.h"
37#include "platform/heap/Handle.h"
38#include "public/platform/WebIDBDatabase.h"
39#include "public/platform/WebIDBTypes.h"
40#include "wtf/HashSet.h"
41
42namespace blink {
43
44class DOMError;
45class ExceptionState;
46class IDBCursor;
47class IDBDatabase;
48class IDBObjectStore;
49class IDBOpenDBRequest;
50struct IDBObjectStoreMetadata;
51
52class IDBTransaction FINAL
53    : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<IDBTransaction>
54    , public EventTargetWithInlineData
55    , public ActiveDOMObject {
56    DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<IDBTransaction>);
57    DEFINE_WRAPPERTYPEINFO();
58    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(IDBTransaction);
59public:
60    static IDBTransaction* create(ScriptState*, int64_t, const Vector<String>& objectStoreNames, WebIDBTransactionMode, IDBDatabase*);
61    static IDBTransaction* create(ScriptState*, int64_t, IDBDatabase*, IDBOpenDBRequest*, const IDBDatabaseMetadata& previousMetadata);
62    virtual ~IDBTransaction();
63    virtual void trace(Visitor*) OVERRIDE;
64
65    static WebIDBTransactionMode stringToMode(const String&, ExceptionState&);
66
67    // When the connection is closed backend will be 0.
68    WebIDBDatabase* backendDB() const;
69
70    int64_t id() const { return m_id; }
71    bool isActive() const { return m_state == Active; }
72    bool isFinished() const { return m_state == Finished; }
73    bool isFinishing() const { return m_state == Finishing; }
74    bool isReadOnly() const { return m_mode == WebIDBTransactionModeReadOnly; }
75    bool isVersionChange() const { return m_mode == WebIDBTransactionModeVersionChange; }
76
77    // Implement the IDBTransaction IDL
78    const String& mode() const;
79    IDBDatabase* db() const { return m_database.get(); }
80    PassRefPtrWillBeRawPtr<DOMError> error() const { return m_error; }
81    IDBObjectStore* objectStore(const String& name, ExceptionState&);
82    void abort(ExceptionState&);
83
84    void registerRequest(IDBRequest*);
85    void unregisterRequest(IDBRequest*);
86    void objectStoreCreated(const String&, IDBObjectStore*);
87    void objectStoreDeleted(const String&);
88    void setActive(bool);
89    void setError(PassRefPtrWillBeRawPtr<DOMError>);
90
91    DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
92    DEFINE_ATTRIBUTE_EVENT_LISTENER(complete);
93    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
94
95    void onAbort(PassRefPtrWillBeRawPtr<DOMError>);
96    void onComplete();
97
98    // EventTarget
99    virtual const AtomicString& interfaceName() const OVERRIDE;
100    virtual ExecutionContext* executionContext() const OVERRIDE;
101
102    using EventTarget::dispatchEvent;
103    virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE;
104
105    // ActiveDOMObject
106    virtual bool hasPendingActivity() const OVERRIDE;
107    virtual void stop() OVERRIDE;
108
109private:
110    IDBTransaction(ScriptState*, int64_t, const Vector<String>&, WebIDBTransactionMode, IDBDatabase*, IDBOpenDBRequest*, const IDBDatabaseMetadata&);
111
112    void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
113
114    enum State {
115        Inactive, // Created or started, but not in an event callback
116        Active, // Created or started, in creation scope or an event callback
117        Finishing, // In the process of aborting or completing.
118        Finished, // No more events will fire and no new requests may be filed.
119    };
120
121    int64_t m_id;
122    Member<IDBDatabase> m_database;
123    const Vector<String> m_objectStoreNames;
124    Member<IDBOpenDBRequest> m_openDBRequest;
125    const WebIDBTransactionMode m_mode;
126    State m_state;
127    bool m_hasPendingActivity;
128    bool m_contextStopped;
129    RefPtrWillBeMember<DOMError> m_error;
130
131    HeapListHashSet<Member<IDBRequest> > m_requestList;
132
133    typedef HeapHashMap<String, Member<IDBObjectStore> > IDBObjectStoreMap;
134    IDBObjectStoreMap m_objectStoreMap;
135
136    typedef HeapHashSet<Member<IDBObjectStore> > IDBObjectStoreSet;
137    IDBObjectStoreSet m_deletedObjectStores;
138
139    typedef HeapHashMap<Member<IDBObjectStore>, IDBObjectStoreMetadata> IDBObjectStoreMetadataMap;
140    IDBObjectStoreMetadataMap m_objectStoreCleanupMap;
141    IDBDatabaseMetadata m_previousMetadata;
142};
143
144} // namespace blink
145
146#endif // IDBTransaction_h
147