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