Bug 1884032 [wpt PR 44942] - [css-color] add missing colorscheme-aware tests, a=testonly
[gecko.git] / widget / BasicEvents.h
blobb3707f1cf4d5da681128b7b48c573221a03a32f3
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 // If mIsBeingDispatched is true, the DOM event created from the event is
103 // dispatching into the DOM tree and not completed.
104 bool mIsBeingDispatched : 1;
105 // If mDispatchedAtLeastOnce is true, the event has been dispatched
106 // as a DOM event and the dispatch has been completed in the process.
107 // So, this is false even if the event has already been dispatched
108 // in another process.
109 bool mDispatchedAtLeastOnce : 1;
110 // If mIsSynthesizedForTests is true, the event has been synthesized for
111 // automated tests or something hacky approach of an add-on.
112 bool mIsSynthesizedForTests : 1;
113 // If mExceptionWasRaised is true, one of the event handlers has raised an
114 // exception.
115 bool mExceptionWasRaised : 1;
116 // If mRetargetToNonNativeAnonymous is true and the target is in a non-native
117 // native anonymous subtree, the event target is set to mOriginalTarget.
118 bool mRetargetToNonNativeAnonymous : 1;
119 // If mNoContentDispatch is true, the event is never dispatched to the
120 // event handlers which are added to the contents, onfoo attributes and
121 // properties. Note that this flag is ignored when
122 // EventChainPreVisitor::mForceContentDispatch is set true. For exapmle,
123 // window and document object sets it true. Therefore, web applications
124 // can handle the event if they add event listeners to the window or the
125 // document.
126 // XXX This is an ancient and broken feature, don't use this for new bug
127 // as far as possible.
128 bool mNoContentDispatch : 1;
129 // If mOnlyChromeDispatch is true, the event is dispatched to only chrome.
130 bool mOnlyChromeDispatch : 1;
131 // Indicates if the key combination is reserved by chrome. This is set by
132 // MarkAsReservedByChrome().
133 bool mIsReservedByChrome : 1;
134 // If mOnlySystemGroupDispatchInContent is true, event listeners added to
135 // the default group for non-chrome EventTarget won't be called.
136 // Be aware, if this is true, EventDispatcher needs to check if each event
137 // listener is added to chrome node, so, don't set this to true for the
138 // events which are fired a lot of times like eMouseMove.
139 bool mOnlySystemGroupDispatchInContent : 1;
140 // If mOnlySystemGroupDispatch is true, the event will be dispatched only to
141 // event listeners added in the system group.
142 bool mOnlySystemGroupDispatch : 1;
143 // The event's action will be handled by APZ. The main thread should not
144 // perform its associated action.
145 bool mHandledByAPZ : 1;
146 // True if the event is currently being handled by an event listener that
147 // was registered as a passive listener.
148 bool mInPassiveListener : 1;
149 // If mComposed is true, the event fired by nodes in shadow DOM can cross the
150 // boundary of shadow DOM and light DOM.
151 bool mComposed : 1;
152 // Similar to mComposed. Set it to true to allow events cross the boundary
153 // between native non-anonymous content and native anonymouse content
154 bool mComposedInNativeAnonymousContent : 1;
155 // Set to true for events which are suppressed or delayed so that later a
156 // DelayedEvent of it is dispatched. This is used when parent side process
157 // the key event after content side, and may drop the event if the event
158 // was suppressed or delayed in contents side.
159 // It is also set to true for the events (in a DelayedInputEvent), which will
160 // be dispatched afterwards.
161 bool mIsSuppressedOrDelayed : 1;
162 // Certain mouse events can be marked as positionless to return 0 from
163 // coordinate related getters.
164 bool mIsPositionless : 1;
166 // Flags managing state of propagation between processes.
167 // Note the the following flags shouldn't be referred directly. Use utility
168 // methods instead.
170 // If mNoRemoteProcessDispatch is true, the event is not allowed to be sent
171 // to remote process.
172 bool mNoRemoteProcessDispatch : 1;
173 // If mWantReplyFromContentProcess is true, the event will be redispatched
174 // in the parent process after the content process has handled it. Useful
175 // for when the parent process need the know first how the event was used
176 // by content before handling it itself.
177 bool mWantReplyFromContentProcess : 1;
178 // If mPostedToRemoteProcess is true, the event has been posted to the
179 // remote process (but it's not handled yet if it's not a duplicated event
180 // instance).
181 bool mPostedToRemoteProcess : 1;
182 // If mCameFromAnotherProcess is true, the event came from another process.
183 bool mCameFromAnotherProcess : 1;
186 * Helper methods for methods of DOM Event.
188 inline void StopPropagation() { mPropagationStopped = true; }
189 inline void StopImmediatePropagation() {
190 StopPropagation();
191 mImmediatePropagationStopped = true;
193 inline void PreventDefault(bool aCalledByDefaultHandler = true) {
194 if (!mCancelable) {
195 return;
197 mDefaultPrevented = true;
198 // Note that even if preventDefault() has already been called by chrome,
199 // a call of preventDefault() by content needs to overwrite
200 // mDefaultPreventedByContent to true because in such case, defaultPrevented
201 // must be true when web apps check it after they call preventDefault().
202 if (aCalledByDefaultHandler) {
203 StopCrossProcessForwarding();
204 mDefaultPreventedByChrome = true;
205 } else {
206 mDefaultPreventedByContent = true;
209 // This should be used only before dispatching events into the DOM tree.
210 inline void PreventDefaultBeforeDispatch(
211 CrossProcessForwarding aCrossProcessForwarding) {
212 if (!mCancelable) {
213 return;
215 mDefaultPrevented = true;
216 if (aCrossProcessForwarding == CrossProcessForwarding::eStop) {
217 StopCrossProcessForwarding();
220 inline bool DefaultPrevented() const { return mDefaultPrevented; }
221 inline bool DefaultPreventedByContent() const {
222 MOZ_ASSERT(!mDefaultPreventedByContent || DefaultPrevented());
223 return mDefaultPreventedByContent;
225 inline bool IsTrusted() const { return mIsTrusted; }
226 inline bool PropagationStopped() const { return mPropagationStopped; }
228 // Helper methods to access flags managing state of propagation between
229 // processes.
232 * Prevent to be dispatched to remote process.
234 inline void StopCrossProcessForwarding() {
235 MOZ_ASSERT(!mPostedToRemoteProcess);
236 mNoRemoteProcessDispatch = true;
237 mWantReplyFromContentProcess = false;
240 * Return true if the event shouldn't be dispatched to remote process.
242 inline bool IsCrossProcessForwardingStopped() const {
243 return mNoRemoteProcessDispatch;
246 * Mark the event as waiting reply from remote process.
247 * If the caller needs to win other keyboard event handlers in chrome,
248 * the caller should call StopPropagation() too.
249 * Otherwise, if the caller just needs to know if the event is consumed by
250 * either content or chrome, it should just call this because the event
251 * may be reserved by chrome and it needs to be dispatched into the DOM
252 * tree in chrome for checking if it's reserved before being sent to any
253 * remote processes.
255 inline void MarkAsWaitingReplyFromRemoteProcess() {
256 MOZ_ASSERT(!mPostedToRemoteProcess);
257 mNoRemoteProcessDispatch = false;
258 mWantReplyFromContentProcess = true;
261 * Reset "waiting reply from remote process" state. This is useful when
262 * you dispatch a copy of an event coming from different process.
264 inline void ResetWaitingReplyFromRemoteProcessState() {
265 if (IsWaitingReplyFromRemoteProcess()) {
266 // FYI: mWantReplyFromContentProcess is also used for indicating
267 // "handled in remote process" state. Therefore, only when
268 // IsWaitingReplyFromRemoteProcess() returns true, this should
269 // reset the flag.
270 mWantReplyFromContentProcess = false;
274 * Return true if the event handler should wait reply event. I.e., if this
275 * returns true, any event handler should do nothing with the event.
277 inline bool IsWaitingReplyFromRemoteProcess() const {
278 return !mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
281 * Mark the event as already handled in the remote process. This should be
282 * called when initializing reply events.
284 inline void MarkAsHandledInRemoteProcess() {
285 mNoRemoteProcessDispatch = true;
286 mWantReplyFromContentProcess = true;
287 mPostedToRemoteProcess = false;
290 * Return true if the event has already been handled in the remote process.
292 inline bool IsHandledInRemoteProcess() const {
293 return mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
296 * Return true if the event should be sent back to its parent process.
298 inline bool WantReplyFromContentProcess() const {
299 MOZ_ASSERT(!XRE_IsParentProcess());
300 return IsWaitingReplyFromRemoteProcess();
303 * Mark the event has already posted to a remote process.
305 inline void MarkAsPostedToRemoteProcess() {
306 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
307 mPostedToRemoteProcess = true;
310 * Reset the cross process dispatching state. This should be used when a
311 * process receives the event because the state is in the sender.
313 inline void ResetCrossProcessDispatchingState() {
314 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
315 mPostedToRemoteProcess = false;
316 // Ignore propagation state in the remote process if it's marked as
317 // "waiting reply from remote process" because the process needs to
318 // stop propagation in the process until receiving a reply event.
319 // Note that the propagation stopped flag is important for the reply event
320 // handler in the main process because it's used for making whether it's
321 // ignored by the remote process or not.
322 if (!XRE_IsParentProcess() && IsWaitingReplyFromRemoteProcess()) {
323 mPropagationStopped = mImmediatePropagationStopped = false;
325 // mDispatchedAtLeastOnce indicates the state in current process.
326 mDispatchedAtLeastOnce = false;
329 * Return true if the event has been posted to a remote process.
330 * Note that MarkAsPostedToRemoteProcess() is called by
331 * ParamTraits<mozilla::WidgetEvent>. Therefore, it *might* be possible
332 * that posting the event failed even if this returns true. But that must
333 * really rare. If that'd be problem for you, you should unmark this in
334 * BrowserParent or somewhere.
336 inline bool HasBeenPostedToRemoteProcess() const {
337 return mPostedToRemoteProcess;
340 * Return true if the event came from another process.
342 inline bool CameFromAnotherProcess() const { return mCameFromAnotherProcess; }
344 * Mark the event as coming from another process.
346 inline void MarkAsComingFromAnotherProcess() {
347 mCameFromAnotherProcess = true;
350 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
351 * content because it shouldn't be cancelable.
353 inline void MarkAsReservedByChrome() {
354 MOZ_ASSERT(!mPostedToRemoteProcess);
355 mIsReservedByChrome = true;
356 // For reserved commands (such as Open New Tab), we don't need to wait for
357 // the content to answer, neither to give a chance for content to override
358 // its behavior.
359 StopCrossProcessForwarding();
360 // If the event is reserved by chrome, we shouldn't expose the event to
361 // web contents because such events shouldn't be cancelable. So, it's not
362 // good behavior to fire such events but to ignore the defaultPrevented
363 // attribute value in chrome.
364 mOnlySystemGroupDispatchInContent = true;
367 * Return true if the event is reserved by chrome.
369 inline bool IsReservedByChrome() const {
370 MOZ_ASSERT(!mIsReservedByChrome || (IsCrossProcessForwardingStopped() &&
371 mOnlySystemGroupDispatchInContent));
372 return mIsReservedByChrome;
375 inline void Clear() { SetRawFlags(0); }
376 // Get if either the instance's bit or the aOther's bit is true, the
377 // instance's bit becomes true. In other words, this works like:
378 // eventFlags |= aOther;
379 inline void Union(const BaseEventFlags& aOther) {
380 RawFlags rawFlags = GetRawFlags() | aOther.GetRawFlags();
381 SetRawFlags(rawFlags);
384 private:
385 typedef uint64_t RawFlags;
387 inline void SetRawFlags(RawFlags aRawFlags) {
388 static_assert(sizeof(BaseEventFlags) <= sizeof(RawFlags),
389 "mozilla::EventFlags must not be bigger than the RawFlags");
390 memcpy(this, &aRawFlags, sizeof(BaseEventFlags));
392 inline RawFlags GetRawFlags() const {
393 RawFlags result = 0;
394 memcpy(&result, this, sizeof(BaseEventFlags));
395 return result;
399 /******************************************************************************
400 * mozilla::EventFlags
401 ******************************************************************************/
403 struct EventFlags : public BaseEventFlags {
404 EventFlags() { Clear(); }
407 /******************************************************************************
408 * mozilla::WidgetEventTime
409 ******************************************************************************/
411 class WidgetEventTime {
412 public:
413 // Timestamp when the message was created.
414 TimeStamp mTimeStamp;
416 WidgetEventTime() : mTimeStamp(TimeStamp::Now()) {}
418 explicit WidgetEventTime(const WidgetEventTime* aTime)
419 : mTimeStamp(aTime ? aTime->mTimeStamp : TimeStamp::Now()) {
420 MOZ_ASSERT(aTime != this);
421 MOZ_ASSERT_IF(aTime, !aTime->mTimeStamp.IsNull());
424 explicit WidgetEventTime(TimeStamp aTimeStamp) : mTimeStamp(aTimeStamp) {}
426 void AssignEventTime(const WidgetEventTime& aOther) {
427 mTimeStamp = aOther.mTimeStamp;
431 /******************************************************************************
432 * mozilla::WidgetEvent
433 ******************************************************************************/
435 class WidgetEvent : public WidgetEventTime {
436 private:
437 void SetDefaultCancelableAndBubbles() {
438 switch (mClass) {
439 case eEditorInputEventClass:
440 mFlags.mCancelable = false;
441 mFlags.mBubbles = mFlags.mIsTrusted;
442 break;
443 case eMouseEventClass:
444 mFlags.mCancelable =
445 (mMessage != eMouseEnter && mMessage != eMouseLeave);
446 mFlags.mBubbles = (mMessage != eMouseEnter && mMessage != eMouseLeave);
447 break;
448 case ePointerEventClass:
449 mFlags.mCancelable =
450 (mMessage != ePointerEnter && mMessage != ePointerLeave &&
451 mMessage != ePointerCancel && mMessage != ePointerGotCapture &&
452 mMessage != ePointerLostCapture);
453 mFlags.mBubbles =
454 (mMessage != ePointerEnter && mMessage != ePointerLeave);
455 break;
456 case eDragEventClass:
457 mFlags.mCancelable = (mMessage != eDragExit && mMessage != eDragLeave &&
458 mMessage != eDragEnd);
459 mFlags.mBubbles = true;
460 break;
461 case eSMILTimeEventClass:
462 mFlags.mCancelable = false;
463 mFlags.mBubbles = false;
464 break;
465 case eTransitionEventClass:
466 case eAnimationEventClass:
467 mFlags.mCancelable = false;
468 mFlags.mBubbles = true;
469 break;
470 case eCompositionEventClass:
471 // XXX compositionstart is cancelable in draft of DOM3 Events.
472 // However, it doesn't make sense for us, we cannot cancel
473 // composition when we send compositionstart event.
474 mFlags.mCancelable = false;
475 mFlags.mBubbles = true;
476 break;
477 default:
478 if (mMessage == eResize || mMessage == eMozVisualResize ||
479 mMessage == eMozVisualScroll || mMessage == eEditorInput ||
480 mMessage == eFormSelect) {
481 mFlags.mCancelable = false;
482 } else {
483 mFlags.mCancelable = true;
485 mFlags.mBubbles = true;
486 break;
490 protected:
491 WidgetEvent(bool aIsTrusted, EventMessage aMessage,
492 EventClassID aEventClassID,
493 const WidgetEventTime* aTime = nullptr)
494 : WidgetEventTime(aTime),
495 mClass(aEventClassID),
496 mMessage(aMessage),
497 mRefPoint(0, 0),
498 mLastRefPoint(0, 0),
499 mFocusSequenceNumber(0),
500 mSpecifiedEventType(nullptr),
501 mPath(nullptr),
502 mLayersId(layers::LayersId{0}) {
503 MOZ_COUNT_CTOR(WidgetEvent);
504 mFlags.Clear();
505 mFlags.mIsTrusted = aIsTrusted;
506 SetDefaultCancelableAndBubbles();
507 SetDefaultComposed();
508 SetDefaultComposedInNativeAnonymousContent();
511 WidgetEvent() : mPath(nullptr) { MOZ_COUNT_CTOR(WidgetEvent); }
513 public:
514 WidgetEvent(bool aIsTrusted, EventMessage aMessage,
515 const WidgetEventTime* aTime = nullptr)
516 : WidgetEvent(aIsTrusted, aMessage, eBasicEventClass, aTime) {}
518 MOZ_COUNTED_DTOR_VIRTUAL(WidgetEvent)
520 WidgetEvent(const WidgetEvent& aOther) : WidgetEventTime(aOther) {
521 MOZ_COUNT_CTOR(WidgetEvent);
522 *this = aOther;
524 WidgetEvent& operator=(const WidgetEvent& aOther) = default;
526 WidgetEvent(WidgetEvent&& aOther)
527 : WidgetEventTime(std::move(aOther)),
528 mClass(aOther.mClass),
529 mMessage(aOther.mMessage),
530 mRefPoint(std::move(aOther.mRefPoint)),
531 mLastRefPoint(std::move(aOther.mLastRefPoint)),
532 mFocusSequenceNumber(aOther.mFocusSequenceNumber),
533 mFlags(std::move(aOther.mFlags)),
534 mSpecifiedEventType(std::move(aOther.mSpecifiedEventType)),
535 mSpecifiedEventTypeString(std::move(aOther.mSpecifiedEventTypeString)),
536 mTarget(std::move(aOther.mTarget)),
537 mCurrentTarget(std::move(aOther.mCurrentTarget)),
538 mOriginalTarget(std::move(aOther.mOriginalTarget)),
539 mRelatedTarget(std::move(aOther.mRelatedTarget)),
540 mOriginalRelatedTarget(std::move(aOther.mOriginalRelatedTarget)),
541 mPath(std::move(aOther.mPath)) {
542 MOZ_COUNT_CTOR(WidgetEvent);
544 WidgetEvent& operator=(WidgetEvent&& aOther) = default;
546 virtual WidgetEvent* Duplicate() const {
547 MOZ_ASSERT(mClass == eBasicEventClass,
548 "Duplicate() must be overridden by sub class");
549 WidgetEvent* result = new WidgetEvent(false, mMessage, this);
550 result->AssignEventData(*this, true);
551 result->mFlags = mFlags;
552 return result;
555 EventClassID mClass;
556 EventMessage mMessage;
557 // Relative to the widget of the event, or if there is no widget then it is
558 // in screen coordinates. Not modified by layout code.
559 // This is in visual coordinates, i.e. the correct RelativeTo value that
560 // expresses what this is relative to is `{viewportFrame, Visual}`, where
561 // `viewportFrame` is the viewport frame of the widget's root document.
562 LayoutDeviceIntPoint mRefPoint;
563 // The previous mRefPoint, if known, used to calculate mouse movement deltas.
564 LayoutDeviceIntPoint mLastRefPoint;
565 // The sequence number of the last potentially focus changing event handled
566 // by APZ. This is used to track when that event has been processed by
567 // content, and focus can be reconfirmed for async keyboard scrolling.
568 uint64_t mFocusSequenceNumber;
569 // See BaseEventFlags definition for the detail.
570 BaseEventFlags mFlags;
572 // If JS creates an event with unknown event type or known event type but
573 // for different event interface, the event type is stored to this.
574 // NOTE: This is always used if the instance is a WidgetCommandEvent instance
575 // or "input" event is dispatched with dom::Event class.
576 RefPtr<nsAtom> mSpecifiedEventType;
578 // nsAtom isn't available on non-main thread due to unsafe. Therefore,
579 // mSpecifiedEventTypeString is used instead of mSpecifiedEventType if
580 // the event is created in non-main thread.
581 nsString mSpecifiedEventTypeString;
583 // Event targets, needed by DOM Events
584 // Note that when you need event target for DOM event, you should use
585 // Get*DOMEventTarget() instead of accessing these members directly.
586 nsCOMPtr<dom::EventTarget> mTarget;
587 nsCOMPtr<dom::EventTarget> mCurrentTarget;
588 nsCOMPtr<dom::EventTarget> mOriginalTarget;
590 /// The possible related target
591 nsCOMPtr<dom::EventTarget> mRelatedTarget;
592 nsCOMPtr<dom::EventTarget> mOriginalRelatedTarget;
594 nsTArray<EventTargetChainItem>* mPath;
596 // The LayersId of the content process that this event should be
597 // dispatched to. This field is only used in the chrome process
598 // and doesn't get remoted to child processes.
599 layers::LayersId mLayersId;
601 dom::EventTarget* GetDOMEventTarget() const;
602 dom::EventTarget* GetCurrentDOMEventTarget() const;
603 dom::EventTarget* GetOriginalDOMEventTarget() const;
605 void AssignEventData(const WidgetEvent& aEvent, bool aCopyTargets) {
606 // mClass should be initialized with the constructor.
607 // mMessage should be initialized with the constructor.
608 mRefPoint = aEvent.mRefPoint;
609 // mLastRefPoint doesn't need to be copied.
610 mFocusSequenceNumber = aEvent.mFocusSequenceNumber;
611 // mLayersId intentionally not copied, since it's not used within content
612 AssignEventTime(aEvent);
613 // mFlags should be copied manually if it's necessary.
614 mSpecifiedEventType = aEvent.mSpecifiedEventType;
615 // mSpecifiedEventTypeString should be copied manually if it's necessary.
616 mTarget = aCopyTargets ? aEvent.mTarget : nullptr;
617 mCurrentTarget = aCopyTargets ? aEvent.mCurrentTarget : nullptr;
618 mOriginalTarget = aCopyTargets ? aEvent.mOriginalTarget : nullptr;
619 mRelatedTarget = aCopyTargets ? aEvent.mRelatedTarget : nullptr;
620 mOriginalRelatedTarget =
621 aCopyTargets ? aEvent.mOriginalRelatedTarget : nullptr;
625 * Helper methods for methods of DOM Event.
627 void StopPropagation() { mFlags.StopPropagation(); }
628 void StopImmediatePropagation() { mFlags.StopImmediatePropagation(); }
629 void PreventDefault(bool aCalledByDefaultHandler = true,
630 nsIPrincipal* aPrincipal = nullptr);
632 void PreventDefaultBeforeDispatch(
633 CrossProcessForwarding aCrossProcessForwarding) {
634 mFlags.PreventDefaultBeforeDispatch(aCrossProcessForwarding);
636 bool DefaultPrevented() const { return mFlags.DefaultPrevented(); }
637 bool DefaultPreventedByContent() const {
638 return mFlags.DefaultPreventedByContent();
640 bool IsTrusted() const { return mFlags.IsTrusted(); }
641 bool PropagationStopped() const { return mFlags.PropagationStopped(); }
644 * Prevent to be dispatched to remote process.
646 inline void StopCrossProcessForwarding() {
647 mFlags.StopCrossProcessForwarding();
650 * Return true if the event shouldn't be dispatched to remote process.
652 inline bool IsCrossProcessForwardingStopped() const {
653 return mFlags.IsCrossProcessForwardingStopped();
656 * Mark the event as waiting reply from remote process.
657 * Note that this also stops immediate propagation in current process.
659 inline void MarkAsWaitingReplyFromRemoteProcess() {
660 mFlags.MarkAsWaitingReplyFromRemoteProcess();
663 * Reset "waiting reply from remote process" state. This is useful when
664 * you dispatch a copy of an event coming from different process.
666 inline void ResetWaitingReplyFromRemoteProcessState() {
667 mFlags.ResetWaitingReplyFromRemoteProcessState();
670 * Return true if the event handler should wait reply event. I.e., if this
671 * returns true, any event handler should do nothing with the event.
673 inline bool IsWaitingReplyFromRemoteProcess() const {
674 return mFlags.IsWaitingReplyFromRemoteProcess();
677 * Mark the event as already handled in the remote process. This should be
678 * called when initializing reply events.
680 inline void MarkAsHandledInRemoteProcess() {
681 mFlags.MarkAsHandledInRemoteProcess();
684 * Return true if the event has already been handled in the remote process.
685 * I.e., if this returns true, the event is a reply event.
687 inline bool IsHandledInRemoteProcess() const {
688 return mFlags.IsHandledInRemoteProcess();
691 * Return true if the event should be sent back to its parent process.
692 * So, usual event handlers shouldn't call this.
694 inline bool WantReplyFromContentProcess() const {
695 return mFlags.WantReplyFromContentProcess();
698 * Mark the event has already posted to a remote process.
700 inline void MarkAsPostedToRemoteProcess() {
701 mFlags.MarkAsPostedToRemoteProcess();
704 * Reset the cross process dispatching state. This should be used when a
705 * process receives the event because the state is in the sender.
707 inline void ResetCrossProcessDispatchingState() {
708 mFlags.ResetCrossProcessDispatchingState();
711 * Return true if the event has been posted to a remote process.
713 inline bool HasBeenPostedToRemoteProcess() const {
714 return mFlags.HasBeenPostedToRemoteProcess();
717 * Return true if the event came from another process.
719 inline bool CameFromAnotherProcess() const {
720 return mFlags.CameFromAnotherProcess();
723 * Mark the event as coming from another process.
725 inline void MarkAsComingFromAnotherProcess() {
726 mFlags.MarkAsComingFromAnotherProcess();
729 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
730 * content because it shouldn't be cancelable.
732 inline void MarkAsReservedByChrome() { mFlags.MarkAsReservedByChrome(); }
734 * Return true if the event is reserved by chrome.
736 inline bool IsReservedByChrome() const { return mFlags.IsReservedByChrome(); }
739 * Utils for checking event types
743 * As*Event() returns the pointer of the instance only when the instance is
744 * the class or one of its derived class.
746 #define NS_ROOT_EVENT_CLASS(aPrefix, aName)
747 #define NS_EVENT_CLASS(aPrefix, aName) \
748 virtual aPrefix##aName* As##aName(); \
749 const aPrefix##aName* As##aName() const;
751 #include "mozilla/EventClassList.h"
753 #undef NS_EVENT_CLASS
754 #undef NS_ROOT_EVENT_CLASS
757 * Returns true if the event is a query content event.
759 bool IsQueryContentEvent() const;
761 * Returns true if the event is a selection event.
763 bool IsSelectionEvent() const;
765 * Returns true if the event is a content command event.
767 bool IsContentCommandEvent() const;
770 * Returns true if the event mMessage is one of mouse events.
772 bool HasMouseEventMessage() const;
774 * Returns true if the event mMessage is one of drag events.
776 bool HasDragEventMessage() const;
778 * Returns true if aMessage or mMessage is one of key events.
780 static bool IsKeyEventMessage(EventMessage aMessage);
781 bool HasKeyEventMessage() const { return IsKeyEventMessage(mMessage); }
783 * Returns true if the event mMessage is one of composition events or text
784 * event.
786 bool HasIMEEventMessage() const;
789 * Returns true if the event can be sent to remote process.
791 bool CanBeSentToRemoteProcess() const;
793 * Returns true if the original target is a remote process and the event
794 * will be posted to the remote process later.
796 bool WillBeSentToRemoteProcess() const;
798 * Returns true if the event is related to IME handling. It includes
799 * IME events, query content events and selection events.
800 * Be careful when you use this.
802 bool IsIMERelatedEvent() const;
805 * Whether the event should be handled by the frame of the mouse cursor
806 * position or not. When it should be handled there (e.g., the mouse events),
807 * this returns true.
809 bool IsUsingCoordinates() const;
811 * Whether the event should be handled by the focused DOM window in the
812 * same top level window's or not. E.g., key events, IME related events
813 * (including the query content events, they are used in IME transaction)
814 * should be handled by the (last) focused window rather than the dispatched
815 * window.
817 * NOTE: Even if this returns true, the event isn't going to be handled by the
818 * application level active DOM window which is on another top level window.
819 * So, when the event is fired on a deactive window, the event is going to be
820 * handled by the last focused DOM window in the last focused window.
822 bool IsTargetedAtFocusedWindow() const;
824 * Whether the event should be handled by the focused content or not. E.g.,
825 * key events, IME related events and other input events which are not handled
826 * by the frame of the mouse cursor position.
828 * NOTE: Even if this returns true, the event isn't going to be handled by the
829 * application level active DOM window which is on another top level window.
830 * So, when the event is fired on a deactive window, the event is going to be
831 * handled by the last focused DOM element of the last focused DOM window in
832 * the last focused window.
834 bool IsTargetedAtFocusedContent() const;
836 * Whether the event should cause a DOM event.
838 bool IsAllowedToDispatchDOMEvent() const;
840 * Whether the event should be dispatched in system group.
842 bool IsAllowedToDispatchInSystemGroup() const;
844 * Whether the event should be blocked for fingerprinting resistance.
846 bool IsBlockedForFingerprintingResistance() const;
848 * Whether the event handler can flush pending notifications or not.
850 bool AllowFlushingPendingNotifications() const;
852 * Initialize mComposed
854 void SetDefaultComposed() {
855 switch (mClass) {
856 case eClipboardEventClass:
857 mFlags.mComposed = true;
858 break;
859 case eCompositionEventClass:
860 mFlags.mComposed =
861 mMessage == eCompositionStart || mMessage == eCompositionUpdate ||
862 mMessage == eCompositionChange || mMessage == eCompositionEnd;
863 break;
864 case eDragEventClass:
865 // All drag & drop events are composed
866 mFlags.mComposed = mMessage == eDrag || mMessage == eDragEnd ||
867 mMessage == eDragEnter || mMessage == eDragExit ||
868 mMessage == eDragLeave || mMessage == eDragOver ||
869 mMessage == eDragStart || mMessage == eDrop;
870 break;
871 case eEditorInputEventClass:
872 mFlags.mComposed =
873 mMessage == eEditorInput || mMessage == eEditorBeforeInput;
874 break;
875 case eFocusEventClass:
876 mFlags.mComposed = mMessage == eBlur || mMessage == eFocus ||
877 mMessage == eFocusOut || mMessage == eFocusIn;
878 break;
879 case eKeyboardEventClass:
880 mFlags.mComposed =
881 mMessage == eKeyDown || mMessage == eKeyUp || mMessage == eKeyPress;
882 break;
883 case eMouseEventClass:
884 mFlags.mComposed =
885 mMessage == eMouseClick || mMessage == eMouseDoubleClick ||
886 mMessage == eMouseAuxClick || mMessage == eMouseDown ||
887 mMessage == eMouseUp || mMessage == eMouseOver ||
888 mMessage == eMouseOut || mMessage == eMouseMove ||
889 mMessage == eContextMenu || mMessage == eXULPopupShowing ||
890 mMessage == eXULPopupHiding || mMessage == eXULPopupShown ||
891 mMessage == eXULPopupHidden;
892 break;
893 case ePointerEventClass:
894 // All pointer events are composed
895 mFlags.mComposed =
896 mMessage == ePointerDown || mMessage == ePointerMove ||
897 mMessage == ePointerUp || mMessage == ePointerCancel ||
898 mMessage == ePointerOver || mMessage == ePointerOut ||
899 mMessage == ePointerGotCapture || mMessage == ePointerLostCapture;
900 break;
901 case eTouchEventClass:
902 // All touch events are composed
903 mFlags.mComposed = mMessage == eTouchStart || mMessage == eTouchEnd ||
904 mMessage == eTouchMove || mMessage == eTouchCancel;
905 break;
906 case eUIEventClass:
907 mFlags.mComposed = mMessage == eLegacyDOMFocusIn ||
908 mMessage == eLegacyDOMFocusOut ||
909 mMessage == eLegacyDOMActivate;
910 break;
911 case eWheelEventClass:
912 // All wheel events are composed
913 mFlags.mComposed = mMessage == eWheel;
914 break;
915 case eMouseScrollEventClass:
916 // Legacy mouse scroll events are composed too, for consistency with
917 // wheel.
918 mFlags.mComposed = mMessage == eLegacyMouseLineOrPageScroll ||
919 mMessage == eLegacyMousePixelScroll;
920 break;
921 default:
922 mFlags.mComposed = false;
923 break;
927 void SetComposed(const nsAString& aEventTypeArg) {
928 mFlags.mComposed = // composition events
929 aEventTypeArg.EqualsLiteral("compositionstart") ||
930 aEventTypeArg.EqualsLiteral("compositionupdate") ||
931 aEventTypeArg.EqualsLiteral("compositionend") ||
932 aEventTypeArg.EqualsLiteral("text") ||
933 // drag and drop events
934 aEventTypeArg.EqualsLiteral("dragstart") ||
935 aEventTypeArg.EqualsLiteral("drag") ||
936 aEventTypeArg.EqualsLiteral("dragenter") ||
937 aEventTypeArg.EqualsLiteral("dragexit") ||
938 aEventTypeArg.EqualsLiteral("dragleave") ||
939 aEventTypeArg.EqualsLiteral("dragover") ||
940 aEventTypeArg.EqualsLiteral("drop") ||
941 aEventTypeArg.EqualsLiteral("dropend") ||
942 // editor input events
943 aEventTypeArg.EqualsLiteral("input") ||
944 aEventTypeArg.EqualsLiteral("beforeinput") ||
945 // focus events
946 aEventTypeArg.EqualsLiteral("blur") ||
947 aEventTypeArg.EqualsLiteral("focus") ||
948 aEventTypeArg.EqualsLiteral("focusin") ||
949 aEventTypeArg.EqualsLiteral("focusout") ||
950 // keyboard events
951 aEventTypeArg.EqualsLiteral("keydown") ||
952 aEventTypeArg.EqualsLiteral("keyup") ||
953 aEventTypeArg.EqualsLiteral("keypress") ||
954 // mouse events
955 aEventTypeArg.EqualsLiteral("click") ||
956 aEventTypeArg.EqualsLiteral("dblclick") ||
957 aEventTypeArg.EqualsLiteral("mousedown") ||
958 aEventTypeArg.EqualsLiteral("mouseup") ||
959 aEventTypeArg.EqualsLiteral("mouseenter") ||
960 aEventTypeArg.EqualsLiteral("mouseleave") ||
961 aEventTypeArg.EqualsLiteral("mouseover") ||
962 aEventTypeArg.EqualsLiteral("mouseout") ||
963 aEventTypeArg.EqualsLiteral("mousemove") ||
964 aEventTypeArg.EqualsLiteral("contextmenu") ||
965 // pointer events
966 aEventTypeArg.EqualsLiteral("pointerdown") ||
967 aEventTypeArg.EqualsLiteral("pointermove") ||
968 aEventTypeArg.EqualsLiteral("pointerup") ||
969 aEventTypeArg.EqualsLiteral("pointercancel") ||
970 aEventTypeArg.EqualsLiteral("pointerover") ||
971 aEventTypeArg.EqualsLiteral("pointerout") ||
972 aEventTypeArg.EqualsLiteral("pointerenter") ||
973 aEventTypeArg.EqualsLiteral("pointerleave") ||
974 aEventTypeArg.EqualsLiteral("gotpointercapture") ||
975 aEventTypeArg.EqualsLiteral("lostpointercapture") ||
976 // touch events
977 aEventTypeArg.EqualsLiteral("touchstart") ||
978 aEventTypeArg.EqualsLiteral("touchend") ||
979 aEventTypeArg.EqualsLiteral("touchmove") ||
980 aEventTypeArg.EqualsLiteral("touchcancel") ||
981 // UI legacy events
982 aEventTypeArg.EqualsLiteral("DOMFocusIn") ||
983 aEventTypeArg.EqualsLiteral("DOMFocusOut") ||
984 aEventTypeArg.EqualsLiteral("DOMActivate") ||
985 // wheel events
986 aEventTypeArg.EqualsLiteral("wheel");
989 void SetComposed(bool aComposed) { mFlags.mComposed = aComposed; }
991 void SetDefaultComposedInNativeAnonymousContent() {
992 // For compatibility concerns, we set mComposedInNativeAnonymousContent to
993 // false for those events we want to stop propagation.
995 // nsVideoFrame may create anonymous image element which fires eLoad,
996 // eLoadStart, eLoadEnd, eLoadError. We don't want these events cross
997 // the boundary of NAC
998 mFlags.mComposedInNativeAnonymousContent =
999 mMessage != eLoad && mMessage != eLoadStart && mMessage != eLoadEnd &&
1000 mMessage != eLoadError;
1003 bool IsUserAction() const;
1006 /******************************************************************************
1007 * mozilla::WidgetGUIEvent
1008 ******************************************************************************/
1010 class WidgetGUIEvent : public WidgetEvent {
1011 protected:
1012 WidgetGUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1013 EventClassID aEventClassID,
1014 const WidgetEventTime* aTime = nullptr)
1015 : WidgetEvent(aIsTrusted, aMessage, aEventClassID, aTime),
1016 mWidget(aWidget) {}
1018 WidgetGUIEvent() = default;
1020 public:
1021 virtual WidgetGUIEvent* AsGUIEvent() override { return this; }
1023 WidgetGUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1024 const WidgetEventTime* aTime = nullptr)
1025 : WidgetEvent(aIsTrusted, aMessage, eGUIEventClass, aTime),
1026 mWidget(aWidget) {}
1028 virtual WidgetEvent* Duplicate() const override {
1029 MOZ_ASSERT(mClass == eGUIEventClass,
1030 "Duplicate() must be overridden by sub class");
1031 // Not copying widget, it is a weak reference.
1032 WidgetGUIEvent* result = new WidgetGUIEvent(false, mMessage, nullptr, this);
1033 result->AssignGUIEventData(*this, true);
1034 result->mFlags = mFlags;
1035 return result;
1038 // Originator of the event
1039 nsCOMPtr<nsIWidget> mWidget;
1041 void AssignGUIEventData(const WidgetGUIEvent& aEvent, bool aCopyTargets) {
1042 AssignEventData(aEvent, aCopyTargets);
1043 // widget should be initialized with the constructor.
1047 /******************************************************************************
1048 * mozilla::Modifier
1050 * All modifier keys should be defined here. This is used for managing
1051 * modifier states for DOM Level 3 or later.
1052 ******************************************************************************/
1054 enum Modifier {
1055 MODIFIER_NONE = 0x0000,
1056 MODIFIER_ALT = 0x0001,
1057 MODIFIER_ALTGRAPH = 0x0002,
1058 MODIFIER_CAPSLOCK = 0x0004,
1059 MODIFIER_CONTROL = 0x0008,
1060 MODIFIER_FN = 0x0010,
1061 MODIFIER_FNLOCK = 0x0020,
1062 MODIFIER_META = 0x0040,
1063 MODIFIER_NUMLOCK = 0x0080,
1064 MODIFIER_SCROLLLOCK = 0x0100,
1065 MODIFIER_SHIFT = 0x0200,
1066 MODIFIER_SYMBOL = 0x0400,
1067 MODIFIER_SYMBOLLOCK = 0x0800,
1070 /******************************************************************************
1071 * Modifier key names.
1072 ******************************************************************************/
1074 #define NS_DOM_KEYNAME_ALT "Alt"
1075 #define NS_DOM_KEYNAME_ALTGRAPH "AltGraph"
1076 #define NS_DOM_KEYNAME_CAPSLOCK "CapsLock"
1077 #define NS_DOM_KEYNAME_CONTROL "Control"
1078 #define NS_DOM_KEYNAME_FN "Fn"
1079 #define NS_DOM_KEYNAME_FNLOCK "FnLock"
1080 #define NS_DOM_KEYNAME_META "Meta"
1081 #define NS_DOM_KEYNAME_NUMLOCK "NumLock"
1082 #define NS_DOM_KEYNAME_SCROLLLOCK "ScrollLock"
1083 #define NS_DOM_KEYNAME_SHIFT "Shift"
1084 #define NS_DOM_KEYNAME_SYMBOL "Symbol"
1085 #define NS_DOM_KEYNAME_SYMBOLLOCK "SymbolLock"
1086 #define NS_DOM_KEYNAME_OS "OS"
1088 /******************************************************************************
1089 * mozilla::Modifiers
1090 ******************************************************************************/
1092 typedef uint16_t Modifiers;
1094 class MOZ_STACK_CLASS GetModifiersName final : public nsAutoCString {
1095 public:
1096 explicit GetModifiersName(Modifiers aModifiers) {
1097 if (aModifiers & MODIFIER_ALT) {
1098 AssignLiteral(NS_DOM_KEYNAME_ALT);
1100 if (aModifiers & MODIFIER_ALTGRAPH) {
1101 MaybeAppendSeparator();
1102 AppendLiteral(NS_DOM_KEYNAME_ALTGRAPH);
1104 if (aModifiers & MODIFIER_CAPSLOCK) {
1105 MaybeAppendSeparator();
1106 AppendLiteral(NS_DOM_KEYNAME_CAPSLOCK);
1108 if (aModifiers & MODIFIER_CONTROL) {
1109 MaybeAppendSeparator();
1110 AppendLiteral(NS_DOM_KEYNAME_CONTROL);
1112 if (aModifiers & MODIFIER_FN) {
1113 MaybeAppendSeparator();
1114 AppendLiteral(NS_DOM_KEYNAME_FN);
1116 if (aModifiers & MODIFIER_FNLOCK) {
1117 MaybeAppendSeparator();
1118 AppendLiteral(NS_DOM_KEYNAME_FNLOCK);
1120 if (aModifiers & MODIFIER_META) {
1121 MaybeAppendSeparator();
1122 AppendLiteral(NS_DOM_KEYNAME_META);
1124 if (aModifiers & MODIFIER_NUMLOCK) {
1125 MaybeAppendSeparator();
1126 AppendLiteral(NS_DOM_KEYNAME_NUMLOCK);
1128 if (aModifiers & MODIFIER_SCROLLLOCK) {
1129 MaybeAppendSeparator();
1130 AppendLiteral(NS_DOM_KEYNAME_SCROLLLOCK);
1132 if (aModifiers & MODIFIER_SHIFT) {
1133 MaybeAppendSeparator();
1134 AppendLiteral(NS_DOM_KEYNAME_SHIFT);
1136 if (aModifiers & MODIFIER_SYMBOL) {
1137 MaybeAppendSeparator();
1138 AppendLiteral(NS_DOM_KEYNAME_SYMBOL);
1140 if (aModifiers & MODIFIER_SYMBOLLOCK) {
1141 MaybeAppendSeparator();
1142 AppendLiteral(NS_DOM_KEYNAME_SYMBOLLOCK);
1144 if (IsEmpty()) {
1145 AssignLiteral("none");
1149 private:
1150 void MaybeAppendSeparator() {
1151 if (!IsEmpty()) {
1152 AppendLiteral(" | ");
1157 /******************************************************************************
1158 * mozilla::WidgetInputEvent
1159 ******************************************************************************/
1161 class WidgetInputEvent : public WidgetGUIEvent {
1162 protected:
1163 WidgetInputEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1164 EventClassID aEventClassID,
1165 const WidgetEventTime* aTime = nullptr)
1166 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, aEventClassID, aTime),
1167 mModifiers(0) {}
1169 WidgetInputEvent() : mModifiers(0) {}
1171 public:
1172 virtual WidgetInputEvent* AsInputEvent() override { return this; }
1174 WidgetInputEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1175 const WidgetEventTime* aTime = nullptr)
1176 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eInputEventClass, aTime),
1177 mModifiers(0) {}
1179 virtual WidgetEvent* Duplicate() const override {
1180 MOZ_ASSERT(mClass == eInputEventClass,
1181 "Duplicate() must be overridden by sub class");
1182 // Not copying widget, it is a weak reference.
1183 WidgetInputEvent* result =
1184 new WidgetInputEvent(false, mMessage, nullptr, this);
1185 result->AssignInputEventData(*this, true);
1186 result->mFlags = mFlags;
1187 return result;
1191 * Returns a modifier of "Accel" virtual modifier which is used for shortcut
1192 * key.
1194 static Modifier AccelModifier();
1197 * GetModifier() returns a modifier flag which is activated by aDOMKeyName.
1199 static Modifier GetModifier(const nsAString& aDOMKeyName);
1201 // true indicates the accel key on the environment is down
1202 bool IsAccel() const { return ((mModifiers & AccelModifier()) != 0); }
1204 // true indicates the shift key is down
1205 bool IsShift() const { return ((mModifiers & MODIFIER_SHIFT) != 0); }
1206 // true indicates the control key is down
1207 bool IsControl() const { return ((mModifiers & MODIFIER_CONTROL) != 0); }
1208 // true indicates the alt key is down
1209 bool IsAlt() const { return ((mModifiers & MODIFIER_ALT) != 0); }
1210 // true indicates the meta key is down (Command key on macOS, Windows logo key
1211 // on Windows, Super/Hyper key on Linux, Meta key on Android).
1212 bool IsMeta() const { return ((mModifiers & MODIFIER_META) != 0); }
1213 // true indicates the alt graph key is down
1214 // NOTE: on Mac, the option key press causes both IsAlt() and IsAltGrpah()
1215 // return true.
1216 bool IsAltGraph() const { return ((mModifiers & MODIFIER_ALTGRAPH) != 0); }
1217 // true indicates the CapLock LED is turn on.
1218 bool IsCapsLocked() const { return ((mModifiers & MODIFIER_CAPSLOCK) != 0); }
1219 // true indicates the NumLock LED is turn on.
1220 bool IsNumLocked() const { return ((mModifiers & MODIFIER_NUMLOCK) != 0); }
1221 // true indicates the ScrollLock LED is turn on.
1222 bool IsScrollLocked() const {
1223 return ((mModifiers & MODIFIER_SCROLLLOCK) != 0);
1226 // true indicates the Fn key is down, but this is not supported by native
1227 // key event on any platform.
1228 bool IsFn() const { return ((mModifiers & MODIFIER_FN) != 0); }
1229 // true indicates the FnLock LED is turn on, but we don't know such
1230 // keyboards nor platforms.
1231 bool IsFnLocked() const { return ((mModifiers & MODIFIER_FNLOCK) != 0); }
1232 // true indicates the Symbol is down, but this is not supported by native
1233 // key event on any platforms.
1234 bool IsSymbol() const { return ((mModifiers & MODIFIER_SYMBOL) != 0); }
1235 // true indicates the SymbolLock LED is turn on, but we don't know such
1236 // keyboards nor platforms.
1237 bool IsSymbolLocked() const {
1238 return ((mModifiers & MODIFIER_SYMBOLLOCK) != 0);
1241 void InitBasicModifiers(bool aCtrlKey, bool aAltKey, bool aShiftKey,
1242 bool aMetaKey) {
1243 mModifiers = 0;
1244 if (aCtrlKey) {
1245 mModifiers |= MODIFIER_CONTROL;
1247 if (aAltKey) {
1248 mModifiers |= MODIFIER_ALT;
1250 if (aShiftKey) {
1251 mModifiers |= MODIFIER_SHIFT;
1253 if (aMetaKey) {
1254 mModifiers |= MODIFIER_META;
1258 Modifiers mModifiers;
1260 void AssignInputEventData(const WidgetInputEvent& aEvent, bool aCopyTargets) {
1261 AssignGUIEventData(aEvent, aCopyTargets);
1263 mModifiers = aEvent.mModifiers;
1267 /******************************************************************************
1268 * mozilla::InternalUIEvent
1270 * XXX Why this inherits WidgetGUIEvent rather than WidgetEvent?
1271 ******************************************************************************/
1273 class InternalUIEvent : public WidgetGUIEvent {
1274 protected:
1275 InternalUIEvent() : mDetail(0), mCausedByUntrustedEvent(false) {}
1277 InternalUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1278 EventClassID aEventClassID,
1279 const WidgetEventTime* aTime = nullptr)
1280 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, aEventClassID, aTime),
1281 mDetail(0),
1282 mCausedByUntrustedEvent(false) {}
1284 InternalUIEvent(bool aIsTrusted, EventMessage aMessage,
1285 EventClassID aEventClassID,
1286 const WidgetEventTime* aTime = nullptr)
1287 : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, aEventClassID, aTime),
1288 mDetail(0),
1289 mCausedByUntrustedEvent(false) {}
1291 public:
1292 virtual InternalUIEvent* AsUIEvent() override { return this; }
1295 * If the UIEvent is caused by another event (e.g., click event),
1296 * aEventCausesThisEvent should be the event. If there is no such event,
1297 * this should be nullptr.
1299 InternalUIEvent(bool aIsTrusted, EventMessage aMessage,
1300 const WidgetEvent* aEventCausesThisEvent,
1301 const WidgetEventTime* aTime = nullptr)
1302 : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, eUIEventClass, aTime),
1303 mDetail(0),
1304 mCausedByUntrustedEvent(aEventCausesThisEvent &&
1305 !aEventCausesThisEvent->IsTrusted()) {}
1307 virtual WidgetEvent* Duplicate() const override {
1308 MOZ_ASSERT(mClass == eUIEventClass,
1309 "Duplicate() must be overridden by sub class");
1310 InternalUIEvent* result =
1311 new InternalUIEvent(false, mMessage, nullptr, this);
1312 result->AssignUIEventData(*this, true);
1313 result->mFlags = mFlags;
1314 return result;
1317 int32_t mDetail;
1318 // mCausedByUntrustedEvent is true if the event is caused by untrusted event.
1319 bool mCausedByUntrustedEvent;
1321 // If you check the event is a trusted event and NOT caused by an untrusted
1322 // event, IsTrustable() returns what you expected.
1323 bool IsTrustable() const { return IsTrusted() && !mCausedByUntrustedEvent; }
1325 void AssignUIEventData(const InternalUIEvent& aEvent, bool aCopyTargets) {
1326 AssignGUIEventData(aEvent, aCopyTargets);
1328 mDetail = aEvent.mDetail;
1329 mCausedByUntrustedEvent = aEvent.mCausedByUntrustedEvent;
1333 } // namespace mozilla
1335 #endif // mozilla_BasicEvents_h__