2011-07-17 12:09:13 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-03-30 21:42:20 -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/. */
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#include "EventTarget.h"
|
2013-09-09 19:10:45 -07:00
|
|
|
#include "mozilla/dom/EventListenerBinding.h"
|
2013-09-19 22:07:03 -07:00
|
|
|
#include "mozilla/dom/EventHandlerBinding.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
2012-05-05 18:15:11 -07:00
|
|
|
using mozilla::ErrorResult;
|
2013-09-09 19:10:45 -07:00
|
|
|
using mozilla::dom::EventListener;
|
2013-09-09 19:47:28 -07:00
|
|
|
using mozilla::dom::Nullable;
|
2013-09-19 22:07:03 -07:00
|
|
|
using mozilla::dom::EventHandlerNonNull;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
void
|
2012-04-26 13:57:33 -07:00
|
|
|
EventTarget::_trace(JSTracer* aTrc)
|
2012-01-18 10:05:38 -08:00
|
|
|
{
|
2012-04-26 13:57:33 -07:00
|
|
|
mListenerManager._trace(aTrc);
|
|
|
|
DOMBindingBase::_trace(aTrc);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
void
|
2012-04-26 13:57:33 -07:00
|
|
|
EventTarget::_finalize(JSFreeOp* aFop)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-04-26 13:57:33 -07:00
|
|
|
mListenerManager._finalize(aFop);
|
|
|
|
DOMBindingBase::_finalize(aFop);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-09-19 22:07:03 -07:00
|
|
|
already_AddRefed<EventHandlerNonNull>
|
2012-05-05 18:15:11 -07:00
|
|
|
EventTarget::GetEventListener(const nsAString& aType, ErrorResult& aRv) const
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-03-30 21:42:20 -07:00
|
|
|
JSContext* cx = GetJSContext();
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-09-16 18:33:40 -07:00
|
|
|
JS::RootedString type(cx,
|
|
|
|
JS_NewUCStringCopyN(cx, aType.BeginReading(), aType.Length()));
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!type || !(type = JS_InternJSString(cx, type))) {
|
2012-05-05 18:15:11 -07:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2013-09-19 22:07:03 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-09-19 22:07:03 -07:00
|
|
|
JS::RootedObject listener(
|
|
|
|
cx, mListenerManager.GetEventListener(INTERNED_STRING_TO_JSID(cx, type)));
|
|
|
|
if (!listener) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<EventHandlerNonNull> handler = new EventHandlerNonNull(listener);
|
|
|
|
return handler.forget();
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
void
|
2013-05-17 18:48:25 -07:00
|
|
|
EventTarget::SetEventListener(const nsAString& aType,
|
2013-09-19 22:07:03 -07:00
|
|
|
EventHandlerNonNull* aListener,
|
2012-05-05 18:15:11 -07:00
|
|
|
ErrorResult& aRv)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-03-30 21:42:20 -07:00
|
|
|
JSContext* cx = GetJSContext();
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-09-16 18:33:40 -07:00
|
|
|
JS::RootedString type(cx,
|
|
|
|
JS_NewUCStringCopyN(cx, aType.BeginReading(), aType.Length()));
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!type || !(type = JS_InternJSString(cx, type))) {
|
2012-05-05 18:15:11 -07:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2012-03-30 21:42:20 -07:00
|
|
|
return;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-09-19 22:07:03 -07:00
|
|
|
JS::RootedObject listener(cx);
|
|
|
|
if (aListener) {
|
|
|
|
listener = aListener->Callable();
|
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
mListenerManager.SetEventListener(cx, INTERNED_STRING_TO_JSID(cx, type),
|
2013-09-19 22:07:03 -07:00
|
|
|
listener, aRv);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
void
|
2013-05-17 18:48:25 -07:00
|
|
|
EventTarget::AddEventListener(const nsAString& aType,
|
2013-09-09 19:10:45 -07:00
|
|
|
EventListener* aListener,
|
2012-03-30 21:42:20 -07:00
|
|
|
bool aCapturing, Nullable<bool> aWantsUntrusted,
|
2012-05-05 18:15:11 -07:00
|
|
|
ErrorResult& aRv)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-17 18:48:25 -07:00
|
|
|
if (!aListener) {
|
2012-03-30 21:42:20 -07:00
|
|
|
return;
|
2012-01-18 10:05:38 -08:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
JSContext* cx = GetJSContext();
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-09-16 18:33:40 -07:00
|
|
|
JS::RootedString type(cx,
|
|
|
|
JS_NewUCStringCopyN(cx, aType.BeginReading(), aType.Length()));
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!type || !(type = JS_InternJSString(cx, type))) {
|
2012-05-05 18:15:11 -07:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2012-03-30 21:42:20 -07:00
|
|
|
return;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
bool wantsUntrusted = !aWantsUntrusted.IsNull() && aWantsUntrusted.Value();
|
|
|
|
mListenerManager.AddEventListener(cx, INTERNED_STRING_TO_JSID(cx, type),
|
2013-09-09 19:10:45 -07:00
|
|
|
aListener->Callback(), aCapturing,
|
|
|
|
wantsUntrusted, aRv);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
void
|
2013-05-17 18:48:25 -07:00
|
|
|
EventTarget::RemoveEventListener(const nsAString& aType,
|
2013-09-09 19:10:45 -07:00
|
|
|
EventListener* aListener,
|
2012-05-05 18:15:11 -07:00
|
|
|
bool aCapturing, ErrorResult& aRv)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-17 18:48:25 -07:00
|
|
|
if (!aListener) {
|
2012-03-30 21:42:20 -07:00
|
|
|
return;
|
2011-12-01 13:30:28 -08:00
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
JSContext* cx = GetJSContext();
|
2012-01-18 10:05:38 -08:00
|
|
|
|
2013-09-16 18:33:40 -07:00
|
|
|
JS::RootedString type(cx,
|
|
|
|
JS_NewUCStringCopyN(cx, aType.BeginReading(), aType.Length()));
|
2012-03-30 21:42:20 -07:00
|
|
|
if (!type || !(type = JS_InternJSString(cx, type))) {
|
2012-05-05 18:15:11 -07:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2012-03-30 21:42:20 -07:00
|
|
|
return;
|
2011-10-06 16:09:43 -07:00
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
mListenerManager.RemoveEventListener(cx, INTERNED_STRING_TO_JSID(cx, type),
|
2013-09-09 19:10:45 -07:00
|
|
|
aListener->Callback(), aCapturing);
|
2012-03-19 07:34:55 -07:00
|
|
|
}
|