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 "ActiveDOMObject.h" 30#include "DOMStringList.h" 31#include "Dictionary.h" 32#include "Event.h" 33#include "EventTarget.h" 34#include "IDBDatabaseCallbacks.h" 35#include "IDBDatabaseMetadata.h" 36#include "IDBObjectStore.h" 37#include "IDBTransaction.h" 38#include "IndexedDB.h" 39#include "ScriptWrappable.h" 40#include <wtf/PassRefPtr.h> 41#include <wtf/RefCounted.h> 42#include <wtf/RefPtr.h> 43 44#if ENABLE(INDEXED_DATABASE) 45 46namespace WebCore { 47 48class ScriptExecutionContext; 49 50typedef int ExceptionCode; 51 52class IDBDatabase final : public RefCounted<IDBDatabase>, public ScriptWrappable, public EventTargetWithInlineData, public ActiveDOMObject { 53public: 54 static PassRefPtr<IDBDatabase> create(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackend>, PassRefPtr<IDBDatabaseCallbacks>); 55 ~IDBDatabase(); 56 57 void setMetadata(const IDBDatabaseMetadata& metadata) { m_metadata = metadata; } 58 void transactionCreated(IDBTransaction*); 59 void transactionFinished(IDBTransaction*); 60 61 // Implement the IDL 62 const String name() const { return m_metadata.name; } 63 uint64_t version() const; 64 PassRefPtr<DOMStringList> objectStoreNames() const; 65 66 PassRefPtr<IDBObjectStore> createObjectStore(const String& name, const Dictionary&, ExceptionCode&); 67 PassRefPtr<IDBObjectStore> createObjectStore(const String& name, const IDBKeyPath&, bool autoIncrement, ExceptionCode&); 68 PassRefPtr<IDBTransaction> transaction(ScriptExecutionContext* context, PassRefPtr<DOMStringList> scope, const String& mode, ExceptionCode& ec) { return transaction(context, *scope, mode, ec); } 69 PassRefPtr<IDBTransaction> transaction(ScriptExecutionContext*, const Vector<String>&, const String& mode, ExceptionCode&); 70 PassRefPtr<IDBTransaction> transaction(ScriptExecutionContext*, const String&, const String& mode, ExceptionCode&); 71 void deleteObjectStore(const String& name, ExceptionCode&); 72 void close(); 73 74 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); 75 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 76 DEFINE_ATTRIBUTE_EVENT_LISTENER(versionchange); 77 78 // IDBDatabaseCallbacks 79 virtual void onVersionChange(uint64_t oldVersion, uint64_t newVersion, IndexedDB::VersionNullness newVersionNullness); 80 virtual void onAbort(int64_t, PassRefPtr<IDBDatabaseError>); 81 virtual void onComplete(int64_t); 82 83 // ActiveDOMObject 84 virtual bool hasPendingActivity() const override; 85 86 // EventTarget 87 virtual EventTargetInterface eventTargetInterface() const override final { return IDBDatabaseEventTargetInterfaceType; } 88 virtual ScriptExecutionContext* scriptExecutionContext() const override final { return ActiveDOMObject::scriptExecutionContext(); } 89 90 bool isClosePending() const { return m_closePending; } 91 void forceClose(); 92 const IDBDatabaseMetadata metadata() const { return m_metadata; } 93 void enqueueEvent(PassRefPtr<Event>); 94 95 using EventTarget::dispatchEvent; 96 virtual bool dispatchEvent(PassRefPtr<Event>) override; 97 98 int64_t findObjectStoreId(const String& name) const; 99 bool containsObjectStore(const String& name) const 100 { 101 return findObjectStoreId(name) != IDBObjectStoreMetadata::InvalidId; 102 } 103 104 IDBDatabaseBackend* backend() const { return m_backend.get(); } 105 106 static int64_t nextTransactionId(); 107 108 using RefCounted<IDBDatabase>::ref; 109 using RefCounted<IDBDatabase>::deref; 110 111private: 112 IDBDatabase(ScriptExecutionContext*, PassRefPtr<IDBDatabaseBackend>, PassRefPtr<IDBDatabaseCallbacks>); 113 114 // ActiveDOMObject 115 virtual void stop() override; 116 117 // EventTarget 118 virtual void refEventTarget() override final { ref(); } 119 virtual void derefEventTarget() override final { deref(); } 120 121 void closeConnection(); 122 123 IDBDatabaseMetadata m_metadata; 124 RefPtr<IDBDatabaseBackend> m_backend; 125 RefPtr<IDBTransaction> m_versionChangeTransaction; 126 typedef HashMap<int64_t, IDBTransaction*> TransactionMap; 127 TransactionMap m_transactions; 128 129 bool m_closePending; 130 bool m_contextStopped; 131 132 // Keep track of the versionchange events waiting to be fired on this 133 // database so that we can cancel them if the database closes. 134 Vector<RefPtr<Event>> m_enqueuedEvents; 135 136 RefPtr<IDBDatabaseCallbacks> m_databaseCallbacks; 137}; 138 139} // namespace WebCore 140 141#endif 142 143#endif // IDBDatabase_h 144