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 "core/dom/ActiveDOMObject.h"
31 #include "core/dom/DOMStringList.h"
32 #include "modules/EventModules.h"
33 #include "modules/EventTargetModules.h"
34 #include "modules/indexeddb/IDBDatabaseCallbacks.h"
35 #include "modules/indexeddb/IDBMetadata.h"
36 #include "modules/indexeddb/IDBObjectStore.h"
37 #include "modules/indexeddb/IDBTransaction.h"
38 #include "modules/indexeddb/IndexedDB.h"
39 #include "platform/heap/Handle.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/RefPtr.h"
50 class ExecutionContext;
52 class IDBDatabase FINAL
53 : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<IDBDatabase>
54 , public EventTargetWithInlineData
55 , public ActiveDOMObject {
56 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<IDBDatabase>);
57 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(IDBDatabase);
59 static IDBDatabase* create(ExecutionContext*, PassOwnPtr<WebIDBDatabase>, IDBDatabaseCallbacks*);
60 virtual ~IDBDatabase();
61 virtual void trace(Visitor*) OVERRIDE;
63 void setMetadata(const IDBDatabaseMetadata& metadata) { m_metadata = metadata; }
64 void indexCreated(int64_t objectStoreId, const IDBIndexMetadata&);
65 void indexDeleted(int64_t objectStoreId, int64_t indexId);
66 void transactionCreated(IDBTransaction*);
67 void transactionFinished(const IDBTransaction*);
70 const String& name() const { return m_metadata.name; }
71 ScriptValue version(ScriptState*) const;
72 PassRefPtrWillBeRawPtr<DOMStringList> objectStoreNames() const;
74 IDBObjectStore* createObjectStore(const String& name, const Dictionary&, ExceptionState&);
75 IDBObjectStore* createObjectStore(const String& name, const IDBKeyPath&, bool autoIncrement, ExceptionState&);
76 IDBTransaction* transaction(ExecutionContext* context, PassRefPtrWillBeRawPtr<DOMStringList> scope, const String& mode, ExceptionState& exceptionState) { return transaction(context, *scope, mode, exceptionState); }
77 IDBTransaction* transaction(ExecutionContext*, const Vector<String>&, const String& mode, ExceptionState&);
78 IDBTransaction* transaction(ExecutionContext*, const String&, const String& mode, ExceptionState&);
79 void deleteObjectStore(const String& name, ExceptionState&);
82 DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
83 DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
84 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
85 DEFINE_ATTRIBUTE_EVENT_LISTENER(versionchange);
87 // IDBDatabaseCallbacks
88 void onVersionChange(int64_t oldVersion, int64_t newVersion);
89 void onAbort(int64_t, PassRefPtrWillBeRawPtr<DOMError>);
90 void onComplete(int64_t);
93 virtual bool hasPendingActivity() const OVERRIDE;
94 virtual void stop() OVERRIDE;
97 virtual const AtomicString& interfaceName() const OVERRIDE;
98 virtual ExecutionContext* executionContext() const OVERRIDE;
100 bool isClosePending() const { return m_closePending; }
102 const IDBDatabaseMetadata& metadata() const { return m_metadata; }
103 void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
105 using EventTarget::dispatchEvent;
106 virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE;
108 int64_t findObjectStoreId(const String& name) const;
109 bool containsObjectStore(const String& name) const
111 return findObjectStoreId(name) != IDBObjectStoreMetadata::InvalidId;
114 // Will return nullptr if this database is stopped.
115 WebIDBDatabase* backend() const { return m_backend.get(); }
117 static int64_t nextTransactionId();
119 void ackReceivedBlobs(const Vector<WebBlobInfo>*);
121 static const char indexDeletedErrorMessage[];
122 static const char isKeyCursorErrorMessage[];
123 static const char noKeyOrKeyRangeErrorMessage[];
124 static const char noSuchIndexErrorMessage[];
125 static const char noSuchObjectStoreErrorMessage[];
126 static const char noValueErrorMessage[];
127 static const char notValidKeyErrorMessage[];
128 static const char notVersionChangeTransactionErrorMessage[];
129 static const char objectStoreDeletedErrorMessage[];
130 static const char requestNotFinishedErrorMessage[];
131 static const char sourceDeletedErrorMessage[];
132 static const char transactionFinishedErrorMessage[];
133 static const char transactionInactiveErrorMessage[];
134 static const char transactionReadOnlyErrorMessage[];
135 static const char databaseClosedErrorMessage[];
138 IDBDatabase(ExecutionContext*, PassOwnPtr<WebIDBDatabase>, IDBDatabaseCallbacks*);
140 void closeConnection();
142 IDBDatabaseMetadata m_metadata;
143 OwnPtr<WebIDBDatabase> m_backend;
144 Member<IDBTransaction> m_versionChangeTransaction;
145 typedef HeapHashMap<int64_t, Member<IDBTransaction> > TransactionMap;
146 TransactionMap m_transactions;
149 bool m_contextStopped;
151 // Keep track of the versionchange events waiting to be fired on this
152 // database so that we can cancel them if the database closes.
153 WillBeHeapVector<RefPtrWillBeMember<Event> > m_enqueuedEvents;
155 Member<IDBDatabaseCallbacks> m_databaseCallbacks;
160 #endif // IDBDatabase_h