2015-05-03 19:32:37 +00: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: */
|
2012-05-21 11:12:37 +00: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/. */
|
2005-07-28 17:18:28 +00:00
|
|
|
|
2014-03-17 06:56:54 +00:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2013-09-25 11:21:22 +00:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2014-03-18 04:48:21 +00:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2018-04-20 16:53:17 +00:00
|
|
|
#include "mozilla/dom/Event.h"
|
2013-04-06 00:44:15 +00:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2014-03-17 06:56:54 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
2013-04-06 00:44:15 +00:00
|
|
|
|
2014-03-17 06:56:54 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::AsyncEventDispatcher
|
|
|
|
******************************************************************************/
|
2011-05-09 19:33:03 +00:00
|
|
|
|
2014-05-14 09:48:37 +00:00
|
|
|
AsyncEventDispatcher::AsyncEventDispatcher(EventTarget* aTarget,
|
2014-03-17 06:56:54 +00:00
|
|
|
WidgetEvent& aEvent)
|
2017-06-12 19:34:10 +00:00
|
|
|
: CancelableRunnable("AsyncEventDispatcher")
|
|
|
|
, mTarget(aTarget)
|
|
|
|
, mEventMessage(eUnidentifiedEvent)
|
2011-05-09 19:33:03 +00:00
|
|
|
{
|
2014-05-14 09:48:37 +00:00
|
|
|
MOZ_ASSERT(mTarget);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Event> event =
|
2015-08-12 11:39:31 +00:00
|
|
|
EventDispatcher::CreateEvent(aTarget, nullptr, &aEvent, EmptyString());
|
2016-07-05 22:49:06 +00:00
|
|
|
mEvent = event.forget();
|
2017-06-17 03:38:30 +00:00
|
|
|
mEventType.SetIsVoid(true);
|
2011-05-09 19:33:03 +00:00
|
|
|
NS_ASSERTION(mEvent, "Should never fail to create an event");
|
2012-06-10 23:44:50 +00:00
|
|
|
mEvent->DuplicatePrivateData();
|
2016-03-17 07:01:30 +00:00
|
|
|
mEvent->SetTrusted(aEvent.IsTrusted());
|
2011-05-09 19:33:03 +00:00
|
|
|
}
|
2005-07-28 17:18:28 +00:00
|
|
|
|
2014-03-17 06:56:54 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncEventDispatcher::Run()
|
2005-07-28 17:18:28 +00:00
|
|
|
{
|
2016-03-12 11:53:50 +00:00
|
|
|
if (mCanceled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-06-17 03:38:30 +00:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(mTarget);
|
2017-05-19 23:07:59 +00:00
|
|
|
if (mCheckStillInDoc) {
|
|
|
|
MOZ_ASSERT(node);
|
|
|
|
if (!node->IsInComposedDoc()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 04:30:35 +00:00
|
|
|
mTarget->AsyncEventRunning(this);
|
2017-06-17 03:38:30 +00:00
|
|
|
if (mEventMessage != eUnidentifiedEvent) {
|
|
|
|
return nsContentUtils::DispatchTrustedEvent<WidgetEvent>
|
|
|
|
(node->OwnerDoc(), mTarget, mEventMessage, mBubbles,
|
|
|
|
false /* aCancelable */, nullptr /* aDefaultAction */,
|
|
|
|
mOnlyChromeDispatch);
|
|
|
|
}
|
2018-04-20 16:53:17 +00:00
|
|
|
RefPtr<Event> event = mEvent;
|
2015-05-25 22:04:38 +00:00
|
|
|
if (!event) {
|
2015-08-12 11:39:31 +00:00
|
|
|
event = NS_NewDOMEvent(mTarget, nullptr, nullptr);
|
2015-11-13 00:09:42 +00:00
|
|
|
event->InitEvent(mEventType, mBubbles, false);
|
2015-05-25 22:04:38 +00:00
|
|
|
event->SetTrusted(true);
|
2005-07-28 17:18:28 +00:00
|
|
|
}
|
2015-05-25 22:04:38 +00:00
|
|
|
if (mOnlyChromeDispatch) {
|
2015-08-12 11:39:31 +00:00
|
|
|
MOZ_ASSERT(event->IsTrusted());
|
2016-02-12 15:40:07 +00:00
|
|
|
event->WidgetEventPtr()->mFlags.mOnlyChromeDispatch = true;
|
2015-05-25 22:04:38 +00:00
|
|
|
}
|
2018-04-05 17:42:41 +00:00
|
|
|
mTarget->DispatchEvent(*event);
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
2005-07-28 17:18:28 +00:00
|
|
|
}
|
|
|
|
|
2016-04-11 18:40:06 +00:00
|
|
|
nsresult
|
2016-03-12 11:53:50 +00:00
|
|
|
AsyncEventDispatcher::Cancel()
|
|
|
|
{
|
|
|
|
mCanceled = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-17 06:56:54 +00:00
|
|
|
nsresult
|
|
|
|
AsyncEventDispatcher::PostDOMEvent()
|
2005-07-28 17:18:28 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<AsyncEventDispatcher> ensureDeletionWhenFailing = this;
|
2016-11-13 18:22:52 +00:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (nsCOMPtr<nsIGlobalObject> global = mTarget->GetOwnerGlobal()) {
|
2017-07-26 08:13:35 +00:00
|
|
|
return global->Dispatch(TaskCategory::Other, ensureDeletionWhenFailing.forget());
|
2016-11-13 18:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sometimes GetOwnerGlobal returns null because it uses
|
|
|
|
// GetScriptHandlingObject rather than GetScopeObject.
|
|
|
|
if (nsCOMPtr<nsINode> node = do_QueryInterface(mTarget)) {
|
|
|
|
nsCOMPtr<nsIDocument> doc = node->OwnerDoc();
|
2017-07-26 08:13:35 +00:00
|
|
|
return doc->Dispatch(TaskCategory::Other, ensureDeletionWhenFailing.forget());
|
2016-11-13 18:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_DispatchToCurrentThread(this);
|
2005-07-28 17:18:28 +00:00
|
|
|
}
|
2008-04-11 17:29:06 +00:00
|
|
|
|
2014-03-17 06:56:54 +00:00
|
|
|
void
|
|
|
|
AsyncEventDispatcher::RunDOMEventWhenSafe()
|
2008-04-11 17:29:06 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<AsyncEventDispatcher> ensureDeletionWhenFailing = this;
|
2011-05-09 19:33:03 +00:00
|
|
|
nsContentUtils::AddScriptRunner(this);
|
2008-04-11 17:29:06 +00:00
|
|
|
}
|
2010-02-25 02:45:43 +00:00
|
|
|
|
2017-05-19 23:07:59 +00:00
|
|
|
void
|
|
|
|
AsyncEventDispatcher::RequireNodeInDocument()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(mTarget);
|
|
|
|
MOZ_ASSERT(node);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mCheckStillInDoc = true;
|
|
|
|
}
|
|
|
|
|
2014-03-17 06:56:54 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::LoadBlockingAsyncEventDispatcher
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
LoadBlockingAsyncEventDispatcher::~LoadBlockingAsyncEventDispatcher()
|
2010-02-25 02:45:43 +00:00
|
|
|
{
|
|
|
|
if (mBlockedDoc) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mBlockedDoc->UnblockOnload(true);
|
2010-02-25 02:45:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-17 06:56:54 +00:00
|
|
|
|
|
|
|
} // namespace mozilla
|