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
|
|
|
|
2014-03-16 23:56:54 -07:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2013-09-25 04:21:22 -07:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2014-03-17 21:48:21 -07:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2014-03-04 16:37:43 -08:00
|
|
|
#include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
|
2013-04-05 17:44:15 -07:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2014-03-16 23:56:54 -07:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
2013-04-05 17:44:15 -07:00
|
|
|
|
2014-03-16 23:56:54 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::AsyncEventDispatcher
|
|
|
|
******************************************************************************/
|
2011-05-09 12:33:03 -07:00
|
|
|
|
2014-05-14 02:48:37 -07:00
|
|
|
AsyncEventDispatcher::AsyncEventDispatcher(EventTarget* aTarget,
|
2014-03-16 23:56:54 -07:00
|
|
|
WidgetEvent& aEvent)
|
2014-05-14 02:48:37 -07:00
|
|
|
: mTarget(aTarget)
|
2014-03-16 23:56:54 -07:00
|
|
|
, mDispatchChromeOnly(false)
|
2011-05-09 12:33:03 -07:00
|
|
|
{
|
2014-05-14 02:48:37 -07:00
|
|
|
MOZ_ASSERT(mTarget);
|
|
|
|
EventDispatcher::CreateEvent(aTarget, nullptr, &aEvent, EmptyString(),
|
2014-03-17 21:48:21 -07:00
|
|
|
getter_AddRefs(mEvent));
|
2011-05-09 12:33:03 -07:00
|
|
|
NS_ASSERTION(mEvent, "Should never fail to create an event");
|
2012-06-10 16:44:50 -07:00
|
|
|
mEvent->DuplicatePrivateData();
|
2012-12-15 17:26:03 -08:00
|
|
|
mEvent->SetTrusted(aEvent.mFlags.mIsTrusted);
|
2011-05-09 12:33:03 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-16 23:56:54 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AsyncEventDispatcher::Run()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-03-06 10:12:20 -08:00
|
|
|
if (mEvent) {
|
2013-03-27 11:15:58 -07:00
|
|
|
if (mDispatchChromeOnly) {
|
|
|
|
MOZ_ASSERT(mEvent->InternalDOMEvent()->IsTrusted());
|
|
|
|
|
2014-05-14 02:48:37 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(mTarget);
|
|
|
|
MOZ_ASSERT(node, "ChromeOnly dispatch supported with Node targets only!");
|
|
|
|
nsPIDOMWindow* window = node->OwnerDoc()->GetWindow();
|
2013-03-27 11:15:58 -07:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-04-05 17:44:26 -07:00
|
|
|
nsCOMPtr<EventTarget> target = window->GetParentTarget();
|
2013-03-27 11:15:58 -07:00
|
|
|
if (!target) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2014-03-17 21:48:21 -07:00
|
|
|
EventDispatcher::DispatchDOMEvent(target, nullptr, mEvent,
|
|
|
|
nullptr, nullptr);
|
2013-03-27 11:15:58 -07:00
|
|
|
} else {
|
|
|
|
bool defaultActionEnabled; // This is not used because the caller is async
|
2014-05-14 02:48:37 -07:00
|
|
|
mTarget->DispatchEvent(mEvent, &defaultActionEnabled);
|
2013-03-27 11:15:58 -07:00
|
|
|
}
|
2009-03-06 10:12:20 -08:00
|
|
|
} else {
|
2011-10-18 04:19:44 -07:00
|
|
|
if (mDispatchChromeOnly) {
|
2014-05-14 02:48:37 -07:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(mTarget);
|
|
|
|
MOZ_ASSERT(node, "ChromeOnly dispatch supported with Node targets only!");
|
|
|
|
nsContentUtils::DispatchChromeEvent(node->OwnerDoc(), node, mEventType,
|
2011-10-18 04:19:44 -07:00
|
|
|
mBubbles, false);
|
|
|
|
} else {
|
2014-05-14 02:48:37 -07:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
NS_NewDOMEvent(getter_AddRefs(event), mTarget, nullptr, nullptr);
|
|
|
|
nsresult rv = event->InitEvent(mEventType, mBubbles, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
bool dummy;
|
|
|
|
mTarget->DispatchEvent(event, &dummy);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-12-11 00:18:04 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-16 23:56:54 -07:00
|
|
|
nsresult
|
|
|
|
AsyncEventDispatcher::PostDOMEvent()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 02:48:37 -07:00
|
|
|
nsRefPtr<AsyncEventDispatcher> ensureDeletionWhenFailing = this;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_DispatchToCurrentThread(this);
|
|
|
|
}
|
2008-04-11 10:29:06 -07:00
|
|
|
|
2014-03-16 23:56:54 -07:00
|
|
|
void
|
|
|
|
AsyncEventDispatcher::RunDOMEventWhenSafe()
|
2008-04-11 10:29:06 -07:00
|
|
|
{
|
2014-05-14 02:48:37 -07:00
|
|
|
nsRefPtr<AsyncEventDispatcher> ensureDeletionWhenFailing = this;
|
2011-05-09 12:33:03 -07:00
|
|
|
nsContentUtils::AddScriptRunner(this);
|
2008-04-11 10:29:06 -07:00
|
|
|
}
|
2010-02-24 18:45:43 -08:00
|
|
|
|
2014-03-16 23:56:54 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* mozilla::LoadBlockingAsyncEventDispatcher
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
LoadBlockingAsyncEventDispatcher::~LoadBlockingAsyncEventDispatcher()
|
2010-02-24 18:45:43 -08:00
|
|
|
{
|
|
|
|
if (mBlockedDoc) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mBlockedDoc->UnblockOnload(true);
|
2010-02-24 18:45:43 -08:00
|
|
|
}
|
|
|
|
}
|
2014-03-16 23:56:54 -07:00
|
|
|
|
|
|
|
} // namespace mozilla
|