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