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  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef IDBDatabase_h
     27 #define IDBDatabase_h
     28 
     29 #include "bindings/v8/Dictionary.h"
     30 #include "bindings/v8/ScriptWrappable.h"
     31 #include "core/dom/ActiveDOMObject.h"
     32 #include "core/dom/DOMStringList.h"
     33 #include "modules/EventModules.h"
     34 #include "modules/EventTargetModules.h"
     35 #include "modules/indexeddb/IDBDatabaseCallbacks.h"
     36 #include "modules/indexeddb/IDBMetadata.h"
     37 #include "modules/indexeddb/IDBObjectStore.h"
     38 #include "modules/indexeddb/IDBTransaction.h"
     39 #include "modules/indexeddb/IndexedDB.h"
     40 #include "platform/heap/Handle.h"
     41 #include "public/platform/WebIDBDatabase.h"
     42 #include "wtf/OwnPtr.h"
     43 #include "wtf/PassOwnPtr.h"
     44 #include "wtf/PassRefPtr.h"
     45 #include "wtf/RefPtr.h"
     46 
     47 namespace WebCore {
     48 
     49 class DOMError;
     50 class ExceptionState;
     51 class ExecutionContext;
     52 
     53 class IDBDatabase FINAL
     54     : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<IDBDatabase>
     55     , public ScriptWrappable
     56     , public EventTargetWithInlineData
     57     , public ActiveDOMObject {
     58     DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<IDBDatabase>);
     59     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(IDBDatabase);
     60 public:
     61     static IDBDatabase* create(ExecutionContext*, PassOwnPtr<blink::WebIDBDatabase>, IDBDatabaseCallbacks*);
     62     virtual ~IDBDatabase();
     63     virtual void trace(Visitor*) OVERRIDE;
     64 
     65     void setMetadata(const IDBDatabaseMetadata& metadata) { m_metadata = metadata; }
     66     void indexCreated(int64_t objectStoreId, const IDBIndexMetadata&);
     67     void indexDeleted(int64_t objectStoreId, int64_t indexId);
     68     void transactionCreated(IDBTransaction*);
     69     void transactionFinished(const IDBTransaction*);
     70 
     71     // Implement the IDL
     72     const String& name() const { return m_metadata.name; }
     73     ScriptValue version(ScriptState*) const;
     74     PassRefPtrWillBeRawPtr<DOMStringList> objectStoreNames() const;
     75 
     76     IDBObjectStore* createObjectStore(const String& name, const Dictionary&, ExceptionState&);
     77     IDBObjectStore* createObjectStore(const String& name, const IDBKeyPath&, bool autoIncrement, ExceptionState&);
     78     IDBTransaction* transaction(ExecutionContext* context, PassRefPtrWillBeRawPtr<DOMStringList> scope, const String& mode, ExceptionState& exceptionState) { return transaction(context, *scope, mode, exceptionState); }
     79     IDBTransaction* transaction(ExecutionContext*, const Vector<String>&, const String& mode, ExceptionState&);
     80     IDBTransaction* transaction(ExecutionContext*, const String&, const String& mode, ExceptionState&);
     81     void deleteObjectStore(const String& name, ExceptionState&);
     82     void close();
     83 
     84     DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
     85     DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
     86     DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
     87     DEFINE_ATTRIBUTE_EVENT_LISTENER(versionchange);
     88 
     89     // IDBDatabaseCallbacks
     90     void onVersionChange(int64_t oldVersion, int64_t newVersion);
     91     void onAbort(int64_t, PassRefPtrWillBeRawPtr<DOMError>);
     92     void onComplete(int64_t);
     93 
     94     // ActiveDOMObject
     95     virtual bool hasPendingActivity() const OVERRIDE;
     96     virtual void stop() OVERRIDE;
     97 
     98     // EventTarget
     99     virtual const AtomicString& interfaceName() const OVERRIDE;
    100     virtual ExecutionContext* executionContext() const OVERRIDE;
    101 
    102     bool isClosePending() const { return m_closePending; }
    103     void forceClose();
    104     const IDBDatabaseMetadata& metadata() const { return m_metadata; }
    105     void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
    106 
    107     using EventTarget::dispatchEvent;
    108     virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE;
    109 
    110     int64_t findObjectStoreId(const String& name) const;
    111     bool containsObjectStore(const String& name) const
    112     {
    113         return findObjectStoreId(name) != IDBObjectStoreMetadata::InvalidId;
    114     }
    115 
    116     // Will return nullptr if this database is stopped.
    117     blink::WebIDBDatabase* backend() const { return m_backend.get(); }
    118 
    119     static int64_t nextTransactionId();
    120 
    121     void ackReceivedBlobs(const Vector<blink::WebBlobInfo>*);
    122 
    123     static const char indexDeletedErrorMessage[];
    124     static const char isKeyCursorErrorMessage[];
    125     static const char noKeyOrKeyRangeErrorMessage[];
    126     static const char noSuchIndexErrorMessage[];
    127     static const char noSuchObjectStoreErrorMessage[];
    128     static const char noValueErrorMessage[];
    129     static const char notValidKeyErrorMessage[];
    130     static const char notVersionChangeTransactionErrorMessage[];
    131     static const char objectStoreDeletedErrorMessage[];
    132     static const char requestNotFinishedErrorMessage[];
    133     static const char sourceDeletedErrorMessage[];
    134     static const char transactionFinishedErrorMessage[];
    135     static const char transactionInactiveErrorMessage[];
    136     static const char transactionReadOnlyErrorMessage[];
    137     static const char databaseClosedErrorMessage[];
    138 
    139 private:
    140     IDBDatabase(ExecutionContext*, PassOwnPtr<blink::WebIDBDatabase>, IDBDatabaseCallbacks*);
    141 
    142     void closeConnection();
    143 
    144     IDBDatabaseMetadata m_metadata;
    145     OwnPtr<blink::WebIDBDatabase> m_backend;
    146     Member<IDBTransaction> m_versionChangeTransaction;
    147     typedef HeapHashMap<int64_t, Member<IDBTransaction> > TransactionMap;
    148     TransactionMap m_transactions;
    149 
    150     bool m_closePending;
    151     bool m_contextStopped;
    152 
    153     // Keep track of the versionchange events waiting to be fired on this
    154     // database so that we can cancel them if the database closes.
    155     WillBeHeapVector<RefPtrWillBeMember<Event> > m_enqueuedEvents;
    156 
    157     Member<IDBDatabaseCallbacks> m_databaseCallbacks;
    158 };
    159 
    160 } // namespace WebCore
    161 
    162 #endif // IDBDatabase_h
    163