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