Bug 1839316: part 5) Guard the "fetchpriority" attribute behind a pref. r=kershaw...
[gecko.git] / widget / BasicEvents.h
blobdfcf2744ce22c4ade0b3c9d57be8c553a395410d
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 // Similar to above but expected to be used during PreHandleEvent phase.
103 bool mMultiplePreActionsPrevented : 1;
104 // If mIsBeingDispatched is true, the DOM event created from the event is
105 // dispatching into the DOM tree and not completed.
106 bool mIsBeingDispatched : 1;
107 // If mDispatchedAtLeastOnce is true, the event has been dispatched
108 // as a DOM event and the dispatch has been completed in the process.
109 // So, this is false even if the event has already been dispatched
110 // in another process.
111 bool mDispatchedAtLeastOnce : 1;
112 // If mIsSynthesizedForTests is true, the event has been synthesized for
113 // automated tests or something hacky approach of an add-on.
114 bool mIsSynthesizedForTests : 1;
115 // If mExceptionWasRaised is true, one of the event handlers has raised an
116 // exception.
117 bool mExceptionWasRaised : 1;
118 // If mRetargetToNonNativeAnonymous is true and the target is in a non-native
119 // native anonymous subtree, the event target is set to mOriginalTarget.
120 bool mRetargetToNonNativeAnonymous : 1;
121 // If mNoContentDispatch is true, the event is never dispatched to the
122 // event handlers which are added to the contents, onfoo attributes and
123 // properties. Note that this flag is ignored when
124 // EventChainPreVisitor::mForceContentDispatch is set true. For exapmle,
125 // window and document object sets it true. Therefore, web applications
126 // can handle the event if they add event listeners to the window or the
127 // document.
128 // XXX This is an ancient and broken feature, don't use this for new bug
129 // as far as possible.
130 bool mNoContentDispatch : 1;
131 // If mOnlyChromeDispatch is true, the event is dispatched to only chrome.
132 bool mOnlyChromeDispatch : 1;
133 // Indicates if the key combination is reserved by chrome. This is set by
134 // MarkAsReservedByChrome().
135 bool mIsReservedByChrome : 1;
136 // If mOnlySystemGroupDispatchInContent is true, event listeners added to
137 // the default group for non-chrome EventTarget won't be called.
138 // Be aware, if this is true, EventDispatcher needs to check if each event
139 // listener is added to chrome node, so, don't set this to true for the
140 // events which are fired a lot of times like eMouseMove.
141 bool mOnlySystemGroupDispatchInContent : 1;
142 // If mOnlySystemGroupDispatch is true, the event will be dispatched only to
143 // event listeners added in the system group.
144 bool mOnlySystemGroupDispatch : 1;
145 // The event's action will be handled by APZ. The main thread should not
146 // perform its associated action.
147 bool mHandledByAPZ : 1;
148 // True if the event is currently being handled by an event listener that
149 // was registered as a passive listener.
150 bool mInPassiveListener : 1;
151 // If mComposed is true, the event fired by nodes in shadow DOM can cross the
152 // boundary of shadow DOM and light DOM.
153 bool mComposed : 1;
154 // Similar to mComposed. Set it to true to allow events cross the boundary
155 // between native non-anonymous content and native anonymouse content
156 bool mComposedInNativeAnonymousContent : 1;
157 // Set to true for events which are suppressed or delayed so that later a
158 // DelayedEvent of it is dispatched. This is used when parent side process
159 // the key event after content side, and may drop the event if the event
160 // was suppressed or delayed in contents side.
161 // It is also set to true for the events (in a DelayedInputEvent), which will
162 // be dispatched afterwards.
163 bool mIsSuppressedOrDelayed : 1;
164 // Certain mouse events can be marked as positionless to return 0 from
165 // coordinate related getters.
166 bool mIsPositionless : 1;
168 // Flags managing state of propagation between processes.
169 // Note the the following flags shouldn't be referred directly. Use utility
170 // methods instead.
172 // If mNoRemoteProcessDispatch is true, the event is not allowed to be sent
173 // to remote process.
174 bool mNoRemoteProcessDispatch : 1;
175 // If mWantReplyFromContentProcess is true, the event will be redispatched
176 // in the parent process after the content process has handled it. Useful
177 // for when the parent process need the know first how the event was used
178 // by content before handling it itself.
179 bool mWantReplyFromContentProcess : 1;
180 // If mPostedToRemoteProcess is true, the event has been posted to the
181 // remote process (but it's not handled yet if it's not a duplicated event
182 // instance).
183 bool mPostedToRemoteProcess : 1;
184 // If mCameFromAnotherProcess is true, the event came from another process.
185 bool mCameFromAnotherProcess : 1;
188 * Helper methods for methods of DOM Event.
190 inline void StopPropagation() { mPropagationStopped = true; }
191 inline void StopImmediatePropagation() {
192 StopPropagation();
193 mImmediatePropagationStopped = true;
195 inline void PreventDefault(bool aCalledByDefaultHandler = true) {
196 if (!mCancelable) {
197 return;
199 mDefaultPrevented = true;
200 // Note that even if preventDefault() has already been called by chrome,
201 // a call of preventDefault() by content needs to overwrite
202 // mDefaultPreventedByContent to true because in such case, defaultPrevented
203 // must be true when web apps check it after they call preventDefault().
204 if (aCalledByDefaultHandler) {
205 StopCrossProcessForwarding();
206 mDefaultPreventedByChrome = true;
207 } else {
208 mDefaultPreventedByContent = true;
211 // This should be used only before dispatching events into the DOM tree.
212 inline void PreventDefaultBeforeDispatch(
213 CrossProcessForwarding aCrossProcessForwarding) {
214 if (!mCancelable) {
215 return;
217 mDefaultPrevented = true;
218 if (aCrossProcessForwarding == CrossProcessForwarding::eStop) {
219 StopCrossProcessForwarding();
222 inline bool DefaultPrevented() const { return mDefaultPrevented; }
223 inline bool DefaultPreventedByContent() const {
224 MOZ_ASSERT(!mDefaultPreventedByContent || DefaultPrevented());
225 return mDefaultPreventedByContent;
227 inline bool IsTrusted() const { return mIsTrusted; }
228 inline bool PropagationStopped() const { return mPropagationStopped; }
230 // Helper methods to access flags managing state of propagation between
231 // processes.
234 * Prevent to be dispatched to remote process.
236 inline void StopCrossProcessForwarding() {
237 MOZ_ASSERT(!mPostedToRemoteProcess);
238 mNoRemoteProcessDispatch = true;
239 mWantReplyFromContentProcess = false;
242 * Return true if the event shouldn't be dispatched to remote process.
244 inline bool IsCrossProcessForwardingStopped() const {
245 return mNoRemoteProcessDispatch;
248 * Mark the event as waiting reply from remote process.
249 * If the caller needs to win other keyboard event handlers in chrome,
250 * the caller should call StopPropagation() too.
251 * Otherwise, if the caller just needs to know if the event is consumed by
252 * either content or chrome, it should just call this because the event
253 * may be reserved by chrome and it needs to be dispatched into the DOM
254 * tree in chrome for checking if it's reserved before being sent to any
255 * remote processes.
257 inline void MarkAsWaitingReplyFromRemoteProcess() {
258 MOZ_ASSERT(!mPostedToRemoteProcess);
259 mNoRemoteProcessDispatch = false;
260 mWantReplyFromContentProcess = true;
263 * Reset "waiting reply from remote process" state. This is useful when
264 * you dispatch a copy of an event coming from different process.
266 inline void ResetWaitingReplyFromRemoteProcessState() {
267 if (IsWaitingReplyFromRemoteProcess()) {
268 // FYI: mWantReplyFromContentProcess is also used for indicating
269 // "handled in remote process" state. Therefore, only when
270 // IsWaitingReplyFromRemoteProcess() returns true, this should
271 // reset the flag.
272 mWantReplyFromContentProcess = false;
276 * Return true if the event handler should wait reply event. I.e., if this
277 * returns true, any event handler should do nothing with the event.
279 inline bool IsWaitingReplyFromRemoteProcess() const {
280 return !mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
283 * Mark the event as already handled in the remote process. This should be
284 * called when initializing reply events.
286 inline void MarkAsHandledInRemoteProcess() {
287 mNoRemoteProcessDispatch = true;
288 mWantReplyFromContentProcess = true;
289 mPostedToRemoteProcess = false;
292 * Return true if the event has already been handled in the remote process.
294 inline bool IsHandledInRemoteProcess() const {
295 return mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
298 * Return true if the event should be sent back to its parent process.
300 inline bool WantReplyFromContentProcess() const {
301 MOZ_ASSERT(!XRE_IsParentProcess());
302 return IsWaitingReplyFromRemoteProcess();
305 * Mark the event has already posted to a remote process.
307 inline void MarkAsPostedToRemoteProcess() {
308 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
309 mPostedToRemoteProcess = true;
312 * Reset the cross process dispatching state. This should be used when a
313 * process receives the event because the state is in the sender.
315 inline void ResetCrossProcessDispatchingState() {
316 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
317 mPostedToRemoteProcess = false;
318 // Ignore propagation state in the remote process if it's marked as
319 // "waiting reply from remote process" because the process needs to
320 // stop propagation in the process until receiving a reply event.
321 // Note that the propagation stopped flag is important for the reply event
322 // handler in the main process because it's used for making whether it's
323 // ignored by the remote process or not.
324 if (!XRE_IsParentProcess() && IsWaitingReplyFromRemoteProcess()) {
325 mPropagationStopped = mImmediatePropagationStopped = false;
327 // mDispatchedAtLeastOnce indicates the state in current process.
328 mDispatchedAtLeastOnce = false;
331 * Return true if the event has been posted to a remote process.
332 * Note that MarkAsPostedToRemoteProcess() is called by
333 * ParamTraits<mozilla::WidgetEvent>. Therefore, it *might* be possible
334 * that posting the event failed even if this returns true. But that must
335 * really rare. If that'd be problem for you, you should unmark this in
336 * BrowserParent or somewhere.
338 inline bool HasBeenPostedToRemoteProcess() const {
339 return mPostedToRemoteProcess;
342 * Return true if the event came from another process.
344 inline bool CameFromAnotherProcess() const { return mCameFromAnotherProcess; }
346 * Mark the event as coming from another process.
348 inline void MarkAsComingFromAnotherProcess() {
349 mCameFromAnotherProcess = true;
352 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
353 * content because it shouldn't be cancelable.
355 inline void MarkAsReservedByChrome() {
356 MOZ_ASSERT(!mPostedToRemoteProcess);
357 mIsReservedByChrome = true;
358 // For reserved commands (such as Open New Tab), we don't need to wait for
359 // the content to answer, neither to give a chance for content to override
360 // its behavior.
361 StopCrossProcessForwarding();
362 // If the event is reserved by chrome, we shouldn't expose the event to
363 // web contents because such events shouldn't be cancelable. So, it's not
364 // good behavior to fire such events but to ignore the defaultPrevented
365 // attribute value in chrome.
366 mOnlySystemGroupDispatchInContent = true;
369 * Return true if the event is reserved by chrome.
371 inline bool IsReservedByChrome() const {
372 MOZ_ASSERT(!mIsReservedByChrome || (IsCrossProcessForwardingStopped() &&
373 mOnlySystemGroupDispatchInContent));
374 return mIsReservedByChrome;
377 inline void Clear() { SetRawFlags(0); }
378 // Get if either the instance's bit or the aOther's bit is true, the
379 // instance's bit becomes true. In other words, this works like:
380 // eventFlags |= aOther;
381 inline void Union(const BaseEventFlags& aOther) {
382 RawFlags rawFlags = GetRawFlags() | aOther.GetRawFlags();
383 SetRawFlags(rawFlags);
386 private:
387 typedef uint64_t RawFlags;
389 inline void SetRawFlags(RawFlags aRawFlags) {
390 static_assert(sizeof(BaseEventFlags) <= sizeof(RawFlags),
391 "mozilla::EventFlags must not be bigger than the RawFlags");
392 memcpy(this, &aRawFlags, sizeof(BaseEventFlags));
394 inline RawFlags GetRawFlags() const {
395 RawFlags result = 0;
396 memcpy(&result, this, sizeof(BaseEventFlags));
397 return result;
401 /******************************************************************************
402 * mozilla::EventFlags
403 ******************************************************************************/
405 struct EventFlags : public BaseEventFlags {
406 EventFlags() { Clear(); }
409 /******************************************************************************
410 * mozilla::WidgetEventTime
411 ******************************************************************************/
413 class WidgetEventTime {
414 public:
415 // Timestamp when the message was created.
416 TimeStamp mTimeStamp;
418 WidgetEventTime() : mTimeStamp(TimeStamp::Now()) {}
420 explicit WidgetEventTime(const WidgetEventTime* aTime)
421 : mTimeStamp(aTime ? aTime->mTimeStamp : TimeStamp::Now()) {
422 MOZ_ASSERT(aTime != this);
423 MOZ_ASSERT_IF(aTime, !aTime->mTimeStamp.IsNull());
426 explicit WidgetEventTime(TimeStamp aTimeStamp) : mTimeStamp(aTimeStamp) {}
428 void AssignEventTime(const WidgetEventTime& aOther) {
429 mTimeStamp = aOther.mTimeStamp;
433 /******************************************************************************
434 * mozilla::WidgetEvent
435 ******************************************************************************/
437 class WidgetEvent : public WidgetEventTime {
438 private:
439 void SetDefaultCancelableAndBubbles() {
440 switch (mClass) {
441 case eEditorInputEventClass:
442 mFlags.mCancelable = false;
443 mFlags.mBubbles = mFlags.mIsTrusted;
444 break;
445 case eMouseEventClass:
446 mFlags.mCancelable =
447 (mMessage != eMouseEnter && mMessage != eMouseLeave);
448 mFlags.mBubbles = (mMessage != eMouseEnter && mMessage != eMouseLeave);
449 break;
450 case ePointerEventClass:
451 mFlags.mCancelable =
452 (mMessage != ePointerEnter && mMessage != ePointerLeave &&
453 mMessage != ePointerCancel && mMessage != ePointerGotCapture &&
454 mMessage != ePointerLostCapture);
455 mFlags.mBubbles =
456 (mMessage != ePointerEnter && mMessage != ePointerLeave);
457 break;
458 case eDragEventClass:
459 mFlags.mCancelable = (mMessage != eDragExit && mMessage != eDragLeave &&
460 mMessage != eDragEnd);
461 mFlags.mBubbles = true;
462 break;
463 case eSMILTimeEventClass:
464 mFlags.mCancelable = false;
465 mFlags.mBubbles = false;
466 break;
467 case eTransitionEventClass:
468 case eAnimationEventClass:
469 mFlags.mCancelable = false;
470 mFlags.mBubbles = true;
471 break;
472 case eCompositionEventClass:
473 // XXX compositionstart is cancelable in draft of DOM3 Events.
474 // However, it doesn't make sense for us, we cannot cancel
475 // composition when we send compositionstart event.
476 mFlags.mCancelable = false;
477 mFlags.mBubbles = true;
478 break;
479 default:
480 if (mMessage == eResize || mMessage == eMozVisualResize ||
481 mMessage == eMozVisualScroll || mMessage == eEditorInput ||
482 mMessage == eFormSelect) {
483 mFlags.mCancelable = false;
484 } else {
485 mFlags.mCancelable = true;
487 mFlags.mBubbles = true;
488 break;
492 protected:
493 WidgetEvent(bool aIsTrusted, EventMessage aMessage,
494 EventClassID aEventClassID,
495 const WidgetEventTime* aTime = nullptr)
496 : WidgetEventTime(aTime),
497 mClass(aEventClassID),
498 mMessage(aMessage),
499 mRefPoint(0, 0),
500 mLastRefPoint(0, 0),
501 mFocusSequenceNumber(0),
502 mSpecifiedEventType(nullptr),
503 mPath(nullptr),
504 mLayersId(layers::LayersId{0}) {
505 MOZ_COUNT_CTOR(WidgetEvent);
506 mFlags.Clear();
507 mFlags.mIsTrusted = aIsTrusted;
508 SetDefaultCancelableAndBubbles();
509 SetDefaultComposed();
510 SetDefaultComposedInNativeAnonymousContent();
513 WidgetEvent() : WidgetEventTime(), mPath(nullptr) {
514 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() {
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__