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 IDBTransaction_h 27 #define IDBTransaction_h 28 29 #include "bindings/v8/ScriptWrappable.h" 30 #include "core/dom/ActiveDOMObject.h" 31 #include "core/dom/DOMError.h" 32 #include "core/dom/Event.h" 33 #include "core/dom/EventListener.h" 34 #include "core/dom/EventNames.h" 35 #include "core/dom/EventTarget.h" 36 #include "modules/indexeddb/IDBMetadata.h" 37 #include "modules/indexeddb/IndexedDB.h" 38 #include "wtf/HashSet.h" 39 #include "wtf/RefCounted.h" 40 41 namespace WebCore { 42 43 class DOMError; 44 class ExceptionState; 45 class IDBCursor; 46 class IDBDatabase; 47 class IDBDatabaseBackendInterface; 48 class IDBObjectStore; 49 class IDBOpenDBRequest; 50 struct IDBObjectStoreMetadata; 51 52 class IDBTransaction : public ScriptWrappable, public RefCounted<IDBTransaction>, public EventTarget, public ActiveDOMObject { 53 public: 54 static PassRefPtr<IDBTransaction> create(ScriptExecutionContext*, int64_t, const Vector<String>& objectStoreNames, IndexedDB::TransactionMode, IDBDatabase*); 55 static PassRefPtr<IDBTransaction> create(ScriptExecutionContext*, int64_t, IDBDatabase*, IDBOpenDBRequest*, const IDBDatabaseMetadata& previousMetadata); 56 virtual ~IDBTransaction(); 57 58 static const AtomicString& modeReadOnly(); 59 static const AtomicString& modeReadWrite(); 60 static const AtomicString& modeVersionChange(); 61 static const AtomicString& modeReadOnlyLegacy(); 62 static const AtomicString& modeReadWriteLegacy(); 63 64 static IndexedDB::TransactionMode stringToMode(const String&, ExceptionState&); 65 static const AtomicString& modeToString(IndexedDB::TransactionMode); 66 67 IDBDatabaseBackendInterface* backendDB() const; 68 69 int64_t id() const { return m_id; } 70 bool isActive() const { return m_state == Active; } 71 bool isFinished() const { return m_state == Finished; } 72 bool isReadOnly() const { return m_mode == IndexedDB::TransactionReadOnly; } 73 bool isVersionChange() const { return m_mode == IndexedDB::TransactionVersionChange; } 74 75 // Implement the IDBTransaction IDL 76 const String& mode() const; 77 IDBDatabase* db() const { return m_database.get(); } 78 PassRefPtr<DOMError> error() const { return m_error; } 79 PassRefPtr<IDBObjectStore> objectStore(const String& name, ExceptionState&); 80 void abort(ExceptionState&); 81 82 class OpenCursorNotifier { 83 public: 84 OpenCursorNotifier(PassRefPtr<IDBTransaction>, IDBCursor*); 85 ~OpenCursorNotifier(); 86 void cursorFinished(); 87 private: 88 RefPtr<IDBTransaction> m_transaction; 89 IDBCursor* m_cursor; 90 }; 91 92 void registerRequest(IDBRequest*); 93 void unregisterRequest(IDBRequest*); 94 void objectStoreCreated(const String&, PassRefPtr<IDBObjectStore>); 95 void objectStoreDeleted(const String&); 96 void setActive(bool); 97 void setError(PassRefPtr<DOMError>); 98 99 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); 100 DEFINE_ATTRIBUTE_EVENT_LISTENER(complete); 101 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 102 103 virtual void onAbort(PassRefPtr<DOMError>); 104 virtual void onComplete(); 105 106 // EventTarget 107 virtual const AtomicString& interfaceName() const; 108 virtual ScriptExecutionContext* scriptExecutionContext() const; 109 110 using EventTarget::dispatchEvent; 111 virtual bool dispatchEvent(PassRefPtr<Event>) OVERRIDE; 112 113 // ActiveDOMObject 114 virtual bool hasPendingActivity() const OVERRIDE; 115 virtual bool canSuspend() const OVERRIDE; 116 virtual void stop() OVERRIDE; 117 118 using RefCounted<IDBTransaction>::ref; 119 using RefCounted<IDBTransaction>::deref; 120 121 private: 122 IDBTransaction(ScriptExecutionContext*, int64_t, const Vector<String>&, IndexedDB::TransactionMode, IDBDatabase*, IDBOpenDBRequest*, const IDBDatabaseMetadata&); 123 124 void enqueueEvent(PassRefPtr<Event>); 125 void closeOpenCursors(); 126 127 void registerOpenCursor(IDBCursor*); 128 void unregisterOpenCursor(IDBCursor*); 129 130 // EventTarget 131 virtual void refEventTarget() { ref(); } 132 virtual void derefEventTarget() { deref(); } 133 virtual EventTargetData* eventTargetData(); 134 virtual EventTargetData* ensureEventTargetData(); 135 136 enum State { 137 Inactive, // Created or started, but not in an event callback 138 Active, // Created or started, in creation scope or an event callback 139 Finishing, // In the process of aborting or completing. 140 Finished, // No more events will fire and no new requests may be filed. 141 }; 142 143 int64_t m_id; 144 RefPtr<IDBDatabase> m_database; 145 const Vector<String> m_objectStoreNames; 146 IDBOpenDBRequest* m_openDBRequest; 147 const IndexedDB::TransactionMode m_mode; 148 State m_state; 149 bool m_hasPendingActivity; 150 bool m_contextStopped; 151 RefPtr<DOMError> m_error; 152 153 ListHashSet<RefPtr<IDBRequest> > m_requestList; 154 155 typedef HashMap<String, RefPtr<IDBObjectStore> > IDBObjectStoreMap; 156 IDBObjectStoreMap m_objectStoreMap; 157 158 typedef HashSet<RefPtr<IDBObjectStore> > IDBObjectStoreSet; 159 IDBObjectStoreSet m_deletedObjectStores; 160 161 typedef HashMap<RefPtr<IDBObjectStore>, IDBObjectStoreMetadata> IDBObjectStoreMetadataMap; 162 IDBObjectStoreMetadataMap m_objectStoreCleanupMap; 163 IDBDatabaseMetadata m_previousMetadata; 164 165 HashSet<IDBCursor*> m_openCursors; 166 167 EventTargetData m_eventTargetData; 168 }; 169 170 } // namespace WebCore 171 172 #endif // IDBTransaction_h 173