2012-11-09 07:43:57 -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 types in C++.
|
|
|
|
*
|
|
|
|
* This class implements common functionality like lifetime
|
|
|
|
* management, initialization with the callable, and setup of the call
|
|
|
|
* environment. Subclasses corresponding to particular callback
|
|
|
|
* function types should provide a Call() method that actually does
|
|
|
|
* the call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jswrapper.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "xpcpublic.h"
|
2012-11-13 00:26:55 -08:00
|
|
|
#include "nsLayoutStatics.h"
|
2012-11-09 07:43:57 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class CallbackFunction : public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackFunction)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a CallbackFunction. aCallable is the callable we're wrapping.
|
|
|
|
* aOwner is the object that will be receiving this CallbackFunction as a
|
|
|
|
* method argument, if any. We need this so we can store our callable in the
|
|
|
|
* same compartment as our owner. If *aInited is set to false, an exception
|
|
|
|
* has been thrown.
|
|
|
|
*/
|
|
|
|
CallbackFunction(JSContext* cx, JSObject* aOwner, JSObject* aCallable,
|
|
|
|
bool* aInited)
|
|
|
|
: mCallable(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(JS_ObjectIsCallable(cx, aCallable));
|
|
|
|
// If aOwner is not null, enter the compartment of aOwner's
|
|
|
|
// underlying object.
|
|
|
|
if (aOwner) {
|
|
|
|
aOwner = js::UnwrapObject(aOwner);
|
|
|
|
JSAutoCompartment ac(cx, aOwner);
|
|
|
|
if (!JS_WrapObject(cx, &aCallable)) {
|
|
|
|
*aInited = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set mCallable before we hold, on the off chance that a GC could somehow
|
|
|
|
// happen in there... (which would be pretty odd, granted).
|
|
|
|
mCallable = aCallable;
|
2012-11-13 00:26:55 -08:00
|
|
|
// Make sure we'll be able to drop as needed
|
|
|
|
nsLayoutStatics::AddRef();
|
2012-11-09 07:43:57 -08:00
|
|
|
NS_HOLD_JS_OBJECTS(this, CallbackFunction);
|
|
|
|
*aInited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~CallbackFunction()
|
|
|
|
{
|
|
|
|
DropCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* Callable() const
|
|
|
|
{
|
|
|
|
xpc_UnmarkGrayObject(mCallable);
|
|
|
|
return mCallable;
|
|
|
|
}
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
bool HasGrayCallable() const
|
|
|
|
{
|
|
|
|
// Play it safe in case this gets called after unlink.
|
|
|
|
return mCallable && xpc_IsGrayGCThing(mCallable);
|
|
|
|
}
|
|
|
|
|
2012-11-09 07:43:57 -08:00
|
|
|
protected:
|
2012-12-06 02:41:14 -08:00
|
|
|
explicit CallbackFunction(CallbackFunction* aCallbackFunction)
|
|
|
|
: mCallable(aCallbackFunction->mCallable)
|
|
|
|
{
|
|
|
|
// Set mCallable before we hold, on the off chance that a GC could somehow
|
|
|
|
// happen in there... (which would be pretty odd, granted).
|
|
|
|
// Make sure we'll be able to drop as needed
|
|
|
|
nsLayoutStatics::AddRef();
|
|
|
|
NS_HOLD_JS_OBJECTS(this, CallbackFunction);
|
|
|
|
}
|
|
|
|
|
2012-11-09 07:43:57 -08:00
|
|
|
void DropCallback()
|
|
|
|
{
|
|
|
|
if (mCallable) {
|
|
|
|
mCallable = nullptr;
|
2012-11-27 17:37:57 -08:00
|
|
|
NS_DROP_JS_OBJECTS(this, CallbackFunction);
|
2012-11-13 00:26:55 -08:00
|
|
|
nsLayoutStatics::Release();
|
2012-11-09 07:43:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* mCallable;
|
|
|
|
|
|
|
|
class NS_STACK_CLASS CallSetup
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A class that performs whatever setup we need to safely make a
|
|
|
|
* call while this class is on the stack, After the constructor
|
|
|
|
* returns, the call is safe to make if GetContext() returns
|
|
|
|
* non-null.
|
|
|
|
*/
|
|
|
|
public:
|
|
|
|
CallSetup(JSObject* const aCallable);
|
|
|
|
~CallSetup();
|
|
|
|
|
|
|
|
JSContext* GetContext() const
|
|
|
|
{
|
|
|
|
return mCx;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// We better not get copy-constructed
|
|
|
|
CallSetup(const CallSetup&) MOZ_DELETE;
|
|
|
|
|
|
|
|
// Members which can go away whenever
|
|
|
|
JSContext* mCx;
|
|
|
|
nsCOMPtr<nsIScriptContext> mCtx;
|
|
|
|
|
|
|
|
// And now members whose construction/destruction order we need to control.
|
|
|
|
|
|
|
|
// Put our nsAutoMicrotask first, so it gets destroyed after everything else
|
|
|
|
// is gone
|
|
|
|
nsAutoMicroTask mMt;
|
|
|
|
|
|
|
|
// Can't construct an XPCAutoRequest until we have a JSContext, so
|
|
|
|
// this needs to be a Maybe.
|
|
|
|
Maybe<XPCAutoRequest> mAr;
|
|
|
|
|
|
|
|
// Can't construct a TerminationFuncHolder without an nsJSContext. But we
|
|
|
|
// generally want its destructor to come after the destructor of mCxPusher.
|
|
|
|
Maybe<nsJSContext::TerminationFuncHolder> mTerminationFuncHolder;
|
|
|
|
|
|
|
|
nsCxPusher mCxPusher;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|