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
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
// Microsoft's API Name hackery sucks
|
|
|
|
#undef CreateEvent
|
|
|
|
|
2014-06-21 11:55:15 -07:00
|
|
|
#include "mozilla/AddonPathService.h"
|
2013-09-25 04:21:22 -07:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2014-06-04 16:48:50 -07:00
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2014-03-17 21:48:21 -07:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2013-08-29 14:18:25 -07:00
|
|
|
#ifdef MOZ_B2G
|
2012-03-24 17:29:49 -07:00
|
|
|
#include "mozilla/Hal.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#endif // #ifdef MOZ_B2G
|
2012-03-24 17:29:49 -07:00
|
|
|
#include "mozilla/HalSensor.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "mozilla/InternalMutationEvent.h"
|
2014-04-01 19:00:45 -07:00
|
|
|
#include "mozilla/JSEventHandler.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/Event.h"
|
|
|
|
|
|
|
|
#include "EventListenerService.h"
|
|
|
|
#include "nsCOMArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDOMCID.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocument.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsISupports.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsJSUtils.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "nsNameSpaceManager.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2012-08-20 11:34:32 -07:00
|
|
|
#include "nsSandboxFlags.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "xpcpublic.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
using namespace dom;
|
|
|
|
using namespace hal;
|
2010-04-30 06:12:05 -07:00
|
|
|
|
2013-08-16 03:06:24 -07:00
|
|
|
#define EVENT_TYPE_EQUALS(ls, type, userType, typeString, allEvents) \
|
|
|
|
((ls->mEventType == type && \
|
|
|
|
(ls->mEventType != NS_USER_DEFINED_EVENT || \
|
|
|
|
(mIsMainThreadELM && ls->mTypeAtom == userType) || \
|
|
|
|
(!mIsMainThreadELM && ls->mTypeString.Equals(typeString)))) || \
|
2012-11-01 05:42:29 -07:00
|
|
|
(allEvents && ls->mAllEvents))
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t kAllMutationBits =
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_EVENT_BITS_MUTATION_SUBTREEMODIFIED |
|
|
|
|
NS_EVENT_BITS_MUTATION_NODEINSERTED |
|
|
|
|
NS_EVENT_BITS_MUTATION_NODEREMOVED |
|
|
|
|
NS_EVENT_BITS_MUTATION_NODEREMOVEDFROMDOCUMENT |
|
|
|
|
NS_EVENT_BITS_MUTATION_NODEINSERTEDINTODOCUMENT |
|
|
|
|
NS_EVENT_BITS_MUTATION_ATTRMODIFIED |
|
|
|
|
NS_EVENT_BITS_MUTATION_CHARACTERDATAMODIFIED;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t
|
|
|
|
MutationBitForEventType(uint32_t aEventType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
switch (aEventType) {
|
|
|
|
case NS_MUTATION_SUBTREEMODIFIED:
|
|
|
|
return NS_EVENT_BITS_MUTATION_SUBTREEMODIFIED;
|
|
|
|
case NS_MUTATION_NODEINSERTED:
|
|
|
|
return NS_EVENT_BITS_MUTATION_NODEINSERTED;
|
|
|
|
case NS_MUTATION_NODEREMOVED:
|
|
|
|
return NS_EVENT_BITS_MUTATION_NODEREMOVED;
|
|
|
|
case NS_MUTATION_NODEREMOVEDFROMDOCUMENT:
|
|
|
|
return NS_EVENT_BITS_MUTATION_NODEREMOVEDFROMDOCUMENT;
|
|
|
|
case NS_MUTATION_NODEINSERTEDINTODOCUMENT:
|
|
|
|
return NS_EVENT_BITS_MUTATION_NODEINSERTEDINTODOCUMENT;
|
|
|
|
case NS_MUTATION_ATTRMODIFIED:
|
|
|
|
return NS_EVENT_BITS_MUTATION_ATTRMODIFIED;
|
|
|
|
case NS_MUTATION_CHARACTERDATAMODIFIED:
|
|
|
|
return NS_EVENT_BITS_MUTATION_CHARACTERDATAMODIFIED;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
uint32_t EventListenerManager::sMainThreadCreatedCount = 0;
|
|
|
|
|
|
|
|
EventListenerManager::EventListenerManager(EventTarget* aTarget)
|
|
|
|
: mMayHavePaintEventListener(false)
|
|
|
|
, mMayHaveMutationListeners(false)
|
|
|
|
, mMayHaveCapturingListeners(false)
|
|
|
|
, mMayHaveSystemGroupListeners(false)
|
|
|
|
, mMayHaveTouchEventListener(false)
|
|
|
|
, mMayHaveMouseEnterLeaveEventListener(false)
|
|
|
|
, mMayHavePointerEnterLeaveEventListener(false)
|
|
|
|
, mClearingListeners(false)
|
|
|
|
, mIsMainThreadELM(NS_IsMainThread())
|
|
|
|
, mNoListenerForEvent(0)
|
|
|
|
, mTarget(aTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-06-23 19:18:01 -07:00
|
|
|
NS_ASSERTION(aTarget, "unexpected null pointer");
|
|
|
|
|
2013-08-16 03:06:24 -07:00
|
|
|
if (mIsMainThreadELM) {
|
2013-06-28 02:53:37 -07:00
|
|
|
++sMainThreadCreatedCount;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::~EventListenerManager()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-11 06:59:00 -07:00
|
|
|
// If your code fails this assertion, a possible reason is that
|
|
|
|
// a class did not call our Disconnect() manually. Note that
|
|
|
|
// this class can have Disconnect called in one of two ways:
|
|
|
|
// if it is part of a cycle, then in Unlink() (such a cycle
|
|
|
|
// would be with one of the listeners, not mTarget which is weak).
|
|
|
|
// If not part of a cycle, then Disconnect must be called manually,
|
|
|
|
// typically from the destructor of the owner class (mTarget).
|
|
|
|
// XXX azakai: Is there any reason to not just call Disconnect
|
|
|
|
// from right here, if not previously called?
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(!mTarget, "didn't call Disconnect");
|
|
|
|
RemoveAllListeners();
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::RemoveAllListeners()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-30 01:13:16 -08:00
|
|
|
if (mClearingListeners) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mClearingListeners = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
mListeners.Clear();
|
2012-11-30 01:13:16 -08:00
|
|
|
mClearingListeners = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::Shutdown()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-03-04 16:37:43 -08:00
|
|
|
Event::Shutdown();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(EventListenerManager, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(EventListenerManager, Release)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-23 11:13:39 -08:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::Listener& aField,
|
2012-11-23 11:13:39 -08:00
|
|
|
const char* aName,
|
|
|
|
unsigned aFlags)
|
|
|
|
{
|
2013-07-30 15:04:38 -07:00
|
|
|
if (MOZ_UNLIKELY(aCallback.WantDebugInfo())) {
|
|
|
|
nsAutoCString name;
|
|
|
|
name.AppendASCII(aName);
|
|
|
|
if (aField.mTypeAtom) {
|
|
|
|
name.AppendASCII(" event=");
|
|
|
|
name.Append(nsAtomCString(aField.mTypeAtom));
|
|
|
|
name.AppendASCII(" listenerType=");
|
|
|
|
name.AppendInt(aField.mListenerType);
|
|
|
|
name.AppendASCII(" ");
|
|
|
|
}
|
|
|
|
CycleCollectionNoteChild(aCallback, aField.mListener.GetISupports(), name.get(),
|
|
|
|
aFlags);
|
|
|
|
} else {
|
|
|
|
CycleCollectionNoteChild(aCallback, aField.mListener.GetISupports(), aName,
|
|
|
|
aFlags);
|
|
|
|
}
|
2012-11-23 11:13:39 -08:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(EventListenerManager)
|
2013-08-01 18:29:05 -07:00
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(EventListenerManager)
|
2012-11-23 11:13:39 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mListeners)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(EventListenerManager)
|
2007-03-22 10:30:00 -07:00
|
|
|
tmp->Disconnect();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
|
2008-10-15 14:06:32 -07:00
|
|
|
nsPIDOMWindow*
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::GetInnerWindowForTarget()
|
2008-10-15 14:06:32 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(mTarget);
|
|
|
|
if (node) {
|
|
|
|
// XXX sXBL/XBL2 issue -- do we really want the owner here? What
|
|
|
|
// if that's the XBL document?
|
2011-10-18 04:19:44 -07:00
|
|
|
return node->OwnerDoc()->GetInnerWindow();
|
2008-10-15 14:06:32 -07:00
|
|
|
}
|
|
|
|
|
2012-10-01 02:43:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTargetAsInnerWindow();
|
|
|
|
return window;
|
2008-10-15 14:06:32 -07:00
|
|
|
}
|
|
|
|
|
2012-09-30 06:47:48 -07:00
|
|
|
already_AddRefed<nsPIDOMWindow>
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::GetTargetAsInnerWindow() const
|
2012-09-30 06:47:48 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(mTarget);
|
|
|
|
if (!window) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(window->IsInnerWindow(), "Target should not be an outer window");
|
|
|
|
return window.forget();
|
|
|
|
}
|
|
|
|
|
2011-08-18 02:45:00 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::AddEventListenerInternal(
|
2014-03-16 23:56:53 -07:00
|
|
|
const EventListenerHolder& aListenerHolder,
|
2014-03-16 23:56:53 -07:00
|
|
|
uint32_t aType,
|
|
|
|
nsIAtom* aTypeAtom,
|
|
|
|
const nsAString& aTypeString,
|
|
|
|
const EventListenerFlags& aFlags,
|
|
|
|
bool aHandler,
|
|
|
|
bool aAllEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-16 03:06:24 -07:00
|
|
|
MOZ_ASSERT((NS_IsMainThread() && aType && aTypeAtom) || // Main thread
|
|
|
|
(!NS_IsMainThread() && aType && !aTypeString.IsEmpty()) || // non-main-thread
|
|
|
|
aAllEvents, "Missing type"); // all-events listener
|
2011-08-18 02:45:00 -07:00
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
if (!aListenerHolder || mClearingListeners) {
|
2011-08-18 02:45:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-16 14:16:08 -07:00
|
|
|
// Since there is no public API to call us with an EventListenerHolder, we
|
|
|
|
// know that there's an EventListenerHolder on the stack holding a strong ref
|
|
|
|
// to the listener.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2014-03-16 23:56:51 -07:00
|
|
|
listener = &mListeners.ElementAt(i);
|
2014-03-16 23:56:53 -07:00
|
|
|
// mListener == aListenerHolder is the last one, since it can be a bit slow.
|
2014-03-16 23:56:51 -07:00
|
|
|
if (listener->mListenerIsHandler == aHandler &&
|
|
|
|
listener->mFlags == aFlags &&
|
|
|
|
EVENT_TYPE_EQUALS(listener, aType, aTypeAtom, aTypeString,
|
|
|
|
aAllEvents) &&
|
2014-03-16 23:56:53 -07:00
|
|
|
listener->mListener == aListenerHolder) {
|
2011-08-18 02:45:00 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 03:04:16 -07:00
|
|
|
mNoListenerForEvent = NS_EVENT_NULL;
|
2012-07-30 07:20:58 -07:00
|
|
|
mNoListenerForEventAtom = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
listener = aAllEvents ? mListeners.InsertElementAt(0) :
|
|
|
|
mListeners.AppendElement();
|
2014-03-16 23:56:53 -07:00
|
|
|
listener->mListener = aListenerHolder;
|
2013-08-16 03:06:24 -07:00
|
|
|
MOZ_ASSERT(aType < PR_UINT16_MAX);
|
2014-03-16 23:56:51 -07:00
|
|
|
listener->mEventType = aType;
|
|
|
|
listener->mTypeString = aTypeString;
|
|
|
|
listener->mTypeAtom = aTypeAtom;
|
|
|
|
listener->mFlags = aFlags;
|
|
|
|
listener->mListenerIsHandler = aHandler;
|
|
|
|
listener->mHandlerIsString = false;
|
|
|
|
listener->mAllEvents = aAllEvents;
|
2011-06-23 19:18:02 -07:00
|
|
|
|
2012-06-28 14:47:56 -07:00
|
|
|
// Detect the type of event listener.
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> wjs;
|
2012-12-15 17:26:05 -08:00
|
|
|
if (aFlags.mListenerIsJSListener) {
|
2014-03-16 23:56:53 -07:00
|
|
|
MOZ_ASSERT(!aListenerHolder.HasWebIDLCallback());
|
2014-03-16 23:56:51 -07:00
|
|
|
listener->mListenerType = Listener::eJSEventListener;
|
2014-03-16 23:56:53 -07:00
|
|
|
} else if (aListenerHolder.HasWebIDLCallback()) {
|
2014-03-16 23:56:51 -07:00
|
|
|
listener->mListenerType = Listener::eWebIDLListener;
|
2014-03-16 23:56:53 -07:00
|
|
|
} else if ((wjs = do_QueryInterface(aListenerHolder.GetXPCOMCallback()))) {
|
2014-03-16 23:56:51 -07:00
|
|
|
listener->mListenerType = Listener::eWrappedJSListener;
|
2012-06-28 14:47:56 -07:00
|
|
|
} else {
|
2014-03-16 23:56:51 -07:00
|
|
|
listener->mListenerType = Listener::eNativeListener;
|
2012-01-26 06:45:12 -08:00
|
|
|
}
|
2012-06-28 14:47:56 -07:00
|
|
|
|
|
|
|
|
2012-12-15 17:26:05 -08:00
|
|
|
if (aFlags.mInSystemGroup) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mMayHaveSystemGroupListeners = true;
|
2011-06-23 19:18:02 -07:00
|
|
|
}
|
2012-12-15 17:26:05 -08:00
|
|
|
if (aFlags.mCapture) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mMayHaveCapturingListeners = true;
|
2009-10-24 08:52:29 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-15 14:06:32 -07:00
|
|
|
if (aType == NS_AFTERPAINT) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mMayHavePaintEventListener = true;
|
2008-10-15 14:06:32 -07:00
|
|
|
nsPIDOMWindow* window = GetInnerWindowForTarget();
|
|
|
|
if (window) {
|
2008-10-15 14:24:19 -07:00
|
|
|
window->SetHasPaintEventListeners();
|
2008-10-15 14:06:32 -07:00
|
|
|
}
|
|
|
|
} else if (aType >= NS_MUTATION_START && aType <= NS_MUTATION_END) {
|
|
|
|
// For mutation listeners, we need to update the global bit on the DOM window.
|
|
|
|
// Otherwise we won't actually fire the mutation event.
|
2011-10-17 07:59:28 -07:00
|
|
|
mMayHaveMutationListeners = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
// Go from our target to the nearest enclosing DOM window.
|
2008-10-15 14:06:32 -07:00
|
|
|
nsPIDOMWindow* window = GetInnerWindowForTarget();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (window) {
|
2013-04-23 21:22:37 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
|
2012-05-23 03:45:14 -07:00
|
|
|
if (doc) {
|
|
|
|
doc->WarnOnceAbout(nsIDocument::eMutationEvent);
|
|
|
|
}
|
2007-03-24 05:18:02 -07:00
|
|
|
// If aType is NS_MUTATION_SUBTREEMODIFIED, we need to listen all
|
2008-04-11 15:44:48 -07:00
|
|
|
// mutations. nsContentUtils::HasMutationListeners relies on this.
|
2007-03-24 05:18:02 -07:00
|
|
|
window->SetMutationListeners((aType == NS_MUTATION_SUBTREEMODIFIED) ?
|
|
|
|
kAllMutationBits :
|
|
|
|
MutationBitForEventType(aType));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-03-24 17:29:49 -07:00
|
|
|
} else if (aTypeAtom == nsGkAtoms::ondeviceorientation) {
|
2012-04-04 09:26:12 -07:00
|
|
|
EnableDevice(NS_DEVICE_ORIENTATION);
|
2012-05-16 13:27:54 -07:00
|
|
|
} else if (aTypeAtom == nsGkAtoms::ondeviceproximity || aTypeAtom == nsGkAtoms::onuserproximity) {
|
2012-05-02 09:43:45 -07:00
|
|
|
EnableDevice(NS_DEVICE_PROXIMITY);
|
2012-05-02 09:43:45 -07:00
|
|
|
} else if (aTypeAtom == nsGkAtoms::ondevicelight) {
|
|
|
|
EnableDevice(NS_DEVICE_LIGHT);
|
2012-03-24 17:29:49 -07:00
|
|
|
} else if (aTypeAtom == nsGkAtoms::ondevicemotion) {
|
2012-04-04 09:26:12 -07:00
|
|
|
EnableDevice(NS_DEVICE_MOTION);
|
2012-11-07 10:10:40 -08:00
|
|
|
#ifdef MOZ_B2G
|
2012-09-10 23:05:00 -07:00
|
|
|
} else if (aTypeAtom == nsGkAtoms::onmoztimechange) {
|
2012-10-01 02:43:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTargetAsInnerWindow();
|
|
|
|
if (window) {
|
|
|
|
window->EnableTimeChangeNotifications();
|
|
|
|
}
|
2012-09-30 06:47:48 -07:00
|
|
|
} else if (aTypeAtom == nsGkAtoms::onmoznetworkupload) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTargetAsInnerWindow();
|
|
|
|
if (window) {
|
|
|
|
window->EnableNetworkEvent(NS_NETWORK_UPLOAD_EVENT);
|
|
|
|
}
|
|
|
|
} else if (aTypeAtom == nsGkAtoms::onmoznetworkdownload) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTargetAsInnerWindow();
|
|
|
|
if (window) {
|
|
|
|
window->EnableNetworkEvent(NS_NETWORK_DOWNLOAD_EVENT);
|
|
|
|
}
|
2012-11-07 10:10:40 -08:00
|
|
|
#endif // MOZ_B2G
|
2012-09-22 12:28:08 -07:00
|
|
|
} else if (aTypeAtom == nsGkAtoms::ontouchstart ||
|
|
|
|
aTypeAtom == nsGkAtoms::ontouchend ||
|
|
|
|
aTypeAtom == nsGkAtoms::ontouchmove ||
|
|
|
|
aTypeAtom == nsGkAtoms::ontouchcancel) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mMayHaveTouchEventListener = true;
|
2010-08-02 06:34:54 -07:00
|
|
|
nsPIDOMWindow* window = GetInnerWindowForTarget();
|
2012-07-09 09:55:37 -07:00
|
|
|
// we don't want touchevent listeners added by scrollbars to flip this flag
|
|
|
|
// so we ignore listeners created with system event flag
|
2012-12-15 17:26:05 -08:00
|
|
|
if (window && !aFlags.mInSystemGroup) {
|
2010-08-02 06:34:54 -07:00
|
|
|
window->SetHasTouchEventListeners();
|
2012-12-15 17:26:05 -08:00
|
|
|
}
|
2014-02-10 22:33:29 -08:00
|
|
|
} else if (aType >= NS_POINTER_EVENT_START && aType <= NS_POINTER_LOST_CAPTURE) {
|
|
|
|
nsPIDOMWindow* window = GetInnerWindowForTarget();
|
|
|
|
if (aTypeAtom == nsGkAtoms::onpointerenter ||
|
|
|
|
aTypeAtom == nsGkAtoms::onpointerleave) {
|
|
|
|
mMayHavePointerEnterLeaveEventListener = true;
|
|
|
|
if (window) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCOMPtr<nsIDocument> d = window->GetExtantDoc();
|
|
|
|
NS_WARN_IF_FALSE(!nsContentUtils::IsChromeDoc(d),
|
|
|
|
"Please do not use pointerenter/leave events in chrome. "
|
|
|
|
"They are slower than pointerover/out!");
|
|
|
|
#endif
|
|
|
|
window->SetHasPointerEnterLeaveEventListeners();
|
|
|
|
}
|
|
|
|
}
|
2011-09-18 00:45:14 -07:00
|
|
|
} else if (aTypeAtom == nsGkAtoms::onmouseenter ||
|
|
|
|
aTypeAtom == nsGkAtoms::onmouseleave) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mMayHaveMouseEnterLeaveEventListener = true;
|
2011-09-18 00:45:14 -07:00
|
|
|
nsPIDOMWindow* window = GetInnerWindowForTarget();
|
|
|
|
if (window) {
|
|
|
|
#ifdef DEBUG
|
2013-04-23 21:22:37 -07:00
|
|
|
nsCOMPtr<nsIDocument> d = window->GetExtantDoc();
|
2011-09-18 00:45:14 -07:00
|
|
|
NS_WARN_IF_FALSE(!nsContentUtils::IsChromeDoc(d),
|
|
|
|
"Please do not use mouseenter/leave events in chrome. "
|
|
|
|
"They are slower than mouseover/out!");
|
|
|
|
#endif
|
|
|
|
window->SetHasMouseEnterLeaveEventListeners();
|
|
|
|
}
|
2011-08-03 11:12:08 -07:00
|
|
|
#ifdef MOZ_GAMEPAD
|
|
|
|
} else if (aType >= NS_GAMEPAD_START &&
|
|
|
|
aType <= NS_GAMEPAD_END) {
|
|
|
|
nsPIDOMWindow* window = GetInnerWindowForTarget();
|
|
|
|
if (window) {
|
|
|
|
window->SetHasGamepadEventListener();
|
|
|
|
}
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-05-23 04:41:00 -07:00
|
|
|
if (aTypeAtom && mTarget) {
|
|
|
|
mTarget->EventListenerAdded(aTypeAtom);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-03-29 10:23:27 -07:00
|
|
|
bool
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::IsDeviceType(uint32_t aType)
|
2012-03-29 10:23:27 -07:00
|
|
|
{
|
|
|
|
switch (aType) {
|
|
|
|
case NS_DEVICE_ORIENTATION:
|
|
|
|
case NS_DEVICE_MOTION:
|
2012-06-04 09:17:16 -07:00
|
|
|
case NS_DEVICE_LIGHT:
|
|
|
|
case NS_DEVICE_PROXIMITY:
|
|
|
|
case NS_USER_PROXIMITY:
|
2012-03-29 10:23:27 -07:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-04 09:26:12 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::EnableDevice(uint32_t aType)
|
2012-04-04 09:26:12 -07:00
|
|
|
{
|
2012-10-01 02:43:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTargetAsInnerWindow();
|
2012-04-04 09:26:12 -07:00
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aType) {
|
|
|
|
case NS_DEVICE_ORIENTATION:
|
|
|
|
window->EnableDeviceSensor(SENSOR_ORIENTATION);
|
|
|
|
break;
|
2012-05-02 09:43:45 -07:00
|
|
|
case NS_DEVICE_PROXIMITY:
|
2012-05-16 13:27:54 -07:00
|
|
|
case NS_USER_PROXIMITY:
|
2012-05-02 09:43:45 -07:00
|
|
|
window->EnableDeviceSensor(SENSOR_PROXIMITY);
|
|
|
|
break;
|
2012-05-02 09:43:45 -07:00
|
|
|
case NS_DEVICE_LIGHT:
|
|
|
|
window->EnableDeviceSensor(SENSOR_LIGHT);
|
|
|
|
break;
|
2012-04-04 09:26:12 -07:00
|
|
|
case NS_DEVICE_MOTION:
|
|
|
|
window->EnableDeviceSensor(SENSOR_ACCELERATION);
|
|
|
|
window->EnableDeviceSensor(SENSOR_LINEAR_ACCELERATION);
|
|
|
|
window->EnableDeviceSensor(SENSOR_GYROSCOPE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Enabling an unknown device sensor.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-29 10:23:27 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::DisableDevice(uint32_t aType)
|
2012-03-29 10:23:27 -07:00
|
|
|
{
|
2012-10-01 02:43:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTargetAsInnerWindow();
|
2012-03-29 10:23:27 -07:00
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-04 09:26:12 -07:00
|
|
|
|
2012-03-29 10:23:27 -07:00
|
|
|
switch (aType) {
|
|
|
|
case NS_DEVICE_ORIENTATION:
|
|
|
|
window->DisableDeviceSensor(SENSOR_ORIENTATION);
|
|
|
|
break;
|
|
|
|
case NS_DEVICE_MOTION:
|
|
|
|
window->DisableDeviceSensor(SENSOR_ACCELERATION);
|
|
|
|
window->DisableDeviceSensor(SENSOR_LINEAR_ACCELERATION);
|
|
|
|
window->DisableDeviceSensor(SENSOR_GYROSCOPE);
|
|
|
|
break;
|
2012-05-02 09:43:45 -07:00
|
|
|
case NS_DEVICE_PROXIMITY:
|
2012-05-16 13:27:54 -07:00
|
|
|
case NS_USER_PROXIMITY:
|
2012-05-02 09:43:45 -07:00
|
|
|
window->DisableDeviceSensor(SENSOR_PROXIMITY);
|
|
|
|
break;
|
2012-05-02 09:43:45 -07:00
|
|
|
case NS_DEVICE_LIGHT:
|
|
|
|
window->DisableDeviceSensor(SENSOR_LIGHT);
|
|
|
|
break;
|
2012-03-29 10:23:27 -07:00
|
|
|
default:
|
|
|
|
NS_WARNING("Disabling an unknown device sensor.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::RemoveEventListenerInternal(
|
2014-03-16 23:56:53 -07:00
|
|
|
const EventListenerHolder& aListenerHolder,
|
2014-03-16 23:56:53 -07:00
|
|
|
uint32_t aType,
|
|
|
|
nsIAtom* aUserType,
|
|
|
|
const nsAString& aTypeString,
|
|
|
|
const EventListenerFlags& aFlags,
|
|
|
|
bool aAllEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-03-16 23:56:53 -07:00
|
|
|
if (!aListenerHolder || !aType || mClearingListeners) {
|
2011-06-23 19:18:02 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
uint32_t typeCount = 0;
|
2012-04-04 09:26:12 -07:00
|
|
|
bool deviceType = IsDeviceType(aType);
|
2012-11-07 10:10:40 -08:00
|
|
|
#ifdef MOZ_B2G
|
2012-09-10 23:05:00 -07:00
|
|
|
bool timeChangeEvent = (aType == NS_MOZ_TIME_CHANGE_EVENT);
|
2012-09-30 06:47:48 -07:00
|
|
|
bool networkEvent = (aType == NS_NETWORK_UPLOAD_EVENT ||
|
|
|
|
aType == NS_NETWORK_DOWNLOAD_EVENT);
|
2012-11-07 10:10:40 -08:00
|
|
|
#endif // MOZ_B2G
|
2012-03-29 10:23:27 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-16 23:56:51 -07:00
|
|
|
listener = &mListeners.ElementAt(i);
|
|
|
|
if (EVENT_TYPE_EQUALS(listener, aType, aUserType, aTypeString,
|
|
|
|
aAllEvents)) {
|
2012-03-29 10:23:27 -07:00
|
|
|
++typeCount;
|
2014-03-16 23:56:53 -07:00
|
|
|
if (listener->mListener == aListenerHolder &&
|
2014-03-16 23:56:51 -07:00
|
|
|
listener->mFlags.EqualsIgnoringTrustness(aFlags)) {
|
2014-03-16 23:56:53 -07:00
|
|
|
nsRefPtr<EventListenerManager> kungFuDeathGrip(this);
|
2012-03-29 10:23:27 -07:00
|
|
|
mListeners.RemoveElementAt(i);
|
|
|
|
--count;
|
2013-09-24 03:04:16 -07:00
|
|
|
mNoListenerForEvent = NS_EVENT_NULL;
|
2012-07-30 07:20:58 -07:00
|
|
|
mNoListenerForEventAtom = nullptr;
|
2013-05-23 04:41:00 -07:00
|
|
|
if (mTarget && aUserType) {
|
|
|
|
mTarget->EventListenerRemoved(aUserType);
|
|
|
|
}
|
2012-03-29 10:23:27 -07:00
|
|
|
|
2012-11-07 10:10:40 -08:00
|
|
|
if (!deviceType
|
|
|
|
#ifdef MOZ_B2G
|
|
|
|
&& !timeChangeEvent && !networkEvent
|
|
|
|
#endif // MOZ_B2G
|
|
|
|
) {
|
2012-03-29 10:23:27 -07:00
|
|
|
return;
|
2012-03-24 17:29:49 -07:00
|
|
|
}
|
2012-03-29 10:23:27 -07:00
|
|
|
--typeCount;
|
2011-10-06 10:27:41 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-03-29 10:23:27 -07:00
|
|
|
|
2012-11-01 05:42:29 -07:00
|
|
|
if (!aAllEvents && deviceType && typeCount == 0) {
|
2012-03-29 10:23:27 -07:00
|
|
|
DisableDevice(aType);
|
2012-11-07 10:10:40 -08:00
|
|
|
#ifdef MOZ_B2G
|
2012-09-10 23:05:00 -07:00
|
|
|
} else if (timeChangeEvent && typeCount == 0) {
|
2012-10-01 02:43:53 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTargetAsInnerWindow();
|
|
|
|
if (window) {
|
|
|
|
window->DisableTimeChangeNotifications();
|
|
|
|
}
|
2012-11-01 05:42:29 -07:00
|
|
|
} else if (!aAllEvents && networkEvent && typeCount == 0) {
|
2012-09-30 06:47:48 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTargetAsInnerWindow();
|
|
|
|
if (window) {
|
|
|
|
window->DisableNetworkEvent(aType);
|
|
|
|
}
|
2012-11-07 10:10:40 -08:00
|
|
|
#endif // MOZ_B2G
|
2012-03-29 10:23:27 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-16 03:06:24 -07:00
|
|
|
bool
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::ListenerCanHandle(Listener* aListener,
|
|
|
|
WidgetEvent* aEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-08-08 11:26:26 -07:00
|
|
|
// This is slightly different from EVENT_TYPE_EQUALS in that it returns
|
|
|
|
// true even when aEvent->message == NS_USER_DEFINED_EVENT and
|
2014-03-16 23:56:51 -07:00
|
|
|
// aListener=>mEventType != NS_USER_DEFINED_EVENT as long as the atoms are
|
|
|
|
// the same
|
|
|
|
if (aListener->mAllEvents) {
|
2013-08-16 03:06:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (aEvent->message == NS_USER_DEFINED_EVENT) {
|
|
|
|
if (mIsMainThreadELM) {
|
2014-03-16 23:56:51 -07:00
|
|
|
return aListener->mTypeAtom == aEvent->userType;
|
2013-08-16 03:06:24 -07:00
|
|
|
}
|
2014-03-16 23:56:51 -07:00
|
|
|
return aListener->mTypeString.Equals(aEvent->typeString);
|
2013-08-16 03:06:24 -07:00
|
|
|
}
|
2013-12-20 16:22:13 -08:00
|
|
|
MOZ_ASSERT(mIsMainThreadELM);
|
2014-03-16 23:56:51 -07:00
|
|
|
return aListener->mEventType == aEvent->message;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-18 02:45:00 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::AddEventListenerByType(
|
2014-03-16 23:56:53 -07:00
|
|
|
const EventListenerHolder& aListenerHolder,
|
2014-03-16 23:56:53 -07:00
|
|
|
const nsAString& aType,
|
|
|
|
const EventListenerFlags& aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-16 03:06:24 -07:00
|
|
|
nsCOMPtr<nsIAtom> atom =
|
|
|
|
mIsMainThreadELM ? do_GetAtom(NS_LITERAL_STRING("on") + aType) : nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t type = nsContentUtils::GetEventId(atom);
|
2014-03-16 23:56:53 -07:00
|
|
|
AddEventListenerInternal(aListenerHolder, type, atom, aType, aFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::RemoveEventListenerByType(
|
2014-03-16 23:56:53 -07:00
|
|
|
const EventListenerHolder& aListenerHolder,
|
2014-03-16 23:56:53 -07:00
|
|
|
const nsAString& aType,
|
|
|
|
const EventListenerFlags& aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-16 03:06:24 -07:00
|
|
|
nsCOMPtr<nsIAtom> atom =
|
|
|
|
mIsMainThreadELM ? do_GetAtom(NS_LITERAL_STRING("on") + aType) : nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t type = nsContentUtils::GetEventId(atom);
|
2014-03-16 23:56:53 -07:00
|
|
|
RemoveEventListenerInternal(aListenerHolder, type, atom, aType, aFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::Listener*
|
|
|
|
EventListenerManager::FindEventHandler(uint32_t aEventType,
|
|
|
|
nsIAtom* aTypeAtom,
|
|
|
|
const nsAString& aTypeString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Run through the listeners for this type and see if a script
|
|
|
|
// listener is registered
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-16 23:56:51 -07:00
|
|
|
listener = &mListeners.ElementAt(i);
|
|
|
|
if (listener->mListenerIsHandler &&
|
|
|
|
EVENT_TYPE_EQUALS(listener, aEventType, aTypeAtom, aTypeString,
|
|
|
|
false)) {
|
|
|
|
return listener;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::Listener*
|
|
|
|
EventListenerManager::SetEventHandlerInternal(
|
|
|
|
nsIAtom* aName,
|
|
|
|
const nsAString& aTypeString,
|
2014-04-01 19:00:45 -07:00
|
|
|
const TypedEventHandler& aTypedHandler,
|
2014-03-16 23:56:53 -07:00
|
|
|
bool aPermitUntrustedEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-09-17 04:01:28 -07:00
|
|
|
MOZ_ASSERT(aName || !aTypeString.IsEmpty());
|
2012-08-30 20:45:16 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t eventType = nsContentUtils::GetEventId(aName);
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener = FindEventHandler(eventType, aName, aTypeString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
if (!listener) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// If we didn't find a script listener or no listeners existed
|
|
|
|
// create and add a new one.
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mListenerIsJSListener = true;
|
2012-11-09 08:00:25 -08:00
|
|
|
|
2014-04-01 19:00:46 -07:00
|
|
|
nsCOMPtr<JSEventHandler> jsEventHandler;
|
2014-04-01 19:00:45 -07:00
|
|
|
NS_NewJSEventHandler(mTarget, aName,
|
2014-04-01 19:00:46 -07:00
|
|
|
aTypedHandler, getter_AddRefs(jsEventHandler));
|
|
|
|
EventListenerHolder listenerHolder(jsEventHandler);
|
2014-03-16 23:56:53 -07:00
|
|
|
AddEventListenerInternal(listenerHolder, eventType, aName, aTypeString,
|
|
|
|
flags, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
listener = FindEventHandler(eventType, aName, aTypeString);
|
2011-08-24 12:49:25 -07:00
|
|
|
} else {
|
2014-04-01 19:00:46 -07:00
|
|
|
JSEventHandler* jsEventHandler = listener->GetJSEventHandler();
|
|
|
|
MOZ_ASSERT(jsEventHandler,
|
|
|
|
"How can we have an event handler with no JSEventHandler?");
|
2012-08-30 20:45:16 -07:00
|
|
|
|
2014-04-01 19:00:46 -07:00
|
|
|
bool same = jsEventHandler->GetTypedEventHandler() == aTypedHandler;
|
2013-05-24 10:22:20 -07:00
|
|
|
// Possibly the same listener, but update still the context and scope.
|
2014-04-01 19:00:46 -07:00
|
|
|
jsEventHandler->SetHandler(aTypedHandler);
|
2013-11-27 07:22:16 -08:00
|
|
|
if (mTarget && !same && aName) {
|
2013-05-24 10:22:20 -07:00
|
|
|
mTarget->EventListenerRemoved(aName);
|
|
|
|
mTarget->EventListenerAdded(aName);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
// Set flag to indicate possible need for compilation later
|
2014-04-01 19:00:45 -07:00
|
|
|
listener->mHandlerIsString = !aTypedHandler.HasEventHandler();
|
2013-09-17 04:01:28 -07:00
|
|
|
if (aPermitUntrustedEvents) {
|
2014-03-16 23:56:51 -07:00
|
|
|
listener->mFlags.mAllowUntrustedEvents = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
return listener;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
nsresult
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::SetEventHandler(nsIAtom* aName,
|
|
|
|
const nsAString& aBody,
|
|
|
|
bool aDeferCompilation,
|
|
|
|
bool aPermitUntrustedEvents,
|
|
|
|
Element* aElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
2013-11-27 01:12:41 -08:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global =
|
|
|
|
GetScriptGlobalAndDocument(getter_AddRefs(doc));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-01 12:08:08 -07:00
|
|
|
if (!global) {
|
|
|
|
// This can happen; for example this document might have been
|
|
|
|
// loaded as data.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-02-23 13:20:46 -08:00
|
|
|
|
2012-11-21 08:19:03 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(global);
|
|
|
|
if (win) {
|
|
|
|
MOZ_ASSERT(win->IsInnerWindow(), "We should not have an outer window here!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-14 05:04:12 -07:00
|
|
|
nsresult rv = NS_OK;
|
2010-02-23 13:20:46 -08:00
|
|
|
// return early preventing the event listener from being added
|
|
|
|
// 'doc' is fetched above
|
|
|
|
if (doc) {
|
2012-08-20 11:34:32 -07:00
|
|
|
// Don't allow adding an event listener if the document is sandboxed
|
|
|
|
// without 'allow-scripts'.
|
|
|
|
if (doc->GetSandboxFlags() & SANDBOXED_SCRIPTS) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2010-02-23 13:20:46 -08:00
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = doc->NodePrincipal()->GetCsp(getter_AddRefs(csp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (csp) {
|
2012-10-15 13:54:58 -07:00
|
|
|
bool inlineOK = true;
|
|
|
|
bool reportViolations = false;
|
|
|
|
rv = csp->GetAllowsInlineScript(&reportViolations, &inlineOK);
|
2010-02-23 13:20:46 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-10-15 13:54:58 -07:00
|
|
|
if (reportViolations) {
|
2011-01-31 10:09:44 -08:00
|
|
|
// gather information to log with violation report
|
|
|
|
nsIURI* uri = doc->GetDocumentURI();
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString asciiSpec;
|
2011-01-31 10:09:44 -08:00
|
|
|
if (uri)
|
|
|
|
uri->GetAsciiSpec(asciiSpec);
|
|
|
|
nsAutoString scriptSample, attr, tagName(NS_LITERAL_STRING("UNKNOWN"));
|
|
|
|
aName->ToString(attr);
|
2011-07-29 12:37:17 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode(do_QueryInterface(mTarget));
|
2011-01-31 10:09:44 -08:00
|
|
|
if (domNode)
|
|
|
|
domNode->GetNodeName(tagName);
|
|
|
|
// build a "script sample" based on what we know about this element
|
|
|
|
scriptSample.Assign(attr);
|
|
|
|
scriptSample.AppendLiteral(" attribute on ");
|
|
|
|
scriptSample.Append(tagName);
|
|
|
|
scriptSample.AppendLiteral(" element");
|
|
|
|
csp->LogViolationDetails(nsIContentSecurityPolicy::VIOLATION_TYPE_INLINE_SCRIPT,
|
|
|
|
NS_ConvertUTF8toUTF16(asciiSpec),
|
|
|
|
scriptSample,
|
2013-11-08 15:44:39 -08:00
|
|
|
0,
|
2014-01-02 11:14:06 -08:00
|
|
|
EmptyString(),
|
2013-11-08 15:44:39 -08:00
|
|
|
EmptyString());
|
2012-10-15 13:54:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// return early if CSP wants us to block inline scripts
|
|
|
|
if (!inlineOK) {
|
2010-02-23 13:20:46 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-01 12:08:08 -07:00
|
|
|
// This might be the first reference to this language in the global
|
|
|
|
// We must init the language before we attempt to fetch its context.
|
2012-03-23 10:13:29 -07:00
|
|
|
if (NS_FAILED(global->EnsureScriptEnvironment())) {
|
2007-05-01 12:08:08 -07:00
|
|
|
NS_WARNING("Failed to setup script environment for this language");
|
|
|
|
// but fall through and let the inevitable failure below handle it.
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-03-23 10:13:29 -07:00
|
|
|
nsIScriptContext* context = global->GetScriptContext();
|
2007-05-01 12:08:08 -07:00
|
|
|
NS_ENSURE_TRUE(context, NS_ERROR_FAILURE);
|
2014-01-06 05:36:10 -08:00
|
|
|
NS_ENSURE_STATE(global->GetGlobalJSObject());
|
|
|
|
|
2014-03-21 19:31:03 -07:00
|
|
|
Listener* listener = SetEventHandlerInternal(aName,
|
2014-03-16 23:56:51 -07:00
|
|
|
EmptyString(),
|
2014-04-01 19:00:45 -07:00
|
|
|
TypedEventHandler(),
|
2014-03-16 23:56:51 -07:00
|
|
|
aPermitUntrustedEvents);
|
2009-03-14 20:42:50 -07:00
|
|
|
|
2011-08-24 12:49:25 -07:00
|
|
|
if (!aDeferCompilation) {
|
2014-03-16 23:56:51 -07:00
|
|
|
return CompileEventHandlerInternal(listener, &aBody, aElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-24 12:49:25 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::RemoveEventHandler(nsIAtom* aName,
|
|
|
|
const nsAString& aTypeString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-30 01:13:16 -08:00
|
|
|
if (mClearingListeners) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t eventType = nsContentUtils::GetEventId(aName);
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener = FindEventHandler(eventType, aName, aTypeString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
if (listener) {
|
|
|
|
mListeners.RemoveElementAt(uint32_t(listener - &mListeners.ElementAt(0)));
|
2013-09-24 03:04:16 -07:00
|
|
|
mNoListenerForEvent = NS_EVENT_NULL;
|
2012-07-30 07:20:58 -07:00
|
|
|
mNoListenerForEventAtom = nullptr;
|
2013-12-04 10:17:53 -08:00
|
|
|
if (mTarget && aName) {
|
2013-05-23 04:41:00 -07:00
|
|
|
mTarget->EventListenerRemoved(aName);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::CompileEventHandlerInternal(Listener* aListener,
|
|
|
|
const nsAString* aBody,
|
|
|
|
Element* aElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-04-01 19:00:46 -07:00
|
|
|
MOZ_ASSERT(aListener->GetJSEventHandler());
|
2014-03-21 19:31:02 -07:00
|
|
|
MOZ_ASSERT(aListener->mHandlerIsString, "Why are we compiling a non-string JS listener?");
|
2014-04-01 19:00:46 -07:00
|
|
|
JSEventHandler* jsEventHandler = aListener->GetJSEventHandler();
|
|
|
|
MOZ_ASSERT(!jsEventHandler->GetTypedEventHandler().HasEventHandler(),
|
2014-04-01 19:00:45 -07:00
|
|
|
"What is there to compile?");
|
2011-08-24 12:49:25 -07:00
|
|
|
|
2014-03-21 19:31:02 -07:00
|
|
|
nsresult result = NS_OK;
|
2013-11-27 01:12:41 -08:00
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global =
|
|
|
|
GetScriptGlobalAndDocument(getter_AddRefs(doc));
|
|
|
|
NS_ENSURE_STATE(global);
|
|
|
|
|
2014-04-14 20:26:59 -07:00
|
|
|
// Activate JSAPI, and make sure that exceptions are reported on the right
|
|
|
|
// Window.
|
2014-06-19 00:21:14 -07:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.InitWithLegacyErrorReporting(global))) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2014-04-14 20:26:59 -07:00
|
|
|
JSContext* cx = jsapi.cx();
|
2013-11-26 08:47:52 -08:00
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
nsCOMPtr<nsIAtom> typeAtom = aListener->mTypeAtom;
|
2014-03-05 05:16:42 -08:00
|
|
|
nsIAtom* attrName = typeAtom;
|
2013-11-26 08:47:52 -08:00
|
|
|
|
2014-03-21 19:31:02 -07:00
|
|
|
// Flag us as not a string so we don't keep trying to compile strings which
|
|
|
|
// can't be compiled.
|
2014-03-21 19:31:02 -07:00
|
|
|
aListener->mHandlerIsString = false;
|
|
|
|
|
|
|
|
// mTarget may not be an Element if it's a window and we're
|
|
|
|
// getting an inline event listener forwarded from <html:body> or
|
|
|
|
// <html:frameset> or <xul:window> or the like.
|
|
|
|
// XXX I don't like that we have to reference content from
|
|
|
|
// here. The alternative is to store the event handler string on
|
2014-04-01 19:00:46 -07:00
|
|
|
// the JSEventHandler itself, and that still doesn't address
|
2014-03-21 19:31:02 -07:00
|
|
|
// the arg names issue.
|
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(mTarget);
|
|
|
|
MOZ_ASSERT(element || aBody, "Where will we get our body?");
|
|
|
|
nsAutoString handlerBody;
|
|
|
|
const nsAString* body = aBody;
|
|
|
|
if (!aBody) {
|
|
|
|
if (aListener->mTypeAtom == nsGkAtoms::onSVGLoad) {
|
|
|
|
attrName = nsGkAtoms::onload;
|
|
|
|
} else if (aListener->mTypeAtom == nsGkAtoms::onSVGUnload) {
|
|
|
|
attrName = nsGkAtoms::onunload;
|
|
|
|
} else if (aListener->mTypeAtom == nsGkAtoms::onSVGResize) {
|
|
|
|
attrName = nsGkAtoms::onresize;
|
|
|
|
} else if (aListener->mTypeAtom == nsGkAtoms::onSVGScroll) {
|
|
|
|
attrName = nsGkAtoms::onscroll;
|
|
|
|
} else if (aListener->mTypeAtom == nsGkAtoms::onSVGZoom) {
|
|
|
|
attrName = nsGkAtoms::onzoom;
|
|
|
|
} else if (aListener->mTypeAtom == nsGkAtoms::onbeginEvent) {
|
|
|
|
attrName = nsGkAtoms::onbegin;
|
|
|
|
} else if (aListener->mTypeAtom == nsGkAtoms::onrepeatEvent) {
|
|
|
|
attrName = nsGkAtoms::onrepeat;
|
|
|
|
} else if (aListener->mTypeAtom == nsGkAtoms::onendEvent) {
|
|
|
|
attrName = nsGkAtoms::onend;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-21 19:31:02 -07:00
|
|
|
element->GetAttr(kNameSpaceID_None, attrName, handlerBody);
|
|
|
|
body = &handlerBody;
|
|
|
|
aElement = element;
|
|
|
|
}
|
|
|
|
aListener = nullptr;
|
|
|
|
|
|
|
|
uint32_t lineNo = 0;
|
|
|
|
nsAutoCString url (NS_LITERAL_CSTRING("-moz-evil:lying-event-listener"));
|
|
|
|
MOZ_ASSERT(body);
|
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
nsIURI *uri = aElement->OwnerDoc()->GetDocumentURI();
|
|
|
|
if (uri) {
|
|
|
|
uri->GetSpec(url);
|
|
|
|
lineNo = 1;
|
|
|
|
}
|
|
|
|
|
2014-05-08 19:20:54 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(mTarget);
|
2014-03-21 19:31:02 -07:00
|
|
|
uint32_t argCount;
|
|
|
|
const char **argNames;
|
|
|
|
nsContentUtils::GetEventArgNames(aElement->GetNameSpaceID(),
|
2014-05-08 19:20:54 -07:00
|
|
|
typeAtom, win,
|
2014-03-21 19:31:02 -07:00
|
|
|
&argCount, &argNames);
|
|
|
|
|
2014-06-21 11:55:15 -07:00
|
|
|
JSAddonId *addonId = MapURIToAddonID(uri);
|
|
|
|
|
2014-03-21 19:31:02 -07:00
|
|
|
// Wrap the event target, so that we can use it as the scope for the event
|
|
|
|
// handler. Note that mTarget is different from aElement in the <body> case,
|
|
|
|
// where mTarget is a Window.
|
|
|
|
//
|
|
|
|
// The wrapScope doesn't really matter here, because the target will create
|
|
|
|
// its reflector in the proper scope, and then we'll enter that compartment.
|
2014-06-19 00:21:14 -07:00
|
|
|
JS::Rooted<JSObject*> wrapScope(cx, global->GetGlobalJSObject());
|
2014-03-21 19:31:02 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, wrapScope);
|
2014-04-09 21:58:41 -07:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(cx, mTarget, &v,
|
2014-03-21 19:31:02 -07:00
|
|
|
/* aAllowWrapping = */ false);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2014-06-21 11:55:15 -07:00
|
|
|
if (addonId) {
|
|
|
|
JS::Rooted<JSObject*> vObj(cx, &v.toObject());
|
|
|
|
JS::Rooted<JSObject*> addonScope(cx, xpc::GetAddonScope(cx, vObj, addonId));
|
|
|
|
if (!addonScope) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
JSAutoCompartment ac(cx, addonScope);
|
|
|
|
if (!JS_WrapValue(cx, &v)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2014-03-21 19:31:02 -07:00
|
|
|
JS::Rooted<JSObject*> target(cx, &v.toObject());
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
|
|
|
|
nsDependentAtomString str(attrName);
|
|
|
|
// Most of our names are short enough that we don't even have to malloc
|
|
|
|
// the JS string stuff, so don't worry about playing games with
|
|
|
|
// refcounting XPCOM stringbuffers.
|
|
|
|
JS::Rooted<JSString*> jsStr(cx, JS_NewUCStringCopyN(cx,
|
|
|
|
str.BeginReading(),
|
|
|
|
str.Length()));
|
|
|
|
NS_ENSURE_TRUE(jsStr, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
// Get the reflector for |aElement|, so that we can pass to setElement.
|
|
|
|
if (NS_WARN_IF(!WrapNewBindingObject(cx, target, aElement, &v))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-03-21 19:31:02 -07:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setIntroductionType("eventHandler")
|
|
|
|
.setFileAndLine(url.get(), lineNo)
|
2014-06-06 01:06:26 -07:00
|
|
|
.setVersion(JSVERSION_DEFAULT)
|
2014-03-21 19:31:02 -07:00
|
|
|
.setElement(&v.toObject())
|
2014-03-21 19:31:02 -07:00
|
|
|
.setElementAttributeName(jsStr)
|
|
|
|
.setDefineOnScope(false);
|
2014-03-21 19:31:02 -07:00
|
|
|
|
2014-03-21 19:31:02 -07:00
|
|
|
JS::Rooted<JSObject*> handler(cx);
|
2014-03-21 19:31:02 -07:00
|
|
|
result = nsJSUtils::CompileFunction(cx, target, options,
|
2014-03-21 19:31:02 -07:00
|
|
|
nsAtomCString(typeAtom),
|
2014-03-21 19:31:02 -07:00
|
|
|
argCount, argNames, *body, handler.address());
|
2014-03-21 19:31:02 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
NS_ENSURE_TRUE(handler, NS_ERROR_FAILURE);
|
|
|
|
|
2014-04-01 19:00:46 -07:00
|
|
|
if (jsEventHandler->EventName() == nsGkAtoms::onerror && win) {
|
2014-03-21 19:31:02 -07:00
|
|
|
nsRefPtr<OnErrorEventHandlerNonNull> handlerCallback =
|
2014-03-21 19:31:02 -07:00
|
|
|
new OnErrorEventHandlerNonNull(handler, /* aIncumbentGlobal = */ nullptr);
|
2014-04-01 19:00:46 -07:00
|
|
|
jsEventHandler->SetHandler(handlerCallback);
|
|
|
|
} else if (jsEventHandler->EventName() == nsGkAtoms::onbeforeunload && win) {
|
2014-03-21 19:31:02 -07:00
|
|
|
nsRefPtr<OnBeforeUnloadEventHandlerNonNull> handlerCallback =
|
2014-03-21 19:31:02 -07:00
|
|
|
new OnBeforeUnloadEventHandlerNonNull(handler, /* aIncumbentGlobal = */ nullptr);
|
2014-04-01 19:00:46 -07:00
|
|
|
jsEventHandler->SetHandler(handlerCallback);
|
2014-03-05 05:16:42 -08:00
|
|
|
} else {
|
2014-03-21 19:31:02 -07:00
|
|
|
nsRefPtr<EventHandlerNonNull> handlerCallback =
|
2014-03-21 19:31:02 -07:00
|
|
|
new EventHandlerNonNull(handler, /* aIncumbentGlobal = */ nullptr);
|
2014-04-01 19:00:46 -07:00
|
|
|
jsEventHandler->SetHandler(handlerCallback);
|
2011-10-29 13:16:43 -07:00
|
|
|
}
|
2011-08-24 12:49:25 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::HandleEventSubType(Listener* aListener,
|
|
|
|
nsIDOMEvent* aDOMEvent,
|
|
|
|
EventTarget* aCurrentTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
2014-03-16 23:56:51 -07:00
|
|
|
EventListenerHolder listenerHolder(aListener->mListener); // strong ref
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If this is a script handler and we haven't yet
|
|
|
|
// compiled the event handler itself
|
2014-03-16 23:56:51 -07:00
|
|
|
if ((aListener->mListenerType == Listener::eJSEventListener) &&
|
2014-03-16 23:56:51 -07:00
|
|
|
aListener->mHandlerIsString) {
|
|
|
|
result = CompileEventHandlerInternal(aListener, nullptr, nullptr);
|
|
|
|
aListener = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-05-14 00:03:15 -07:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2013-09-13 17:22:39 -07:00
|
|
|
if (mIsMainThreadELM) {
|
|
|
|
nsContentUtils::EnterMicroTask();
|
|
|
|
}
|
2014-03-17 21:48:21 -07:00
|
|
|
// nsIDOMEvent::currentTarget is set in EventDispatcher.
|
2014-03-16 23:56:51 -07:00
|
|
|
if (listenerHolder.HasWebIDLCallback()) {
|
2013-04-16 14:15:57 -07:00
|
|
|
ErrorResult rv;
|
2014-03-16 23:56:51 -07:00
|
|
|
listenerHolder.GetWebIDLCallback()->
|
2013-04-16 14:15:57 -07:00
|
|
|
HandleEvent(aCurrentTarget, *(aDOMEvent->InternalDOMEvent()), rv);
|
|
|
|
result = rv.ErrorCode();
|
|
|
|
} else {
|
2014-03-16 23:56:51 -07:00
|
|
|
result = listenerHolder.GetXPCOMCallback()->HandleEvent(aDOMEvent);
|
2013-04-16 14:15:57 -07:00
|
|
|
}
|
2013-09-13 17:22:39 -07:00
|
|
|
if (mIsMainThreadELM) {
|
|
|
|
nsContentUtils::LeaveMicroTask();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Causes a check for event listeners and processing by them if they exist.
|
|
|
|
* @param an event listener
|
|
|
|
*/
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::HandleEventInternal(nsPresContext* aPresContext,
|
|
|
|
WidgetEvent* aEvent,
|
|
|
|
nsIDOMEvent** aDOMEvent,
|
|
|
|
EventTarget* aCurrentTarget,
|
|
|
|
nsEventStatus* aEventStatus)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
//Set the value of the internal PreventDefault flag properly based on aEventStatus
|
|
|
|
if (*aEventStatus == nsEventStatus_eConsumeNoDefault) {
|
2012-12-15 17:26:04 -08:00
|
|
|
aEvent->mFlags.mDefaultPrevented = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
nsAutoTObserverArray<Listener, 2>::EndLimitedIterator iter(mListeners);
|
2013-11-05 06:16:26 -08:00
|
|
|
Maybe<nsAutoPopupStatePusher> popupStatePusher;
|
|
|
|
if (mIsMainThreadELM) {
|
2014-08-13 15:39:41 -07:00
|
|
|
popupStatePusher.emplace(Event::GetEventPopupControlState(aEvent));
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasListener = false;
|
2007-12-19 23:30:04 -08:00
|
|
|
while (iter.HasMore()) {
|
2012-12-15 17:26:03 -08:00
|
|
|
if (aEvent->mFlags.mImmediatePropagationStopped) {
|
2011-11-04 10:17:19 -07:00
|
|
|
break;
|
|
|
|
}
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener = &iter.GetNext();
|
2007-03-22 10:30:00 -07:00
|
|
|
// Check that the phase is same in event and event listener.
|
|
|
|
// Handle only trusted events, except when listener permits untrusted events.
|
2014-03-16 23:56:51 -07:00
|
|
|
if (ListenerCanHandle(listener, aEvent)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
hasListener = true;
|
2014-03-16 23:56:51 -07:00
|
|
|
if (listener->IsListening(aEvent) &&
|
|
|
|
(aEvent->mFlags.mIsTrusted ||
|
|
|
|
listener->mFlags.mAllowUntrustedEvents)) {
|
2011-08-08 11:26:26 -07:00
|
|
|
if (!*aDOMEvent) {
|
2013-03-09 03:34:29 -08:00
|
|
|
// This is tiny bit slow, but happens only once per event.
|
2014-03-16 23:56:53 -07:00
|
|
|
nsCOMPtr<EventTarget> et =
|
2013-03-09 03:34:29 -08:00
|
|
|
do_QueryInterface(aEvent->originalTarget);
|
2014-03-17 21:48:21 -07:00
|
|
|
EventDispatcher::CreateEvent(et, aPresContext,
|
|
|
|
aEvent, EmptyString(), aDOMEvent);
|
2011-08-08 11:26:26 -07:00
|
|
|
}
|
|
|
|
if (*aDOMEvent) {
|
|
|
|
if (!aEvent->currentTarget) {
|
|
|
|
aEvent->currentTarget = aCurrentTarget->GetTargetForDOMEvent();
|
2009-06-15 03:21:06 -07:00
|
|
|
if (!aEvent->currentTarget) {
|
2011-08-08 11:26:26 -07:00
|
|
|
break;
|
2009-06-15 03:21:06 -07:00
|
|
|
}
|
2011-08-08 11:26:26 -07:00
|
|
|
}
|
2012-06-28 14:47:56 -07:00
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
if (NS_FAILED(HandleEventSubType(listener, *aDOMEvent,
|
|
|
|
aCurrentTarget))) {
|
2012-12-15 17:26:04 -08:00
|
|
|
aEvent->mFlags.mExceptionHasBeenRisen = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
aEvent->currentTarget = nullptr;
|
2009-06-15 03:21:06 -07:00
|
|
|
|
2013-09-26 14:59:27 -07:00
|
|
|
if (mIsMainThreadELM && !hasListener) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mNoListenerForEvent = aEvent->message;
|
|
|
|
mNoListenerForEventAtom = aEvent->userType;
|
|
|
|
}
|
|
|
|
|
2012-12-15 17:26:04 -08:00
|
|
|
if (aEvent->mFlags.mDefaultPrevented) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*aEventStatus = nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::Disconnect()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mTarget = nullptr;
|
2011-06-23 19:18:02 -07:00
|
|
|
RemoveAllListeners();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-18 02:45:00 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::AddEventListener(
|
|
|
|
const nsAString& aType,
|
|
|
|
const EventListenerHolder& aListenerHolder,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mCapture = aUseCapture;
|
|
|
|
flags.mAllowUntrustedEvents = aWantsUntrusted;
|
2014-03-16 23:56:53 -07:00
|
|
|
return AddEventListenerByType(aListenerHolder, aType, flags);
|
2011-06-23 19:18:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::RemoveEventListener(
|
|
|
|
const nsAString& aType,
|
|
|
|
const EventListenerHolder& aListenerHolder,
|
|
|
|
bool aUseCapture)
|
2011-06-23 19:18:02 -07:00
|
|
|
{
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mCapture = aUseCapture;
|
2014-03-16 23:56:53 -07:00
|
|
|
RemoveEventListenerByType(aListenerHolder, aType, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-01 05:42:29 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::AddListenerForAllEvents(nsIDOMEventListener* aDOMListener,
|
2014-03-16 23:56:53 -07:00
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
|
|
|
bool aSystemEventGroup)
|
2012-11-01 05:42:29 -07:00
|
|
|
{
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mCapture = aUseCapture;
|
|
|
|
flags.mAllowUntrustedEvents = aWantsUntrusted;
|
|
|
|
flags.mInSystemGroup = aSystemEventGroup;
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerHolder listenerHolder(aDOMListener);
|
|
|
|
AddEventListenerInternal(listenerHolder, NS_EVENT_ALL, nullptr, EmptyString(),
|
2013-08-16 03:06:24 -07:00
|
|
|
flags, false, true);
|
2012-11-01 05:42:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::RemoveListenerForAllEvents(
|
|
|
|
nsIDOMEventListener* aDOMListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aSystemEventGroup)
|
2012-11-01 05:42:29 -07:00
|
|
|
{
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mCapture = aUseCapture;
|
|
|
|
flags.mInSystemGroup = aSystemEventGroup;
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerHolder listenerHolder(aDOMListener);
|
|
|
|
RemoveEventListenerInternal(listenerHolder, NS_EVENT_ALL, nullptr,
|
|
|
|
EmptyString(), flags, true);
|
2012-11-01 05:42:29 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::HasMutationListeners()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mMayHaveMutationListeners) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener = &mListeners.ElementAt(i);
|
|
|
|
if (listener->mEventType >= NS_MUTATION_START &&
|
|
|
|
listener->mEventType <= NS_MUTATION_END) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::MutationListenerBits()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bits = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mMayHaveMutationListeners) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener = &mListeners.ElementAt(i);
|
|
|
|
if (listener->mEventType >= NS_MUTATION_START &&
|
|
|
|
listener->mEventType <= NS_MUTATION_END) {
|
|
|
|
if (listener->mEventType == NS_MUTATION_SUBTREEMODIFIED) {
|
2007-03-24 05:18:02 -07:00
|
|
|
return kAllMutationBits;
|
|
|
|
}
|
2014-03-16 23:56:51 -07:00
|
|
|
bits |= MutationBitForEventType(listener->mEventType);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::HasListenersFor(const nsAString& aEventName)
|
2007-05-08 14:13:13 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> atom = do_GetAtom(NS_LITERAL_STRING("on") + aEventName);
|
2012-08-17 07:12:58 -07:00
|
|
|
return HasListenersFor(atom);
|
|
|
|
}
|
2007-05-08 14:13:13 -07:00
|
|
|
|
2012-08-17 07:12:58 -07:00
|
|
|
bool
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::HasListenersFor(nsIAtom* aEventNameWithOn)
|
2012-08-17 07:12:58 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoString name;
|
|
|
|
aEventNameWithOn->ToString(name);
|
|
|
|
#endif
|
|
|
|
NS_ASSERTION(StringBeginsWith(name, NS_LITERAL_STRING("on")),
|
|
|
|
"Event name does not start with 'on'");
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener = &mListeners.ElementAt(i);
|
|
|
|
if (listener->mTypeAtom == aEventNameWithOn) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-05-08 14:13:13 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-05-08 14:13:13 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::HasListeners()
|
2008-09-30 17:51:53 -07:00
|
|
|
{
|
|
|
|
return !mListeners.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2009-10-16 01:57:32 -07:00
|
|
|
nsresult
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::GetListenerInfo(nsCOMArray<nsIEventListenerInfo>* aList)
|
2009-10-16 01:57:32 -07:00
|
|
|
{
|
2013-04-05 17:44:15 -07:00
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(mTarget);
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_ENSURE_STATE(target);
|
|
|
|
aList->Clear();
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-16 23:56:51 -07:00
|
|
|
const Listener& listener = mListeners.ElementAt(i);
|
2009-10-16 01:57:32 -07:00
|
|
|
// If this is a script handler and we haven't yet
|
2011-08-24 12:49:25 -07:00
|
|
|
// compiled the event handler itself go ahead and compile it
|
2014-03-16 23:56:51 -07:00
|
|
|
if (listener.mListenerType == Listener::eJSEventListener &&
|
2014-03-16 23:56:51 -07:00
|
|
|
listener.mHandlerIsString) {
|
|
|
|
CompileEventHandlerInternal(const_cast<Listener*>(&listener), nullptr,
|
2013-11-26 08:44:37 -08:00
|
|
|
nullptr);
|
2009-10-16 01:57:32 -07:00
|
|
|
}
|
2013-06-14 10:10:22 -07:00
|
|
|
nsAutoString eventType;
|
2014-03-16 23:56:51 -07:00
|
|
|
if (listener.mAllEvents) {
|
2013-06-14 10:10:22 -07:00
|
|
|
eventType.SetIsVoid(true);
|
|
|
|
} else {
|
2014-03-16 23:56:51 -07:00
|
|
|
eventType.Assign(Substring(nsDependentAtomString(listener.mTypeAtom), 2));
|
2013-06-14 10:10:22 -07:00
|
|
|
}
|
2013-04-16 14:15:57 -07:00
|
|
|
// EventListenerInfo is defined in XPCOM, so we have to go ahead
|
|
|
|
// and convert to an XPCOM callback here...
|
2014-03-16 23:56:50 -07:00
|
|
|
nsRefPtr<EventListenerInfo> info =
|
2014-03-16 23:56:51 -07:00
|
|
|
new EventListenerInfo(eventType, listener.mListener.ToXPCOMCallback(),
|
|
|
|
listener.mFlags.mCapture,
|
|
|
|
listener.mFlags.mAllowUntrustedEvents,
|
|
|
|
listener.mFlags.mInSystemGroup);
|
2011-08-08 11:26:26 -07:00
|
|
|
aList->AppendObject(info);
|
2009-10-16 01:57:32 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::HasUnloadListeners()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener = &mListeners.ElementAt(i);
|
|
|
|
if (listener->mEventType == NS_PAGE_UNLOAD ||
|
|
|
|
listener->mEventType == NS_BEFORE_PAGE_UNLOAD) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-08-24 12:49:25 -07:00
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::SetEventHandler(nsIAtom* aEventName,
|
|
|
|
const nsAString& aTypeString,
|
|
|
|
EventHandlerNonNull* aHandler)
|
2011-08-24 12:49:25 -07:00
|
|
|
{
|
2012-11-09 08:00:25 -08:00
|
|
|
if (!aHandler) {
|
2013-08-16 03:06:24 -07:00
|
|
|
RemoveEventHandler(aEventName, aTypeString);
|
2013-09-17 04:01:28 -07:00
|
|
|
return;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
// Untrusted events are always permitted for non-chrome script
|
|
|
|
// handlers.
|
2014-04-01 19:00:45 -07:00
|
|
|
SetEventHandlerInternal(aEventName, aTypeString, TypedEventHandler(aHandler),
|
2013-09-17 04:01:28 -07:00
|
|
|
!mIsMainThreadELM ||
|
|
|
|
!nsContentUtils::IsCallerChrome());
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::SetEventHandler(OnErrorEventHandlerNonNull* aHandler)
|
2012-11-09 08:00:25 -08:00
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
if (mIsMainThreadELM) {
|
|
|
|
if (!aHandler) {
|
|
|
|
RemoveEventHandler(nsGkAtoms::onerror, EmptyString());
|
|
|
|
return;
|
|
|
|
}
|
2012-06-14 10:22:34 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
// Untrusted events are always permitted for non-chrome script
|
|
|
|
// handlers.
|
2014-03-21 19:31:03 -07:00
|
|
|
SetEventHandlerInternal(nsGkAtoms::onerror, EmptyString(),
|
2014-04-01 19:00:45 -07:00
|
|
|
TypedEventHandler(aHandler),
|
2013-11-05 06:16:26 -08:00
|
|
|
!nsContentUtils::IsCallerChrome());
|
|
|
|
} else {
|
|
|
|
if (!aHandler) {
|
|
|
|
RemoveEventHandler(nullptr, NS_LITERAL_STRING("error"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Untrusted events are always permitted.
|
2014-03-21 19:31:03 -07:00
|
|
|
SetEventHandlerInternal(nullptr, NS_LITERAL_STRING("error"),
|
2014-04-01 19:00:45 -07:00
|
|
|
TypedEventHandler(aHandler), true);
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::SetEventHandler(
|
|
|
|
OnBeforeUnloadEventHandlerNonNull* aHandler)
|
2012-11-09 08:00:25 -08:00
|
|
|
{
|
|
|
|
if (!aHandler) {
|
2013-08-16 03:06:24 -07:00
|
|
|
RemoveEventHandler(nsGkAtoms::onbeforeunload, EmptyString());
|
2013-09-17 04:01:28 -07:00
|
|
|
return;
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Untrusted events are always permitted for non-chrome script
|
|
|
|
// handlers.
|
2014-03-21 19:31:03 -07:00
|
|
|
SetEventHandlerInternal(nsGkAtoms::onbeforeunload, EmptyString(),
|
2014-04-01 19:00:45 -07:00
|
|
|
TypedEventHandler(aHandler),
|
2013-09-17 04:01:28 -07:00
|
|
|
!mIsMainThreadELM ||
|
|
|
|
!nsContentUtils::IsCallerChrome());
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
|
|
|
|
2014-04-01 19:00:45 -07:00
|
|
|
const TypedEventHandler*
|
|
|
|
EventListenerManager::GetTypedEventHandler(nsIAtom* aEventName,
|
|
|
|
const nsAString& aTypeString)
|
2011-08-24 12:49:25 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t eventType = nsContentUtils::GetEventId(aEventName);
|
2014-03-16 23:56:51 -07:00
|
|
|
Listener* listener = FindEventHandler(eventType, aEventName, aTypeString);
|
2011-08-24 12:49:25 -07:00
|
|
|
|
2014-03-16 23:56:51 -07:00
|
|
|
if (!listener) {
|
2012-11-09 08:00:25 -08:00
|
|
|
return nullptr;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2014-04-01 19:00:46 -07:00
|
|
|
JSEventHandler* jsEventHandler = listener->GetJSEventHandler();
|
2014-03-16 23:56:51 -07:00
|
|
|
|
|
|
|
if (listener->mHandlerIsString) {
|
|
|
|
CompileEventHandlerInternal(listener, nullptr, nullptr);
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2014-04-01 19:00:46 -07:00
|
|
|
const TypedEventHandler& typedHandler =
|
|
|
|
jsEventHandler->GetTypedEventHandler();
|
2014-04-01 19:00:45 -07:00
|
|
|
return typedHandler.HasEventHandler() ? &typedHandler : nullptr;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2012-02-01 13:58:01 -08:00
|
|
|
size_t
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2011-08-26 15:39:00 -07:00
|
|
|
{
|
2012-02-01 13:58:01 -08:00
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += mListeners.SizeOfExcludingThis(aMallocSizeOf);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-04-01 19:00:46 -07:00
|
|
|
JSEventHandler* jsEventHandler =
|
|
|
|
mListeners.ElementAt(i).GetJSEventHandler();
|
|
|
|
if (jsEventHandler) {
|
|
|
|
n += jsEventHandler->SizeOfIncludingThis(aMallocSizeOf);
|
2011-08-26 15:39:00 -07:00
|
|
|
}
|
|
|
|
}
|
2012-02-01 13:58:01 -08:00
|
|
|
return n;
|
2011-08-26 15:39:00 -07:00
|
|
|
}
|
2012-01-26 06:45:12 -08:00
|
|
|
|
|
|
|
void
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::MarkForCC()
|
2012-01-26 06:45:12 -08:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2014-03-16 23:56:51 -07:00
|
|
|
const Listener& listener = mListeners.ElementAt(i);
|
2014-04-01 19:00:46 -07:00
|
|
|
JSEventHandler* jsEventHandler = listener.GetJSEventHandler();
|
|
|
|
if (jsEventHandler) {
|
2014-04-01 19:00:45 -07:00
|
|
|
const TypedEventHandler& typedHandler =
|
2014-04-01 19:00:46 -07:00
|
|
|
jsEventHandler->GetTypedEventHandler();
|
2014-04-01 19:00:45 -07:00
|
|
|
if (typedHandler.HasEventHandler()) {
|
|
|
|
JS::ExposeObjectToActiveJS(typedHandler.Ptr()->Callable());
|
2013-09-08 20:28:48 -07:00
|
|
|
}
|
2014-03-16 23:56:51 -07:00
|
|
|
} else if (listener.mListenerType == Listener::eWrappedJSListener) {
|
2014-03-16 23:56:51 -07:00
|
|
|
xpc_TryUnmarkWrappedGrayObject(listener.mListener.GetXPCOMCallback());
|
2014-03-16 23:56:51 -07:00
|
|
|
} else if (listener.mListenerType == Listener::eWebIDLListener) {
|
2013-04-16 14:15:57 -07:00
|
|
|
// Callback() unmarks gray
|
2014-03-16 23:56:51 -07:00
|
|
|
listener.mListener.GetWebIDLCallback()->Callback();
|
2012-01-26 06:45:12 -08:00
|
|
|
}
|
|
|
|
}
|
2012-10-16 18:22:02 -07:00
|
|
|
if (mRefCnt.IsPurple()) {
|
|
|
|
mRefCnt.RemovePurple();
|
|
|
|
}
|
2012-01-26 06:45:12 -08:00
|
|
|
}
|
2013-11-27 01:12:41 -08:00
|
|
|
|
2014-06-04 16:48:50 -07:00
|
|
|
void
|
|
|
|
EventListenerManager::TraceListeners(JSTracer* aTrc)
|
|
|
|
{
|
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
|
|
const Listener& listener = mListeners.ElementAt(i);
|
|
|
|
JSEventHandler* jsEventHandler = listener.GetJSEventHandler();
|
|
|
|
if (jsEventHandler) {
|
|
|
|
const TypedEventHandler& typedHandler =
|
|
|
|
jsEventHandler->GetTypedEventHandler();
|
|
|
|
if (typedHandler.HasEventHandler()) {
|
|
|
|
mozilla::TraceScriptHolder(typedHandler.Ptr(), aTrc);
|
|
|
|
}
|
|
|
|
} else if (listener.mListenerType == Listener::eWebIDLListener) {
|
|
|
|
mozilla::TraceScriptHolder(listener.mListener.GetWebIDLCallback(), aTrc);
|
|
|
|
}
|
|
|
|
// We might have eWrappedJSListener, but that is the legacy type for
|
|
|
|
// JS implemented event listeners, and trickier to handle here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 01:12:41 -08:00
|
|
|
already_AddRefed<nsIScriptGlobalObject>
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager::GetScriptGlobalAndDocument(nsIDocument** aDoc)
|
2013-11-27 01:12:41 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node(do_QueryInterface(mTarget));
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global;
|
|
|
|
if (node) {
|
|
|
|
// Try to get context from doc
|
|
|
|
// XXX sXBL/XBL2 issue -- do we really want the owner here? What
|
|
|
|
// if that's the XBL document?
|
|
|
|
doc = node->OwnerDoc();
|
2013-12-13 06:36:29 -08:00
|
|
|
if (doc->IsLoadedAsData()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-11-27 01:12:41 -08:00
|
|
|
|
|
|
|
// We want to allow compiling an event handler even in an unloaded
|
|
|
|
// document, so use GetScopeObject here, not GetScriptHandlingObject.
|
|
|
|
global = do_QueryInterface(doc->GetScopeObject());
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = GetTargetAsInnerWindow();
|
|
|
|
if (win) {
|
|
|
|
doc = win->GetExtantDoc();
|
|
|
|
global = do_QueryInterface(win);
|
|
|
|
} else {
|
|
|
|
global = do_QueryInterface(mTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doc.forget(aDoc);
|
|
|
|
return global.forget();
|
|
|
|
}
|
2014-03-16 23:56:53 -07:00
|
|
|
|
|
|
|
} // namespace mozilla
|