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_idbobjectstore_h__
8
#define mozilla_dom_idbobjectstore_h__
9
10
#include "js/RootingAPI.h"
11
#include "mozilla/dom/IDBCursorBinding.h"
12
#include "mozilla/dom/IDBIndexBinding.h"
13
#include "nsAutoPtr.h"
14
#include "nsCycleCollectionParticipant.h"
15
#include "nsISupports.h"
16
#include "nsString.h"
17
#include "nsTArray.h"
18
#include "nsWrapperCache.h"
19
20
struct JSClass;
21
class nsIGlobalObject;
22
23
namespace mozilla {
24
25
class ErrorResult;
26
27
namespace dom {
28
29
class DOMStringList;
30
class IDBCursor;
31
class IDBRequest;
32
class IDBTransaction;
33
class StringOrStringSequence;
34
template <typename>
35
class Sequence;
36
37
namespace indexedDB {
38
class Key;
39
class KeyPath;
40
class IndexUpdateInfo;
41
class ObjectStoreSpec;
42
struct StructuredCloneReadInfo;
43
} // namespace indexedDB
44
45
class IDBObjectStore final : public nsISupports, public nsWrapperCache {
46
typedef indexedDB::IndexUpdateInfo IndexUpdateInfo;
47
typedef indexedDB::Key Key;
48
typedef indexedDB::KeyPath KeyPath;
49
typedef indexedDB::ObjectStoreSpec ObjectStoreSpec;
50
typedef indexedDB::StructuredCloneReadInfo StructuredCloneReadInfo;
51
52
// For AddOrPut() and DeleteInternal().
53
friend class IDBCursor;
54
55
static const JSClass sDummyPropJSClass;
56
57
// TODO: This could be made const if Bug 1575173 is resolved. It is
58
// initialized in the constructor and never modified/cleared.
59
RefPtr<IDBTransaction> mTransaction;
60
JS::Heap<JS::Value> mCachedKeyPath;
61
62
// This normally points to the ObjectStoreSpec owned by the parent IDBDatabase
63
// object. However, if this objectStore is part of a versionchange transaction
64
// and it gets deleted then the spec is copied into mDeletedSpec and mSpec is
65
// set to point at mDeletedSpec.
66
const ObjectStoreSpec* mSpec;
67
nsAutoPtr<ObjectStoreSpec> mDeletedSpec;
68
69
nsTArray<RefPtr<IDBIndex>> mIndexes;
70
nsTArray<RefPtr<IDBIndex>> mDeletedIndexes;
71
72
const int64_t mId;
73
bool mRooted;
74
75
public:
76
struct StructuredCloneWriteInfo;
77
struct StructuredCloneInfo;
78
79
class MOZ_STACK_CLASS ValueWrapper final {
80
JS::Rooted<JS::Value> mValue;
81
bool mCloned;
82
83
public:
84
ValueWrapper(JSContext* aCx, JS::Handle<JS::Value> aValue)
85
: mValue(aCx, aValue), mCloned(false) {
86
MOZ_COUNT_CTOR(IDBObjectStore::ValueWrapper);
87
}
88
89
~ValueWrapper() { MOZ_COUNT_DTOR(IDBObjectStore::ValueWrapper); }
90
91
const JS::Rooted<JS::Value>& Value() const { return mValue; }
92
93
bool Clone(JSContext* aCx);
94
};
95
96
static already_AddRefed<IDBObjectStore> Create(IDBTransaction* aTransaction,
97
const ObjectStoreSpec& aSpec);
98
99
static void AppendIndexUpdateInfo(int64_t aIndexID, const KeyPath& aKeyPath,
100
bool aMultiEntry, const nsCString& aLocale,
101
JSContext* aCx, JS::Handle<JS::Value> aVal,
102
nsTArray<IndexUpdateInfo>* aUpdateInfoArray,
103
ErrorResult* aRv);
104
105
static void DeserializeIndexValueToUpdateInfos(
106
int64_t aIndexID, const KeyPath& aKeyPath, bool aMultiEntry,
107
const nsCString& aLocale, StructuredCloneReadInfo& aCloneReadInfo,
108
nsTArray<IndexUpdateInfo>& aUpdateInfoArray, ErrorResult& aRv);
109
110
static void ClearCloneReadInfo(StructuredCloneReadInfo& aReadInfo);
111
112
static bool DeserializeValue(JSContext* aCx,
113
StructuredCloneReadInfo& aCloneReadInfo,
114
JS::MutableHandle<JS::Value> aValue);
115
116
static nsresult DeserializeUpgradeValueToFileIds(
117
StructuredCloneReadInfo& aCloneReadInfo, nsAString& aFileIds);
118
119
static const JSClass* DummyPropClass() { return &sDummyPropJSClass; }
120
121
void AssertIsOnOwningThread() const
122
#ifdef DEBUG
123
;
124
#else
125
{
126
}
127
#endif
128
129
int64_t Id() const {
130
AssertIsOnOwningThread();
131
132
return mId;
133
}
134
135
const nsString& Name() const;
136
137
bool AutoIncrement() const;
138
139
const KeyPath& GetKeyPath() const;
140
141
bool HasValidKeyPath() const;
142
143
nsIGlobalObject* GetParentObject() const;
144
145
void GetName(nsString& aName) const {
146
AssertIsOnOwningThread();
147
148
aName = Name();
149
}
150
151
void SetName(const nsAString& aName, ErrorResult& aRv);
152
153
void GetKeyPath(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
154
ErrorResult& aRv);
155
156
already_AddRefed<DOMStringList> IndexNames();
157
158
IDBTransaction* Transaction() const {
159
AssertIsOnOwningThread();
160
161
return mTransaction;
162
}
163
164
already_AddRefed<IDBRequest> Add(JSContext* aCx, JS::Handle<JS::Value> aValue,
165
JS::Handle<JS::Value> aKey,
166
ErrorResult& aRv) {
167
AssertIsOnOwningThread();
168
169
ValueWrapper valueWrapper(aCx, aValue);
170
171
return AddOrPut(aCx, valueWrapper, aKey, false, /* aFromCursor */ false,
172
aRv);
173
}
174
175
already_AddRefed<IDBRequest> Put(JSContext* aCx, JS::Handle<JS::Value> aValue,
176
JS::Handle<JS::Value> aKey,
177
ErrorResult& aRv) {
178
AssertIsOnOwningThread();
179
180
ValueWrapper valueWrapper(aCx, aValue);
181
182
return AddOrPut(aCx, valueWrapper, aKey, true, /* aFromCursor */ false,
183
aRv);
184
}
185
186
already_AddRefed<IDBRequest> Delete(JSContext* aCx,
187
JS::Handle<JS::Value> aKey,
188
ErrorResult& aRv) {
189
AssertIsOnOwningThread();
190
191
return DeleteInternal(aCx, aKey, /* aFromCursor */ false, aRv);
192
}
193
194
already_AddRefed<IDBRequest> Get(JSContext* aCx, JS::Handle<JS::Value> aKey,
195
ErrorResult& aRv) {
196
AssertIsOnOwningThread();
197
198
return GetInternal(/* aKeyOnly */ false, aCx, aKey, aRv);
199
}
200
201
already_AddRefed<IDBRequest> GetKey(JSContext* aCx,
202
JS::Handle<JS::Value> aKey,
203
ErrorResult& aRv) {
204
AssertIsOnOwningThread();
205
206
return GetInternal(/* aKeyOnly */ true, aCx, aKey, aRv);
207
}
208
209
already_AddRefed<IDBRequest> Clear(JSContext* aCx, ErrorResult& aRv);
210
211
already_AddRefed<IDBIndex> CreateIndex(
212
const nsAString& aName, const StringOrStringSequence& aKeyPath,
213
const IDBIndexParameters& aOptionalParameters, ErrorResult& aRv);
214
215
already_AddRefed<IDBIndex> Index(const nsAString& aName, ErrorResult& aRv);
216
217
void DeleteIndex(const nsAString& aName, ErrorResult& aRv);
218
219
already_AddRefed<IDBRequest> Count(JSContext* aCx, JS::Handle<JS::Value> aKey,
220
ErrorResult& aRv);
221
222
already_AddRefed<IDBRequest> GetAll(JSContext* aCx,
223
JS::Handle<JS::Value> aKey,
224
const Optional<uint32_t>& aLimit,
225
ErrorResult& aRv) {
226
AssertIsOnOwningThread();
227
228
return GetAllInternal(/* aKeysOnly */ false, aCx, aKey, aLimit, aRv);
229
}
230
231
already_AddRefed<IDBRequest> GetAllKeys(JSContext* aCx,
232
JS::Handle<JS::Value> aKey,
233
const Optional<uint32_t>& aLimit,
234
ErrorResult& aRv) {
235
AssertIsOnOwningThread();
236
237
return GetAllInternal(/* aKeysOnly */ true, aCx, aKey, aLimit, aRv);
238
}
239
240
already_AddRefed<IDBRequest> OpenCursor(JSContext* aCx,
241
JS::Handle<JS::Value> aRange,
242
IDBCursorDirection aDirection,
243
ErrorResult& aRv) {
244
AssertIsOnOwningThread();
245
246
return OpenCursorInternal(/* aKeysOnly */ false, aCx, aRange, aDirection,
247
aRv);
248
}
249
250
already_AddRefed<IDBRequest> OpenCursor(JSContext* aCx,
251
IDBCursorDirection aDirection,
252
ErrorResult& aRv) {
253
AssertIsOnOwningThread();
254
255
return OpenCursorInternal(/* aKeysOnly */ false, aCx,
256
JS::UndefinedHandleValue, aDirection, aRv);
257
}
258
259
already_AddRefed<IDBRequest> OpenKeyCursor(JSContext* aCx,
260
JS::Handle<JS::Value> aRange,
261
IDBCursorDirection aDirection,
262
ErrorResult& aRv) {
263
AssertIsOnOwningThread();
264
265
return OpenCursorInternal(/* aKeysOnly */ true, aCx, aRange, aDirection,
266
aRv);
267
}
268
269
void RefreshSpec(bool aMayDelete);
270
271
const ObjectStoreSpec& Spec() const;
272
273
void NoteDeletion();
274
275
bool IsDeleted() const {
276
AssertIsOnOwningThread();
277
278
return !!mDeletedSpec;
279
}
280
281
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
282
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBObjectStore)
283
284
// nsWrapperCache
285
virtual JSObject* WrapObject(JSContext* aCx,
286
JS::Handle<JSObject*> aGivenProto) override;
287
288
private:
289
IDBObjectStore(IDBTransaction* aTransaction, const ObjectStoreSpec* aSpec);
290
291
~IDBObjectStore();
292
293
void GetAddInfo(JSContext* aCx, ValueWrapper& aValueWrapper,
294
JS::Handle<JS::Value> aKeyVal,
295
StructuredCloneWriteInfo& aCloneWriteInfo, Key& aKey,
296
nsTArray<IndexUpdateInfo>& aUpdateInfoArray,
297
ErrorResult& aRv);
298
299
already_AddRefed<IDBRequest> AddOrPut(JSContext* aCx,
300
ValueWrapper& aValueWrapper,
301
JS::Handle<JS::Value> aKey,
302
bool aOverwrite, bool aFromCursor,
303
ErrorResult& aRv);
304
305
already_AddRefed<IDBRequest> DeleteInternal(JSContext* aCx,
306
JS::Handle<JS::Value> aKey,
307
bool aFromCursor,
308
ErrorResult& aRv);
309
310
already_AddRefed<IDBRequest> GetInternal(bool aKeyOnly, JSContext* aCx,
311
JS::Handle<JS::Value> aKey,
312
ErrorResult& aRv);
313
314
already_AddRefed<IDBRequest> GetAllInternal(bool aKeysOnly, JSContext* aCx,
315
JS::Handle<JS::Value> aKey,
316
const Optional<uint32_t>& aLimit,
317
ErrorResult& aRv);
318
319
already_AddRefed<IDBRequest> OpenCursorInternal(bool aKeysOnly,
320
JSContext* aCx,
321
JS::Handle<JS::Value> aRange,
322
IDBCursorDirection aDirection,
323
ErrorResult& aRv);
324
};
325
326
} // namespace dom
327
} // namespace mozilla
328
329
#endif // mozilla_dom_idbobjectstore_h__