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/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/dom/Event.h"
     38 #include "core/dom/EventListener.h"
     39 #include "core/dom/EventNames.h"
     40 #include "core/dom/EventTarget.h"
     41 #include "modules/indexeddb/IDBAny.h"
     42 #include "modules/indexeddb/IDBCallbacks.h"
     43 #include "modules/indexeddb/IDBCursor.h"
     44 
     45 namespace WebCore {
     46 
     47 class ExceptionState;
     48 class IDBTransaction;
     49 
     50 class IDBRequest : public ScriptWrappable, public IDBCallbacks, public EventTarget, public ActiveDOMObject {
     51 public:
     52     static PassRefPtr<IDBRequest> create(ScriptExecutionContext*, PassRefPtr<IDBAny> source, IDBTransaction*);
     53     static PassRefPtr<IDBRequest> create(ScriptExecutionContext*, PassRefPtr<IDBAny> source, IDBDatabaseBackendInterface::TaskType, IDBTransaction*);
     54     virtual ~IDBRequest();
     55 
     56     PassRefPtr<IDBAny> result(ExceptionState&) const;
     57     PassRefPtr<DOMError> error(ExceptionState&) const;
     58     PassRefPtr<IDBAny> source() const;
     59     PassRefPtr<IDBTransaction> transaction() const;
     60     void preventPropagation() { m_preventPropagation = true; }
     61 
     62     // Defined in the IDL
     63     enum ReadyState {
     64         PENDING = 1,
     65         DONE = 2,
     66         EarlyDeath = 3
     67     };
     68 
     69     const String& readyState() const;
     70 
     71     DEFINE_ATTRIBUTE_EVENT_LISTENER(success);
     72     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
     73 
     74     void markEarlyDeath();
     75     void setCursorDetails(IndexedDB::CursorType, IndexedDB::CursorDirection);
     76     void setPendingCursor(PassRefPtr<IDBCursor>);
     77     void finishCursor();
     78     void abort();
     79 
     80     // IDBCallbacks
     81     virtual void onError(PassRefPtr<DOMError>);
     82     virtual void onSuccess(const Vector<String>&);
     83     virtual void onSuccess(PassRefPtr<IDBCursorBackendInterface>, PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer>);
     84     virtual void onSuccess(PassRefPtr<IDBKey>);
     85     virtual void onSuccess(PassRefPtr<SharedBuffer>);
     86     virtual void onSuccess(PassRefPtr<SharedBuffer>, PassRefPtr<IDBKey>, const IDBKeyPath&);
     87     virtual void onSuccess(int64_t);
     88     virtual void onSuccess();
     89     virtual void onSuccess(PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer>);
     90 
     91     // ActiveDOMObject
     92     virtual bool hasPendingActivity() const OVERRIDE;
     93     virtual void stop() OVERRIDE;
     94 
     95     // EventTarget
     96     virtual const AtomicString& interfaceName() const;
     97     virtual ScriptExecutionContext* scriptExecutionContext() const;
     98     virtual void uncaughtExceptionInEventHandler();
     99 
    100     using EventTarget::dispatchEvent;
    101     virtual bool dispatchEvent(PassRefPtr<Event>) OVERRIDE;
    102 
    103     void transactionDidFinishAndDispatch();
    104 
    105     using RefCounted<IDBCallbacks>::ref;
    106     using RefCounted<IDBCallbacks>::deref;
    107 
    108     IDBDatabaseBackendInterface::TaskType taskType() { return m_taskType; }
    109 
    110     DOMRequestState* requestState() { return &m_requestState; }
    111 
    112 protected:
    113     IDBRequest(ScriptExecutionContext*, PassRefPtr<IDBAny> source, IDBDatabaseBackendInterface::TaskType, IDBTransaction*);
    114     void enqueueEvent(PassRefPtr<Event>);
    115     virtual bool shouldEnqueueEvent() const;
    116     void onSuccessInternal(PassRefPtr<SerializedScriptValue>);
    117     void onSuccessInternal(const ScriptValue&);
    118 
    119     RefPtr<IDBAny> m_result;
    120     RefPtr<DOMError> m_error;
    121     bool m_contextStopped;
    122     RefPtr<IDBTransaction> m_transaction;
    123     ReadyState m_readyState;
    124     bool m_requestAborted; // May be aborted by transaction then receive async onsuccess; ignore vs. assert.
    125 
    126 private:
    127     // EventTarget
    128     virtual void refEventTarget() { ref(); }
    129     virtual void derefEventTarget() { deref(); }
    130     virtual EventTargetData* eventTargetData();
    131     virtual EventTargetData* ensureEventTargetData();
    132 
    133     PassRefPtr<IDBCursor> getResultCursor();
    134     void setResultCursor(PassRefPtr<IDBCursor>, PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, const ScriptValue&);
    135 
    136     RefPtr<IDBAny> m_source;
    137     const IDBDatabaseBackendInterface::TaskType m_taskType;
    138 
    139     bool m_hasPendingActivity;
    140     Vector<RefPtr<Event> > m_enqueuedEvents;
    141 
    142     // Only used if the result type will be a cursor.
    143     IndexedDB::CursorType m_cursorType;
    144     IndexedDB::CursorDirection m_cursorDirection;
    145     bool m_cursorFinished;
    146     RefPtr<IDBCursor> m_pendingCursor;
    147     RefPtr<IDBKey> m_cursorKey;
    148     RefPtr<IDBKey> m_cursorPrimaryKey;
    149     ScriptValue m_cursorValue;
    150     bool m_didFireUpgradeNeededEvent;
    151     bool m_preventPropagation;
    152 
    153     EventTargetData m_eventTargetData;
    154     DOMRequestState m_requestState;
    155 };
    156 
    157 } // namespace WebCore
    158 
    159 #endif // IDBRequest_h
    160