2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "bindings/core/v8/Dictionary.h"
30 #include "bindings/core/v8/ScriptState.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"
51 class ExecutionContext;
53 class IDBDatabase final
54 : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<IDBDatabase>
55 , public EventTargetWithInlineData
56 , public ActiveDOMObject {
57 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<IDBDatabase>);
58 DEFINE_WRAPPERTYPEINFO();
59 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(IDBDatabase);
61 static IDBDatabase* create(ExecutionContext*, PassOwnPtr<WebIDBDatabase>, IDBDatabaseCallbacks*);
62 virtual ~IDBDatabase();
63 virtual void trace(Visitor*) override;
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*);
72 const String& name() const { return m_metadata.name; }
73 ScriptValue version(ScriptState*) const;
74 PassRefPtrWillBeRawPtr<DOMStringList> objectStoreNames() const;
76 IDBObjectStore* createObjectStore(const String& name, const Dictionary&, ExceptionState&);
77 IDBObjectStore* createObjectStore(const String& name, const IDBKeyPath&, bool autoIncrement, ExceptionState&);
78 IDBTransaction* transaction(ScriptState* scriptState, PassRefPtrWillBeRawPtr<DOMStringList> scope, const String& mode, ExceptionState& exceptionState) { return transaction(scriptState, *scope, mode, exceptionState); }
79 IDBTransaction* transaction(ScriptState*, const Vector<String>&, const String& mode, ExceptionState&);
80 IDBTransaction* transaction(ScriptState*, const String&, const String& mode, ExceptionState&);
81 void deleteObjectStore(const String& name, ExceptionState&);
84 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
85 DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
86 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
87 DEFINE_ATTRIBUTE_EVENT_LISTENER(versionchange);
89 // IDBDatabaseCallbacks
90 void onVersionChange(int64_t oldVersion, int64_t newVersion);
91 void onAbort(int64_t, DOMError*);
92 void onComplete(int64_t);
95 virtual bool hasPendingActivity() const override;
96 virtual void stop() override;
99 virtual const AtomicString& interfaceName() const override;
100 virtual ExecutionContext* executionContext() const override;
102 bool isClosePending() const { return m_closePending; }
104 const IDBDatabaseMetadata& metadata() const { return m_metadata; }
105 void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
107 using EventTarget::dispatchEvent;
108 virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) override;
110 int64_t findObjectStoreId(const String& name) const;
111 bool containsObjectStore(const String& name) const
113 return findObjectStoreId(name) != IDBObjectStoreMetadata::InvalidId;
116 // Will return nullptr if this database is stopped.
117 WebIDBDatabase* backend() const { return m_backend.get(); }
119 static int64_t nextTransactionId();
121 void ackReceivedBlobs(const Vector<WebBlobInfo>*);
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[];
140 IDBDatabase(ExecutionContext*, PassOwnPtr<WebIDBDatabase>, IDBDatabaseCallbacks*);
142 void closeConnection();
144 IDBDatabaseMetadata m_metadata;
145 OwnPtr<WebIDBDatabase> m_backend;
146 Member<IDBTransaction> m_versionChangeTransaction;
147 typedef HeapHashMap<int64_t, Member<IDBTransaction> > TransactionMap;
148 TransactionMap m_transactions;
151 bool m_contextStopped;
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;
157 Member<IDBDatabaseCallbacks> m_databaseCallbacks;
162 #endif // IDBDatabase_h