Bug 1260441 - Never pass a null js context to OpenCursor() r=bz
[gecko.git] / dom / indexedDB / IDBObjectStore.h
blob6c18326a29076ba450bf56dc3f2ad1821c851bce
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 "js/RootingAPI.h"
11 #include "mozilla/dom/IDBCursorBinding.h"
12 #include "mozilla/dom/IDBIndexBinding.h"
13 #include "nsAutoPtr.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsISupports.h"
16 #include "nsString.h"
17 #include "nsTArray.h"
18 #include "nsWrapperCache.h"
20 struct JSClass;
21 class nsPIDOMWindowInner;
23 namespace mozilla {
25 class ErrorResult;
27 namespace dom {
29 class DOMStringList;
30 class IDBCursor;
31 class IDBRequest;
32 class IDBTransaction;
33 template <typename> class Sequence;
35 namespace indexedDB {
36 class Key;
37 class KeyPath;
38 class IndexUpdateInfo;
39 class ObjectStoreSpec;
40 struct StructuredCloneReadInfo;
41 } // namespace indexedDB
43 class IDBObjectStore final
44 : public nsISupports
45 , public nsWrapperCache
47 typedef indexedDB::IndexUpdateInfo IndexUpdateInfo;
48 typedef indexedDB::Key Key;
49 typedef indexedDB::KeyPath KeyPath;
50 typedef indexedDB::ObjectStoreSpec ObjectStoreSpec;
51 typedef indexedDB::StructuredCloneReadInfo StructuredCloneReadInfo;
53 // For AddOrPut() and DeleteInternal().
54 friend class IDBCursor;
56 static const JSClass sDummyPropJSClass;
58 RefPtr<IDBTransaction> mTransaction;
59 JS::Heap<JS::Value> mCachedKeyPath;
61 // This normally points to the ObjectStoreSpec owned by the parent IDBDatabase
62 // object. However, if this objectStore is part of a versionchange transaction
63 // and it gets deleted then the spec is copied into mDeletedSpec and mSpec is
64 // set to point at mDeletedSpec.
65 const ObjectStoreSpec* mSpec;
66 nsAutoPtr<ObjectStoreSpec> mDeletedSpec;
68 nsTArray<RefPtr<IDBIndex>> mIndexes;
70 const int64_t mId;
71 bool mRooted;
73 public:
74 struct StructuredCloneWriteInfo;
76 static already_AddRefed<IDBObjectStore>
77 Create(IDBTransaction* aTransaction, const ObjectStoreSpec& aSpec);
79 static nsresult
80 AppendIndexUpdateInfo(int64_t aIndexID,
81 const KeyPath& aKeyPath,
82 bool aUnique,
83 bool aMultiEntry,
84 const nsCString& aLocale,
85 JSContext* aCx,
86 JS::Handle<JS::Value> aObject,
87 nsTArray<IndexUpdateInfo>& aUpdateInfoArray);
89 static void
90 ClearCloneReadInfo(StructuredCloneReadInfo& aReadInfo);
92 static bool
93 DeserializeValue(JSContext* aCx,
94 StructuredCloneReadInfo& aCloneReadInfo,
95 JS::MutableHandle<JS::Value> aValue);
97 static bool
98 DeserializeIndexValue(JSContext* aCx,
99 StructuredCloneReadInfo& aCloneReadInfo,
100 JS::MutableHandle<JS::Value> aValue);
102 #if !defined(MOZ_B2G)
103 static bool
104 DeserializeUpgradeValue(JSContext* aCx,
105 StructuredCloneReadInfo& aCloneReadInfo,
106 JS::MutableHandle<JS::Value> aValue);
107 #endif
109 static const JSClass*
110 DummyPropClass()
112 return &sDummyPropJSClass;
115 void
116 AssertIsOnOwningThread() const
117 #ifdef DEBUG
119 #else
121 #endif
123 int64_t
124 Id() const
126 AssertIsOnOwningThread();
128 return mId;
131 const nsString&
132 Name() const;
134 bool
135 AutoIncrement() const;
137 const KeyPath&
138 GetKeyPath() const;
140 bool
141 HasValidKeyPath() const;
143 nsPIDOMWindowInner*
144 GetParentObject() const;
146 void
147 GetName(nsString& aName) const
149 AssertIsOnOwningThread();
151 aName = Name();
154 void
155 GetKeyPath(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
156 ErrorResult& aRv);
158 already_AddRefed<DOMStringList>
159 IndexNames();
161 IDBTransaction*
162 Transaction() const
164 AssertIsOnOwningThread();
166 return mTransaction;
169 already_AddRefed<IDBRequest>
170 Add(JSContext* aCx,
171 JS::Handle<JS::Value> aValue,
172 JS::Handle<JS::Value> aKey,
173 ErrorResult& aRv)
175 AssertIsOnOwningThread();
177 return AddOrPut(aCx, aValue, aKey, false, /* aFromCursor */ false, aRv);
180 already_AddRefed<IDBRequest>
181 Put(JSContext* aCx,
182 JS::Handle<JS::Value> aValue,
183 JS::Handle<JS::Value> aKey,
184 ErrorResult& aRv)
186 AssertIsOnOwningThread();
188 return AddOrPut(aCx, aValue, aKey, true, /* aFromCursor */ false, aRv);
191 already_AddRefed<IDBRequest>
192 Delete(JSContext* aCx,
193 JS::Handle<JS::Value> aKey,
194 ErrorResult& aRv)
196 AssertIsOnOwningThread();
198 return DeleteInternal(aCx, aKey, /* aFromCursor */ false, aRv);
201 already_AddRefed<IDBRequest>
202 Get(JSContext* aCx, JS::Handle<JS::Value> aKey, ErrorResult& aRv);
204 already_AddRefed<IDBRequest>
205 Clear(JSContext* aCx, ErrorResult& aRv);
207 already_AddRefed<IDBIndex>
208 CreateIndex(const nsAString& aName,
209 const nsAString& aKeyPath,
210 const IDBIndexParameters& aOptionalParameters,
211 ErrorResult& aRv);
213 already_AddRefed<IDBIndex>
214 CreateIndex(const nsAString& aName,
215 const Sequence<nsString>& aKeyPath,
216 const IDBIndexParameters& aOptionalParameters,
217 ErrorResult& aRv);
219 already_AddRefed<IDBIndex>
220 Index(const nsAString& aName, ErrorResult &aRv);
222 void
223 DeleteIndex(const nsAString& aIndexName, ErrorResult& aRv);
225 already_AddRefed<IDBRequest>
226 Count(JSContext* aCx,
227 JS::Handle<JS::Value> aKey,
228 ErrorResult& aRv);
230 already_AddRefed<IDBRequest>
231 GetAll(JSContext* aCx,
232 JS::Handle<JS::Value> aKey,
233 const Optional<uint32_t>& aLimit,
234 ErrorResult& aRv)
236 AssertIsOnOwningThread();
238 return GetAllInternal(/* aKeysOnly */ false, aCx, aKey, aLimit, aRv);
241 already_AddRefed<IDBRequest>
242 GetAllKeys(JSContext* aCx,
243 JS::Handle<JS::Value> aKey,
244 const Optional<uint32_t>& aLimit,
245 ErrorResult& aRv)
247 AssertIsOnOwningThread();
249 return GetAllInternal(/* aKeysOnly */ true, aCx, aKey, aLimit, aRv);
252 already_AddRefed<IDBRequest>
253 OpenCursor(JSContext* aCx,
254 JS::Handle<JS::Value> aRange,
255 IDBCursorDirection aDirection,
256 ErrorResult& aRv)
258 AssertIsOnOwningThread();
260 return OpenCursorInternal(/* aKeysOnly */ false, aCx, aRange, aDirection,
261 aRv);
264 already_AddRefed<IDBRequest>
265 OpenCursor(JSContext* aCx,
266 IDBCursorDirection aDirection,
267 ErrorResult& aRv)
269 AssertIsOnOwningThread();
271 return OpenCursorInternal(/* aKeysOnly */ false, aCx,
272 JS::UndefinedHandleValue, aDirection, aRv);
275 already_AddRefed<IDBRequest>
276 OpenKeyCursor(JSContext* aCx,
277 JS::Handle<JS::Value> aRange,
278 IDBCursorDirection aDirection,
279 ErrorResult& aRv)
281 AssertIsOnOwningThread();
283 return OpenCursorInternal(/* aKeysOnly */ true, aCx, aRange, aDirection,
284 aRv);
287 void
288 RefreshSpec(bool aMayDelete);
290 const ObjectStoreSpec&
291 Spec() const;
293 void
294 NoteDeletion();
296 bool
297 IsDeleted() const
299 AssertIsOnOwningThread();
301 return !!mDeletedSpec;
304 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
305 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBObjectStore)
307 // nsWrapperCache
308 virtual JSObject*
309 WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
311 private:
312 IDBObjectStore(IDBTransaction* aTransaction, const ObjectStoreSpec* aSpec);
314 ~IDBObjectStore();
316 nsresult
317 GetAddInfo(JSContext* aCx,
318 JS::Handle<JS::Value> aValue,
319 JS::Handle<JS::Value> aKeyVal,
320 StructuredCloneWriteInfo& aCloneWriteInfo,
321 Key& aKey,
322 nsTArray<IndexUpdateInfo>& aUpdateInfoArray);
324 already_AddRefed<IDBRequest>
325 AddOrPut(JSContext* aCx,
326 JS::Handle<JS::Value> aValue,
327 JS::Handle<JS::Value> aKey,
328 bool aOverwrite,
329 bool aFromCursor,
330 ErrorResult& aRv);
332 already_AddRefed<IDBRequest>
333 DeleteInternal(JSContext* aCx,
334 JS::Handle<JS::Value> aKey,
335 bool aFromCursor,
336 ErrorResult& aRv);
338 already_AddRefed<IDBRequest>
339 GetAllInternal(bool aKeysOnly,
340 JSContext* aCx,
341 JS::Handle<JS::Value> aKey,
342 const Optional<uint32_t>& aLimit,
343 ErrorResult& aRv);
345 already_AddRefed<IDBIndex>
346 CreateIndexInternal(const nsAString& aName,
347 const KeyPath& aKeyPath,
348 const IDBIndexParameters& aOptionalParameters,
349 ErrorResult& aRv);
351 already_AddRefed<IDBRequest>
352 OpenCursorInternal(bool aKeysOnly,
353 JSContext* aCx,
354 JS::Handle<JS::Value> aRange,
355 IDBCursorDirection aDirection,
356 ErrorResult& aRv);
359 } // namespace dom
360 } // namespace mozilla
362 #endif // mozilla_dom_idbobjectstore_h__