IDBIndex.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_idbindex_h__
  6. #define mozilla_dom_idbindex_h__
  7. #include "js/RootingAPI.h"
  8. #include "mozilla/Attributes.h"
  9. #include "mozilla/dom/IDBCursorBinding.h"
  10. #include "nsAutoPtr.h"
  11. #include "nsCycleCollectionParticipant.h"
  12. #include "nsISupports.h"
  13. #include "nsTArrayForwardDeclare.h"
  14. #include "nsWrapperCache.h"
  15. class nsPIDOMWindowInner;
  16. namespace mozilla {
  17. class ErrorResult;
  18. namespace dom {
  19. class IDBObjectStore;
  20. class IDBRequest;
  21. template <typename> class Sequence;
  22. namespace indexedDB {
  23. class IndexMetadata;
  24. class KeyPath;
  25. } // namespace indexedDB
  26. class IDBIndex final
  27. : public nsISupports
  28. , public nsWrapperCache
  29. {
  30. RefPtr<IDBObjectStore> mObjectStore;
  31. JS::Heap<JS::Value> mCachedKeyPath;
  32. // This normally points to the IndexMetadata owned by the parent IDBDatabase
  33. // object. However, if this index is part of a versionchange transaction and
  34. // it gets deleted then the metadata is copied into mDeletedMetadata and
  35. // mMetadata is set to point at mDeletedMetadata.
  36. const indexedDB::IndexMetadata* mMetadata;
  37. nsAutoPtr<indexedDB::IndexMetadata> mDeletedMetadata;
  38. const int64_t mId;
  39. bool mRooted;
  40. public:
  41. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  42. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBIndex)
  43. static already_AddRefed<IDBIndex>
  44. Create(IDBObjectStore* aObjectStore, const indexedDB::IndexMetadata& aMetadata);
  45. int64_t
  46. Id() const
  47. {
  48. AssertIsOnOwningThread();
  49. return mId;
  50. }
  51. const nsString&
  52. Name() const;
  53. bool
  54. Unique() const;
  55. bool
  56. MultiEntry() const;
  57. bool
  58. LocaleAware() const;
  59. const indexedDB::KeyPath&
  60. GetKeyPath() const;
  61. void
  62. GetLocale(nsString& aLocale) const;
  63. const nsCString&
  64. Locale() const;
  65. bool
  66. IsAutoLocale() const;
  67. IDBObjectStore*
  68. ObjectStore() const
  69. {
  70. AssertIsOnOwningThread();
  71. return mObjectStore;
  72. }
  73. nsPIDOMWindowInner*
  74. GetParentObject() const;
  75. void
  76. GetName(nsString& aName) const
  77. {
  78. aName = Name();
  79. }
  80. void
  81. SetName(const nsAString& aName, ErrorResult& aRv);
  82. void
  83. GetKeyPath(JSContext* aCx,
  84. JS::MutableHandle<JS::Value> aResult,
  85. ErrorResult& aRv);
  86. already_AddRefed<IDBRequest>
  87. OpenCursor(JSContext* aCx,
  88. JS::Handle<JS::Value> aRange,
  89. IDBCursorDirection aDirection,
  90. ErrorResult& aRv)
  91. {
  92. AssertIsOnOwningThread();
  93. return OpenCursorInternal(/* aKeysOnly */ false, aCx, aRange, aDirection,
  94. aRv);
  95. }
  96. already_AddRefed<IDBRequest>
  97. OpenKeyCursor(JSContext* aCx,
  98. JS::Handle<JS::Value> aRange,
  99. IDBCursorDirection aDirection,
  100. ErrorResult& aRv)
  101. {
  102. AssertIsOnOwningThread();
  103. return OpenCursorInternal(/* aKeysOnly */ true, aCx, aRange, aDirection,
  104. aRv);
  105. }
  106. already_AddRefed<IDBRequest>
  107. Get(JSContext* aCx, JS::Handle<JS::Value> aKey, ErrorResult& aRv)
  108. {
  109. AssertIsOnOwningThread();
  110. return GetInternal(/* aKeyOnly */ false, aCx, aKey, aRv);
  111. }
  112. already_AddRefed<IDBRequest>
  113. GetKey(JSContext* aCx, JS::Handle<JS::Value> aKey, ErrorResult& aRv)
  114. {
  115. AssertIsOnOwningThread();
  116. return GetInternal(/* aKeyOnly */ true, aCx, aKey, aRv);
  117. }
  118. already_AddRefed<IDBRequest>
  119. Count(JSContext* aCx, JS::Handle<JS::Value> aKey,
  120. ErrorResult& aRv);
  121. already_AddRefed<IDBRequest>
  122. GetAll(JSContext* aCx, JS::Handle<JS::Value> aKey,
  123. const Optional<uint32_t>& aLimit, ErrorResult& aRv)
  124. {
  125. AssertIsOnOwningThread();
  126. return GetAllInternal(/* aKeysOnly */ false, aCx, aKey, aLimit, aRv);
  127. }
  128. already_AddRefed<IDBRequest>
  129. GetAllKeys(JSContext* aCx, JS::Handle<JS::Value> aKey,
  130. const Optional<uint32_t>& aLimit, ErrorResult& aRv)
  131. {
  132. AssertIsOnOwningThread();
  133. return GetAllInternal(/* aKeysOnly */ true, aCx, aKey, aLimit, aRv);
  134. }
  135. void
  136. RefreshMetadata(bool aMayDelete);
  137. void
  138. NoteDeletion();
  139. bool
  140. IsDeleted() const
  141. {
  142. AssertIsOnOwningThread();
  143. return !!mDeletedMetadata;
  144. }
  145. void
  146. AssertIsOnOwningThread() const
  147. #ifdef DEBUG
  148. ;
  149. #else
  150. { }
  151. #endif
  152. // nsWrapperCache
  153. virtual JSObject*
  154. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  155. private:
  156. IDBIndex(IDBObjectStore* aObjectStore, const indexedDB::IndexMetadata* aMetadata);
  157. ~IDBIndex();
  158. already_AddRefed<IDBRequest>
  159. GetInternal(bool aKeyOnly,
  160. JSContext* aCx,
  161. JS::Handle<JS::Value> aKey,
  162. ErrorResult& aRv);
  163. already_AddRefed<IDBRequest>
  164. GetAllInternal(bool aKeysOnly,
  165. JSContext* aCx,
  166. JS::Handle<JS::Value> aKey,
  167. const Optional<uint32_t>& aLimit,
  168. ErrorResult& aRv);
  169. already_AddRefed<IDBRequest>
  170. OpenCursorInternal(bool aKeysOnly,
  171. JSContext* aCx,
  172. JS::Handle<JS::Value> aRange,
  173. IDBCursorDirection aDirection,
  174. ErrorResult& aRv);
  175. };
  176. } // namespace dom
  177. } // namespace mozilla
  178. #endif // mozilla_dom_idbindex_h__