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
|
|
|
|
2013-09-25 04:21:22 -07:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2013-08-29 14:18:25 -07:00
|
|
|
#ifdef MOZ_B2G
|
2012-03-24 17:29:49 -07:00
|
|
|
#include "mozilla/Hal.h"
|
2013-08-29 14:18:25 -07:00
|
|
|
#endif
|
2012-03-24 17:29:49 -07:00
|
|
|
#include "mozilla/HalSensor.h"
|
2012-03-24 17:30:03 -07:00
|
|
|
|
|
|
|
// Microsoft's API Name hackery sucks
|
|
|
|
#undef CreateEvent
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
#include "nsEventListenerManager.h"
|
2011-08-08 11:26:26 -07:00
|
|
|
#include "nsIDOMEventListener.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIJSEventListener.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsIContent.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocument.h"
|
2013-09-24 03:04:16 -07:00
|
|
|
#include "mozilla/MutationEvent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsDOMCID.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
2009-10-16 01:57:32 -07:00
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsEventListenerService.h"
|
2010-02-23 13:20:46 -08:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2012-01-26 06:45:12 -08:00
|
|
|
#include "xpcpublic.h"
|
2012-08-20 11:34:32 -07:00
|
|
|
#include "nsSandboxFlags.h"
|
2013-11-26 08:47:52 -08:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-16 14:15:57 -07:00
|
|
|
using namespace mozilla;
|
2010-04-30 06:12:05 -07:00
|
|
|
using namespace mozilla::dom;
|
2012-03-24 17:29:49 -07:00
|
|
|
using namespace mozilla::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;
|
|
|
|
}
|
|
|
|
|
2013-06-28 02:53:37 -07:00
|
|
|
uint32_t nsEventListenerManager::sMainThreadCreatedCount = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-23 04:38:01 -07:00
|
|
|
nsEventListenerManager::nsEventListenerManager(EventTarget* aTarget) :
|
2011-10-17 07:59:28 -07:00
|
|
|
mMayHavePaintEventListener(false),
|
|
|
|
mMayHaveMutationListeners(false),
|
|
|
|
mMayHaveCapturingListeners(false),
|
|
|
|
mMayHaveSystemGroupListeners(false),
|
|
|
|
mMayHaveAudioAvailableEventListener(false),
|
|
|
|
mMayHaveTouchEventListener(false),
|
|
|
|
mMayHaveMouseEnterLeaveEventListener(false),
|
2012-11-30 01:13:16 -08:00
|
|
|
mClearingListeners(false),
|
2013-08-16 03:06:24 -07:00
|
|
|
mIsMainThreadELM(NS_IsMainThread()),
|
2011-06-29 13:40:01 -07:00
|
|
|
mNoListenerForEvent(0),
|
2011-06-23 19:18:01 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
nsEventListenerManager::~nsEventListenerManager()
|
|
|
|
{
|
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
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventListenerManager::RemoveAllListeners()
|
|
|
|
{
|
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
|
|
|
|
nsEventListenerManager::Shutdown()
|
|
|
|
{
|
|
|
|
nsDOMEvent::Shutdown();
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:01 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsEventListenerManager, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsEventListenerManager, Release)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-23 11:13:39 -08:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
nsListenerStruct& aField,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsEventListenerManager)
|
|
|
|
|
2012-11-22 09:15:38 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsEventListenerManager)
|
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
|
|
|
|
|
2012-11-22 09:15:38 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsEventListenerManager)
|
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*
|
|
|
|
nsEventListenerManager::GetInnerWindowForTarget()
|
|
|
|
{
|
|
|
|
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>
|
|
|
|
nsEventListenerManager::GetTargetAsInnerWindow() const
|
|
|
|
{
|
|
|
|
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
|
2012-12-15 17:26:05 -08:00
|
|
|
nsEventListenerManager::AddEventListenerInternal(
|
2013-04-16 14:16:08 -07:00
|
|
|
const EventListenerHolder& aListener,
|
2012-12-15 17:26:05 -08:00
|
|
|
uint32_t aType,
|
|
|
|
nsIAtom* aTypeAtom,
|
2013-08-16 03:06:24 -07:00
|
|
|
const nsAString& aTypeString,
|
2012-12-15 17:26:05 -08:00
|
|
|
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
|
|
|
|
2012-11-30 01:13:16 -08:00
|
|
|
if (!aListener || 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
|
|
|
|
2008-02-06 20:02:25 -08:00
|
|
|
nsListenerStruct* ls;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2008-02-06 20:02:25 -08:00
|
|
|
ls = &mListeners.ElementAt(i);
|
2013-11-08 15:29:02 -08:00
|
|
|
// mListener == aListener is the last one, since it can be a bit slow.
|
|
|
|
if (ls->mListenerIsHandler == aHandler &&
|
2012-08-30 19:52:32 -07:00
|
|
|
ls->mFlags == aFlags &&
|
2013-11-08 15:29:02 -08:00
|
|
|
EVENT_TYPE_EQUALS(ls, aType, aTypeAtom, aTypeString, aAllEvents) &&
|
|
|
|
ls->mListener == aListener) {
|
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
|
|
|
|
2012-11-01 05:42:29 -07:00
|
|
|
ls = aAllEvents ? mListeners.InsertElementAt(0) : mListeners.AppendElement();
|
2007-03-22 10:30:00 -07:00
|
|
|
ls->mListener = aListener;
|
2013-08-16 03:06:24 -07:00
|
|
|
MOZ_ASSERT(aType < PR_UINT16_MAX);
|
2007-03-22 10:30:00 -07:00
|
|
|
ls->mEventType = aType;
|
2013-08-16 03:06:24 -07:00
|
|
|
ls->mTypeString = aTypeString;
|
2007-03-22 10:30:00 -07:00
|
|
|
ls->mTypeAtom = aTypeAtom;
|
|
|
|
ls->mFlags = aFlags;
|
2012-08-30 19:52:32 -07:00
|
|
|
ls->mListenerIsHandler = aHandler;
|
2011-10-17 07:59:28 -07:00
|
|
|
ls->mHandlerIsString = false;
|
2012-11-01 05:42:29 -07:00
|
|
|
ls->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) {
|
2013-04-16 14:16:08 -07:00
|
|
|
MOZ_ASSERT(!aListener.HasWebIDLCallback());
|
2012-06-28 14:47:56 -07:00
|
|
|
ls->mListenerType = eJSEventListener;
|
2013-04-16 14:16:08 -07:00
|
|
|
} else if (aListener.HasWebIDLCallback()) {
|
|
|
|
ls->mListenerType = eWebIDLListener;
|
|
|
|
} else if ((wjs = do_QueryInterface(aListener.GetXPCOMCallback()))) {
|
2012-06-28 14:47:56 -07:00
|
|
|
ls->mListenerType = eWrappedJSListener;
|
|
|
|
} else {
|
|
|
|
ls->mListenerType = 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
|
|
|
}
|
2010-08-25 06:10:00 -07:00
|
|
|
} else if (aType == NS_MOZAUDIOAVAILABLE) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mMayHaveAudioAvailableEventListener = true;
|
2010-08-25 06:10:00 -07:00
|
|
|
nsPIDOMWindow* window = GetInnerWindowForTarget();
|
|
|
|
if (window) {
|
|
|
|
window->SetHasAudioAvailableEventListeners();
|
|
|
|
}
|
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::ontouchenter ||
|
|
|
|
aTypeAtom == nsGkAtoms::ontouchleave ||
|
|
|
|
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
|
|
|
}
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEventListenerManager::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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEventListenerManager::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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsEventListenerManager::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
|
2012-12-15 17:26:05 -08:00
|
|
|
nsEventListenerManager::RemoveEventListenerInternal(
|
2013-04-16 14:16:08 -07:00
|
|
|
const EventListenerHolder& aListener,
|
2012-12-15 17:26:05 -08:00
|
|
|
uint32_t aType,
|
|
|
|
nsIAtom* aUserType,
|
2013-08-16 03:06:24 -07:00
|
|
|
const nsAString& aTypeString,
|
2012-12-15 17:26:05 -08:00
|
|
|
const EventListenerFlags& aFlags,
|
|
|
|
bool aAllEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-30 01:13:16 -08:00
|
|
|
if (!aListener || !aType || mClearingListeners) {
|
2011-06-23 19:18:02 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-06 20:02:25 -08:00
|
|
|
nsListenerStruct* ls;
|
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) {
|
2008-02-06 20:02:25 -08:00
|
|
|
ls = &mListeners.ElementAt(i);
|
2013-08-16 03:06:24 -07:00
|
|
|
if (EVENT_TYPE_EQUALS(ls, aType, aUserType, aTypeString, aAllEvents)) {
|
2012-03-29 10:23:27 -07:00
|
|
|
++typeCount;
|
|
|
|
if (ls->mListener == aListener &&
|
2012-12-15 17:26:05 -08:00
|
|
|
ls->mFlags.EqualsIgnoringTrustness(aFlags)) {
|
2012-03-29 10:23:27 -07:00
|
|
|
nsRefPtr<nsEventListenerManager> kungFuDeathGrip = this;
|
|
|
|
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
|
2013-10-01 20:46:04 -07:00
|
|
|
nsEventListenerManager::ListenerCanHandle(nsListenerStruct* aLs,
|
|
|
|
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
|
|
|
|
// aLs=>mEventType != NS_USER_DEFINED_EVENT as long as the atoms are the same
|
2013-08-16 03:06:24 -07:00
|
|
|
if (aLs->mAllEvents) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (aEvent->message == NS_USER_DEFINED_EVENT) {
|
|
|
|
if (mIsMainThreadELM) {
|
|
|
|
return aLs->mTypeAtom == aEvent->userType;
|
|
|
|
}
|
|
|
|
return aLs->mTypeString.Equals(aEvent->typeString);
|
|
|
|
}
|
2013-12-20 16:22:13 -08:00
|
|
|
MOZ_ASSERT(mIsMainThreadELM);
|
2013-08-16 03:06:24 -07:00
|
|
|
return aLs->mEventType == aEvent->message;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-18 02:45:00 -07:00
|
|
|
void
|
2013-04-16 14:16:08 -07:00
|
|
|
nsEventListenerManager::AddEventListenerByType(const EventListenerHolder& aListener,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString& aType,
|
2012-12-15 17:26:05 -08:00
|
|
|
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);
|
2013-08-16 03:06:24 -07:00
|
|
|
AddEventListenerInternal(aListener, type, atom, aType, aFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
void
|
2012-12-15 17:26:05 -08:00
|
|
|
nsEventListenerManager::RemoveEventListenerByType(
|
2013-04-16 14:16:08 -07:00
|
|
|
const EventListenerHolder& aListener,
|
2012-12-15 17:26:05 -08: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);
|
2013-08-16 03:06:24 -07:00
|
|
|
RemoveEventListenerInternal(aListener, type, atom, aType, aFlags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsListenerStruct*
|
2012-08-30 09:25:10 -07:00
|
|
|
nsEventListenerManager::FindEventHandler(uint32_t aEventType,
|
2013-08-16 03:06:24 -07:00
|
|
|
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
|
|
|
|
nsListenerStruct *ls;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2008-02-06 20:02:25 -08:00
|
|
|
ls = &mListeners.ElementAt(i);
|
2012-11-01 05:42:29 -07:00
|
|
|
if (ls->mListenerIsHandler &&
|
2013-08-16 03:06:24 -07:00
|
|
|
EVENT_TYPE_EQUALS(ls, aEventType, aTypeAtom, aTypeString, false)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return ls;
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
nsListenerStruct*
|
2013-11-27 01:12:41 -08:00
|
|
|
nsEventListenerManager::SetEventHandlerInternal(JS::Handle<JSObject*> aScopeObject,
|
2012-08-30 09:25:10 -07:00
|
|
|
nsIAtom* aName,
|
2013-08-16 03:06:24 -07:00
|
|
|
const nsAString& aTypeString,
|
2012-11-09 08:00:25 -08:00
|
|
|
const nsEventHandler& aHandler,
|
2013-09-17 04:01:28 -07:00
|
|
|
bool aPermitUntrustedEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-27 01:12:41 -08:00
|
|
|
MOZ_ASSERT(aScopeObject || aHandler.HasEventHandler(),
|
2013-09-17 04:01:28 -07:00
|
|
|
"Must have one or the other!");
|
|
|
|
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);
|
2013-08-16 03:06:24 -07:00
|
|
|
nsListenerStruct* ls = FindEventHandler(eventType, aName, aTypeString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!ls) {
|
|
|
|
// 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
|
|
|
|
|
|
|
nsCOMPtr<nsIJSEventListener> scriptListener;
|
2013-11-27 01:12:41 -08:00
|
|
|
NS_NewJSEventListener(aScopeObject, mTarget, aName,
|
2013-09-17 04:01:28 -07:00
|
|
|
aHandler, getter_AddRefs(scriptListener));
|
|
|
|
EventListenerHolder holder(scriptListener);
|
|
|
|
AddEventListenerInternal(holder, eventType, aName, aTypeString, flags,
|
|
|
|
true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
ls = FindEventHandler(eventType, aName, aTypeString);
|
2011-08-24 12:49:25 -07:00
|
|
|
} else {
|
2012-08-30 20:45:16 -07:00
|
|
|
nsIJSEventListener* scriptListener = ls->GetJSListener();
|
2012-11-09 08:00:25 -08:00
|
|
|
MOZ_ASSERT(scriptListener,
|
|
|
|
"How can we have an event handler with no nsIJSEventListener?");
|
2012-08-30 20:45:16 -07:00
|
|
|
|
2013-05-24 10:22:20 -07:00
|
|
|
bool same = scriptListener->GetHandler() == aHandler;
|
|
|
|
// Possibly the same listener, but update still the context and scope.
|
2013-11-27 01:12:41 -08:00
|
|
|
scriptListener->SetHandler(aHandler, aScopeObject);
|
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
|
|
|
|
ls->mHandlerIsString = !aHandler.HasEventHandler();
|
|
|
|
if (aPermitUntrustedEvents) {
|
|
|
|
ls->mFlags.mAllowUntrustedEvents = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
return ls;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
nsresult
|
2012-08-30 09:25:10 -07:00
|
|
|
nsEventListenerManager::SetEventHandler(nsIAtom *aName,
|
|
|
|
const nsAString& aBody,
|
|
|
|
uint32_t aLanguage,
|
|
|
|
bool aDeferCompilation,
|
2013-11-26 08:44:37 -08:00
|
|
|
bool aPermitUntrustedEvents,
|
|
|
|
Element* aElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aLanguage != nsIProgrammingLanguage::UNKNOWN,
|
|
|
|
"Must know the language for the script event listener");
|
|
|
|
|
|
|
|
// |aPermitUntrustedEvents| is set to False for chrome - events
|
|
|
|
// *generated* from an unknown source are not allowed.
|
|
|
|
// However, for script languages with no 'sandbox', we want to reject
|
|
|
|
// such scripts based on the source of their code, not just the source
|
|
|
|
// of the event.
|
|
|
|
if (aPermitUntrustedEvents &&
|
|
|
|
aLanguage != nsIProgrammingLanguage::JAVASCRIPT) {
|
|
|
|
NS_WARNING("Discarding non-JS event listener from untrusted source");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-06 05:36:10 -08:00
|
|
|
NS_ENSURE_STATE(global->GetGlobalJSObject());
|
|
|
|
|
2013-06-20 11:05:34 -07:00
|
|
|
JSAutoRequest ar(context->GetNativeContext());
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JSObject*> scope(context->GetNativeContext(),
|
|
|
|
global->GetGlobalJSObject());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-27 01:12:41 -08:00
|
|
|
nsListenerStruct* ls = SetEventHandlerInternal(scope, aName,
|
2013-09-17 04:01:28 -07:00
|
|
|
EmptyString(),
|
|
|
|
nsEventHandler(),
|
|
|
|
aPermitUntrustedEvents);
|
2009-03-14 20:42:50 -07:00
|
|
|
|
2011-08-24 12:49:25 -07:00
|
|
|
if (!aDeferCompilation) {
|
2013-11-26 08:44:37 -08:00
|
|
|
return CompileEventHandlerInternal(ls, &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
|
2013-08-16 03:06:24 -07:00
|
|
|
nsEventListenerManager::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);
|
2013-08-16 03:06:24 -07:00
|
|
|
nsListenerStruct* ls = FindEventHandler(eventType, aName, aTypeString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (ls) {
|
2012-08-22 08:56:38 -07:00
|
|
|
mListeners.RemoveElementAt(uint32_t(ls - &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
|
2011-08-24 12:49:25 -07:00
|
|
|
nsEventListenerManager::CompileEventHandlerInternal(nsListenerStruct *aListenerStruct,
|
2013-11-26 08:44:37 -08:00
|
|
|
const nsAString* aBody,
|
|
|
|
Element* aElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-08-24 12:49:25 -07:00
|
|
|
NS_PRECONDITION(aListenerStruct->GetJSListener(),
|
|
|
|
"Why do we not have a JS listener?");
|
|
|
|
NS_PRECONDITION(aListenerStruct->mHandlerIsString,
|
|
|
|
"Why are we compiling a non-string JS listener?");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2011-08-24 12:49:25 -07:00
|
|
|
nsIJSEventListener *listener = aListenerStruct->GetJSListener();
|
2012-11-09 08:00:25 -08:00
|
|
|
NS_ASSERTION(!listener->GetHandler().HasEventHandler(),
|
|
|
|
"What is there to compile?");
|
2011-08-24 12:49:25 -07:00
|
|
|
|
2013-11-27 01:12:41 -08:00
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global =
|
|
|
|
GetScriptGlobalAndDocument(getter_AddRefs(doc));
|
|
|
|
NS_ENSURE_STATE(global);
|
|
|
|
|
|
|
|
nsIScriptContext* context = global->GetScriptContext();
|
|
|
|
NS_ENSURE_STATE(context);
|
|
|
|
|
2013-12-13 18:22:18 -08:00
|
|
|
// Push a context to make sure exceptions are reported in the right place.
|
|
|
|
AutoPushJSContext cx(context->GetNativeContext());
|
2013-03-20 13:09:09 -07:00
|
|
|
JS::Rooted<JSObject*> handler(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-26 08:47:52 -08:00
|
|
|
JS::Rooted<JSObject*> scope(cx, listener->GetEventScope());
|
|
|
|
|
|
|
|
nsIAtom* attrName = aListenerStruct->mTypeAtom;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aListenerStruct->mHandlerIsString) {
|
2011-08-24 12:49:25 -07:00
|
|
|
// OK, we didn't find an existing compiled event handler. Flag us
|
|
|
|
// as not a string so we don't keep trying to compile strings
|
|
|
|
// which can't be compiled
|
2011-10-17 07:59:28 -07:00
|
|
|
aListenerStruct->mHandlerIsString = false;
|
2011-08-24 12:49:25 -07:00
|
|
|
|
2013-11-26 08:47:52 -08:00
|
|
|
// mTarget may not be an Element if it's a window and we're
|
2011-08-24 12:49:25 -07:00
|
|
|
// 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
|
|
|
|
// the nsIJSEventListener itself, and that still doesn't address
|
|
|
|
// the arg names issue.
|
2013-11-26 08:47:52 -08:00
|
|
|
nsCOMPtr<Element> element = do_QueryInterface(mTarget);
|
|
|
|
MOZ_ASSERT(element || aBody, "Where will we get our body?");
|
2011-08-24 12:49:25 -07:00
|
|
|
nsAutoString handlerBody;
|
|
|
|
const nsAString* body = aBody;
|
2013-11-26 08:47:52 -08:00
|
|
|
if (!aBody) {
|
2011-08-24 12:49:25 -07:00
|
|
|
if (aListenerStruct->mTypeAtom == nsGkAtoms::onSVGLoad)
|
2007-03-22 10:30:00 -07:00
|
|
|
attrName = nsGkAtoms::onload;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onSVGUnload)
|
2007-03-22 10:30:00 -07:00
|
|
|
attrName = nsGkAtoms::onunload;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onSVGAbort)
|
2007-03-22 10:30:00 -07:00
|
|
|
attrName = nsGkAtoms::onabort;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onSVGError)
|
2007-03-22 10:30:00 -07:00
|
|
|
attrName = nsGkAtoms::onerror;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onSVGResize)
|
2007-03-22 10:30:00 -07:00
|
|
|
attrName = nsGkAtoms::onresize;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onSVGScroll)
|
2007-03-22 10:30:00 -07:00
|
|
|
attrName = nsGkAtoms::onscroll;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onSVGZoom)
|
2007-03-22 10:30:00 -07:00
|
|
|
attrName = nsGkAtoms::onzoom;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onbeginEvent)
|
2010-07-31 00:02:52 -07:00
|
|
|
attrName = nsGkAtoms::onbegin;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onrepeatEvent)
|
2010-07-31 00:02:52 -07:00
|
|
|
attrName = nsGkAtoms::onrepeat;
|
2011-08-24 12:49:25 -07:00
|
|
|
else if (aListenerStruct->mTypeAtom == nsGkAtoms::onendEvent)
|
2010-07-31 00:02:52 -07:00
|
|
|
attrName = nsGkAtoms::onend;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-26 08:47:52 -08:00
|
|
|
element->GetAttr(kNameSpaceID_None, attrName, handlerBody);
|
2011-08-24 12:49:25 -07:00
|
|
|
body = &handlerBody;
|
2013-11-26 08:47:52 -08:00
|
|
|
aElement = element;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t lineNo = 0;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString url (NS_LITERAL_CSTRING("-moz-evil:lying-event-listener"));
|
2013-11-26 08:47:52 -08:00
|
|
|
MOZ_ASSERT(body);
|
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
nsIURI *uri = aElement->OwnerDoc()->GetDocumentURI();
|
|
|
|
if (uri) {
|
|
|
|
uri->GetSpec(url);
|
|
|
|
lineNo = 1;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
2009-03-14 20:42:50 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t argCount;
|
2012-07-14 04:28:35 -07:00
|
|
|
const char **argNames;
|
2013-11-26 08:47:52 -08:00
|
|
|
nsContentUtils::GetEventArgNames(aElement->GetNameSpaceID(),
|
2012-07-14 04:28:35 -07:00
|
|
|
aListenerStruct->mTypeAtom,
|
|
|
|
&argCount, &argNames);
|
|
|
|
|
2013-09-04 14:06:57 -07:00
|
|
|
JSAutoCompartment ac(cx, context->GetWindowProxy());
|
2013-01-22 22:12:50 -08:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setFileAndLine(url.get(), lineNo)
|
2013-01-22 22:18:51 -08:00
|
|
|
.setVersion(SCRIPTVERSION_DEFAULT);
|
2013-01-22 22:12:50 -08:00
|
|
|
|
2013-11-26 08:47:52 -08:00
|
|
|
JS::Rooted<JS::Value> targetVal(cx);
|
|
|
|
// Go ahead and wrap into the current compartment of cx directly.
|
|
|
|
JS::Rooted<JSObject*> wrapScope(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
if (WrapNewBindingObject(cx, wrapScope, aElement, &targetVal)) {
|
|
|
|
MOZ_ASSERT(targetVal.isObject());
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
options.setElement(&targetVal.toObject())
|
|
|
|
.setElementAttributeName(jsStr);
|
|
|
|
}
|
|
|
|
|
2013-05-13 10:43:53 -07:00
|
|
|
JS::Rooted<JSObject*> handlerFun(cx);
|
|
|
|
result = nsJSUtils::CompileFunction(cx, JS::NullPtr(), options,
|
2013-01-22 22:12:50 -08:00
|
|
|
nsAtomCString(aListenerStruct->mTypeAtom),
|
2013-05-13 10:43:53 -07:00
|
|
|
argCount, argNames, *body, handlerFun.address());
|
2012-07-14 04:28:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2013-03-20 13:09:09 -07:00
|
|
|
handler = handlerFun;
|
2013-01-22 22:12:50 -08:00
|
|
|
NS_ENSURE_TRUE(handler, NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-24 12:49:25 -07:00
|
|
|
if (handler) {
|
2013-11-27 01:12:41 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(mTarget);
|
2011-08-24 12:49:25 -07:00
|
|
|
// Bind it
|
2013-03-20 13:09:09 -07:00
|
|
|
JS::Rooted<JSObject*> boundHandler(cx);
|
2013-05-11 22:17:42 -07:00
|
|
|
context->BindCompiledEventHandler(mTarget, scope, handler, &boundHandler);
|
2013-11-17 19:54:35 -08:00
|
|
|
aListenerStruct = nullptr;
|
2013-12-11 17:51:58 -08:00
|
|
|
// Note - We pass null for aIncumbentGlobal below. We could also pass the
|
|
|
|
// compilation global, but since the handler is guaranteed to be scripted,
|
|
|
|
// there's no need to use an override, since the JS engine will always give
|
|
|
|
// us the right answer.
|
2013-09-26 13:01:19 -07:00
|
|
|
if (!boundHandler) {
|
|
|
|
listener->ForgetHandler();
|
|
|
|
} else if (listener->EventName() == nsGkAtoms::onerror && win) {
|
2012-11-09 08:00:25 -08:00
|
|
|
nsRefPtr<OnErrorEventHandlerNonNull> handlerCallback =
|
2013-12-11 17:51:58 -08:00
|
|
|
new OnErrorEventHandlerNonNull(boundHandler, /* aIncumbentGlobal = */ nullptr);
|
2012-11-09 08:00:25 -08:00
|
|
|
listener->SetHandler(handlerCallback);
|
2012-11-14 10:05:41 -08:00
|
|
|
} else if (listener->EventName() == nsGkAtoms::onbeforeunload && win) {
|
2013-10-08 08:51:15 -07:00
|
|
|
nsRefPtr<OnBeforeUnloadEventHandlerNonNull> handlerCallback =
|
2013-12-11 17:51:58 -08:00
|
|
|
new OnBeforeUnloadEventHandlerNonNull(boundHandler, /* aIncumbentGlobal = */ nullptr);
|
2012-11-09 08:00:25 -08:00
|
|
|
listener->SetHandler(handlerCallback);
|
|
|
|
} else {
|
|
|
|
nsRefPtr<EventHandlerNonNull> handlerCallback =
|
2013-12-11 17:51:58 -08:00
|
|
|
new EventHandlerNonNull(boundHandler, /* aIncumbentGlobal = */ nullptr);
|
2012-11-09 08:00:25 -08:00
|
|
|
listener->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
|
|
|
|
nsEventListenerManager::HandleEventSubType(nsListenerStruct* aListenerStruct,
|
|
|
|
nsIDOMEvent* aDOMEvent,
|
2013-12-13 18:22:18 -08:00
|
|
|
EventTarget* aCurrentTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
2013-11-17 19:54:35 -08:00
|
|
|
EventListenerHolder listener(aListenerStruct->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
|
2012-06-28 14:47:56 -07:00
|
|
|
if ((aListenerStruct->mListenerType == eJSEventListener) &&
|
2007-03-22 10:30:00 -07:00
|
|
|
aListenerStruct->mHandlerIsString) {
|
2013-11-26 08:44:37 -08:00
|
|
|
result = CompileEventHandlerInternal(aListenerStruct, nullptr, nullptr);
|
2013-11-17 19:54:35 -08:00
|
|
|
aListenerStruct = 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();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsIDOMEvent::currentTarget is set in nsEventDispatcher.
|
2013-11-17 19:54:35 -08:00
|
|
|
if (listener.HasWebIDLCallback()) {
|
2013-04-16 14:15:57 -07:00
|
|
|
ErrorResult rv;
|
2013-11-17 19:54:35 -08:00
|
|
|
listener.GetWebIDLCallback()->
|
2013-04-16 14:15:57 -07:00
|
|
|
HandleEvent(aCurrentTarget, *(aDOMEvent->InternalDOMEvent()), rv);
|
|
|
|
result = rv.ErrorCode();
|
|
|
|
} else {
|
2013-11-17 19:54:35 -08:00
|
|
|
result = listener.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
|
2010-04-16 02:04:41 -07:00
|
|
|
nsEventListenerManager::HandleEventInternal(nsPresContext* aPresContext,
|
2013-10-01 20:46:04 -07:00
|
|
|
WidgetEvent* aEvent,
|
2010-04-16 02:04:41 -07:00
|
|
|
nsIDOMEvent** aDOMEvent,
|
2013-04-05 17:44:15 -07:00
|
|
|
EventTarget* aCurrentTarget,
|
2013-12-13 18:22:18 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-06 20:02:25 -08:00
|
|
|
nsAutoTObserverArray<nsListenerStruct, 2>::EndLimitedIterator iter(mListeners);
|
2013-11-05 06:16:26 -08:00
|
|
|
Maybe<nsAutoPopupStatePusher> popupStatePusher;
|
|
|
|
if (mIsMainThreadELM) {
|
|
|
|
popupStatePusher.construct(nsDOMEvent::GetEventPopupControlState(aEvent));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2008-02-06 20:02:25 -08:00
|
|
|
nsListenerStruct* ls = &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.
|
2011-08-08 11:26:26 -07:00
|
|
|
if (ListenerCanHandle(ls, aEvent)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
hasListener = true;
|
2012-12-15 17:26:05 -08:00
|
|
|
if (ls->IsListening(aEvent) &&
|
2012-12-15 17:26:05 -08:00
|
|
|
(aEvent->mFlags.mIsTrusted || ls->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.
|
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> et =
|
|
|
|
do_QueryInterface(aEvent->originalTarget);
|
|
|
|
nsEventDispatcher::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
|
|
|
|
2013-12-13 18:22:18 -08:00
|
|
|
if (NS_FAILED(HandleEventSubType(ls, *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
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventListenerManager::Disconnect()
|
|
|
|
{
|
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
|
2011-06-23 19:18:02 -07:00
|
|
|
nsEventListenerManager::AddEventListener(const nsAString& aType,
|
2013-04-16 14:16:08 -07:00
|
|
|
const EventListenerHolder& aListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
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;
|
2011-06-23 19:18:02 -07:00
|
|
|
return AddEventListenerByType(aListener, 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
|
|
|
|
nsEventListenerManager::RemoveEventListener(const nsAString& aType,
|
2013-04-16 14:16:08 -07:00
|
|
|
const EventListenerHolder& aListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aUseCapture)
|
2011-06-23 19:18:02 -07:00
|
|
|
{
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mCapture = aUseCapture;
|
2011-06-23 19:18:02 -07:00
|
|
|
RemoveEventListenerByType(aListener, aType, flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-01 05:42:29 -07:00
|
|
|
void
|
|
|
|
nsEventListenerManager::AddListenerForAllEvents(nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
|
|
|
bool aSystemEventGroup)
|
|
|
|
{
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mCapture = aUseCapture;
|
|
|
|
flags.mAllowUntrustedEvents = aWantsUntrusted;
|
|
|
|
flags.mInSystemGroup = aSystemEventGroup;
|
2013-04-16 14:16:08 -07:00
|
|
|
EventListenerHolder holder(aListener);
|
2013-09-24 03:04:16 -07:00
|
|
|
AddEventListenerInternal(holder, 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
|
|
|
|
nsEventListenerManager::RemoveListenerForAllEvents(nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aSystemEventGroup)
|
|
|
|
{
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mCapture = aUseCapture;
|
|
|
|
flags.mInSystemGroup = aSystemEventGroup;
|
2013-04-16 14:16:08 -07:00
|
|
|
EventListenerHolder holder(aListener);
|
2013-09-24 03:04:16 -07:00
|
|
|
RemoveEventListenerInternal(holder, NS_EVENT_ALL, nullptr, EmptyString(),
|
2013-08-16 03:06:24 -07:00
|
|
|
flags, true);
|
2012-11-01 05:42:29 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-06-23 19:18:02 -07:00
|
|
|
nsEventListenerManager::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) {
|
2008-02-06 20:02:25 -08:00
|
|
|
nsListenerStruct* ls = &mListeners.ElementAt(i);
|
2007-12-13 17:41:48 -08:00
|
|
|
if (ls->mEventType >= NS_MUTATION_START &&
|
2007-03-22 10:30:00 -07:00
|
|
|
ls->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
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventListenerManager::MutationListenerBits()
|
|
|
|
{
|
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) {
|
2008-02-06 20:02:25 -08:00
|
|
|
nsListenerStruct* ls = &mListeners.ElementAt(i);
|
2007-12-13 17:41:48 -08:00
|
|
|
if (ls->mEventType >= NS_MUTATION_START &&
|
|
|
|
ls->mEventType <= NS_MUTATION_END) {
|
2007-03-24 05:18:02 -07:00
|
|
|
if (ls->mEventType == NS_MUTATION_SUBTREEMODIFIED) {
|
|
|
|
return kAllMutationBits;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
bits |= MutationBitForEventType(ls->mEventType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-05-08 14:13:13 -07:00
|
|
|
nsEventListenerManager::HasListenersFor(const nsAString& aEventName)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
nsEventListenerManager::HasListenersFor(nsIAtom* aEventNameWithOn)
|
|
|
|
{
|
|
|
|
#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) {
|
2008-02-06 20:02:25 -08:00
|
|
|
nsListenerStruct* ls = &mListeners.ElementAt(i);
|
2012-08-17 07:12:58 -07:00
|
|
|
if (ls->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
|
2008-09-30 17:51:53 -07:00
|
|
|
nsEventListenerManager::HasListeners()
|
|
|
|
{
|
|
|
|
return !mListeners.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2009-10-16 01:57:32 -07:00
|
|
|
nsresult
|
|
|
|
nsEventListenerManager::GetListenerInfo(nsCOMArray<nsIEventListenerInfo>* aList)
|
|
|
|
{
|
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) {
|
2009-10-16 01:57:32 -07:00
|
|
|
const nsListenerStruct& ls = mListeners.ElementAt(i);
|
|
|
|
// 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
|
2012-06-28 14:47:56 -07:00
|
|
|
if ((ls.mListenerType == eJSEventListener) && ls.mHandlerIsString) {
|
2013-11-26 08:44:37 -08:00
|
|
|
CompileEventHandlerInternal(const_cast<nsListenerStruct*>(&ls), nullptr,
|
|
|
|
nullptr);
|
2009-10-16 01:57:32 -07:00
|
|
|
}
|
2013-06-14 10:10:22 -07:00
|
|
|
nsAutoString eventType;
|
|
|
|
if (ls.mAllEvents) {
|
|
|
|
eventType.SetIsVoid(true);
|
|
|
|
} else {
|
|
|
|
eventType.Assign(Substring(nsDependentAtomString(ls.mTypeAtom), 2));
|
|
|
|
}
|
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...
|
2011-08-08 11:26:26 -07:00
|
|
|
nsRefPtr<nsEventListenerInfo> info =
|
2013-04-16 14:15:57 -07:00
|
|
|
new nsEventListenerInfo(eventType, ls.mListener.ToXPCOMCallback(),
|
|
|
|
ls.mFlags.mCapture,
|
2012-12-15 17:26:05 -08:00
|
|
|
ls.mFlags.mAllowUntrustedEvents,
|
|
|
|
ls.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
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEventListenerManager::HasUnloadListeners()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mListeners.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2008-02-06 20:02:25 -08:00
|
|
|
nsListenerStruct* ls = &mListeners.ElementAt(i);
|
2007-12-13 17:41:48 -08:00
|
|
|
if (ls->mEventType == NS_PAGE_UNLOAD ||
|
2011-08-08 11:26:26 -07:00
|
|
|
ls->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
|
2012-11-09 08:00:25 -08:00
|
|
|
nsEventListenerManager::SetEventHandler(nsIAtom* aEventName,
|
2013-08-16 03:06:24 -07:00
|
|
|
const nsAString& aTypeString,
|
2012-11-09 08:00:25 -08:00
|
|
|
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.
|
2013-11-27 01:12:41 -08:00
|
|
|
SetEventHandlerInternal(JS::NullPtr(), aEventName,
|
2013-09-17 04:01:28 -07:00
|
|
|
aTypeString, nsEventHandler(aHandler),
|
|
|
|
!mIsMainThreadELM ||
|
|
|
|
!nsContentUtils::IsCallerChrome());
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
void
|
2012-11-09 08:00:25 -08:00
|
|
|
nsEventListenerManager::SetEventHandler(OnErrorEventHandlerNonNull* aHandler)
|
|
|
|
{
|
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.
|
2013-11-27 01:12:41 -08:00
|
|
|
SetEventHandlerInternal(JS::NullPtr(), nsGkAtoms::onerror,
|
2013-11-05 06:16:26 -08:00
|
|
|
EmptyString(), nsEventHandler(aHandler),
|
|
|
|
!nsContentUtils::IsCallerChrome());
|
|
|
|
} else {
|
|
|
|
if (!aHandler) {
|
|
|
|
RemoveEventHandler(nullptr, NS_LITERAL_STRING("error"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Untrusted events are always permitted.
|
2013-11-27 01:12:41 -08:00
|
|
|
SetEventHandlerInternal(JS::NullPtr(), nullptr,
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_LITERAL_STRING("error"),
|
|
|
|
nsEventHandler(aHandler), true);
|
|
|
|
}
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:01:28 -07:00
|
|
|
void
|
2013-10-08 08:51:15 -07:00
|
|
|
nsEventListenerManager::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.
|
2013-11-27 01:12:41 -08:00
|
|
|
SetEventHandlerInternal(JS::NullPtr(), nsGkAtoms::onbeforeunload,
|
2013-09-17 04:01:28 -07:00
|
|
|
EmptyString(), nsEventHandler(aHandler),
|
|
|
|
!mIsMainThreadELM ||
|
|
|
|
!nsContentUtils::IsCallerChrome());
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsEventHandler*
|
2013-08-16 03:06:24 -07:00
|
|
|
nsEventListenerManager::GetEventHandlerInternal(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);
|
2013-08-16 03:06:24 -07:00
|
|
|
nsListenerStruct* ls = FindEventHandler(eventType, aEventName, aTypeString);
|
2011-08-24 12:49:25 -07:00
|
|
|
|
|
|
|
if (!ls) {
|
2012-11-09 08:00:25 -08:00
|
|
|
return nullptr;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIJSEventListener *listener = ls->GetJSListener();
|
|
|
|
|
|
|
|
if (ls->mHandlerIsString) {
|
2013-11-26 08:44:37 -08:00
|
|
|
CompileEventHandlerInternal(ls, nullptr, nullptr);
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:00:25 -08:00
|
|
|
const nsEventHandler& handler = listener->GetHandler();
|
|
|
|
if (handler.HasEventHandler()) {
|
2012-11-09 08:00:25 -08:00
|
|
|
return &handler;
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
2012-11-09 08:00:25 -08:00
|
|
|
|
|
|
|
return nullptr;
|
2011-08-24 12:49:25 -07:00
|
|
|
}
|
|
|
|
|
2012-02-01 13:58:01 -08:00
|
|
|
size_t
|
2013-06-23 05:03:39 -07:00
|
|
|
nsEventListenerManager::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
|
2012-02-01 13:58:01 -08:00
|
|
|
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) {
|
2012-02-01 13:58:01 -08:00
|
|
|
nsIJSEventListener* jsl = mListeners.ElementAt(i).GetJSListener();
|
2011-08-26 15:39:00 -07:00
|
|
|
if (jsl) {
|
2012-02-01 13:58:01 -08:00
|
|
|
n += jsl->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
|
2012-10-16 18:22:02 -07:00
|
|
|
nsEventListenerManager::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) {
|
2012-01-26 06:45:12 -08:00
|
|
|
const nsListenerStruct& ls = mListeners.ElementAt(i);
|
|
|
|
nsIJSEventListener* jsl = ls.GetJSListener();
|
|
|
|
if (jsl) {
|
2012-11-09 08:00:25 -08:00
|
|
|
if (jsl->GetHandler().HasEventHandler()) {
|
2013-09-08 20:28:48 -07:00
|
|
|
JS::ExposeObjectToActiveJS(jsl->GetHandler().Ptr()->Callable());
|
|
|
|
}
|
|
|
|
if (JSObject* scope = jsl->GetEventScope()) {
|
|
|
|
JS::ExposeObjectToActiveJS(scope);
|
2012-11-09 08:00:25 -08:00
|
|
|
}
|
2012-06-28 14:47:56 -07:00
|
|
|
} else if (ls.mListenerType == eWrappedJSListener) {
|
2013-04-16 14:15:57 -07:00
|
|
|
xpc_TryUnmarkWrappedGrayObject(ls.mListener.GetXPCOMCallback());
|
|
|
|
} else if (ls.mListenerType == eWebIDLListener) {
|
|
|
|
// Callback() unmarks gray
|
|
|
|
ls.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
|
|
|
|
|
|
|
already_AddRefed<nsIScriptGlobalObject>
|
|
|
|
nsEventListenerManager::GetScriptGlobalAndDocument(nsIDocument** aDoc)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|