Home | History | Annotate | Download | only in indexeddb
      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/EventListener.h"
     39 #include "core/events/EventTarget.h"
     40 #include "modules/EventModules.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 #include "public/platform/WebIDBTypes.h"
     48 
     49 namespace WebCore {
     50 
     51 class ExceptionState;
     52 class IDBCursor;
     53 struct IDBDatabaseMetadata;
     54 class SharedBuffer;
     55 
     56 class IDBRequest
     57     : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<IDBRequest>
     58     , public ScriptWrappable
     59     , public EventTargetWithInlineData
     60     , public ActiveDOMObject {
     61     DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<IDBRequest>);
     62     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(IDBRequest);
     63 public:
     64     static IDBRequest* create(ScriptState*, IDBAny* source, IDBTransaction*);
     65     virtual ~IDBRequest();
     66     virtual void trace(Visitor*) OVERRIDE;
     67 
     68     ScriptValue result(ExceptionState&);
     69     PassRefPtrWillBeRawPtr<DOMError> error(ExceptionState&) const;
     70     ScriptValue source() const;
     71     IDBTransaction* transaction() const { return m_transaction.get(); }
     72 
     73     bool isResultDirty() const { return m_resultDirty; }
     74     IDBAny* resultAsAny() const { return m_result; }
     75 
     76     // Requests made during index population are implementation details and so
     77     // events should not be visible to script.
     78     void preventPropagation() { m_preventPropagation = true; }
     79 
     80     // Defined in the IDL
     81     enum ReadyState {
     82         PENDING = 1,
     83         DONE = 2,
     84         EarlyDeath = 3
     85     };
     86 
     87     const String& readyState() const;
     88 
     89     DEFINE_ATTRIBUTE_EVENT_LISTENER(success);
     90     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
     91 
     92     void setCursorDetails(IndexedDB::CursorType, blink::WebIDBCursorDirection);
     93     void setPendingCursor(IDBCursor*);
     94     void abort();
     95 
     96     virtual void onError(PassRefPtrWillBeRawPtr<DOMError>);
     97     virtual void onSuccess(const Vector<String>&);
     98     virtual void onSuccess(PassOwnPtr<blink::WebIDBCursor>, IDBKey*, IDBKey* primaryKey, PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<blink::WebBlobInfo> >);
     99     virtual void onSuccess(IDBKey*);
    100     virtual void onSuccess(PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<blink::WebBlobInfo> >);
    101     virtual void onSuccess(PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<blink::WebBlobInfo> >, IDBKey*, const IDBKeyPath&);
    102     virtual void onSuccess(int64_t);
    103     virtual void onSuccess();
    104     virtual void onSuccess(IDBKey*, IDBKey* primaryKey, PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<blink::WebBlobInfo> >);
    105 
    106     // Only IDBOpenDBRequest instances should receive these:
    107     virtual void onBlocked(int64_t oldVersion) { ASSERT_NOT_REACHED(); }
    108     virtual void onUpgradeNeeded(int64_t oldVersion, PassOwnPtr<blink::WebIDBDatabase>, const IDBDatabaseMetadata&, blink::WebIDBDataLoss, String dataLossMessage) { ASSERT_NOT_REACHED(); }
    109     virtual void onSuccess(PassOwnPtr<blink::WebIDBDatabase>, const IDBDatabaseMetadata&) { ASSERT_NOT_REACHED(); }
    110 
    111     // ActiveDOMObject
    112     virtual bool hasPendingActivity() const OVERRIDE FINAL;
    113     virtual void stop() OVERRIDE FINAL;
    114 
    115     // EventTarget
    116     virtual const AtomicString& interfaceName() const OVERRIDE;
    117     virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
    118     virtual void uncaughtExceptionInEventHandler() OVERRIDE FINAL;
    119 
    120     using EventTarget::dispatchEvent;
    121     virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE;
    122 
    123     // Called by a version change transaction that has finished to set this
    124     // request back from DONE (following "upgradeneeded") back to PENDING (for
    125     // the upcoming "success" or "error").
    126     void transactionDidFinishAndDispatch();
    127 
    128     IDBCursor* getResultCursor() const;
    129 
    130 protected:
    131     IDBRequest(ScriptState*, IDBAny* source, IDBTransaction*);
    132     void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
    133     void dequeueEvent(Event*);
    134     virtual bool shouldEnqueueEvent() const;
    135     void onSuccessInternal(IDBAny*);
    136     void setResult(IDBAny*);
    137 
    138     bool m_contextStopped;
    139     Member<IDBTransaction> m_transaction;
    140     ReadyState m_readyState;
    141     bool m_requestAborted; // May be aborted by transaction then receive async onsuccess; ignore vs. assert.
    142 
    143 private:
    144     void setResultCursor(IDBCursor*, IDBKey*, IDBKey* primaryKey, PassRefPtr<SharedBuffer> value, PassOwnPtr<Vector<blink::WebBlobInfo> >);
    145     void handleBlobAcks();
    146 
    147     RefPtr<ScriptState> m_scriptState;
    148     Member<IDBAny> m_source;
    149     Member<IDBAny> m_result;
    150     RefPtrWillBeMember<DOMError> m_error;
    151 
    152     bool m_hasPendingActivity;
    153     WillBeHeapVector<RefPtrWillBeMember<Event> > m_enqueuedEvents;
    154 
    155     // Only used if the result type will be a cursor.
    156     IndexedDB::CursorType m_cursorType;
    157     blink::WebIDBCursorDirection m_cursorDirection;
    158     // When a cursor is continued/advanced, m_result is cleared and m_pendingCursor holds it.
    159     Member<IDBCursor> m_pendingCursor;
    160     // New state is not applied to the cursor object until the event is dispatched.
    161     Member<IDBKey> m_cursorKey;
    162     Member<IDBKey> m_cursorPrimaryKey;
    163     RefPtr<SharedBuffer> m_cursorValue;
    164     OwnPtr<Vector<blink::WebBlobInfo> > m_blobInfo;
    165 
    166     bool m_didFireUpgradeNeededEvent;
    167     bool m_preventPropagation;
    168     bool m_resultDirty;
    169 };
    170 
    171 } // namespace WebCore
    172 
    173 #endif // IDBRequest_h
    174