IDBObjectStore.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_idbobjectstore_h__
  6. #define mozilla_dom_idbobjectstore_h__
  7. #include "js/RootingAPI.h"
  8. #include "mozilla/dom/IDBCursorBinding.h"
  9. #include "mozilla/dom/IDBIndexBinding.h"
  10. #include "nsAutoPtr.h"
  11. #include "nsCycleCollectionParticipant.h"
  12. #include "nsISupports.h"
  13. #include "nsString.h"
  14. #include "nsTArray.h"
  15. #include "nsWrapperCache.h"
  16. struct JSClass;
  17. class nsPIDOMWindowInner;
  18. namespace mozilla {
  19. class ErrorResult;
  20. namespace dom {
  21. class DOMStringList;
  22. class IDBCursor;
  23. class IDBRequest;
  24. class IDBTransaction;
  25. class StringOrStringSequence;
  26. template <typename> class Sequence;
  27. namespace indexedDB {
  28. class Key;
  29. class KeyPath;
  30. class IndexUpdateInfo;
  31. class ObjectStoreSpec;
  32. struct StructuredCloneReadInfo;
  33. } // namespace indexedDB
  34. class IDBObjectStore final
  35. : public nsISupports
  36. , public nsWrapperCache
  37. {
  38. typedef indexedDB::IndexUpdateInfo IndexUpdateInfo;
  39. typedef indexedDB::Key Key;
  40. typedef indexedDB::KeyPath KeyPath;
  41. typedef indexedDB::ObjectStoreSpec ObjectStoreSpec;
  42. typedef indexedDB::StructuredCloneReadInfo StructuredCloneReadInfo;
  43. // For AddOrPut() and DeleteInternal().
  44. friend class IDBCursor;
  45. static const JSClass sDummyPropJSClass;
  46. RefPtr<IDBTransaction> mTransaction;
  47. JS::Heap<JS::Value> mCachedKeyPath;
  48. // This normally points to the ObjectStoreSpec owned by the parent IDBDatabase
  49. // object. However, if this objectStore is part of a versionchange transaction
  50. // and it gets deleted then the spec is copied into mDeletedSpec and mSpec is
  51. // set to point at mDeletedSpec.
  52. const ObjectStoreSpec* mSpec;
  53. nsAutoPtr<ObjectStoreSpec> mDeletedSpec;
  54. nsTArray<RefPtr<IDBIndex>> mIndexes;
  55. nsTArray<RefPtr<IDBIndex>> mDeletedIndexes;
  56. const int64_t mId;
  57. bool mRooted;
  58. public:
  59. struct StructuredCloneWriteInfo;
  60. static already_AddRefed<IDBObjectStore>
  61. Create(IDBTransaction* aTransaction, const ObjectStoreSpec& aSpec);
  62. static nsresult
  63. AppendIndexUpdateInfo(int64_t aIndexID,
  64. const KeyPath& aKeyPath,
  65. bool aUnique,
  66. bool aMultiEntry,
  67. const nsCString& aLocale,
  68. JSContext* aCx,
  69. JS::Handle<JS::Value> aObject,
  70. nsTArray<IndexUpdateInfo>& aUpdateInfoArray);
  71. static void
  72. ClearCloneReadInfo(StructuredCloneReadInfo& aReadInfo);
  73. static bool
  74. DeserializeValue(JSContext* aCx,
  75. StructuredCloneReadInfo& aCloneReadInfo,
  76. JS::MutableHandle<JS::Value> aValue);
  77. static bool
  78. DeserializeIndexValue(JSContext* aCx,
  79. StructuredCloneReadInfo& aCloneReadInfo,
  80. JS::MutableHandle<JS::Value> aValue);
  81. static bool
  82. DeserializeUpgradeValue(JSContext* aCx,
  83. StructuredCloneReadInfo& aCloneReadInfo,
  84. JS::MutableHandle<JS::Value> aValue);
  85. static const JSClass*
  86. DummyPropClass()
  87. {
  88. return &sDummyPropJSClass;
  89. }
  90. void
  91. AssertIsOnOwningThread() const
  92. #ifdef DEBUG
  93. ;
  94. #else
  95. { }
  96. #endif
  97. int64_t
  98. Id() const
  99. {
  100. AssertIsOnOwningThread();
  101. return mId;
  102. }
  103. const nsString&
  104. Name() const;
  105. bool
  106. AutoIncrement() const;
  107. const KeyPath&
  108. GetKeyPath() const;
  109. bool
  110. HasValidKeyPath() const;
  111. nsPIDOMWindowInner*
  112. GetParentObject() const;
  113. void
  114. GetName(nsString& aName) const
  115. {
  116. AssertIsOnOwningThread();
  117. aName = Name();
  118. }
  119. void
  120. SetName(const nsAString& aName, ErrorResult& aRv);
  121. void
  122. GetKeyPath(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
  123. ErrorResult& aRv);
  124. already_AddRefed<DOMStringList>
  125. IndexNames();
  126. IDBTransaction*
  127. Transaction() const
  128. {
  129. AssertIsOnOwningThread();
  130. return mTransaction;
  131. }
  132. already_AddRefed<IDBRequest>
  133. Add(JSContext* aCx,
  134. JS::Handle<JS::Value> aValue,
  135. JS::Handle<JS::Value> aKey,
  136. ErrorResult& aRv)
  137. {
  138. AssertIsOnOwningThread();
  139. return AddOrPut(aCx, aValue, aKey, false, /* aFromCursor */ false, aRv);
  140. }
  141. already_AddRefed<IDBRequest>
  142. Put(JSContext* aCx,
  143. JS::Handle<JS::Value> aValue,
  144. JS::Handle<JS::Value> aKey,
  145. ErrorResult& aRv)
  146. {
  147. AssertIsOnOwningThread();
  148. return AddOrPut(aCx, aValue, aKey, true, /* aFromCursor */ false, aRv);
  149. }
  150. already_AddRefed<IDBRequest>
  151. Delete(JSContext* aCx,
  152. JS::Handle<JS::Value> aKey,
  153. ErrorResult& aRv)
  154. {
  155. AssertIsOnOwningThread();
  156. return DeleteInternal(aCx, aKey, /* aFromCursor */ false, aRv);
  157. }
  158. already_AddRefed<IDBRequest>
  159. Get(JSContext* aCx,
  160. JS::Handle<JS::Value> aKey,
  161. ErrorResult& aRv)
  162. {
  163. AssertIsOnOwningThread();
  164. return GetInternal(/* aKeyOnly */ false, aCx, aKey, aRv);
  165. }
  166. already_AddRefed<IDBRequest>
  167. GetKey(JSContext* aCx,
  168. JS::Handle<JS::Value> aKey,
  169. ErrorResult& aRv)
  170. {
  171. AssertIsOnOwningThread();
  172. return GetInternal(/* aKeyOnly */ true, aCx, aKey, aRv);
  173. }
  174. already_AddRefed<IDBRequest>
  175. Clear(JSContext* aCx, ErrorResult& aRv);
  176. already_AddRefed<IDBIndex>
  177. CreateIndex(const nsAString& aName,
  178. const StringOrStringSequence& aKeyPath,
  179. const IDBIndexParameters& aOptionalParameters,
  180. ErrorResult& aRv);
  181. already_AddRefed<IDBIndex>
  182. Index(const nsAString& aName, ErrorResult &aRv);
  183. void
  184. DeleteIndex(const nsAString& aIndexName, ErrorResult& aRv);
  185. already_AddRefed<IDBRequest>
  186. Count(JSContext* aCx,
  187. JS::Handle<JS::Value> aKey,
  188. ErrorResult& aRv);
  189. already_AddRefed<IDBRequest>
  190. GetAll(JSContext* aCx,
  191. JS::Handle<JS::Value> aKey,
  192. const Optional<uint32_t>& aLimit,
  193. ErrorResult& aRv)
  194. {
  195. AssertIsOnOwningThread();
  196. return GetAllInternal(/* aKeysOnly */ false, aCx, aKey, aLimit, aRv);
  197. }
  198. already_AddRefed<IDBRequest>
  199. GetAllKeys(JSContext* aCx,
  200. JS::Handle<JS::Value> aKey,
  201. const Optional<uint32_t>& aLimit,
  202. ErrorResult& aRv)
  203. {
  204. AssertIsOnOwningThread();
  205. return GetAllInternal(/* aKeysOnly */ true, aCx, aKey, aLimit, aRv);
  206. }
  207. already_AddRefed<IDBRequest>
  208. OpenCursor(JSContext* aCx,
  209. JS::Handle<JS::Value> aRange,
  210. IDBCursorDirection aDirection,
  211. ErrorResult& aRv)
  212. {
  213. AssertIsOnOwningThread();
  214. return OpenCursorInternal(/* aKeysOnly */ false, aCx, aRange, aDirection,
  215. aRv);
  216. }
  217. already_AddRefed<IDBRequest>
  218. OpenCursor(JSContext* aCx,
  219. IDBCursorDirection aDirection,
  220. ErrorResult& aRv)
  221. {
  222. AssertIsOnOwningThread();
  223. return OpenCursorInternal(/* aKeysOnly */ false, aCx,
  224. JS::UndefinedHandleValue, aDirection, aRv);
  225. }
  226. already_AddRefed<IDBRequest>
  227. OpenKeyCursor(JSContext* aCx,
  228. JS::Handle<JS::Value> aRange,
  229. IDBCursorDirection aDirection,
  230. ErrorResult& aRv)
  231. {
  232. AssertIsOnOwningThread();
  233. return OpenCursorInternal(/* aKeysOnly */ true, aCx, aRange, aDirection,
  234. aRv);
  235. }
  236. void
  237. RefreshSpec(bool aMayDelete);
  238. const ObjectStoreSpec&
  239. Spec() const;
  240. void
  241. NoteDeletion();
  242. bool
  243. IsDeleted() const
  244. {
  245. AssertIsOnOwningThread();
  246. return !!mDeletedSpec;
  247. }
  248. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  249. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBObjectStore)
  250. // nsWrapperCache
  251. virtual JSObject*
  252. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  253. private:
  254. IDBObjectStore(IDBTransaction* aTransaction, const ObjectStoreSpec* aSpec);
  255. ~IDBObjectStore();
  256. nsresult
  257. GetAddInfo(JSContext* aCx,
  258. JS::Handle<JS::Value> aValue,
  259. JS::Handle<JS::Value> aKeyVal,
  260. StructuredCloneWriteInfo& aCloneWriteInfo,
  261. Key& aKey,
  262. nsTArray<IndexUpdateInfo>& aUpdateInfoArray);
  263. already_AddRefed<IDBRequest>
  264. AddOrPut(JSContext* aCx,
  265. JS::Handle<JS::Value> aValue,
  266. JS::Handle<JS::Value> aKey,
  267. bool aOverwrite,
  268. bool aFromCursor,
  269. ErrorResult& aRv);
  270. already_AddRefed<IDBRequest>
  271. DeleteInternal(JSContext* aCx,
  272. JS::Handle<JS::Value> aKey,
  273. bool aFromCursor,
  274. ErrorResult& aRv);
  275. already_AddRefed<IDBRequest>
  276. GetInternal(bool aKeyOnly,
  277. JSContext* aCx,
  278. JS::Handle<JS::Value> aKey,
  279. ErrorResult& aRv);
  280. already_AddRefed<IDBRequest>
  281. GetAllInternal(bool aKeysOnly,
  282. JSContext* aCx,
  283. JS::Handle<JS::Value> aKey,
  284. const Optional<uint32_t>& aLimit,
  285. ErrorResult& aRv);
  286. already_AddRefed<IDBRequest>
  287. OpenCursorInternal(bool aKeysOnly,
  288. JSContext* aCx,
  289. JS::Handle<JS::Value> aRange,
  290. IDBCursorDirection aDirection,
  291. ErrorResult& aRv);
  292. };
  293. } // namespace dom
  294. } // namespace mozilla
  295. #endif // mozilla_dom_idbobjectstore_h__