Source code

Revision control

Other Tools

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
#include "IDBKeyRange.h"
8
9
#include "Key.h"
10
#include "mozilla/ErrorResult.h"
11
#include "mozilla/dom/BindingUtils.h"
12
#include "mozilla/dom/IDBKeyRangeBinding.h"
13
#include "mozilla/dom/indexedDB/PBackgroundIDBSharedTypes.h"
14
15
namespace mozilla {
16
namespace dom {
17
18
using namespace mozilla::dom::indexedDB;
19
20
namespace {
21
22
void GetKeyFromJSVal(JSContext* aCx, JS::Handle<JS::Value> aVal, Key& aKey,
23
ErrorResult& aRv) {
24
auto result = aKey.SetFromJSVal(aCx, aVal, aRv);
25
if (!result.Is(Ok, aRv)) {
26
if (result.Is(Invalid, aRv)) {
27
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
28
}
29
return;
30
}
31
32
if (aKey.IsUnset()) {
33
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
34
}
35
}
36
37
} // namespace
38
39
IDBKeyRange::IDBKeyRange(nsISupports* aGlobal, bool aLowerOpen, bool aUpperOpen,
40
bool aIsOnly)
41
: mGlobal(aGlobal),
42
mCachedLowerVal(JS::UndefinedValue()),
43
mCachedUpperVal(JS::UndefinedValue()),
44
mLowerOpen(aLowerOpen),
45
mUpperOpen(aUpperOpen),
46
mIsOnly(aIsOnly),
47
mHaveCachedLowerVal(false),
48
mHaveCachedUpperVal(false),
49
mRooted(false) {
50
AssertIsOnOwningThread();
51
}
52
53
IDBKeyRange::~IDBKeyRange() { DropJSObjects(); }
54
55
IDBLocaleAwareKeyRange::IDBLocaleAwareKeyRange(nsISupports* aGlobal,
56
bool aLowerOpen, bool aUpperOpen,
57
bool aIsOnly)
58
: IDBKeyRange(aGlobal, aLowerOpen, aUpperOpen, aIsOnly) {
59
AssertIsOnOwningThread();
60
}
61
62
IDBLocaleAwareKeyRange::~IDBLocaleAwareKeyRange() { DropJSObjects(); }
63
64
// static
65
void IDBKeyRange::FromJSVal(JSContext* aCx, JS::Handle<JS::Value> aVal,
66
IDBKeyRange** aKeyRange, ErrorResult& aRv) {
67
MOZ_ASSERT_IF(!aCx, aVal.isUndefined());
68
69
RefPtr<IDBKeyRange> keyRange;
70
71
if (aVal.isNullOrUndefined()) {
72
// undefined and null returns no IDBKeyRange.
73
keyRange.forget(aKeyRange);
74
return;
75
}
76
77
JS::Rooted<JSObject*> obj(aCx, aVal.isObject() ? &aVal.toObject() : nullptr);
78
79
// Unwrap an IDBKeyRange object if possible.
80
if (obj && NS_SUCCEEDED(UNWRAP_OBJECT(IDBKeyRange, obj, keyRange))) {
81
MOZ_ASSERT(keyRange);
82
keyRange.forget(aKeyRange);
83
return;
84
}
85
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
keyRange.forget(aKeyRange);
91
}
92
}
93
94
// static
95
already_AddRefed<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();
103
}
104
return keyRange.forget();
105
}
106
107
void IDBKeyRange::ToSerialized(SerializedKeyRange& aKeyRange) const {
108
aKeyRange.lowerOpen() = LowerOpen();
109
aKeyRange.upperOpen() = UpperOpen();
110
aKeyRange.isOnly() = IsOnly();
111
112
aKeyRange.lower() = Lower();
113
if (!IsOnly()) {
114
aKeyRange.upper() = Upper();
115
}
116
}
117
118
NS_IMPL_CYCLE_COLLECTION_CLASS(IDBKeyRange)
119
120
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IDBKeyRange)
121
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
122
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
123
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
128
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
133
134
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBKeyRange)
135
NS_INTERFACE_MAP_ENTRY(nsISupports)
136
NS_INTERFACE_MAP_END
137
138
NS_IMPL_CYCLE_COLLECTING_ADDREF(IDBKeyRange)
139
NS_IMPL_CYCLE_COLLECTING_RELEASE(IDBKeyRange)
140
141
void IDBKeyRange::DropJSObjects() {
142
if (!mRooted) {
143
return;
144
}
145
mCachedLowerVal.setUndefined();
146
mCachedUpperVal.setUndefined();
147
mHaveCachedLowerVal = false;
148
mHaveCachedUpperVal = false;
149
mRooted = false;
150
mozilla::DropJSObjects(this);
151
}
152
153
bool IDBKeyRange::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
154
JS::MutableHandle<JSObject*> aReflector) {
155
return IDBKeyRange_Binding::Wrap(aCx, this, aGivenProto, aReflector);
156
}
157
158
bool IDBLocaleAwareKeyRange::WrapObject(
159
JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
160
JS::MutableHandle<JSObject*> aReflector) {
161
return IDBLocaleAwareKeyRange_Binding::Wrap(aCx, this, aGivenProto,
162
aReflector);
163
}
164
165
void IDBKeyRange::GetLower(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
166
ErrorResult& aRv) {
167
AssertIsOnOwningThread();
168
169
if (!mHaveCachedLowerVal) {
170
if (!mRooted) {
171
mozilla::HoldJSObjects(this);
172
mRooted = true;
173
}
174
175
aRv = Lower().ToJSVal(aCx, mCachedLowerVal);
176
if (aRv.Failed()) {
177
return;
178
}
179
180
mHaveCachedLowerVal = true;
181
}
182
183
aResult.set(mCachedLowerVal);
184
}
185
186
void IDBKeyRange::GetUpper(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
187
ErrorResult& aRv) {
188
AssertIsOnOwningThread();
189
190
if (!mHaveCachedUpperVal) {
191
if (!mRooted) {
192
mozilla::HoldJSObjects(this);
193
mRooted = true;
194
}
195
196
aRv = Upper().ToJSVal(aCx, mCachedUpperVal);
197
if (aRv.Failed()) {
198
return;
199
}
200
201
mHaveCachedUpperVal = true;
202
}
203
204
aResult.set(mCachedUpperVal);
205
}
206
207
bool IDBKeyRange::Includes(JSContext* aCx, JS::Handle<JS::Value> aValue,
208
ErrorResult& aRv) const {
209
Key key;
210
GetKeyFromJSVal(aCx, aValue, key, aRv);
211
if (aRv.Failed()) {
212
return false;
213
}
214
215
MOZ_ASSERT(!(Lower().IsUnset() && Upper().IsUnset()));
216
MOZ_ASSERT_IF(IsOnly(), !Lower().IsUnset() && !LowerOpen() &&
217
Lower() == Upper() && LowerOpen() == UpperOpen());
218
219
if (!Lower().IsUnset()) {
220
switch (Key::CompareKeys(Lower(), key)) {
221
case 1:
222
return false;
223
case 0:
224
// Identical keys.
225
return !LowerOpen();
226
case -1:
227
if (IsOnly()) {
228
return false;
229
}
230
break;
231
default:
232
MOZ_CRASH();
233
}
234
}
235
236
if (!Upper().IsUnset()) {
237
switch (Key::CompareKeys(key, Upper())) {
238
case 1:
239
return false;
240
case 0:
241
// Identical keys.
242
return !UpperOpen();
243
case -1:
244
break;
245
}
246
}
247
248
return true;
249
}
250
251
// static
252
already_AddRefed<IDBKeyRange> IDBKeyRange::Only(const GlobalObject& aGlobal,
253
JS::Handle<JS::Value> aValue,
254
ErrorResult& aRv) {
255
RefPtr<IDBKeyRange> keyRange =
256
new IDBKeyRange(aGlobal.GetAsSupports(), false, false, true);
257
258
GetKeyFromJSVal(aGlobal.Context(), aValue, keyRange->Lower(), aRv);
259
if (aRv.Failed()) {
260
return nullptr;
261
}
262
263
return keyRange.forget();
264
}
265
266
// static
267
already_AddRefed<IDBKeyRange> IDBKeyRange::LowerBound(
268
const GlobalObject& aGlobal, JS::Handle<JS::Value> aValue, bool aOpen,
269
ErrorResult& aRv) {
270
RefPtr<IDBKeyRange> keyRange =
271
new IDBKeyRange(aGlobal.GetAsSupports(), aOpen, true, false);
272
273
GetKeyFromJSVal(aGlobal.Context(), aValue, keyRange->Lower(), aRv);
274
if (aRv.Failed()) {
275
return nullptr;
276
}
277
278
return keyRange.forget();
279
}
280
281
// static
282
already_AddRefed<IDBKeyRange> IDBKeyRange::UpperBound(
283
const GlobalObject& aGlobal, JS::Handle<JS::Value> aValue, bool aOpen,
284
ErrorResult& aRv) {
285
RefPtr<IDBKeyRange> keyRange =
286
new IDBKeyRange(aGlobal.GetAsSupports(), true, aOpen, false);
287
288
GetKeyFromJSVal(aGlobal.Context(), aValue, keyRange->Upper(), aRv);
289
if (aRv.Failed()) {
290
return nullptr;
291
}
292
293
return keyRange.forget();
294
}
295
296
// static
297
already_AddRefed<IDBKeyRange> IDBKeyRange::Bound(const GlobalObject& aGlobal,
298
JS::Handle<JS::Value> aLower,
299
JS::Handle<JS::Value> aUpper,
300
bool aLowerOpen,
301
bool aUpperOpen,
302
ErrorResult& aRv) {
303
RefPtr<IDBKeyRange> keyRange =
304
new IDBKeyRange(aGlobal.GetAsSupports(), aLowerOpen, aUpperOpen, false);
305
306
GetKeyFromJSVal(aGlobal.Context(), aLower, keyRange->Lower(), aRv);
307
if (aRv.Failed()) {
308
return nullptr;
309
}
310
311
GetKeyFromJSVal(aGlobal.Context(), aUpper, keyRange->Upper(), aRv);
312
if (aRv.Failed()) {
313
return nullptr;
314
}
315
316
if (keyRange->Lower() > keyRange->Upper() ||
317
(keyRange->Lower() == keyRange->Upper() && (aLowerOpen || aUpperOpen))) {
318
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
319
return nullptr;
320
}
321
322
return keyRange.forget();
323
}
324
325
// static
326
already_AddRefed<IDBLocaleAwareKeyRange> IDBLocaleAwareKeyRange::Bound(
327
const GlobalObject& aGlobal, JS::Handle<JS::Value> aLower,
328
JS::Handle<JS::Value> aUpper, bool aLowerOpen, bool aUpperOpen,
329
ErrorResult& aRv) {
330
RefPtr<IDBLocaleAwareKeyRange> keyRange = new IDBLocaleAwareKeyRange(
331
aGlobal.GetAsSupports(), aLowerOpen, aUpperOpen, false);
332
333
GetKeyFromJSVal(aGlobal.Context(), aLower, keyRange->Lower(), aRv);
334
if (aRv.Failed()) {
335
return nullptr;
336
}
337
338
GetKeyFromJSVal(aGlobal.Context(), aUpper, keyRange->Upper(), aRv);
339
if (aRv.Failed()) {
340
return nullptr;
341
}
342
343
if (keyRange->Lower() == keyRange->Upper() && (aLowerOpen || aUpperOpen)) {
344
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_DATA_ERR);
345
return nullptr;
346
}
347
348
return keyRange.forget();
349
}
350
351
} // namespace dom
352
} // namespace mozilla