2001-09-25 22:43:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
1998-07-17 04:29:16 +00:00
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
#ifndef mozilla_JSEventHandler_h_
|
|
|
|
#define mozilla_JSEventHandler_h_
|
1998-07-17 04:29:16 +00:00
|
|
|
|
2013-06-05 16:15:48 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-04-02 02:00:45 +00:00
|
|
|
#include "mozilla/dom/EventHandlerBinding.h"
|
2000-04-06 22:32:36 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2014-04-02 02:00:45 +00:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2000-04-06 22:32:36 +00:00
|
|
|
#include "nsIAtom.h"
|
2014-04-02 02:00:45 +00:00
|
|
|
#include "nsIDOMKeyEvent.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
2001-08-09 08:12:25 +00:00
|
|
|
#include "nsIScriptContext.h"
|
1998-07-17 04:29:16 +00:00
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class TypedEventHandler
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum HandlerType
|
|
|
|
{
|
|
|
|
eUnset = 0,
|
|
|
|
eNormal = 0x1,
|
|
|
|
eOnError = 0x2,
|
|
|
|
eOnBeforeUnload = 0x3,
|
|
|
|
eTypeBits = 0x3
|
|
|
|
};
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
TypedEventHandler()
|
2014-04-02 02:00:45 +00:00
|
|
|
: mBits(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-05 13:19:51 +00:00
|
|
|
explicit TypedEventHandler(dom::EventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
Assign(aHandler, eNormal);
|
|
|
|
}
|
|
|
|
|
2014-08-05 13:19:51 +00:00
|
|
|
explicit TypedEventHandler(dom::OnErrorEventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
Assign(aHandler, eOnError);
|
|
|
|
}
|
|
|
|
|
2014-08-05 13:19:51 +00:00
|
|
|
explicit TypedEventHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
Assign(aHandler, eOnBeforeUnload);
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
TypedEventHandler(const TypedEventHandler& aOther)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
if (aOther.HasEventHandler()) {
|
|
|
|
// Have to make sure we take our own ref
|
|
|
|
Assign(aOther.Ptr(), aOther.Type());
|
|
|
|
} else {
|
|
|
|
mBits = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
~TypedEventHandler()
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
HandlerType Type() const
|
|
|
|
{
|
|
|
|
return HandlerType(mBits & eTypeBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasEventHandler() const
|
|
|
|
{
|
|
|
|
return !!Ptr();
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
void SetHandler(const TypedEventHandler& aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
if (aHandler.HasEventHandler()) {
|
|
|
|
ReleaseHandler();
|
|
|
|
Assign(aHandler.Ptr(), aHandler.Type());
|
|
|
|
} else {
|
|
|
|
ForgetHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
dom::EventHandlerNonNull* NormalEventHandler() const
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(Type() == eNormal && Ptr());
|
2014-04-02 02:00:45 +00:00
|
|
|
return reinterpret_cast<dom::EventHandlerNonNull*>(Ptr());
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
void SetHandler(dom::EventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
Assign(aHandler, eNormal);
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
dom::OnBeforeUnloadEventHandlerNonNull* OnBeforeUnloadEventHandler() const
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(Type() == eOnBeforeUnload);
|
2014-04-02 02:00:45 +00:00
|
|
|
return reinterpret_cast<dom::OnBeforeUnloadEventHandlerNonNull*>(Ptr());
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
void SetHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
Assign(aHandler, eOnBeforeUnload);
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
dom::OnErrorEventHandlerNonNull* OnErrorEventHandler() const
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(Type() == eOnError);
|
2014-04-02 02:00:45 +00:00
|
|
|
return reinterpret_cast<dom::OnErrorEventHandlerNonNull*>(Ptr());
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
void SetHandler(dom::OnErrorEventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
Assign(aHandler, eOnError);
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
dom::CallbackFunction* Ptr() const
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
// Have to cast eTypeBits so we don't have to worry about
|
|
|
|
// promotion issues after the bitflip.
|
2014-04-02 02:00:45 +00:00
|
|
|
return reinterpret_cast<dom::CallbackFunction*>(mBits &
|
|
|
|
~uintptr_t(eTypeBits));
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForgetHandler()
|
|
|
|
{
|
|
|
|
ReleaseHandler();
|
|
|
|
mBits = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
bool operator==(const TypedEventHandler& aOther) const
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
return
|
|
|
|
Ptr() && aOther.Ptr() &&
|
|
|
|
Ptr()->CallbackPreserveColor() == aOther.Ptr()->CallbackPreserveColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-04-02 02:00:45 +00:00
|
|
|
void operator=(const TypedEventHandler&) MOZ_DELETE;
|
2014-04-02 02:00:45 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implemented by script event listeners. Used to retrieve the script object
|
|
|
|
* corresponding to the event target and the handler itself.
|
|
|
|
*
|
2014-04-02 02:00:46 +00:00
|
|
|
* Note, mTarget is a raw pointer and the owner of the JSEventHandler object
|
2014-04-02 02:00:45 +00:00
|
|
|
* is expected to call Disconnect()!
|
|
|
|
*/
|
|
|
|
|
2014-04-02 02:00:46 +00:00
|
|
|
#define NS_JSEVENTHANDLER_IID \
|
2014-04-02 02:00:45 +00:00
|
|
|
{ 0x4f486881, 0x1956, 0x4079, \
|
|
|
|
{ 0x8c, 0xa0, 0xf3, 0xbd, 0x60, 0x5c, 0xc2, 0x79 } }
|
|
|
|
|
2014-04-02 02:00:46 +00:00
|
|
|
class JSEventHandler : public nsIDOMEventListener
|
1999-11-25 00:01:30 +00:00
|
|
|
{
|
1998-07-17 04:29:16 +00:00
|
|
|
public:
|
2014-04-02 02:00:46 +00:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_JSEVENTHANDLER_IID)
|
2014-04-02 02:00:45 +00:00
|
|
|
|
2014-04-02 02:00:46 +00:00
|
|
|
JSEventHandler(nsISupports* aTarget, nsIAtom* aType,
|
|
|
|
const TypedEventHandler& aTypedHandler);
|
1998-07-17 04:29:16 +00:00
|
|
|
|
2007-01-04 22:31:26 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
1998-07-17 04:29:16 +00:00
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
// nsIDOMEventListener interface
|
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
1998-07-17 04:29:16 +00:00
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
nsISupports* GetEventTarget() const
|
|
|
|
{
|
|
|
|
return mTarget;
|
|
|
|
}
|
2011-10-29 20:16:43 +00:00
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
void Disconnect()
|
|
|
|
{
|
|
|
|
mTarget = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:46 +00:00
|
|
|
const TypedEventHandler& GetTypedEventHandler() const
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
2014-04-02 02:00:45 +00:00
|
|
|
return mTypedHandler;
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ForgetHandler()
|
|
|
|
{
|
2014-04-02 02:00:45 +00:00
|
|
|
mTypedHandler.ForgetHandler();
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom* EventName() const
|
|
|
|
{
|
|
|
|
return mEventName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a handler for this event listener. The handler must already
|
|
|
|
// be bound to the right target.
|
2014-04-02 02:00:46 +00:00
|
|
|
void SetHandler(const TypedEventHandler& aTypedHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
2014-04-02 02:00:45 +00:00
|
|
|
mTypedHandler.SetHandler(aTypedHandler);
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
2014-04-02 02:00:46 +00:00
|
|
|
void SetHandler(dom::EventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
2014-04-02 02:00:45 +00:00
|
|
|
mTypedHandler.SetHandler(aHandler);
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
2014-04-02 02:00:46 +00:00
|
|
|
void SetHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
2014-04-02 02:00:45 +00:00
|
|
|
mTypedHandler.SetHandler(aHandler);
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
2014-04-02 02:00:46 +00:00
|
|
|
void SetHandler(dom::OnErrorEventHandlerNonNull* aHandler)
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
2014-04-02 02:00:45 +00:00
|
|
|
mTypedHandler.SetHandler(aHandler);
|
2014-04-02 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:46 +00:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
2014-04-02 02:00:45 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it
|
|
|
|
// is worthwhile:
|
|
|
|
// - mTarget
|
|
|
|
//
|
|
|
|
// The following members are not measured:
|
2014-04-02 02:00:45 +00:00
|
|
|
// - mTypedHandler: may be shared with others
|
2014-04-02 02:00:45 +00:00
|
|
|
// - mEventName: shared with others
|
|
|
|
}
|
|
|
|
|
2014-04-02 02:00:46 +00:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
|
2011-08-26 22:39:00 +00:00
|
|
|
{
|
2012-02-01 21:58:01 +00:00
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
2011-08-26 22:39:00 +00:00
|
|
|
}
|
2009-10-16 08:57:32 +00:00
|
|
|
|
2014-04-02 02:00:46 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_CLASS(JSEventHandler)
|
2012-11-09 16:00:25 +00:00
|
|
|
|
2012-01-26 15:03:21 +00:00
|
|
|
bool IsBlackForCC();
|
2014-04-02 02:00:45 +00:00
|
|
|
|
|
|
|
protected:
|
2014-06-23 19:56:07 +00:00
|
|
|
virtual ~JSEventHandler();
|
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
nsISupports* mTarget;
|
|
|
|
nsCOMPtr<nsIAtom> mEventName;
|
2014-04-02 02:00:46 +00:00
|
|
|
TypedEventHandler mTypedHandler;
|
1998-07-17 04:29:16 +00:00
|
|
|
};
|
|
|
|
|
2014-04-02 02:00:46 +00:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(JSEventHandler, NS_JSEVENTHANDLER_IID)
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2014-04-02 02:00:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory function. aHandler must already be bound to aTarget.
|
|
|
|
* aContext is allowed to be null if aHandler is already set up.
|
|
|
|
*/
|
|
|
|
nsresult NS_NewJSEventHandler(nsISupports* aTarget,
|
|
|
|
nsIAtom* aType,
|
2014-04-02 02:00:45 +00:00
|
|
|
const mozilla::TypedEventHandler& aTypedHandler,
|
2014-04-02 02:00:46 +00:00
|
|
|
mozilla::JSEventHandler** aReturn);
|
2014-04-02 02:00:45 +00:00
|
|
|
|
2014-04-02 02:00:45 +00:00
|
|
|
#endif // mozilla_JSEventHandler_h_
|
1999-11-25 00:01:30 +00:00
|
|
|
|