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/core/v8/ScriptState.h"
     33 #include "bindings/core/v8/ScriptValue.h"
     34 #include "core/dom/ActiveDOMObject.h"
     35 #include "core/dom/DOMError.h"
     36 #include "core/dom/DOMStringList.h"
     37 #include "core/events/EventListener.h"
     38 #include "core/events/EventTarget.h"
     39 #include "modules/EventModules.h"
     40 #include "modules/indexeddb/IDBAny.h"
     41 #include "modules/indexeddb/IDBTransaction.h"
     42 #include "modules/indexeddb/IndexedDB.h"
     43 #include "platform/heap/Handle.h"
     44 #include "public/platform/WebBlobInfo.h"
     45 #include "public/platform/WebIDBCursor.h"
     46 #include "public/platform/WebIDBTypes.h"
     47 
     48 namespace blink {
     49 
     50 class ExceptionState;
     51 class IDBCursor;
     52 struct IDBDatabaseMetadata;
     53 class SharedBuffer;
     54 
     55 class IDBRequest
     56     : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<IDBRequest>
     57     , public EventTargetWithInlineData
     58     , public ActiveDOMObject {
     59     DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<IDBRequest>);
     60     DEFINE_WRAPPERTYPEINFO();
     61     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(IDBRequest);
     62 public:
     63     static IDBRequest* create(ScriptState*, IDBAny* source, IDBTransaction*);
     64     virtual ~IDBRequest();
     65     void dispose();
     66     virtual void trace(Visitor*) OVERRIDE;
     67 
     68     ScriptState* scriptState() { return m_scriptState.get(); }
     69     ScriptValue result(ExceptionState&);
     70     PassRefPtrWillBeRawPtr<DOMError> error(ExceptionState&) const;
     71     ScriptValue source() const;
     72     IDBTransaction* transaction() const { return m_transaction.get(); }
     73 
     74     bool isResultDirty() const { return m_resultDirty; }
     75     IDBAny* resultAsAny() const { return m_result; }
     76 
     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; }
     80 
     81     // Defined in the IDL
     82     enum ReadyState {
     83         PENDING = 1,
     84         DONE = 2,
     85         EarlyDeath = 3
     86     };
     87 
     88     const String& readyState() const;
     89 
     90     DEFINE_ATTRIBUTE_EVENT_LISTENER(success);
     91     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
     92 
     93     void setCursorDetails(IndexedDB::CursorType, WebIDBCursorDirection);
     94     void setPendingCursor(IDBCursor*);
     95     void abort();
     96 
     97     virtual void onError(PassRefPtrWillBeRawPtr<DOMError>);
     98     virtual void onSuccess(const Vector<String>&);
     99     virtual void onSuccess(PassOwnPtr<WebIDBCursor>, IDBKey*, IDBKey* primaryKey, PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<WebBlobInfo> >);
    100     virtual void onSuccess(IDBKey*);
    101     virtual void onSuccess(PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<WebBlobInfo> >);
    102     virtual void onSuccess(PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<WebBlobInfo> >, IDBKey*, const IDBKeyPath&);
    103     virtual void onSuccess(int64_t);
    104     virtual void onSuccess();
    105     virtual void onSuccess(IDBKey*, IDBKey* primaryKey, PassRefPtr<SharedBuffer>, PassOwnPtr<Vector<WebBlobInfo> >);
    106 
    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<WebIDBDatabase>, const IDBDatabaseMetadata&, WebIDBDataLoss, String dataLossMessage) { ASSERT_NOT_REACHED(); }
    110     virtual void onSuccess(PassOwnPtr<WebIDBDatabase>, const IDBDatabaseMetadata&) { ASSERT_NOT_REACHED(); }
    111 
    112     // ActiveDOMObject
    113     virtual bool hasPendingActivity() const OVERRIDE FINAL;
    114     virtual void stop() OVERRIDE FINAL;
    115 
    116     // EventTarget
    117     virtual const AtomicString& interfaceName() const OVERRIDE;
    118     virtual ExecutionContext* executionContext() const OVERRIDE FINAL;
    119     virtual void uncaughtExceptionInEventHandler() OVERRIDE FINAL;
    120 
    121     using EventTarget::dispatchEvent;
    122     virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE;
    123 
    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();
    128 
    129     IDBCursor* getResultCursor() const;
    130 
    131 protected:
    132     IDBRequest(ScriptState*, IDBAny* source, IDBTransaction*);
    133     void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
    134     void dequeueEvent(Event*);
    135     virtual bool shouldEnqueueEvent() const;
    136     void onSuccessInternal(IDBAny*);
    137     void setResult(IDBAny*);
    138 
    139     bool m_contextStopped;
    140     Member<IDBTransaction> m_transaction;
    141     ReadyState m_readyState;
    142     bool m_requestAborted; // May be aborted by transaction then receive async onsuccess; ignore vs. assert.
    143 
    144 private:
    145     void setResultCursor(IDBCursor*, IDBKey*, IDBKey* primaryKey, PassRefPtr<SharedBuffer> value, PassOwnPtr<Vector<WebBlobInfo> >);
    146     void setBlobInfo(PassOwnPtr<Vector<WebBlobInfo>>);
    147     void handleBlobAcks();
    148 
    149     RefPtr<ScriptState> m_scriptState;
    150     Member<IDBAny> m_source;
    151     Member<IDBAny> m_result;
    152     RefPtrWillBeMember<DOMError> m_error;
    153 
    154     bool m_hasPendingActivity;
    155     WillBeHeapVector<RefPtrWillBeMember<Event> > m_enqueuedEvents;
    156 
    157     // Only used if the result type will be a cursor.
    158     IndexedDB::CursorType m_cursorType;
    159     WebIDBCursorDirection m_cursorDirection;
    160     // When a cursor is continued/advanced, m_result is cleared and m_pendingCursor holds it.
    161     Member<IDBCursor> m_pendingCursor;
    162     // New state is not applied to the cursor object until the event is dispatched.
    163     Member<IDBKey> m_cursorKey;
    164     Member<IDBKey> m_cursorPrimaryKey;
    165     RefPtr<SharedBuffer> m_cursorValue;
    166     OwnPtr<Vector<WebBlobInfo> > m_blobInfo;
    167 
    168     bool m_didFireUpgradeNeededEvent;
    169     bool m_preventPropagation;
    170     bool m_resultDirty;
    171 };
    172 
    173 } // namespace blink
    174 
    175 #endif // IDBRequest_h
    176