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 "core/events/Event.h" 34 #include "core/events/EventTarget.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 "public/platform/WebIDBDatabase.h" 41 #include "wtf/OwnPtr.h" 42 #include "wtf/PassOwnPtr.h" 43 #include "wtf/PassRefPtr.h" 44 #include "wtf/RefCounted.h" 45 #include "wtf/RefPtr.h" 46 47 namespace WebCore { 48 49 class DOMError; 50 class ExceptionState; 51 class ExecutionContext; 52 53 class IDBDatabase : public RefCounted<IDBDatabase>, public ScriptWrappable, public EventTargetWithInlineData, public ActiveDOMObject { 54 REFCOUNTED_EVENT_TARGET(IDBDatabase); 55 56 public: 57 static PassRefPtr<IDBDatabase> create(ExecutionContext*, PassOwnPtr<blink::WebIDBDatabase>, PassRefPtr<IDBDatabaseCallbacks>); 58 ~IDBDatabase(); 59 60 void setMetadata(const IDBDatabaseMetadata& metadata) { m_metadata = metadata; } 61 void indexCreated(int64_t objectStoreId, const IDBIndexMetadata&); 62 void indexDeleted(int64_t objectStoreId, int64_t indexId); 63 void transactionCreated(IDBTransaction*); 64 void transactionFinished(const IDBTransaction*); 65 66 // Implement the IDL 67 const String& name() const { return m_metadata.name; } 68 ScriptValue version(ExecutionContext*) const; 69 PassRefPtr<DOMStringList> objectStoreNames() const; 70 71 PassRefPtr<IDBObjectStore> createObjectStore(const String& name, const Dictionary&, ExceptionState&); 72 PassRefPtr<IDBObjectStore> createObjectStore(const String& name, const IDBKeyPath&, bool autoIncrement, ExceptionState&); 73 PassRefPtr<IDBTransaction> transaction(ExecutionContext* context, PassRefPtr<DOMStringList> scope, const String& mode, ExceptionState& exceptionState) { return transaction(context, *scope, mode, exceptionState); } 74 PassRefPtr<IDBTransaction> transaction(ExecutionContext*, const Vector<String>&, const String& mode, ExceptionState&); 75 PassRefPtr<IDBTransaction> transaction(ExecutionContext*, const String&, const String& mode, ExceptionState&); 76 void deleteObjectStore(const String& name, ExceptionState&); 77 void close(); 78 79 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); 80 DEFINE_ATTRIBUTE_EVENT_LISTENER(close); 81 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 82 DEFINE_ATTRIBUTE_EVENT_LISTENER(versionchange); 83 84 // IDBDatabaseCallbacks 85 virtual void onVersionChange(int64_t oldVersion, int64_t newVersion); 86 virtual void onAbort(int64_t, PassRefPtr<DOMError>); 87 virtual void onComplete(int64_t); 88 89 // ActiveDOMObject 90 virtual bool hasPendingActivity() const OVERRIDE; 91 virtual void stop() OVERRIDE; 92 93 // EventTarget 94 virtual const AtomicString& interfaceName() const OVERRIDE; 95 virtual ExecutionContext* executionContext() const OVERRIDE; 96 97 bool isClosePending() const { return m_closePending; } 98 void forceClose(); 99 const IDBDatabaseMetadata& metadata() const { return m_metadata; } 100 void enqueueEvent(PassRefPtr<Event>); 101 102 using EventTarget::dispatchEvent; 103 virtual bool dispatchEvent(PassRefPtr<Event>) OVERRIDE; 104 105 int64_t findObjectStoreId(const String& name) const; 106 bool containsObjectStore(const String& name) const 107 { 108 return findObjectStoreId(name) != IDBObjectStoreMetadata::InvalidId; 109 } 110 111 blink::WebIDBDatabase* backend() const { return m_backend.get(); } 112 113 static int64_t nextTransactionId(); 114 115 static const char indexDeletedErrorMessage[]; 116 static const char isKeyCursorErrorMessage[]; 117 static const char noKeyOrKeyRangeErrorMessage[]; 118 static const char noSuchIndexErrorMessage[]; 119 static const char noSuchObjectStoreErrorMessage[]; 120 static const char noValueErrorMessage[]; 121 static const char notValidKeyErrorMessage[]; 122 static const char notVersionChangeTransactionErrorMessage[]; 123 static const char objectStoreDeletedErrorMessage[]; 124 static const char requestNotFinishedErrorMessage[]; 125 static const char sourceDeletedErrorMessage[]; 126 static const char transactionFinishedErrorMessage[]; 127 static const char transactionInactiveErrorMessage[]; 128 129 private: 130 IDBDatabase(ExecutionContext*, PassOwnPtr<blink::WebIDBDatabase>, PassRefPtr<IDBDatabaseCallbacks>); 131 132 void closeConnection(); 133 134 IDBDatabaseMetadata m_metadata; 135 OwnPtr<blink::WebIDBDatabase> m_backend; 136 RefPtr<IDBTransaction> m_versionChangeTransaction; 137 typedef HashMap<int64_t, RefPtr<IDBTransaction> > TransactionMap; 138 TransactionMap m_transactions; 139 140 bool m_closePending; 141 bool m_contextStopped; 142 143 // Keep track of the versionchange events waiting to be fired on this 144 // database so that we can cancel them if the database closes. 145 Vector<RefPtr<Event> > m_enqueuedEvents; 146 147 RefPtr<IDBDatabaseCallbacks> m_databaseCallbacks; 148 }; 149 150 } // namespace WebCore 151 152 #endif // IDBDatabase_h 153