/work/obj-fuzz/dist/include/mozilla/AsyncEventDispatcher.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef mozilla_AsyncEventDispatcher_h_ |
8 | | #define mozilla_AsyncEventDispatcher_h_ |
9 | | |
10 | | #include "mozilla/Attributes.h" |
11 | | #include "mozilla/EventForwards.h" |
12 | | #include "mozilla/RefPtr.h" |
13 | | #include "mozilla/dom/Event.h" |
14 | | #include "nsCOMPtr.h" |
15 | | #include "nsIDocument.h" |
16 | | #include "nsString.h" |
17 | | #include "nsThreadUtils.h" |
18 | | |
19 | | class nsINode; |
20 | | |
21 | | namespace mozilla { |
22 | | |
23 | | /** |
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. |
28 | | */ |
29 | | |
30 | | class AsyncEventDispatcher : public CancelableRunnable |
31 | | { |
32 | | public: |
33 | | /** |
34 | | * If aOnlyChromeDispatch is true, the event is dispatched to only |
35 | | * chrome node. In that case, if aTarget is already a chrome node, |
36 | | * the event is dispatched to it, otherwise the dispatch path starts |
37 | | * at the first chrome ancestor of that target. |
38 | | */ |
39 | | AsyncEventDispatcher(nsINode* aTarget, |
40 | | const nsAString& aEventType, |
41 | | CanBubble aCanBubble, |
42 | | ChromeOnlyDispatch aOnlyChromeDispatch, |
43 | | Composed aComposed = Composed::eDefault) |
44 | | : CancelableRunnable("AsyncEventDispatcher") |
45 | | , mTarget(aTarget) |
46 | | , mEventType(aEventType) |
47 | | , mEventMessage(eUnidentifiedEvent) |
48 | | , mCanBubble(aCanBubble) |
49 | | , mOnlyChromeDispatch(aOnlyChromeDispatch) |
50 | | , mComposed(aComposed) |
51 | 0 | { |
52 | 0 | } |
53 | | |
54 | | /** |
55 | | * If aOnlyChromeDispatch is true, the event is dispatched to only |
56 | | * chrome node. In that case, if aTarget is already a chrome node, |
57 | | * the event is dispatched to it, otherwise the dispatch path starts |
58 | | * at the first chrome ancestor of that target. |
59 | | */ |
60 | | AsyncEventDispatcher(nsINode* aTarget, |
61 | | mozilla::EventMessage aEventMessage, |
62 | | CanBubble aCanBubble, |
63 | | ChromeOnlyDispatch aOnlyChromeDispatch) |
64 | | : CancelableRunnable("AsyncEventDispatcher") |
65 | | , mTarget(aTarget) |
66 | | , mEventMessage(aEventMessage) |
67 | | , mCanBubble(aCanBubble) |
68 | | , mOnlyChromeDispatch(aOnlyChromeDispatch) |
69 | 0 | { |
70 | 0 | mEventType.SetIsVoid(true); |
71 | 0 | MOZ_ASSERT(mEventMessage != eUnidentifiedEvent); |
72 | 0 | } |
73 | | |
74 | | AsyncEventDispatcher(dom::EventTarget* aTarget, |
75 | | const nsAString& aEventType, |
76 | | CanBubble aCanBubble) |
77 | | : CancelableRunnable("AsyncEventDispatcher") |
78 | | , mTarget(aTarget) |
79 | | , mEventType(aEventType) |
80 | | , mEventMessage(eUnidentifiedEvent) |
81 | | , mCanBubble(aCanBubble) |
82 | 0 | { |
83 | 0 | } |
84 | | |
85 | | AsyncEventDispatcher(dom::EventTarget* aTarget, |
86 | | mozilla::EventMessage aEventMessage, |
87 | | CanBubble aCanBubble) |
88 | | : CancelableRunnable("AsyncEventDispatcher") |
89 | | , mTarget(aTarget) |
90 | | , mEventMessage(aEventMessage) |
91 | | , mCanBubble(aCanBubble) |
92 | 0 | { |
93 | 0 | mEventType.SetIsVoid(true); |
94 | 0 | MOZ_ASSERT(mEventMessage != eUnidentifiedEvent); |
95 | 0 | } |
96 | | |
97 | | /** |
98 | | * aEvent must have been created without Widget*Event and Internal*Event |
99 | | * because this constructor assumes that it's safe to use aEvent |
100 | | * asynchronously (i.e., after all objects allocated in the stack are |
101 | | * destroyed). |
102 | | */ |
103 | | AsyncEventDispatcher(dom::EventTarget* aTarget, dom::Event* aEvent) |
104 | | : CancelableRunnable("AsyncEventDispatcher") |
105 | | , mTarget(aTarget) |
106 | | , mEvent(aEvent) |
107 | | , mEventMessage(eUnidentifiedEvent) |
108 | 0 | { |
109 | 0 | MOZ_ASSERT(aEvent->IsSafeToBeDispatchedAsynchronously(), |
110 | 0 | "The DOM event should be created without Widget*Event and Internal*Event " |
111 | 0 | "because if it needs to be safe to be dispatched asynchronously"); |
112 | 0 | } |
113 | | |
114 | | AsyncEventDispatcher(dom::EventTarget* aTarget, WidgetEvent& aEvent); |
115 | | |
116 | | NS_IMETHOD Run() override; |
117 | | nsresult Cancel() override; |
118 | | nsresult PostDOMEvent(); |
119 | | void RunDOMEventWhenSafe(); |
120 | | |
121 | | // Calling this causes the Run() method to check that |
122 | | // mTarget->IsInComposedDoc(). mTarget must be an nsINode or else we'll |
123 | | // assert. |
124 | | void RequireNodeInDocument(); |
125 | | |
126 | | nsCOMPtr<dom::EventTarget> mTarget; |
127 | | RefPtr<dom::Event> mEvent; |
128 | | // If mEventType is set, mEventMessage will be eUnidentifiedEvent. |
129 | | // If mEventMessage is set, mEventType will be void. |
130 | | // They can never both be set at the same time. |
131 | | nsString mEventType; |
132 | | EventMessage mEventMessage; |
133 | | CanBubble mCanBubble = CanBubble::eNo; |
134 | | ChromeOnlyDispatch mOnlyChromeDispatch = ChromeOnlyDispatch::eNo; |
135 | | Composed mComposed = Composed::eDefault; |
136 | | bool mCanceled = false; |
137 | | bool mCheckStillInDoc = false; |
138 | | }; |
139 | | |
140 | | class LoadBlockingAsyncEventDispatcher final : public AsyncEventDispatcher |
141 | | { |
142 | | public: |
143 | | LoadBlockingAsyncEventDispatcher(nsINode* aEventNode, |
144 | | const nsAString& aEventType, |
145 | | CanBubble aBubbles, |
146 | | ChromeOnlyDispatch aDispatchChromeOnly) |
147 | | : AsyncEventDispatcher(aEventNode, |
148 | | aEventType, |
149 | | aBubbles, |
150 | | aDispatchChromeOnly) |
151 | | , mBlockedDoc(aEventNode->OwnerDoc()) |
152 | 0 | { |
153 | 0 | if (mBlockedDoc) { |
154 | 0 | mBlockedDoc->BlockOnload(); |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | | LoadBlockingAsyncEventDispatcher(nsINode* aEventNode, dom::Event* aEvent) |
159 | | : AsyncEventDispatcher(aEventNode, aEvent) |
160 | | , mBlockedDoc(aEventNode->OwnerDoc()) |
161 | 0 | { |
162 | 0 | if (mBlockedDoc) { |
163 | 0 | mBlockedDoc->BlockOnload(); |
164 | 0 | } |
165 | 0 | } |
166 | | |
167 | | ~LoadBlockingAsyncEventDispatcher(); |
168 | | |
169 | | private: |
170 | | nsCOMPtr<nsIDocument> mBlockedDoc; |
171 | | }; |
172 | | |
173 | | } // namespace mozilla |
174 | | |
175 | | #endif // mozilla_AsyncEventDispatcher_h_ |