no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / bindings / CallbackObject.h
blob74d27886a99639e1aabf921228970a7ab0a7c4e8
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() { ClearJSReferences(); }
268 friend class mozilla::PromiseJobRunnable;
270 inline void ClearJSReferences() {
271 mCallback = nullptr;
272 mCallbackGlobal = nullptr;
273 mCreationStack = nullptr;
274 mIncumbentJSGlobal = nullptr;
277 CallbackObject(const CallbackObject&) = delete;
278 CallbackObject& operator=(const CallbackObject&) = delete;
280 protected:
281 void ClearJSObjects() {
282 MOZ_ASSERT_IF(mIncumbentJSGlobal, mCallback);
283 if (mCallback) {
284 ClearJSReferences();
288 // For use from subclasses that want to be usable with Rooted.
289 void Trace(JSTracer* aTracer);
291 // For use from subclasses that want to be traced for a bit then possibly
292 // switch to HoldJSObjects and do other slow JS-related init work we might do.
293 // If we have more than one owner, this will HoldJSObjects and do said slow
294 // init work; otherwise it will just forget all our JS references.
295 void FinishSlowJSInitIfMoreThanOneOwner(JSContext* aCx);
297 // Struct used as a way to force a CallbackObject constructor to not call
298 // HoldJSObjects. We're putting it here so that CallbackObject subclasses will
299 // have access to it, but outside code will not.
301 // Places that use this need to ensure that the callback is traced (e.g. via a
302 // Rooted) until the HoldJSObjects call happens.
303 struct FastCallbackConstructor {};
305 // Just like the public version without the FastCallbackConstructor argument,
306 // except for not calling HoldJSObjects and not capturing async stacks (on the
307 // assumption that we will do that last whenever we decide to actually
308 // HoldJSObjects; see FinishSlowJSInitIfMoreThanOneOwner). If you use this,
309 // you MUST ensure that the object is traced until the HoldJSObjects happens!
310 CallbackObject(JSObject* aCallback, JSObject* aCallbackGlobal,
311 const FastCallbackConstructor&) {
312 InitNoHold(aCallback, aCallbackGlobal, nullptr, nullptr);
315 // mCallback is not unwrapped, so it can be a cross-compartment-wrapper.
316 // This is done to ensure that, if JS code can't call a callback f(), or get
317 // its members, directly itself, this code won't call f(), or get its members,
318 // on the code's behalf.
319 JS::Heap<JSObject*> mCallback;
320 // mCallbackGlobal is the global that we were in when we created the
321 // callback. In particular, it is guaranteed to be same-compartment with
322 // aCallback. We store it separately, because we have no way to recover the
323 // global if mCallback is a cross-compartment wrapper.
324 JS::Heap<JSObject*> mCallbackGlobal;
325 JS::Heap<JSObject*> mCreationStack;
326 // Ideally, we'd just hold a reference to the nsIGlobalObject, since that's
327 // what we need to pass to AutoIncumbentScript. Unfortunately, that doesn't
328 // hold the actual JS global alive. So we maintain an additional pointer to
329 // the JS global itself so that we can trace it.
331 // At some point we should consider trying to make native globals hold their
332 // scripted global alive, at which point we can get rid of the duplication
333 // here.
334 nsCOMPtr<nsIGlobalObject> mIncumbentGlobal;
335 JS::TenuredHeap<JSObject*> mIncumbentJSGlobal;
337 class MOZ_STACK_CLASS CallSetup {
339 * A class that performs whatever setup we need to safely make a
340 * call while this class is on the stack, After the constructor
341 * returns, the call is safe to make if GetContext() returns
342 * non-null.
344 public:
345 // If aExceptionHandling == eRethrowContentExceptions then aRealm
346 // needs to be set to the realm in which exceptions will be rethrown.
348 // If aExceptionHandling == eRethrowExceptions then aRealm may be set
349 // to the realm in which exceptions will be rethrown. In that case
350 // they will only be rethrown if that realm's principal subsumes the
351 // principal of our (unwrapped) callback.
352 CallSetup(CallbackObject* aCallback, ErrorResult& aRv,
353 const char* aExecutionReason,
354 ExceptionHandling aExceptionHandling, JS::Realm* aRealm = nullptr,
355 bool aIsJSImplementedWebIDL = false);
356 MOZ_CAN_RUN_SCRIPT ~CallSetup();
358 JSContext* GetContext() const { return mCx; }
360 // Safe to call this after the constructor has run without throwing on the
361 // ErrorResult it was handed.
362 BindingCallContext& GetCallContext() { return *mCallContext; }
364 private:
365 // We better not get copy-constructed
366 CallSetup(const CallSetup&) = delete;
368 bool ShouldRethrowException(JS::Handle<JS::Value> aException);
370 // Members which can go away whenever
371 JSContext* mCx;
373 // Caller's realm. This will only have a sensible value if
374 // mExceptionHandling == eRethrowContentExceptions.
375 JS::Realm* mRealm;
377 // And now members whose construction/destruction order we need to control.
378 Maybe<AutoEntryScript> mAutoEntryScript;
379 Maybe<AutoIncumbentScript> mAutoIncumbentScript;
381 Maybe<JS::Rooted<JSObject*>> mRootedCallable;
382 // The global of mRootedCallable.
383 Maybe<JS::Rooted<JSObject*>> mRootedCallableGlobal;
385 // Members which are used to set the async stack.
386 Maybe<JS::Rooted<JSObject*>> mAsyncStack;
387 Maybe<JS::AutoSetAsyncStackForNewCalls> mAsyncStackSetter;
389 // Can't construct a JSAutoRealm without a JSContext either. Also,
390 // Put mAr after mAutoEntryScript so that we exit the realm before we
391 // pop the script settings stack. Though in practice we'll often manually
392 // order those two things.
393 Maybe<JSAutoRealm> mAr;
395 // Our BindingCallContext. This is a Maybe so we can avoid constructing it
396 // until after we have a JSContext to construct it with.
397 Maybe<BindingCallContext> mCallContext;
399 // An ErrorResult to possibly re-throw exceptions on and whether
400 // we should re-throw them.
401 ErrorResult& mErrorResult;
402 const ExceptionHandling mExceptionHandling;
403 const bool mIsMainThread;
407 template <class WebIDLCallbackT, class XPCOMCallbackT>
408 class CallbackObjectHolder;
410 template <class T, class U>
411 void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField);
413 class CallbackObjectHolderBase {
414 protected:
415 // Returns null on all failures
416 already_AddRefed<nsISupports> ToXPCOMCallback(CallbackObject* aCallback,
417 const nsIID& aIID) const;
420 template <class WebIDLCallbackT, class XPCOMCallbackT>
421 class CallbackObjectHolder : CallbackObjectHolderBase {
423 * A class which stores either a WebIDLCallbackT* or an XPCOMCallbackT*. Both
424 * types must inherit from nsISupports. The pointer that's stored can be
425 * null.
427 * When storing a WebIDLCallbackT*, mPtrBits is set to the pointer value.
428 * When storing an XPCOMCallbackT*, mPtrBits is the pointer value with low bit
429 * set.
431 public:
432 explicit CallbackObjectHolder(WebIDLCallbackT* aCallback)
433 : mPtrBits(reinterpret_cast<uintptr_t>(aCallback)) {
434 NS_IF_ADDREF(aCallback);
437 explicit CallbackObjectHolder(XPCOMCallbackT* aCallback)
438 : mPtrBits(reinterpret_cast<uintptr_t>(aCallback) | XPCOMCallbackFlag) {
439 NS_IF_ADDREF(aCallback);
442 CallbackObjectHolder(CallbackObjectHolder&& aOther)
443 : mPtrBits(aOther.mPtrBits) {
444 aOther.mPtrBits = 0;
445 static_assert(sizeof(CallbackObjectHolder) == sizeof(void*),
446 "This object is expected to be as small as a pointer, and it "
447 "is currently passed by value in various places. If it is "
448 "bloating, we may want to pass it by reference then.");
451 CallbackObjectHolder(const CallbackObjectHolder& aOther) = delete;
453 CallbackObjectHolder() : mPtrBits(0) {}
455 ~CallbackObjectHolder() { UnlinkSelf(); }
457 void operator=(WebIDLCallbackT* aCallback) {
458 UnlinkSelf();
459 mPtrBits = reinterpret_cast<uintptr_t>(aCallback);
460 NS_IF_ADDREF(aCallback);
463 void operator=(XPCOMCallbackT* aCallback) {
464 UnlinkSelf();
465 mPtrBits = reinterpret_cast<uintptr_t>(aCallback) | XPCOMCallbackFlag;
466 NS_IF_ADDREF(aCallback);
469 void operator=(CallbackObjectHolder&& aOther) {
470 UnlinkSelf();
471 mPtrBits = aOther.mPtrBits;
472 aOther.mPtrBits = 0;
475 void operator=(const CallbackObjectHolder& aOther) = delete;
477 void Reset() { UnlinkSelf(); }
479 nsISupports* GetISupports() const {
480 return reinterpret_cast<nsISupports*>(mPtrBits & ~XPCOMCallbackFlag);
483 already_AddRefed<nsISupports> Forget() {
484 // This can be called from random threads. Make sure to not refcount things
485 // in here!
486 nsISupports* supp = GetISupports();
487 mPtrBits = 0;
488 return dont_AddRef(supp);
491 // Boolean conversion operator so people can use this in boolean tests
492 explicit operator bool() const { return GetISupports(); }
494 CallbackObjectHolder Clone() const {
495 CallbackObjectHolder result;
496 result.mPtrBits = mPtrBits;
497 NS_IF_ADDREF(GetISupports());
498 return result;
501 // Even if HasWebIDLCallback returns true, GetWebIDLCallback() might still
502 // return null.
503 bool HasWebIDLCallback() const { return !(mPtrBits & XPCOMCallbackFlag); }
505 WebIDLCallbackT* GetWebIDLCallback() const {
506 MOZ_ASSERT(HasWebIDLCallback());
507 return reinterpret_cast<WebIDLCallbackT*>(mPtrBits);
510 XPCOMCallbackT* GetXPCOMCallback() const {
511 MOZ_ASSERT(!HasWebIDLCallback());
512 return reinterpret_cast<XPCOMCallbackT*>(mPtrBits & ~XPCOMCallbackFlag);
515 bool operator==(WebIDLCallbackT* aOtherCallback) const {
516 if (!aOtherCallback) {
517 // If other is null, then we must be null to be equal.
518 return !GetISupports();
521 if (!HasWebIDLCallback() || !GetWebIDLCallback()) {
522 // If other is non-null, then we can't be equal if we have a
523 // non-WebIDL callback or a null callback.
524 return false;
527 return *GetWebIDLCallback() == *aOtherCallback;
530 bool operator==(XPCOMCallbackT* aOtherCallback) const {
531 return (!aOtherCallback && !GetISupports()) ||
532 (!HasWebIDLCallback() && GetXPCOMCallback() == aOtherCallback);
535 bool operator==(const CallbackObjectHolder& aOtherCallback) const {
536 if (aOtherCallback.HasWebIDLCallback()) {
537 return *this == aOtherCallback.GetWebIDLCallback();
540 return *this == aOtherCallback.GetXPCOMCallback();
543 // Try to return an XPCOMCallbackT version of this object.
544 already_AddRefed<XPCOMCallbackT> ToXPCOMCallback() const {
545 if (!HasWebIDLCallback()) {
546 RefPtr<XPCOMCallbackT> callback = GetXPCOMCallback();
547 return callback.forget();
550 nsCOMPtr<nsISupports> supp = CallbackObjectHolderBase::ToXPCOMCallback(
551 GetWebIDLCallback(), NS_GET_TEMPLATE_IID(XPCOMCallbackT));
552 if (supp) {
553 // ToXPCOMCallback already did the right QI for us.
554 return supp.forget().downcast<XPCOMCallbackT>();
556 return nullptr;
559 // Try to return a WebIDLCallbackT version of this object.
560 already_AddRefed<WebIDLCallbackT> ToWebIDLCallback() const {
561 if (HasWebIDLCallback()) {
562 RefPtr<WebIDLCallbackT> callback = GetWebIDLCallback();
563 return callback.forget();
565 return nullptr;
568 private:
569 static const uintptr_t XPCOMCallbackFlag = 1u;
571 friend void ImplCycleCollectionUnlink<WebIDLCallbackT, XPCOMCallbackT>(
572 CallbackObjectHolder& aField);
574 void UnlinkSelf() {
575 // NS_IF_RELEASE because we might have been unlinked before
576 nsISupports* ptr = GetISupports();
577 // Clear mPtrBits before the release to prevent reentrance.
578 mPtrBits = 0;
579 NS_IF_RELEASE(ptr);
582 uintptr_t mPtrBits;
585 NS_DEFINE_STATIC_IID_ACCESSOR(CallbackObject, DOM_CALLBACKOBJECT_IID)
587 template <class T, class U>
588 inline void ImplCycleCollectionTraverse(
589 nsCycleCollectionTraversalCallback& aCallback,
590 CallbackObjectHolder<T, U>& aField, const char* aName,
591 uint32_t aFlags = 0) {
592 if (aField) {
593 CycleCollectionNoteChild(aCallback, aField.GetISupports(), aName, aFlags);
597 template <class T, class U>
598 void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField) {
599 aField.UnlinkSelf();
602 // T is expected to be a RefPtr or OwningNonNull around a CallbackObject
603 // subclass. This class is used in bindings to safely handle Fast* callbacks;
604 // it ensures that the callback is traced, and that if something is holding onto
605 // the callback when we're done with it HoldJSObjects is called.
607 // Since we effectively hold a ref to a refcounted thing (like RefPtr or
608 // OwningNonNull), we are also MOZ_IS_SMARTPTR_TO_REFCOUNTED for static analysis
609 // purposes.
610 template <typename T>
611 class MOZ_RAII MOZ_IS_SMARTPTR_TO_REFCOUNTED RootedCallback
612 : public JS::Rooted<T> {
613 public:
614 explicit RootedCallback(JSContext* cx) : JS::Rooted<T>(cx), mCx(cx) {}
616 // We need a way to make assignment from pointers (how we're normally used)
617 // work.
618 template <typename S>
619 void operator=(S* arg) {
620 this->get().operator=(arg);
623 // But nullptr can't use the above template, because it doesn't know which S
624 // to select. So we need a special overload for nullptr.
625 void operator=(decltype(nullptr) arg) { this->get().operator=(arg); }
627 // Codegen relies on being able to do CallbackOrNull() and Callback() on us.
628 JSObject* CallbackOrNull() const { return this->get()->CallbackOrNull(); }
630 JSObject* Callback(JSContext* aCx) const {
631 return this->get()->Callback(aCx);
634 ~RootedCallback() {
635 // Ensure that our callback starts holding on to its own JS objects as
636 // needed. We really do need to check that things are initialized even when
637 // T is OwningNonNull, because we might be running before the OwningNonNull
638 // ever got assigned to!
639 if (IsInitialized(this->get())) {
640 this->get()->FinishSlowJSInitIfMoreThanOneOwner(mCx);
644 private:
645 template <typename U>
646 static bool IsInitialized(U& aArg); // Not implemented
648 template <typename U>
649 static bool IsInitialized(RefPtr<U>& aRefPtr) {
650 return aRefPtr;
653 template <typename U>
654 static bool IsInitialized(OwningNonNull<U>& aOwningNonNull) {
655 return aOwningNonNull.isInitialized();
658 JSContext* mCx;
661 } // namespace dom
662 } // namespace mozilla
664 #endif // mozilla_dom_CallbackObject_h