2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-01-28 17:34:29 +04:00
|
|
|
/* 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"
|
2018-02-21 19:30:19 +03:00
|
|
|
#include "js/Wrapper.h"
|
2013-01-28 17:34:29 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-03-17 11:55:15 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-09-10 19:29:43 +04:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2014-01-04 22:15:41 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-05-18 19:23:35 +03:00
|
|
|
#include "mozilla/OwningNonNull.h"
|
2013-12-12 05:51:57 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-01-28 17:34:29 +04:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "xpcpublic.h"
|
2015-07-24 14:01:00 +03:00
|
|
|
#include "jsapi.h"
|
2019-01-24 03:56:56 +03:00
|
|
|
#include "js/ContextOptions.h"
|
2016-05-18 19:23:35 +03:00
|
|
|
#include "js/TracingAPI.h"
|
2013-01-28 17:34:29 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2018-09-20 15:28:59 +03:00
|
|
|
|
|
|
|
class PromiseJobRunnable;
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2013-04-17 01:14:30 +04:00
|
|
|
#define DOM_CALLBACKOBJECT_IID \
|
2018-11-30 13:46:48 +03:00
|
|
|
{ \
|
2013-04-17 01:14:30 +04:00
|
|
|
0xbe74c190, 0x6d76, 0x4991, { \
|
|
|
|
0x84, 0xb9, 0x65, 0x06, 0x99, 0xe6, 0x93, 0x2b \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
class CallbackObject : public nsISupports {
|
|
|
|
public:
|
2013-04-17 01:14:30 +04:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(DOM_CALLBACKOBJECT_IID)
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2016-11-15 06:49:28 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(CallbackObject)
|
2013-01-28 17:34:29 +04:00
|
|
|
|
2013-12-12 05:51:58 +04:00
|
|
|
// The caller may pass a global object which will act as an override for the
|
|
|
|
// incumbent script settings object when the callback is invoked (overriding
|
|
|
|
// the entry point computed from aCallback). If no override is required, the
|
2015-07-24 14:00:00 +03:00
|
|
|
// caller should pass null. |aCx| is used to capture the current
|
|
|
|
// stack, which is later used as an async parent when the callback
|
|
|
|
// is invoked. aCx can be nullptr, in which case no stack is
|
|
|
|
// captured.
|
|
|
|
explicit CallbackObject(JSContext* aCx, JS::Handle<JSObject*> aCallback,
|
2018-08-04 00:11:39 +03:00
|
|
|
JS::Handle<JSObject*> aCallbackGlobal,
|
2016-05-18 19:23:35 +03:00
|
|
|
nsIGlobalObject* aIncumbentGlobal) {
|
2016-07-07 09:15:15 +03:00
|
|
|
if (aCx && JS::ContextOptionsRef(aCx).asyncStack()) {
|
2015-07-24 14:01:00 +03:00
|
|
|
JS::RootedObject stack(aCx);
|
|
|
|
if (!JS::CaptureCurrentStack(aCx, &stack)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
2018-08-04 00:11:39 +03:00
|
|
|
Init(aCallback, aCallbackGlobal, stack, aIncumbentGlobal);
|
2015-07-24 14:01:00 +03:00
|
|
|
} else {
|
2018-08-04 00:11:39 +03:00
|
|
|
Init(aCallback, aCallbackGlobal, nullptr, aIncumbentGlobal);
|
2015-07-24 14:01:00 +03:00
|
|
|
}
|
2013-04-04 06:22:15 +04:00
|
|
|
}
|
|
|
|
|
2016-03-17 20:07:43 +03:00
|
|
|
// Instead of capturing the current stack to use as an async parent when the
|
|
|
|
// callback is invoked, the caller can use this overload to pass in a stack
|
|
|
|
// for that purpose.
|
2018-08-04 00:11:39 +03:00
|
|
|
explicit CallbackObject(JSObject* aCallback, JSObject* aCallbackGlobal,
|
|
|
|
JSObject* aAsyncStack,
|
2016-05-18 19:23:35 +03:00
|
|
|
nsIGlobalObject* aIncumbentGlobal) {
|
2018-08-04 00:11:39 +03:00
|
|
|
Init(aCallback, aCallbackGlobal, aAsyncStack, aIncumbentGlobal);
|
2016-03-17 20:07:43 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:25:37 +03:00
|
|
|
// This is guaranteed to be non-null from the time the CallbackObject is
|
|
|
|
// created until JavaScript has had a chance to run. It will only return null
|
2018-09-20 15:28:59 +03:00
|
|
|
// after a JavaScript caller has called nukeSandbox on a Sandbox object and
|
|
|
|
// the cycle collector has had a chance to run, unless Reset() is explicitly
|
|
|
|
// called (see below).
|
2016-11-15 08:25:37 +03:00
|
|
|
//
|
|
|
|
// This means that any native callee which receives a CallbackObject as an
|
|
|
|
// argument can safely rely on the callback being non-null so long as it
|
|
|
|
// doesn't trigger any scripts before it accesses it.
|
|
|
|
JS::Handle<JSObject*> CallbackOrNull() const {
|
2016-10-18 19:58:19 +03:00
|
|
|
mCallback.exposeToActiveJS();
|
2013-04-25 20:29:52 +04:00
|
|
|
return CallbackPreserveColor();
|
2013-01-28 17:34:29 +04:00
|
|
|
}
|
|
|
|
|
2018-08-04 00:11:39 +03:00
|
|
|
JSObject* CallbackGlobalOrNull() const {
|
|
|
|
mCallbackGlobal.exposeToActiveJS();
|
|
|
|
return mCallbackGlobal;
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:21:06 +03:00
|
|
|
// Like CallbackOrNull(), but will return a new dead proxy object in the
|
2018-05-31 12:28:48 +03:00
|
|
|
// caller's realm if the callback is null.
|
2017-09-05 03:21:06 +03:00
|
|
|
JSObject* Callback(JSContext* aCx);
|
|
|
|
|
2015-07-24 14:01:00 +03:00
|
|
|
JSObject* GetCreationStack() const { return mCreationStack; }
|
|
|
|
|
2015-08-20 18:36:06 +03:00
|
|
|
void MarkForCC() {
|
2016-10-18 19:58:19 +03:00
|
|
|
mCallback.exposeToActiveJS();
|
2018-08-04 00:11:39 +03:00
|
|
|
mCallbackGlobal.exposeToActiveJS();
|
2016-10-18 19:58:19 +03:00
|
|
|
mCreationStack.exposeToActiveJS();
|
2015-08-20 18:36:06 +03:00
|
|
|
}
|
|
|
|
|
2013-02-27 00:10:15 +04:00
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
2013-04-25 20:29:52 +04:00
|
|
|
JS::Handle<JSObject*> CallbackPreserveColor() const {
|
2013-07-23 13:58:28 +04:00
|
|
|
// Calling fromMarkedLocation() is safe because we trace our mCallback, and
|
|
|
|
// because the value of mCallback cannot change after if has been set.
|
2013-07-23 13:58:27 +04:00
|
|
|
return JS::Handle<JSObject*>::fromMarkedLocation(mCallback.address());
|
2013-02-27 00:10:15 +04:00
|
|
|
}
|
2018-12-12 11:02:30 +03:00
|
|
|
JS::Handle<JSObject*> CallbackGlobalPreserveColor() const {
|
|
|
|
// The comment in CallbackPreserveColor applies here as well.
|
|
|
|
return JS::Handle<JSObject*>::fromMarkedLocation(mCallbackGlobal.address());
|
|
|
|
}
|
2013-02-27 00:10:15 +04:00
|
|
|
|
2016-07-13 14:28:00 +03:00
|
|
|
/*
|
|
|
|
* If the callback is known to be non-gray, then this method can be
|
2016-11-15 08:25:37 +03:00
|
|
|
* used instead of CallbackOrNull() to avoid the overhead of
|
2016-07-13 14:28:00 +03:00
|
|
|
* ExposeObjectToActiveJS().
|
|
|
|
*/
|
|
|
|
JS::Handle<JSObject*> CallbackKnownNotGray() const {
|
2018-12-07 00:28:10 +03:00
|
|
|
JS::AssertObjectIsNotGray(mCallback);
|
2016-07-13 14:28:00 +03:00
|
|
|
return CallbackPreserveColor();
|
|
|
|
}
|
|
|
|
|
2013-12-12 05:51:58 +04:00
|
|
|
nsIGlobalObject* IncumbentGlobalOrNull() const { return mIncumbentGlobal; }
|
|
|
|
|
2013-02-19 20:54:40 +04:00
|
|
|
enum ExceptionHandling {
|
2013-09-03 16:01:53 +04:00
|
|
|
// Report any exception and don't throw it to the caller code.
|
2013-02-19 20:54:40 +04:00
|
|
|
eReportExceptions,
|
2013-09-03 16:01:53 +04:00
|
|
|
// Throw an exception to the caller code if the thrown exception is a
|
2017-08-06 20:47:00 +03:00
|
|
|
// binding object for a DOMException from the caller's scope, otherwise
|
|
|
|
// report it.
|
2013-09-03 16:01:53 +04:00
|
|
|
eRethrowContentExceptions,
|
2018-05-31 12:28:48 +03:00
|
|
|
// Throw exceptions to the caller code, unless the caller realm is
|
2017-08-06 20:47:00 +03:00
|
|
|
// provided, the exception is not a DOMException from the caller
|
2018-05-31 12:28:48 +03:00
|
|
|
// realm, and the caller realm does not subsume our unwrapped callback.
|
2013-02-19 20:54:40 +04:00
|
|
|
eRethrowExceptions
|
|
|
|
};
|
|
|
|
|
2014-01-04 22:15:41 +04:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this);
|
|
|
|
}
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
protected:
|
2017-06-17 00:15:32 +03:00
|
|
|
virtual ~CallbackObject() { mozilla::DropJSObjects(this); }
|
2014-06-23 22:49:08 +04:00
|
|
|
|
2013-04-04 06:22:15 +04:00
|
|
|
explicit CallbackObject(CallbackObject* aCallbackObject) {
|
2018-08-04 00:11:39 +03:00
|
|
|
Init(aCallbackObject->mCallback, aCallbackObject->mCallbackGlobal,
|
2015-07-24 14:01:00 +03:00
|
|
|
aCallbackObject->mCreationStack, aCallbackObject->mIncumbentGlobal);
|
2013-04-04 06:22:15 +04:00
|
|
|
}
|
|
|
|
|
2013-12-16 22:06:35 +04:00
|
|
|
bool operator==(const CallbackObject& aOther) const {
|
2016-11-15 08:25:37 +03:00
|
|
|
JSObject* wrappedThis = CallbackPreserveColor();
|
|
|
|
JSObject* wrappedOther = aOther.CallbackPreserveColor();
|
|
|
|
if (!wrappedThis || !wrappedOther) {
|
|
|
|
return this == &aOther;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* thisObj = js::UncheckedUnwrap(wrappedThis);
|
|
|
|
JSObject* otherObj = js::UncheckedUnwrap(wrappedOther);
|
2013-12-16 22:06:35 +04:00
|
|
|
return thisObj == otherObj;
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:49:28 +03:00
|
|
|
class JSObjectsDropper final {
|
|
|
|
public:
|
|
|
|
explicit JSObjectsDropper(CallbackObject* aHolder) : mHolder(aHolder) {}
|
|
|
|
|
2017-06-17 00:15:32 +03:00
|
|
|
~JSObjectsDropper() { mHolder->ClearJSObjects(); }
|
2016-11-15 06:49:28 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<CallbackObject> mHolder;
|
|
|
|
};
|
|
|
|
|
2013-04-04 06:22:15 +04:00
|
|
|
private:
|
2018-08-04 00:11:39 +03:00
|
|
|
inline void InitNoHold(JSObject* aCallback, JSObject* aCallbackGlobal,
|
|
|
|
JSObject* aCreationStack,
|
2016-05-18 19:23:35 +03:00
|
|
|
nsIGlobalObject* aIncumbentGlobal) {
|
2013-07-23 13:58:28 +04:00
|
|
|
MOZ_ASSERT(aCallback && !mCallback);
|
2018-08-04 00:11:39 +03:00
|
|
|
MOZ_ASSERT(aCallbackGlobal);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(js::GetObjectCompartment(aCallback) ==
|
|
|
|
js::GetObjectCompartment(aCallbackGlobal));
|
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(aCallbackGlobal));
|
2013-04-04 06:22:15 +04:00
|
|
|
mCallback = aCallback;
|
2018-08-04 00:11:39 +03:00
|
|
|
mCallbackGlobal = aCallbackGlobal;
|
2015-07-24 14:01:00 +03:00
|
|
|
mCreationStack = aCreationStack;
|
2014-03-03 20:53:42 +04:00
|
|
|
if (aIncumbentGlobal) {
|
|
|
|
mIncumbentGlobal = aIncumbentGlobal;
|
|
|
|
mIncumbentJSGlobal = aIncumbentGlobal->GetGlobalJSObject();
|
|
|
|
}
|
2016-05-18 19:23:35 +03:00
|
|
|
}
|
|
|
|
|
2018-08-04 00:11:39 +03:00
|
|
|
inline void Init(JSObject* aCallback, JSObject* aCallbackGlobal,
|
|
|
|
JSObject* aCreationStack,
|
2016-05-18 19:23:35 +03:00
|
|
|
nsIGlobalObject* aIncumbentGlobal) {
|
2018-08-04 00:11:39 +03:00
|
|
|
// Set script objects before we hold, on the off chance that a GC could
|
|
|
|
// somehow happen in there... (which would be pretty odd, granted).
|
|
|
|
InitNoHold(aCallback, aCallbackGlobal, aCreationStack, aIncumbentGlobal);
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2013-01-28 17:34:29 +04:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:28:59 +03:00
|
|
|
// Provide a way to clear this object's pointers to GC things after the
|
|
|
|
// callback has been run. Note that CallbackOrNull() will return null after
|
|
|
|
// this point. This should only be called if the object is known not to be
|
|
|
|
// used again.
|
|
|
|
void Reset() { ClearJSReferences(); }
|
|
|
|
friend class mozilla::PromiseJobRunnable;
|
|
|
|
|
2016-05-18 19:23:35 +03:00
|
|
|
inline void ClearJSReferences() {
|
|
|
|
mCallback = nullptr;
|
2018-08-04 00:11:39 +03:00
|
|
|
mCallbackGlobal = nullptr;
|
2016-05-18 19:23:35 +03:00
|
|
|
mCreationStack = nullptr;
|
|
|
|
mIncumbentJSGlobal = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
CallbackObject(const CallbackObject&) = delete;
|
|
|
|
CallbackObject& operator=(const CallbackObject&) = delete;
|
2013-09-12 02:35:51 +04:00
|
|
|
|
2013-04-04 06:22:15 +04:00
|
|
|
protected:
|
2017-06-17 00:15:32 +03:00
|
|
|
void ClearJSObjects() {
|
2014-03-03 20:53:42 +04:00
|
|
|
MOZ_ASSERT_IF(mIncumbentJSGlobal, mCallback);
|
2013-01-28 17:34:29 +04:00
|
|
|
if (mCallback) {
|
2016-05-18 19:23:35 +03:00
|
|
|
ClearJSReferences();
|
2013-01-28 17:34:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:23:35 +03:00
|
|
|
// For use from subclasses that want to be usable with Rooted.
|
|
|
|
void Trace(JSTracer* aTracer);
|
|
|
|
|
|
|
|
// For use from subclasses that want to be traced for a bit then possibly
|
2016-12-30 00:19:26 +03:00
|
|
|
// switch to HoldJSObjects and do other slow JS-related init work we might do.
|
|
|
|
// If we have more than one owner, this will HoldJSObjects and do said slow
|
|
|
|
// init work; otherwise it will just forget all our JS references.
|
|
|
|
void FinishSlowJSInitIfMoreThanOneOwner(JSContext* aCx);
|
2016-05-18 19:23:35 +03:00
|
|
|
|
2016-05-18 19:23:35 +03:00
|
|
|
// Struct used as a way to force a CallbackObject constructor to not call
|
|
|
|
// HoldJSObjects. We're putting it here so that CallbackObject subclasses will
|
|
|
|
// have access to it, but outside code will not.
|
|
|
|
//
|
|
|
|
// Places that use this need to ensure that the callback is traced (e.g. via a
|
|
|
|
// Rooted) until the HoldJSObjects call happens.
|
|
|
|
struct FastCallbackConstructor {};
|
|
|
|
|
|
|
|
// Just like the public version without the FastCallbackConstructor argument,
|
2016-12-30 00:19:26 +03:00
|
|
|
// except for not calling HoldJSObjects and not capturing async stacks (on the
|
|
|
|
// assumption that we will do that last whenever we decide to actually
|
|
|
|
// HoldJSObjects; see FinishSlowJSInitIfMoreThanOneOwner). If you use this,
|
|
|
|
// you MUST ensure that the object is traced until the HoldJSObjects happens!
|
2018-08-04 00:11:39 +03:00
|
|
|
CallbackObject(JSObject* aCallback, JSObject* aCallbackGlobal,
|
2016-05-18 19:23:35 +03:00
|
|
|
const FastCallbackConstructor&) {
|
2018-08-04 00:11:39 +03:00
|
|
|
InitNoHold(aCallback, aCallbackGlobal, nullptr, nullptr);
|
2016-05-18 19:23:35 +03:00
|
|
|
}
|
|
|
|
|
2016-04-07 10:11:52 +03:00
|
|
|
// mCallback is not unwrapped, so it can be a cross-compartment-wrapper.
|
|
|
|
// This is done to ensure that, if JS code can't call a callback f(), or get
|
|
|
|
// its members, directly itself, this code won't call f(), or get its members,
|
|
|
|
// on the code's behalf.
|
2013-06-18 14:00:38 +04:00
|
|
|
JS::Heap<JSObject*> mCallback;
|
2018-08-04 00:11:39 +03:00
|
|
|
// mCallbackGlobal is the global that we were in when we created the
|
|
|
|
// callback. In particular, it is guaranteed to be same-compartment with
|
|
|
|
// aCallback. We store it separately, because we have no way to recover the
|
|
|
|
// global if mCallback is a cross-compartment wrapper.
|
|
|
|
JS::Heap<JSObject*> mCallbackGlobal;
|
2015-07-24 14:01:00 +03:00
|
|
|
JS::Heap<JSObject*> mCreationStack;
|
2014-03-03 20:53:42 +04:00
|
|
|
// Ideally, we'd just hold a reference to the nsIGlobalObject, since that's
|
|
|
|
// what we need to pass to AutoIncumbentScript. Unfortunately, that doesn't
|
|
|
|
// hold the actual JS global alive. So we maintain an additional pointer to
|
|
|
|
// the JS global itself so that we can trace it.
|
|
|
|
//
|
|
|
|
// At some point we should consider trying to make native globals hold their
|
|
|
|
// scripted global alive, at which point we can get rid of the duplication
|
|
|
|
// here.
|
2013-12-12 05:51:58 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> mIncumbentGlobal;
|
2014-03-03 20:53:42 +04:00
|
|
|
JS::TenuredHeap<JSObject*> mIncumbentJSGlobal;
|
2013-01-28 17:34:29 +04:00
|
|
|
|
2013-04-12 07:20:18 +04:00
|
|
|
class MOZ_STACK_CLASS CallSetup {
|
2013-01-28 17:34:29 +04:00
|
|
|
/**
|
|
|
|
* 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:
|
2018-05-31 12:28:48 +03:00
|
|
|
// If aExceptionHandling == eRethrowContentExceptions then aRealm
|
|
|
|
// needs to be set to the realm in which exceptions will be rethrown.
|
2015-01-16 01:39:01 +03:00
|
|
|
//
|
2018-05-31 12:28:48 +03:00
|
|
|
// If aExceptionHandling == eRethrowExceptions then aRealm may be set
|
|
|
|
// to the realm in which exceptions will be rethrown. In that case
|
|
|
|
// they will only be rethrown if that realm's principal subsumes the
|
2015-01-16 01:39:01 +03:00
|
|
|
// principal of our (unwrapped) callback.
|
2013-12-12 05:51:57 +04:00
|
|
|
CallSetup(CallbackObject* aCallback, ErrorResult& aRv,
|
2015-04-09 04:23:48 +03:00
|
|
|
const char* aExecutionReason,
|
2013-09-03 16:01:53 +04:00
|
|
|
ExceptionHandling aExceptionHandling, JS::Realm* aRealm = nullptr,
|
2014-02-15 10:36:44 +04:00
|
|
|
bool aIsJSImplementedWebIDL = false);
|
2019-03-19 18:14:11 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT ~CallSetup();
|
2013-01-28 17:34:29 +04:00
|
|
|
|
|
|
|
JSContext* GetContext() const { return mCx; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// We better not get copy-constructed
|
2015-01-07 02:35:02 +03:00
|
|
|
CallSetup(const CallSetup&) = delete;
|
2013-01-28 17:34:29 +04:00
|
|
|
|
2013-09-03 16:01:53 +04:00
|
|
|
bool ShouldRethrowException(JS::Handle<JS::Value> aException);
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
// Members which can go away whenever
|
|
|
|
JSContext* mCx;
|
|
|
|
|
2018-05-31 12:28:48 +03:00
|
|
|
// Caller's realm. This will only have a sensible value if
|
2015-01-16 01:39:01 +03:00
|
|
|
// mExceptionHandling == eRethrowContentExceptions or eRethrowExceptions.
|
2018-05-31 12:28:48 +03:00
|
|
|
JS::Realm* mRealm;
|
2013-09-03 16:01:53 +04:00
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
// And now members whose construction/destruction order we need to control.
|
2013-12-12 05:51:57 +04:00
|
|
|
Maybe<AutoEntryScript> mAutoEntryScript;
|
2013-12-12 05:51:58 +04:00
|
|
|
Maybe<AutoIncumbentScript> mAutoIncumbentScript;
|
2013-01-28 17:34:29 +04:00
|
|
|
|
2018-08-04 00:11:39 +03:00
|
|
|
Maybe<JS::Rooted<JSObject*>> mRootedCallable;
|
|
|
|
// The global of mRootedCallable.
|
|
|
|
Maybe<JS::Rooted<JSObject*>> mRootedCallableGlobal;
|
2013-06-20 22:05:34 +04:00
|
|
|
|
2015-07-24 14:01:00 +03:00
|
|
|
// Members which are used to set the async stack.
|
|
|
|
Maybe<JS::Rooted<JSObject*>> mAsyncStack;
|
|
|
|
Maybe<JS::AutoSetAsyncStackForNewCalls> mAsyncStackSetter;
|
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
// Can't construct a JSAutoRealm without a JSContext either. Also,
|
|
|
|
// Put mAr after mAutoEntryScript so that we exit the realm before we
|
|
|
|
// pop the script settings stack. Though in practice we'll often manually
|
|
|
|
// order those two things.
|
2018-08-07 15:06:44 +03:00
|
|
|
Maybe<JSAutoRealm> mAr;
|
2013-02-19 20:54:40 +04:00
|
|
|
|
|
|
|
// An ErrorResult to possibly re-throw exceptions on and whether
|
|
|
|
// we should re-throw them.
|
|
|
|
ErrorResult& mErrorResult;
|
|
|
|
const ExceptionHandling mExceptionHandling;
|
2013-09-10 01:58:29 +04:00
|
|
|
const bool mIsMainThread;
|
2013-01-28 17:34:29 +04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-02-27 00:10:15 +04:00
|
|
|
template <class WebIDLCallbackT, class XPCOMCallbackT>
|
|
|
|
class CallbackObjectHolder;
|
|
|
|
|
|
|
|
template <class T, class U>
|
2013-02-27 00:10:15 +04:00
|
|
|
void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField);
|
2013-02-27 00:10:15 +04:00
|
|
|
|
|
|
|
class CallbackObjectHolderBase {
|
|
|
|
protected:
|
|
|
|
// Returns null on all failures
|
|
|
|
already_AddRefed<nsISupports> ToXPCOMCallback(CallbackObject* aCallback,
|
2013-04-17 01:15:57 +04:00
|
|
|
const nsIID& aIID) const;
|
2013-02-27 00:10:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
CallbackObjectHolder(CallbackObjectHolder&& aOther)
|
2013-02-27 00:10:15 +04:00
|
|
|
: mPtrBits(aOther.mPtrBits) {
|
2016-07-28 05:00:06 +03:00
|
|
|
aOther.mPtrBits = 0;
|
|
|
|
static_assert(sizeof(CallbackObjectHolder) == sizeof(void*),
|
|
|
|
"This object is expected to be as small as a pointer, and it "
|
|
|
|
"is currently passed by value in various places. If it is "
|
|
|
|
"bloating, we may want to pass it by reference then.");
|
2013-02-27 00:10:15 +04:00
|
|
|
}
|
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
CallbackObjectHolder(const CallbackObjectHolder& aOther) = delete;
|
|
|
|
|
2013-02-27 00:10:15 +04:00
|
|
|
CallbackObjectHolder() : mPtrBits(0) {}
|
|
|
|
|
|
|
|
~CallbackObjectHolder() { UnlinkSelf(); }
|
|
|
|
|
2013-04-17 01:15:57 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
void operator=(CallbackObjectHolder&& aOther) {
|
2013-04-17 01:15:57 +04:00
|
|
|
UnlinkSelf();
|
|
|
|
mPtrBits = aOther.mPtrBits;
|
2016-07-28 05:00:06 +03:00
|
|
|
aOther.mPtrBits = 0;
|
2013-04-17 01:15:57 +04:00
|
|
|
}
|
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
void operator=(const CallbackObjectHolder& aOther) = delete;
|
|
|
|
|
2018-07-28 07:40:26 +03:00
|
|
|
void Reset() { UnlinkSelf(); }
|
|
|
|
|
2013-02-27 00:10:15 +04:00
|
|
|
nsISupports* GetISupports() const {
|
|
|
|
return reinterpret_cast<nsISupports*>(mPtrBits & ~XPCOMCallbackFlag);
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:40:26 +03:00
|
|
|
already_AddRefed<nsISupports> Forget() {
|
|
|
|
// This can be called from random threads. Make sure to not refcount things
|
|
|
|
// in here!
|
|
|
|
nsISupports* supp = GetISupports();
|
|
|
|
mPtrBits = 0;
|
|
|
|
return dont_AddRef(supp);
|
|
|
|
}
|
|
|
|
|
2013-04-17 01:15:57 +04:00
|
|
|
// Boolean conversion operator so people can use this in boolean tests
|
2015-04-11 06:05:46 +03:00
|
|
|
explicit operator bool() const { return GetISupports(); }
|
2013-04-17 01:15:57 +04:00
|
|
|
|
2016-07-28 05:00:06 +03:00
|
|
|
CallbackObjectHolder Clone() const {
|
|
|
|
CallbackObjectHolder result;
|
|
|
|
result.mPtrBits = mPtrBits;
|
|
|
|
NS_IF_ADDREF(GetISupports());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-02-27 00:10:15 +04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2013-12-16 22:06:35 +04:00
|
|
|
return *GetWebIDLCallback() == *aOtherCallback;
|
2013-02-27 00:10:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2013-04-17 01:15:57 +04:00
|
|
|
already_AddRefed<XPCOMCallbackT> ToXPCOMCallback() const {
|
2013-02-27 00:10:15 +04:00
|
|
|
if (!HasWebIDLCallback()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<XPCOMCallbackT> callback = GetXPCOMCallback();
|
2013-02-27 00:10:15 +04:00
|
|
|
return callback.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> supp = CallbackObjectHolderBase::ToXPCOMCallback(
|
|
|
|
GetWebIDLCallback(), NS_GET_TEMPLATE_IID(XPCOMCallbackT));
|
2017-02-06 22:34:56 +03:00
|
|
|
if (supp) {
|
|
|
|
// ToXPCOMCallback already did the right QI for us.
|
|
|
|
return supp.forget().downcast<XPCOMCallbackT>();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2013-02-27 00:10:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to return a WebIDLCallbackT version of this object.
|
2013-04-17 01:15:57 +04:00
|
|
|
already_AddRefed<WebIDLCallbackT> ToWebIDLCallback() const {
|
2013-02-27 00:10:15 +04:00
|
|
|
if (HasWebIDLCallback()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WebIDLCallbackT> callback = GetWebIDLCallback();
|
2013-02-27 00:10:15 +04:00
|
|
|
return callback.forget();
|
|
|
|
}
|
2013-12-12 05:51:58 +04:00
|
|
|
return nullptr;
|
2013-02-27 00:10:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2013-04-17 01:14:30 +04:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(CallbackObject, DOM_CALLBACKOBJECT_IID)
|
|
|
|
|
2013-02-27 00:10:15 +04:00
|
|
|
template <class T, class U>
|
|
|
|
inline void ImplCycleCollectionTraverse(
|
|
|
|
nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
CallbackObjectHolder<T, U>& aField, const char* aName,
|
|
|
|
uint32_t aFlags = 0) {
|
2016-11-15 06:49:28 +03:00
|
|
|
if (aField) {
|
|
|
|
CycleCollectionNoteChild(aCallback, aField.GetISupports(), aName, aFlags);
|
|
|
|
}
|
2013-02-27 00:10:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField) {
|
|
|
|
aField.UnlinkSelf();
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:23:35 +03:00
|
|
|
// T is expected to be a RefPtr or OwningNonNull around a CallbackObject
|
|
|
|
// subclass. This class is used in bindings to safely handle Fast* callbacks;
|
|
|
|
// it ensures that the callback is traced, and that if something is holding onto
|
|
|
|
// the callback when we're done with it HoldJSObjects is called.
|
2019-03-19 15:59:53 +03:00
|
|
|
//
|
|
|
|
// Since we effectively hold a ref to a refcounted thing (like RefPtr or
|
|
|
|
// OwningNonNull), we are also MOZ_IS_SMARTPTR_TO_REFCOUNTED for static analysis
|
|
|
|
// purposes.
|
2016-05-18 19:23:35 +03:00
|
|
|
template <typename T>
|
2019-03-19 15:59:53 +03:00
|
|
|
class MOZ_RAII MOZ_IS_SMARTPTR_TO_REFCOUNTED RootedCallback
|
|
|
|
: public JS::Rooted<T> {
|
2016-05-18 19:23:35 +03:00
|
|
|
public:
|
|
|
|
explicit RootedCallback(JSContext* cx) : JS::Rooted<T>(cx), mCx(cx) {}
|
|
|
|
|
|
|
|
// We need a way to make assignment from pointers (how we're normally used)
|
|
|
|
// work.
|
|
|
|
template <typename S>
|
|
|
|
void operator=(S* arg) {
|
|
|
|
this->get().operator=(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// But nullptr can't use the above template, because it doesn't know which S
|
|
|
|
// to select. So we need a special overload for nullptr.
|
|
|
|
void operator=(decltype(nullptr) arg) { this->get().operator=(arg); }
|
|
|
|
|
2017-09-05 03:21:06 +03:00
|
|
|
// Codegen relies on being able to do CallbackOrNull() and Callback() on us.
|
2016-11-15 08:25:37 +03:00
|
|
|
JS::Handle<JSObject*> CallbackOrNull() const {
|
|
|
|
return this->get()->CallbackOrNull();
|
2016-05-18 19:23:35 +03:00
|
|
|
}
|
|
|
|
|
2017-09-05 03:21:06 +03:00
|
|
|
JSObject* Callback(JSContext* aCx) const {
|
|
|
|
return this->get()->Callback(aCx);
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:23:35 +03:00
|
|
|
~RootedCallback() {
|
|
|
|
// Ensure that our callback starts holding on to its own JS objects as
|
2016-12-30 00:19:26 +03:00
|
|
|
// needed. We really do need to check that things are initialized even when
|
|
|
|
// T is OwningNonNull, because we might be running before the OwningNonNull
|
|
|
|
// ever got assigned to!
|
2016-05-18 19:23:35 +03:00
|
|
|
if (IsInitialized(this->get())) {
|
2016-12-30 00:19:26 +03:00
|
|
|
this->get()->FinishSlowJSInitIfMoreThanOneOwner(mCx);
|
2016-05-18 19:23:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename U>
|
|
|
|
static bool IsInitialized(U& aArg); // Not implemented
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
static bool IsInitialized(RefPtr<U>& aRefPtr) {
|
|
|
|
return aRefPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
static bool IsInitialized(OwningNonNull<U>& aOwningNonNull) {
|
|
|
|
return aOwningNonNull.isInitialized();
|
|
|
|
}
|
2016-12-30 00:19:25 +03:00
|
|
|
|
|
|
|
JSContext* mCx;
|
2016-05-18 19:23:35 +03:00
|
|
|
};
|
|
|
|
|
2013-01-28 17:34:29 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_CallbackObject_h
|