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/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"
     46 
     47 namespace WebCore {
     48 
     49 class ExceptionState;
     50 struct IDBDatabaseMetadata;
     51 class SharedBuffer;
     52 
     53 // Base class to simplify usage of event target refcounting.
     54 class IDBRequestBase : public WTF::RefCountedBase {
     55 public:
     56     virtual void deref() = 0;
     57 
     58 protected:
     59     virtual ~IDBRequestBase() { }
     60 };
     61 
     62 class IDBRequest : public IDBRequestBase, public ScriptWrappable, public EventTargetWithInlineData, public ActiveDOMObject {
     63     DEFINE_EVENT_TARGET_REFCOUNTING(IDBRequestBase);
     64 
     65 public:
     66     static PassRefPtr<IDBRequest> create(ExecutionContext*, PassRefPtr<IDBAny> source, IDBTransaction*);
     67     virtual ~IDBRequest();
     68 
     69     ScriptValue result(ExceptionState&);
     70     PassRefPtr<DOMError> error(ExceptionState&) const;
     71     ScriptValue source(ExecutionContext*) const;
     72     PassRefPtr<IDBTransaction> transaction() const { return m_transaction; }
     73 
     74     bool isResultDirty() const { return m_resultDirty; }
     75     PassRefPtr<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, IndexedDB::CursorDirection);
     94     void setPendingCursor(PassRefPtr<IDBCursor>);
     95     void abort();
     96 
     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>);
    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<blink::WebIDBDatabase>, const IDBDatabaseMetadata&, blink::WebIDBDataLoss, String dataLossMessage) { ASSERT_NOT_REACHED(); }
    110     virtual void onSuccess(PassOwnPtr<blink::WebIDBDatabase>, const IDBDatabaseMetadata&) { ASSERT_NOT_REACHED(); }
    111 
    112     // ActiveDOMObject
    113     virtual bool hasPendingActivity() const OVERRIDE;
    114     virtual void stop() OVERRIDE;
    115 
    116     // EventTarget
    117     virtual const AtomicString& interfaceName() const OVERRIDE;
    118     virtual ExecutionContext* executionContext() const OVERRIDE;
    119     virtual void uncaughtExceptionInEventHandler() OVERRIDE;
    120 
    121     using EventTarget::dispatchEvent;
    122     virtual bool dispatchEvent(PassRefPtr<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     virtual void deref() OVERRIDE
    130     {
    131         if (derefBase())
    132             delete this;
    133         else if (hasOneRef())
    134             checkForReferenceCycle();
    135     }
    136 
    137     DOMRequestState* requestState() { return &m_requestState; }
    138     IDBCursor* getResultCursor() const;
    139 
    140 protected:
    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>);
    147 
    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.
    152 
    153 private:
    154     void setResultCursor(PassRefPtr<IDBCursor>, PassRefPtr<IDBKey>, PassRefPtr<IDBKey> primaryKey, PassRefPtr<SharedBuffer> value);
    155     void checkForReferenceCycle();
    156 
    157     RefPtr<IDBAny> m_source;
    158     RefPtr<IDBAny> m_result;
    159     RefPtr<DOMError> m_error;
    160 
    161     bool m_hasPendingActivity;
    162     Vector<RefPtr<Event> > m_enqueuedEvents;
    163 
    164     // Only used if the result type will be a cursor.
    165     IndexedDB::CursorType m_cursorType;
    166     IndexedDB::CursorDirection 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;
    173 
    174     bool m_didFireUpgradeNeededEvent;
    175     bool m_preventPropagation;
    176     bool m_resultDirty;
    177 
    178     DOMRequestState m_requestState;
    179 };
    180 
    181 } // namespace WebCore
    182 
    183 #endif // IDBRequest_h
    184