2013-03-04 20:59:29 -08: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 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 "nsPresContext.h"
|
2008-10-08 04:35:29 -07:00
|
|
|
#include "nsEventListenerManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsMutationEvent.h"
|
2013-07-11 17:16:41 -07:00
|
|
|
#include <new>
|
2009-10-29 04:11:02 -07:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2011-04-26 05:30:17 -07:00
|
|
|
#include "nsDOMTouchEvent.h"
|
2013-03-18 07:25:50 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2012-07-20 09:42:08 -07:00
|
|
|
#include "GeneratedEvents.h"
|
2013-04-05 17:44:15 -07:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-11 18:42:34 -07:00
|
|
|
using namespace mozilla;
|
2013-04-05 17:44:15 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-08-11 18:42:34 -07:00
|
|
|
|
2013-06-28 02:53:37 -07:00
|
|
|
class ELMCreationDetector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ELMCreationDetector() :
|
|
|
|
// We can do this optimization only in the main thread.
|
2013-08-27 02:01:37 -07:00
|
|
|
mNonMainThread(!NS_IsMainThread()),
|
|
|
|
mInitialCount(mNonMainThread ?
|
|
|
|
0 : nsEventListenerManager::sMainThreadCreatedCount)
|
2013-06-28 02:53:37 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MayHaveNewListenerManager()
|
|
|
|
{
|
2013-08-27 02:01:37 -07:00
|
|
|
return mNonMainThread ||
|
2013-06-28 02:53:37 -07:00
|
|
|
mInitialCount != nsEventListenerManager::sMainThreadCreatedCount;
|
|
|
|
}
|
2013-08-27 02:01:37 -07:00
|
|
|
|
|
|
|
bool IsMainThread()
|
|
|
|
{
|
|
|
|
return !mNonMainThread;
|
|
|
|
}
|
2013-06-28 02:53:37 -07:00
|
|
|
private:
|
2013-08-27 02:01:37 -07:00
|
|
|
bool mNonMainThread;
|
2013-06-28 02:53:37 -07:00
|
|
|
uint32_t mInitialCount;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
// nsEventTargetChainItem represents a single item in the event target chain.
|
|
|
|
class nsEventTargetChainItem
|
|
|
|
{
|
|
|
|
private:
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem(EventTarget* aTarget);
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem()
|
|
|
|
: mFlags(0), mItemFlags(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-16 03:00:00 -07:00
|
|
|
static nsEventTargetChainItem* Create(nsTArray<nsEventTargetChainItem>& aChain,
|
|
|
|
EventTarget* aTarget,
|
|
|
|
nsEventTargetChainItem* aChild = nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-16 03:00:00 -07:00
|
|
|
MOZ_ASSERT(!aChild || &aChain.ElementAt(aChain.Length() - 1) == aChild);
|
|
|
|
return new (aChain.AppendElement()) nsEventTargetChainItem(aTarget);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-16 03:00:00 -07:00
|
|
|
static void DestroyLast(nsTArray<nsEventTargetChainItem>& aChain,
|
|
|
|
nsEventTargetChainItem* aItem)
|
2013-03-04 20:59:29 -08:00
|
|
|
{
|
2013-08-16 03:00:00 -07:00
|
|
|
uint32_t lastIndex = aChain.Length() - 1;
|
|
|
|
MOZ_ASSERT(&aChain[lastIndex] == aItem);
|
|
|
|
aChain.RemoveElementAt(lastIndex);
|
2013-03-04 20:59:29 -08: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);
|
|
|
|
}
|
|
|
|
|
2013-04-05 17:44:15 -07:00
|
|
|
EventTarget* GetNewTarget()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mNewTarget;
|
|
|
|
}
|
|
|
|
|
2013-04-05 17:44:15 -07:00
|
|
|
void SetNewTarget(EventTarget* 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);
|
|
|
|
}
|
|
|
|
|
2013-04-05 17:44:15 -07:00
|
|
|
EventTarget* 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.
|
|
|
|
*/
|
2013-08-16 03:00:00 -07:00
|
|
|
static void HandleEventTargetChain(nsTArray<nsEventTargetChainItem>& aChain,
|
|
|
|
nsEventChainPostVisitor& aVisitor,
|
|
|
|
nsDispatchingCallback* aCallback,
|
|
|
|
ELMCreationDetector& aCd,
|
|
|
|
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
|
|
|
*/
|
2012-12-15 17:26:05 -08:00
|
|
|
nsresult HandleEvent(nsEventChainPostVisitor& aVisitor,
|
2013-06-28 02:53:37 -07:00
|
|
|
ELMCreationDetector& aCd,
|
2009-12-18 10:50:49 -08:00
|
|
|
nsCxPusher* aPusher)
|
|
|
|
{
|
|
|
|
if (WantsWillHandleEvent()) {
|
|
|
|
mTarget->WillHandleEvent(aVisitor);
|
|
|
|
}
|
2012-12-15 17:26:03 -08:00
|
|
|
if (aVisitor.mEvent->mFlags.mPropagationStopped) {
|
2009-12-18 10:50:49 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!mManager) {
|
2013-06-28 02:53:37 -07:00
|
|
|
if (!MayHaveListenerManager() && !aCd.MayHaveNewListenerManager()) {
|
2009-12-18 10:50:49 -08:00
|
|
|
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,
|
2012-12-15 17:26:05 -08:00
|
|
|
CurrentTarget(),
|
2009-12-18 10:50:49 -08:00
|
|
|
&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
|
|
|
|
2013-04-05 17:44:15 -07:00
|
|
|
nsCOMPtr<EventTarget> mTarget;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t mFlags;
|
|
|
|
uint16_t mItemFlags;
|
2007-09-05 02:02:23 -07:00
|
|
|
nsCOMPtr<nsISupports> mItemData;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Event retargeting must happen whenever mNewTarget is non-null.
|
2013-04-05 17:44:15 -07:00
|
|
|
nsCOMPtr<EventTarget> 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;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::nsEventTargetChainItem(EventTarget* aTarget)
|
|
|
|
: mTarget(aTarget), mFlags(0), mItemFlags(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-27 16:15:29 -07:00
|
|
|
MOZ_ASSERT(!aTarget || mTarget == aTarget->GetTargetForEventTargetChain());
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-16 03:00:00 -07:00
|
|
|
void
|
2012-12-15 17:26:05 -08:00
|
|
|
nsEventTargetChainItem::HandleEventTargetChain(
|
2013-08-16 03:00:00 -07:00
|
|
|
nsTArray<nsEventTargetChainItem>& aChain,
|
2012-12-15 17:26:05 -08:00
|
|
|
nsEventChainPostVisitor& aVisitor,
|
|
|
|
nsDispatchingCallback* aCallback,
|
2013-06-28 02:53:37 -07:00
|
|
|
ELMCreationDetector& aCd,
|
2012-12-15 17:26:05 -08:00
|
|
|
nsCxPusher* aPusher)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Save the target so that it can be restored later.
|
2013-04-05 17:44:26 -07:00
|
|
|
nsCOMPtr<EventTarget> firstTarget = aVisitor.mEvent->target;
|
2013-08-16 03:00:00 -07:00
|
|
|
uint32_t chainLength = aChain.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Capture
|
2012-12-15 17:26:03 -08:00
|
|
|
aVisitor.mEvent->mFlags.mInCapturePhase = true;
|
|
|
|
aVisitor.mEvent->mFlags.mInBubblingPhase = false;
|
2013-08-16 03:00:00 -07:00
|
|
|
for (uint32_t i = chainLength - 1; i > 0; --i) {
|
|
|
|
nsEventTargetChainItem& item = aChain[i];
|
2012-12-15 17:26:05 -08:00
|
|
|
if ((!aVisitor.mEvent->mFlags.mNoContentDispatch ||
|
2013-08-16 03:00:00 -07:00
|
|
|
item.ForceContentDispatch()) &&
|
2012-12-15 17:26:03 -08:00
|
|
|
!aVisitor.mEvent->mFlags.mPropagationStopped) {
|
2013-08-16 03:00:00 -07:00
|
|
|
item.HandleEvent(aVisitor, aCd, aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-16 03:00:00 -07:00
|
|
|
if (item.GetNewTarget()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// item is at anonymous boundary. Need to retarget for the child items.
|
2013-08-16 03:00:00 -07:00
|
|
|
for (uint32_t j = i; j > 0; --j) {
|
|
|
|
uint32_t childIndex = j - 1;
|
|
|
|
EventTarget* newTarget = aChain[childIndex].GetNewTarget();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (newTarget) {
|
|
|
|
aVisitor.mEvent->target = newTarget;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Target
|
2012-12-15 17:26:03 -08:00
|
|
|
aVisitor.mEvent->mFlags.mInBubblingPhase = true;
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem& targetItem = aChain[0];
|
2012-12-15 17:26:03 -08:00
|
|
|
if (!aVisitor.mEvent->mFlags.mPropagationStopped &&
|
2012-12-15 17:26:05 -08:00
|
|
|
(!aVisitor.mEvent->mFlags.mNoContentDispatch ||
|
2013-08-16 03:00:00 -07:00
|
|
|
targetItem.ForceContentDispatch())) {
|
|
|
|
targetItem.HandleEvent(aVisitor, aCd, aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-12-15 17:26:05 -08:00
|
|
|
if (aVisitor.mEvent->mFlags.mInSystemGroup) {
|
2013-08-16 03:00:00 -07:00
|
|
|
targetItem.PostHandleEvent(aVisitor, aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bubble
|
2012-12-15 17:26:03 -08:00
|
|
|
aVisitor.mEvent->mFlags.mInCapturePhase = false;
|
2013-08-16 03:00:00 -07:00
|
|
|
for (uint32_t i = 1; i < chainLength; ++i) {
|
|
|
|
nsEventTargetChainItem& item = aChain[i];
|
|
|
|
EventTarget* 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;
|
|
|
|
}
|
|
|
|
|
2012-12-15 17:26:03 -08:00
|
|
|
if (aVisitor.mEvent->mFlags.mBubbles || newTarget) {
|
2012-12-15 17:26:05 -08:00
|
|
|
if ((!aVisitor.mEvent->mFlags.mNoContentDispatch ||
|
2013-08-16 03:00:00 -07:00
|
|
|
item.ForceContentDispatch()) &&
|
2012-12-15 17:26:03 -08:00
|
|
|
!aVisitor.mEvent->mFlags.mPropagationStopped) {
|
2013-08-16 03:00:00 -07:00
|
|
|
item.HandleEvent(aVisitor, aCd, aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-12-15 17:26:05 -08:00
|
|
|
if (aVisitor.mEvent->mFlags.mInSystemGroup) {
|
2013-08-16 03:00:00 -07:00
|
|
|
item.PostHandleEvent(aVisitor, aPusher);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-15 17:26:03 -08:00
|
|
|
aVisitor.mEvent->mFlags.mInBubblingPhase = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-15 17:26:05 -08:00
|
|
|
if (!aVisitor.mEvent->mFlags.mInSystemGroup) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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.
|
2012-12-15 17:26:03 -08:00
|
|
|
aVisitor.mEvent->mFlags.mPropagationStopped = false;
|
|
|
|
aVisitor.mEvent->mFlags.mImmediatePropagationStopped = false;
|
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;
|
2012-12-15 17:26:05 -08:00
|
|
|
aVisitor.mEvent->mFlags.mInSystemGroup = true;
|
2013-08-16 03:00:00 -07:00
|
|
|
HandleEventTargetChain(aChain,
|
|
|
|
aVisitor,
|
2008-10-08 04:35:29 -07:00
|
|
|
aCallback,
|
2013-06-28 02:53:37 -07:00
|
|
|
aCd,
|
2009-10-24 08:52:29 -07:00
|
|
|
aPusher);
|
2012-12-15 17:26:05 -08:00
|
|
|
aVisitor.mEvent->mFlags.mInSystemGroup = false;
|
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.
|
2012-12-15 17:26:03 -08:00
|
|
|
aVisitor.mEvent->mFlags.mPropagationStopped = false;
|
|
|
|
aVisitor.mEvent->mFlags.mImmediatePropagationStopped = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-08-10 02:24:09 -07:00
|
|
|
}
|
|
|
|
|
2013-08-27 02:01:37 -07:00
|
|
|
static nsTArray<nsEventTargetChainItem>* sCachedMainThreadChain = nullptr;
|
|
|
|
|
|
|
|
void
|
|
|
|
NS_ShutdownEventTargetChainRecycler()
|
|
|
|
{
|
|
|
|
delete sCachedMainThreadChain;
|
|
|
|
sCachedMainThreadChain = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-01-03 07:17:36 -08:00
|
|
|
nsEventTargetChainItem*
|
2013-08-16 03:00:00 -07:00
|
|
|
EventTargetChainItemForChromeTarget(nsTArray<nsEventTargetChainItem>& aChain,
|
|
|
|
nsINode* aNode,
|
2013-01-03 07:17:36 -08:00
|
|
|
nsEventTargetChainItem* aChild = nullptr)
|
|
|
|
{
|
|
|
|
if (!aNode->IsInDoc()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsPIDOMWindow* win = aNode->OwnerDoc()->GetInnerWindow();
|
2013-04-05 17:44:15 -07:00
|
|
|
EventTarget* piTarget = win ? win->GetParentTarget() : nullptr;
|
2013-01-03 07:17:36 -08:00
|
|
|
NS_ENSURE_TRUE(piTarget, nullptr);
|
|
|
|
|
|
|
|
nsEventTargetChainItem* etci =
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::Create(aChain,
|
|
|
|
piTarget->GetTargetForEventTargetChain(),
|
2013-01-03 07:17:36 -08:00
|
|
|
aChild);
|
|
|
|
if (!etci->IsValid()) {
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::DestroyLast(aChain, etci);
|
2013-01-03 07:17:36 -08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return etci;
|
|
|
|
}
|
|
|
|
|
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,
|
2013-04-05 17:44:26 -07:00
|
|
|
nsCOMArray<EventTarget>* aTargets)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("nsEventDispatcher", "Dispatch");
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(aEvent, "Trying to dispatch without nsEvent!");
|
2012-12-15 17:26:04 -08:00
|
|
|
NS_ENSURE_TRUE(!aEvent->mFlags.mIsBeingDispatched,
|
2013-04-02 07:10:45 -07:00
|
|
|
NS_ERROR_DOM_INVALID_STATE_ERR);
|
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
|
|
|
|
2013-04-05 17:44:15 -07:00
|
|
|
nsCOMPtr<EventTarget> 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
|
|
|
|
2012-12-15 17:26:05 -08:00
|
|
|
if (aEvent->mFlags.mRetargetToNonNativeAnonymous) {
|
2011-06-19 02:23:12 -07:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(target);
|
|
|
|
if (content && content->IsInNativeAnonymousSubtree()) {
|
2013-04-05 17:44:15 -07:00
|
|
|
nsCOMPtr<EventTarget> newTarget =
|
2012-10-10 12:04:42 -07:00
|
|
|
do_QueryInterface(content->FindFirstNonChromeOnlyAccessContent());
|
2011-06-19 02:23:12 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 17:26:05 -08:00
|
|
|
if (aEvent->mFlags.mOnlyChromeDispatch) {
|
2009-10-29 04:11:02 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aTarget);
|
|
|
|
if (!node) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aTarget);
|
|
|
|
if (win) {
|
2013-04-23 21:22:37 -07:00
|
|
|
node = win->GetExtantDoc();
|
2009-10-29 04:11:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2013-04-05 17:44:15 -07:00
|
|
|
EventTarget* piTarget = win ? win->GetParentTarget() : nullptr;
|
2011-03-25 06:39:58 -07:00
|
|
|
NS_ENSURE_TRUE(piTarget, NS_OK);
|
2013-04-05 17:44:15 -07:00
|
|
|
|
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
|
|
|
|
2013-08-27 02:01:37 -07:00
|
|
|
ELMCreationDetector cd;
|
|
|
|
nsTArray<nsEventTargetChainItem> chain;
|
|
|
|
if (cd.IsMainThread()) {
|
|
|
|
if (!sCachedMainThreadChain) {
|
|
|
|
sCachedMainThreadChain = new nsTArray<nsEventTargetChainItem>();
|
|
|
|
}
|
|
|
|
chain.SwapElements(*sCachedMainThreadChain);
|
|
|
|
chain.SetCapacity(128);
|
|
|
|
}
|
2013-08-16 03:00:00 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Create the event target chain item for the event target.
|
|
|
|
nsEventTargetChainItem* targetEtci =
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::Create(chain, target->GetTargetForEventTargetChain());
|
2013-08-19 14:12:56 -07:00
|
|
|
MOZ_ASSERT(&chain[0] == targetEtci);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!targetEtci->IsValid()) {
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::DestroyLast(chain, targetEtci);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-04 00:44:00 -07:00
|
|
|
// Make sure that nsIDOMEvent::target and nsIDOMEvent::originalTarget
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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
|
|
|
|
2012-12-15 17:26:04 -08:00
|
|
|
aEvent->mFlags.mIsBeingDispatched = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2013-01-03 07:17:36 -08:00
|
|
|
if (!preVisitor.mCanHandle && preVisitor.mAutomaticChromeDispatch && content) {
|
|
|
|
// Event target couldn't handle the event. Try to propagate to chrome.
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::DestroyLast(chain, targetEtci);
|
|
|
|
targetEtci = EventTargetChainItemForChromeTarget(chain, content);
|
2013-01-03 07:17:36 -08:00
|
|
|
NS_ENSURE_STATE(targetEtci);
|
2013-08-19 14:12:56 -07:00
|
|
|
MOZ_ASSERT(&chain[0] == targetEtci);
|
2013-01-03 07:17:36 -08:00
|
|
|
targetEtci->PreHandleEvent(preVisitor);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (preVisitor.mCanHandle) {
|
|
|
|
// At least the original target can handle the event.
|
|
|
|
// Setting the retarget to the |target| simplifies retargeting code.
|
2013-04-05 17:44:15 -07:00
|
|
|
nsCOMPtr<EventTarget> t = do_QueryInterface(aEvent->target);
|
2008-10-08 04:35:29 -07:00
|
|
|
targetEtci->SetNewTarget(t);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventTargetChainItem* topEtci = targetEtci;
|
2013-08-19 14:12:56 -07:00
|
|
|
targetEtci = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (preVisitor.mParentTarget) {
|
2013-04-05 17:44:15 -07:00
|
|
|
EventTarget* parentTarget = preVisitor.mParentTarget;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventTargetChainItem* parentEtci =
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::Create(chain, preVisitor.mParentTarget, topEtci);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!parentEtci->IsValid()) {
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::DestroyLast(chain, parentEtci);
|
2007-03-22 10:30:00 -07:00
|
|
|
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 {
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::DestroyLast(chain, parentEtci);
|
2012-07-30 07:20:58 -07:00
|
|
|
parentEtci = nullptr;
|
2013-01-03 07:17:36 -08:00
|
|
|
if (preVisitor.mAutomaticChromeDispatch && content) {
|
|
|
|
// Even if the current target can't handle the event, try to
|
|
|
|
// propagate to chrome.
|
|
|
|
nsCOMPtr<nsINode> disabledTarget = do_QueryInterface(parentTarget);
|
|
|
|
if (disabledTarget) {
|
2013-08-16 03:00:00 -07:00
|
|
|
parentEtci = EventTargetChainItemForChromeTarget(chain,
|
|
|
|
disabledTarget,
|
2013-01-03 07:17:36 -08:00
|
|
|
topEtci);
|
|
|
|
if (parentEtci) {
|
|
|
|
parentEtci->PreHandleEvent(preVisitor);
|
|
|
|
if (preVisitor.mCanHandle) {
|
2013-08-19 14:12:56 -07:00
|
|
|
chain[0].SetNewTarget(parentTarget);
|
2013-01-03 07:17:36 -08:00
|
|
|
topEtci = parentEtci;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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();
|
2013-08-16 03:00:00 -07:00
|
|
|
aTargets->SetCapacity(chain.Length());
|
|
|
|
for (uint32_t i = 0; i < chain.Length(); ++i) {
|
|
|
|
aTargets->AppendObject(chain[i].CurrentTarget()->GetTargetForDOMEvent());
|
2009-10-16 01:57:32 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Event target chain is created. Handle the chain.
|
|
|
|
nsEventChainPostVisitor postVisitor(preVisitor);
|
2009-10-24 08:52:29 -07:00
|
|
|
nsCxPusher pusher;
|
2013-08-16 03:00:00 -07:00
|
|
|
nsEventTargetChainItem::HandleEventTargetChain(chain,
|
|
|
|
postVisitor,
|
|
|
|
aCallback,
|
|
|
|
cd,
|
|
|
|
&pusher);
|
2013-03-04 20:59:29 -08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-16 03:00:00 -07:00
|
|
|
// Note, nsEventTargetChainItem objects are deleted when the chain goes out of
|
|
|
|
// the scope.
|
|
|
|
|
2012-12-15 17:26:04 -08:00
|
|
|
aEvent->mFlags.mIsBeingDispatched = false;
|
|
|
|
aEvent->mFlags.mDispatchedAtLeastOnce = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-08-27 02:01:37 -07:00
|
|
|
|
|
|
|
if (cd.IsMainThread() && chain.Capacity() == 128 && sCachedMainThreadChain) {
|
|
|
|
chain.ClearAndRetainStorage();
|
|
|
|
chain.SwapElements(*sCachedMainThreadChain);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2012-12-15 17:26:04 -08:00
|
|
|
if (innerEvent->mFlags.mDispatchedAtLeastOnce) {
|
2012-07-30 07:20:58 -07:00
|
|
|
innerEvent->target = nullptr;
|
|
|
|
innerEvent->originalTarget = nullptr;
|
2012-08-04 00:44:00 -07:00
|
|
|
} else {
|
|
|
|
aDOMEvent->GetIsTrusted(&dontResetTrusted);
|
2012-06-10 16:44:50 -07:00
|
|
|
}
|
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
|
2012-10-25 16:10:54 -07:00
|
|
|
aDOMEvent->SetTrusted(nsContentUtils::IsCallerChrome());
|
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
|
2013-03-09 03:34:29 -08:00
|
|
|
nsEventDispatcher::CreateEvent(mozilla::dom::EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
2007-03-22 10:30:00 -07:00
|
|
|
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:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMMutationEvent(aDOMEvent, aOwner, 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:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMUIEvent(aDOMEvent, aOwner, 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:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMScrollAreaEvent(aDOMEvent, aOwner, aPresContext,
|
2009-09-11 16:13:56 -07:00
|
|
|
static_cast<nsScrollAreaEvent *>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_KEY_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMKeyboardEvent(aDOMEvent, aOwner, 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(
|
2013-03-09 03:34:29 -08:00
|
|
|
aDOMEvent, aOwner,
|
|
|
|
aPresContext, static_cast<nsCompositionEvent*>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_MOUSE_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMMouseEvent(aDOMEvent, aOwner, aPresContext,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsInputEvent*>(aEvent));
|
2013-06-05 05:02:51 -07:00
|
|
|
case NS_FOCUS_EVENT:
|
|
|
|
return NS_NewDOMFocusEvent(aDOMEvent, aOwner, aPresContext,
|
|
|
|
static_cast<nsFocusEvent*>(aEvent));
|
2008-08-12 20:08:59 -07:00
|
|
|
case NS_MOUSE_SCROLL_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMMouseScrollEvent(aDOMEvent, aOwner, aPresContext,
|
2008-08-12 20:08:59 -07:00
|
|
|
static_cast<nsInputEvent*>(aEvent));
|
2012-08-11 18:42:34 -07:00
|
|
|
case NS_WHEEL_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMWheelEvent(aDOMEvent, aOwner, aPresContext,
|
2012-08-11 18:42:34 -07:00
|
|
|
static_cast<widget::WheelEvent*>(aEvent));
|
2008-08-27 05:07:27 -07:00
|
|
|
case NS_DRAG_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMDragEvent(aDOMEvent, aOwner, aPresContext,
|
2008-08-27 05:07:27 -07:00
|
|
|
static_cast<nsDragEvent*>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_TEXT_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMTextEvent(aDOMEvent, aOwner, aPresContext,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsTextEvent*>(aEvent));
|
2013-03-11 08:49:48 -07:00
|
|
|
case NS_CLIPBOARD_EVENT:
|
2013-03-11 08:49:56 -07:00
|
|
|
return NS_NewDOMClipboardEvent(aDOMEvent, aOwner, aPresContext,
|
2013-03-11 08:49:48 -07:00
|
|
|
static_cast<nsClipboardEvent*>(aEvent));
|
2007-03-22 10:30:00 -07:00
|
|
|
case NS_SVGZOOM_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMSVGZoomEvent(aDOMEvent, aOwner, 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:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMTimeEvent(aDOMEvent, aOwner, aPresContext, aEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case NS_COMMAND_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMCommandEvent(aDOMEvent, aOwner, 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:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMSimpleGestureEvent(aDOMEvent, aOwner, aPresContext,
|
2008-10-23 13:15:20 -07:00
|
|
|
static_cast<nsSimpleGestureEvent*>(aEvent));
|
2011-12-16 16:24:11 -08:00
|
|
|
case NS_TOUCH_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMTouchEvent(aDOMEvent, aOwner, aPresContext,
|
2011-12-16 16:24:11 -08:00
|
|
|
static_cast<nsTouchEvent*>(aEvent));
|
2009-12-23 11:10:31 -08:00
|
|
|
case NS_TRANSITION_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMTransitionEvent(aDOMEvent, aOwner, aPresContext,
|
2009-12-23 11:10:31 -08:00
|
|
|
static_cast<nsTransitionEvent*>(aEvent));
|
2011-04-11 23:18:44 -07:00
|
|
|
case NS_ANIMATION_EVENT:
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMAnimationEvent(aDOMEvent, aOwner, aPresContext,
|
2011-04-11 23:18:44 -07:00
|
|
|
static_cast<nsAnimationEvent*>(aEvent));
|
2012-11-19 22:05:56 -08:00
|
|
|
default:
|
|
|
|
// For all other types of events, create a vanilla event object.
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMEvent(aDOMEvent, aOwner, aPresContext, aEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And if we didn't get an event, check the type argument.
|
|
|
|
|
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mouseevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("mouseevents") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("popupevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMMouseEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2008-08-12 20:08:59 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mousescrollevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMMouseScrollEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2008-08-27 05:07:27 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("dragevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("dragevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMDragEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("keyboardevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("keyevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMKeyboardEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2011-09-22 02:17:40 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("compositionevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMCompositionEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mutationevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("mutationevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMMutationEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("textevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("textevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMTextEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("popupblockedevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMPopupBlockedEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2011-04-29 16:49:20 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("deviceorientationevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMDeviceOrientationEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2011-06-19 22:36:17 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("devicemotionevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMDeviceMotionEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("uievent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("uievents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMUIEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("event") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("events") ||
|
2013-03-11 11:57:08 -07:00
|
|
|
aEventType.LowerCaseEqualsLiteral("htmlevents") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("svgevent") ||
|
2007-03-22 10:30:00 -07:00
|
|
|
aEventType.LowerCaseEqualsLiteral("svgevents"))
|
2013-03-11 11:57:08 -07:00
|
|
|
return NS_NewDOMEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("svgzoomevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("svgzoomevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMSVGZoomEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2010-07-31 00:02:52 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("timeevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("timeevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMTimeEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("xulcommandevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("xulcommandevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMXULCommandEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("commandevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("commandevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMCommandEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2012-11-01 11:18:08 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("elementreplace"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMElementReplaceEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2007-12-11 00:18:04 -08:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("datacontainerevent") ||
|
|
|
|
aEventType.LowerCaseEqualsLiteral("datacontainerevents"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMDataContainerEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2008-01-29 17:31:29 -08:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("messageevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMMessageEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2008-09-18 02:47:21 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("notifypaintevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMNotifyPaintEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2008-10-23 13:15:20 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("simplegestureevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMSimpleGestureEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2009-06-24 01:42:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("beforeunloadevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMBeforeUnloadEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2009-06-24 06:33:02 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("pagetransition"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMPageTransitionEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2013-01-03 22:54:26 -08:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("domtransaction"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMDOMTransactionEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2009-09-11 16:13:56 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("scrollareaevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMScrollAreaEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2009-09-01 09:45:05 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("popstateevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMPopStateEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2010-08-25 06:10:00 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mozaudioavailableevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMAudioAvailableEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2010-06-17 11:36:01 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("closeevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMCloseEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2011-04-26 05:30:17 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("touchevent") &&
|
|
|
|
nsDOMTouchEvent::PrefEnabled())
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMTouchEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2011-03-31 13:30:32 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("hashchangeevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMHashChangeEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2011-05-10 23:33:11 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("customevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMCustomEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2011-11-21 08:57:26 -08:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mozsmsevent"))
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMMozSmsEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2013-03-08 23:22:42 -08:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("mozmmsevent"))
|
|
|
|
return NS_NewDOMMozMmsEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2012-03-18 11:55:46 -07:00
|
|
|
if (aEventType.LowerCaseEqualsLiteral("storageevent")) {
|
2013-03-09 03:34:29 -08:00
|
|
|
return NS_NewDOMStorageEvent(aDOMEvent, aOwner, aPresContext, nullptr);
|
2012-03-18 11:55:46 -07:00
|
|
|
}
|
2013-05-08 13:45:35 -07:00
|
|
|
// NEW EVENT TYPES SHOULD NOT BE ADDED HERE; THEY SHOULD USE ONLY EVENT
|
|
|
|
// CONSTRUCTORS
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|