1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_AsyncEventDispatcher_h_
8 #define mozilla_AsyncEventDispatcher_h_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/EventForwards.h"
12 #include "mozilla/RefPtr.h"
13 #include "mozilla/dom/Event.h"
15 #include "mozilla/dom/Document.h"
17 #include "nsThreadUtils.h"
24 * Use AsyncEventDispatcher to fire a DOM event that requires safe a stable DOM.
25 * For example, you may need to fire an event from within layout, but
26 * want to ensure that the event handler doesn't mutate the DOM at
27 * the wrong time, in order to avoid resulting instability.
30 class AsyncEventDispatcher
: public CancelableRunnable
{
33 * If aOnlyChromeDispatch is true, the event is dispatched to only
34 * chrome node. In that case, if aTarget is already a chrome node,
35 * the event is dispatched to it, otherwise the dispatch path starts
36 * at the first chrome ancestor of that target.
38 AsyncEventDispatcher(nsINode
* aTarget
, const nsAString
& aEventType
,
40 ChromeOnlyDispatch aOnlyChromeDispatch
,
41 Composed aComposed
= Composed::eDefault
)
42 : CancelableRunnable("AsyncEventDispatcher"),
44 mEventType(aEventType
),
45 mEventMessage(eUnidentifiedEvent
),
46 mCanBubble(aCanBubble
),
47 mOnlyChromeDispatch(aOnlyChromeDispatch
),
48 mComposed(aComposed
) {}
51 * If aOnlyChromeDispatch is true, the event is dispatched to only
52 * chrome node. In that case, if aTarget is already a chrome node,
53 * the event is dispatched to it, otherwise the dispatch path starts
54 * at the first chrome ancestor of that target.
56 AsyncEventDispatcher(nsINode
* aTarget
, mozilla::EventMessage aEventMessage
,
58 ChromeOnlyDispatch aOnlyChromeDispatch
)
59 : CancelableRunnable("AsyncEventDispatcher"),
61 mEventMessage(aEventMessage
),
62 mCanBubble(aCanBubble
),
63 mOnlyChromeDispatch(aOnlyChromeDispatch
) {
64 mEventType
.SetIsVoid(true);
65 MOZ_ASSERT(mEventMessage
!= eUnidentifiedEvent
);
68 AsyncEventDispatcher(dom::EventTarget
* aTarget
, const nsAString
& aEventType
,
70 : CancelableRunnable("AsyncEventDispatcher"),
72 mEventType(aEventType
),
73 mEventMessage(eUnidentifiedEvent
),
74 mCanBubble(aCanBubble
) {}
76 AsyncEventDispatcher(dom::EventTarget
* aTarget
,
77 mozilla::EventMessage aEventMessage
,
79 : CancelableRunnable("AsyncEventDispatcher"),
81 mEventMessage(aEventMessage
),
82 mCanBubble(aCanBubble
) {
83 mEventType
.SetIsVoid(true);
84 MOZ_ASSERT(mEventMessage
!= eUnidentifiedEvent
);
88 * aEvent must have been created without Widget*Event and Internal*Event
89 * because this constructor assumes that it's safe to use aEvent
90 * asynchronously (i.e., after all objects allocated in the stack are
93 AsyncEventDispatcher(dom::EventTarget
* aTarget
, dom::Event
* aEvent
)
94 : CancelableRunnable("AsyncEventDispatcher"),
97 mEventMessage(eUnidentifiedEvent
) {
99 aEvent
->IsSafeToBeDispatchedAsynchronously(),
100 "The DOM event should be created without Widget*Event and "
102 "because if it needs to be safe to be dispatched asynchronously");
105 AsyncEventDispatcher(dom::EventTarget
* aTarget
, WidgetEvent
& aEvent
);
107 NS_IMETHOD
Run() override
;
108 nsresult
Cancel() override
;
109 nsresult
PostDOMEvent();
110 void RunDOMEventWhenSafe();
112 // Calling this causes the Run() method to check that
113 // mTarget->IsInComposedDoc(). mTarget must be an nsINode or else we'll
115 void RequireNodeInDocument();
117 nsCOMPtr
<dom::EventTarget
> mTarget
;
118 RefPtr
<dom::Event
> mEvent
;
119 // If mEventType is set, mEventMessage will be eUnidentifiedEvent.
120 // If mEventMessage is set, mEventType will be void.
121 // They can never both be set at the same time.
123 EventMessage mEventMessage
;
124 CanBubble mCanBubble
= CanBubble::eNo
;
125 ChromeOnlyDispatch mOnlyChromeDispatch
= ChromeOnlyDispatch::eNo
;
126 Composed mComposed
= Composed::eDefault
;
127 bool mCanceled
= false;
128 bool mCheckStillInDoc
= false;
131 class LoadBlockingAsyncEventDispatcher final
: public AsyncEventDispatcher
{
133 LoadBlockingAsyncEventDispatcher(nsINode
* aEventNode
,
134 const nsAString
& aEventType
,
136 ChromeOnlyDispatch aDispatchChromeOnly
)
137 : AsyncEventDispatcher(aEventNode
, aEventType
, aBubbles
,
138 aDispatchChromeOnly
),
139 mBlockedDoc(aEventNode
->OwnerDoc()) {
141 mBlockedDoc
->BlockOnload();
145 LoadBlockingAsyncEventDispatcher(nsINode
* aEventNode
, dom::Event
* aEvent
)
146 : AsyncEventDispatcher(aEventNode
, aEvent
),
147 mBlockedDoc(aEventNode
->OwnerDoc()) {
149 mBlockedDoc
->BlockOnload();
153 ~LoadBlockingAsyncEventDispatcher();
156 RefPtr
<dom::Document
> mBlockedDoc
;
159 } // namespace mozilla
161 #endif // mozilla_AsyncEventDispatcher_h_