2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-04-17 01:16:08 +04: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/. */
|
|
|
|
|
2014-03-17 10:56:53 +04:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2013-04-17 01:16:08 +04:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2016-04-26 11:23:17 +03:00
|
|
|
#include "mozilla/dom/EventTargetBinding.h"
|
2017-11-20 21:59:22 +03:00
|
|
|
#include "mozilla/dom/ConstructibleEventTarget.h"
|
|
|
|
#include "nsIGlobalObject.h"
|
2013-08-28 02:39:02 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-04-17 01:16:08 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-11-20 21:59:22 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<EventTarget>
|
|
|
|
EventTarget::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
RefPtr<EventTarget> target = new ConstructibleEventTarget(global);
|
|
|
|
return target.forget();
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:42:42 +03:00
|
|
|
bool
|
|
|
|
EventTarget::ComputeWantsUntrusted(const Nullable<bool>& aWantsUntrusted,
|
2018-07-30 04:51:00 +03:00
|
|
|
const AddEventListenerOptionsOrBoolean* aOptions,
|
2018-04-05 20:42:42 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2018-07-30 04:51:00 +03:00
|
|
|
if (aOptions && aOptions->IsAddEventListenerOptions()) {
|
|
|
|
const auto& options = aOptions->GetAsAddEventListenerOptions();
|
|
|
|
if (options.mWantUntrusted.WasPassed()) {
|
|
|
|
return options.mWantUntrusted.Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:42:42 +03:00
|
|
|
if (!aWantsUntrusted.IsNull()) {
|
|
|
|
return aWantsUntrusted.Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool defaultWantsUntrusted = ComputeDefaultWantsUntrusted(aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultWantsUntrusted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTarget::AddEventListener(const nsAString& aType,
|
|
|
|
EventListener* aCallback,
|
|
|
|
const AddEventListenerOptionsOrBoolean& aOptions,
|
|
|
|
const Nullable<bool>& aWantsUntrusted,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2018-07-30 04:51:00 +03:00
|
|
|
bool wantsUntrusted = ComputeWantsUntrusted(aWantsUntrusted, &aOptions, aRv);
|
2018-04-05 20:42:42 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerManager* elm = GetOrCreateListenerManager();
|
|
|
|
if (!elm) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
elm->AddEventListener(aType, aCallback, aOptions, wantsUntrusted);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
EventTarget::AddEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
const Nullable<bool>& aWantsUntrusted)
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
2018-07-30 04:51:00 +03:00
|
|
|
bool wantsUntrusted = ComputeWantsUntrusted(aWantsUntrusted, nullptr, rv);
|
2018-04-05 20:42:42 +03:00
|
|
|
if (rv.Failed()) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerManager* elm = GetOrCreateListenerManager();
|
|
|
|
NS_ENSURE_STATE(elm);
|
|
|
|
elm->AddEventListener(aType, aListener, aUseCapture, wantsUntrusted);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-17 01:16:08 +04:00
|
|
|
void
|
|
|
|
EventTarget::RemoveEventListener(const nsAString& aType,
|
2013-09-24 04:25:17 +04:00
|
|
|
EventListener* aListener,
|
2016-04-26 11:23:17 +03:00
|
|
|
const EventListenerOptionsOrBoolean& aOptions,
|
2013-04-17 01:16:08 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
2013-04-17 01:16:08 +04:00
|
|
|
if (elm) {
|
2016-04-26 11:23:17 +03:00
|
|
|
elm->RemoveEventListener(aType, aListener, aOptions);
|
2013-04-17 01:16:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:42:40 +03:00
|
|
|
void
|
|
|
|
EventTarget::RemoveEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture)
|
|
|
|
{
|
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
|
|
|
if (elm) {
|
|
|
|
elm->RemoveEventListener(aType, aListener, aUseCapture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:42:42 +03:00
|
|
|
nsresult
|
|
|
|
EventTarget::AddSystemEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener* aListener,
|
|
|
|
bool aUseCapture,
|
|
|
|
const Nullable<bool>& aWantsUntrusted)
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
2018-07-30 04:51:00 +03:00
|
|
|
bool wantsUntrusted = ComputeWantsUntrusted(aWantsUntrusted, nullptr, rv);
|
2018-04-05 20:42:42 +03:00
|
|
|
if (rv.Failed()) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
EventListenerManager* elm = GetOrCreateListenerManager();
|
|
|
|
NS_ENSURE_STATE(elm);
|
|
|
|
|
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mInSystemGroup = true;
|
|
|
|
flags.mCapture = aUseCapture;
|
|
|
|
flags.mAllowUntrustedEvents = wantsUntrusted;
|
|
|
|
elm->AddEventListenerByType(aListener, aType, flags);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:42:40 +03:00
|
|
|
void
|
|
|
|
EventTarget::RemoveSystemEventListener(const nsAString& aType,
|
|
|
|
nsIDOMEventListener *aListener,
|
|
|
|
bool aUseCapture)
|
|
|
|
{
|
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
|
|
|
if (elm) {
|
|
|
|
EventListenerFlags flags;
|
|
|
|
flags.mInSystemGroup = true;
|
|
|
|
flags.mCapture = aUseCapture;
|
|
|
|
elm->RemoveEventListenerByType(aListener, aType, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 21:07:16 +04:00
|
|
|
EventHandlerNonNull*
|
2018-07-25 01:15:19 +03:00
|
|
|
EventTarget::GetEventHandler(nsAtom* aType)
|
2013-05-09 21:07:16 +04:00
|
|
|
{
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
2018-07-25 01:15:19 +03:00
|
|
|
return elm ? elm->GetEventHandler(aType) : nullptr;
|
2013-05-09 21:07:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-16 14:06:24 +04:00
|
|
|
EventTarget::SetEventHandler(const nsAString& aType,
|
|
|
|
EventHandlerNonNull* aHandler,
|
2013-09-17 15:01:28 +04:00
|
|
|
ErrorResult& aRv)
|
2013-05-09 21:07:16 +04:00
|
|
|
{
|
2013-09-17 15:01:28 +04:00
|
|
|
if (!StringBeginsWith(aType, NS_LITERAL_STRING("on"))) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-25 01:15:19 +03:00
|
|
|
RefPtr<nsAtom> type = NS_Atomize(aType);
|
|
|
|
SetEventHandler(type, aHandler);
|
2013-08-16 14:06:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-25 01:15:19 +03:00
|
|
|
EventTarget::SetEventHandler(nsAtom* aType, EventHandlerNonNull* aHandler)
|
2013-08-16 14:06:24 +04:00
|
|
|
{
|
2018-07-25 01:15:19 +03:00
|
|
|
GetOrCreateListenerManager()->SetEventHandler(aType, aHandler);
|
2013-05-09 21:07:16 +04:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:22:16 +03:00
|
|
|
bool
|
2017-07-28 02:46:44 +03:00
|
|
|
EventTarget::HasNonSystemGroupListenersForUntrustedKeyEvents() const
|
2017-06-06 03:22:16 +03:00
|
|
|
{
|
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
2017-07-28 02:46:44 +03:00
|
|
|
return elm && elm->HasNonSystemGroupListenersForUntrustedKeyEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EventTarget::HasNonPassiveNonSystemGroupListenersForUntrustedKeyEvents() const
|
|
|
|
{
|
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
|
|
|
return elm && elm->HasNonPassiveNonSystemGroupListenersForUntrustedKeyEvents();
|
2017-06-06 03:22:16 +03:00
|
|
|
}
|
|
|
|
|
2015-09-17 13:16:20 +03:00
|
|
|
bool
|
2016-05-06 13:39:10 +03:00
|
|
|
EventTarget::IsApzAware() const
|
2015-09-17 13:16:20 +03:00
|
|
|
{
|
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
|
|
|
return elm && elm->HasApzAwareListeners();
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:42:41 +03:00
|
|
|
void
|
|
|
|
EventTarget::DispatchEvent(Event& aEvent)
|
|
|
|
{
|
|
|
|
// The caller type doesn't really matter if we don't care about the
|
|
|
|
// return value, but let's be safe and pass NonSystem.
|
|
|
|
Unused << DispatchEvent(aEvent, CallerType::NonSystem, IgnoreErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EventTarget::DispatchEvent(Event& aEvent, ErrorResult& aRv)
|
2016-04-07 15:49:12 +03:00
|
|
|
{
|
2018-04-05 20:42:41 +03:00
|
|
|
// The caller type doesn't really matter if we don't care about the
|
|
|
|
// return value, but let's be safe and pass NonSystem.
|
|
|
|
Unused << DispatchEvent(aEvent, CallerType::NonSystem, IgnoreErrors());
|
2016-04-07 15:49:12 +03:00
|
|
|
}
|
|
|
|
|
2013-04-17 01:16:08 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|