2013-01-28 05:34:29 -08:00
|
|
|
/* -*- 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"
|
2013-03-17 00:55:15 -07:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-09-10 08:29:43 -07:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2013-01-28 05:34:29 -08:00
|
|
|
#include "mozilla/Util.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2013-01-28 05:34:29 -08:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "xpcpublic.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-04-16 14:14:30 -07:00
|
|
|
#define DOM_CALLBACKOBJECT_IID \
|
|
|
|
{ 0xbe74c190, 0x6d76, 0x4991, \
|
|
|
|
{ 0x84, 0xb9, 0x65, 0x06, 0x99, 0xe6, 0x93, 0x2b } }
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
class CallbackObject : public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
2013-04-16 14:14:30 -07:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(DOM_CALLBACKOBJECT_IID)
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackObject)
|
|
|
|
|
2013-04-03 19:22:15 -07:00
|
|
|
explicit CallbackObject(JSObject* aCallback)
|
|
|
|
{
|
|
|
|
Init(aCallback);
|
|
|
|
}
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
virtual ~CallbackObject()
|
|
|
|
{
|
|
|
|
DropCallback();
|
|
|
|
}
|
|
|
|
|
2013-04-25 09:29:52 -07:00
|
|
|
JS::Handle<JSObject*> Callback() const
|
2013-01-28 05:34:29 -08:00
|
|
|
{
|
2013-09-08 20:28:48 -07:00
|
|
|
JS::ExposeObjectToActiveJS(mCallback);
|
2013-04-25 09:29:52 -07:00
|
|
|
return CallbackPreserveColor();
|
2013-01-28 05:34:29 -08:00
|
|
|
}
|
|
|
|
|
2013-02-26 12:10:15 -08: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 09:29:52 -07:00
|
|
|
JS::Handle<JSObject*> CallbackPreserveColor() const
|
2013-02-26 12:10:15 -08:00
|
|
|
{
|
2013-07-23 02:58:28 -07: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 02:58:27 -07:00
|
|
|
return JS::Handle<JSObject*>::fromMarkedLocation(mCallback.address());
|
2013-02-26 12:10:15 -08:00
|
|
|
}
|
|
|
|
|
2013-02-19 08:54:40 -08:00
|
|
|
enum ExceptionHandling {
|
2013-09-03 05:01:53 -07:00
|
|
|
// Report any exception and don't throw it to the caller code.
|
2013-02-19 08:54:40 -08:00
|
|
|
eReportExceptions,
|
2013-09-03 05:01:53 -07:00
|
|
|
// 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.
|
2013-02-19 08:54:40 -08:00
|
|
|
eRethrowExceptions
|
|
|
|
};
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
protected:
|
2013-04-03 19:22:15 -07:00
|
|
|
explicit CallbackObject(CallbackObject* aCallbackObject)
|
|
|
|
{
|
|
|
|
Init(aCallbackObject->mCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
inline void Init(JSObject* aCallback)
|
2013-01-28 05:34:29 -08:00
|
|
|
{
|
2013-07-23 02:58:28 -07:00
|
|
|
MOZ_ASSERT(aCallback && !mCallback);
|
2013-01-28 05:34:29 -08:00
|
|
|
// Set mCallback before we hold, on the off chance that a GC could somehow
|
|
|
|
// happen in there... (which would be pretty odd, granted).
|
2013-04-03 19:22:15 -07:00
|
|
|
mCallback = aCallback;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2013-01-28 05:34:29 -08:00
|
|
|
}
|
|
|
|
|
2013-04-03 19:22:15 -07:00
|
|
|
protected:
|
2013-01-28 05:34:29 -08:00
|
|
|
void DropCallback()
|
|
|
|
{
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback = nullptr;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2013-01-28 05:34:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:38 -07:00
|
|
|
JS::Heap<JSObject*> mCallback;
|
2013-01-28 05:34:29 -08:00
|
|
|
|
2013-04-11 20:20:18 -07:00
|
|
|
class MOZ_STACK_CLASS CallSetup
|
2013-01-28 05:34:29 -08: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:
|
2013-09-03 05:01:53 -07:00
|
|
|
// If aExceptionHandling == eRethrowContentExceptions then aCompartment
|
|
|
|
// needs to be set to the caller's compartment.
|
2013-05-03 16:29:09 -07:00
|
|
|
CallSetup(JS::Handle<JSObject*> aCallable, ErrorResult& aRv,
|
2013-09-03 05:01:53 -07:00
|
|
|
ExceptionHandling aExceptionHandling,
|
|
|
|
JSCompartment* aCompartment = nullptr);
|
2013-01-28 05:34:29 -08:00
|
|
|
~CallSetup();
|
|
|
|
|
|
|
|
JSContext* GetContext() const
|
|
|
|
{
|
|
|
|
return mCx;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// We better not get copy-constructed
|
|
|
|
CallSetup(const CallSetup&) MOZ_DELETE;
|
|
|
|
|
2013-09-03 05:01:53 -07:00
|
|
|
bool ShouldRethrowException(JS::Handle<JS::Value> aException);
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
// Members which can go away whenever
|
|
|
|
JSContext* mCx;
|
|
|
|
|
2013-09-03 05:01:53 -07:00
|
|
|
// Caller's compartment. This will only have a sensible value if
|
|
|
|
// mExceptionHandling == eRethrowContentExceptions.
|
|
|
|
JSCompartment* mCompartment;
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
// And now members whose construction/destruction order we need to control.
|
|
|
|
|
|
|
|
nsCxPusher mCxPusher;
|
|
|
|
|
2013-06-20 11:05:34 -07:00
|
|
|
// 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;
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
// 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;
|
2013-02-19 08:54:40 -08:00
|
|
|
|
|
|
|
// An ErrorResult to possibly re-throw exceptions on and whether
|
|
|
|
// we should re-throw them.
|
|
|
|
ErrorResult& mErrorResult;
|
|
|
|
const ExceptionHandling mExceptionHandling;
|
|
|
|
uint32_t mSavedJSContextOptions;
|
2013-09-09 14:58:29 -07:00
|
|
|
const bool mIsMainThread;
|
2013-01-28 05:34:29 -08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-02-26 12:10:15 -08:00
|
|
|
template<class WebIDLCallbackT, class XPCOMCallbackT>
|
|
|
|
class CallbackObjectHolder;
|
|
|
|
|
|
|
|
template<class T, class U>
|
2013-02-26 12:10:15 -08:00
|
|
|
void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField);
|
2013-02-26 12:10:15 -08:00
|
|
|
|
|
|
|
class CallbackObjectHolderBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
// Returns null on all failures
|
|
|
|
already_AddRefed<nsISupports> ToXPCOMCallback(CallbackObject* aCallback,
|
2013-04-16 14:15:57 -07:00
|
|
|
const nsIID& aIID) const;
|
2013-02-26 12:10:15 -08: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit CallbackObjectHolder(const CallbackObjectHolder& aOther)
|
|
|
|
: mPtrBits(aOther.mPtrBits)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(GetISupports());
|
|
|
|
}
|
|
|
|
|
|
|
|
CallbackObjectHolder()
|
|
|
|
: mPtrBits(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~CallbackObjectHolder()
|
|
|
|
{
|
|
|
|
UnlinkSelf();
|
|
|
|
}
|
|
|
|
|
2013-04-16 14:15:57 -07: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(const CallbackObjectHolder& aOther)
|
|
|
|
{
|
|
|
|
UnlinkSelf();
|
|
|
|
mPtrBits = aOther.mPtrBits;
|
|
|
|
NS_IF_ADDREF(GetISupports());
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:10:15 -08:00
|
|
|
nsISupports* GetISupports() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<nsISupports*>(mPtrBits & ~XPCOMCallbackFlag);
|
|
|
|
}
|
|
|
|
|
2013-04-16 14:15:57 -07:00
|
|
|
// Boolean conversion operator so people can use this in boolean tests
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return GetISupports();
|
|
|
|
}
|
|
|
|
|
2013-02-26 12:10:15 -08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* thisObj =
|
2013-04-11 11:50:18 -07:00
|
|
|
js::UncheckedUnwrap(GetWebIDLCallback()->CallbackPreserveColor());
|
2013-02-26 12:10:15 -08:00
|
|
|
JSObject* otherObj =
|
2013-04-11 11:50:18 -07:00
|
|
|
js::UncheckedUnwrap(aOtherCallback->CallbackPreserveColor());
|
2013-02-26 12:10:15 -08:00
|
|
|
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.
|
2013-04-16 14:15:57 -07:00
|
|
|
already_AddRefed<XPCOMCallbackT> ToXPCOMCallback() const
|
2013-02-26 12:10:15 -08:00
|
|
|
{
|
|
|
|
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.
|
2013-04-21 04:56:06 -07:00
|
|
|
return supp.forget().downcast<XPCOMCallbackT>();
|
2013-02-26 12:10:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to return a WebIDLCallbackT version of this object.
|
2013-04-16 14:15:57 -07:00
|
|
|
already_AddRefed<WebIDLCallbackT> ToWebIDLCallback() const
|
2013-02-26 12:10:15 -08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-06 06:04:17 -07:00
|
|
|
AutoSafeJSContext cx;
|
2013-05-03 16:29:09 -07:00
|
|
|
|
2013-05-20 05:44:18 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, wrappedJS->GetJSObject());
|
|
|
|
if (!obj) {
|
2013-02-26 12:10:15 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
|
2013-04-24 11:59:15 -07:00
|
|
|
nsRefPtr<WebIDLCallbackT> newCallback = new WebIDLCallbackT(obj);
|
2013-02-26 12:10:15 -08:00
|
|
|
return newCallback.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const uintptr_t XPCOMCallbackFlag = 1u;
|
|
|
|
|
2013-02-26 12:10:15 -08:00
|
|
|
friend void
|
2013-02-26 12:10:15 -08:00
|
|
|
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-16 14:14:30 -07:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(CallbackObject, DOM_CALLBACKOBJECT_IID)
|
|
|
|
|
2013-02-26 12:10:15 -08:00
|
|
|
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>
|
2013-02-26 12:10:15 -08:00
|
|
|
void
|
2013-02-26 12:10:15 -08:00
|
|
|
ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField)
|
|
|
|
{
|
|
|
|
aField.UnlinkSelf();
|
|
|
|
}
|
|
|
|
|
2013-01-28 05:34:29 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_CallbackObject_h
|