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
#ifndef mozilla_dom_indexeddb_key_h__
8
#define mozilla_dom_indexeddb_key_h__
9
10
#include "mozilla/dom/indexedDB/IDBResult.h"
11
12
#include "js/Array.h" // JS::GetArrayLength
13
#include "js/RootingAPI.h"
14
#include "jsapi.h"
15
#include "mozilla/ErrorResult.h"
16
#include "nsString.h"
17
18
class mozIStorageStatement;
19
class mozIStorageValueArray;
20
21
namespace IPC {
22
23
template <typename>
24
struct ParamTraits;
25
26
} // namespace IPC
27
28
namespace mozilla {
29
namespace dom {
30
namespace indexedDB {
31
32
class Key {
33
friend struct IPC::ParamTraits<Key>;
34
35
nsCString mBuffer;
36
37
public:
38
enum {
39
eTerminator = 0,
40
eFloat = 0x10,
41
eDate = 0x20,
42
eString = 0x30,
43
eBinary = 0x40,
44
eArray = 0x50,
45
eMaxType = eArray
46
};
47
48
static const uint8_t kMaxArrayCollapse = uint8_t(3);
49
static const uint8_t kMaxRecursionDepth = uint8_t(64);
50
51
Key() { Unset(); }
52
53
explicit Key(const nsACString& aBuffer) : mBuffer(aBuffer) {}
54
55
Key& operator=(int64_t aInt) {
56
SetFromInteger(aInt);
57
return *this;
58
}
59
60
bool operator==(const Key& aOther) const {
61
MOZ_ASSERT(!mBuffer.IsVoid());
62
MOZ_ASSERT(!aOther.mBuffer.IsVoid());
63
64
return mBuffer.Equals(aOther.mBuffer);
65
}
66
67
bool operator!=(const Key& aOther) const {
68
MOZ_ASSERT(!mBuffer.IsVoid());
69
MOZ_ASSERT(!aOther.mBuffer.IsVoid());
70
71
return !mBuffer.Equals(aOther.mBuffer);
72
}
73
74
bool operator<(const Key& aOther) const {
75
MOZ_ASSERT(!mBuffer.IsVoid());
76
MOZ_ASSERT(!aOther.mBuffer.IsVoid());
77
78
return Compare(mBuffer, aOther.mBuffer) < 0;
79
}
80
81
bool operator>(const Key& aOther) const {
82
MOZ_ASSERT(!mBuffer.IsVoid());
83
MOZ_ASSERT(!aOther.mBuffer.IsVoid());
84
85
return Compare(mBuffer, aOther.mBuffer) > 0;
86
}
87
88
bool operator<=(const Key& aOther) const {
89
MOZ_ASSERT(!mBuffer.IsVoid());
90
MOZ_ASSERT(!aOther.mBuffer.IsVoid());
91
92
return Compare(mBuffer, aOther.mBuffer) <= 0;
93
}
94
95
bool operator>=(const Key& aOther) const {
96
MOZ_ASSERT(!mBuffer.IsVoid());
97
MOZ_ASSERT(!aOther.mBuffer.IsVoid());
98
99
return Compare(mBuffer, aOther.mBuffer) >= 0;
100
}
101
102
void Unset() { mBuffer.SetIsVoid(true); }
103
104
bool IsUnset() const { return mBuffer.IsVoid(); }
105
106
bool IsFloat() const { return !IsUnset() && *BufferStart() == eFloat; }
107
108
bool IsDate() const { return !IsUnset() && *BufferStart() == eDate; }
109
110
bool IsString() const { return !IsUnset() && *BufferStart() == eString; }
111
112
bool IsBinary() const { return !IsUnset() && *BufferStart() == eBinary; }
113
114
bool IsArray() const { return !IsUnset() && *BufferStart() >= eArray; }
115
116
double ToFloat() const {
117
MOZ_ASSERT(IsFloat());
118
const EncodedDataType* pos = BufferStart();
119
double res = DecodeNumber(pos, BufferEnd());
120
MOZ_ASSERT(pos >= BufferEnd());
121
return res;
122
}
123
124
double ToDateMsec() const {
125
MOZ_ASSERT(IsDate());
126
const EncodedDataType* pos = BufferStart();
127
double res = DecodeNumber(pos, BufferEnd());
128
MOZ_ASSERT(pos >= BufferEnd());
129
return res;
130
}
131
132
void ToString(nsString& aString) const {
133
MOZ_ASSERT(IsString());
134
const EncodedDataType* pos = BufferStart();
135
DecodeString(pos, BufferEnd(), aString);
136
MOZ_ASSERT(pos >= BufferEnd());
137
}
138
139
IDBResult<void, IDBSpecialValue::Invalid> SetFromString(
140
const nsAString& aString, ErrorResult& aRv);
141
142
void SetFromInteger(int64_t aInt) {
143
mBuffer.Truncate();
144
EncodeNumber(double(aInt), eFloat);
145
TrimBuffer();
146
}
147
148
// This function implements the standard algorithm "convert a value to a key".
149
// A key return value is indicated by returning `true` whereas `false` means
150
// either invalid (if `aRv.Failed()` is `false`) or an exception (otherwise).
151
IDBResult<void, IDBSpecialValue::Invalid> SetFromJSVal(
152
JSContext* aCx, JS::Handle<JS::Value> aVal, ErrorResult& aRv);
153
154
nsresult ToJSVal(JSContext* aCx, JS::MutableHandle<JS::Value> aVal) const;
155
156
nsresult ToJSVal(JSContext* aCx, JS::Heap<JS::Value>& aVal) const;
157
158
// See SetFromJSVal() for the meaning of values returned by this function.
159
IDBResult<void, IDBSpecialValue::Invalid> AppendItem(
160
JSContext* aCx, bool aFirstOfArray, JS::Handle<JS::Value> aVal,
161
ErrorResult& aRv);
162
163
IDBResult<void, IDBSpecialValue::Invalid> ToLocaleAwareKey(
164
Key& aTarget, const nsCString& aLocale, ErrorResult& aRv) const;
165
166
void FinishArray() { TrimBuffer(); }
167
168
const nsCString& GetBuffer() const { return mBuffer; }
169
170
nsresult BindToStatement(mozIStorageStatement* aStatement,
171
const nsACString& aParamName) const;
172
173
nsresult SetFromStatement(mozIStorageStatement* aStatement, uint32_t aIndex);
174
175
nsresult SetFromValueArray(mozIStorageValueArray* aValues, uint32_t aIndex);
176
177
static int16_t CompareKeys(const Key& aFirst, const Key& aSecond) {
178
int32_t result = Compare(aFirst.mBuffer, aSecond.mBuffer);
179
180
if (result < 0) {
181
return -1;
182
}
183
184
if (result > 0) {
185
return 1;
186
}
187
188
return 0;
189
}
190
191
// Implementation of the array branch of step 3 of
193
template <typename ArrayConversionPolicy>
194
static IDBResult<void, IDBSpecialValue::Invalid> ConvertArrayValueToKey(
195
JSContext* const aCx, JS::HandleObject aObject,
196
ArrayConversionPolicy&& aPolicy, ErrorResult& aRv) {
197
// 1. Let `len` be ? ToLength( ? Get(`input`, "length")).
198
uint32_t len;
199
if (!JS::GetArrayLength(aCx, aObject, &len)) {
200
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
201
return Exception;
202
}
203
204
// 2. Add `input` to `seen`.
205
aPolicy.AddToSeenSet(aCx, aObject);
206
207
// 3. Let `keys` be a new empty list.
208
aPolicy.BeginSubkeyList();
209
210
// 4. Let `index` be 0.
211
uint32_t index = 0;
212
213
// 5. While `index` is less than `len`:
214
while (index < len) {
215
JS::RootedId indexId(aCx);
216
if (!JS_IndexToId(aCx, index, &indexId)) {
217
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
218
return Exception;
219
}
220
221
// 1. Let `hop` be ? HasOwnProperty(`input`, `index`).
222
bool hop;
223
if (!JS_HasOwnPropertyById(aCx, aObject, indexId, &hop)) {
224
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
225
return Exception;
226
}
227
228
// 2. If `hop` is false, return invalid.
229
if (!hop) {
230
return Invalid;
231
}
232
233
// 3. Let `entry` be ? Get(`input`, `index`).
234
JS::RootedValue entry(aCx);
235
if (!JS_GetPropertyById(aCx, aObject, indexId, &entry)) {
236
aRv.Throw(NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
237
return Exception;
238
}
239
240
// 4. Let `key` be the result of running the steps to convert a value to a
241
// key with arguments `entry` and `seen`.
242
// 5. ReturnIfAbrupt(`key`).
243
// 6. If `key` is invalid abort these steps and return invalid.
244
// 7. Append `key` to `keys`.
245
auto result = aPolicy.ConvertSubkey(aCx, entry, index, aRv);
246
if (!result.Is(Ok, aRv)) {
247
return result;
248
}
249
250
// 8. Increase `index` by 1.
251
index += 1;
252
}
253
254
// 6. Return a new array key with value `keys`.
255
aPolicy.EndSubkeyList();
256
return Ok();
257
}
258
259
private:
260
class MOZ_STACK_CLASS ArrayValueEncoder;
261
262
using EncodedDataType = unsigned char;
263
264
const EncodedDataType* BufferStart() const {
265
// TODO it would be nicer if mBuffer was also using EncodedDataType
266
return reinterpret_cast<const EncodedDataType*>(mBuffer.BeginReading());
267
}
268
269
const EncodedDataType* BufferEnd() const {
270
return reinterpret_cast<const EncodedDataType*>(mBuffer.EndReading());
271
}
272
273
// Encoding helper. Trims trailing zeros off of mBuffer as a post-processing
274
// step.
275
void TrimBuffer() {
276
const char* end = mBuffer.EndReading() - 1;
277
while (!*end) {
278
--end;
279
}
280
281
mBuffer.Truncate(end + 1 - mBuffer.BeginReading());
282
}
283
284
// Encoding functions. These append the encoded value to the end of mBuffer
285
IDBResult<void, IDBSpecialValue::Invalid> EncodeJSVal(
286
JSContext* aCx, JS::Handle<JS::Value> aVal, uint8_t aTypeOffset,
287
ErrorResult& aRv);
288
289
IDBResult<void, IDBSpecialValue::Invalid> EncodeString(
290
const nsAString& aString, uint8_t aTypeOffset, ErrorResult& aRv);
291
292
template <typename T>
293
IDBResult<void, IDBSpecialValue::Invalid> EncodeString(const T* aStart,
294
const T* aEnd,
295
uint8_t aTypeOffset,
296
ErrorResult& aRv);
297
298
template <typename T>
299
IDBResult<void, IDBSpecialValue::Invalid> EncodeAsString(const T* aStart,
300
const T* aEnd,
301
uint8_t aType,
302
ErrorResult& aRv);
303
304
IDBResult<void, IDBSpecialValue::Invalid> EncodeLocaleString(
305
const nsDependentString& aString, uint8_t aTypeOffset,
306
const nsCString& aLocale, ErrorResult& aRv);
307
308
void EncodeNumber(double aFloat, uint8_t aType);
309
310
IDBResult<void, IDBSpecialValue::Invalid> EncodeBinary(JSObject* aObject,
311
bool aIsViewObject,
312
uint8_t aTypeOffset,
313
ErrorResult& aRv);
314
315
// Decoding functions. aPos points into mBuffer and is adjusted to point
316
// past the consumed value. (Note: this may be beyond aEnd).
317
static nsresult DecodeJSVal(const EncodedDataType*& aPos,
318
const EncodedDataType* aEnd, JSContext* aCx,
319
JS::MutableHandle<JS::Value> aVal);
320
321
static void DecodeString(const EncodedDataType*& aPos,
322
const EncodedDataType* aEnd, nsString& aString);
323
324
static double DecodeNumber(const EncodedDataType*& aPos,
325
const EncodedDataType* aEnd);
326
327
static JSObject* DecodeBinary(const EncodedDataType*& aPos,
328
const EncodedDataType* aEnd, JSContext* aCx);
329
330
// Returns the size of the decoded data for stringy (string or binary),
331
// excluding a null terminator.
332
// On return, aOutSectionEnd points to the last byte behind the current
333
// encoded section, i.e. either aEnd, or the eTerminator.
334
// T is the base type for the decoded data.
335
template <typename T>
336
static uint32_t CalcDecodedStringySize(
337
const EncodedDataType* aBegin, const EncodedDataType* aEnd,
338
const EncodedDataType** aOutEncodedSectionEnd);
339
340
static uint32_t LengthOfEncodedBinary(const EncodedDataType* aPos,
341
const EncodedDataType* aEnd);
342
343
template <typename T>
344
static void DecodeAsStringy(const EncodedDataType* aEncodedSectionBegin,
345
const EncodedDataType* aEncodedSectionEnd,
346
uint32_t aDecodedLength, T* aOut);
347
348
template <EncodedDataType TypeMask, typename T, typename AcquireBuffer,
349
typename AcquireEmpty>
350
static void DecodeStringy(const EncodedDataType*& aPos,
351
const EncodedDataType* aEnd,
352
const AcquireBuffer& acquireBuffer,
353
const AcquireEmpty& acquireEmpty);
354
355
IDBResult<void, IDBSpecialValue::Invalid> EncodeJSValInternal(
356
JSContext* aCx, JS::Handle<JS::Value> aVal, uint8_t aTypeOffset,
357
uint16_t aRecursionDepth, ErrorResult& aRv);
358
359
static nsresult DecodeJSValInternal(const EncodedDataType*& aPos,
360
const EncodedDataType* aEnd,
361
JSContext* aCx, uint8_t aTypeOffset,
362
JS::MutableHandle<JS::Value> aVal,
363
uint16_t aRecursionDepth);
364
365
template <typename T>
366
nsresult SetFromSource(T* aSource, uint32_t aIndex);
367
};
368
369
} // namespace indexedDB
370
} // namespace dom
371
} // namespace mozilla
372
373
#endif // mozilla_dom_indexeddb_key_h__