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
|
|
|
|
|
|
|
#include "nsEventDispatcher.h"
|
|
|
|
#include "nsDOMEvent.h"
|
2011-06-23 19:18:00 -07:00
|
|
|
#include "nsIDOMEventTarget.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsPresContext.h"
|
2008-10-08 04:35:29 -07:00
|
|
|
#include "nsEventListenerManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMError.h"
|
2010-04-30 12:40:59 -07:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsMutationEvent.h"
|
|
|
|
#include NEW_H
|
|
|
|
#include "nsFixedSizeAllocator.h"
|
2009-10-29 04:11:02 -07:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2011-03-25 06:39:58 -07:00
|
|
|
#include "nsFrameLoader.h"
|
2011-04-26 05:30:17 -07:00
|
|
|
#include "nsDOMTouchEvent.h"
|
2012-03-18 11:55:46 -07:00
|
|
|
#include "nsDOMStorage.h"
|
2012-04-16 11:43:06 -07:00
|
|
|
#include "sampler.h"
|
2012-07-20 09:42:08 -07:00
|
|
|
#include "GeneratedEvents.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-08 04:35:29 -07:00
|
|
|
#define NS_TARGET_CHAIN_FORCE_CONTENT_DISPATCH (1 << 0)
|
|
|
|
#define NS_TARGET_CHAIN_WANTS_WILL_HANDLE_EVENT (1 << 1)
|
2009-12-18 10:50:49 -08:00
|
|
|
#define NS_TARGET_CHAIN_MAY_HAVE_MANAGER (1 << 2)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
static nsEventTargetChainItem* gCachedETCI = nullptr;
|
2010-04-15 02:38:32 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsEventTargetChainItem represents a single item in the event target chain.
|
|
|
|
class nsEventTargetChainItem
|
|
|
|
{
|
|
|
|
private:
|
2011-06-23 19:18:00 -07:00
|
|
|
nsEventTargetChainItem(nsIDOMEventTarget* aTarget,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsEventTargetChainItem* aChild = nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
static nsEventTargetChainItem* Create(nsFixedSizeAllocator* aAllocator,
|
2011-06-23 19:18:00 -07:00
|
|
|
nsIDOMEventTarget* aTarget,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsEventTargetChainItem* aChild = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
void* place = nullptr;
|
2010-04-15 02:38:32 -07:00
|
|
|
if (gCachedETCI) {
|
|
|
|
place = gCachedETCI;
|
|
|
|
gCachedETCI = gCachedETCI->mNext;
|
|
|
|
} else {
|
|
|
|
place = aAllocator->Alloc(sizeof(nsEventTargetChainItem));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return place
|
|
|
|
? ::new (place) nsEventTargetChainItem(aTarget, aChild)
|
2012-07-30 07:20:58 -07:00
|
|
|
: nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Destroy(nsFixedSizeAllocator* aAllocator,
|
|
|
|
nsEventTargetChainItem* aItem)
|
|
|
|
{
|
2008-12-16 02:35:56 -08:00
|
|
|
// ::Destroy deletes ancestor chain.
|
|
|
|
nsEventTargetChainItem* item = aItem;
|
|
|
|
if (item->mChild) {
|
2012-07-30 07:20:58 -07:00
|
|
|
item->mChild->mParent = nullptr;
|
|
|
|
item->mChild = nullptr;
|
2008-12-16 02:35:56 -08:00
|
|
|
}
|
|
|
|
while (item) {
|
|
|
|
nsEventTargetChainItem* parent = item->mParent;
|
|
|
|
item->~nsEventTargetChainItem();
|
2010-04-15 02:38:32 -07:00
|
|
|
item->mNext = gCachedETCI;
|
|
|
|
gCachedETCI = item;
|
2009-06-16 02:15:46 -07:00
|
|
|
--sCurrentEtciCount;
|
2008-12-16 02:35:56 -08:00
|
|
|
item = parent;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsValid()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-14 04:04:43 -07:00
|
|
|
NS_WARN_IF_FALSE(!!(mTarget), "Event target is not valid!");
|
2007-03-22 10:30:00 -07:00
|
|
|
return !!(mTarget);
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:00 -07:00
|
|
|
nsIDOMEventTarget* GetNewTarget()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mNewTarget;
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:00 -07:00
|
|
|
void SetNewTarget(nsIDOMEventTarget* aNewTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mNewTarget = aNewTarget;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void SetForceContentDispatch(bool aForce)
|
2008-10-08 04:35:29 -07:00
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aForce) {
|
|
|
|
mFlags |= NS_TARGET_CHAIN_FORCE_CONTENT_DISPATCH;
|
|
|
|
} else {
|
|
|
|
mFlags &= ~NS_TARGET_CHAIN_FORCE_CONTENT_DISPATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ForceContentDispatch()
|
2008-10-08 04:35:29 -07:00
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
return !!(mFlags & NS_TARGET_CHAIN_FORCE_CONTENT_DISPATCH);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void SetWantsWillHandleEvent(bool aWants)
|
2008-10-08 04:35:29 -07:00
|
|
|
{
|
|
|
|
if (aWants) {
|
|
|
|
mFlags |= NS_TARGET_CHAIN_WANTS_WILL_HANDLE_EVENT;
|
|
|
|
} else {
|
|
|
|
mFlags &= ~NS_TARGET_CHAIN_WANTS_WILL_HANDLE_EVENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool WantsWillHandleEvent()
|
2008-10-08 04:35:29 -07:00
|
|
|
{
|
|
|
|
return !!(mFlags & NS_TARGET_CHAIN_WANTS_WILL_HANDLE_EVENT);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void SetMayHaveListenerManager(bool aMayHave)
|
2009-12-18 10:50:49 -08:00
|
|
|
{
|
|
|
|
if (aMayHave) {
|
|
|
|
mFlags |= NS_TARGET_CHAIN_MAY_HAVE_MANAGER;
|
|
|
|
} else {
|
|
|
|
mFlags &= ~NS_TARGET_CHAIN_MAY_HAVE_MANAGER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool MayHaveListenerManager()
|
2009-12-18 10:50:49 -08:00
|
|
|
{
|
|
|
|
return !!(mFlags & NS_TARGET_CHAIN_MAY_HAVE_MANAGER);
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:00 -07:00
|
|
|
nsIDOMEventTarget* CurrentTarget()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches event through the event target chain.
|
|
|
|
* Handles capture, target and bubble phases both in default
|
|
|
|
* and system event group and calls also PostHandleEvent for each
|
|
|
|
* item in the chain.
|
|
|
|
*/
|
|
|
|
nsresult HandleEventTargetChain(nsEventChainPostVisitor& aVisitor,
|
|
|
|
PRUint32 aFlags,
|
2008-10-08 04:35:29 -07:00
|
|
|
nsDispatchingCallback* aCallback,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aMayHaveNewListenerManagers,
|
2009-10-24 08:52:29 -07:00
|
|
|
nsCxPusher* aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets aVisitor object and calls PreHandleEvent.
|
|
|
|
* Copies mItemFlags and mItemData to the current nsEventTargetChainItem.
|
|
|
|
*/
|
|
|
|
nsresult PreHandleEvent(nsEventChainPreVisitor& aVisitor);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the current item in the event target chain has an event listener
|
2011-06-23 19:18:01 -07:00
|
|
|
* manager, this method calls nsEventListenerManager::HandleEvent().
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2008-10-08 04:35:29 -07:00
|
|
|
nsresult HandleEvent(nsEventChainPostVisitor& aVisitor, PRUint32 aFlags,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aMayHaveNewListenerManagers,
|
2009-12-18 10:50:49 -08:00
|
|
|
nsCxPusher* aPusher)
|
|
|
|
{
|
|
|
|
if (WantsWillHandleEvent()) {
|
|
|
|
mTarget->WillHandleEvent(aVisitor);
|
|
|
|
}
|
|
|
|
if (aVisitor.mEvent->flags & NS_EVENT_FLAG_STOP_DISPATCH) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!mManager) {
|
|
|
|
if (!MayHaveListenerManager() && !aMayHaveNewListenerManagers) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-04-16 02:04:41 -07:00
|
|
|
mManager =
|
2011-10-17 07:59:28 -07:00
|
|
|
static_cast<nsEventListenerManager*>(mTarget->GetListenerManager(false));
|
2009-12-18 10:50:49 -08:00
|
|
|
}
|
|
|
|
if (mManager) {
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(aVisitor.mEvent->currentTarget == nullptr,
|
2009-12-18 10:50:49 -08:00
|
|
|
"CurrentTarget should be null!");
|
|
|
|
mManager->HandleEvent(aVisitor.mPresContext, aVisitor.mEvent,
|
|
|
|
&aVisitor.mDOMEvent,
|
|
|
|
CurrentTarget(), aFlags,
|
|
|
|
&aVisitor.mEventStatus,
|
|
|
|
aPusher);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(aVisitor.mEvent->currentTarget == nullptr,
|
2009-12-18 10:50:49 -08:00
|
|
|
"CurrentTarget should be null!");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies mItemFlags and mItemData to aVisitor and calls PostHandleEvent.
|
|
|
|
*/
|
2011-12-07 11:29:53 -08:00
|
|
|
nsresult PostHandleEvent(nsEventChainPostVisitor& aVisitor,
|
|
|
|
nsCxPusher* aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-06-16 02:15:46 -07:00
|
|
|
static PRUint32 MaxEtciCount() { return sMaxEtciCount; }
|
|
|
|
|
|
|
|
static void ResetMaxEtciCount()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!sCurrentEtciCount, "Wrong time to call ResetMaxEtciCount()!");
|
|
|
|
sMaxEtciCount = 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-23 19:18:01 -07:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> mTarget;
|
2007-09-05 02:02:23 -07:00
|
|
|
nsEventTargetChainItem* mChild;
|
2010-04-15 02:38:32 -07:00
|
|
|
union {
|
|
|
|
nsEventTargetChainItem* mParent;
|
|
|
|
// This is used only when caching ETCI objects.
|
|
|
|
nsEventTargetChainItem* mNext;
|
|
|
|
};
|
2007-09-05 02:02:23 -07:00
|
|
|
PRUint16 mFlags;
|
|
|
|
PRUint16 mItemFlags;
|
|
|
|
nsCOMPtr<nsISupports> mItemData;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Event retargeting must happen whenever mNewTarget is non-null.
|
2011-06-23 19:18:01 -07:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> mNewTarget;
|
2007-09-05 02:02:23 -07:00
|
|
|
// Cache mTarget's event listener manager.
|
2010-04-16 02:04:41 -07:00
|
|
|
nsRefPtr<nsEventListenerManager> mManager;
|
2009-06-16 02:15:46 -07:00
|
|
|
|
|
|
|
static PRUint32 sMaxEtciCount;
|
|
|
|
static PRUint32 sCurrentEtciCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2009-06-16 02:15:46 -07:00
|
|
|
PRUint32 nsEventTargetChainItem::sMaxEtciCount = 0;
|
|
|
|
PRUint32 nsEventTargetChainItem::sCurrentEtciCount = 0;
|
|
|
|
|
2011-06-23 19:18:00 -07:00
|
|
|
nsEventTargetChainItem::nsEventTargetChainItem(nsIDOMEventTarget* aTarget,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventTargetChainItem* aChild)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mChild(aChild), mParent(nullptr), mFlags(0), mItemFlags(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-10-08 04:35:29 -07:00
|
|
|
mTarget = aTarget->GetTargetForEventTargetChain();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mChild) {
|
|
|
|
mChild->mParent = this;
|
|
|
|
}
|
2009-06-16 02:15:46 -07:00
|
|
|
|
|
|
|
if (++sCurrentEtciCount > sMaxEtciCount) {
|
|
|
|
sMaxEtciCount = sCurrentEtciCount;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEventTargetChainItem::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
aVisitor.Reset();
|
|
|
|
nsresult rv = mTarget->PreHandleEvent(aVisitor);
|
|
|
|
SetForceContentDispatch(aVisitor.mForceContentDispatch);
|
2008-10-08 04:35:29 -07:00
|
|
|
SetWantsWillHandleEvent(aVisitor.mWantsWillHandleEvent);
|
2009-12-18 10:50:49 -08:00
|
|
|
SetMayHaveListenerManager(aVisitor.mMayHaveListenerManager);
|
2007-03-22 10:30:00 -07:00
|
|
|
mItemFlags = aVisitor.mItemFlags;
|
|
|
|
mItemData = aVisitor.mItemData;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-12-07 11:29:53 -08:00
|
|
|
nsEventTargetChainItem::PostHandleEvent(nsEventChainPostVisitor& aVisitor,
|
|
|
|
nsCxPusher* aPusher)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-12-07 11:29:53 -08:00
|
|
|
aPusher->Pop();
|
2007-03-22 10:30:00 -07:00
|
|
|
aVisitor.mItemFlags = mItemFlags;
|
|
|
|
aVisitor.mItemData = mItemData;
|
|
|
|
mTarget->PostHandleEvent(aVisitor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEventTargetChainItem::HandleEventTargetChain(nsEventChainPostVisitor& aVisitor, PRUint32 aFlags,
|
2008-10-08 04:35:29 -07:00
|
|
|
nsDispatchingCallback* aCallback,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aMayHaveNewListenerManagers,
|
2009-10-24 08:52:29 -07:00
|
|
|
nsCxPusher* aPusher)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-10-08 04:35:29 -07:00
|
|
|
PRUint32 createdELMs = nsEventListenerManager::sCreatedCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Save the target so that it can be restored later.
|
2011-06-23 19:18:00 -07:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> firstTarget = aVisitor.mEvent->target;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Capture
|
|
|
|
nsEventTargetChainItem* item = this;
|
|
|
|
aVisitor.mEvent->flags |= NS_EVENT_FLAG_CAPTURE;
|
|
|
|
aVisitor.mEvent->flags &= ~NS_EVENT_FLAG_BUBBLE;
|
|
|
|
while (item->mChild) {
|
|
|
|
if ((!(aVisitor.mEvent->flags & NS_EVENT_FLAG_NO_CONTENT_DISPATCH) ||
|
|
|
|
item->ForceContentDispatch()) &&
|
|
|
|
!(aVisitor.mEvent->flags & NS_EVENT_FLAG_STOP_DISPATCH)) {
|
2008-10-08 04:35:29 -07:00
|
|
|
item->HandleEvent(aVisitor, aFlags & NS_EVENT_CAPTURE_MASK,
|
|
|
|
aMayHaveNewListenerManagers ||
|
2009-10-24 08:52:29 -07:00
|
|
|
createdELMs != nsEventListenerManager::sCreatedCount,
|
|
|
|
aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item->GetNewTarget()) {
|
|
|
|
// item is at anonymous boundary. Need to retarget for the child items.
|
|
|
|
nsEventTargetChainItem* nextTarget = item->mChild;
|
|
|
|
while (nextTarget) {
|
2011-06-23 19:18:00 -07:00
|
|
|
nsIDOMEventTarget* newTarget = nextTarget->GetNewTarget();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (newTarget) {
|
|
|
|
aVisitor.mEvent->target = newTarget;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nextTarget = nextTarget->mChild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item = item->mChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Target
|
|
|
|
aVisitor.mEvent->flags |= NS_EVENT_FLAG_BUBBLE;
|
|
|
|
if (!(aVisitor.mEvent->flags & NS_EVENT_FLAG_STOP_DISPATCH) &&
|
|
|
|
(!(aVisitor.mEvent->flags & NS_EVENT_FLAG_NO_CONTENT_DISPATCH) ||
|
|
|
|
item->ForceContentDispatch())) {
|
|
|
|
// FIXME Should use aFlags & NS_EVENT_BUBBLE_MASK because capture phase
|
|
|
|
// event listeners should not be fired. But it breaks at least
|
|
|
|
// <xul:dialog>'s buttons. Bug 235441.
|
2008-10-08 04:35:29 -07:00
|
|
|
item->HandleEvent(aVisitor, aFlags,
|
|
|
|
aMayHaveNewListenerManagers ||
|
2009-10-24 08:52:29 -07:00
|
|
|
createdELMs != nsEventListenerManager::sCreatedCount,
|
|
|
|
aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (aFlags & NS_EVENT_FLAG_SYSTEM_EVENT) {
|
2011-12-07 11:29:53 -08:00
|
|
|
item->PostHandleEvent(aVisitor, aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bubble
|
|
|
|
aVisitor.mEvent->flags &= ~NS_EVENT_FLAG_CAPTURE;
|
|
|
|
item = item->mParent;
|
|
|
|
while (item) {
|
2011-06-23 19:18:00 -07:00
|
|
|
nsIDOMEventTarget* newTarget = item->GetNewTarget();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (newTarget) {
|
|
|
|
// Item is at anonymous boundary. Need to retarget for the current item
|
|
|
|
// and for parent items.
|
|
|
|
aVisitor.mEvent->target = newTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(aVisitor.mEvent->flags & NS_EVENT_FLAG_CANT_BUBBLE) || newTarget) {
|
|
|
|
if ((!(aVisitor.mEvent->flags & NS_EVENT_FLAG_NO_CONTENT_DISPATCH) ||
|
|
|
|
item->ForceContentDispatch()) &&
|
|
|
|
!(aVisitor.mEvent->flags & NS_EVENT_FLAG_STOP_DISPATCH)) {
|
2008-10-08 04:35:29 -07:00
|
|
|
item->HandleEvent(aVisitor, aFlags & NS_EVENT_BUBBLE_MASK,
|
2009-10-24 08:52:29 -07:00
|
|
|
createdELMs != nsEventListenerManager::sCreatedCount,
|
|
|
|
aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (aFlags & NS_EVENT_FLAG_SYSTEM_EVENT) {
|
2011-12-07 11:29:53 -08:00
|
|
|
item->PostHandleEvent(aVisitor, aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
item = item->mParent;
|
|
|
|
}
|
|
|
|
aVisitor.mEvent->flags &= ~NS_EVENT_FLAG_BUBBLE;
|
|
|
|
|
|
|
|
if (!(aFlags & NS_EVENT_FLAG_SYSTEM_EVENT)) {
|
|
|
|
// Dispatch to the system event group. Make sure to clear the
|
2011-11-04 10:17:19 -07:00
|
|
|
// STOP_DISPATCH flag since this resets for each event group.
|
|
|
|
aVisitor.mEvent->flags &=
|
|
|
|
~(NS_EVENT_FLAG_STOP_DISPATCH | NS_EVENT_FLAG_STOP_DISPATCH_IMMEDIATELY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Setting back the original target of the event.
|
|
|
|
aVisitor.mEvent->target = aVisitor.mEvent->originalTarget;
|
|
|
|
|
|
|
|
// Special handling if PresShell (or some other caller)
|
|
|
|
// used a callback object.
|
|
|
|
if (aCallback) {
|
2011-12-07 11:29:53 -08:00
|
|
|
aPusher->Pop();
|
2007-03-22 10:30:00 -07:00
|
|
|
aCallback->HandleEvent(aVisitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retarget for system event group (which does the default handling too).
|
|
|
|
// Setting back the target which was used also for default event group.
|
|
|
|
aVisitor.mEvent->target = firstTarget;
|
|
|
|
HandleEventTargetChain(aVisitor, aFlags | NS_EVENT_FLAG_SYSTEM_EVENT,
|
2008-10-08 04:35:29 -07:00
|
|
|
aCallback,
|
2009-10-24 08:52:29 -07:00
|
|
|
createdELMs != nsEventListenerManager::sCreatedCount,
|
|
|
|
aPusher);
|
2011-11-04 10:17:19 -07:00
|
|
|
|
|
|
|
// After dispatch, clear all the propagation flags so that
|
|
|
|
// system group listeners don't affect to the event.
|
|
|
|
aVisitor.mEvent->flags &=
|
|
|
|
~(NS_EVENT_FLAG_STOP_DISPATCH | NS_EVENT_FLAG_STOP_DISPATCH_IMMEDIATELY);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-06-16 02:15:46 -07:00
|
|
|
#define NS_CHAIN_POOL_SIZE 128
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
class ChainItemPool {
|
|
|
|
public:
|
|
|
|
ChainItemPool() {
|
|
|
|
if (!sEtciPool) {
|
|
|
|
sEtciPool = new nsFixedSizeAllocator();
|
|
|
|
if (sEtciPool) {
|
|
|
|
static const size_t kBucketSizes[] = { sizeof(nsEventTargetChainItem) };
|
|
|
|
static const PRInt32 kNumBuckets = sizeof(kBucketSizes) / sizeof(size_t);
|
|
|
|
static const PRInt32 kInitialPoolSize =
|
2012-04-18 21:15:23 -07:00
|
|
|
sizeof(nsEventTargetChainItem) * NS_CHAIN_POOL_SIZE;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = sEtciPool->Init("EventTargetChainItem Pool", kBucketSizes,
|
|
|
|
kNumBuckets, kInitialPoolSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete sEtciPool;
|
2012-07-30 07:20:58 -07:00
|
|
|
sEtciPool = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sEtciPool) {
|
|
|
|
++sEtciPoolUsers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ChainItemPool() {
|
|
|
|
if (sEtciPool) {
|
|
|
|
--sEtciPoolUsers;
|
|
|
|
}
|
2009-06-16 02:15:46 -07:00
|
|
|
if (!sEtciPoolUsers) {
|
|
|
|
if (nsEventTargetChainItem::MaxEtciCount() > NS_CHAIN_POOL_SIZE) {
|
2012-07-30 07:20:58 -07:00
|
|
|
gCachedETCI = nullptr;
|
2009-06-16 02:15:46 -07:00
|
|
|
delete sEtciPool;
|
2012-07-30 07:20:58 -07:00
|
|
|
sEtciPool = nullptr;
|
2009-06-16 02:15:46 -07:00
|
|
|
nsEventTargetChainItem::ResetMaxEtciCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Shutdown()
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!sEtciPoolUsers) {
|
2012-07-30 07:20:58 -07:00
|
|
|
gCachedETCI = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
delete sEtciPool;
|
2012-07-30 07:20:58 -07:00
|
|
|
sEtciPool = nullptr;
|
2009-06-16 02:15:46 -07:00
|
|
|
nsEventTargetChainItem::ResetMaxEtciCount();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFixedSizeAllocator* GetPool() { return sEtciPool; }
|
|
|
|
|
|
|
|
static nsFixedSizeAllocator* sEtciPool;
|
|
|
|
static PRInt32 sEtciPoolUsers;
|
|
|
|
};
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsFixedSizeAllocator* ChainItemPool::sEtciPool = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 ChainItemPool::sEtciPoolUsers = 0;
|
|
|
|
|
2009-06-16 02:15:46 -07:00
|
|
|
void NS_ShutdownChainItemPool() { ChainItemPool::Shutdown(); }
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* static */ nsresult
|
|
|
|
nsEventDispatcher::Dispatch(nsISupports* aTarget,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
nsEvent* aEvent,
|
|
|
|
nsIDOMEvent* aDOMEvent,
|
|
|
|
nsEventStatus* aEventStatus,
|
2009-10-16 01:57:32 -07:00
|
|
|
nsDispatchingCallback* aCallback,
|
2011-06-23 19:18:00 -07:00
|
|
|
nsCOMArray<nsIDOMEventTarget>* aTargets)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-16 11:43:06 -07:00
|
|
|
SAMPLE_LABEL("nsEventDispatcher", "Dispatch");
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(aEvent, "Trying to dispatch without nsEvent!");
|
|
|
|
NS_ENSURE_TRUE(!NS_IS_EVENT_IN_DISPATCH(aEvent),
|
|
|
|
NS_ERROR_ILLEGAL_VALUE);
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_ASSERTION(!aTargets || !aEvent->message, "Wrong parameters!");
|
2010-04-30 12:40:59 -07:00
|
|
|
|
2011-03-24 04:34:03 -07:00
|
|
|
// If we're dispatching an already created DOMEvent object, make
|
|
|
|
// sure it is initialized!
|
|
|
|
// If aTargets is non-null, the event isn't going to be dispatched.
|
|
|
|
NS_ENSURE_TRUE(aEvent->message || !aDOMEvent || aTargets,
|
2011-10-09 11:23:13 -07:00
|
|
|
NS_ERROR_DOM_INVALID_STATE_ERR);
|
2011-03-24 04:34:03 -07:00
|
|
|
|
2010-04-30 12:40:59 -07:00
|
|
|
#ifdef NS_FUNCTION_TIMER
|
|
|
|
const char* timer_event_name = nsDOMEvent::GetEventName(aEvent->message);
|
|
|
|
NS_TIME_FUNCTION_MIN_FMT(20, "Dispatching '%s' event",
|
|
|
|
timer_event_name ? timer_event_name : "<other>");
|
|
|
|
#endif
|
|
|
|
|
2011-06-23 19:18:00 -07:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(aTarget);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool retargeted = false;
|
2011-06-19 02:23:12 -07:00
|
|
|
|
|
|
|
if (aEvent->flags & NS_EVENT_RETARGET_TO_NON_NATIVE_ANONYMOUS) {
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(target);
|
|
|
|
if (content && content->IsInNativeAnonymousSubtree()) {
|
|
|
|
nsCOMPtr<nsPIDOMEventTarget> newTarget =
|
|
|
|
do_QueryInterface(content->FindFirstNonNativeAnonymous());
|
|
|
|
NS_ENSURE_STATE(newTarget);
|
|
|
|
|
|
|
|
aEvent->originalTarget = target;
|
|
|
|
target = newTarget;
|
2011-10-17 07:59:28 -07:00
|
|
|
retargeted = true;
|
2011-06-19 02:23:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-29 04:11:02 -07:00
|
|
|
if (aEvent->flags & NS_EVENT_FLAG_ONLY_CHROME_DISPATCH) {
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aTarget);
|
|
|
|
if (!node) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aTarget);
|
|
|
|
if (win) {
|
|
|
|
node = do_QueryInterface(win->GetExtantDocument());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_STATE(node);
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = node->OwnerDoc();
|
2009-10-29 04:11:02 -07:00
|
|
|
if (!nsContentUtils::IsChromeDoc(doc)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
nsPIDOMWindow* win = doc ? doc->GetInnerWindow() : nullptr;
|
2009-10-29 04:11:02 -07:00
|
|
|
// If we can't dispatch the event to chrome, do nothing.
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIDOMEventTarget* piTarget = win ? win->GetParentTarget() : nullptr;
|
2011-03-25 06:39:58 -07:00
|
|
|
NS_ENSURE_TRUE(piTarget, NS_OK);
|
|
|
|
|
2009-10-29 04:11:02 -07:00
|
|
|
// Set the target to be the original dispatch target,
|
2010-03-31 05:44:18 -07:00
|
|
|
aEvent->target = target;
|
2011-03-25 06:39:58 -07:00
|
|
|
// but use chrome event handler or TabChildGlobal for event target chain.
|
|
|
|
target = piTarget;
|
2009-10-29 04:11:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG
|
2009-12-18 11:27:57 -08:00
|
|
|
if (!nsContentUtils::IsSafeToRunScript()) {
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
if (target->GetContextForEventHandlers(&rv) ||
|
|
|
|
NS_FAILED(rv)) {
|
2010-03-19 03:52:48 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(target);
|
2011-10-18 03:53:36 -07:00
|
|
|
if (node && nsContentUtils::IsChromeDoc(node->OwnerDoc())) {
|
2010-03-19 03:52:48 -07:00
|
|
|
NS_WARNING("Fix the caller!");
|
|
|
|
} else {
|
|
|
|
NS_ERROR("This is unsafe! Fix the caller!");
|
|
|
|
}
|
2009-12-18 11:27:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aDOMEvent) {
|
2012-06-10 16:44:50 -07:00
|
|
|
nsEvent* innerEvent = aDOMEvent->GetInternalNSEvent();
|
|
|
|
NS_ASSERTION(innerEvent == aEvent,
|
|
|
|
"The inner event of aDOMEvent is not the same as aEvent!");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool externalDOMEvent = !!(aDOMEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If we have a PresContext, make sure it doesn't die before
|
|
|
|
// event dispatching is finished.
|
2010-03-25 06:17:11 -07:00
|
|
|
nsRefPtr<nsPresContext> kungFuDeathGrip(aPresContext);
|
2007-03-22 10:30:00 -07:00
|
|
|
ChainItemPool pool;
|
|
|
|
NS_ENSURE_TRUE(pool.GetPool(), NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
// Create the event target chain item for the event target.
|
|
|
|
nsEventTargetChainItem* targetEtci =
|
2008-10-08 04:35:29 -07:00
|
|
|
nsEventTargetChainItem::Create(pool.GetPool(), target);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(targetEtci, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
if (!targetEtci->IsValid()) {
|
|
|
|
nsEventTargetChainItem::Destroy(pool.GetPool(), targetEtci);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that nsIDOMEvent::target and nsIDOMNSEvent::originalTarget
|
|
|
|
// point to the last item in the chain.
|
|
|
|
if (!aEvent->target) {
|
2007-04-17 02:18:36 -07:00
|
|
|
// Note, CurrentTarget() points always to the object returned by
|
|
|
|
// GetTargetForEventTargetChain().
|
|
|
|
aEvent->target = targetEtci->CurrentTarget();
|
|
|
|
} else {
|
|
|
|
// XXX But if the target is already set, use that. This is a hack
|
|
|
|
// for the 'load', 'beforeunload' and 'unload' events,
|
|
|
|
// which are dispatched to |window| but have document as their target.
|
|
|
|
//
|
|
|
|
// Make sure that the event target points to the right object.
|
2010-03-31 05:44:18 -07:00
|
|
|
aEvent->target = aEvent->target->GetTargetForEventTargetChain();
|
2007-04-17 02:18:36 -07:00
|
|
|
NS_ENSURE_STATE(aEvent->target);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-06-19 02:23:12 -07:00
|
|
|
|
|
|
|
if (retargeted) {
|
|
|
|
aEvent->originalTarget =
|
|
|
|
aEvent->originalTarget->GetTargetForEventTargetChain();
|
|
|
|
NS_ENSURE_STATE(aEvent->originalTarget);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aEvent->originalTarget = aEvent->target;
|
|
|
|
}
|
|
|
|
|
2008-12-03 01:26:38 -08:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aEvent->originalTarget);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isInAnon = (content && content->IsInAnonymousSubtree());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_MARK_EVENT_DISPATCH_STARTED(aEvent);
|
|
|
|
|
|
|
|
// Create visitor object and start event dispatching.
|
|
|
|
// PreHandleEvent for the original target.
|
|
|
|
nsEventStatus status = aEventStatus ? *aEventStatus : nsEventStatus_eIgnore;
|
2008-12-03 01:26:38 -08:00
|
|
|
nsEventChainPreVisitor preVisitor(aPresContext, aEvent, aDOMEvent, status,
|
|
|
|
isInAnon);
|
2007-03-22 10:30:00 -07:00
|
|
|
targetEtci->PreHandleEvent(preVisitor);
|
|
|
|
|
|
|
|
if (preVisitor.mCanHandle) {
|
|
|
|
// At least the original target can handle the event.
|
|
|
|
// Setting the retarget to the |target| simplifies retargeting code.
|
2011-06-23 19:18:01 -07:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> t = aEvent->target;
|
2008-10-08 04:35:29 -07:00
|
|
|
targetEtci->SetNewTarget(t);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventTargetChainItem* topEtci = targetEtci;
|
|
|
|
while (preVisitor.mParentTarget) {
|
|
|
|
nsEventTargetChainItem* parentEtci =
|
|
|
|
nsEventTargetChainItem::Create(pool.GetPool(), preVisitor.mParentTarget,
|
|
|
|
topEtci);
|
|
|
|
if (!parentEtci) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!parentEtci->IsValid()) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Item needs event retargetting.
|
|
|
|
if (preVisitor.mEventTargetAtParent) {
|
|
|
|
// Need to set the target of the event
|
|
|
|
// so that also the next retargeting works.
|
|
|
|
preVisitor.mEvent->target = preVisitor.mEventTargetAtParent;
|
|
|
|
parentEtci->SetNewTarget(preVisitor.mEventTargetAtParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
parentEtci->PreHandleEvent(preVisitor);
|
|
|
|
if (preVisitor.mCanHandle) {
|
|
|
|
topEtci = parentEtci;
|
|
|
|
} else {
|
|
|
|
nsEventTargetChainItem::Destroy(pool.GetPool(), parentEtci);
|
2012-07-30 07:20:58 -07:00
|
|
|
parentEtci = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-10-16 01:57:32 -07:00
|
|
|
if (aTargets) {
|
|
|
|
aTargets->Clear();
|
|
|
|
nsEventTargetChainItem* item = targetEtci;
|
|
|
|
while(item) {
|
|
|
|
aTargets->AppendObject(item->CurrentTarget()->GetTargetForDOMEvent());
|
|
|
|
item = item->mParent;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Event target chain is created. Handle the chain.
|
|
|
|
nsEventChainPostVisitor postVisitor(preVisitor);
|
2009-10-24 08:52:29 -07:00
|
|
|
nsCxPusher pusher;
|
2009-10-16 01:57:32 -07:00
|
|
|
rv = topEtci->HandleEventTargetChain(postVisitor,
|
|
|
|
NS_EVENT_FLAG_BUBBLE |
|
|
|
|
NS_EVENT_FLAG_CAPTURE,
|
|
|
|
aCallback,
|
2011-10-17 07:59:28 -07:00
|
|
|
false,
|
2009-10-24 08:52:29 -07:00
|
|
|
&pusher);
|
2009-10-16 01:57:32 -07:00
|
|
|
|
|
|
|
preVisitor.mEventStatus = postVisitor.mEventStatus;
|
|
|
|
// If the DOM event was created during event flow.
|
|
|
|
if (!preVisitor.mDOMEvent && postVisitor.mDOMEvent) {
|
|
|
|
preVisitor.mDOMEvent = postVisitor.mDOMEvent;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEventTargetChainItem::Destroy(pool.GetPool(), targetEtci);
|
2012-07-30 07:20:58 -07:00
|
|
|
targetEtci = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_MARK_EVENT_DISPATCH_DONE(aEvent);
|
|
|
|
|
|
|
|
if (!externalDOMEvent && preVisitor.mDOMEvent) {
|
|
|
|
// An nsDOMEvent was created while dispatching the event.
|
|
|
|
// Duplicate private data if someone holds a pointer to it.
|
|
|
|
nsrefcnt rc = 0;
|
|
|
|
NS_RELEASE2(preVisitor.mDOMEvent, rc);
|
2012-06-10 16:44:50 -07:00
|
|
|
if (preVisitor.mDOMEvent) {
|
|
|
|
preVisitor.mDOMEvent->DuplicatePrivateData();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aEventStatus) {
|
|
|
|
*aEventStatus = preVisitor.mEventStatus;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ nsresult
|
|
|
|
nsEventDispatcher::DispatchDOMEvent(nsISupports* aTarget,
|
|
|
|
nsEvent* aEvent,
|
|
|
|
nsIDOMEvent* aDOMEvent,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
nsEventStatus* aEventStatus)
|
|
|
|
{
|
|
|
|
if (aDOMEvent) {
|
2012-06-10 16:44:50 -07:00
|
|
|
nsEvent* innerEvent = aDOMEvent->GetInternalNSEvent();
|
|
|
|
NS_ENSURE_TRUE(innerEvent, NS_ERROR_ILLEGAL_VALUE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
bool dontResetTrusted = false;
|
|
|
|
if (innerEvent->flags & NS_EVENT_DISPATCHED) {
|
2012-07-30 07:20:58 -07:00
|
|
|
innerEvent->target = nullptr;
|
|
|
|
innerEvent->originalTarget = nullptr;
|
2012-06-10 16:44:50 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIDOMNSEvent> nsevent(do_QueryInterface(aDOMEvent));
|
|
|
|
nsevent->GetIsTrusted(&dontResetTrusted);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
if (!dontResetTrusted) {
|
|
|
|
//Check security state to determine if dispatcher is trusted
|
|
|
|
aDOMEvent->SetTrusted(nsContentUtils::IsCallerTrustedForWrite());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-10 16:44:50 -07:00
|
|
|
|
|
|
|
return nsEventDispatcher::Dispatch(aTarget, aPresContext, innerEvent,
|
|
|
|
aDOMEvent, aEventStatus);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (aEvent) {
|
|
|
|
return nsEventDispatcher::Dispatch(aTarget, aPresContext, aEvent,
|
|
|
|
aDOMEvent, aEventStatus);
|
|
|
|
}
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ nsresult
|
|
|
|
nsEventDispatcher::CreateEvent(nsPresContext* aPresContext,
|
|
|
|
nsEvent* aEvent,
|
|
|
|
const nsAString& aEventType,
|
|
|
|
nsIDOMEvent** aDOMEvent)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aDOMEvent = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aEvent) {
|
|
|
|
switch(aEvent->eventStructType) {
|
|
|
|
case NS_MUTATION_EVENT:
|
|
|
|
return NS_NewDOMMutationEvent(aDOMEvent, aPresContext,
|
2009-06-24 04:49:27 -07:00
|
|
|
static_cast<nsMutationEvent*>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_GUI_EVENT:
|
|
|
|
case NS_SCROLLPORT_EVENT:
|
2011-05-19 06:40:52 -07:00
|
|
|
case NS_UI_EVENT:
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_NewDOMUIEvent(aDOMEvent, aPresContext,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsGUIEvent*>(aEvent));
|
2009-09-11 16:13:56 -07:00
|
|
|
case NS_SCROLLAREA_EVENT:
|
|
|
|
return NS_NewDOMScrollAreaEvent(aDOMEvent, aPresContext,
|
|
|
|
static_cast<nsScrollAreaEvent *>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_KEY_EVENT:
|
|
|
|
return NS_NewDOMKeyboardEvent(aDOMEvent, aPresContext,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsKeyEvent*>(aEvent));
|
2011-09-22 02:17:40 -07:00
|
|
|
case NS_COMPOSITION_EVENT:
|
|
|
|
return NS_NewDOMCompositionEvent(
|
|
|
|
aDOMEvent, aPresContext, static_cast<nsCompositionEvent*>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_MOUSE_EVENT:
|
|
|
|
case NS_POPUP_EVENT:
|
|
|
|
return NS_NewDOMMouseEvent(aDOMEvent, aPresContext,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsInputEvent*>(aEvent));
|
2008-08-12 20:08:59 -07:00
|
|
|
case NS_MOUSE_SCROLL_EVENT:
|
|
|
|
return NS_NewDOMMouseScrollEvent(aDOMEvent, aPresContext,
|
|
|
|
static_cast<nsInputEvent*>(aEvent));
|
2008-08-27 05:07:27 -07:00
|
|
|
case NS_DRAG_EVENT:
|
|
|
|
return NS_NewDOMDragEvent(aDOMEvent, aPresContext,
|
|
|
|
static_cast<nsDragEvent*>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_TEXT_EVENT:
|
|
|
|
return NS_NewDOMTextEvent(aDOMEvent, aPresContext,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsTextEvent*>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_SVG_EVENT:
|
|
|
|
return NS_NewDOMSVGEvent(aDOMEvent, aPresContext,
|
|
|
|
aEvent);
|
|
|
|
case NS_SVGZOOM_EVENT:
|
|
|
|
return NS_NewDOMSVGZoomEvent(aDOMEvent, aPresContext,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsGUIEvent*>(aEvent));
|
2010-07-31 00:02:52 -07:00
|
|
|
case NS_SMIL_TIME_EVENT:
|
|
|
|
return NS_NewDOMTimeEvent(aDOMEvent, aPresContext, aEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case NS_COMMAND_EVENT:
|
|
|
|
return NS_NewDOMCommandEvent(aDOMEvent, aPresContext,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsCommandEvent*>(aEvent));
|
2008-10-23 13:15:20 -07:00
|
|
|
case NS_SIMPLE_GESTURE_EVENT:
|
|
|
|
return NS_NewDOMSimpleGestureEvent(aDOMEvent, aPresContext,
|
|
|
|
static_cast<nsSimpleGestureEvent*>(aEvent));
|
2010-08-02 06:34:54 -07:00
|
|
|
case NS_MOZTOUCH_EVENT:
|
|
|
|
return NS_NewDOMMozTouchEvent(aDOMEvent, aPresContext,
|
|
|
|
static_cast<nsMozTouchEvent*>(aEvent));
|
2011-12-16 16:24:11 -08:00
|
|
|
case NS_TOUCH_EVENT:
|
|
|
|
return NS_NewDOMTouchEvent(aDOMEvent, aPresContext,
|
|
|
|
static_cast<nsTouchEvent*>(aEvent));
|
2009-12-23 11:10:31 -08:00
|
|
|
case NS_TRANSITION_EVENT:
|
|
|
|
return NS_NewDOMTransitionEvent(aDOMEvent, aPresContext,
|
|
|
|
static_cast<nsTransitionEvent*>(aEvent));
|
2011-04-11 23:18:44 -07:00
|
|
|
case NS_ANIMATION_EVENT:
|
|
|
|
return NS_NewDOMAnimationEvent(aDOMEvent, aPresContext,
|
|
|
|
static_cast<nsAnimationEvent*>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// For all other types of events, create a vanilla event object.
|
|
|
|
return NS_NewDOMEvent(aDOMEvent, aPresContext, aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And if we didn't get an event, check the type argument.
|
|
|
|
|
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mouseevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("mouseevents") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("popupevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMMouseEvent(aDOMEvent, aPresContext, nullptr);
|
2008-08-12 20:08:59 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mousescrollevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMMouseScrollEvent(aDOMEvent, aPresContext, nullptr);
|
2008-08-27 05:07:27 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("dragevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("dragevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMDragEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("keyboardevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("keyevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMKeyboardEvent(aDOMEvent, aPresContext, nullptr);
|
2011-09-22 02:17:40 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("compositionevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMCompositionEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mutationevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("mutationevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMMutationEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("textevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("textevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMTextEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("popupblockedevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMPopupBlockedEvent(aDOMEvent, aPresContext, nullptr);
|
2011-04-29 16:49:20 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("deviceorientationevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMDeviceOrientationEvent(aDOMEvent, aPresContext, nullptr);
|
2011-06-19 22:36:17 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("devicemotionevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMDeviceMotionEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("uievent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("uievents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMUIEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("event") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("events") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("htmlevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("svgevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("svgevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMSVGEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("svgzoomevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("svgzoomevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMSVGZoomEvent(aDOMEvent, aPresContext, nullptr);
|
2010-07-31 00:02:52 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("timeevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("timeevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMTimeEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("xulcommandevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("xulcommandevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMXULCommandEvent(aDOMEvent, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("commandevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("commandevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMCommandEvent(aDOMEvent, aPresContext, nullptr);
|
2007-12-11 00:18:04 -08:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("datacontainerevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("datacontainerevents"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMDataContainerEvent(aDOMEvent, aPresContext, nullptr);
|
2008-01-29 17:31:29 -08:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("messageevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMMessageEvent(aDOMEvent, aPresContext, nullptr);
|
2008-07-09 01:22:20 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("progressevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMProgressEvent(aDOMEvent, aPresContext, nullptr);
|
2008-09-18 02:47:21 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("notifypaintevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMNotifyPaintEvent(aDOMEvent, aPresContext, nullptr);
|
2008-10-23 13:15:20 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("simplegestureevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMSimpleGestureEvent(aDOMEvent, aPresContext, nullptr);
|
2009-06-24 01:42:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("beforeunloadevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMBeforeUnloadEvent(aDOMEvent, aPresContext, nullptr);
|
2009-06-24 06:33:02 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("pagetransition"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMPageTransitionEvent(aDOMEvent, aPresContext, nullptr);
|
2010-08-02 06:34:54 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("moztouchevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMMozTouchEvent(aDOMEvent, aPresContext, nullptr);
|
2009-09-11 16:13:56 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("scrollareaevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMScrollAreaEvent(aDOMEvent, aPresContext, nullptr);
|
2009-12-23 11:10:31 -08:00
|
|
|
// FIXME: Should get spec to say what the right string is here! This
|
|
|
|
// is probably wrong!
|
|
|
|
if (aEventType.LowerCaseEqualsLiteral("transitionevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMTransitionEvent(aDOMEvent, aPresContext, nullptr);
|
2011-04-11 23:18:44 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("animationevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMAnimationEvent(aDOMEvent, aPresContext, nullptr);
|
2009-09-01 09:45:05 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("popstateevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMPopStateEvent(aDOMEvent, aPresContext, nullptr);
|
2010-08-25 06:10:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mozaudioavailableevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMAudioAvailableEvent(aDOMEvent, aPresContext, nullptr);
|
2010-06-17 11:36:01 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("closeevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMCloseEvent(aDOMEvent, aPresContext, nullptr);
|
2011-04-26 05:30:17 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("touchevent") &&
|
|
|
|
nsDOMTouchEvent::PrefEnabled())
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMTouchEvent(aDOMEvent, aPresContext, nullptr);
|
2011-03-31 13:30:32 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("hashchangeevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMHashChangeEvent(aDOMEvent, aPresContext, nullptr);
|
2011-05-10 23:33:11 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("customevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMCustomEvent(aDOMEvent, aPresContext, nullptr);
|
2011-11-21 08:57:26 -08:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mozsmsevent"))
|
2012-07-30 07:20:58 -07:00
|
|
|
return NS_NewDOMSmsEvent(aDOMEvent, aPresContext, nullptr);
|
2012-03-18 11:55:46 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("storageevent")) {
|
|
|
|
NS_ADDREF(*aDOMEvent = static_cast<nsDOMEvent*>(new nsDOMStorageEvent()));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|