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: */
|
2014-03-17 10:56:54 +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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_AsyncEventDispatcher_h_
|
|
|
|
#define mozilla_AsyncEventDispatcher_h_
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
2018-06-25 19:23:50 +03:00
|
|
|
#include "mozilla/EventForwards.h"
|
2018-04-20 19:53:17 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/dom/Event.h"
|
2014-03-17 10:56:54 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2014-08-18 18:44:50 +04:00
|
|
|
class nsINode;
|
|
|
|
|
2014-03-17 10:56:54 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
2016-03-12 14:53:50 +03:00
|
|
|
* Use AsyncEventDispatcher to fire a DOM event that requires safe a stable DOM.
|
2014-03-17 10:56:54 +04:00
|
|
|
* For example, you may need to fire an event from within layout, but
|
|
|
|
* want to ensure that the event handler doesn't mutate the DOM at
|
|
|
|
* the wrong time, in order to avoid resulting instability.
|
|
|
|
*/
|
2016-03-12 14:53:50 +03:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
class AsyncEventDispatcher : public CancelableRunnable
|
2014-03-17 10:56:54 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-05-26 01:04:38 +03:00
|
|
|
/**
|
|
|
|
* If aOnlyChromeDispatch is true, the event is dispatched to only
|
|
|
|
* chrome node. In that case, if aTarget is already a chrome node,
|
|
|
|
* the event is dispatched to it, otherwise the dispatch path starts
|
|
|
|
* at the first chrome ancestor of that target.
|
|
|
|
*/
|
2017-06-12 22:34:10 +03:00
|
|
|
AsyncEventDispatcher(nsINode* aTarget,
|
|
|
|
const nsAString& aEventType,
|
2018-06-25 19:23:50 +03:00
|
|
|
CanBubble aCanBubble,
|
2018-06-22 22:47:29 +03:00
|
|
|
ChromeOnlyDispatch aOnlyChromeDispatch,
|
|
|
|
Composed aComposed = Composed::eDefault)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("AsyncEventDispatcher")
|
|
|
|
, mTarget(aTarget)
|
2014-03-17 10:56:54 +04:00
|
|
|
, mEventType(aEventType)
|
2017-06-17 06:38:30 +03:00
|
|
|
, mEventMessage(eUnidentifiedEvent)
|
2018-06-25 19:23:50 +03:00
|
|
|
, mCanBubble(aCanBubble)
|
2015-05-26 01:04:38 +03:00
|
|
|
, mOnlyChromeDispatch(aOnlyChromeDispatch)
|
2018-06-22 22:47:29 +03:00
|
|
|
, mComposed(aComposed)
|
2014-03-17 10:56:54 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-17 06:38:30 +03:00
|
|
|
/**
|
|
|
|
* If aOnlyChromeDispatch is true, the event is dispatched to only
|
|
|
|
* chrome node. In that case, if aTarget is already a chrome node,
|
|
|
|
* the event is dispatched to it, otherwise the dispatch path starts
|
|
|
|
* at the first chrome ancestor of that target.
|
|
|
|
*/
|
|
|
|
AsyncEventDispatcher(nsINode* aTarget,
|
|
|
|
mozilla::EventMessage aEventMessage,
|
2018-06-25 19:23:50 +03:00
|
|
|
CanBubble aCanBubble,
|
|
|
|
ChromeOnlyDispatch aOnlyChromeDispatch)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("AsyncEventDispatcher")
|
|
|
|
, mTarget(aTarget)
|
2017-06-17 06:38:30 +03:00
|
|
|
, mEventMessage(aEventMessage)
|
2018-06-25 19:23:50 +03:00
|
|
|
, mCanBubble(aCanBubble)
|
2017-06-17 06:38:30 +03:00
|
|
|
, mOnlyChromeDispatch(aOnlyChromeDispatch)
|
|
|
|
{
|
|
|
|
mEventType.SetIsVoid(true);
|
|
|
|
MOZ_ASSERT(mEventMessage != eUnidentifiedEvent);
|
|
|
|
}
|
|
|
|
|
2018-06-25 19:23:50 +03:00
|
|
|
AsyncEventDispatcher(dom::EventTarget* aTarget,
|
|
|
|
const nsAString& aEventType,
|
|
|
|
CanBubble aCanBubble)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("AsyncEventDispatcher")
|
|
|
|
, mTarget(aTarget)
|
2014-05-14 13:48:37 +04:00
|
|
|
, mEventType(aEventType)
|
2017-06-17 06:38:30 +03:00
|
|
|
, mEventMessage(eUnidentifiedEvent)
|
2018-06-25 19:23:50 +03:00
|
|
|
, mCanBubble(aCanBubble)
|
2017-06-17 06:38:30 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncEventDispatcher(dom::EventTarget* aTarget,
|
|
|
|
mozilla::EventMessage aEventMessage,
|
2018-06-25 19:23:50 +03:00
|
|
|
CanBubble aCanBubble)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("AsyncEventDispatcher")
|
|
|
|
, mTarget(aTarget)
|
2017-06-17 06:38:30 +03:00
|
|
|
, mEventMessage(aEventMessage)
|
2018-06-25 19:23:50 +03:00
|
|
|
, mCanBubble(aCanBubble)
|
2014-05-14 13:48:37 +04:00
|
|
|
{
|
2017-06-17 06:38:30 +03:00
|
|
|
mEventType.SetIsVoid(true);
|
|
|
|
MOZ_ASSERT(mEventMessage != eUnidentifiedEvent);
|
2014-05-14 13:48:37 +04:00
|
|
|
}
|
|
|
|
|
2018-07-24 08:51:32 +03:00
|
|
|
/**
|
|
|
|
* aEvent must have been created without Widget*Event and Internal*Event
|
|
|
|
* because this constructor assumes that it's safe to use aEvent
|
|
|
|
* asynchronously (i.e., after all objects allocated in the stack are
|
|
|
|
* destroyed).
|
|
|
|
*/
|
2018-04-20 19:53:17 +03:00
|
|
|
AsyncEventDispatcher(dom::EventTarget* aTarget, dom::Event* aEvent)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("AsyncEventDispatcher")
|
|
|
|
, mTarget(aTarget)
|
2014-03-17 10:56:54 +04:00
|
|
|
, mEvent(aEvent)
|
2017-06-17 06:38:30 +03:00
|
|
|
, mEventMessage(eUnidentifiedEvent)
|
2014-03-17 10:56:54 +04:00
|
|
|
{
|
2018-07-24 08:51:32 +03:00
|
|
|
MOZ_ASSERT(aEvent->IsSafeToBeDispatchedAsynchronously(),
|
|
|
|
"The DOM event should be created without Widget*Event and Internal*Event "
|
|
|
|
"because if it needs to be safe to be dispatched asynchronously");
|
2014-03-17 10:56:54 +04:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:48:37 +04:00
|
|
|
AsyncEventDispatcher(dom::EventTarget* aTarget, WidgetEvent& aEvent);
|
2014-03-17 10:56:54 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override;
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() override;
|
2014-03-17 10:56:54 +04:00
|
|
|
nsresult PostDOMEvent();
|
|
|
|
void RunDOMEventWhenSafe();
|
|
|
|
|
2017-05-20 02:07:59 +03:00
|
|
|
// Calling this causes the Run() method to check that
|
|
|
|
// mTarget->IsInComposedDoc(). mTarget must be an nsINode or else we'll
|
|
|
|
// assert.
|
|
|
|
void RequireNodeInDocument();
|
|
|
|
|
2014-05-14 13:48:37 +04:00
|
|
|
nsCOMPtr<dom::EventTarget> mTarget;
|
2018-04-20 19:53:17 +03:00
|
|
|
RefPtr<dom::Event> mEvent;
|
2017-06-17 06:38:30 +03:00
|
|
|
// If mEventType is set, mEventMessage will be eUnidentifiedEvent.
|
|
|
|
// If mEventMessage is set, mEventType will be void.
|
|
|
|
// They can never both be set at the same time.
|
2014-03-17 10:56:54 +04:00
|
|
|
nsString mEventType;
|
2018-06-25 19:23:50 +03:00
|
|
|
EventMessage mEventMessage;
|
|
|
|
CanBubble mCanBubble = CanBubble::eNo;
|
|
|
|
ChromeOnlyDispatch mOnlyChromeDispatch = ChromeOnlyDispatch::eNo;
|
2018-06-22 22:47:29 +03:00
|
|
|
Composed mComposed = Composed::eDefault;
|
2016-03-12 14:53:50 +03:00
|
|
|
bool mCanceled = false;
|
2017-05-20 02:07:59 +03:00
|
|
|
bool mCheckStillInDoc = false;
|
2014-03-17 10:56:54 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class LoadBlockingAsyncEventDispatcher final : public AsyncEventDispatcher
|
2014-03-17 10:56:54 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
LoadBlockingAsyncEventDispatcher(nsINode* aEventNode,
|
|
|
|
const nsAString& aEventType,
|
2018-06-25 19:23:50 +03:00
|
|
|
CanBubble aBubbles,
|
|
|
|
ChromeOnlyDispatch aDispatchChromeOnly)
|
|
|
|
: AsyncEventDispatcher(aEventNode,
|
|
|
|
aEventType,
|
|
|
|
aBubbles,
|
|
|
|
aDispatchChromeOnly)
|
2014-03-17 10:56:54 +04:00
|
|
|
, mBlockedDoc(aEventNode->OwnerDoc())
|
|
|
|
{
|
|
|
|
if (mBlockedDoc) {
|
|
|
|
mBlockedDoc->BlockOnload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 19:53:17 +03:00
|
|
|
LoadBlockingAsyncEventDispatcher(nsINode* aEventNode, dom::Event* aEvent)
|
2014-03-17 10:56:54 +04:00
|
|
|
: AsyncEventDispatcher(aEventNode, aEvent)
|
|
|
|
, mBlockedDoc(aEventNode->OwnerDoc())
|
|
|
|
{
|
|
|
|
if (mBlockedDoc) {
|
|
|
|
mBlockedDoc->BlockOnload();
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 14:53:50 +03:00
|
|
|
|
2014-03-17 10:56:54 +04:00
|
|
|
~LoadBlockingAsyncEventDispatcher();
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIDocument> mBlockedDoc;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_AsyncEventDispatcher_h_
|