Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / dom / indexedDB / IDBObjectStore.h
blob99bec23327a4d77aa4fe870474d72eb0512b4084
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"
17 #include "nsString.h"
18 #include "nsTArray.h"
19 #include "nsWrapperCache.h"
21 struct JSClass;
22 class nsIGlobalObject;
24 namespace mozilla {
26 class ErrorResult;
28 namespace dom {
30 class DOMStringList;
31 class IDBRequest;
32 class IDBTransaction;
33 class StringOrStringSequence;
34 template <typename>
35 class Sequence;
37 namespace indexedDB {
38 class Key;
39 class KeyPath;
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;
75 const int64_t mId;
76 bool mRooted;
78 public:
79 struct StructuredCloneWriteInfo;
80 struct StructuredCloneInfo;
82 class MOZ_STACK_CLASS ValueWrapper final {
83 JS::Rooted<JS::Value> mValue;
84 bool mCloned;
86 public:
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,
107 ErrorResult* aRv);
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
119 #ifdef DEBUG
121 #else
124 #endif
126 int64_t Id() const {
127 AssertIsOnOwningThread();
129 return mId;
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();
145 aName = Name();
148 void SetName(const nsAString& aName, ErrorResult& aRv);
150 void GetKeyPath(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
151 ErrorResult& aRv);
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,
182 ErrorResult& aRv);
184 [[nodiscard]] RefPtr<IDBRequest> Put(JSContext* aCx,
185 JS::Handle<JS::Value> aValue,
186 JS::Handle<JS::Value> aKey,
187 ErrorResult& aRv);
189 [[nodiscard]] RefPtr<IDBRequest> Delete(JSContext* aCx,
190 JS::Handle<JS::Value> aKey,
191 ErrorResult& aRv);
193 [[nodiscard]] RefPtr<IDBRequest> Get(JSContext* aCx,
194 JS::Handle<JS::Value> aKey,
195 ErrorResult& aRv);
197 [[nodiscard]] RefPtr<IDBRequest> GetKey(JSContext* aCx,
198 JS::Handle<JS::Value> aKey,
199 ErrorResult& aRv);
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,
208 ErrorResult& aRv);
210 void DeleteIndex(const nsAString& aName, ErrorResult& aRv);
212 [[nodiscard]] RefPtr<IDBRequest> Count(JSContext* aCx,
213 JS::Handle<JS::Value> aKey,
214 ErrorResult& aRv);
216 [[nodiscard]] RefPtr<IDBRequest> GetAll(JSContext* aCx,
217 JS::Handle<JS::Value> aKey,
218 const Optional<uint32_t>& aLimit,
219 ErrorResult& aRv);
221 [[nodiscard]] RefPtr<IDBRequest> GetAllKeys(JSContext* aCx,
222 JS::Handle<JS::Value> aKey,
223 const Optional<uint32_t>& aLimit,
224 ErrorResult& aRv);
226 [[nodiscard]] RefPtr<IDBRequest> OpenCursor(JSContext* aCx,
227 JS::Handle<JS::Value> aRange,
228 IDBCursorDirection aDirection,
229 ErrorResult& aRv);
231 [[nodiscard]] RefPtr<IDBRequest> OpenCursor(JSContext* aCx,
232 IDBCursorDirection aDirection,
233 ErrorResult& aRv);
235 [[nodiscard]] RefPtr<IDBRequest> OpenKeyCursor(JSContext* aCx,
236 JS::Handle<JS::Value> aRange,
237 IDBCursorDirection aDirection,
238 ErrorResult& aRv);
240 void RefreshSpec(bool aMayDelete);
242 const ObjectStoreSpec& Spec() const;
244 void NoteDeletion();
246 bool IsDeleted() const {
247 AssertIsOnOwningThread();
249 return !!mDeletedSpec;
252 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
253 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBObjectStore)
255 // nsWrapperCache
256 virtual JSObject* WrapObject(JSContext* aCx,
257 JS::Handle<JSObject*> aGivenProto) override;
259 private:
260 IDBObjectStore(SafeRefPtr<IDBTransaction> aTransaction,
261 ObjectStoreSpec* aSpec);
263 ~IDBObjectStore();
265 void GetAddInfo(JSContext* aCx, ValueWrapper& aValueWrapper,
266 JS::Handle<JS::Value> aKeyVal,
267 StructuredCloneWriteInfo& aCloneWriteInfo, Key& aKey,
268 nsTArray<IndexUpdateInfo>& aUpdateInfoArray,
269 ErrorResult& aRv);
271 [[nodiscard]] RefPtr<IDBRequest> AddOrPut(JSContext* aCx,
272 ValueWrapper& aValueWrapper,
273 JS::Handle<JS::Value> aKey,
274 bool aOverwrite, bool aFromCursor,
275 ErrorResult& aRv);
277 [[nodiscard]] RefPtr<IDBRequest> DeleteInternal(JSContext* aCx,
278 JS::Handle<JS::Value> aKey,
279 bool aFromCursor,
280 ErrorResult& aRv);
282 [[nodiscard]] RefPtr<IDBRequest> GetInternal(bool aKeyOnly, JSContext* aCx,
283 JS::Handle<JS::Value> aKey,
284 ErrorResult& aRv);
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);
295 } // namespace dom
296 } // namespace mozilla
298 #endif // mozilla_dom_idbobjectstore_h__