Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / indexeddb / IDBRequest.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  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef IDBRequest_h
30 #define IDBRequest_h
31
32 #include "bindings/v8/ScriptState.h"
33 #include "bindings/v8/ScriptValue.h"
34 #include "bindings/v8/ScriptWrappable.h"
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/dom/DOMError.h"
37 #include "core/dom/DOMStringList.h"
38 #include "core/events/Event.h"
39 #include "core/events/EventListener.h"
40 #include "core/events/EventTarget.h"
41 #include "modules/indexeddb/IDBAny.h"
42 #include "modules/indexeddb/IDBTransaction.h"
43 #include "modules/indexeddb/IndexedDB.h"
44 #include "platform/heap/Handle.h"
45 #include "public/platform/WebBlobInfo.h"
46 #include "public/platform/WebIDBCursor.h"
47
48 namespace WebCore {
49
50 class ExceptionState;
51 class IDBCursor;
52 struct IDBDatabaseMetadata;
53 class SharedBuffer;
54
55 #if ENABLE(OILPAN)
56 typedef RefCountedGarbageCollected<IDBRequest> IDBRequestBase;
57 #else
58 // Base class to simplify usage of event target refcounting.
59 class IDBRequestBase : public WTF::RefCountedBase {
60 public:
61     virtual void deref() = 0;
62
63 protected:
64     virtual ~IDBRequestBase() { }
65 };
66 #endif
67
68 class IDBRequest : public IDBRequestBase, public ScriptWrappable, public EventTargetWithInlineData, public ActiveDOMObject {
69     DEFINE_EVENT_TARGET_REFCOUNTING(IDBRequestBase);
70
71 public:
72     static PassRefPtrWillBeRawPtr<IDBRequest> create(ExecutionContext*, PassRefPtrWillBeRawPtr<IDBAny> source, IDBTransaction*);
73     virtual ~IDBRequest();
74     virtual void trace(Visitor*);
75
76     ScriptValue result(ExceptionState&);
77     PassRefPtrWillBeRawPtr<DOMError> error(ExceptionState&) const;
78     ScriptValue source() const;
79     IDBTransaction* transaction() const { return m_transaction.get(); }
80
81     bool isResultDirty() const { return m_resultDirty; }
82     PassRefPtrWillBeRawPtr<IDBAny> resultAsAny() const { return m_result; }
83
84     // Requests made during index population are implementation details and so
85     // events should not be visible to script.
86     void preventPropagation() { m_preventPropagation = true; }
87
88     // Defined in the IDL
89     enum ReadyState {
90         PENDING = 1,
91         DONE = 2,
92         EarlyDeath = 3
93     };
94
95     const String& readyState() const;
96
97     DEFINE_ATTRIBUTE_EVENT_LISTENER(success);
98     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
99
100     void setCursorDetails(IndexedDB::CursorType, blink::WebIDBCursor::Direction);
101     void setPendingCursor(PassRefPtrWillBeRawPtr<IDBCursor>);
102     void abort();
103
104     virtual void onError(PassRefPtrWillBeRawPtr<DOMError>);
105     virtual void onSuccess(const Vector<String>&);
106     virtual void onSuccess(PassOwnPtr<blink::WebIDBCursor>, PassRefPtrWillBeRawPtr<IDBKey>, PassRefPtrWillBeRawPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<blink::WebBlobInfo> >);
107     virtual void onSuccess(PassRefPtrWillBeRawPtr<IDBKey>);
108     virtual void onSuccess(PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<blink::WebBlobInfo> >);
109     virtual void onSuccess(PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<blink::WebBlobInfo> >, PassRefPtrWillBeRawPtr<IDBKey>, const IDBKeyPath&);
110     virtual void onSuccess(int64_t);
111     virtual void onSuccess();
112     virtual void onSuccess(PassRefPtrWillBeRawPtr<IDBKey>, PassRefPtrWillBeRawPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<blink::WebBlobInfo> >);
113
114     // Only IDBOpenDBRequest instances should receive these:
115     virtual void onBlocked(int64_t oldVersion) { ASSERT_NOT_REACHED(); }
116     virtual void onUpgradeNeeded(int64_t oldVersion, PassOwnPtr<blink::WebIDBDatabase>, const IDBDatabaseMetadata&, blink::WebIDBDataLoss, String dataLossMessage) { ASSERT_NOT_REACHED(); }
117     virtual void onSuccess(PassOwnPtr<blink::WebIDBDatabase>, const IDBDatabaseMetadata&) { ASSERT_NOT_REACHED(); }
118
119     // ActiveDOMObject
120     virtual bool hasPendingActivity() const OVERRIDE FINAL;
121     virtual void stop() OVERRIDE FINAL;
122
123     // EventTarget
124     virtual const AtomicString& interfaceName() const OVERRIDE;
125     virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
126     virtual void uncaughtExceptionInEventHandler() OVERRIDE FINAL;
127
128     using EventTarget::dispatchEvent;
129     virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE;
130
131     // Called by a version change transaction that has finished to set this
132     // request back from DONE (following "upgradeneeded") back to PENDING (for
133     // the upcoming "success" or "error").
134     void transactionDidFinishAndDispatch();
135
136 #if !ENABLE(OILPAN)
137     virtual void deref() OVERRIDE FINAL
138     {
139         if (derefBase())
140             delete this;
141         else if (hasOneRef())
142             checkForReferenceCycle();
143     }
144 #endif
145
146     IDBCursor* getResultCursor() const;
147
148 protected:
149     IDBRequest(ExecutionContext*, PassRefPtrWillBeRawPtr<IDBAny> source, IDBTransaction*);
150     void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
151     void dequeueEvent(Event*);
152     virtual bool shouldEnqueueEvent() const;
153     void onSuccessInternal(PassRefPtrWillBeRawPtr<IDBAny>);
154     void setResult(PassRefPtrWillBeRawPtr<IDBAny>);
155
156     bool m_contextStopped;
157     RefPtrWillBeMember<IDBTransaction> m_transaction;
158     ReadyState m_readyState;
159     bool m_requestAborted; // May be aborted by transaction then receive async onsuccess; ignore vs. assert.
160
161 private:
162     void setResultCursor(PassRefPtrWillBeRawPtr<IDBCursor>, PassRefPtrWillBeRawPtr<IDBKey>, PassRefPtrWillBeRawPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer> value, PassOwnPtr<Vector<blink::WebBlobInfo> >);
163     void handleBlobAcks();
164 #if !ENABLE(OILPAN)
165     void checkForReferenceCycle();
166 #endif
167
168     RefPtr<ScriptState> m_scriptState;
169     RefPtrWillBeMember<IDBAny> m_source;
170     RefPtrWillBeMember<IDBAny> m_result;
171     RefPtrWillBeMember<DOMError> m_error;
172
173     bool m_hasPendingActivity;
174     WillBeHeapVector<RefPtrWillBeMember<Event> > m_enqueuedEvents;
175
176     // Only used if the result type will be a cursor.
177     IndexedDB::CursorType m_cursorType;
178     blink::WebIDBCursor::Direction m_cursorDirection;
179     // When a cursor is continued/advanced, m_result is cleared and m_pendingCursor holds it.
180     RefPtrWillBeMember<IDBCursor> m_pendingCursor;
181     // New state is not applied to the cursor object until the event is dispatched.
182     RefPtrWillBeMember<IDBKey> m_cursorKey;
183     RefPtrWillBeMember<IDBKey> m_cursorPrimaryKey;
184     RefPtr<SharedBuffer> m_cursorValue;
185     OwnPtr<Vector<blink::WebBlobInfo> > m_blobInfo;
186
187     bool m_didFireUpgradeNeededEvent;
188     bool m_preventPropagation;
189     bool m_resultDirty;
190 };
191
192 } // namespace WebCore
193
194 #endif // IDBRequest_h