2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsIJSEventListener_h__
|
|
|
|
#define nsIJSEventListener_h__
|
|
|
|
|
|
|
|
#include "nsIScriptContext.h"
|
2009-10-17 07:40:44 -07:00
|
|
|
#include "jsapi.h"
|
2012-03-20 21:29:47 -07:00
|
|
|
#include "xpcpublic.h"
|
2011-08-24 12:49:25 -07:00
|
|
|
#include "nsIDOMEventListener.h"
|
2012-11-09 08:00:25 -08:00
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "mozilla/dom/EventHandlerBinding.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-29 13:16:43 -07:00
|
|
|
#define NS_IJSEVENTLISTENER_IID \
|
2011-11-29 01:44:06 -08:00
|
|
|
{ 0x92f9212b, 0xa6aa, 0x4867, \
|
|
|
|
{ 0x93, 0x8a, 0x56, 0xbe, 0x17, 0x67, 0x4f, 0xd4 } }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
class nsEventHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef mozilla::dom::EventHandlerNonNull EventHandlerNonNull;
|
|
|
|
typedef mozilla::dom::BeforeUnloadEventHandlerNonNull
|
|
|
|
BeforeUnloadEventHandlerNonNull;
|
|
|
|
typedef mozilla::dom::OnErrorEventHandlerNonNull OnErrorEventHandlerNonNull;
|
|
|
|
typedef mozilla::dom::CallbackFunction CallbackFunction;
|
|
|
|
|
|
|
|
enum HandlerType {
|
|
|
|
eUnset = 0,
|
|
|
|
eNormal = 0x1,
|
|
|
|
eOnError = 0x2,
|
|
|
|
eOnBeforeUnload = 0x3,
|
|
|
|
eTypeBits = 0x3
|
|
|
|
};
|
|
|
|
|
|
|
|
nsEventHandler() :
|
|
|
|
mBits(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsEventHandler(EventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
Assign(aHandler, eNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventHandler(OnErrorEventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
Assign(aHandler, eOnError);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventHandler(BeforeUnloadEventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
Assign(aHandler, eOnBeforeUnload);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventHandler(const nsEventHandler& aOther)
|
|
|
|
{
|
|
|
|
if (aOther.HasEventHandler()) {
|
|
|
|
// Have to make sure we take our own ref
|
|
|
|
Assign(aOther.Ptr(), aOther.Type());
|
|
|
|
} else {
|
|
|
|
mBits = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsEventHandler()
|
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
HandlerType Type() const {
|
|
|
|
return HandlerType(mBits & eTypeBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasEventHandler() const
|
|
|
|
{
|
|
|
|
return !!Ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHandler(const nsEventHandler& aHandler)
|
|
|
|
{
|
|
|
|
if (aHandler.HasEventHandler()) {
|
|
|
|
ReleaseHandler();
|
|
|
|
Assign(aHandler.Ptr(), aHandler.Type());
|
|
|
|
} else {
|
|
|
|
ForgetHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EventHandlerNonNull* EventHandler() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Type() == eNormal && Ptr());
|
|
|
|
return reinterpret_cast<EventHandlerNonNull*>(Ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHandler(EventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
Assign(aHandler, eNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
BeforeUnloadEventHandlerNonNull* BeforeUnloadEventHandler() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Type() == eOnBeforeUnload);
|
|
|
|
return reinterpret_cast<BeforeUnloadEventHandlerNonNull*>(Ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHandler(BeforeUnloadEventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
Assign(aHandler, eOnBeforeUnload);
|
|
|
|
}
|
|
|
|
|
|
|
|
OnErrorEventHandlerNonNull* OnErrorEventHandler() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Type() == eOnError);
|
|
|
|
return reinterpret_cast<OnErrorEventHandlerNonNull*>(Ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHandler(OnErrorEventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
Assign(aHandler, eOnError);
|
|
|
|
}
|
|
|
|
|
|
|
|
CallbackFunction* Ptr() const
|
|
|
|
{
|
|
|
|
// Have to cast eTypeBits so we don't have to worry about
|
|
|
|
// promotion issues after the bitflip.
|
|
|
|
return reinterpret_cast<CallbackFunction*>(mBits & ~uintptr_t(eTypeBits));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ForgetHandler()
|
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
mBits = 0;
|
|
|
|
}
|
|
|
|
|
2013-05-24 10:22:20 -07:00
|
|
|
bool operator==(const nsEventHandler& aOther) const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
Ptr() && aOther.Ptr() &&
|
|
|
|
Ptr()->CallbackPreserveColor() == aOther.Ptr()->CallbackPreserveColor();
|
|
|
|
}
|
2012-11-09 08:00:25 -08:00
|
|
|
private:
|
|
|
|
void operator=(const nsEventHandler&) MOZ_DELETE;
|
|
|
|
|
|
|
|
void ReleaseHandler()
|
|
|
|
{
|
|
|
|
nsISupports* ptr = Ptr();
|
|
|
|
NS_IF_RELEASE(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Assign(nsISupports* aHandler, HandlerType aType) {
|
|
|
|
MOZ_ASSERT(aHandler, "Must have handler");
|
|
|
|
NS_ADDREF(aHandler);
|
|
|
|
mBits = uintptr_t(aHandler) | uintptr_t(aType);
|
|
|
|
}
|
|
|
|
|
|
|
|
uintptr_t mBits;
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Implemented by script event listeners. Used to retrieve the
|
2011-08-24 12:49:25 -07:00
|
|
|
// script object corresponding to the event target and the handler itself.
|
2007-03-22 10:30:00 -07:00
|
|
|
// (Note this interface is now used to store script objects for all
|
|
|
|
// script languages, so is no longer JS specific)
|
2011-11-29 01:44:06 -08:00
|
|
|
//
|
|
|
|
// Note, mTarget is a raw pointer and the owner of the nsIJSEventListener object
|
|
|
|
// is expected to call Disconnect()!
|
2011-08-24 12:49:25 -07:00
|
|
|
class nsIJSEventListener : public nsIDOMEventListener
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IJSEVENTLISTENER_IID)
|
|
|
|
|
2011-10-29 13:17:43 -07:00
|
|
|
nsIJSEventListener(nsIScriptContext* aContext, JSObject* aScopeObject,
|
2012-11-09 08:00:25 -08:00
|
|
|
nsISupports *aTarget, nsIAtom* aType,
|
|
|
|
const nsEventHandler& aHandler)
|
|
|
|
: mContext(aContext), mScopeObject(aScopeObject), mEventName(aType),
|
|
|
|
mHandler(aHandler)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-29 01:44:06 -08:00
|
|
|
nsCOMPtr<nsISupports> base = do_QueryInterface(aTarget);
|
|
|
|
mTarget = base.get();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
// Can return null if we already have a handler.
|
2011-08-24 12:49:25 -07:00
|
|
|
nsIScriptContext *GetEventContext() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
2011-08-24 12:49:25 -07:00
|
|
|
nsISupports *GetEventTarget() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mTarget;
|
|
|
|
}
|
|
|
|
|
2011-11-29 01:44:06 -08:00
|
|
|
void Disconnect()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mTarget = nullptr;
|
2011-11-29 01:44:06 -08:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
// Can return null if we already have a handler.
|
2011-10-29 13:17:43 -07:00
|
|
|
JSObject* GetEventScope() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-03-20 21:29:47 -07:00
|
|
|
return xpc_UnmarkGrayObject(mScopeObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
const nsEventHandler& GetHandler() const
|
|
|
|
{
|
|
|
|
return mHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom* EventName() const
|
2011-08-24 12:49:25 -07:00
|
|
|
{
|
2012-11-09 08:00:25 -08:00
|
|
|
return mEventName;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
// Set a handler for this event listener. The handler must already
|
|
|
|
// be bound to the right target.
|
2012-11-09 08:00:25 -08:00
|
|
|
void SetHandler(const nsEventHandler& aHandler, nsIScriptContext* aContext,
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Handle<JSObject*> aScopeObject)
|
2012-11-09 08:00:25 -08:00
|
|
|
{
|
|
|
|
mHandler.SetHandler(aHandler);
|
2012-11-09 08:00:25 -08:00
|
|
|
mContext = aContext;
|
2012-11-09 08:00:25 -08:00
|
|
|
UpdateScopeObject(aScopeObject);
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
|
|
|
void SetHandler(mozilla::dom::EventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
mHandler.SetHandler(aHandler);
|
|
|
|
}
|
|
|
|
void SetHandler(mozilla::dom::BeforeUnloadEventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
mHandler.SetHandler(aHandler);
|
|
|
|
}
|
|
|
|
void SetHandler(mozilla::dom::OnErrorEventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
mHandler.SetHandler(aHandler);
|
|
|
|
}
|
2009-10-16 01:57:32 -07:00
|
|
|
|
2012-02-01 13:58:01 -08:00
|
|
|
virtual size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it
|
|
|
|
// is worthwhile:
|
|
|
|
// - mContext
|
|
|
|
// - mTarget
|
|
|
|
//
|
|
|
|
// The following members are not measured:
|
2012-11-09 08:00:25 -08:00
|
|
|
// - mScopeObject: because they're measured by the JS memory
|
2012-02-01 13:58:01 -08:00
|
|
|
// reporters
|
2012-11-09 08:00:25 -08:00
|
|
|
// - mHandler: may be shared with others
|
|
|
|
// - mEventName: shared with others
|
2012-02-01 13:58:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
|
|
|
virtual ~nsIJSEventListener()
|
|
|
|
{
|
2011-11-29 01:44:06 -08:00
|
|
|
NS_ASSERTION(!mTarget, "Should have called Disconnect()!");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-11-09 08:00:25 -08:00
|
|
|
|
|
|
|
// Update our mScopeObject; we have to make sure we properly handle
|
|
|
|
// the hold/drop stuff, so have to do it in nsJSEventListener.
|
2013-05-04 00:52:57 -07:00
|
|
|
virtual void UpdateScopeObject(JS::Handle<JSObject*> aScopeObject) = 0;
|
2012-11-09 08:00:25 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIScriptContext> mContext;
|
2013-06-18 03:00:38 -07:00
|
|
|
JS::Heap<JSObject*> mScopeObject;
|
2011-11-29 01:44:06 -08:00
|
|
|
nsISupports* mTarget;
|
2012-11-09 08:00:25 -08:00
|
|
|
nsCOMPtr<nsIAtom> mEventName;
|
|
|
|
nsEventHandler mHandler;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIJSEventListener, NS_IJSEVENTLISTENER_IID)
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
/* factory function. aHandler must already be bound to aTarget.
|
|
|
|
aContext is allowed to be null if aHandler is already set up.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult NS_NewJSEventListener(nsIScriptContext *aContext,
|
2011-10-29 13:17:43 -07:00
|
|
|
JSObject* aScopeObject, nsISupports* aTarget,
|
2012-11-09 08:00:25 -08:00
|
|
|
nsIAtom* aType, const nsEventHandler& aHandler,
|
2011-11-29 01:44:06 -08:00
|
|
|
nsIJSEventListener **aReturn);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#endif // nsIJSEventListener_h__
|