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/DOMRequestState.h"
33 #include "bindings/v8/ScriptWrappable.h"
34 #include "core/dom/ActiveDOMObject.h"
35 #include "core/dom/DOMError.h"
36 #include "core/dom/DOMStringList.h"
37 #include "core/events/Event.h"
38 #include "core/events/EventListener.h"
39 #include "core/events/EventTarget.h"
40 #include "core/events/ThreadLocalEventNames.h"
41 #include "modules/indexeddb/IDBAny.h"
42 #include "modules/indexeddb/IDBCursor.h"
43 #include "modules/indexeddb/IDBTransaction.h"
44 #include "modules/indexeddb/IndexedDB.h"
45 #include "public/platform/WebIDBCursor.h"
50 struct IDBDatabaseMetadata;
53 // Base class to simplify usage of event target refcounting.
54 class IDBRequestBase : public WTF::RefCountedBase {
56 virtual void deref() = 0;
59 virtual ~IDBRequestBase() { }
62 class IDBRequest : public IDBRequestBase, public ScriptWrappable, public EventTargetWithInlineData, public ActiveDOMObject {
63 DEFINE_EVENT_TARGET_REFCOUNTING(IDBRequestBase);
66 static PassRefPtr<IDBRequest> create(ExecutionContext*, PassRefPtr<IDBAny> source, IDBTransaction*);
67 virtual ~IDBRequest();
69 ScriptValue result(ExceptionState&);
70 PassRefPtr<DOMError> error(ExceptionState&) const;
71 ScriptValue source(ExecutionContext*) const;
72 PassRefPtr<IDBTransaction> transaction() const { return m_transaction; }
74 bool isResultDirty() const { return m_resultDirty; }
75 PassRefPtr<IDBAny> resultAsAny() const { return m_result; }
77 // Requests made during index population are implementation details and so
78 // events should not be visible to script.
79 void preventPropagation() { m_preventPropagation = true; }
88 const String& readyState() const;
90 DEFINE_ATTRIBUTE_EVENT_LISTENER(success);
91 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
93 void setCursorDetails(IndexedDB::CursorType, blink::WebIDBCursor::Direction);
94 void setPendingCursor(PassRefPtr<IDBCursor>);
97 virtual void onError(PassRefPtr<DOMError>);
98 virtual void onSuccess(const Vector<String>&);
99 virtual void onSuccess(PassOwnPtr<blink::WebIDBCursor>, PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer>);
100 virtual void onSuccess(PassRefPtr<IDBKey>);
101 virtual void onSuccess(PassRefPtr<SharedBuffer>);
102 virtual void onSuccess(PassRefPtr<SharedBuffer>, PassRefPtr<IDBKey>, const IDBKeyPath&);
103 virtual void onSuccess(int64_t);
104 virtual void onSuccess();
105 virtual void onSuccess(PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer>);
107 // Only IDBOpenDBRequest instances should receive these:
108 virtual void onBlocked(int64_t oldVersion) { ASSERT_NOT_REACHED(); }
109 virtual void onUpgradeNeeded(int64_t oldVersion, PassOwnPtr<blink::WebIDBDatabase>, const IDBDatabaseMetadata&, blink::WebIDBDataLoss, String dataLossMessage) { ASSERT_NOT_REACHED(); }
110 virtual void onSuccess(PassOwnPtr<blink::WebIDBDatabase>, const IDBDatabaseMetadata&) { ASSERT_NOT_REACHED(); }
113 virtual bool hasPendingActivity() const OVERRIDE FINAL;
114 virtual void stop() OVERRIDE FINAL;
117 virtual const AtomicString& interfaceName() const OVERRIDE;
118 virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
119 virtual void uncaughtExceptionInEventHandler() OVERRIDE FINAL;
121 using EventTarget::dispatchEvent;
122 virtual bool dispatchEvent(PassRefPtr<Event>) OVERRIDE;
124 // Called by a version change transaction that has finished to set this
125 // request back from DONE (following "upgradeneeded") back to PENDING (for
126 // the upcoming "success" or "error").
127 void transactionDidFinishAndDispatch();
129 virtual void deref() OVERRIDE FINAL
133 else if (hasOneRef())
134 checkForReferenceCycle();
137 DOMRequestState* requestState() { return &m_requestState; }
138 IDBCursor* getResultCursor() const;
141 IDBRequest(ExecutionContext*, PassRefPtr<IDBAny> source, IDBTransaction*);
142 void enqueueEvent(PassRefPtr<Event>);
143 void dequeueEvent(Event*);
144 virtual bool shouldEnqueueEvent() const;
145 void onSuccessInternal(PassRefPtr<IDBAny>);
146 void setResult(PassRefPtr<IDBAny>);
148 bool m_contextStopped;
149 RefPtr<IDBTransaction> m_transaction;
150 ReadyState m_readyState;
151 bool m_requestAborted; // May be aborted by transaction then receive async onsuccess; ignore vs. assert.
154 void setResultCursor(PassRefPtr<IDBCursor>, PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer> value);
155 void checkForReferenceCycle();
157 RefPtr<IDBAny> m_source;
158 RefPtr<IDBAny> m_result;
159 RefPtr<DOMError> m_error;
161 bool m_hasPendingActivity;
162 Vector<RefPtr<Event> > m_enqueuedEvents;
164 // Only used if the result type will be a cursor.
165 IndexedDB::CursorType m_cursorType;
166 blink::WebIDBCursor::Direction m_cursorDirection;
167 // When a cursor is continued/advanced, m_result is cleared and m_pendingCursor holds it.
168 RefPtr<IDBCursor> m_pendingCursor;
169 // New state is not applied to the cursor object until the event is dispatched.
170 RefPtr<IDBKey> m_cursorKey;
171 RefPtr<IDBKey> m_cursorPrimaryKey;
172 RefPtr<SharedBuffer> m_cursorValue;
174 bool m_didFireUpgradeNeededEvent;
175 bool m_preventPropagation;
178 DOMRequestState m_requestState;
181 } // namespace WebCore
183 #endif // IDBRequest_h