forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CallbackObject.h
404 lines (336 loc) · 11.2 KB
/
CallbackObject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* A common base class for representing WebIDL callback function and
* callback interface types in C++.
*
* This class implements common functionality like lifetime
* management, initialization with the JS object, and setup of the
* call environment. Subclasses are responsible for providing methods
* that do the call into JS as needed.
*/
#ifndef mozilla_dom_CallbackObject_h
#define mozilla_dom_CallbackObject_h
#include "nsISupports.h"
#include "nsISupportsImpl.h"
#include "nsCycleCollectionParticipant.h"
#include "jswrapper.h"
#include "mozilla/Assertions.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/HoldDropJSObjects.h"
#include "mozilla/Util.h"
#include "nsContentUtils.h"
#include "nsCxPusher.h"
#include "nsWrapperCache.h"
#include "nsJSEnvironment.h"
#include "xpcpublic.h"
namespace mozilla {
namespace dom {
#define DOM_CALLBACKOBJECT_IID \
{ 0xbe74c190, 0x6d76, 0x4991, \
{ 0x84, 0xb9, 0x65, 0x06, 0x99, 0xe6, 0x93, 0x2b } }
class CallbackObject : public nsISupports
{
public:
NS_DECLARE_STATIC_IID_ACCESSOR(DOM_CALLBACKOBJECT_IID)
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackObject)
explicit CallbackObject(JSObject* aCallback)
{
Init(aCallback);
}
virtual ~CallbackObject()
{
DropCallback();
}
JS::Handle<JSObject*> Callback() const
{
JS::ExposeObjectToActiveJS(mCallback);
return CallbackPreserveColor();
}
/*
* This getter does not change the color of the JSObject meaning that the
* object returned is not guaranteed to be kept alive past the next CC.
*
* This should only be called if you are certain that the return value won't
* be passed into a JS API function and that it won't be stored without being
* rooted (or otherwise signaling the stored value to the CC).
*/
JS::Handle<JSObject*> CallbackPreserveColor() const
{
// Calling fromMarkedLocation() is safe because we trace our mCallback, and
// because the value of mCallback cannot change after if has been set.
return JS::Handle<JSObject*>::fromMarkedLocation(mCallback.address());
}
enum ExceptionHandling {
// Report any exception and don't throw it to the caller code.
eReportExceptions,
// Throw an exception to the caller code if the thrown exception is a
// binding object for a DOMError from the caller's scope, otherwise report
// it.
eRethrowContentExceptions,
// Throw any exception to the caller code.
eRethrowExceptions
};
protected:
explicit CallbackObject(CallbackObject* aCallbackObject)
{
Init(aCallbackObject->mCallback);
}
private:
inline void Init(JSObject* aCallback)
{
MOZ_ASSERT(aCallback && !mCallback);
// Set mCallback before we hold, on the off chance that a GC could somehow
// happen in there... (which would be pretty odd, granted).
mCallback = aCallback;
mozilla::HoldJSObjects(this);
}
CallbackObject(const CallbackObject&) MOZ_DELETE;
CallbackObject& operator =(const CallbackObject&) MOZ_DELETE;
protected:
void DropCallback()
{
if (mCallback) {
mCallback = nullptr;
mozilla::DropJSObjects(this);
}
}
JS::Heap<JSObject*> mCallback;
class MOZ_STACK_CLASS CallSetup
{
/**
* A class that performs whatever setup we need to safely make a
* call while this class is on the stack, After the constructor
* returns, the call is safe to make if GetContext() returns
* non-null.
*/
public:
// If aExceptionHandling == eRethrowContentExceptions then aCompartment
// needs to be set to the caller's compartment.
CallSetup(JS::Handle<JSObject*> aCallable, ErrorResult& aRv,
ExceptionHandling aExceptionHandling,
JSCompartment* aCompartment = nullptr);
~CallSetup();
JSContext* GetContext() const
{
return mCx;
}
private:
// We better not get copy-constructed
CallSetup(const CallSetup&) MOZ_DELETE;
bool ShouldRethrowException(JS::Handle<JS::Value> aException);
// Members which can go away whenever
JSContext* mCx;
// Caller's compartment. This will only have a sensible value if
// mExceptionHandling == eRethrowContentExceptions.
JSCompartment* mCompartment;
// And now members whose construction/destruction order we need to control.
nsCxPusher mCxPusher;
// Constructed the rooter within the scope of mCxPusher above, so that it's
// always within a request during its lifetime.
Maybe<JS::Rooted<JSObject*> > mRootedCallable;
// Can't construct a JSAutoCompartment without a JSContext either. Also,
// Put mAc after mCxPusher so that we exit the compartment before we pop the
// JSContext. Though in practice we'll often manually order those two
// things.
Maybe<JSAutoCompartment> mAc;
// An ErrorResult to possibly re-throw exceptions on and whether
// we should re-throw them.
ErrorResult& mErrorResult;
const ExceptionHandling mExceptionHandling;
JS::ContextOptions mSavedJSContextOptions;
const bool mIsMainThread;
};
};
template<class WebIDLCallbackT, class XPCOMCallbackT>
class CallbackObjectHolder;
template<class T, class U>
void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField);
class CallbackObjectHolderBase
{
protected:
// Returns null on all failures
already_AddRefed<nsISupports> ToXPCOMCallback(CallbackObject* aCallback,
const nsIID& aIID) const;
};
template<class WebIDLCallbackT, class XPCOMCallbackT>
class CallbackObjectHolder : CallbackObjectHolderBase
{
/**
* A class which stores either a WebIDLCallbackT* or an XPCOMCallbackT*. Both
* types must inherit from nsISupports. The pointer that's stored can be
* null.
*
* When storing a WebIDLCallbackT*, mPtrBits is set to the pointer value.
* When storing an XPCOMCallbackT*, mPtrBits is the pointer value with low bit
* set.
*/
public:
explicit CallbackObjectHolder(WebIDLCallbackT* aCallback)
: mPtrBits(reinterpret_cast<uintptr_t>(aCallback))
{
NS_IF_ADDREF(aCallback);
}
explicit CallbackObjectHolder(XPCOMCallbackT* aCallback)
: mPtrBits(reinterpret_cast<uintptr_t>(aCallback) | XPCOMCallbackFlag)
{
NS_IF_ADDREF(aCallback);
}
explicit CallbackObjectHolder(const CallbackObjectHolder& aOther)
: mPtrBits(aOther.mPtrBits)
{
NS_IF_ADDREF(GetISupports());
}
CallbackObjectHolder()
: mPtrBits(0)
{}
~CallbackObjectHolder()
{
UnlinkSelf();
}
void operator=(WebIDLCallbackT* aCallback)
{
UnlinkSelf();
mPtrBits = reinterpret_cast<uintptr_t>(aCallback);
NS_IF_ADDREF(aCallback);
}
void operator=(XPCOMCallbackT* aCallback)
{
UnlinkSelf();
mPtrBits = reinterpret_cast<uintptr_t>(aCallback) | XPCOMCallbackFlag;
NS_IF_ADDREF(aCallback);
}
void operator=(const CallbackObjectHolder& aOther)
{
UnlinkSelf();
mPtrBits = aOther.mPtrBits;
NS_IF_ADDREF(GetISupports());
}
nsISupports* GetISupports() const
{
return reinterpret_cast<nsISupports*>(mPtrBits & ~XPCOMCallbackFlag);
}
// Boolean conversion operator so people can use this in boolean tests
operator bool() const
{
return GetISupports();
}
// Even if HasWebIDLCallback returns true, GetWebIDLCallback() might still
// return null.
bool HasWebIDLCallback() const
{
return !(mPtrBits & XPCOMCallbackFlag);
}
WebIDLCallbackT* GetWebIDLCallback() const
{
MOZ_ASSERT(HasWebIDLCallback());
return reinterpret_cast<WebIDLCallbackT*>(mPtrBits);
}
XPCOMCallbackT* GetXPCOMCallback() const
{
MOZ_ASSERT(!HasWebIDLCallback());
return reinterpret_cast<XPCOMCallbackT*>(mPtrBits & ~XPCOMCallbackFlag);
}
bool operator==(WebIDLCallbackT* aOtherCallback) const
{
if (!aOtherCallback) {
// If other is null, then we must be null to be equal.
return !GetISupports();
}
if (!HasWebIDLCallback() || !GetWebIDLCallback()) {
// If other is non-null, then we can't be equal if we have a
// non-WebIDL callback or a null callback.
return false;
}
JSObject* thisObj =
js::UncheckedUnwrap(GetWebIDLCallback()->CallbackPreserveColor());
JSObject* otherObj =
js::UncheckedUnwrap(aOtherCallback->CallbackPreserveColor());
return thisObj == otherObj;
}
bool operator==(XPCOMCallbackT* aOtherCallback) const
{
return (!aOtherCallback && !GetISupports()) ||
(!HasWebIDLCallback() && GetXPCOMCallback() == aOtherCallback);
}
bool operator==(const CallbackObjectHolder& aOtherCallback) const
{
if (aOtherCallback.HasWebIDLCallback()) {
return *this == aOtherCallback.GetWebIDLCallback();
}
return *this == aOtherCallback.GetXPCOMCallback();
}
// Try to return an XPCOMCallbackT version of this object.
already_AddRefed<XPCOMCallbackT> ToXPCOMCallback() const
{
if (!HasWebIDLCallback()) {
nsRefPtr<XPCOMCallbackT> callback = GetXPCOMCallback();
return callback.forget();
}
nsCOMPtr<nsISupports> supp =
CallbackObjectHolderBase::ToXPCOMCallback(GetWebIDLCallback(),
NS_GET_TEMPLATE_IID(XPCOMCallbackT));
// ToXPCOMCallback already did the right QI for us.
return supp.forget().downcast<XPCOMCallbackT>();
}
// Try to return a WebIDLCallbackT version of this object.
already_AddRefed<WebIDLCallbackT> ToWebIDLCallback() const
{
if (HasWebIDLCallback()) {
nsRefPtr<WebIDLCallbackT> callback = GetWebIDLCallback();
return callback.forget();
}
XPCOMCallbackT* callback = GetXPCOMCallback();
if (!callback) {
return nullptr;
}
nsCOMPtr<nsIXPConnectWrappedJS> wrappedJS = do_QueryInterface(callback);
if (!wrappedJS) {
return nullptr;
}
AutoSafeJSContext cx;
JS::Rooted<JSObject*> obj(cx, wrappedJS->GetJSObject());
if (!obj) {
return nullptr;
}
JSAutoCompartment ac(cx, obj);
nsRefPtr<WebIDLCallbackT> newCallback = new WebIDLCallbackT(obj);
return newCallback.forget();
}
private:
static const uintptr_t XPCOMCallbackFlag = 1u;
friend void
ImplCycleCollectionUnlink<WebIDLCallbackT,
XPCOMCallbackT>(CallbackObjectHolder& aField);
void UnlinkSelf()
{
// NS_IF_RELEASE because we might have been unlinked before
nsISupports* ptr = GetISupports();
NS_IF_RELEASE(ptr);
mPtrBits = 0;
}
uintptr_t mPtrBits;
};
NS_DEFINE_STATIC_IID_ACCESSOR(CallbackObject, DOM_CALLBACKOBJECT_IID)
template<class T, class U>
inline void
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
CallbackObjectHolder<T, U>& aField,
const char* aName,
uint32_t aFlags = 0)
{
CycleCollectionNoteChild(aCallback, aField.GetISupports(), aName, aFlags);
}
template<class T, class U>
void
ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField)
{
aField.UnlinkSelf();
}
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_CallbackObject_h