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_idbindex_h__
8
#define mozilla_dom_idbindex_h__
9
10
#include "js/RootingAPI.h"
11
#include "mozilla/Attributes.h"
12
#include "mozilla/dom/IDBCursorBinding.h"
13
#include "nsAutoPtr.h"
14
#include "nsCycleCollectionParticipant.h"
15
#include "nsISupports.h"
16
#include "nsTArrayForwardDeclare.h"
17
#include "nsWrapperCache.h"
18
19
class nsIGlobalObject;
20
21
namespace mozilla {
22
23
class ErrorResult;
24
25
namespace dom {
26
27
class IDBObjectStore;
28
class IDBRequest;
29
template <typename>
30
class Sequence;
31
32
namespace indexedDB {
33
class IndexMetadata;
34
class KeyPath;
35
} // namespace indexedDB
36
37
class IDBIndex final : public nsISupports, public nsWrapperCache {
38
// TODO: This could be made const if Bug 1575173 is resolved. It is
39
// initialized in the constructor and never modified/cleared.
40
RefPtr<IDBObjectStore> mObjectStore;
41
42
JS::Heap<JS::Value> mCachedKeyPath;
43
44
// This normally points to the IndexMetadata owned by the parent IDBDatabase
45
// object. However, if this index is part of a versionchange transaction and
46
// it gets deleted then the metadata is copied into mDeletedMetadata and
47
// mMetadata is set to point at mDeletedMetadata.
48
const indexedDB::IndexMetadata* mMetadata;
49
nsAutoPtr<indexedDB::IndexMetadata> mDeletedMetadata;
50
51
const int64_t mId;
52
bool mRooted;
53
54
public:
55
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
56
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBIndex)
57
58
static already_AddRefed<IDBIndex> Create(
59
IDBObjectStore* aObjectStore, const indexedDB::IndexMetadata& aMetadata);
60
61
int64_t Id() const {
62
AssertIsOnOwningThread();
63
64
return mId;
65
}
66
67
const nsString& Name() const;
68
69
bool Unique() const;
70
71
bool MultiEntry() const;
72
73
bool LocaleAware() const;
74
75
const indexedDB::KeyPath& GetKeyPath() const;
76
77
void GetLocale(nsString& aLocale) const;
78
79
const nsCString& Locale() const;
80
81
bool IsAutoLocale() const;
82
83
IDBObjectStore* ObjectStore() const {
84
AssertIsOnOwningThread();
85
return mObjectStore;
86
}
87
88
nsIGlobalObject* GetParentObject() const;
89
90
void GetName(nsString& aName) const { aName = Name(); }
91
92
void SetName(const nsAString& aName, ErrorResult& aRv);
93
94
void GetKeyPath(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
95
ErrorResult& aRv);
96
97
already_AddRefed<IDBRequest> OpenCursor(JSContext* aCx,
98
JS::Handle<JS::Value> aRange,
99
IDBCursorDirection aDirection,
100
ErrorResult& aRv) {
101
AssertIsOnOwningThread();
102
103
return OpenCursorInternal(/* aKeysOnly */ false, aCx, aRange, aDirection,
104
aRv);
105
}
106
107
already_AddRefed<IDBRequest> OpenKeyCursor(JSContext* aCx,
108
JS::Handle<JS::Value> aRange,
109
IDBCursorDirection aDirection,
110
ErrorResult& aRv) {
111
AssertIsOnOwningThread();
112
113
return OpenCursorInternal(/* aKeysOnly */ true, aCx, aRange, aDirection,
114
aRv);
115
}
116
117
already_AddRefed<IDBRequest> Get(JSContext* aCx, JS::Handle<JS::Value> aKey,
118
ErrorResult& aRv) {
119
AssertIsOnOwningThread();
120
121
return GetInternal(/* aKeyOnly */ false, aCx, aKey, aRv);
122
}
123
124
already_AddRefed<IDBRequest> GetKey(JSContext* aCx,
125
JS::Handle<JS::Value> aKey,
126
ErrorResult& aRv) {
127
AssertIsOnOwningThread();
128
129
return GetInternal(/* aKeyOnly */ true, aCx, aKey, aRv);
130
}
131
132
already_AddRefed<IDBRequest> Count(JSContext* aCx, JS::Handle<JS::Value> aKey,
133
ErrorResult& aRv);
134
135
already_AddRefed<IDBRequest> GetAll(JSContext* aCx,
136
JS::Handle<JS::Value> aKey,
137
const Optional<uint32_t>& aLimit,
138
ErrorResult& aRv) {
139
AssertIsOnOwningThread();
140
141
return GetAllInternal(/* aKeysOnly */ false, aCx, aKey, aLimit, aRv);
142
}
143
144
already_AddRefed<IDBRequest> GetAllKeys(JSContext* aCx,
145
JS::Handle<JS::Value> aKey,
146
const Optional<uint32_t>& aLimit,
147
ErrorResult& aRv) {
148
AssertIsOnOwningThread();
149
150
return GetAllInternal(/* aKeysOnly */ true, aCx, aKey, aLimit, aRv);
151
}
152
153
void RefreshMetadata(bool aMayDelete);
154
155
void NoteDeletion();
156
157
bool IsDeleted() const {
158
AssertIsOnOwningThread();
159
160
return !!mDeletedMetadata;
161
}
162
163
void AssertIsOnOwningThread() const
164
#ifdef DEBUG
165
;
166
#else
167
{
168
}
169
#endif
170
171
// nsWrapperCache
172
virtual JSObject* WrapObject(JSContext* aCx,
173
JS::Handle<JSObject*> aGivenProto) override;
174
175
private:
176
IDBIndex(IDBObjectStore* aObjectStore,
177
const indexedDB::IndexMetadata* aMetadata);
178
179
~IDBIndex();
180
181
already_AddRefed<IDBRequest> GetInternal(bool aKeyOnly, JSContext* aCx,
182
JS::Handle<JS::Value> aKey,
183
ErrorResult& aRv);
184
185
already_AddRefed<IDBRequest> GetAllInternal(bool aKeysOnly, JSContext* aCx,
186
JS::Handle<JS::Value> aKey,
187
const Optional<uint32_t>& aLimit,
188
ErrorResult& aRv);
189
190
already_AddRefed<IDBRequest> OpenCursorInternal(bool aKeysOnly,
191
JSContext* aCx,
192
JS::Handle<JS::Value> aRange,
193
IDBCursorDirection aDirection,
194
ErrorResult& aRv);
195
};
196
197
} // namespace dom
198
} // namespace mozilla
199
200
#endif // mozilla_dom_idbindex_h__