2005-07-28 17:18:28 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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"
|
2014-03-05 00:37:43 +00:00
|
|
|
#include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
|
2013-04-06 00:44:15 +00:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2014-03-17 06:56:54 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIDOMEvent.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)
|
2014-05-14 09:48:37 +00:00
|
|
|
: mTarget(aTarget)
|
2014-03-17 06:56:54 +00:00
|
|
|
, mDispatchChromeOnly(false)
|
2011-05-09 19:33:03 +00:00
|
|
|
{
|
2014-05-14 09:48:37 +00:00
|
|
|
MOZ_ASSERT(mTarget);
|
|
|
|
EventDispatcher::CreateEvent(aTarget, nullptr, &aEvent, EmptyString(),
|
2014-03-18 04:48:21 +00:00
|
|
|
getter_AddRefs(mEvent));
|
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();
|
2012-12-16 01:26:03 +00:00
|
|
|
mEvent->SetTrusted(aEvent.mFlags.mIsTrusted);
|
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
|
|
|
{
|
2009-03-06 18:12:20 +00:00
|
|
|
if (mEvent) {
|
2013-03-27 18:15:58 +00:00
|
|
|
if (mDispatchChromeOnly) {
|
|
|
|
MOZ_ASSERT(mEvent->InternalDOMEvent()->IsTrusted());
|
|
|
|
|
2014-05-14 09:48:37 +00: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 18:15:58 +00:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-04-06 00:44:26 +00:00
|
|
|
nsCOMPtr<EventTarget> target = window->GetParentTarget();
|
2013-03-27 18:15:58 +00:00
|
|
|
if (!target) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2014-03-18 04:48:21 +00:00
|
|
|
EventDispatcher::DispatchDOMEvent(target, nullptr, mEvent,
|
|
|
|
nullptr, nullptr);
|
2013-03-27 18:15:58 +00:00
|
|
|
} else {
|
|
|
|
bool defaultActionEnabled; // This is not used because the caller is async
|
2014-05-14 09:48:37 +00:00
|
|
|
mTarget->DispatchEvent(mEvent, &defaultActionEnabled);
|
2013-03-27 18:15:58 +00:00
|
|
|
}
|
2009-03-06 18:12:20 +00:00
|
|
|
} else {
|
2011-10-18 11:19:44 +00:00
|
|
|
if (mDispatchChromeOnly) {
|
2014-05-14 09:48:37 +00: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 11:19:44 +00:00
|
|
|
mBubbles, false);
|
|
|
|
} else {
|
2014-05-14 09:48:37 +00: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);
|
2005-07-28 17:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-11 08:18:04 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
return NS_OK;
|
2005-07-28 17:18:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 06:56:54 +00:00
|
|
|
nsresult
|
|
|
|
AsyncEventDispatcher::PostDOMEvent()
|
2005-07-28 17:18:28 +00:00
|
|
|
{
|
2014-05-14 09:48:37 +00:00
|
|
|
nsRefPtr<AsyncEventDispatcher> ensureDeletionWhenFailing = this;
|
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
|
|
|
{
|
2014-05-14 09:48:37 +00:00
|
|
|
nsRefPtr<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
|
|
|
|
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
|