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 #include "IDBKeyRange.h"
10 #include "mozilla/ErrorResult.h"
11 #include "mozilla/HoldDropJSObjects.h"
12 #include "mozilla/dom/BindingUtils.h"
13 #include "mozilla/dom/IDBKeyRangeBinding.h"
14 #include "mozilla/dom/indexedDB/PBackgroundIDBSharedTypes.h"
16 namespace mozilla::dom
{
18 using namespace mozilla::dom::indexedDB
;
22 void GetKeyFromJSVal(JSContext
* aCx
, JS::Handle
<JS::Value
> aVal
, Key
& aKey
,
24 auto result
= aKey
.SetFromJSVal(aCx
, aVal
);
26 aRv
= result
.unwrapErr().ExtractErrorResult(
27 InvalidMapsTo
<NS_ERROR_DOM_INDEXEDDB_DATA_ERR
>);
32 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR
);
38 IDBKeyRange::IDBKeyRange(nsISupports
* aGlobal
, bool aLowerOpen
, bool aUpperOpen
,
41 mCachedLowerVal(JS::UndefinedValue()),
42 mCachedUpperVal(JS::UndefinedValue()),
43 mLowerOpen(aLowerOpen
),
44 mUpperOpen(aUpperOpen
),
46 mHaveCachedLowerVal(false),
47 mHaveCachedUpperVal(false),
49 AssertIsOnOwningThread();
52 IDBKeyRange::~IDBKeyRange() { DropJSObjects(); }
54 IDBLocaleAwareKeyRange::IDBLocaleAwareKeyRange(nsISupports
* aGlobal
,
55 bool aLowerOpen
, bool aUpperOpen
,
57 : IDBKeyRange(aGlobal
, aLowerOpen
, aUpperOpen
, aIsOnly
) {
58 AssertIsOnOwningThread();
61 IDBLocaleAwareKeyRange::~IDBLocaleAwareKeyRange() { DropJSObjects(); }
64 void IDBKeyRange::FromJSVal(JSContext
* aCx
, JS::Handle
<JS::Value
> aVal
,
65 RefPtr
<IDBKeyRange
>* aKeyRange
, ErrorResult
& aRv
) {
66 MOZ_ASSERT_IF(!aCx
, aVal
.isUndefined());
67 MOZ_ASSERT(aKeyRange
);
69 RefPtr
<IDBKeyRange
> keyRange
;
71 if (aVal
.isNullOrUndefined()) {
72 // undefined and null returns no IDBKeyRange.
73 *aKeyRange
= std::move(keyRange
);
77 JS::Rooted
<JSObject
*> obj(aCx
, aVal
.isObject() ? &aVal
.toObject() : nullptr);
79 // Unwrap an IDBKeyRange object if possible.
80 if (obj
&& NS_SUCCEEDED(UNWRAP_OBJECT(IDBKeyRange
, obj
, keyRange
))) {
82 *aKeyRange
= std::move(keyRange
);
86 // A valid key returns an 'only' IDBKeyRange.
87 keyRange
= new IDBKeyRange(nullptr, false, false, true);
88 GetKeyFromJSVal(aCx
, aVal
, keyRange
->Lower(), aRv
);
90 *aKeyRange
= std::move(keyRange
);
95 RefPtr
<IDBKeyRange
> IDBKeyRange::FromSerialized(
96 const SerializedKeyRange
& aKeyRange
) {
97 RefPtr
<IDBKeyRange
> keyRange
=
98 new IDBKeyRange(nullptr, aKeyRange
.lowerOpen(), aKeyRange
.upperOpen(),
100 keyRange
->Lower() = aKeyRange
.lower();
101 if (!keyRange
->IsOnly()) {
102 keyRange
->Upper() = aKeyRange
.upper();
107 void IDBKeyRange::ToSerialized(SerializedKeyRange
& aKeyRange
) const {
108 aKeyRange
.lowerOpen() = LowerOpen();
109 aKeyRange
.upperOpen() = UpperOpen();
110 aKeyRange
.isOnly() = IsOnly();
112 aKeyRange
.lower() = Lower();
114 aKeyRange
.upper() = Upper();
118 NS_IMPL_CYCLE_COLLECTION_CLASS(IDBKeyRange
)
120 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBKeyRange
)
121 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal
)
122 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
124 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(IDBKeyRange
)
125 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCachedLowerVal
)
126 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCachedUpperVal
)
127 NS_IMPL_CYCLE_COLLECTION_TRACE_END
129 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IDBKeyRange
)
130 NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal
)
131 tmp
->DropJSObjects();
132 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
134 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBKeyRange
)
135 NS_INTERFACE_MAP_ENTRY(nsISupports
)
138 NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBKeyRange
)
139 NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBKeyRange
)
141 void IDBKeyRange::DropJSObjects() {
145 mHaveCachedLowerVal
= false;
146 mHaveCachedUpperVal
= false;
148 mozilla::DropJSObjects(this);
151 bool IDBKeyRange::WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
,
152 JS::MutableHandle
<JSObject
*> aReflector
) {
153 return IDBKeyRange_Binding::Wrap(aCx
, this, aGivenProto
, aReflector
);
156 bool IDBLocaleAwareKeyRange::WrapObject(
157 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
,
158 JS::MutableHandle
<JSObject
*> aReflector
) {
159 return IDBLocaleAwareKeyRange_Binding::Wrap(aCx
, this, aGivenProto
,
163 void IDBKeyRange::GetLower(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aResult
,
165 AssertIsOnOwningThread();
167 if (!mHaveCachedLowerVal
) {
169 mozilla::HoldJSObjects(this);
173 aRv
= Lower().ToJSVal(aCx
, mCachedLowerVal
);
178 mHaveCachedLowerVal
= true;
181 aResult
.set(mCachedLowerVal
);
184 void IDBKeyRange::GetUpper(JSContext
* aCx
, JS::MutableHandle
<JS::Value
> aResult
,
186 AssertIsOnOwningThread();
188 if (!mHaveCachedUpperVal
) {
190 mozilla::HoldJSObjects(this);
194 aRv
= Upper().ToJSVal(aCx
, mCachedUpperVal
);
199 mHaveCachedUpperVal
= true;
202 aResult
.set(mCachedUpperVal
);
205 bool IDBKeyRange::Includes(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
206 ErrorResult
& aRv
) const {
208 GetKeyFromJSVal(aCx
, aValue
, key
, aRv
);
213 MOZ_ASSERT(!(Lower().IsUnset() && Upper().IsUnset()));
214 MOZ_ASSERT_IF(IsOnly(), !Lower().IsUnset() && !LowerOpen() &&
215 Lower() == Upper() && LowerOpen() == UpperOpen());
217 if (!Lower().IsUnset()) {
218 switch (Key::CompareKeys(Lower(), key
)) {
234 if (!Upper().IsUnset()) {
235 switch (Key::CompareKeys(key
, Upper())) {
250 RefPtr
<IDBKeyRange
> IDBKeyRange::Only(const GlobalObject
& aGlobal
,
251 JS::Handle
<JS::Value
> aValue
,
253 RefPtr
<IDBKeyRange
> keyRange
=
254 new IDBKeyRange(aGlobal
.GetAsSupports(), false, false, true);
256 GetKeyFromJSVal(aGlobal
.Context(), aValue
, keyRange
->Lower(), aRv
);
265 RefPtr
<IDBKeyRange
> IDBKeyRange::LowerBound(const GlobalObject
& aGlobal
,
266 JS::Handle
<JS::Value
> aValue
,
267 bool aOpen
, ErrorResult
& aRv
) {
268 RefPtr
<IDBKeyRange
> keyRange
=
269 new IDBKeyRange(aGlobal
.GetAsSupports(), aOpen
, true, false);
271 GetKeyFromJSVal(aGlobal
.Context(), aValue
, keyRange
->Lower(), aRv
);
280 RefPtr
<IDBKeyRange
> IDBKeyRange::UpperBound(const GlobalObject
& aGlobal
,
281 JS::Handle
<JS::Value
> aValue
,
282 bool aOpen
, ErrorResult
& aRv
) {
283 RefPtr
<IDBKeyRange
> keyRange
=
284 new IDBKeyRange(aGlobal
.GetAsSupports(), true, aOpen
, false);
286 GetKeyFromJSVal(aGlobal
.Context(), aValue
, keyRange
->Upper(), aRv
);
295 RefPtr
<IDBKeyRange
> IDBKeyRange::Bound(const GlobalObject
& aGlobal
,
296 JS::Handle
<JS::Value
> aLower
,
297 JS::Handle
<JS::Value
> aUpper
,
298 bool aLowerOpen
, bool aUpperOpen
,
300 RefPtr
<IDBKeyRange
> keyRange
=
301 new IDBKeyRange(aGlobal
.GetAsSupports(), aLowerOpen
, aUpperOpen
, false);
303 GetKeyFromJSVal(aGlobal
.Context(), aLower
, keyRange
->Lower(), aRv
);
308 GetKeyFromJSVal(aGlobal
.Context(), aUpper
, keyRange
->Upper(), aRv
);
313 if (keyRange
->Lower() > keyRange
->Upper() ||
314 (keyRange
->Lower() == keyRange
->Upper() && (aLowerOpen
|| aUpperOpen
))) {
315 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR
);
323 RefPtr
<IDBLocaleAwareKeyRange
> IDBLocaleAwareKeyRange::Bound(
324 const GlobalObject
& aGlobal
, JS::Handle
<JS::Value
> aLower
,
325 JS::Handle
<JS::Value
> aUpper
, bool aLowerOpen
, bool aUpperOpen
,
327 RefPtr
<IDBLocaleAwareKeyRange
> keyRange
= new IDBLocaleAwareKeyRange(
328 aGlobal
.GetAsSupports(), aLowerOpen
, aUpperOpen
, false);
330 GetKeyFromJSVal(aGlobal
.Context(), aLower
, keyRange
->Lower(), aRv
);
335 GetKeyFromJSVal(aGlobal
.Context(), aUpper
, keyRange
->Upper(), aRv
);
340 if (keyRange
->Lower() == keyRange
->Upper() && (aLowerOpen
|| aUpperOpen
)) {
341 aRv
.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR
);
348 } // namespace mozilla::dom