2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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"
52 struct IDBDatabaseMetadata;
56 typedef RefCountedGarbageCollected<IDBRequest> IDBRequestBase;
58 // Base class to simplify usage of event target refcounting.
59 class IDBRequestBase : public WTF::RefCountedBase {
61 virtual void deref() = 0;
64 virtual ~IDBRequestBase() { }
68 class IDBRequest : public IDBRequestBase, public ScriptWrappable, public EventTargetWithInlineData, public ActiveDOMObject {
69 DEFINE_EVENT_TARGET_REFCOUNTING(IDBRequestBase);
72 static PassRefPtrWillBeRawPtr<IDBRequest> create(ExecutionContext*, PassRefPtrWillBeRawPtr<IDBAny> source, IDBTransaction*);
73 virtual ~IDBRequest();
74 virtual void trace(Visitor*);
76 ScriptValue result(ExceptionState&);
77 PassRefPtrWillBeRawPtr<DOMError> error(ExceptionState&) const;
78 ScriptValue source() const;
79 IDBTransaction* transaction() const { return m_transaction.get(); }
81 bool isResultDirty() const { return m_resultDirty; }
82 PassRefPtrWillBeRawPtr<IDBAny> resultAsAny() const { return m_result; }
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; }
95 const String& readyState() const;
97 DEFINE_ATTRIBUTE_EVENT_LISTENER(success);
98 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
100 void setCursorDetails(IndexedDB::CursorType, blink::WebIDBCursor::Direction);
101 void setPendingCursor(PassRefPtrWillBeRawPtr<IDBCursor>);
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> >);
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(); }
120 virtual bool hasPendingActivity() const OVERRIDE FINAL;
121 virtual void stop() OVERRIDE FINAL;
124 virtual const AtomicString& interfaceName() const OVERRIDE;
125 virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
126 virtual void uncaughtExceptionInEventHandler() OVERRIDE FINAL;
128 using EventTarget::dispatchEvent;
129 virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE;
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();
137 virtual void deref() OVERRIDE FINAL
141 else if (hasOneRef())
142 checkForReferenceCycle();
146 IDBCursor* getResultCursor() const;
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>);
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.
162 void setResultCursor(PassRefPtrWillBeRawPtr<IDBCursor>, PassRefPtrWillBeRawPtr<IDBKey>, PassRefPtrWillBeRawPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer> value, PassOwnPtr<Vector<blink::WebBlobInfo> >);
163 void handleBlobAcks();
165 void checkForReferenceCycle();
168 RefPtr<ScriptState> m_scriptState;
169 RefPtrWillBeMember<IDBAny> m_source;
170 RefPtrWillBeMember<IDBAny> m_result;
171 RefPtrWillBeMember<DOMError> m_error;
173 bool m_hasPendingActivity;
174 WillBeHeapVector<RefPtrWillBeMember<Event> > m_enqueuedEvents;
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;
187 bool m_didFireUpgradeNeededEvent;
188 bool m_preventPropagation;
192 } // namespace WebCore
194 #endif // IDBRequest_h