Backed out changeset 0a133d5fd155 (bug 1864534) for causing screenshot related failur...
[gecko.git] / widget / BasicEvents.h
blobb1e375a46cf61ddcaec74aaca009a6fc7fb6e299
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_BasicEvents_h__
7 #define mozilla_BasicEvents_h__
9 #include <stdint.h>
10 #include <type_traits>
12 #include "mozilla/EventForwards.h"
13 #include "mozilla/TimeStamp.h"
14 #include "mozilla/dom/EventTarget.h"
15 #include "mozilla/layers/LayersTypes.h"
16 #include "nsCOMPtr.h"
17 #include "nsAtom.h"
18 #include "nsISupportsImpl.h"
19 #include "nsIWidget.h"
20 #include "nsString.h"
21 #include "Units.h"
23 #ifdef DEBUG
24 # include "nsXULAppAPI.h"
25 #endif // #ifdef DEBUG
27 class nsIPrincipal;
29 namespace IPC {
30 template <typename T>
31 struct ParamTraits;
32 } // namespace IPC
34 namespace mozilla {
36 class EventTargetChainItem;
38 enum class CrossProcessForwarding {
39 // eStop prevents the event to be sent to remote process.
40 eStop,
41 // eAllow keeps current state of the event whether it's sent to remote
42 // process. In other words, eAllow does NOT mean that making the event
43 // sent to remote process when IsCrossProcessForwardingStopped() returns
44 // true.
45 eAllow,
48 /******************************************************************************
49 * mozilla::BaseEventFlags
51 * BaseEventFlags must be a POD struct for safe to use memcpy (including
52 * in ParamTraits<BaseEventFlags>). So don't make virtual methods, constructor,
53 * destructor and operators.
54 * This is necessary for VC which is NOT C++0x compiler.
55 ******************************************************************************/
57 struct BaseEventFlags {
58 public:
59 // If mIsTrusted is true, the event is a trusted event. Otherwise, it's
60 // an untrusted event.
61 bool mIsTrusted : 1;
62 // If mInBubblingPhase is true, the event is in bubbling phase or target
63 // phase.
64 bool mInBubblingPhase : 1;
65 // If mInCapturePhase is true, the event is in capture phase or target phase.
66 bool mInCapturePhase : 1;
67 // If mInTargetPhase is true, the event is in target phase.
68 bool mInTargetPhase : 1;
69 // If mInSystemGroup is true, the event is being dispatched in system group.
70 bool mInSystemGroup : 1;
71 // If mCancelable is true, the event can be consumed. I.e., calling
72 // dom::Event::PreventDefault() can prevent the default action.
73 bool mCancelable : 1;
74 // If mBubbles is true, the event can bubble. Otherwise, cannot be handled
75 // in bubbling phase.
76 bool mBubbles : 1;
77 // If mPropagationStopped is true, dom::Event::StopPropagation() or
78 // dom::Event::StopImmediatePropagation() has been called.
79 bool mPropagationStopped : 1;
80 // If mImmediatePropagationStopped is true,
81 // dom::Event::StopImmediatePropagation() has been called.
82 // Note that mPropagationStopped must be true when this is true.
83 bool mImmediatePropagationStopped : 1;
84 // If mDefaultPrevented is true, the event has been consumed.
85 // E.g., dom::Event::PreventDefault() has been called or
86 // the default action has been performed.
87 bool mDefaultPrevented : 1;
88 // If mDefaultPreventedByContent is true, the event has been
89 // consumed by content.
90 // Note that mDefaultPrevented must be true when this is true.
91 bool mDefaultPreventedByContent : 1;
92 // If mDefaultPreventedByChrome is true, the event has been
93 // consumed by chrome.
94 // Note that mDefaultPrevented must be true when this is true.
95 bool mDefaultPreventedByChrome : 1;
96 // mMultipleActionsPrevented may be used when default handling don't want to
97 // be prevented, but only one of the event targets should handle the event.
98 // For example, when a <label> element is in another <label> element and
99 // the first <label> element is clicked, that one may set this true.
100 // Then, the second <label> element won't handle the event.
101 bool mMultipleActionsPrevented : 1;
102 // XXXedgar: This is a temporary hack for elements, like <a>, which have not
103 // yet adopted the activation behavior defined in the specification. This
104 // should be removed after resolving bug 1851970.
105 bool mMultipleActivationPrevented : 1;
106 // If mIsBeingDispatched is true, the DOM event created from the event is
107 // dispatching into the DOM tree and not completed.
108 bool mIsBeingDispatched : 1;
109 // If mDispatchedAtLeastOnce is true, the event has been dispatched
110 // as a DOM event and the dispatch has been completed in the process.
111 // So, this is false even if the event has already been dispatched
112 // in another process.
113 bool mDispatchedAtLeastOnce : 1;
114 // If mIsSynthesizedForTests is true, the event has been synthesized for
115 // automated tests or something hacky approach of an add-on.
116 bool mIsSynthesizedForTests : 1;
117 // If mExceptionWasRaised is true, one of the event handlers has raised an
118 // exception.
119 bool mExceptionWasRaised : 1;
120 // If mRetargetToNonNativeAnonymous is true and the target is in a non-native
121 // native anonymous subtree, the event target is set to mOriginalTarget.
122 bool mRetargetToNonNativeAnonymous : 1;
123 // If mNoContentDispatch is true, the event is never dispatched to the
124 // event handlers which are added to the contents, onfoo attributes and
125 // properties. Note that this flag is ignored when
126 // EventChainPreVisitor::mForceContentDispatch is set true. For exapmle,
127 // window and document object sets it true. Therefore, web applications
128 // can handle the event if they add event listeners to the window or the
129 // document.
130 // XXX This is an ancient and broken feature, don't use this for new bug
131 // as far as possible.
132 bool mNoContentDispatch : 1;
133 // If mOnlyChromeDispatch is true, the event is dispatched to only chrome.
134 bool mOnlyChromeDispatch : 1;
135 // Indicates if the key combination is reserved by chrome. This is set by
136 // MarkAsReservedByChrome().
137 bool mIsReservedByChrome : 1;
138 // If mOnlySystemGroupDispatchInContent is true, event listeners added to
139 // the default group for non-chrome EventTarget won't be called.
140 // Be aware, if this is true, EventDispatcher needs to check if each event
141 // listener is added to chrome node, so, don't set this to true for the
142 // events which are fired a lot of times like eMouseMove.
143 bool mOnlySystemGroupDispatchInContent : 1;
144 // If mOnlySystemGroupDispatch is true, the event will be dispatched only to
145 // event listeners added in the system group.
146 bool mOnlySystemGroupDispatch : 1;
147 // The event's action will be handled by APZ. The main thread should not
148 // perform its associated action.
149 bool mHandledByAPZ : 1;
150 // True if the event is currently being handled by an event listener that
151 // was registered as a passive listener.
152 bool mInPassiveListener : 1;
153 // If mComposed is true, the event fired by nodes in shadow DOM can cross the
154 // boundary of shadow DOM and light DOM.
155 bool mComposed : 1;
156 // Similar to mComposed. Set it to true to allow events cross the boundary
157 // between native non-anonymous content and native anonymouse content
158 bool mComposedInNativeAnonymousContent : 1;
159 // Set to true for events which are suppressed or delayed so that later a
160 // DelayedEvent of it is dispatched. This is used when parent side process
161 // the key event after content side, and may drop the event if the event
162 // was suppressed or delayed in contents side.
163 // It is also set to true for the events (in a DelayedInputEvent), which will
164 // be dispatched afterwards.
165 bool mIsSuppressedOrDelayed : 1;
166 // Certain mouse events can be marked as positionless to return 0 from
167 // coordinate related getters.
168 bool mIsPositionless : 1;
170 // Flags managing state of propagation between processes.
171 // Note the the following flags shouldn't be referred directly. Use utility
172 // methods instead.
174 // If mNoRemoteProcessDispatch is true, the event is not allowed to be sent
175 // to remote process.
176 bool mNoRemoteProcessDispatch : 1;
177 // If mWantReplyFromContentProcess is true, the event will be redispatched
178 // in the parent process after the content process has handled it. Useful
179 // for when the parent process need the know first how the event was used
180 // by content before handling it itself.
181 bool mWantReplyFromContentProcess : 1;
182 // If mPostedToRemoteProcess is true, the event has been posted to the
183 // remote process (but it's not handled yet if it's not a duplicated event
184 // instance).
185 bool mPostedToRemoteProcess : 1;
186 // If mCameFromAnotherProcess is true, the event came from another process.
187 bool mCameFromAnotherProcess : 1;
190 * Helper methods for methods of DOM Event.
192 inline void StopPropagation() { mPropagationStopped = true; }
193 inline void StopImmediatePropagation() {
194 StopPropagation();
195 mImmediatePropagationStopped = true;
197 inline void PreventDefault(bool aCalledByDefaultHandler = true) {
198 if (!mCancelable) {
199 return;
201 mDefaultPrevented = true;
202 // Note that even if preventDefault() has already been called by chrome,
203 // a call of preventDefault() by content needs to overwrite
204 // mDefaultPreventedByContent to true because in such case, defaultPrevented
205 // must be true when web apps check it after they call preventDefault().
206 if (aCalledByDefaultHandler) {
207 StopCrossProcessForwarding();
208 mDefaultPreventedByChrome = true;
209 } else {
210 mDefaultPreventedByContent = true;
213 // This should be used only before dispatching events into the DOM tree.
214 inline void PreventDefaultBeforeDispatch(
215 CrossProcessForwarding aCrossProcessForwarding) {
216 if (!mCancelable) {
217 return;
219 mDefaultPrevented = true;
220 if (aCrossProcessForwarding == CrossProcessForwarding::eStop) {
221 StopCrossProcessForwarding();
224 inline bool DefaultPrevented() const { return mDefaultPrevented; }
225 inline bool DefaultPreventedByContent() const {
226 MOZ_ASSERT(!mDefaultPreventedByContent || DefaultPrevented());
227 return mDefaultPreventedByContent;
229 inline bool IsTrusted() const { return mIsTrusted; }
230 inline bool PropagationStopped() const { return mPropagationStopped; }
232 // Helper methods to access flags managing state of propagation between
233 // processes.
236 * Prevent to be dispatched to remote process.
238 inline void StopCrossProcessForwarding() {
239 MOZ_ASSERT(!mPostedToRemoteProcess);
240 mNoRemoteProcessDispatch = true;
241 mWantReplyFromContentProcess = false;
244 * Return true if the event shouldn't be dispatched to remote process.
246 inline bool IsCrossProcessForwardingStopped() const {
247 return mNoRemoteProcessDispatch;
250 * Mark the event as waiting reply from remote process.
251 * If the caller needs to win other keyboard event handlers in chrome,
252 * the caller should call StopPropagation() too.
253 * Otherwise, if the caller just needs to know if the event is consumed by
254 * either content or chrome, it should just call this because the event
255 * may be reserved by chrome and it needs to be dispatched into the DOM
256 * tree in chrome for checking if it's reserved before being sent to any
257 * remote processes.
259 inline void MarkAsWaitingReplyFromRemoteProcess() {
260 MOZ_ASSERT(!mPostedToRemoteProcess);
261 mNoRemoteProcessDispatch = false;
262 mWantReplyFromContentProcess = true;
265 * Reset "waiting reply from remote process" state. This is useful when
266 * you dispatch a copy of an event coming from different process.
268 inline void ResetWaitingReplyFromRemoteProcessState() {
269 if (IsWaitingReplyFromRemoteProcess()) {
270 // FYI: mWantReplyFromContentProcess is also used for indicating
271 // "handled in remote process" state. Therefore, only when
272 // IsWaitingReplyFromRemoteProcess() returns true, this should
273 // reset the flag.
274 mWantReplyFromContentProcess = false;
278 * Return true if the event handler should wait reply event. I.e., if this
279 * returns true, any event handler should do nothing with the event.
281 inline bool IsWaitingReplyFromRemoteProcess() const {
282 return !mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
285 * Mark the event as already handled in the remote process. This should be
286 * called when initializing reply events.
288 inline void MarkAsHandledInRemoteProcess() {
289 mNoRemoteProcessDispatch = true;
290 mWantReplyFromContentProcess = true;
291 mPostedToRemoteProcess = false;
294 * Return true if the event has already been handled in the remote process.
296 inline bool IsHandledInRemoteProcess() const {
297 return mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
300 * Return true if the event should be sent back to its parent process.
302 inline bool WantReplyFromContentProcess() const {
303 MOZ_ASSERT(!XRE_IsParentProcess());
304 return IsWaitingReplyFromRemoteProcess();
307 * Mark the event has already posted to a remote process.
309 inline void MarkAsPostedToRemoteProcess() {
310 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
311 mPostedToRemoteProcess = true;
314 * Reset the cross process dispatching state. This should be used when a
315 * process receives the event because the state is in the sender.
317 inline void ResetCrossProcessDispatchingState() {
318 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
319 mPostedToRemoteProcess = false;
320 // Ignore propagation state in the remote process if it's marked as
321 // "waiting reply from remote process" because the process needs to
322 // stop propagation in the process until receiving a reply event.
323 // Note that the propagation stopped flag is important for the reply event
324 // handler in the main process because it's used for making whether it's
325 // ignored by the remote process or not.
326 if (!XRE_IsParentProcess() && IsWaitingReplyFromRemoteProcess()) {
327 mPropagationStopped = mImmediatePropagationStopped = false;
329 // mDispatchedAtLeastOnce indicates the state in current process.
330 mDispatchedAtLeastOnce = false;
333 * Return true if the event has been posted to a remote process.
334 * Note that MarkAsPostedToRemoteProcess() is called by
335 * ParamTraits<mozilla::WidgetEvent>. Therefore, it *might* be possible
336 * that posting the event failed even if this returns true. But that must
337 * really rare. If that'd be problem for you, you should unmark this in
338 * BrowserParent or somewhere.
340 inline bool HasBeenPostedToRemoteProcess() const {
341 return mPostedToRemoteProcess;
344 * Return true if the event came from another process.
346 inline bool CameFromAnotherProcess() const { return mCameFromAnotherProcess; }
348 * Mark the event as coming from another process.
350 inline void MarkAsComingFromAnotherProcess() {
351 mCameFromAnotherProcess = true;
354 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
355 * content because it shouldn't be cancelable.
357 inline void MarkAsReservedByChrome() {
358 MOZ_ASSERT(!mPostedToRemoteProcess);
359 mIsReservedByChrome = true;
360 // For reserved commands (such as Open New Tab), we don't need to wait for
361 // the content to answer, neither to give a chance for content to override
362 // its behavior.
363 StopCrossProcessForwarding();
364 // If the event is reserved by chrome, we shouldn't expose the event to
365 // web contents because such events shouldn't be cancelable. So, it's not
366 // good behavior to fire such events but to ignore the defaultPrevented
367 // attribute value in chrome.
368 mOnlySystemGroupDispatchInContent = true;
371 * Return true if the event is reserved by chrome.
373 inline bool IsReservedByChrome() const {
374 MOZ_ASSERT(!mIsReservedByChrome || (IsCrossProcessForwardingStopped() &&
375 mOnlySystemGroupDispatchInContent));
376 return mIsReservedByChrome;
379 inline void Clear() { SetRawFlags(0); }
380 // Get if either the instance's bit or the aOther's bit is true, the
381 // instance's bit becomes true. In other words, this works like:
382 // eventFlags |= aOther;
383 inline void Union(const BaseEventFlags& aOther) {
384 RawFlags rawFlags = GetRawFlags() | aOther.GetRawFlags();
385 SetRawFlags(rawFlags);
388 private:
389 typedef uint64_t RawFlags;
391 inline void SetRawFlags(RawFlags aRawFlags) {
392 static_assert(sizeof(BaseEventFlags) <= sizeof(RawFlags),
393 "mozilla::EventFlags must not be bigger than the RawFlags");
394 memcpy(this, &aRawFlags, sizeof(BaseEventFlags));
396 inline RawFlags GetRawFlags() const {
397 RawFlags result = 0;
398 memcpy(&result, this, sizeof(BaseEventFlags));
399 return result;
403 /******************************************************************************
404 * mozilla::EventFlags
405 ******************************************************************************/
407 struct EventFlags : public BaseEventFlags {
408 EventFlags() { Clear(); }
411 /******************************************************************************
412 * mozilla::WidgetEventTime
413 ******************************************************************************/
415 class WidgetEventTime {
416 public:
417 // Timestamp when the message was created.
418 TimeStamp mTimeStamp;
420 WidgetEventTime() : mTimeStamp(TimeStamp::Now()) {}
422 explicit WidgetEventTime(const WidgetEventTime* aTime)
423 : mTimeStamp(aTime ? aTime->mTimeStamp : TimeStamp::Now()) {
424 MOZ_ASSERT(aTime != this);
425 MOZ_ASSERT_IF(aTime, !aTime->mTimeStamp.IsNull());
428 explicit WidgetEventTime(TimeStamp aTimeStamp) : mTimeStamp(aTimeStamp) {}
430 void AssignEventTime(const WidgetEventTime& aOther) {
431 mTimeStamp = aOther.mTimeStamp;
435 /******************************************************************************
436 * mozilla::WidgetEvent
437 ******************************************************************************/
439 class WidgetEvent : public WidgetEventTime {
440 private:
441 void SetDefaultCancelableAndBubbles() {
442 switch (mClass) {
443 case eEditorInputEventClass:
444 mFlags.mCancelable = false;
445 mFlags.mBubbles = mFlags.mIsTrusted;
446 break;
447 case eMouseEventClass:
448 mFlags.mCancelable =
449 (mMessage != eMouseEnter && mMessage != eMouseLeave);
450 mFlags.mBubbles = (mMessage != eMouseEnter && mMessage != eMouseLeave);
451 break;
452 case ePointerEventClass:
453 mFlags.mCancelable =
454 (mMessage != ePointerEnter && mMessage != ePointerLeave &&
455 mMessage != ePointerCancel && mMessage != ePointerGotCapture &&
456 mMessage != ePointerLostCapture);
457 mFlags.mBubbles =
458 (mMessage != ePointerEnter && mMessage != ePointerLeave);
459 break;
460 case eDragEventClass:
461 mFlags.mCancelable = (mMessage != eDragExit && mMessage != eDragLeave &&
462 mMessage != eDragEnd);
463 mFlags.mBubbles = true;
464 break;
465 case eSMILTimeEventClass:
466 mFlags.mCancelable = false;
467 mFlags.mBubbles = false;
468 break;
469 case eTransitionEventClass:
470 case eAnimationEventClass:
471 mFlags.mCancelable = false;
472 mFlags.mBubbles = true;
473 break;
474 case eCompositionEventClass:
475 // XXX compositionstart is cancelable in draft of DOM3 Events.
476 // However, it doesn't make sense for us, we cannot cancel
477 // composition when we send compositionstart event.
478 mFlags.mCancelable = false;
479 mFlags.mBubbles = true;
480 break;
481 default:
482 if (mMessage == eResize || mMessage == eMozVisualResize ||
483 mMessage == eMozVisualScroll || mMessage == eEditorInput ||
484 mMessage == eFormSelect) {
485 mFlags.mCancelable = false;
486 } else {
487 mFlags.mCancelable = true;
489 mFlags.mBubbles = true;
490 break;
494 protected:
495 WidgetEvent(bool aIsTrusted, EventMessage aMessage,
496 EventClassID aEventClassID,
497 const WidgetEventTime* aTime = nullptr)
498 : WidgetEventTime(aTime),
499 mClass(aEventClassID),
500 mMessage(aMessage),
501 mRefPoint(0, 0),
502 mLastRefPoint(0, 0),
503 mFocusSequenceNumber(0),
504 mSpecifiedEventType(nullptr),
505 mPath(nullptr),
506 mLayersId(layers::LayersId{0}) {
507 MOZ_COUNT_CTOR(WidgetEvent);
508 mFlags.Clear();
509 mFlags.mIsTrusted = aIsTrusted;
510 SetDefaultCancelableAndBubbles();
511 SetDefaultComposed();
512 SetDefaultComposedInNativeAnonymousContent();
515 WidgetEvent() : mPath(nullptr) { MOZ_COUNT_CTOR(WidgetEvent); }
517 public:
518 WidgetEvent(bool aIsTrusted, EventMessage aMessage,
519 const WidgetEventTime* aTime = nullptr)
520 : WidgetEvent(aIsTrusted, aMessage, eBasicEventClass, aTime) {}
522 MOZ_COUNTED_DTOR_VIRTUAL(WidgetEvent)
524 WidgetEvent(const WidgetEvent& aOther) : WidgetEventTime(aOther) {
525 MOZ_COUNT_CTOR(WidgetEvent);
526 *this = aOther;
528 WidgetEvent& operator=(const WidgetEvent& aOther) = default;
530 WidgetEvent(WidgetEvent&& aOther)
531 : WidgetEventTime(std::move(aOther)),
532 mClass(aOther.mClass),
533 mMessage(aOther.mMessage),
534 mRefPoint(std::move(aOther.mRefPoint)),
535 mLastRefPoint(std::move(aOther.mLastRefPoint)),
536 mFocusSequenceNumber(aOther.mFocusSequenceNumber),
537 mFlags(std::move(aOther.mFlags)),
538 mSpecifiedEventType(std::move(aOther.mSpecifiedEventType)),
539 mSpecifiedEventTypeString(std::move(aOther.mSpecifiedEventTypeString)),
540 mTarget(std::move(aOther.mTarget)),
541 mCurrentTarget(std::move(aOther.mCurrentTarget)),
542 mOriginalTarget(std::move(aOther.mOriginalTarget)),
543 mRelatedTarget(std::move(aOther.mRelatedTarget)),
544 mOriginalRelatedTarget(std::move(aOther.mOriginalRelatedTarget)),
545 mPath(std::move(aOther.mPath)) {
546 MOZ_COUNT_CTOR(WidgetEvent);
548 WidgetEvent& operator=(WidgetEvent&& aOther) = default;
550 virtual WidgetEvent* Duplicate() const {
551 MOZ_ASSERT(mClass == eBasicEventClass,
552 "Duplicate() must be overridden by sub class");
553 WidgetEvent* result = new WidgetEvent(false, mMessage, this);
554 result->AssignEventData(*this, true);
555 result->mFlags = mFlags;
556 return result;
559 EventClassID mClass;
560 EventMessage mMessage;
561 // Relative to the widget of the event, or if there is no widget then it is
562 // in screen coordinates. Not modified by layout code.
563 // This is in visual coordinates, i.e. the correct RelativeTo value that
564 // expresses what this is relative to is `{viewportFrame, Visual}`, where
565 // `viewportFrame` is the viewport frame of the widget's root document.
566 LayoutDeviceIntPoint mRefPoint;
567 // The previous mRefPoint, if known, used to calculate mouse movement deltas.
568 LayoutDeviceIntPoint mLastRefPoint;
569 // The sequence number of the last potentially focus changing event handled
570 // by APZ. This is used to track when that event has been processed by
571 // content, and focus can be reconfirmed for async keyboard scrolling.
572 uint64_t mFocusSequenceNumber;
573 // See BaseEventFlags definition for the detail.
574 BaseEventFlags mFlags;
576 // If JS creates an event with unknown event type or known event type but
577 // for different event interface, the event type is stored to this.
578 // NOTE: This is always used if the instance is a WidgetCommandEvent instance
579 // or "input" event is dispatched with dom::Event class.
580 RefPtr<nsAtom> mSpecifiedEventType;
582 // nsAtom isn't available on non-main thread due to unsafe. Therefore,
583 // mSpecifiedEventTypeString is used instead of mSpecifiedEventType if
584 // the event is created in non-main thread.
585 nsString mSpecifiedEventTypeString;
587 // Event targets, needed by DOM Events
588 // Note that when you need event target for DOM event, you should use
589 // Get*DOMEventTarget() instead of accessing these members directly.
590 nsCOMPtr<dom::EventTarget> mTarget;
591 nsCOMPtr<dom::EventTarget> mCurrentTarget;
592 nsCOMPtr<dom::EventTarget> mOriginalTarget;
594 /// The possible related target
595 nsCOMPtr<dom::EventTarget> mRelatedTarget;
596 nsCOMPtr<dom::EventTarget> mOriginalRelatedTarget;
598 nsTArray<EventTargetChainItem>* mPath;
600 // The LayersId of the content process that this event should be
601 // dispatched to. This field is only used in the chrome process
602 // and doesn't get remoted to child processes.
603 layers::LayersId mLayersId;
605 dom::EventTarget* GetDOMEventTarget() const;
606 dom::EventTarget* GetCurrentDOMEventTarget() const;
607 dom::EventTarget* GetOriginalDOMEventTarget() const;
609 void AssignEventData(const WidgetEvent& aEvent, bool aCopyTargets) {
610 // mClass should be initialized with the constructor.
611 // mMessage should be initialized with the constructor.
612 mRefPoint = aEvent.mRefPoint;
613 // mLastRefPoint doesn't need to be copied.
614 mFocusSequenceNumber = aEvent.mFocusSequenceNumber;
615 // mLayersId intentionally not copied, since it's not used within content
616 AssignEventTime(aEvent);
617 // mFlags should be copied manually if it's necessary.
618 mSpecifiedEventType = aEvent.mSpecifiedEventType;
619 // mSpecifiedEventTypeString should be copied manually if it's necessary.
620 mTarget = aCopyTargets ? aEvent.mTarget : nullptr;
621 mCurrentTarget = aCopyTargets ? aEvent.mCurrentTarget : nullptr;
622 mOriginalTarget = aCopyTargets ? aEvent.mOriginalTarget : nullptr;
623 mRelatedTarget = aCopyTargets ? aEvent.mRelatedTarget : nullptr;
624 mOriginalRelatedTarget =
625 aCopyTargets ? aEvent.mOriginalRelatedTarget : nullptr;
629 * Helper methods for methods of DOM Event.
631 void StopPropagation() { mFlags.StopPropagation(); }
632 void StopImmediatePropagation() { mFlags.StopImmediatePropagation(); }
633 void PreventDefault(bool aCalledByDefaultHandler = true,
634 nsIPrincipal* aPrincipal = nullptr);
636 void PreventDefaultBeforeDispatch(
637 CrossProcessForwarding aCrossProcessForwarding) {
638 mFlags.PreventDefaultBeforeDispatch(aCrossProcessForwarding);
640 bool DefaultPrevented() const { return mFlags.DefaultPrevented(); }
641 bool DefaultPreventedByContent() const {
642 return mFlags.DefaultPreventedByContent();
644 bool IsTrusted() const { return mFlags.IsTrusted(); }
645 bool PropagationStopped() const { return mFlags.PropagationStopped(); }
648 * Prevent to be dispatched to remote process.
650 inline void StopCrossProcessForwarding() {
651 mFlags.StopCrossProcessForwarding();
654 * Return true if the event shouldn't be dispatched to remote process.
656 inline bool IsCrossProcessForwardingStopped() const {
657 return mFlags.IsCrossProcessForwardingStopped();
660 * Mark the event as waiting reply from remote process.
661 * Note that this also stops immediate propagation in current process.
663 inline void MarkAsWaitingReplyFromRemoteProcess() {
664 mFlags.MarkAsWaitingReplyFromRemoteProcess();
667 * Reset "waiting reply from remote process" state. This is useful when
668 * you dispatch a copy of an event coming from different process.
670 inline void ResetWaitingReplyFromRemoteProcessState() {
671 mFlags.ResetWaitingReplyFromRemoteProcessState();
674 * Return true if the event handler should wait reply event. I.e., if this
675 * returns true, any event handler should do nothing with the event.
677 inline bool IsWaitingReplyFromRemoteProcess() const {
678 return mFlags.IsWaitingReplyFromRemoteProcess();
681 * Mark the event as already handled in the remote process. This should be
682 * called when initializing reply events.
684 inline void MarkAsHandledInRemoteProcess() {
685 mFlags.MarkAsHandledInRemoteProcess();
688 * Return true if the event has already been handled in the remote process.
689 * I.e., if this returns true, the event is a reply event.
691 inline bool IsHandledInRemoteProcess() const {
692 return mFlags.IsHandledInRemoteProcess();
695 * Return true if the event should be sent back to its parent process.
696 * So, usual event handlers shouldn't call this.
698 inline bool WantReplyFromContentProcess() const {
699 return mFlags.WantReplyFromContentProcess();
702 * Mark the event has already posted to a remote process.
704 inline void MarkAsPostedToRemoteProcess() {
705 mFlags.MarkAsPostedToRemoteProcess();
708 * Reset the cross process dispatching state. This should be used when a
709 * process receives the event because the state is in the sender.
711 inline void ResetCrossProcessDispatchingState() {
712 mFlags.ResetCrossProcessDispatchingState();
715 * Return true if the event has been posted to a remote process.
717 inline bool HasBeenPostedToRemoteProcess() const {
718 return mFlags.HasBeenPostedToRemoteProcess();
721 * Return true if the event came from another process.
723 inline bool CameFromAnotherProcess() const {
724 return mFlags.CameFromAnotherProcess();
727 * Mark the event as coming from another process.
729 inline void MarkAsComingFromAnotherProcess() {
730 mFlags.MarkAsComingFromAnotherProcess();
733 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
734 * content because it shouldn't be cancelable.
736 inline void MarkAsReservedByChrome() { mFlags.MarkAsReservedByChrome(); }
738 * Return true if the event is reserved by chrome.
740 inline bool IsReservedByChrome() const { return mFlags.IsReservedByChrome(); }
743 * Utils for checking event types
747 * As*Event() returns the pointer of the instance only when the instance is
748 * the class or one of its derived class.
750 #define NS_ROOT_EVENT_CLASS(aPrefix, aName)
751 #define NS_EVENT_CLASS(aPrefix, aName) \
752 virtual aPrefix##aName* As##aName(); \
753 const aPrefix##aName* As##aName() const;
755 #include "mozilla/EventClassList.h"
757 #undef NS_EVENT_CLASS
758 #undef NS_ROOT_EVENT_CLASS
761 * Returns true if the event is a query content event.
763 bool IsQueryContentEvent() const;
765 * Returns true if the event is a selection event.
767 bool IsSelectionEvent() const;
769 * Returns true if the event is a content command event.
771 bool IsContentCommandEvent() const;
774 * Returns true if the event mMessage is one of mouse events.
776 bool HasMouseEventMessage() const;
778 * Returns true if the event mMessage is one of drag events.
780 bool HasDragEventMessage() const;
782 * Returns true if aMessage or mMessage is one of key events.
784 static bool IsKeyEventMessage(EventMessage aMessage);
785 bool HasKeyEventMessage() const { return IsKeyEventMessage(mMessage); }
787 * Returns true if the event mMessage is one of composition events or text
788 * event.
790 bool HasIMEEventMessage() const;
793 * Returns true if the event can be sent to remote process.
795 bool CanBeSentToRemoteProcess() const;
797 * Returns true if the original target is a remote process and the event
798 * will be posted to the remote process later.
800 bool WillBeSentToRemoteProcess() const;
802 * Returns true if the event is related to IME handling. It includes
803 * IME events, query content events and selection events.
804 * Be careful when you use this.
806 bool IsIMERelatedEvent() const;
809 * Whether the event should be handled by the frame of the mouse cursor
810 * position or not. When it should be handled there (e.g., the mouse events),
811 * this returns true.
813 bool IsUsingCoordinates() const;
815 * Whether the event should be handled by the focused DOM window in the
816 * same top level window's or not. E.g., key events, IME related events
817 * (including the query content events, they are used in IME transaction)
818 * should be handled by the (last) focused window rather than the dispatched
819 * window.
821 * NOTE: Even if this returns true, the event isn't going to be handled by the
822 * application level active DOM window which is on another top level window.
823 * So, when the event is fired on a deactive window, the event is going to be
824 * handled by the last focused DOM window in the last focused window.
826 bool IsTargetedAtFocusedWindow() const;
828 * Whether the event should be handled by the focused content or not. E.g.,
829 * key events, IME related events and other input events which are not handled
830 * by the frame of the mouse cursor position.
832 * NOTE: Even if this returns true, the event isn't going to be handled by the
833 * application level active DOM window which is on another top level window.
834 * So, when the event is fired on a deactive window, the event is going to be
835 * handled by the last focused DOM element of the last focused DOM window in
836 * the last focused window.
838 bool IsTargetedAtFocusedContent() const;
840 * Whether the event should cause a DOM event.
842 bool IsAllowedToDispatchDOMEvent() const;
844 * Whether the event should be dispatched in system group.
846 bool IsAllowedToDispatchInSystemGroup() const;
848 * Whether the event should be blocked for fingerprinting resistance.
850 bool IsBlockedForFingerprintingResistance() const;
852 * Whether the event handler can flush pending notifications or not.
854 bool AllowFlushingPendingNotifications() const;
856 * Initialize mComposed
858 void SetDefaultComposed() {
859 switch (mClass) {
860 case eClipboardEventClass:
861 mFlags.mComposed = true;
862 break;
863 case eCompositionEventClass:
864 mFlags.mComposed =
865 mMessage == eCompositionStart || mMessage == eCompositionUpdate ||
866 mMessage == eCompositionChange || mMessage == eCompositionEnd;
867 break;
868 case eDragEventClass:
869 // All drag & drop events are composed
870 mFlags.mComposed = mMessage == eDrag || mMessage == eDragEnd ||
871 mMessage == eDragEnter || mMessage == eDragExit ||
872 mMessage == eDragLeave || mMessage == eDragOver ||
873 mMessage == eDragStart || mMessage == eDrop;
874 break;
875 case eEditorInputEventClass:
876 mFlags.mComposed =
877 mMessage == eEditorInput || mMessage == eEditorBeforeInput;
878 break;
879 case eFocusEventClass:
880 mFlags.mComposed = mMessage == eBlur || mMessage == eFocus ||
881 mMessage == eFocusOut || mMessage == eFocusIn;
882 break;
883 case eKeyboardEventClass:
884 mFlags.mComposed =
885 mMessage == eKeyDown || mMessage == eKeyUp || mMessage == eKeyPress;
886 break;
887 case eMouseEventClass:
888 mFlags.mComposed =
889 mMessage == eMouseClick || mMessage == eMouseDoubleClick ||
890 mMessage == eMouseAuxClick || mMessage == eMouseDown ||
891 mMessage == eMouseUp || mMessage == eMouseOver ||
892 mMessage == eMouseOut || mMessage == eMouseMove ||
893 mMessage == eContextMenu || mMessage == eXULPopupShowing ||
894 mMessage == eXULPopupHiding || mMessage == eXULPopupShown ||
895 mMessage == eXULPopupHidden;
896 break;
897 case ePointerEventClass:
898 // All pointer events are composed
899 mFlags.mComposed =
900 mMessage == ePointerDown || mMessage == ePointerMove ||
901 mMessage == ePointerUp || mMessage == ePointerCancel ||
902 mMessage == ePointerOver || mMessage == ePointerOut ||
903 mMessage == ePointerGotCapture || mMessage == ePointerLostCapture;
904 break;
905 case eTouchEventClass:
906 // All touch events are composed
907 mFlags.mComposed = mMessage == eTouchStart || mMessage == eTouchEnd ||
908 mMessage == eTouchMove || mMessage == eTouchCancel;
909 break;
910 case eUIEventClass:
911 mFlags.mComposed = mMessage == eLegacyDOMFocusIn ||
912 mMessage == eLegacyDOMFocusOut ||
913 mMessage == eLegacyDOMActivate;
914 break;
915 case eWheelEventClass:
916 // All wheel events are composed
917 mFlags.mComposed = mMessage == eWheel;
918 break;
919 case eMouseScrollEventClass:
920 // Legacy mouse scroll events are composed too, for consistency with
921 // wheel.
922 mFlags.mComposed = mMessage == eLegacyMouseLineOrPageScroll ||
923 mMessage == eLegacyMousePixelScroll;
924 break;
925 default:
926 mFlags.mComposed = false;
927 break;
931 void SetComposed(const nsAString& aEventTypeArg) {
932 mFlags.mComposed = // composition events
933 aEventTypeArg.EqualsLiteral("compositionstart") ||
934 aEventTypeArg.EqualsLiteral("compositionupdate") ||
935 aEventTypeArg.EqualsLiteral("compositionend") ||
936 aEventTypeArg.EqualsLiteral("text") ||
937 // drag and drop events
938 aEventTypeArg.EqualsLiteral("dragstart") ||
939 aEventTypeArg.EqualsLiteral("drag") ||
940 aEventTypeArg.EqualsLiteral("dragenter") ||
941 aEventTypeArg.EqualsLiteral("dragexit") ||
942 aEventTypeArg.EqualsLiteral("dragleave") ||
943 aEventTypeArg.EqualsLiteral("dragover") ||
944 aEventTypeArg.EqualsLiteral("drop") ||
945 aEventTypeArg.EqualsLiteral("dropend") ||
946 // editor input events
947 aEventTypeArg.EqualsLiteral("input") ||
948 aEventTypeArg.EqualsLiteral("beforeinput") ||
949 // focus events
950 aEventTypeArg.EqualsLiteral("blur") ||
951 aEventTypeArg.EqualsLiteral("focus") ||
952 aEventTypeArg.EqualsLiteral("focusin") ||
953 aEventTypeArg.EqualsLiteral("focusout") ||
954 // keyboard events
955 aEventTypeArg.EqualsLiteral("keydown") ||
956 aEventTypeArg.EqualsLiteral("keyup") ||
957 aEventTypeArg.EqualsLiteral("keypress") ||
958 // mouse events
959 aEventTypeArg.EqualsLiteral("click") ||
960 aEventTypeArg.EqualsLiteral("dblclick") ||
961 aEventTypeArg.EqualsLiteral("mousedown") ||
962 aEventTypeArg.EqualsLiteral("mouseup") ||
963 aEventTypeArg.EqualsLiteral("mouseenter") ||
964 aEventTypeArg.EqualsLiteral("mouseleave") ||
965 aEventTypeArg.EqualsLiteral("mouseover") ||
966 aEventTypeArg.EqualsLiteral("mouseout") ||
967 aEventTypeArg.EqualsLiteral("mousemove") ||
968 aEventTypeArg.EqualsLiteral("contextmenu") ||
969 // pointer events
970 aEventTypeArg.EqualsLiteral("pointerdown") ||
971 aEventTypeArg.EqualsLiteral("pointermove") ||
972 aEventTypeArg.EqualsLiteral("pointerup") ||
973 aEventTypeArg.EqualsLiteral("pointercancel") ||
974 aEventTypeArg.EqualsLiteral("pointerover") ||
975 aEventTypeArg.EqualsLiteral("pointerout") ||
976 aEventTypeArg.EqualsLiteral("pointerenter") ||
977 aEventTypeArg.EqualsLiteral("pointerleave") ||
978 aEventTypeArg.EqualsLiteral("gotpointercapture") ||
979 aEventTypeArg.EqualsLiteral("lostpointercapture") ||
980 // touch events
981 aEventTypeArg.EqualsLiteral("touchstart") ||
982 aEventTypeArg.EqualsLiteral("touchend") ||
983 aEventTypeArg.EqualsLiteral("touchmove") ||
984 aEventTypeArg.EqualsLiteral("touchcancel") ||
985 // UI legacy events
986 aEventTypeArg.EqualsLiteral("DOMFocusIn") ||
987 aEventTypeArg.EqualsLiteral("DOMFocusOut") ||
988 aEventTypeArg.EqualsLiteral("DOMActivate") ||
989 // wheel events
990 aEventTypeArg.EqualsLiteral("wheel");
993 void SetComposed(bool aComposed) { mFlags.mComposed = aComposed; }
995 void SetDefaultComposedInNativeAnonymousContent() {
996 // For compatibility concerns, we set mComposedInNativeAnonymousContent to
997 // false for those events we want to stop propagation.
999 // nsVideoFrame may create anonymous image element which fires eLoad,
1000 // eLoadStart, eLoadEnd, eLoadError. We don't want these events cross
1001 // the boundary of NAC
1002 mFlags.mComposedInNativeAnonymousContent =
1003 mMessage != eLoad && mMessage != eLoadStart && mMessage != eLoadEnd &&
1004 mMessage != eLoadError;
1007 bool IsUserAction() const;
1010 /******************************************************************************
1011 * mozilla::WidgetGUIEvent
1012 ******************************************************************************/
1014 class WidgetGUIEvent : public WidgetEvent {
1015 protected:
1016 WidgetGUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1017 EventClassID aEventClassID,
1018 const WidgetEventTime* aTime = nullptr)
1019 : WidgetEvent(aIsTrusted, aMessage, aEventClassID, aTime),
1020 mWidget(aWidget) {}
1022 WidgetGUIEvent() = default;
1024 public:
1025 virtual WidgetGUIEvent* AsGUIEvent() override { return this; }
1027 WidgetGUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1028 const WidgetEventTime* aTime = nullptr)
1029 : WidgetEvent(aIsTrusted, aMessage, eGUIEventClass, aTime),
1030 mWidget(aWidget) {}
1032 virtual WidgetEvent* Duplicate() const override {
1033 MOZ_ASSERT(mClass == eGUIEventClass,
1034 "Duplicate() must be overridden by sub class");
1035 // Not copying widget, it is a weak reference.
1036 WidgetGUIEvent* result = new WidgetGUIEvent(false, mMessage, nullptr, this);
1037 result->AssignGUIEventData(*this, true);
1038 result->mFlags = mFlags;
1039 return result;
1042 // Originator of the event
1043 nsCOMPtr<nsIWidget> mWidget;
1045 void AssignGUIEventData(const WidgetGUIEvent& aEvent, bool aCopyTargets) {
1046 AssignEventData(aEvent, aCopyTargets);
1047 // widget should be initialized with the constructor.
1051 /******************************************************************************
1052 * mozilla::Modifier
1054 * All modifier keys should be defined here. This is used for managing
1055 * modifier states for DOM Level 3 or later.
1056 ******************************************************************************/
1058 enum Modifier {
1059 MODIFIER_NONE = 0x0000,
1060 MODIFIER_ALT = 0x0001,
1061 MODIFIER_ALTGRAPH = 0x0002,
1062 MODIFIER_CAPSLOCK = 0x0004,
1063 MODIFIER_CONTROL = 0x0008,
1064 MODIFIER_FN = 0x0010,
1065 MODIFIER_FNLOCK = 0x0020,
1066 MODIFIER_META = 0x0040,
1067 MODIFIER_NUMLOCK = 0x0080,
1068 MODIFIER_SCROLLLOCK = 0x0100,
1069 MODIFIER_SHIFT = 0x0200,
1070 MODIFIER_SYMBOL = 0x0400,
1071 MODIFIER_SYMBOLLOCK = 0x0800,
1074 /******************************************************************************
1075 * Modifier key names.
1076 ******************************************************************************/
1078 #define NS_DOM_KEYNAME_ALT "Alt"
1079 #define NS_DOM_KEYNAME_ALTGRAPH "AltGraph"
1080 #define NS_DOM_KEYNAME_CAPSLOCK "CapsLock"
1081 #define NS_DOM_KEYNAME_CONTROL "Control"
1082 #define NS_DOM_KEYNAME_FN "Fn"
1083 #define NS_DOM_KEYNAME_FNLOCK "FnLock"
1084 #define NS_DOM_KEYNAME_META "Meta"
1085 #define NS_DOM_KEYNAME_NUMLOCK "NumLock"
1086 #define NS_DOM_KEYNAME_SCROLLLOCK "ScrollLock"
1087 #define NS_DOM_KEYNAME_SHIFT "Shift"
1088 #define NS_DOM_KEYNAME_SYMBOL "Symbol"
1089 #define NS_DOM_KEYNAME_SYMBOLLOCK "SymbolLock"
1090 #define NS_DOM_KEYNAME_OS "OS"
1092 /******************************************************************************
1093 * mozilla::Modifiers
1094 ******************************************************************************/
1096 typedef uint16_t Modifiers;
1098 class MOZ_STACK_CLASS GetModifiersName final : public nsAutoCString {
1099 public:
1100 explicit GetModifiersName(Modifiers aModifiers) {
1101 if (aModifiers & MODIFIER_ALT) {
1102 AssignLiteral(NS_DOM_KEYNAME_ALT);
1104 if (aModifiers & MODIFIER_ALTGRAPH) {
1105 MaybeAppendSeparator();
1106 AppendLiteral(NS_DOM_KEYNAME_ALTGRAPH);
1108 if (aModifiers & MODIFIER_CAPSLOCK) {
1109 MaybeAppendSeparator();
1110 AppendLiteral(NS_DOM_KEYNAME_CAPSLOCK);
1112 if (aModifiers & MODIFIER_CONTROL) {
1113 MaybeAppendSeparator();
1114 AppendLiteral(NS_DOM_KEYNAME_CONTROL);
1116 if (aModifiers & MODIFIER_FN) {
1117 MaybeAppendSeparator();
1118 AppendLiteral(NS_DOM_KEYNAME_FN);
1120 if (aModifiers & MODIFIER_FNLOCK) {
1121 MaybeAppendSeparator();
1122 AppendLiteral(NS_DOM_KEYNAME_FNLOCK);
1124 if (aModifiers & MODIFIER_META) {
1125 MaybeAppendSeparator();
1126 AppendLiteral(NS_DOM_KEYNAME_META);
1128 if (aModifiers & MODIFIER_NUMLOCK) {
1129 MaybeAppendSeparator();
1130 AppendLiteral(NS_DOM_KEYNAME_NUMLOCK);
1132 if (aModifiers & MODIFIER_SCROLLLOCK) {
1133 MaybeAppendSeparator();
1134 AppendLiteral(NS_DOM_KEYNAME_SCROLLLOCK);
1136 if (aModifiers & MODIFIER_SHIFT) {
1137 MaybeAppendSeparator();
1138 AppendLiteral(NS_DOM_KEYNAME_SHIFT);
1140 if (aModifiers & MODIFIER_SYMBOL) {
1141 MaybeAppendSeparator();
1142 AppendLiteral(NS_DOM_KEYNAME_SYMBOL);
1144 if (aModifiers & MODIFIER_SYMBOLLOCK) {
1145 MaybeAppendSeparator();
1146 AppendLiteral(NS_DOM_KEYNAME_SYMBOLLOCK);
1148 if (IsEmpty()) {
1149 AssignLiteral("none");
1153 private:
1154 void MaybeAppendSeparator() {
1155 if (!IsEmpty()) {
1156 AppendLiteral(" | ");
1161 /******************************************************************************
1162 * mozilla::WidgetInputEvent
1163 ******************************************************************************/
1165 class WidgetInputEvent : public WidgetGUIEvent {
1166 protected:
1167 WidgetInputEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1168 EventClassID aEventClassID,
1169 const WidgetEventTime* aTime = nullptr)
1170 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, aEventClassID, aTime),
1171 mModifiers(0) {}
1173 WidgetInputEvent() : mModifiers(0) {}
1175 public:
1176 virtual WidgetInputEvent* AsInputEvent() override { return this; }
1178 WidgetInputEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1179 const WidgetEventTime* aTime = nullptr)
1180 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eInputEventClass, aTime),
1181 mModifiers(0) {}
1183 virtual WidgetEvent* Duplicate() const override {
1184 MOZ_ASSERT(mClass == eInputEventClass,
1185 "Duplicate() must be overridden by sub class");
1186 // Not copying widget, it is a weak reference.
1187 WidgetInputEvent* result =
1188 new WidgetInputEvent(false, mMessage, nullptr, this);
1189 result->AssignInputEventData(*this, true);
1190 result->mFlags = mFlags;
1191 return result;
1195 * Returns a modifier of "Accel" virtual modifier which is used for shortcut
1196 * key.
1198 static Modifier AccelModifier();
1201 * GetModifier() returns a modifier flag which is activated by aDOMKeyName.
1203 static Modifier GetModifier(const nsAString& aDOMKeyName);
1205 // true indicates the accel key on the environment is down
1206 bool IsAccel() const { return ((mModifiers & AccelModifier()) != 0); }
1208 // true indicates the shift key is down
1209 bool IsShift() const { return ((mModifiers & MODIFIER_SHIFT) != 0); }
1210 // true indicates the control key is down
1211 bool IsControl() const { return ((mModifiers & MODIFIER_CONTROL) != 0); }
1212 // true indicates the alt key is down
1213 bool IsAlt() const { return ((mModifiers & MODIFIER_ALT) != 0); }
1214 // true indicates the meta key is down (Command key on macOS, Windows logo key
1215 // on Windows, Super/Hyper key on Linux, Meta key on Android).
1216 bool IsMeta() const { return ((mModifiers & MODIFIER_META) != 0); }
1217 // true indicates the alt graph key is down
1218 // NOTE: on Mac, the option key press causes both IsAlt() and IsAltGrpah()
1219 // return true.
1220 bool IsAltGraph() const { return ((mModifiers & MODIFIER_ALTGRAPH) != 0); }
1221 // true indicates the CapLock LED is turn on.
1222 bool IsCapsLocked() const { return ((mModifiers & MODIFIER_CAPSLOCK) != 0); }
1223 // true indicates the NumLock LED is turn on.
1224 bool IsNumLocked() const { return ((mModifiers & MODIFIER_NUMLOCK) != 0); }
1225 // true indicates the ScrollLock LED is turn on.
1226 bool IsScrollLocked() const {
1227 return ((mModifiers & MODIFIER_SCROLLLOCK) != 0);
1230 // true indicates the Fn key is down, but this is not supported by native
1231 // key event on any platform.
1232 bool IsFn() const { return ((mModifiers & MODIFIER_FN) != 0); }
1233 // true indicates the FnLock LED is turn on, but we don't know such
1234 // keyboards nor platforms.
1235 bool IsFnLocked() const { return ((mModifiers & MODIFIER_FNLOCK) != 0); }
1236 // true indicates the Symbol is down, but this is not supported by native
1237 // key event on any platforms.
1238 bool IsSymbol() const { return ((mModifiers & MODIFIER_SYMBOL) != 0); }
1239 // true indicates the SymbolLock LED is turn on, but we don't know such
1240 // keyboards nor platforms.
1241 bool IsSymbolLocked() const {
1242 return ((mModifiers & MODIFIER_SYMBOLLOCK) != 0);
1245 void InitBasicModifiers(bool aCtrlKey, bool aAltKey, bool aShiftKey,
1246 bool aMetaKey) {
1247 mModifiers = 0;
1248 if (aCtrlKey) {
1249 mModifiers |= MODIFIER_CONTROL;
1251 if (aAltKey) {
1252 mModifiers |= MODIFIER_ALT;
1254 if (aShiftKey) {
1255 mModifiers |= MODIFIER_SHIFT;
1257 if (aMetaKey) {
1258 mModifiers |= MODIFIER_META;
1262 Modifiers mModifiers;
1264 void AssignInputEventData(const WidgetInputEvent& aEvent, bool aCopyTargets) {
1265 AssignGUIEventData(aEvent, aCopyTargets);
1267 mModifiers = aEvent.mModifiers;
1271 /******************************************************************************
1272 * mozilla::InternalUIEvent
1274 * XXX Why this inherits WidgetGUIEvent rather than WidgetEvent?
1275 ******************************************************************************/
1277 class InternalUIEvent : public WidgetGUIEvent {
1278 protected:
1279 InternalUIEvent() : mDetail(0), mCausedByUntrustedEvent(false) {}
1281 InternalUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1282 EventClassID aEventClassID,
1283 const WidgetEventTime* aTime = nullptr)
1284 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, aEventClassID, aTime),
1285 mDetail(0),
1286 mCausedByUntrustedEvent(false) {}
1288 InternalUIEvent(bool aIsTrusted, EventMessage aMessage,
1289 EventClassID aEventClassID,
1290 const WidgetEventTime* aTime = nullptr)
1291 : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, aEventClassID, aTime),
1292 mDetail(0),
1293 mCausedByUntrustedEvent(false) {}
1295 public:
1296 virtual InternalUIEvent* AsUIEvent() override { return this; }
1299 * If the UIEvent is caused by another event (e.g., click event),
1300 * aEventCausesThisEvent should be the event. If there is no such event,
1301 * this should be nullptr.
1303 InternalUIEvent(bool aIsTrusted, EventMessage aMessage,
1304 const WidgetEvent* aEventCausesThisEvent,
1305 const WidgetEventTime* aTime = nullptr)
1306 : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, eUIEventClass, aTime),
1307 mDetail(0),
1308 mCausedByUntrustedEvent(aEventCausesThisEvent &&
1309 !aEventCausesThisEvent->IsTrusted()) {}
1311 virtual WidgetEvent* Duplicate() const override {
1312 MOZ_ASSERT(mClass == eUIEventClass,
1313 "Duplicate() must be overridden by sub class");
1314 InternalUIEvent* result =
1315 new InternalUIEvent(false, mMessage, nullptr, this);
1316 result->AssignUIEventData(*this, true);
1317 result->mFlags = mFlags;
1318 return result;
1321 int32_t mDetail;
1322 // mCausedByUntrustedEvent is true if the event is caused by untrusted event.
1323 bool mCausedByUntrustedEvent;
1325 // If you check the event is a trusted event and NOT caused by an untrusted
1326 // event, IsTrustable() returns what you expected.
1327 bool IsTrustable() const { return IsTrusted() && !mCausedByUntrustedEvent; }
1329 void AssignUIEventData(const InternalUIEvent& aEvent, bool aCopyTargets) {
1330 AssignGUIEventData(aEvent, aCopyTargets);
1332 mDetail = aEvent.mDetail;
1333 mCausedByUntrustedEvent = aEvent.mCausedByUntrustedEvent;
1337 } // namespace mozilla
1339 #endif // mozilla_BasicEvents_h__