2009-10-16 01:57:32 -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/. */
|
2009-10-16 01:57:32 -07:00
|
|
|
#include "nsEventListenerService.h"
|
|
|
|
#include "nsCOMArray.h"
|
2010-06-09 18:16:58 -07:00
|
|
|
#include "nsEventListenerManager.h"
|
2009-10-16 01:57:32 -07:00
|
|
|
#include "nsMemory.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2009-10-16 01:57:32 -07:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
|
|
|
#include "nsIJSEventListener.h"
|
2009-10-17 07:40:44 -07:00
|
|
|
#ifdef MOZ_JSDEBUGGER
|
|
|
|
#include "jsdIDebuggerService.h"
|
|
|
|
#endif
|
2012-07-20 09:42:08 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2013-09-25 04:21:22 -07:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2013-08-27 15:10:28 -07:00
|
|
|
#include "mozilla/Maybe.h"
|
2013-08-29 14:18:25 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2009-10-16 01:57:32 -07:00
|
|
|
|
2013-10-01 20:46:04 -07:00
|
|
|
using namespace mozilla;
|
2012-12-15 17:26:05 -08:00
|
|
|
using namespace mozilla::dom;
|
2013-05-06 06:04:17 -07:00
|
|
|
using mozilla::AutoSafeJSContext;
|
2012-12-15 17:26:05 -08:00
|
|
|
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_1(nsEventListenerInfo, mListener)
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(EventListenerInfo, nsEventListenerInfo)
|
|
|
|
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsEventListenerInfo)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEventListenerInfo)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(EventListenerInfo)
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsEventListenerInfo)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsEventListenerInfo)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerInfo::GetType(nsAString& aType)
|
|
|
|
{
|
|
|
|
aType = mType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEventListenerInfo::GetCapturing(bool* aCapturing)
|
2009-10-16 01:57:32 -07:00
|
|
|
{
|
|
|
|
*aCapturing = mCapturing;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEventListenerInfo::GetAllowsUntrusted(bool* aAllowsUntrusted)
|
2009-10-16 01:57:32 -07:00
|
|
|
{
|
|
|
|
*aAllowsUntrusted = mAllowsUntrusted;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsEventListenerInfo::GetInSystemEventGroup(bool* aInSystemEventGroup)
|
2009-10-16 01:57:32 -07:00
|
|
|
{
|
|
|
|
*aInSystemEventGroup = mInSystemEventGroup;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-01 14:17:56 -07:00
|
|
|
NS_IMETHODIMP
|
2014-01-09 09:39:36 -08:00
|
|
|
nsEventListenerInfo::GetListenerObject(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aObject)
|
2013-04-01 14:17:56 -07:00
|
|
|
{
|
|
|
|
mozilla::Maybe<JSAutoCompartment> ac;
|
2014-01-23 11:49:40 -08:00
|
|
|
GetJSVal(aCx, ac, aObject);
|
2013-04-01 14:17:56 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(nsEventListenerService, nsIEventListenerService)
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-03-16 16:39:51 -07:00
|
|
|
nsEventListenerInfo::GetJSVal(JSContext* aCx,
|
|
|
|
mozilla::Maybe<JSAutoCompartment>& aAc,
|
2014-01-23 11:49:40 -08:00
|
|
|
JS::MutableHandle<JS::Value> aJSVal)
|
2009-10-16 01:57:32 -07:00
|
|
|
{
|
2014-01-23 11:49:40 -08:00
|
|
|
aJSVal.setNull();
|
2009-10-16 01:57:32 -07:00
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> wrappedJS = do_QueryInterface(mListener);
|
|
|
|
if (wrappedJS) {
|
2013-05-20 05:44:18 -07:00
|
|
|
JS::Rooted<JSObject*> object(aCx, wrappedJS->GetJSObject());
|
|
|
|
if (!object) {
|
2011-10-10 03:38:14 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-21 18:42:53 -07:00
|
|
|
aAc.construct(aCx, object);
|
2014-01-23 11:49:40 -08:00
|
|
|
aJSVal.setObject(*object);
|
2011-10-10 03:38:14 -07:00
|
|
|
return true;
|
2009-10-17 07:40:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSEventListener> jsl = do_QueryInterface(mListener);
|
2014-01-18 09:50:08 -08:00
|
|
|
if (jsl && jsl->GetHandler().HasEventHandler()) {
|
2013-05-13 10:43:53 -07:00
|
|
|
JS::Handle<JSObject*> handler(jsl->GetHandler().Ptr()->Callable());
|
2011-08-24 12:49:25 -07:00
|
|
|
if (handler) {
|
2012-08-21 18:42:53 -07:00
|
|
|
aAc.construct(aCx, handler);
|
2014-01-23 11:49:40 -08:00
|
|
|
aJSVal.setObject(*handler);
|
2011-10-10 03:38:14 -07:00
|
|
|
return true;
|
2009-10-17 07:40:44 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-10 03:38:14 -07:00
|
|
|
return false;
|
2009-10-17 07:40:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerInfo::ToSource(nsAString& aResult)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
aResult.SetIsVoid(true);
|
2009-10-17 07:40:44 -07:00
|
|
|
|
2013-05-06 06:04:17 -07:00
|
|
|
AutoSafeJSContext cx;
|
2013-05-22 09:05:28 -07:00
|
|
|
mozilla::Maybe<JSAutoCompartment> ac;
|
2014-01-23 11:49:40 -08:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
|
|
|
if (GetJSVal(cx, ac, &v)) {
|
2013-05-22 09:05:28 -07:00
|
|
|
JSString* str = JS_ValueToSource(cx, v);
|
|
|
|
if (str) {
|
|
|
|
nsDependentJSString depStr;
|
|
|
|
if (depStr.init(cx, str)) {
|
|
|
|
aResult.Assign(depStr);
|
2009-10-16 01:57:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-17 07:40:44 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerInfo::GetDebugObject(nsISupports** aRetVal)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aRetVal = nullptr;
|
2009-10-17 07:40:44 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_JSDEBUGGER
|
|
|
|
nsresult rv = NS_OK;
|
2010-11-26 06:11:14 -08:00
|
|
|
nsCOMPtr<jsdIDebuggerService> jsd =
|
|
|
|
do_GetService("@mozilla.org/js/jsd/debugger-service;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
2013-05-02 02:12:47 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isOn = false;
|
2010-11-26 06:11:14 -08:00
|
|
|
jsd->GetIsOn(&isOn);
|
|
|
|
NS_ENSURE_TRUE(isOn, NS_OK);
|
|
|
|
|
2013-05-06 06:04:17 -07:00
|
|
|
AutoSafeJSContext cx;
|
2013-05-22 09:05:28 -07:00
|
|
|
mozilla::Maybe<JSAutoCompartment> ac;
|
2014-01-23 11:49:40 -08:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
|
|
|
if (GetJSVal(cx, ac, &v)) {
|
2013-05-22 09:05:28 -07:00
|
|
|
nsCOMPtr<jsdIValue> jsdValue;
|
|
|
|
rv = jsd->WrapValue(v, getter_AddRefs(jsdValue));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
jsdValue.forget(aRetVal);
|
2009-10-17 07:40:44 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-16 01:57:32 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerService::GetListenerInfoFor(nsIDOMEventTarget* aEventTarget,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t* aCount,
|
2009-10-16 01:57:32 -07:00
|
|
|
nsIEventListenerInfo*** aOutArray)
|
|
|
|
{
|
2011-10-10 03:38:14 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aEventTarget);
|
2009-10-16 01:57:32 -07:00
|
|
|
*aCount = 0;
|
2012-07-30 07:20:58 -07:00
|
|
|
*aOutArray = nullptr;
|
2009-10-16 01:57:32 -07:00
|
|
|
nsCOMArray<nsIEventListenerInfo> listenerInfos;
|
2013-10-22 16:32:04 -07:00
|
|
|
|
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(aEventTarget);
|
|
|
|
NS_ENSURE_TRUE(eventTarget, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
|
|
|
nsEventListenerManager* elm = eventTarget->GetExistingListenerManager();
|
2011-06-23 19:18:01 -07:00
|
|
|
if (elm) {
|
|
|
|
elm->GetListenerInfo(&listenerInfos);
|
2009-10-16 01:57:32 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = listenerInfos.Count();
|
2009-10-16 01:57:32 -07:00
|
|
|
if (count == 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutArray =
|
|
|
|
static_cast<nsIEventListenerInfo**>(
|
|
|
|
nsMemory::Alloc(sizeof(nsIEventListenerInfo*) * count));
|
|
|
|
NS_ENSURE_TRUE(*aOutArray, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_ADDREF((*aOutArray)[i] = listenerInfos[i]);
|
|
|
|
}
|
|
|
|
*aCount = count;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerService::GetEventTargetChainFor(nsIDOMEventTarget* aEventTarget,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t* aCount,
|
2009-10-16 01:57:32 -07:00
|
|
|
nsIDOMEventTarget*** aOutArray)
|
|
|
|
{
|
|
|
|
*aCount = 0;
|
2012-07-30 07:20:58 -07:00
|
|
|
*aOutArray = nullptr;
|
2011-06-23 19:18:01 -07:00
|
|
|
NS_ENSURE_ARG(aEventTarget);
|
2013-10-01 20:46:04 -07:00
|
|
|
WidgetEvent event(true, NS_EVENT_NULL);
|
2013-04-05 17:44:26 -07:00
|
|
|
nsCOMArray<EventTarget> targets;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = nsEventDispatcher::Dispatch(aEventTarget, nullptr, &event,
|
|
|
|
nullptr, nullptr, nullptr, &targets);
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = targets.Count();
|
2009-10-16 01:57:32 -07:00
|
|
|
if (count == 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutArray =
|
|
|
|
static_cast<nsIDOMEventTarget**>(
|
2011-06-23 19:18:00 -07:00
|
|
|
nsMemory::Alloc(sizeof(nsIDOMEventTarget*) * count));
|
2009-10-16 01:57:32 -07:00
|
|
|
NS_ENSURE_TRUE(*aOutArray, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < count; ++i) {
|
2011-06-23 19:18:01 -07:00
|
|
|
NS_ADDREF((*aOutArray)[i] = targets[i]);
|
2009-10-16 01:57:32 -07:00
|
|
|
}
|
|
|
|
*aCount = count;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-23 19:18:02 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerService::HasListenersFor(nsIDOMEventTarget* aEventTarget,
|
|
|
|
const nsAString& aType,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aRetVal)
|
2011-06-23 19:18:02 -07:00
|
|
|
{
|
2013-10-22 16:32:04 -07:00
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(aEventTarget);
|
|
|
|
NS_ENSURE_TRUE(eventTarget, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
|
|
|
nsEventListenerManager* elm = eventTarget->GetExistingListenerManager();
|
2011-06-23 19:18:02 -07:00
|
|
|
*aRetVal = elm && elm->HasListenersFor(aType);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-09 18:16:58 -07:00
|
|
|
NS_IMETHODIMP
|
2011-06-23 19:18:02 -07:00
|
|
|
nsEventListenerService::AddSystemEventListener(nsIDOMEventTarget *aTarget,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aUseCapture)
|
2010-06-09 18:16:58 -07:00
|
|
|
{
|
2011-06-23 19:18:02 -07:00
|
|
|
NS_PRECONDITION(aTarget, "Missing target");
|
|
|
|
NS_PRECONDITION(aListener, "Missing listener");
|
|
|
|
|
2013-10-22 16:32:04 -07:00
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(aTarget);
|
|
|
|
NS_ENSURE_TRUE(eventTarget, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
|
|
|
nsEventListenerManager* manager = eventTarget->GetOrCreateListenerManager();
|
2011-06-23 19:18:02 -07:00
|
|
|
NS_ENSURE_STATE(manager);
|
|
|
|
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags =
|
|
|
|
aUseCapture ? TrustedEventsAtSystemGroupCapture() :
|
|
|
|
TrustedEventsAtSystemGroupBubble();
|
2011-08-18 02:45:00 -07:00
|
|
|
manager->AddEventListenerByType(aListener, aType, flags);
|
|
|
|
return NS_OK;
|
2011-06-23 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerService::RemoveSystemEventListener(nsIDOMEventTarget *aTarget,
|
|
|
|
const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aUseCapture)
|
2011-06-23 19:18:02 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aTarget, "Missing target");
|
|
|
|
NS_PRECONDITION(aListener, "Missing listener");
|
|
|
|
|
2013-10-22 16:32:04 -07:00
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(aTarget);
|
|
|
|
NS_ENSURE_TRUE(eventTarget, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
|
|
|
nsEventListenerManager* manager = eventTarget->GetExistingListenerManager();
|
2011-06-23 19:18:02 -07:00
|
|
|
if (manager) {
|
2012-12-15 17:26:05 -08:00
|
|
|
EventListenerFlags flags =
|
|
|
|
aUseCapture ? TrustedEventsAtSystemGroupCapture() :
|
|
|
|
TrustedEventsAtSystemGroupBubble();
|
2011-06-23 19:18:02 -07:00
|
|
|
manager->RemoveEventListenerByType(aListener, aType, flags);
|
2011-06-23 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
2010-06-09 18:16:58 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-01 05:42:29 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerService::AddListenerForAllEvents(nsIDOMEventTarget* aTarget,
|
|
|
|
nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aWantsUntrusted,
|
|
|
|
bool aSystemEventGroup)
|
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(aTarget && aListener);
|
2013-10-22 16:32:04 -07:00
|
|
|
|
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(aTarget);
|
|
|
|
NS_ENSURE_TRUE(eventTarget, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
|
|
|
nsEventListenerManager* manager = eventTarget->GetOrCreateListenerManager();
|
2012-11-01 05:42:29 -07:00
|
|
|
NS_ENSURE_STATE(manager);
|
|
|
|
manager->AddListenerForAllEvents(aListener, aUseCapture, aWantsUntrusted,
|
|
|
|
aSystemEventGroup);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEventListenerService::RemoveListenerForAllEvents(nsIDOMEventTarget* aTarget,
|
|
|
|
nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
bool aSystemEventGroup)
|
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(aTarget && aListener);
|
2013-10-22 16:32:04 -07:00
|
|
|
|
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(aTarget);
|
|
|
|
NS_ENSURE_TRUE(eventTarget, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
|
|
|
nsEventListenerManager* manager = eventTarget->GetExistingListenerManager();
|
2012-11-01 05:42:29 -07:00
|
|
|
if (manager) {
|
|
|
|
manager->RemoveListenerForAllEvents(aListener, aUseCapture, aSystemEventGroup);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-16 01:57:32 -07:00
|
|
|
nsresult
|
|
|
|
NS_NewEventListenerService(nsIEventListenerService** aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsEventListenerService();
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|