1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_idbdatabase_h__
8 #define mozilla_dom_idbdatabase_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/IDBTransactionBinding.h"
12 #include "mozilla/dom/indexedDB/PBackgroundIDBSharedTypes.h"
13 #include "mozilla/dom/quota/PersistenceType.h"
14 #include "mozilla/DOMEventTargetHelper.h"
15 #include "mozilla/UniquePtr.h"
16 #include "nsTHashMap.h"
17 #include "nsHashKeys.h"
19 #include "nsTHashSet.h"
22 class nsIGlobalObject
;
27 class EventChainPostVisitor
;
35 struct IDBObjectStoreParameters
;
36 class IDBOpenDBRequest
;
41 class StringOrStringSequence
;
44 class BackgroundDatabaseChild
;
45 class PBackgroundIDBDatabaseFileChild
;
46 } // namespace indexedDB
48 class IDBDatabase final
: public DOMEventTargetHelper
{
49 using DatabaseSpec
= mozilla::dom::indexedDB::DatabaseSpec
;
50 using PersistenceType
= mozilla::dom::quota::PersistenceType
;
53 friend class Observer
;
55 friend class IDBObjectStore
;
56 friend class IDBIndex
;
58 // The factory must be kept alive when IndexedDB is used in multiple
59 // processes. If it dies then the entire actor tree will be destroyed with it
60 // and the world will explode.
61 SafeRefPtr
<IDBFactory
> mFactory
;
63 UniquePtr
<DatabaseSpec
> mSpec
;
65 // Normally null except during a versionchange transaction.
66 UniquePtr
<DatabaseSpec
> mPreviousSpec
;
68 indexedDB::BackgroundDatabaseChild
* mBackgroundActor
;
70 nsTHashSet
<IDBTransaction
*> mTransactions
;
72 nsTHashMap
<nsISupportsHashKey
, indexedDB::PBackgroundIDBDatabaseFileChild
*>
75 RefPtr
<Observer
> mObserver
;
80 bool mIncreasedActiveDatabaseCount
;
83 [[nodiscard
]] static RefPtr
<IDBDatabase
> Create(
84 IDBOpenDBRequest
* aRequest
, SafeRefPtr
<IDBFactory
> aFactory
,
85 indexedDB::BackgroundDatabaseChild
* aActor
,
86 UniquePtr
<DatabaseSpec
> aSpec
);
88 void AssertIsOnOwningThread() const
96 nsIEventTarget
* EventTarget() const;
98 const nsString
& Name() const;
100 void GetName(nsAString
& aName
) const {
101 AssertIsOnOwningThread();
106 uint64_t Version() const;
108 [[nodiscard
]] RefPtr
<Document
> GetOwnerDocument() const;
111 AssertIsOnOwningThread();
116 bool IsClosed() const {
117 AssertIsOnOwningThread();
124 // Whether or not the database has been invalidated. If it has then no further
125 // transactions for this database will be allowed to run.
126 bool IsInvalidated() const {
127 AssertIsOnOwningThread();
132 void SetQuotaExceeded() { mQuotaExceeded
= true; }
134 void EnterSetVersionTransaction(uint64_t aNewVersion
);
136 void ExitSetVersionTransaction();
138 // Called when a versionchange transaction is aborted to reset the
140 void RevertToPreviousState();
142 void RegisterTransaction(IDBTransaction
& aTransaction
);
144 void UnregisterTransaction(IDBTransaction
& aTransaction
);
146 void AbortTransactions(bool aShouldWarn
);
148 indexedDB::PBackgroundIDBDatabaseFileChild
* GetOrCreateFileActorForBlob(
151 void NoteFinishedFileActor(
152 indexedDB::PBackgroundIDBDatabaseFileChild
* aFileActor
);
154 void NoteActiveTransaction();
156 void NoteInactiveTransaction();
158 [[nodiscard
]] RefPtr
<DOMStringList
> ObjectStoreNames() const;
160 [[nodiscard
]] RefPtr
<IDBObjectStore
> CreateObjectStore(
161 const nsAString
& aName
,
162 const IDBObjectStoreParameters
& aOptionalParameters
, ErrorResult
& aRv
);
164 void DeleteObjectStore(const nsAString
& name
, ErrorResult
& aRv
);
166 // This will be called from the DOM.
167 [[nodiscard
]] RefPtr
<IDBTransaction
> Transaction(
168 JSContext
* aCx
, const StringOrStringSequence
& aStoreNames
,
169 IDBTransactionMode aMode
, ErrorResult
& aRv
);
171 IMPL_EVENT_HANDLER(abort
)
172 IMPL_EVENT_HANDLER(close
)
173 IMPL_EVENT_HANDLER(error
)
174 IMPL_EVENT_HANDLER(versionchange
)
176 void ClearBackgroundActor() {
177 AssertIsOnOwningThread();
179 // Decrease the number of active databases if it was not done in
181 MaybeDecreaseActiveDatabaseCount();
183 mBackgroundActor
= nullptr;
186 const DatabaseSpec
* Spec() const { return mSpec
.get(); }
188 template <typename Pred
>
189 indexedDB::ObjectStoreSpec
* LookupModifiableObjectStoreSpec(Pred
&& aPred
) {
190 auto& objectStores
= mSpec
->objectStores();
192 std::find_if(objectStores
.begin(), objectStores
.end(), aPred
);
193 return foundIt
!= objectStores
.end() ? &*foundIt
: nullptr;
196 NS_DECL_ISUPPORTS_INHERITED
197 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBDatabase
, DOMEventTargetHelper
)
199 // DOMEventTargetHelper
200 void DisconnectFromOwner() override
;
202 virtual void LastRelease() override
;
205 virtual JSObject
* WrapObject(JSContext
* aCx
,
206 JS::Handle
<JSObject
*> aGivenProto
) override
;
209 IDBDatabase(IDBOpenDBRequest
* aRequest
, SafeRefPtr
<IDBFactory
> aFactory
,
210 indexedDB::BackgroundDatabaseChild
* aActor
,
211 UniquePtr
<DatabaseSpec
> aSpec
);
215 void CloseInternal();
217 void InvalidateInternal();
219 bool RunningVersionChangeTransaction() const {
220 AssertIsOnOwningThread();
222 return !!mPreviousSpec
;
225 void RefreshSpec(bool aMayDelete
);
227 void ExpireFileActors(bool aExpireAll
);
229 void NoteInactiveTransactionDelayed();
231 void LogWarning(const char* aMessageName
, const nsAString
& aFilename
,
232 uint32_t aLineNumber
, uint32_t aColumnNumber
);
234 // Only accessed by IDBObjectStore.
235 nsresult
RenameObjectStore(int64_t aObjectStoreId
, const nsAString
& aName
);
237 // Only accessed by IDBIndex.
238 nsresult
RenameIndex(int64_t aObjectStoreId
, int64_t aIndexId
,
239 const nsAString
& aName
);
241 void IncreaseActiveDatabaseCount();
243 void MaybeDecreaseActiveDatabaseCount();
247 } // namespace mozilla
249 #endif // mozilla_dom_idbdatabase_h__