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_idbcursor_h__
8
#define mozilla_dom_idbcursor_h__
9
10
#include "IndexedDatabase.h"
11
#include "js/RootingAPI.h"
12
#include "mozilla/Attributes.h"
13
#include "mozilla/dom/IDBCursorBinding.h"
14
#include "mozilla/dom/indexedDB/Key.h"
15
#include "nsCycleCollectionParticipant.h"
16
#include "nsWrapperCache.h"
17
18
class nsIGlobalObject;
19
20
namespace mozilla {
21
22
class ErrorResult;
23
24
namespace dom {
25
26
class IDBIndex;
27
class IDBObjectStore;
28
class IDBRequest;
29
class IDBTransaction;
30
class OwningIDBObjectStoreOrIDBIndex;
31
32
namespace indexedDB {
33
class BackgroundCursorChild;
34
}
35
36
// TODO: Consider defining different subclasses for the different cursor types,
37
// possibly using the CRTP, which would remove the need for various case
38
// distinctions.
39
class IDBCursor final : public nsISupports, public nsWrapperCache {
40
public:
41
typedef indexedDB::Key Key;
42
typedef indexedDB::StructuredCloneReadInfo StructuredCloneReadInfo;
43
44
enum Direction {
45
NEXT = 0,
46
NEXT_UNIQUE,
47
PREV,
48
PREV_UNIQUE,
49
50
// Only needed for IPC serialization helper, should never be used in code.
51
DIRECTION_INVALID
52
};
53
54
enum Type {
55
Type_ObjectStore,
56
Type_ObjectStoreKey,
57
Type_Index,
58
Type_IndexKey,
59
};
60
61
private:
62
indexedDB::BackgroundCursorChild* mBackgroundActor;
63
64
// TODO: mRequest, mSourceObjectStore and mSourceIndex could be made const if
65
// Bug 1575173 is resolved. They are initialized in the constructor and never
66
// modified/cleared.
67
RefPtr<IDBRequest> mRequest;
68
RefPtr<IDBObjectStore> mSourceObjectStore;
69
RefPtr<IDBIndex> mSourceIndex;
70
71
// mSourceObjectStore or mSourceIndex will hold this alive.
72
IDBTransaction* const mTransaction;
73
74
// These are cycle-collected!
75
JS::Heap<JS::Value> mCachedKey;
76
JS::Heap<JS::Value> mCachedPrimaryKey;
77
JS::Heap<JS::Value> mCachedValue;
78
79
Key mKey;
80
Key mSortKey; ///< AKA locale aware key/position elsewhere
81
Key mPrimaryKey; ///< AKA object store key/position elsewhere
82
StructuredCloneReadInfo mCloneInfo;
83
84
const Type mType;
85
const Direction mDirection;
86
87
bool mHaveCachedKey : 1;
88
bool mHaveCachedPrimaryKey : 1;
89
bool mHaveCachedValue : 1;
90
bool mRooted : 1;
91
bool mContinueCalled : 1;
92
bool mHaveValue : 1;
93
94
public:
95
static already_AddRefed<IDBCursor> Create(
96
indexedDB::BackgroundCursorChild* aBackgroundActor, Key aKey,
97
StructuredCloneReadInfo&& aCloneInfo);
98
99
static already_AddRefed<IDBCursor> Create(
100
indexedDB::BackgroundCursorChild* aBackgroundActor, Key aKey);
101
102
static already_AddRefed<IDBCursor> Create(
103
indexedDB::BackgroundCursorChild* aBackgroundActor, Key aKey,
104
Key aSortKey, Key aPrimaryKey, StructuredCloneReadInfo&& aCloneInfo);
105
106
static already_AddRefed<IDBCursor> Create(
107
indexedDB::BackgroundCursorChild* aBackgroundActor, Key aKey,
108
Key aSortKey, Key aPrimaryKey);
109
110
static Direction ConvertDirection(IDBCursorDirection aDirection);
111
112
void AssertIsOnOwningThread() const
113
#ifdef DEBUG
114
;
115
#else
116
{
117
}
118
#endif
119
120
nsIGlobalObject* GetParentObject() const;
121
122
void GetSource(OwningIDBObjectStoreOrIDBIndex& aSource) const;
123
124
IDBCursorDirection GetDirection() const;
125
126
Type GetType() const;
127
128
void GetKey(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
129
ErrorResult& aRv);
130
131
void GetPrimaryKey(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
132
ErrorResult& aRv);
133
134
void GetValue(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
135
ErrorResult& aRv);
136
137
void Continue(JSContext* aCx, JS::Handle<JS::Value> aKey, ErrorResult& aRv);
138
139
void ContinuePrimaryKey(JSContext* aCx, JS::Handle<JS::Value> aKey,
140
JS::Handle<JS::Value> aPrimaryKey, ErrorResult& aRv);
141
142
void Advance(uint32_t aCount, ErrorResult& aRv);
143
144
already_AddRefed<IDBRequest> Update(JSContext* aCx,
145
JS::Handle<JS::Value> aValue,
146
ErrorResult& aRv);
147
148
already_AddRefed<IDBRequest> Delete(JSContext* aCx, ErrorResult& aRv);
149
150
void Reset();
151
152
void Reset(Key&& aKey, StructuredCloneReadInfo&& aValue);
153
154
void Reset(Key&& aKey);
155
156
void Reset(Key&& aKey, Key&& aSortKey, Key&& aPrimaryKey,
157
StructuredCloneReadInfo&& aValue);
158
159
void Reset(Key&& aKey, Key&& aSortKey, Key&& aPrimaryKey);
160
161
void ClearBackgroundActor() {
162
AssertIsOnOwningThread();
163
164
mBackgroundActor = nullptr;
165
}
166
167
void InvalidateCachedResponses();
168
169
// Checks if this is a locale aware cursor (ie. the index's sortKey is unset)
170
bool IsLocaleAware() const;
171
172
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
173
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(IDBCursor)
174
175
// nsWrapperCache
176
virtual JSObject* WrapObject(JSContext* aCx,
177
JS::Handle<JSObject*> aGivenProto) override;
178
179
private:
180
IDBCursor(Type aType, indexedDB::BackgroundCursorChild* aBackgroundActor,
181
Key aKey);
182
183
~IDBCursor();
184
185
void DropJSObjects();
186
187
bool IsSourceDeleted() const;
188
};
189
190
} // namespace dom
191
} // namespace mozilla
192
193
#endif // mozilla_dom_idbcursor_h__