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_idbrequest_h__
8
#define mozilla_dom_idbrequest_h__
9
10
#include "js/RootingAPI.h"
11
#include "mozilla/Attributes.h"
12
#include "mozilla/EventForwards.h"
13
#include "mozilla/dom/IDBRequestBinding.h"
14
#include "mozilla/DOMEventTargetHelper.h"
15
#include "nsAutoPtr.h"
16
#include "nsCycleCollectionParticipant.h"
17
18
#define PRIVATE_IDBREQUEST_IID \
19
{ \
20
0xe68901e5, 0x1d50, 0x4ee9, { \
21
0xaf, 0x49, 0x90, 0x99, 0x4a, 0xff, 0xc8, 0x39 \
22
} \
23
}
24
25
class nsIGlobalObject;
26
27
namespace mozilla {
28
29
class ErrorResult;
30
31
namespace dom {
32
33
class DOMException;
34
class IDBCursor;
35
class IDBDatabase;
36
class IDBFactory;
37
class IDBIndex;
38
class IDBObjectStore;
39
class IDBTransaction;
40
template <typename>
41
struct Nullable;
42
class OwningIDBObjectStoreOrIDBIndexOrIDBCursor;
43
class StrongWorkerRef;
44
45
class IDBRequest : public DOMEventTargetHelper {
46
protected:
47
// mSourceAsObjectStore and mSourceAsIndex are exclusive and one must always
48
// be set. mSourceAsCursor is sometimes set also.
49
RefPtr<IDBObjectStore> mSourceAsObjectStore;
50
RefPtr<IDBIndex> mSourceAsIndex;
51
RefPtr<IDBCursor> mSourceAsCursor;
52
53
RefPtr<IDBTransaction> mTransaction;
54
55
JS::Heap<JS::Value> mResultVal;
56
RefPtr<DOMException> mError;
57
58
nsString mFilename;
59
uint64_t mLoggingSerialNumber;
60
nsresult mErrorCode;
61
uint32_t mLineNo;
62
uint32_t mColumn;
63
bool mHaveResultOrErrorCode;
64
65
public:
66
class ResultCallback;
67
68
static already_AddRefed<IDBRequest> Create(JSContext* aCx,
69
IDBDatabase* aDatabase,
70
IDBTransaction* aTransaction);
71
72
static already_AddRefed<IDBRequest> Create(JSContext* aCx,
73
IDBObjectStore* aSource,
74
IDBDatabase* aDatabase,
75
IDBTransaction* aTransaction);
76
77
static already_AddRefed<IDBRequest> Create(JSContext* aCx, IDBIndex* aSource,
78
IDBDatabase* aDatabase,
79
IDBTransaction* aTransaction);
80
81
static void CaptureCaller(JSContext* aCx, nsAString& aFilename,
82
uint32_t* aLineNo, uint32_t* aColumn);
83
84
static uint64_t NextSerialNumber();
85
86
// EventTarget
87
void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
88
89
void GetSource(
90
Nullable<OwningIDBObjectStoreOrIDBIndexOrIDBCursor>& aSource) const;
91
92
void Reset();
93
94
void SetResultCallback(ResultCallback* aCallback);
95
96
void SetError(nsresult aRv);
97
98
nsresult GetErrorCode() const
99
#ifdef DEBUG
100
;
101
#else
102
{
103
return mErrorCode;
104
}
105
#endif
106
107
DOMException* GetErrorAfterResult() const
108
#ifdef DEBUG
109
;
110
#else
111
{
112
return mError;
113
}
114
#endif
115
116
DOMException* GetError(ErrorResult& aRv);
117
118
void GetCallerLocation(nsAString& aFilename, uint32_t* aLineNo,
119
uint32_t* aColumn) const;
120
121
bool IsPending() const { return !mHaveResultOrErrorCode; }
122
123
uint64_t LoggingSerialNumber() const {
124
AssertIsOnOwningThread();
125
126
return mLoggingSerialNumber;
127
}
128
129
void SetLoggingSerialNumber(uint64_t aLoggingSerialNumber);
130
131
nsIGlobalObject* GetParentObject() const { return GetOwnerGlobal(); }
132
133
void GetResult(JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) const;
134
135
void GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult,
136
ErrorResult& aRv) const {
137
GetResult(aResult, aRv);
138
}
139
140
IDBTransaction* GetTransaction() const {
141
AssertIsOnOwningThread();
142
143
return mTransaction;
144
}
145
146
IDBRequestReadyState ReadyState() const;
147
148
void SetSource(IDBCursor* aSource);
149
150
IMPL_EVENT_HANDLER(success);
151
IMPL_EVENT_HANDLER(error);
152
153
void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(IDBRequest); }
154
155
NS_DECL_ISUPPORTS_INHERITED
156
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(IDBRequest,
157
DOMEventTargetHelper)
158
159
// nsWrapperCache
160
virtual JSObject* WrapObject(JSContext* aCx,
161
JS::Handle<JSObject*> aGivenProto) override;
162
163
protected:
164
explicit IDBRequest(IDBDatabase* aDatabase);
165
explicit IDBRequest(nsIGlobalObject* aGlobal);
166
~IDBRequest();
167
168
void InitMembers();
169
170
void ConstructResult();
171
};
172
173
class NS_NO_VTABLE IDBRequest::ResultCallback {
174
public:
175
virtual nsresult GetResult(JSContext* aCx,
176
JS::MutableHandle<JS::Value> aResult) = 0;
177
178
protected:
179
ResultCallback() = default;
180
};
181
182
class IDBOpenDBRequest final : public IDBRequest {
183
// Only touched on the owning thread.
184
RefPtr<IDBFactory> mFactory;
185
186
RefPtr<StrongWorkerRef> mWorkerRef;
187
188
const bool mFileHandleDisabled;
189
bool mIncreasedActiveDatabaseCount;
190
191
public:
192
static already_AddRefed<IDBOpenDBRequest> Create(JSContext* aCx,
193
IDBFactory* aFactory,
194
nsIGlobalObject* aGlobal);
195
196
bool IsFileHandleDisabled() const { return mFileHandleDisabled; }
197
198
void SetTransaction(IDBTransaction* aTransaction);
199
200
void DispatchNonTransactionError(nsresult aErrorCode);
201
202
void NoteComplete();
203
204
// EventTarget
205
virtual nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;
206
207
IDBFactory* Factory() const { return mFactory; }
208
209
IMPL_EVENT_HANDLER(blocked);
210
IMPL_EVENT_HANDLER(upgradeneeded);
211
212
NS_DECL_ISUPPORTS_INHERITED
213
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(IDBOpenDBRequest, IDBRequest)
214
215
// nsWrapperCache
216
virtual JSObject* WrapObject(JSContext* aCx,
217
JS::Handle<JSObject*> aGivenProto) override;
218
219
private:
220
IDBOpenDBRequest(IDBFactory* aFactory, nsIGlobalObject* aGlobal,
221
bool aFileHandleDisabled);
222
223
~IDBOpenDBRequest();
224
225
void IncreaseActiveDatabaseCount();
226
227
void MaybeDecreaseActiveDatabaseCount();
228
};
229
230
} // namespace dom
231
} // namespace mozilla
232
233
#endif // mozilla_dom_idbrequest_h__