Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / dom / indexedDB / IDBKeyRange.cpp
blobaf31cf64905f00c05e7977563100dd0974f2c76f
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"
9 #include "Key.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;
20 namespace {
22 void GetKeyFromJSVal(JSContext* aCx, JS::Handle<JS::Value> aVal, Key& aKey,
23 ErrorResult& aRv) {
24 auto result = aKey.SetFromJSVal(aCx, aVal);
25 if (result.isErr()) {
26 aRv = result.unwrapErr().ExtractErrorResult(
27 InvalidMapsTo<NS_ERROR_DOM_INDEXEDDB_DATA_ERR>);
28 return;
31 if (aKey.IsUnset()) {
32 aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
36 } // namespace
38 IDBKeyRange::IDBKeyRange(nsISupports* aGlobal, bool aLowerOpen, bool aUpperOpen,
39 bool aIsOnly)
40 : mGlobal(aGlobal),
41 mCachedLowerVal(JS::UndefinedValue()),
42 mCachedUpperVal(JS::UndefinedValue()),
43 mLowerOpen(aLowerOpen),
44 mUpperOpen(aUpperOpen),
45 mIsOnly(aIsOnly),
46 mHaveCachedLowerVal(false),
47 mHaveCachedUpperVal(false),
48 mRooted(false) {
49 AssertIsOnOwningThread();
52 IDBKeyRange::~IDBKeyRange() { DropJSObjects(); }
54 IDBLocaleAwareKeyRange::IDBLocaleAwareKeyRange(nsISupports* aGlobal,
55 bool aLowerOpen, bool aUpperOpen,
56 bool aIsOnly)
57 : IDBKeyRange(aGlobal, aLowerOpen, aUpperOpen, aIsOnly) {
58 AssertIsOnOwningThread();
61 IDBLocaleAwareKeyRange::~IDBLocaleAwareKeyRange() { DropJSObjects(); }
63 // static
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);
74 return;
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))) {
81 MOZ_ASSERT(keyRange);
82 *aKeyRange = std::move(keyRange);
83 return;
86 // A valid key returns an 'only' IDBKeyRange.
87 keyRange = new IDBKeyRange(nullptr, false, false, true);
88 GetKeyFromJSVal(aCx, aVal, keyRange->Lower(), aRv);
89 if (!aRv.Failed()) {
90 *aKeyRange = std::move(keyRange);
94 // static
95 RefPtr<IDBKeyRange> IDBKeyRange::FromSerialized(
96 const SerializedKeyRange& aKeyRange) {
97 RefPtr<IDBKeyRange> keyRange =
98 new IDBKeyRange(nullptr, aKeyRange.lowerOpen(), aKeyRange.upperOpen(),
99 aKeyRange.isOnly());
100 keyRange->Lower() = aKeyRange.lower();
101 if (!keyRange->IsOnly()) {
102 keyRange->Upper() = aKeyRange.upper();
104 return keyRange;
107 void IDBKeyRange::ToSerialized(SerializedKeyRange& aKeyRange) const {
108 aKeyRange.lowerOpen() = LowerOpen();
109 aKeyRange.upperOpen() = UpperOpen();
110 aKeyRange.isOnly() = IsOnly();
112 aKeyRange.lower() = Lower();
113 if (!IsOnly()) {
114 aKeyRange.upper() = Upper();
118 NS_IMPL_CYCLE_COLLECTION_MULTI_ZONE_JSHOLDER_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)
136 NS_INTERFACE_MAP_END
138 NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBKeyRange)
139 NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBKeyRange)
141 void IDBKeyRange::DropJSObjects() {
142 if (!mRooted) {
143 return;
145 mHaveCachedLowerVal = false;
146 mHaveCachedUpperVal = false;
147 mRooted = 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,
160 aReflector);
163 void IDBKeyRange::GetLower(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
164 ErrorResult& aRv) {
165 AssertIsOnOwningThread();
167 if (!mHaveCachedLowerVal) {
168 if (!mRooted) {
169 mozilla::HoldJSObjects(this);
170 mRooted = true;
173 aRv = Lower().ToJSVal(aCx, mCachedLowerVal);
174 if (aRv.Failed()) {
175 return;
178 mHaveCachedLowerVal = true;
181 aResult.set(mCachedLowerVal);
184 void IDBKeyRange::GetUpper(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
185 ErrorResult& aRv) {
186 AssertIsOnOwningThread();
188 if (!mHaveCachedUpperVal) {
189 if (!mRooted) {
190 mozilla::HoldJSObjects(this);
191 mRooted = true;
194 aRv = Upper().ToJSVal(aCx, mCachedUpperVal);
195 if (aRv.Failed()) {
196 return;
199 mHaveCachedUpperVal = true;
202 aResult.set(mCachedUpperVal);
205 bool IDBKeyRange::Includes(JSContext* aCx, JS::Handle<JS::Value> aValue,
206 ErrorResult& aRv) const {
207 Key key;
208 GetKeyFromJSVal(aCx, aValue, key, aRv);
209 if (aRv.Failed()) {
210 return false;
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)) {
219 case 1:
220 return false;
221 case 0:
222 // Identical keys.
223 return !LowerOpen();
224 case -1:
225 if (IsOnly()) {
226 return false;
228 break;
229 default:
230 MOZ_CRASH();
234 if (!Upper().IsUnset()) {
235 switch (Key::CompareKeys(key, Upper())) {
236 case 1:
237 return false;
238 case 0:
239 // Identical keys.
240 return !UpperOpen();
241 case -1:
242 break;
246 return true;
249 // static
250 RefPtr<IDBKeyRange> IDBKeyRange::Only(const GlobalObject& aGlobal,
251 JS::Handle<JS::Value> aValue,
252 ErrorResult& aRv) {
253 RefPtr<IDBKeyRange> keyRange =
254 new IDBKeyRange(aGlobal.GetAsSupports(), false, false, true);
256 GetKeyFromJSVal(aGlobal.Context(), aValue, keyRange->Lower(), aRv);
257 if (aRv.Failed()) {
258 return nullptr;
261 return keyRange;
264 // static
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);
272 if (aRv.Failed()) {
273 return nullptr;
276 return keyRange;
279 // static
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);
287 if (aRv.Failed()) {
288 return nullptr;
291 return keyRange;
294 // static
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,
299 ErrorResult& aRv) {
300 RefPtr<IDBKeyRange> keyRange =
301 new IDBKeyRange(aGlobal.GetAsSupports(), aLowerOpen, aUpperOpen, false);
303 GetKeyFromJSVal(aGlobal.Context(), aLower, keyRange->Lower(), aRv);
304 if (aRv.Failed()) {
305 return nullptr;
308 GetKeyFromJSVal(aGlobal.Context(), aUpper, keyRange->Upper(), aRv);
309 if (aRv.Failed()) {
310 return nullptr;
313 if (keyRange->Lower() > keyRange->Upper() ||
314 (keyRange->Lower() == keyRange->Upper() && (aLowerOpen || aUpperOpen))) {
315 aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
316 return nullptr;
319 return keyRange;
322 // static
323 RefPtr<IDBLocaleAwareKeyRange> IDBLocaleAwareKeyRange::Bound(
324 const GlobalObject& aGlobal, JS::Handle<JS::Value> aLower,
325 JS::Handle<JS::Value> aUpper, bool aLowerOpen, bool aUpperOpen,
326 ErrorResult& aRv) {
327 RefPtr<IDBLocaleAwareKeyRange> keyRange = new IDBLocaleAwareKeyRange(
328 aGlobal.GetAsSupports(), aLowerOpen, aUpperOpen, false);
330 GetKeyFromJSVal(aGlobal.Context(), aLower, keyRange->Lower(), aRv);
331 if (aRv.Failed()) {
332 return nullptr;
335 GetKeyFromJSVal(aGlobal.Context(), aUpper, keyRange->Upper(), aRv);
336 if (aRv.Failed()) {
337 return nullptr;
340 if (keyRange->Lower() == keyRange->Upper() && (aLowerOpen || aUpperOpen)) {
341 aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
342 return nullptr;
345 return keyRange;
348 } // namespace mozilla::dom