2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
2006-03-07 20:08:51 +03:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2014-03-18 08:48:21 +04:00
|
|
|
#ifndef mozilla_EventDispatcher_h_
|
|
|
|
#define mozilla_EventDispatcher_h_
|
2006-03-07 20:08:51 +03:00
|
|
|
|
2017-04-20 15:45:37 +03:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2013-09-24 14:04:14 +04:00
|
|
|
#include "mozilla/EventForwards.h"
|
2009-04-02 01:59:02 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2014-08-27 07:19:56 +04:00
|
|
|
#include "nsTArray.h"
|
2006-03-07 20:08:51 +03:00
|
|
|
|
2013-11-21 01:05:37 +04:00
|
|
|
// Microsoft's API Name hackery sucks
|
|
|
|
#undef CreateEvent
|
|
|
|
|
2014-08-27 07:19:56 +04:00
|
|
|
class nsIContent;
|
2009-04-02 01:59:02 +04:00
|
|
|
class nsIDOMEvent;
|
2013-04-06 04:44:15 +04:00
|
|
|
class nsPresContext;
|
|
|
|
|
2009-10-16 12:57:32 +04:00
|
|
|
template<class E> class nsCOMArray;
|
2013-04-06 04:44:15 +04:00
|
|
|
|
2013-03-09 15:34:29 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-08-12 14:39:31 +03:00
|
|
|
class Event;
|
2013-03-09 15:34:29 +04:00
|
|
|
class EventTarget;
|
2014-03-18 08:48:18 +04:00
|
|
|
} // namespace dom
|
2006-03-07 20:08:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* About event dispatching:
|
2014-03-18 08:48:21 +04:00
|
|
|
* When either EventDispatcher::Dispatch or
|
|
|
|
* EventDispatcher::DispatchDOMEvent is called an event target chain is
|
2017-07-06 15:00:35 +03:00
|
|
|
* created. EventDispatcher creates the chain by calling GetEventTargetParent
|
2007-02-16 02:04:33 +03:00
|
|
|
* on each event target and the creation continues until either the mCanHandle
|
2014-03-18 08:48:19 +04:00
|
|
|
* member of the EventChainPreVisitor object is false or the mParentTarget
|
2007-02-16 02:04:33 +03:00
|
|
|
* does not point to a new target. The event target chain is created in the
|
|
|
|
* heap.
|
2006-03-07 20:08:51 +03:00
|
|
|
*
|
|
|
|
* If the event needs retargeting, mEventTargetAtParent must be set in
|
2016-10-21 05:11:07 +03:00
|
|
|
* GetEventTargetParent.
|
2006-03-07 20:08:51 +03:00
|
|
|
*
|
|
|
|
* The capture, target and bubble phases of the event dispatch are handled
|
|
|
|
* by iterating through the event target chain. Iteration happens twice,
|
|
|
|
* first for the default event group and then for the system event group.
|
|
|
|
* While dispatching the event for the system event group PostHandleEvent
|
2007-02-16 02:04:33 +03:00
|
|
|
* is called right after calling event listener for the current event target.
|
2006-03-07 20:08:51 +03:00
|
|
|
*/
|
|
|
|
|
2014-03-18 08:48:18 +04:00
|
|
|
class EventChainVisitor
|
|
|
|
{
|
2006-03-07 20:08:51 +03:00
|
|
|
public:
|
2014-03-18 08:48:18 +04:00
|
|
|
EventChainVisitor(nsPresContext* aPresContext,
|
|
|
|
WidgetEvent* aEvent,
|
|
|
|
nsIDOMEvent* aDOMEvent,
|
|
|
|
nsEventStatus aEventStatus = nsEventStatus_eIgnore)
|
|
|
|
: mPresContext(aPresContext)
|
|
|
|
, mEvent(aEvent)
|
|
|
|
, mDOMEvent(aDOMEvent)
|
|
|
|
, mEventStatus(aEventStatus)
|
|
|
|
, mItemFlags(0)
|
|
|
|
{
|
|
|
|
}
|
2006-03-07 20:08:51 +03:00
|
|
|
|
|
|
|
/**
|
2012-07-30 18:20:58 +04:00
|
|
|
* The prescontext, possibly nullptr.
|
2006-03-07 20:08:51 +03:00
|
|
|
*/
|
|
|
|
nsPresContext* const mPresContext;
|
|
|
|
|
|
|
|
/**
|
2013-10-02 07:46:04 +04:00
|
|
|
* The WidgetEvent which is being dispatched. Never nullptr.
|
2006-03-07 20:08:51 +03:00
|
|
|
*/
|
2014-03-18 08:48:18 +04:00
|
|
|
WidgetEvent* const mEvent;
|
2006-03-07 20:08:51 +03:00
|
|
|
|
|
|
|
/**
|
2012-07-30 18:20:58 +04:00
|
|
|
* The DOM Event assiciated with the mEvent. Possibly nullptr if a DOM Event
|
2006-03-07 20:08:51 +03:00
|
|
|
* is not (yet) created.
|
|
|
|
*/
|
|
|
|
nsIDOMEvent* mDOMEvent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The status of the event.
|
|
|
|
* @see nsEventStatus.h
|
|
|
|
*/
|
|
|
|
nsEventStatus mEventStatus;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bits for items in the event target chain.
|
2016-10-21 05:11:07 +03:00
|
|
|
* Set in GetEventTargetParent() and used in PostHandleEvent().
|
2006-03-07 20:08:51 +03:00
|
|
|
*
|
|
|
|
* @note These bits are different for each item in the event target chain.
|
|
|
|
* It is up to the Pre/PostHandleEvent implementation to decide how to
|
|
|
|
* use these bits.
|
|
|
|
*
|
2014-03-18 08:48:22 +04:00
|
|
|
* @note Using uint16_t because that is used also in EventTargetChainItem.
|
2006-03-07 20:08:51 +03:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t mItemFlags;
|
2006-03-07 20:08:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data for items in the event target chain.
|
2016-10-21 05:11:07 +03:00
|
|
|
* Set in GetEventTargetParent() and used in PostHandleEvent().
|
2006-03-07 20:08:51 +03:00
|
|
|
*
|
|
|
|
* @note This data is different for each item in the event target chain.
|
|
|
|
* It is up to the Pre/PostHandleEvent implementation to decide how to
|
|
|
|
* use this.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsISupports> mItemData;
|
|
|
|
};
|
|
|
|
|
2014-03-18 08:48:19 +04:00
|
|
|
class EventChainPreVisitor : public EventChainVisitor
|
2014-03-18 08:48:18 +04:00
|
|
|
{
|
2006-03-07 20:08:51 +03:00
|
|
|
public:
|
2014-03-18 08:48:19 +04:00
|
|
|
EventChainPreVisitor(nsPresContext* aPresContext,
|
|
|
|
WidgetEvent* aEvent,
|
|
|
|
nsIDOMEvent* aDOMEvent,
|
|
|
|
nsEventStatus aEventStatus,
|
|
|
|
bool aIsInAnon)
|
|
|
|
: EventChainVisitor(aPresContext, aEvent, aDOMEvent, aEventStatus)
|
|
|
|
, mCanHandle(true)
|
|
|
|
, mAutomaticChromeDispatch(true)
|
|
|
|
, mForceContentDispatch(false)
|
|
|
|
, mRelatedTargetIsInAnon(false)
|
|
|
|
, mOriginalTargetIsInAnon(aIsInAnon)
|
|
|
|
, mWantsWillHandleEvent(false)
|
|
|
|
, mMayHaveListenerManager(true)
|
2016-10-21 06:29:34 +03:00
|
|
|
, mWantsPreHandleEvent(false)
|
2014-03-18 08:48:19 +04:00
|
|
|
, mParentTarget(nullptr)
|
|
|
|
, mEventTargetAtParent(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset()
|
|
|
|
{
|
2006-03-07 20:08:51 +03:00
|
|
|
mItemFlags = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
mItemData = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mCanHandle = true;
|
2013-01-03 19:17:36 +04:00
|
|
|
mAutomaticChromeDispatch = true;
|
2011-10-17 18:59:28 +04:00
|
|
|
mForceContentDispatch = false;
|
|
|
|
mWantsWillHandleEvent = false;
|
|
|
|
mMayHaveListenerManager = true;
|
2016-10-21 06:29:34 +03:00
|
|
|
mWantsPreHandleEvent = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
mParentTarget = nullptr;
|
|
|
|
mEventTargetAtParent = nullptr;
|
2006-03-07 20:08:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-21 05:11:07 +03:00
|
|
|
* Member that must be set in GetEventTargetParent by event targets. If set to
|
|
|
|
* false, indicates that this event target will not be handling the event and
|
2007-02-16 02:04:33 +03:00
|
|
|
* construction of the event target chain is complete. The target that sets
|
|
|
|
* mCanHandle to false is NOT included in the event target chain.
|
2006-03-07 20:08:51 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mCanHandle;
|
2006-03-07 20:08:51 +03:00
|
|
|
|
2013-01-03 19:17:36 +04:00
|
|
|
/**
|
|
|
|
* If mCanHandle is false and mAutomaticChromeDispatch is also false
|
|
|
|
* event will not be dispatched to the chrome event handler.
|
|
|
|
*/
|
|
|
|
bool mAutomaticChromeDispatch;
|
|
|
|
|
2006-03-07 20:08:51 +03:00
|
|
|
/**
|
2011-10-17 18:59:28 +04:00
|
|
|
* If mForceContentDispatch is set to true,
|
2006-03-07 20:08:51 +03:00
|
|
|
* content dispatching is not disabled for this event target.
|
|
|
|
* FIXME! This is here for backward compatibility. Bug 329119
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mForceContentDispatch;
|
2006-03-07 20:08:51 +03:00
|
|
|
|
2007-05-13 17:59:00 +04:00
|
|
|
/**
|
2011-10-17 18:59:28 +04:00
|
|
|
* true if it is known that related target is or is a descendant of an
|
2007-05-13 17:59:00 +04:00
|
|
|
* element which is anonymous for events.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mRelatedTargetIsInAnon;
|
2008-12-03 12:26:38 +03:00
|
|
|
|
|
|
|
/**
|
2011-10-17 18:59:28 +04:00
|
|
|
* true if the original target of the event is inside anonymous content.
|
2016-10-21 05:11:07 +03:00
|
|
|
* This is set before calling GetEventTargetParent on event targets.
|
2008-12-03 12:26:38 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mOriginalTargetIsInAnon;
|
2008-10-08 15:35:29 +04:00
|
|
|
|
|
|
|
/**
|
2011-06-24 06:17:58 +04:00
|
|
|
* Whether or not nsIDOMEventTarget::WillHandleEvent will be
|
2011-10-17 18:59:28 +04:00
|
|
|
* called. Default is false;
|
2008-10-08 15:35:29 +04:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mWantsWillHandleEvent;
|
2007-05-13 17:59:00 +04:00
|
|
|
|
2009-12-18 21:50:49 +03:00
|
|
|
/**
|
|
|
|
* If it is known that the current target doesn't have a listener manager
|
2016-10-21 05:11:07 +03:00
|
|
|
* when GetEventTargetParent is called, set this to false.
|
2009-12-18 21:50:49 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mMayHaveListenerManager;
|
2009-12-18 21:50:49 +03:00
|
|
|
|
2016-10-21 06:29:34 +03:00
|
|
|
/**
|
|
|
|
* Whether or not nsIDOMEventTarget::PreHandleEvent will be called. Default is
|
|
|
|
* false;
|
|
|
|
*/
|
|
|
|
bool mWantsPreHandleEvent;
|
|
|
|
|
2006-03-07 20:08:51 +03:00
|
|
|
/**
|
|
|
|
* Parent item in the event target chain.
|
|
|
|
*/
|
2014-03-18 08:48:19 +04:00
|
|
|
dom::EventTarget* mParentTarget;
|
2006-03-07 20:08:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the event needs to be retargeted, this is the event target,
|
|
|
|
* which should be used when the event is handled at mParentTarget.
|
|
|
|
*/
|
2014-03-18 08:48:19 +04:00
|
|
|
dom::EventTarget* mEventTargetAtParent;
|
2014-08-27 07:19:56 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of destination insertion points that need to be inserted
|
|
|
|
* into the event path of nodes that are distributed by the
|
|
|
|
* web components distribution algorithm.
|
|
|
|
*/
|
|
|
|
nsTArray<nsIContent*> mDestInsertionPoints;
|
2006-03-07 20:08:51 +03:00
|
|
|
};
|
|
|
|
|
2014-03-18 08:48:20 +04:00
|
|
|
class EventChainPostVisitor : public mozilla::EventChainVisitor
|
2014-03-18 08:48:18 +04:00
|
|
|
{
|
2006-03-07 20:08:51 +03:00
|
|
|
public:
|
2014-08-05 17:19:51 +04:00
|
|
|
explicit EventChainPostVisitor(EventChainVisitor& aOther)
|
2014-03-18 08:48:20 +04:00
|
|
|
: EventChainVisitor(aOther.mPresContext, aOther.mEvent,
|
|
|
|
aOther.mDOMEvent, aOther.mEventStatus)
|
2014-03-18 08:48:18 +04:00
|
|
|
{
|
|
|
|
}
|
2006-03-07 20:08:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-03-18 08:48:20 +04:00
|
|
|
* If an EventDispatchingCallback object is passed to Dispatch,
|
2006-03-07 20:08:51 +03:00
|
|
|
* its HandleEvent method is called after handling the default event group,
|
|
|
|
* before handling the system event group.
|
|
|
|
* This is used in nsPresShell.
|
|
|
|
*/
|
2014-03-18 08:48:20 +04:00
|
|
|
class MOZ_STACK_CLASS EventDispatchingCallback
|
|
|
|
{
|
2006-03-07 20:08:51 +03:00
|
|
|
public:
|
2014-03-18 08:48:20 +04:00
|
|
|
virtual void HandleEvent(EventChainPostVisitor& aVisitor) = 0;
|
2006-03-07 20:08:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The generic class for event dispatching.
|
|
|
|
* Must not be used outside Gecko!
|
|
|
|
*/
|
2014-03-18 08:48:21 +04:00
|
|
|
class EventDispatcher
|
2006-03-07 20:08:51 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2013-04-06 04:44:15 +04:00
|
|
|
* aTarget should QI to EventTarget.
|
2017-07-06 15:00:35 +03:00
|
|
|
* If the target of aEvent is set before calling this method, the target of
|
2006-03-07 20:08:51 +03:00
|
|
|
* aEvent is used as the target (unless there is event
|
|
|
|
* retargeting) and the originalTarget of the DOM Event.
|
|
|
|
* aTarget is always used as the starting point for constructing the event
|
2016-04-18 18:46:00 +03:00
|
|
|
* target chain, no matter what the value of aEvent->mTarget is.
|
|
|
|
* In other words, aEvent->mTarget is only a property of the event and it has
|
2007-03-12 23:28:38 +03:00
|
|
|
* nothing to do with the construction of the event target chain.
|
2012-07-30 18:20:58 +04:00
|
|
|
* Neither aTarget nor aEvent is allowed to be nullptr.
|
2009-10-16 12:57:32 +04:00
|
|
|
*
|
|
|
|
* If aTargets is non-null, event target chain will be created, but
|
2015-08-22 04:34:51 +03:00
|
|
|
* event won't be handled. In this case aEvent->mMessage should be
|
2015-08-29 02:58:26 +03:00
|
|
|
* eVoidEvent.
|
2013-10-02 07:46:04 +04:00
|
|
|
* @note Use this method when dispatching a WidgetEvent.
|
2006-03-07 20:08:51 +03:00
|
|
|
*/
|
|
|
|
static nsresult Dispatch(nsISupports* aTarget,
|
|
|
|
nsPresContext* aPresContext,
|
2014-03-18 08:48:21 +04:00
|
|
|
WidgetEvent* aEvent,
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIDOMEvent* aDOMEvent = nullptr,
|
|
|
|
nsEventStatus* aEventStatus = nullptr,
|
2014-03-18 08:48:21 +04:00
|
|
|
EventDispatchingCallback* aCallback = nullptr,
|
2015-02-11 00:28:07 +03:00
|
|
|
nsTArray<dom::EventTarget*>* aTargets = nullptr);
|
2006-03-07 20:08:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches an event.
|
2012-07-30 18:20:58 +04:00
|
|
|
* If aDOMEvent is not nullptr, it is used for dispatching
|
|
|
|
* (aEvent can then be nullptr) and (if aDOMEvent is not |trusted| already),
|
2011-12-30 21:35:39 +04:00
|
|
|
* the |trusted| flag is set based on the UniversalXPConnect capability.
|
2014-03-18 08:48:21 +04:00
|
|
|
* Otherwise this works like EventDispatcher::Dispatch.
|
2006-03-07 20:08:51 +03:00
|
|
|
* @note Use this method when dispatching nsIDOMEvent.
|
|
|
|
*/
|
|
|
|
static nsresult DispatchDOMEvent(nsISupports* aTarget,
|
2014-03-18 08:48:21 +04:00
|
|
|
WidgetEvent* aEvent,
|
2013-10-02 07:46:04 +04:00
|
|
|
nsIDOMEvent* aDOMEvent,
|
2006-03-07 20:08:51 +03:00
|
|
|
nsPresContext* aPresContext,
|
|
|
|
nsEventStatus* aEventStatus);
|
|
|
|
|
|
|
|
/**
|
2015-08-12 14:39:31 +03:00
|
|
|
* Creates a DOM Event. Returns null if the event type is unsupported.
|
2006-03-07 20:08:51 +03:00
|
|
|
*/
|
2015-08-12 14:39:31 +03:00
|
|
|
static already_AddRefed<dom::Event> CreateEvent(dom::EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
WidgetEvent* aEvent,
|
2017-04-20 15:45:37 +03:00
|
|
|
const nsAString& aEventType,
|
|
|
|
dom::CallerType aCallerType =
|
|
|
|
dom::CallerType::System);
|
2006-03-07 20:08:51 +03:00
|
|
|
|
2014-03-18 08:48:22 +04:00
|
|
|
/**
|
|
|
|
* Called at shutting down.
|
|
|
|
*/
|
|
|
|
static void Shutdown();
|
2006-03-07 20:08:51 +03:00
|
|
|
};
|
|
|
|
|
2014-03-18 08:48:21 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_EventDispatcher_h_
|
2006-03-07 20:08:51 +03:00
|
|
|
#endif
|