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_idbobjectstore_h__
8 #define mozilla_dom_idbobjectstore_h__
10 #include "IDBCursor.h"
11 #include "js/RootingAPI.h"
12 #include "mozilla/dom/IDBCursorBinding.h"
13 #include "mozilla/dom/IDBIndexBinding.h"
14 #include "mozilla/UniquePtr.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsISupports.h"
19 #include "nsWrapperCache.h"
22 class nsIGlobalObject
;
33 class StringOrStringSequence
;
40 class IndexUpdateInfo
;
41 class ObjectStoreSpec
;
42 struct StructuredCloneReadInfoChild
;
43 } // namespace indexedDB
45 class IDBObjectStore final
: public nsISupports
, public nsWrapperCache
{
46 using IndexUpdateInfo
= indexedDB::IndexUpdateInfo
;
47 using Key
= indexedDB::Key
;
48 using KeyPath
= indexedDB::KeyPath
;
49 using ObjectStoreSpec
= indexedDB::ObjectStoreSpec
;
50 using StructuredCloneReadInfoChild
= indexedDB::StructuredCloneReadInfoChild
;
51 using VoidOrObjectStoreKeyPathString
= nsAString
;
53 // For AddOrPut() and DeleteInternal().
54 // TODO Consider removing this, and making the functions public?
55 template <IDBCursor::Type
>
56 friend class IDBTypedCursor
;
58 static const JSClass sDummyPropJSClass
;
60 // TODO: This could be made const if Bug 1575173 is resolved. It is
61 // initialized in the constructor and never modified/cleared.
62 SafeRefPtr
<IDBTransaction
> mTransaction
;
63 JS::Heap
<JS::Value
> mCachedKeyPath
;
65 // This normally points to the ObjectStoreSpec owned by the parent IDBDatabase
66 // object. However, if this objectStore is part of a versionchange transaction
67 // and it gets deleted then the spec is copied into mDeletedSpec and mSpec is
68 // set to point at mDeletedSpec.
69 ObjectStoreSpec
* mSpec
;
70 UniquePtr
<ObjectStoreSpec
> mDeletedSpec
;
72 nsTArray
<RefPtr
<IDBIndex
>> mIndexes
;
73 nsTArray
<RefPtr
<IDBIndex
>> mDeletedIndexes
;
79 struct StructuredCloneWriteInfo
;
80 struct StructuredCloneInfo
;
82 class MOZ_STACK_CLASS ValueWrapper final
{
83 JS::Rooted
<JS::Value
> mValue
;
87 ValueWrapper(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
)
88 : mValue(aCx
, aValue
), mCloned(false) {
89 MOZ_COUNT_CTOR(IDBObjectStore::ValueWrapper
);
92 MOZ_COUNTED_DTOR_NESTED(ValueWrapper
, IDBObjectStore::ValueWrapper
)
94 const JS::Rooted
<JS::Value
>& Value() const { return mValue
; }
96 bool Clone(JSContext
* aCx
);
99 [[nodiscard
]] static RefPtr
<IDBObjectStore
> Create(
100 SafeRefPtr
<IDBTransaction
> aTransaction
, ObjectStoreSpec
& aSpec
);
102 static void AppendIndexUpdateInfo(
103 int64_t aIndexID
, const KeyPath
& aKeyPath
, bool aMultiEntry
,
104 const nsCString
& aLocale
, JSContext
* aCx
, JS::Handle
<JS::Value
> aVal
,
105 nsTArray
<IndexUpdateInfo
>* aUpdateInfoArray
,
106 const VoidOrObjectStoreKeyPathString
& aAutoIncrementedObjectStoreKeyPath
,
109 static void ClearCloneReadInfo(
110 indexedDB::StructuredCloneReadInfoChild
& aReadInfo
);
112 static bool DeserializeValue(JSContext
* aCx
,
113 StructuredCloneReadInfoChild
&& aCloneReadInfo
,
114 JS::MutableHandle
<JS::Value
> aValue
);
116 static const JSClass
* DummyPropClass() { return &sDummyPropJSClass
; }
118 void AssertIsOnOwningThread() const
127 AssertIsOnOwningThread();
132 const nsString
& Name() const;
134 bool AutoIncrement() const;
136 const KeyPath
& GetKeyPath() const;
138 bool HasValidKeyPath() const;
140 nsIGlobalObject
* GetParentObject() const;
142 void GetName(nsString
& aName
) const {
143 AssertIsOnOwningThread();
148 void SetName(const nsAString
& aName
, ErrorResult
& aRv
);
150 void GetKeyPath(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aResult
,
153 [[nodiscard
]] RefPtr
<DOMStringList
> IndexNames();
155 const IDBTransaction
& TransactionRef() const {
156 AssertIsOnOwningThread();
158 return *mTransaction
;
161 IDBTransaction
& MutableTransactionRef() {
162 AssertIsOnOwningThread();
164 return *mTransaction
;
167 SafeRefPtr
<IDBTransaction
> AcquireTransaction() const {
168 AssertIsOnOwningThread();
170 return mTransaction
.clonePtr();
173 RefPtr
<IDBTransaction
> Transaction() const {
174 AssertIsOnOwningThread();
176 return AsRefPtr(mTransaction
.clonePtr());
179 [[nodiscard
]] RefPtr
<IDBRequest
> Add(JSContext
* aCx
,
180 JS::Handle
<JS::Value
> aValue
,
181 JS::Handle
<JS::Value
> aKey
,
184 [[nodiscard
]] RefPtr
<IDBRequest
> Put(JSContext
* aCx
,
185 JS::Handle
<JS::Value
> aValue
,
186 JS::Handle
<JS::Value
> aKey
,
189 [[nodiscard
]] RefPtr
<IDBRequest
> Delete(JSContext
* aCx
,
190 JS::Handle
<JS::Value
> aKey
,
193 [[nodiscard
]] RefPtr
<IDBRequest
> Get(JSContext
* aCx
,
194 JS::Handle
<JS::Value
> aKey
,
197 [[nodiscard
]] RefPtr
<IDBRequest
> GetKey(JSContext
* aCx
,
198 JS::Handle
<JS::Value
> aKey
,
201 [[nodiscard
]] RefPtr
<IDBRequest
> Clear(JSContext
* aCx
, ErrorResult
& aRv
);
203 [[nodiscard
]] RefPtr
<IDBIndex
> CreateIndex(
204 const nsAString
& aName
, const StringOrStringSequence
& aKeyPath
,
205 const IDBIndexParameters
& aOptionalParameters
, ErrorResult
& aRv
);
207 [[nodiscard
]] RefPtr
<IDBIndex
> Index(const nsAString
& aName
,
210 void DeleteIndex(const nsAString
& aName
, ErrorResult
& aRv
);
212 [[nodiscard
]] RefPtr
<IDBRequest
> Count(JSContext
* aCx
,
213 JS::Handle
<JS::Value
> aKey
,
216 [[nodiscard
]] RefPtr
<IDBRequest
> GetAll(JSContext
* aCx
,
217 JS::Handle
<JS::Value
> aKey
,
218 const Optional
<uint32_t>& aLimit
,
221 [[nodiscard
]] RefPtr
<IDBRequest
> GetAllKeys(JSContext
* aCx
,
222 JS::Handle
<JS::Value
> aKey
,
223 const Optional
<uint32_t>& aLimit
,
226 [[nodiscard
]] RefPtr
<IDBRequest
> OpenCursor(JSContext
* aCx
,
227 JS::Handle
<JS::Value
> aRange
,
228 IDBCursorDirection aDirection
,
231 [[nodiscard
]] RefPtr
<IDBRequest
> OpenCursor(JSContext
* aCx
,
232 IDBCursorDirection aDirection
,
235 [[nodiscard
]] RefPtr
<IDBRequest
> OpenKeyCursor(JSContext
* aCx
,
236 JS::Handle
<JS::Value
> aRange
,
237 IDBCursorDirection aDirection
,
240 void RefreshSpec(bool aMayDelete
);
242 const ObjectStoreSpec
& Spec() const;
246 bool IsDeleted() const {
247 AssertIsOnOwningThread();
249 return !!mDeletedSpec
;
252 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
253 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBObjectStore
)
256 virtual JSObject
* WrapObject(JSContext
* aCx
,
257 JS::Handle
<JSObject
*> aGivenProto
) override
;
260 IDBObjectStore(SafeRefPtr
<IDBTransaction
> aTransaction
,
261 ObjectStoreSpec
* aSpec
);
265 void GetAddInfo(JSContext
* aCx
, ValueWrapper
& aValueWrapper
,
266 JS::Handle
<JS::Value
> aKeyVal
,
267 StructuredCloneWriteInfo
& aCloneWriteInfo
, Key
& aKey
,
268 nsTArray
<IndexUpdateInfo
>& aUpdateInfoArray
,
271 [[nodiscard
]] RefPtr
<IDBRequest
> AddOrPut(JSContext
* aCx
,
272 ValueWrapper
& aValueWrapper
,
273 JS::Handle
<JS::Value
> aKey
,
274 bool aOverwrite
, bool aFromCursor
,
277 [[nodiscard
]] RefPtr
<IDBRequest
> DeleteInternal(JSContext
* aCx
,
278 JS::Handle
<JS::Value
> aKey
,
282 [[nodiscard
]] RefPtr
<IDBRequest
> GetInternal(bool aKeyOnly
, JSContext
* aCx
,
283 JS::Handle
<JS::Value
> aKey
,
286 [[nodiscard
]] RefPtr
<IDBRequest
> GetAllInternal(
287 bool aKeysOnly
, JSContext
* aCx
, JS::Handle
<JS::Value
> aKey
,
288 const Optional
<uint32_t>& aLimit
, ErrorResult
& aRv
);
290 [[nodiscard
]] RefPtr
<IDBRequest
> OpenCursorInternal(
291 bool aKeysOnly
, JSContext
* aCx
, JS::Handle
<JS::Value
> aRange
,
292 IDBCursorDirection aDirection
, ErrorResult
& aRv
);
296 } // namespace mozilla
298 #endif // mozilla_dom_idbobjectstore_h__