Bug 1905470 - Update android nightly application-services version bump to 7fae3fe1457...
[gecko.git] / dom / bindings / CallbackObject.h
blobdeae1d9914dedc62b20f9cd23e69db30986da821
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/. */
7 /**
8 * A common base class for representing WebIDL callback function and
9 * callback interface types in C++.
11 * This class implements common functionality like lifetime
12 * management, initialization with the JS object, and setup of the
13 * call environment. Subclasses are responsible for providing methods
14 * that do the call into JS as needed.
17 #ifndef mozilla_dom_CallbackObject_h
18 #define mozilla_dom_CallbackObject_h
20 #include <cstddef>
21 #include <cstdint>
22 #include <utility>
23 #include "js/Exception.h"
24 #include "js/RootingAPI.h"
25 #include "js/Wrapper.h"
26 #include "jsapi.h"
27 #include "mozilla/AlreadyAddRefed.h"
28 #include "mozilla/Assertions.h"
29 #include "mozilla/Attributes.h"
30 #include "mozilla/HoldDropJSObjects.h"
31 #include "mozilla/Maybe.h"
32 #include "mozilla/MemoryReporting.h"
33 #include "mozilla/RefPtr.h"
34 #include "mozilla/dom/AutoEntryScript.h"
35 #include "mozilla/dom/BindingCallContext.h"
36 #include "mozilla/dom/ScriptSettings.h"
37 #include "nsCOMPtr.h"
38 #include "nsCycleCollectionParticipant.h"
39 #include "nsID.h"
40 #include "nsIGlobalObject.h"
41 #include "nsISupports.h"
42 #include "nsISupportsUtils.h"
43 #include "nsStringFwd.h"
45 class JSAutoRealm;
46 class JSObject;
47 class JSTracer;
48 class nsCycleCollectionTraversalCallback;
49 struct JSContext;
51 namespace JS {
52 class AutoSetAsyncStackForNewCalls;
53 class Realm;
54 class Value;
55 } // namespace JS
57 namespace mozilla {
59 class ErrorResult;
60 class PromiseJobRunnable;
61 template <class T>
62 class OwningNonNull;
64 namespace dom {
66 #define DOM_CALLBACKOBJECT_IID \
67 { \
68 0xbe74c190, 0x6d76, 0x4991, { \
69 0x84, 0xb9, 0x65, 0x06, 0x99, 0xe6, 0x93, 0x2b \
70 } \
73 class CallbackObject : public nsISupports {
74 public:
75 NS_DECLARE_STATIC_IID_ACCESSOR(DOM_CALLBACKOBJECT_IID)
77 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
78 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(CallbackObject)
80 // The caller may pass a global object which will act as an override for the
81 // incumbent script settings object when the callback is invoked (overriding
82 // the entry point computed from aCallback). If no override is required, the
83 // caller should pass null. |aCx| is used to capture the current
84 // stack, which is later used as an async parent when the callback
85 // is invoked. aCx can be nullptr, in which case no stack is
86 // captured.
87 explicit CallbackObject(JSContext* aCx, JS::Handle<JSObject*> aCallback,
88 JS::Handle<JSObject*> aCallbackGlobal,
89 nsIGlobalObject* aIncumbentGlobal) {
90 if (aCx && JS::IsAsyncStackCaptureEnabledForRealm(aCx)) {
91 JS::Rooted<JSObject*> stack(aCx);
92 if (!JS::CaptureCurrentStack(aCx, &stack)) {
93 JS_ClearPendingException(aCx);
95 Init(aCallback, aCallbackGlobal, stack, aIncumbentGlobal);
96 } else {
97 Init(aCallback, aCallbackGlobal, nullptr, aIncumbentGlobal);
101 // Instead of capturing the current stack to use as an async parent when the
102 // callback is invoked, the caller can use this overload to pass in a stack
103 // for that purpose.
104 explicit CallbackObject(JSObject* aCallback, JSObject* aCallbackGlobal,
105 JSObject* aAsyncStack,
106 nsIGlobalObject* aIncumbentGlobal) {
107 Init(aCallback, aCallbackGlobal, aAsyncStack, aIncumbentGlobal);
110 // This is guaranteed to be non-null from the time the CallbackObject is
111 // created until JavaScript has had a chance to run. It will only return null
112 // after a JavaScript caller has called nukeSandbox on a Sandbox object and
113 // the cycle collector has had a chance to run, unless Reset() is explicitly
114 // called (see below).
116 // This means that any native callee which receives a CallbackObject as an
117 // argument can safely rely on the callback being non-null so long as it
118 // doesn't trigger any scripts before it accesses it.
119 JSObject* CallbackOrNull() const {
120 mCallback.exposeToActiveJS();
121 return CallbackPreserveColor();
124 JSObject* CallbackGlobalOrNull() const {
125 mCallbackGlobal.exposeToActiveJS();
126 return mCallbackGlobal;
129 // Like CallbackOrNull(), but will return a new dead proxy object in the
130 // caller's realm if the callback is null.
131 JSObject* Callback(JSContext* aCx);
133 JSObject* GetCreationStack() const { return mCreationStack; }
135 void MarkForCC() {
136 mCallback.exposeToActiveJS();
137 mCallbackGlobal.exposeToActiveJS();
138 mCreationStack.exposeToActiveJS();
142 * This getter does not change the color of the JSObject meaning that the
143 * object returned is not guaranteed to be kept alive past the next CC.
145 JSObject* CallbackPreserveColor() const { return mCallback.unbarrieredGet(); }
146 JSObject* CallbackGlobalPreserveColor() const {
147 return mCallbackGlobal.unbarrieredGet();
151 * If the callback is known to be non-gray, then this method can be
152 * used instead of CallbackOrNull() to avoid the overhead of
153 * ExposeObjectToActiveJS().
155 JSObject* CallbackKnownNotGray() const {
156 JS::AssertObjectIsNotGray(mCallback);
157 return CallbackPreserveColor();
160 nsIGlobalObject* IncumbentGlobalOrNull() const { return mIncumbentGlobal; }
162 enum ExceptionHandling {
163 // Report any exception and don't throw it to the caller code.
164 eReportExceptions,
165 // Throw any exception to the caller code and don't report it.
166 eRethrowExceptions,
167 // Throw an exception to the caller code if the thrown exception is a
168 // binding object for a DOMException from the caller's scope, otherwise
169 // report it.
170 eRethrowContentExceptions
173 // Append a UTF-8 string to aOutString that describes the callback function,
174 // for use in logging or profiler markers.
175 // The string contains the function name and its source location, if
176 // available, in the following format:
177 // "<functionName> (<sourceURL>:<lineNumber>)"
178 void GetDescription(nsACString& aOutString);
180 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
181 return aMallocSizeOf(this);
184 // Used for cycle collection optimization. Should return true only if all our
185 // outgoing edges are to known-live objects. In that case, there's no point
186 // traversing our edges to them, because we know they can't be collected
187 // anyway.
188 bool IsBlackForCC() const {
189 // Play it safe in case this gets called after unlink.
190 return (!mCallback || !JS::ObjectIsMarkedGray(mCallback)) &&
191 (!mCallbackGlobal || !JS::ObjectIsMarkedGray(mCallbackGlobal)) &&
192 (!mCreationStack || !JS::ObjectIsMarkedGray(mCreationStack)) &&
193 (!mIncumbentJSGlobal ||
194 !JS::ObjectIsMarkedGray(mIncumbentJSGlobal)) &&
195 // mIncumbentGlobal is known-live if we have a known-live
196 // mIncumbentJSGlobal, since mIncumbentJSGlobal will keep a ref to
197 // it. At this point if mIncumbentJSGlobal is not null, it's
198 // known-live.
199 (!mIncumbentGlobal || mIncumbentJSGlobal);
202 protected:
203 virtual ~CallbackObject() { mozilla::DropJSObjects(this); }
205 explicit CallbackObject(CallbackObject* aCallbackObject) {
206 Init(aCallbackObject->mCallback, aCallbackObject->mCallbackGlobal,
207 aCallbackObject->mCreationStack, aCallbackObject->mIncumbentGlobal);
210 bool operator==(const CallbackObject& aOther) const {
211 JSObject* wrappedThis = CallbackPreserveColor();
212 JSObject* wrappedOther = aOther.CallbackPreserveColor();
213 if (!wrappedThis || !wrappedOther) {
214 return this == &aOther;
217 JSObject* thisObj = js::UncheckedUnwrap(wrappedThis);
218 JSObject* otherObj = js::UncheckedUnwrap(wrappedOther);
219 return thisObj == otherObj;
222 class JSObjectsDropper final {
223 public:
224 explicit JSObjectsDropper(CallbackObject* aHolder) : mHolder(aHolder) {}
226 ~JSObjectsDropper() { mHolder->ClearJSObjects(); }
228 private:
229 RefPtr<CallbackObject> mHolder;
232 private:
233 inline void InitNoHold(JSObject* aCallback, JSObject* aCallbackGlobal,
234 JSObject* aCreationStack,
235 nsIGlobalObject* aIncumbentGlobal) {
236 MOZ_ASSERT(aCallback && !mCallback);
237 MOZ_ASSERT(aCallbackGlobal);
238 MOZ_DIAGNOSTIC_ASSERT(JS::GetCompartment(aCallback) ==
239 JS::GetCompartment(aCallbackGlobal));
240 MOZ_ASSERT(JS_IsGlobalObject(aCallbackGlobal));
241 mCallback = aCallback;
242 mCallbackGlobal = aCallbackGlobal;
243 mCreationStack = aCreationStack;
244 if (aIncumbentGlobal) {
245 mIncumbentGlobal = aIncumbentGlobal;
246 // We don't want to expose to JS here (change the color). If someone ever
247 // reads mIncumbentJSGlobal, that will expose. If not, no need to expose
248 // here.
249 mIncumbentJSGlobal = aIncumbentGlobal->GetGlobalJSObjectPreserveColor();
253 inline void Init(JSObject* aCallback, JSObject* aCallbackGlobal,
254 JSObject* aCreationStack,
255 nsIGlobalObject* aIncumbentGlobal) {
256 // Set script objects before we hold, on the off chance that a GC could
257 // somehow happen in there... (which would be pretty odd, granted).
258 InitNoHold(aCallback, aCallbackGlobal, aCreationStack, aIncumbentGlobal);
259 mozilla::HoldJSObjects(this);
262 // Provide a way to clear this object's pointers to GC things after the
263 // callback has been run. Note that CallbackOrNull() will return null after
264 // this point. This should only be called if the object is known not to be
265 // used again, and no handles (e.g. those returned by CallbackPreserveColor)
266 // are in use.
267 void Reset() {
268 ClearJSReferences();
269 mozilla::DropJSObjects(this);
271 friend class mozilla::PromiseJobRunnable;
273 inline void ClearJSReferences() {
274 mCallback = nullptr;
275 mCallbackGlobal = nullptr;
276 mCreationStack = nullptr;
277 mIncumbentJSGlobal = nullptr;
280 CallbackObject(const CallbackObject&) = delete;
281 CallbackObject& operator=(const CallbackObject&) = delete;
283 protected:
284 void ClearJSObjects() {
285 MOZ_ASSERT_IF(mIncumbentJSGlobal, mCallback);
286 if (mCallback) {
287 ClearJSReferences();
291 // For use from subclasses that want to be usable with Rooted.
292 void Trace(JSTracer* aTracer);
294 // For use from subclasses that want to be traced for a bit then possibly
295 // switch to HoldJSObjects and do other slow JS-related init work we might do.
296 // If we have more than one owner, this will HoldJSObjects and do said slow
297 // init work; otherwise it will just forget all our JS references.
298 void FinishSlowJSInitIfMoreThanOneOwner(JSContext* aCx);
300 // Struct used as a way to force a CallbackObject constructor to not call
301 // HoldJSObjects. We're putting it here so that CallbackObject subclasses will
302 // have access to it, but outside code will not.
304 // Places that use this need to ensure that the callback is traced (e.g. via a
305 // Rooted) until the HoldJSObjects call happens.
306 struct FastCallbackConstructor {};
308 // Just like the public version without the FastCallbackConstructor argument,
309 // except for not calling HoldJSObjects and not capturing async stacks (on the
310 // assumption that we will do that last whenever we decide to actually
311 // HoldJSObjects; see FinishSlowJSInitIfMoreThanOneOwner). If you use this,
312 // you MUST ensure that the object is traced until the HoldJSObjects happens!
313 CallbackObject(JSObject* aCallback, JSObject* aCallbackGlobal,
314 const FastCallbackConstructor&) {
315 InitNoHold(aCallback, aCallbackGlobal, nullptr, nullptr);
318 // mCallback is not unwrapped, so it can be a cross-compartment-wrapper.
319 // This is done to ensure that, if JS code can't call a callback f(), or get
320 // its members, directly itself, this code won't call f(), or get its members,
321 // on the code's behalf.
322 JS::Heap<JSObject*> mCallback;
323 // mCallbackGlobal is the global that we were in when we created the
324 // callback. In particular, it is guaranteed to be same-compartment with
325 // aCallback. We store it separately, because we have no way to recover the
326 // global if mCallback is a cross-compartment wrapper.
327 JS::Heap<JSObject*> mCallbackGlobal;
328 JS::Heap<JSObject*> mCreationStack;
329 // Ideally, we'd just hold a reference to the nsIGlobalObject, since that's
330 // what we need to pass to AutoIncumbentScript. Unfortunately, that doesn't
331 // hold the actual JS global alive. So we maintain an additional pointer to
332 // the JS global itself so that we can trace it.
334 // At some point we should consider trying to make native globals hold their
335 // scripted global alive, at which point we can get rid of the duplication
336 // here.
337 nsCOMPtr<nsIGlobalObject> mIncumbentGlobal;
338 JS::TenuredHeap<JSObject*> mIncumbentJSGlobal;
340 class MOZ_STACK_CLASS CallSetup {
342 * A class that performs whatever setup we need to safely make a
343 * call while this class is on the stack, After the constructor
344 * returns, the call is safe to make if GetContext() returns
345 * non-null.
347 public:
348 // If aExceptionHandling == eRethrowContentExceptions then aRealm
349 // needs to be set to the realm in which exceptions will be rethrown.
351 // If aExceptionHandling == eRethrowExceptions then aRealm may be set
352 // to the realm in which exceptions will be rethrown. In that case
353 // they will only be rethrown if that realm's principal subsumes the
354 // principal of our (unwrapped) callback.
355 CallSetup(CallbackObject* aCallback, ErrorResult& aRv,
356 const char* aExecutionReason,
357 ExceptionHandling aExceptionHandling, JS::Realm* aRealm = nullptr,
358 bool aIsJSImplementedWebIDL = false);
359 MOZ_CAN_RUN_SCRIPT ~CallSetup();
361 JSContext* GetContext() const { return mCx; }
363 // Safe to call this after the constructor has run without throwing on the
364 // ErrorResult it was handed.
365 BindingCallContext& GetCallContext() { return *mCallContext; }
367 private:
368 // We better not get copy-constructed
369 CallSetup(const CallSetup&) = delete;
371 bool ShouldRethrowException(JS::Handle<JS::Value> aException);
373 // Members which can go away whenever
374 JSContext* mCx;
376 // Caller's realm. This will only have a sensible value if
377 // mExceptionHandling == eRethrowContentExceptions.
378 JS::Realm* mRealm;
380 // And now members whose construction/destruction order we need to control.
381 Maybe<AutoEntryScript> mAutoEntryScript;
382 Maybe<AutoIncumbentScript> mAutoIncumbentScript;
384 Maybe<JS::Rooted<JSObject*>> mRootedCallable;
385 // The global of mRootedCallable.
386 Maybe<JS::Rooted<JSObject*>> mRootedCallableGlobal;
388 // Members which are used to set the async stack.
389 Maybe<JS::Rooted<JSObject*>> mAsyncStack;
390 Maybe<JS::AutoSetAsyncStackForNewCalls> mAsyncStackSetter;
392 // Can't construct a JSAutoRealm without a JSContext either. Also,
393 // Put mAr after mAutoEntryScript so that we exit the realm before we
394 // pop the script settings stack. Though in practice we'll often manually
395 // order those two things.
396 Maybe<JSAutoRealm> mAr;
398 // Our BindingCallContext. This is a Maybe so we can avoid constructing it
399 // until after we have a JSContext to construct it with.
400 Maybe<BindingCallContext> mCallContext;
402 // An ErrorResult to possibly re-throw exceptions on and whether
403 // we should re-throw them.
404 ErrorResult& mErrorResult;
405 const ExceptionHandling mExceptionHandling;
406 const bool mIsMainThread;
410 template <class WebIDLCallbackT, class XPCOMCallbackT>
411 class CallbackObjectHolder;
413 template <class T, class U>
414 void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField);
416 class CallbackObjectHolderBase {
417 protected:
418 // Returns null on all failures
419 already_AddRefed<nsISupports> ToXPCOMCallback(CallbackObject* aCallback,
420 const nsIID& aIID) const;
423 template <class WebIDLCallbackT, class XPCOMCallbackT>
424 class CallbackObjectHolder : CallbackObjectHolderBase {
426 * A class which stores either a WebIDLCallbackT* or an XPCOMCallbackT*. Both
427 * types must inherit from nsISupports. The pointer that's stored can be
428 * null.
430 * When storing a WebIDLCallbackT*, mPtrBits is set to the pointer value.
431 * When storing an XPCOMCallbackT*, mPtrBits is the pointer value with low bit
432 * set.
434 public:
435 explicit CallbackObjectHolder(WebIDLCallbackT* aCallback)
436 : mPtrBits(reinterpret_cast<uintptr_t>(aCallback)) {
437 NS_IF_ADDREF(aCallback);
440 explicit CallbackObjectHolder(XPCOMCallbackT* aCallback)
441 : mPtrBits(reinterpret_cast<uintptr_t>(aCallback) | XPCOMCallbackFlag) {
442 NS_IF_ADDREF(aCallback);
445 CallbackObjectHolder(CallbackObjectHolder&& aOther)
446 : mPtrBits(aOther.mPtrBits) {
447 aOther.mPtrBits = 0;
448 static_assert(sizeof(CallbackObjectHolder) == sizeof(void*),
449 "This object is expected to be as small as a pointer, and it "
450 "is currently passed by value in various places. If it is "
451 "bloating, we may want to pass it by reference then.");
454 CallbackObjectHolder(const CallbackObjectHolder& aOther) = delete;
456 CallbackObjectHolder() : mPtrBits(0) {}
458 ~CallbackObjectHolder() { UnlinkSelf(); }
460 void operator=(WebIDLCallbackT* aCallback) {
461 UnlinkSelf();
462 mPtrBits = reinterpret_cast<uintptr_t>(aCallback);
463 NS_IF_ADDREF(aCallback);
466 void operator=(XPCOMCallbackT* aCallback) {
467 UnlinkSelf();
468 mPtrBits = reinterpret_cast<uintptr_t>(aCallback) | XPCOMCallbackFlag;
469 NS_IF_ADDREF(aCallback);
472 void operator=(CallbackObjectHolder&& aOther) {
473 UnlinkSelf();
474 mPtrBits = aOther.mPtrBits;
475 aOther.mPtrBits = 0;
478 void operator=(const CallbackObjectHolder& aOther) = delete;
480 void Reset() { UnlinkSelf(); }
482 nsISupports* GetISupports() const {
483 return reinterpret_cast<nsISupports*>(mPtrBits & ~XPCOMCallbackFlag);
486 already_AddRefed<nsISupports> Forget() {
487 // This can be called from random threads. Make sure to not refcount things
488 // in here!
489 nsISupports* supp = GetISupports();
490 mPtrBits = 0;
491 return dont_AddRef(supp);
494 // Boolean conversion operator so people can use this in boolean tests
495 explicit operator bool() const { return GetISupports(); }
497 CallbackObjectHolder Clone() const {
498 CallbackObjectHolder result;
499 result.mPtrBits = mPtrBits;
500 NS_IF_ADDREF(GetISupports());
501 return result;
504 // Even if HasWebIDLCallback returns true, GetWebIDLCallback() might still
505 // return null.
506 bool HasWebIDLCallback() const { return !(mPtrBits & XPCOMCallbackFlag); }
508 WebIDLCallbackT* GetWebIDLCallback() const {
509 MOZ_ASSERT(HasWebIDLCallback());
510 return reinterpret_cast<WebIDLCallbackT*>(mPtrBits);
513 XPCOMCallbackT* GetXPCOMCallback() const {
514 MOZ_ASSERT(!HasWebIDLCallback());
515 return reinterpret_cast<XPCOMCallbackT*>(mPtrBits & ~XPCOMCallbackFlag);
518 bool operator==(WebIDLCallbackT* aOtherCallback) const {
519 if (!aOtherCallback) {
520 // If other is null, then we must be null to be equal.
521 return !GetISupports();
524 if (!HasWebIDLCallback() || !GetWebIDLCallback()) {
525 // If other is non-null, then we can't be equal if we have a
526 // non-WebIDL callback or a null callback.
527 return false;
530 return *GetWebIDLCallback() == *aOtherCallback;
533 bool operator==(XPCOMCallbackT* aOtherCallback) const {
534 return (!aOtherCallback && !GetISupports()) ||
535 (!HasWebIDLCallback() && GetXPCOMCallback() == aOtherCallback);
538 bool operator==(const CallbackObjectHolder& aOtherCallback) const {
539 if (aOtherCallback.HasWebIDLCallback()) {
540 return *this == aOtherCallback.GetWebIDLCallback();
543 return *this == aOtherCallback.GetXPCOMCallback();
546 // Try to return an XPCOMCallbackT version of this object.
547 already_AddRefed<XPCOMCallbackT> ToXPCOMCallback() const {
548 if (!HasWebIDLCallback()) {
549 RefPtr<XPCOMCallbackT> callback = GetXPCOMCallback();
550 return callback.forget();
553 nsCOMPtr<nsISupports> supp = CallbackObjectHolderBase::ToXPCOMCallback(
554 GetWebIDLCallback(), NS_GET_TEMPLATE_IID(XPCOMCallbackT));
555 if (supp) {
556 // ToXPCOMCallback already did the right QI for us.
557 return supp.forget().downcast<XPCOMCallbackT>();
559 return nullptr;
562 // Try to return a WebIDLCallbackT version of this object.
563 already_AddRefed<WebIDLCallbackT> ToWebIDLCallback() const {
564 if (HasWebIDLCallback()) {
565 RefPtr<WebIDLCallbackT> callback = GetWebIDLCallback();
566 return callback.forget();
568 return nullptr;
571 private:
572 static const uintptr_t XPCOMCallbackFlag = 1u;
574 friend void ImplCycleCollectionUnlink<WebIDLCallbackT, XPCOMCallbackT>(
575 CallbackObjectHolder& aField);
577 void UnlinkSelf() {
578 // NS_IF_RELEASE because we might have been unlinked before
579 nsISupports* ptr = GetISupports();
580 // Clear mPtrBits before the release to prevent reentrance.
581 mPtrBits = 0;
582 NS_IF_RELEASE(ptr);
585 uintptr_t mPtrBits;
588 NS_DEFINE_STATIC_IID_ACCESSOR(CallbackObject, DOM_CALLBACKOBJECT_IID)
590 template <class T, class U>
591 inline void ImplCycleCollectionTraverse(
592 nsCycleCollectionTraversalCallback& aCallback,
593 CallbackObjectHolder<T, U>& aField, const char* aName,
594 uint32_t aFlags = 0) {
595 if (aField) {
596 CycleCollectionNoteChild(aCallback, aField.GetISupports(), aName, aFlags);
600 template <class T, class U>
601 void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField) {
602 aField.UnlinkSelf();
605 // T is expected to be a RefPtr or OwningNonNull around a CallbackObject
606 // subclass. This class is used in bindings to safely handle Fast* callbacks;
607 // it ensures that the callback is traced, and that if something is holding onto
608 // the callback when we're done with it HoldJSObjects is called.
610 // Since we effectively hold a ref to a refcounted thing (like RefPtr or
611 // OwningNonNull), we are also MOZ_IS_SMARTPTR_TO_REFCOUNTED for static analysis
612 // purposes.
613 template <typename T>
614 class MOZ_RAII MOZ_IS_SMARTPTR_TO_REFCOUNTED RootedCallback
615 : public JS::Rooted<T> {
616 public:
617 explicit RootedCallback(JSContext* cx) : JS::Rooted<T>(cx), mCx(cx) {}
619 // We need a way to make assignment from pointers (how we're normally used)
620 // work.
621 template <typename S>
622 void operator=(S* arg) {
623 this->get().operator=(arg);
626 // But nullptr can't use the above template, because it doesn't know which S
627 // to select. So we need a special overload for nullptr.
628 void operator=(decltype(nullptr) arg) { this->get().operator=(arg); }
630 // Codegen relies on being able to do CallbackOrNull() and Callback() on us.
631 JSObject* CallbackOrNull() const { return this->get()->CallbackOrNull(); }
633 JSObject* Callback(JSContext* aCx) const {
634 return this->get()->Callback(aCx);
637 ~RootedCallback() {
638 // Ensure that our callback starts holding on to its own JS objects as
639 // needed. We really do need to check that things are initialized even when
640 // T is OwningNonNull, because we might be running before the OwningNonNull
641 // ever got assigned to!
642 if (IsInitialized(this->get())) {
643 this->get()->FinishSlowJSInitIfMoreThanOneOwner(mCx);
647 private:
648 template <typename U>
649 static bool IsInitialized(U& aArg); // Not implemented
651 template <typename U>
652 static bool IsInitialized(RefPtr<U>& aRefPtr) {
653 return aRefPtr;
656 template <typename U>
657 static bool IsInitialized(OwningNonNull<U>& aOwningNonNull) {
658 return aOwningNonNull.isInitialized();
661 JSContext* mCx;
664 } // namespace dom
665 } // namespace mozilla
667 #endif // mozilla_dom_CallbackObject_h