Bug 1770047 [wpt PR 34117] - Update wpt metadata, a=testonly
[gecko.git] / widget / BasicEvents.h
blobbc4a34bbfc6ada7bc334d300aaf67652a0aa1a6d
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 mInSystemGroup is true, the event is being dispatched in system group.
68 bool mInSystemGroup : 1;
69 // If mCancelable is true, the event can be consumed. I.e., calling
70 // dom::Event::PreventDefault() can prevent the default action.
71 bool mCancelable : 1;
72 // If mBubbles is true, the event can bubble. Otherwise, cannot be handled
73 // in bubbling phase.
74 bool mBubbles : 1;
75 // If mPropagationStopped is true, dom::Event::StopPropagation() or
76 // dom::Event::StopImmediatePropagation() has been called.
77 bool mPropagationStopped : 1;
78 // If mImmediatePropagationStopped is true,
79 // dom::Event::StopImmediatePropagation() has been called.
80 // Note that mPropagationStopped must be true when this is true.
81 bool mImmediatePropagationStopped : 1;
82 // If mDefaultPrevented is true, the event has been consumed.
83 // E.g., dom::Event::PreventDefault() has been called or
84 // the default action has been performed.
85 bool mDefaultPrevented : 1;
86 // If mDefaultPreventedByContent is true, the event has been
87 // consumed by content.
88 // Note that mDefaultPrevented must be true when this is true.
89 bool mDefaultPreventedByContent : 1;
90 // If mDefaultPreventedByChrome is true, the event has been
91 // consumed by chrome.
92 // Note that mDefaultPrevented must be true when this is true.
93 bool mDefaultPreventedByChrome : 1;
94 // mMultipleActionsPrevented may be used when default handling don't want to
95 // be prevented, but only one of the event targets should handle the event.
96 // For example, when a <label> element is in another <label> element and
97 // the first <label> element is clicked, that one may set this true.
98 // Then, the second <label> element won't handle the event.
99 bool mMultipleActionsPrevented : 1;
100 // Similar to above but expected to be used during PreHandleEvent phase.
101 bool mMultiplePreActionsPrevented : 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;
185 // At lease one of the event in the event path had non privileged click
186 // listener.
187 bool mHadNonPrivilegedClickListeners : 1;
189 // If the event is being handled in target phase, returns true.
190 inline bool InTargetPhase() const {
191 return (mInBubblingPhase && mInCapturePhase);
195 * Helper methods for methods of DOM Event.
197 inline void StopPropagation() { mPropagationStopped = true; }
198 inline void StopImmediatePropagation() {
199 StopPropagation();
200 mImmediatePropagationStopped = true;
202 inline void PreventDefault(bool aCalledByDefaultHandler = true) {
203 if (!mCancelable) {
204 return;
206 mDefaultPrevented = true;
207 // Note that even if preventDefault() has already been called by chrome,
208 // a call of preventDefault() by content needs to overwrite
209 // mDefaultPreventedByContent to true because in such case, defaultPrevented
210 // must be true when web apps check it after they call preventDefault().
211 if (aCalledByDefaultHandler) {
212 StopCrossProcessForwarding();
213 mDefaultPreventedByChrome = true;
214 } else {
215 mDefaultPreventedByContent = true;
218 // This should be used only before dispatching events into the DOM tree.
219 inline void PreventDefaultBeforeDispatch(
220 CrossProcessForwarding aCrossProcessForwarding) {
221 if (!mCancelable) {
222 return;
224 mDefaultPrevented = true;
225 if (aCrossProcessForwarding == CrossProcessForwarding::eStop) {
226 StopCrossProcessForwarding();
229 inline bool DefaultPrevented() const { return mDefaultPrevented; }
230 inline bool DefaultPreventedByContent() const {
231 MOZ_ASSERT(!mDefaultPreventedByContent || DefaultPrevented());
232 return mDefaultPreventedByContent;
234 inline bool IsTrusted() const { return mIsTrusted; }
235 inline bool PropagationStopped() const { return mPropagationStopped; }
237 // Helper methods to access flags managing state of propagation between
238 // processes.
241 * Prevent to be dispatched to remote process.
243 inline void StopCrossProcessForwarding() {
244 MOZ_ASSERT(!mPostedToRemoteProcess);
245 mNoRemoteProcessDispatch = true;
246 mWantReplyFromContentProcess = false;
249 * Return true if the event shouldn't be dispatched to remote process.
251 inline bool IsCrossProcessForwardingStopped() const {
252 return mNoRemoteProcessDispatch;
255 * Mark the event as waiting reply from remote process.
256 * If the caller needs to win other keyboard event handlers in chrome,
257 * the caller should call StopPropagation() too.
258 * Otherwise, if the caller just needs to know if the event is consumed by
259 * either content or chrome, it should just call this because the event
260 * may be reserved by chrome and it needs to be dispatched into the DOM
261 * tree in chrome for checking if it's reserved before being sent to any
262 * remote processes.
264 inline void MarkAsWaitingReplyFromRemoteProcess() {
265 MOZ_ASSERT(!mPostedToRemoteProcess);
266 mNoRemoteProcessDispatch = false;
267 mWantReplyFromContentProcess = true;
270 * Reset "waiting reply from remote process" state. This is useful when
271 * you dispatch a copy of an event coming from different process.
273 inline void ResetWaitingReplyFromRemoteProcessState() {
274 if (IsWaitingReplyFromRemoteProcess()) {
275 // FYI: mWantReplyFromContentProcess is also used for indicating
276 // "handled in remote process" state. Therefore, only when
277 // IsWaitingReplyFromRemoteProcess() returns true, this should
278 // reset the flag.
279 mWantReplyFromContentProcess = false;
283 * Return true if the event handler should wait reply event. I.e., if this
284 * returns true, any event handler should do nothing with the event.
286 inline bool IsWaitingReplyFromRemoteProcess() const {
287 return !mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
290 * Mark the event as already handled in the remote process. This should be
291 * called when initializing reply events.
293 inline void MarkAsHandledInRemoteProcess() {
294 mNoRemoteProcessDispatch = true;
295 mWantReplyFromContentProcess = true;
296 mPostedToRemoteProcess = false;
299 * Return true if the event has already been handled in the remote process.
301 inline bool IsHandledInRemoteProcess() const {
302 return mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
305 * Return true if the event should be sent back to its parent process.
307 inline bool WantReplyFromContentProcess() const {
308 MOZ_ASSERT(!XRE_IsParentProcess());
309 return IsWaitingReplyFromRemoteProcess();
312 * Mark the event has already posted to a remote process.
314 inline void MarkAsPostedToRemoteProcess() {
315 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
316 mPostedToRemoteProcess = true;
319 * Reset the cross process dispatching state. This should be used when a
320 * process receives the event because the state is in the sender.
322 inline void ResetCrossProcessDispatchingState() {
323 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
324 mPostedToRemoteProcess = false;
325 // Ignore propagation state in the remote process if it's marked as
326 // "waiting reply from remote process" because the process needs to
327 // stop propagation in the process until receiving a reply event.
328 // Note that the propagation stopped flag is important for the reply event
329 // handler in the main process because it's used for making whether it's
330 // ignored by the remote process or not.
331 if (!XRE_IsParentProcess() && IsWaitingReplyFromRemoteProcess()) {
332 mPropagationStopped = mImmediatePropagationStopped = false;
334 // mDispatchedAtLeastOnce indicates the state in current process.
335 mDispatchedAtLeastOnce = false;
338 * Return true if the event has been posted to a remote process.
339 * Note that MarkAsPostedToRemoteProcess() is called by
340 * ParamTraits<mozilla::WidgetEvent>. Therefore, it *might* be possible
341 * that posting the event failed even if this returns true. But that must
342 * really rare. If that'd be problem for you, you should unmark this in
343 * BrowserParent or somewhere.
345 inline bool HasBeenPostedToRemoteProcess() const {
346 return mPostedToRemoteProcess;
349 * Return true if the event came from another process.
351 inline bool CameFromAnotherProcess() const { return mCameFromAnotherProcess; }
353 * Mark the event as coming from another process.
355 inline void MarkAsComingFromAnotherProcess() {
356 mCameFromAnotherProcess = true;
359 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
360 * content because it shouldn't be cancelable.
362 inline void MarkAsReservedByChrome() {
363 MOZ_ASSERT(!mPostedToRemoteProcess);
364 mIsReservedByChrome = true;
365 // For reserved commands (such as Open New Tab), we don't need to wait for
366 // the content to answer, neither to give a chance for content to override
367 // its behavior.
368 StopCrossProcessForwarding();
369 // If the event is reserved by chrome, we shouldn't expose the event to
370 // web contents because such events shouldn't be cancelable. So, it's not
371 // good behavior to fire such events but to ignore the defaultPrevented
372 // attribute value in chrome.
373 mOnlySystemGroupDispatchInContent = true;
376 * Return true if the event is reserved by chrome.
378 inline bool IsReservedByChrome() const {
379 MOZ_ASSERT(!mIsReservedByChrome || (IsCrossProcessForwardingStopped() &&
380 mOnlySystemGroupDispatchInContent));
381 return mIsReservedByChrome;
384 inline void Clear() { SetRawFlags(0); }
385 // Get if either the instance's bit or the aOther's bit is true, the
386 // instance's bit becomes true. In other words, this works like:
387 // eventFlags |= aOther;
388 inline void Union(const BaseEventFlags& aOther) {
389 RawFlags rawFlags = GetRawFlags() | aOther.GetRawFlags();
390 SetRawFlags(rawFlags);
393 private:
394 typedef uint64_t RawFlags;
396 inline void SetRawFlags(RawFlags aRawFlags) {
397 static_assert(sizeof(BaseEventFlags) <= sizeof(RawFlags),
398 "mozilla::EventFlags must not be bigger than the RawFlags");
399 memcpy(this, &aRawFlags, sizeof(BaseEventFlags));
401 inline RawFlags GetRawFlags() const {
402 RawFlags result = 0;
403 memcpy(&result, this, sizeof(BaseEventFlags));
404 return result;
408 /******************************************************************************
409 * mozilla::EventFlags
410 ******************************************************************************/
412 struct EventFlags : public BaseEventFlags {
413 EventFlags() { Clear(); }
416 /******************************************************************************
417 * mozilla::WidgetEventTime
418 ******************************************************************************/
420 class WidgetEventTime {
421 public:
422 // Elapsed time, in milliseconds, from a platform-specific zero time
423 // to the time the message was created
424 uint64_t mTime;
425 // Timestamp when the message was created. Set in parallel to 'time' until we
426 // determine if it is safe to drop 'time' (see bug 77992).
427 TimeStamp mTimeStamp;
429 WidgetEventTime() : mTime(0), mTimeStamp(TimeStamp::Now()) {}
431 WidgetEventTime(uint64_t aTime, TimeStamp aTimeStamp)
432 : mTime(aTime), mTimeStamp(aTimeStamp) {}
434 void AssignEventTime(const WidgetEventTime& aOther) {
435 mTime = aOther.mTime;
436 mTimeStamp = aOther.mTimeStamp;
440 /******************************************************************************
441 * mozilla::WidgetEvent
442 ******************************************************************************/
444 class WidgetEvent : public WidgetEventTime {
445 private:
446 void SetDefaultCancelableAndBubbles() {
447 switch (mClass) {
448 case eEditorInputEventClass:
449 mFlags.mCancelable = false;
450 mFlags.mBubbles = mFlags.mIsTrusted;
451 break;
452 case eMouseEventClass:
453 mFlags.mCancelable =
454 (mMessage != eMouseEnter && mMessage != eMouseLeave);
455 mFlags.mBubbles = (mMessage != eMouseEnter && mMessage != eMouseLeave);
456 break;
457 case ePointerEventClass:
458 mFlags.mCancelable =
459 (mMessage != ePointerEnter && mMessage != ePointerLeave &&
460 mMessage != ePointerCancel && mMessage != ePointerGotCapture &&
461 mMessage != ePointerLostCapture);
462 mFlags.mBubbles =
463 (mMessage != ePointerEnter && mMessage != ePointerLeave);
464 break;
465 case eDragEventClass:
466 mFlags.mCancelable = (mMessage != eDragExit && mMessage != eDragLeave &&
467 mMessage != eDragEnd);
468 mFlags.mBubbles = true;
469 break;
470 case eSMILTimeEventClass:
471 mFlags.mCancelable = false;
472 mFlags.mBubbles = false;
473 break;
474 case eTransitionEventClass:
475 case eAnimationEventClass:
476 mFlags.mCancelable = false;
477 mFlags.mBubbles = true;
478 break;
479 case eCompositionEventClass:
480 // XXX compositionstart is cancelable in draft of DOM3 Events.
481 // However, it doesn't make sense for us, we cannot cancel
482 // composition when we send compositionstart event.
483 mFlags.mCancelable = false;
484 mFlags.mBubbles = true;
485 break;
486 default:
487 if (mMessage == eResize || mMessage == eMozVisualResize ||
488 mMessage == eMozVisualScroll || mMessage == eEditorInput ||
489 mMessage == eFormSelect) {
490 mFlags.mCancelable = false;
491 } else {
492 mFlags.mCancelable = true;
494 mFlags.mBubbles = true;
495 break;
499 protected:
500 WidgetEvent(bool aIsTrusted, EventMessage aMessage,
501 EventClassID aEventClassID)
502 : WidgetEventTime(),
503 mClass(aEventClassID),
504 mMessage(aMessage),
505 mRefPoint(0, 0),
506 mLastRefPoint(0, 0),
507 mFocusSequenceNumber(0),
508 mSpecifiedEventType(nullptr),
509 mPath(nullptr),
510 mLayersId(layers::LayersId{0}) {
511 MOZ_COUNT_CTOR(WidgetEvent);
512 mFlags.Clear();
513 mFlags.mIsTrusted = aIsTrusted;
514 SetDefaultCancelableAndBubbles();
515 SetDefaultComposed();
516 SetDefaultComposedInNativeAnonymousContent();
519 WidgetEvent() : WidgetEventTime(), mPath(nullptr) {
520 MOZ_COUNT_CTOR(WidgetEvent);
523 public:
524 WidgetEvent(bool aIsTrusted, EventMessage aMessage)
525 : WidgetEvent(aIsTrusted, aMessage, eBasicEventClass) {}
527 MOZ_COUNTED_DTOR_VIRTUAL(WidgetEvent)
529 WidgetEvent(const WidgetEvent& aOther) : WidgetEventTime() {
530 MOZ_COUNT_CTOR(WidgetEvent);
531 *this = aOther;
533 WidgetEvent& operator=(const WidgetEvent& aOther) = default;
535 WidgetEvent(WidgetEvent&& aOther)
536 : WidgetEventTime(std::move(aOther)),
537 mClass(aOther.mClass),
538 mMessage(aOther.mMessage),
539 mRefPoint(std::move(aOther.mRefPoint)),
540 mLastRefPoint(std::move(aOther.mLastRefPoint)),
541 mFocusSequenceNumber(aOther.mFocusSequenceNumber),
542 mFlags(std::move(aOther.mFlags)),
543 mSpecifiedEventType(std::move(aOther.mSpecifiedEventType)),
544 mSpecifiedEventTypeString(std::move(aOther.mSpecifiedEventTypeString)),
545 mTarget(std::move(aOther.mTarget)),
546 mCurrentTarget(std::move(aOther.mCurrentTarget)),
547 mOriginalTarget(std::move(aOther.mOriginalTarget)),
548 mRelatedTarget(std::move(aOther.mRelatedTarget)),
549 mOriginalRelatedTarget(std::move(aOther.mOriginalRelatedTarget)),
550 mPath(std::move(aOther.mPath)) {
551 MOZ_COUNT_CTOR(WidgetEvent);
553 WidgetEvent& operator=(WidgetEvent&& aOther) = default;
555 virtual WidgetEvent* Duplicate() const {
556 MOZ_ASSERT(mClass == eBasicEventClass,
557 "Duplicate() must be overridden by sub class");
558 WidgetEvent* result = new WidgetEvent(false, mMessage);
559 result->AssignEventData(*this, true);
560 result->mFlags = mFlags;
561 return result;
564 EventClassID mClass;
565 EventMessage mMessage;
566 // Relative to the widget of the event, or if there is no widget then it is
567 // in screen coordinates. Not modified by layout code.
568 // This is in visual coordinates, i.e. the correct RelativeTo value that
569 // expresses what this is relative to is `{viewportFrame, Visual}`, where
570 // `viewportFrame` is the viewport frame of the widget's root document.
571 LayoutDeviceIntPoint mRefPoint;
572 // The previous mRefPoint, if known, used to calculate mouse movement deltas.
573 LayoutDeviceIntPoint mLastRefPoint;
574 // The sequence number of the last potentially focus changing event handled
575 // by APZ. This is used to track when that event has been processed by
576 // content, and focus can be reconfirmed for async keyboard scrolling.
577 uint64_t mFocusSequenceNumber;
578 // See BaseEventFlags definition for the detail.
579 BaseEventFlags mFlags;
581 // If JS creates an event with unknown event type or known event type but
582 // for different event interface, the event type is stored to this.
583 // NOTE: This is always used if the instance is a WidgetCommandEvent instance
584 // or "input" event is dispatched with dom::Event class.
585 RefPtr<nsAtom> mSpecifiedEventType;
587 // nsAtom isn't available on non-main thread due to unsafe. Therefore,
588 // mSpecifiedEventTypeString is used instead of mSpecifiedEventType if
589 // the event is created in non-main thread.
590 nsString mSpecifiedEventTypeString;
592 // Event targets, needed by DOM Events
593 // Note that when you need event target for DOM event, you should use
594 // Get*DOMEventTarget() instead of accessing these members directly.
595 nsCOMPtr<dom::EventTarget> mTarget;
596 nsCOMPtr<dom::EventTarget> mCurrentTarget;
597 nsCOMPtr<dom::EventTarget> mOriginalTarget;
599 /// The possible related target
600 nsCOMPtr<dom::EventTarget> mRelatedTarget;
601 nsCOMPtr<dom::EventTarget> mOriginalRelatedTarget;
603 nsTArray<EventTargetChainItem>* mPath;
605 // The LayersId of the content process that this event should be
606 // dispatched to. This field is only used in the chrome process
607 // and doesn't get remoted to child processes.
608 layers::LayersId mLayersId;
610 dom::EventTarget* GetDOMEventTarget() const;
611 dom::EventTarget* GetCurrentDOMEventTarget() const;
612 dom::EventTarget* GetOriginalDOMEventTarget() const;
614 void AssignEventData(const WidgetEvent& aEvent, bool aCopyTargets) {
615 // mClass should be initialized with the constructor.
616 // mMessage should be initialized with the constructor.
617 mRefPoint = aEvent.mRefPoint;
618 // mLastRefPoint doesn't need to be copied.
619 mFocusSequenceNumber = aEvent.mFocusSequenceNumber;
620 // mLayersId intentionally not copied, since it's not used within content
621 AssignEventTime(aEvent);
622 // mFlags should be copied manually if it's necessary.
623 mSpecifiedEventType = aEvent.mSpecifiedEventType;
624 // mSpecifiedEventTypeString should be copied manually if it's necessary.
625 mTarget = aCopyTargets ? aEvent.mTarget : nullptr;
626 mCurrentTarget = aCopyTargets ? aEvent.mCurrentTarget : nullptr;
627 mOriginalTarget = aCopyTargets ? aEvent.mOriginalTarget : nullptr;
628 mRelatedTarget = aCopyTargets ? aEvent.mRelatedTarget : nullptr;
629 mOriginalRelatedTarget =
630 aCopyTargets ? aEvent.mOriginalRelatedTarget : nullptr;
634 * Helper methods for methods of DOM Event.
636 void StopPropagation() { mFlags.StopPropagation(); }
637 void StopImmediatePropagation() { mFlags.StopImmediatePropagation(); }
638 void PreventDefault(bool aCalledByDefaultHandler = true,
639 nsIPrincipal* aPrincipal = nullptr);
641 void PreventDefaultBeforeDispatch(
642 CrossProcessForwarding aCrossProcessForwarding) {
643 mFlags.PreventDefaultBeforeDispatch(aCrossProcessForwarding);
645 bool DefaultPrevented() const { return mFlags.DefaultPrevented(); }
646 bool DefaultPreventedByContent() const {
647 return mFlags.DefaultPreventedByContent();
649 bool IsTrusted() const { return mFlags.IsTrusted(); }
650 bool PropagationStopped() const { return mFlags.PropagationStopped(); }
653 * Prevent to be dispatched to remote process.
655 inline void StopCrossProcessForwarding() {
656 mFlags.StopCrossProcessForwarding();
659 * Return true if the event shouldn't be dispatched to remote process.
661 inline bool IsCrossProcessForwardingStopped() const {
662 return mFlags.IsCrossProcessForwardingStopped();
665 * Mark the event as waiting reply from remote process.
666 * Note that this also stops immediate propagation in current process.
668 inline void MarkAsWaitingReplyFromRemoteProcess() {
669 mFlags.MarkAsWaitingReplyFromRemoteProcess();
672 * Reset "waiting reply from remote process" state. This is useful when
673 * you dispatch a copy of an event coming from different process.
675 inline void ResetWaitingReplyFromRemoteProcessState() {
676 mFlags.ResetWaitingReplyFromRemoteProcessState();
679 * Return true if the event handler should wait reply event. I.e., if this
680 * returns true, any event handler should do nothing with the event.
682 inline bool IsWaitingReplyFromRemoteProcess() const {
683 return mFlags.IsWaitingReplyFromRemoteProcess();
686 * Mark the event as already handled in the remote process. This should be
687 * called when initializing reply events.
689 inline void MarkAsHandledInRemoteProcess() {
690 mFlags.MarkAsHandledInRemoteProcess();
693 * Return true if the event has already been handled in the remote process.
694 * I.e., if this returns true, the event is a reply event.
696 inline bool IsHandledInRemoteProcess() const {
697 return mFlags.IsHandledInRemoteProcess();
700 * Return true if the event should be sent back to its parent process.
701 * So, usual event handlers shouldn't call this.
703 inline bool WantReplyFromContentProcess() const {
704 return mFlags.WantReplyFromContentProcess();
707 * Mark the event has already posted to a remote process.
709 inline void MarkAsPostedToRemoteProcess() {
710 mFlags.MarkAsPostedToRemoteProcess();
713 * Reset the cross process dispatching state. This should be used when a
714 * process receives the event because the state is in the sender.
716 inline void ResetCrossProcessDispatchingState() {
717 mFlags.ResetCrossProcessDispatchingState();
720 * Return true if the event has been posted to a remote process.
722 inline bool HasBeenPostedToRemoteProcess() const {
723 return mFlags.HasBeenPostedToRemoteProcess();
726 * Return true if the event came from another process.
728 inline bool CameFromAnotherProcess() const {
729 return mFlags.CameFromAnotherProcess();
732 * Mark the event as coming from another process.
734 inline void MarkAsComingFromAnotherProcess() {
735 mFlags.MarkAsComingFromAnotherProcess();
738 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
739 * content because it shouldn't be cancelable.
741 inline void MarkAsReservedByChrome() { mFlags.MarkAsReservedByChrome(); }
743 * Return true if the event is reserved by chrome.
745 inline bool IsReservedByChrome() const { return mFlags.IsReservedByChrome(); }
748 * Utils for checking event types
752 * As*Event() returns the pointer of the instance only when the instance is
753 * the class or one of its derived class.
755 #define NS_ROOT_EVENT_CLASS(aPrefix, aName)
756 #define NS_EVENT_CLASS(aPrefix, aName) \
757 virtual aPrefix##aName* As##aName(); \
758 const aPrefix##aName* As##aName() const;
760 #include "mozilla/EventClassList.h"
762 #undef NS_EVENT_CLASS
763 #undef NS_ROOT_EVENT_CLASS
766 * Returns true if the event is a query content event.
768 bool IsQueryContentEvent() const;
770 * Returns true if the event is a selection event.
772 bool IsSelectionEvent() const;
774 * Returns true if the event is a content command event.
776 bool IsContentCommandEvent() const;
779 * Returns true if the event mMessage is one of mouse events.
781 bool HasMouseEventMessage() const;
783 * Returns true if the event mMessage is one of drag events.
785 bool HasDragEventMessage() const;
787 * Returns true if aMessage or mMessage is one of key events.
789 static bool IsKeyEventMessage(EventMessage aMessage);
790 bool HasKeyEventMessage() const { return IsKeyEventMessage(mMessage); }
792 * Returns true if the event mMessage is one of composition events or text
793 * event.
795 bool HasIMEEventMessage() const;
798 * Returns true if the event can be sent to remote process.
800 bool CanBeSentToRemoteProcess() const;
802 * Returns true if the original target is a remote process and the event
803 * will be posted to the remote process later.
805 bool WillBeSentToRemoteProcess() const;
807 * Returns true if the event is related to IME handling. It includes
808 * IME events, query content events and selection events.
809 * Be careful when you use this.
811 bool IsIMERelatedEvent() const;
814 * Whether the event should be handled by the frame of the mouse cursor
815 * position or not. When it should be handled there (e.g., the mouse events),
816 * this returns true.
818 bool IsUsingCoordinates() const;
820 * Whether the event should be handled by the focused DOM window in the
821 * same top level window's or not. E.g., key events, IME related events
822 * (including the query content events, they are used in IME transaction)
823 * should be handled by the (last) focused window rather than the dispatched
824 * window.
826 * NOTE: Even if this returns true, the event isn't going to be handled by the
827 * application level active DOM window which is on another top level window.
828 * So, when the event is fired on a deactive window, the event is going to be
829 * handled by the last focused DOM window in the last focused window.
831 bool IsTargetedAtFocusedWindow() const;
833 * Whether the event should be handled by the focused content or not. E.g.,
834 * key events, IME related events and other input events which are not handled
835 * by the frame of the mouse cursor position.
837 * NOTE: Even if this returns true, the event isn't going to be handled by the
838 * application level active DOM window which is on another top level window.
839 * So, when the event is fired on a deactive window, the event is going to be
840 * handled by the last focused DOM element of the last focused DOM window in
841 * the last focused window.
843 bool IsTargetedAtFocusedContent() const;
845 * Whether the event should cause a DOM event.
847 bool IsAllowedToDispatchDOMEvent() const;
849 * Whether the event should be dispatched in system group.
851 bool IsAllowedToDispatchInSystemGroup() const;
853 * Whether the event should be blocked for fingerprinting resistance.
855 bool IsBlockedForFingerprintingResistance() const;
857 * Initialize mComposed
859 void SetDefaultComposed() {
860 switch (mClass) {
861 case eClipboardEventClass:
862 mFlags.mComposed = true;
863 break;
864 case eCompositionEventClass:
865 mFlags.mComposed =
866 mMessage == eCompositionStart || mMessage == eCompositionUpdate ||
867 mMessage == eCompositionChange || mMessage == eCompositionEnd;
868 break;
869 case eDragEventClass:
870 // All drag & drop events are composed
871 mFlags.mComposed = mMessage == eDrag || mMessage == eDragEnd ||
872 mMessage == eDragEnter || mMessage == eDragExit ||
873 mMessage == eDragLeave || mMessage == eDragOver ||
874 mMessage == eDragStart || mMessage == eDrop;
875 break;
876 case eEditorInputEventClass:
877 mFlags.mComposed =
878 mMessage == eEditorInput || mMessage == eEditorBeforeInput;
879 break;
880 case eFocusEventClass:
881 mFlags.mComposed = mMessage == eBlur || mMessage == eFocus ||
882 mMessage == eFocusOut || mMessage == eFocusIn;
883 break;
884 case eKeyboardEventClass:
885 mFlags.mComposed =
886 mMessage == eKeyDown || mMessage == eKeyUp || mMessage == eKeyPress;
887 break;
888 case eMouseEventClass:
889 mFlags.mComposed =
890 mMessage == eMouseClick || mMessage == eMouseDoubleClick ||
891 mMessage == eMouseAuxClick || mMessage == eMouseDown ||
892 mMessage == eMouseUp || mMessage == eMouseOver ||
893 mMessage == eMouseOut || mMessage == eMouseMove ||
894 mMessage == eContextMenu || mMessage == eXULPopupShowing ||
895 mMessage == eXULPopupHiding || mMessage == eXULPopupShown ||
896 mMessage == eXULPopupHidden;
897 break;
898 case ePointerEventClass:
899 // All pointer events are composed
900 mFlags.mComposed =
901 mMessage == ePointerDown || mMessage == ePointerMove ||
902 mMessage == ePointerUp || mMessage == ePointerCancel ||
903 mMessage == ePointerOver || mMessage == ePointerOut ||
904 mMessage == ePointerGotCapture || mMessage == ePointerLostCapture;
905 break;
906 case eTouchEventClass:
907 // All touch events are composed
908 mFlags.mComposed = mMessage == eTouchStart || mMessage == eTouchEnd ||
909 mMessage == eTouchMove || mMessage == eTouchCancel;
910 break;
911 case eUIEventClass:
912 mFlags.mComposed = mMessage == eLegacyDOMFocusIn ||
913 mMessage == eLegacyDOMFocusOut ||
914 mMessage == eLegacyDOMActivate;
915 break;
916 case eWheelEventClass:
917 // All wheel events are composed
918 mFlags.mComposed = mMessage == eWheel;
919 break;
920 case eMouseScrollEventClass:
921 // Legacy mouse scroll events are composed too, for consistency with
922 // wheel.
923 mFlags.mComposed = mMessage == eLegacyMouseLineOrPageScroll ||
924 mMessage == eLegacyMousePixelScroll;
925 break;
926 default:
927 mFlags.mComposed = false;
928 break;
932 void SetComposed(const nsAString& aEventTypeArg) {
933 mFlags.mComposed = // composition events
934 aEventTypeArg.EqualsLiteral("compositionstart") ||
935 aEventTypeArg.EqualsLiteral("compositionupdate") ||
936 aEventTypeArg.EqualsLiteral("compositionend") ||
937 aEventTypeArg.EqualsLiteral("text") ||
938 // drag and drop events
939 aEventTypeArg.EqualsLiteral("dragstart") ||
940 aEventTypeArg.EqualsLiteral("drag") ||
941 aEventTypeArg.EqualsLiteral("dragenter") ||
942 aEventTypeArg.EqualsLiteral("dragexit") ||
943 aEventTypeArg.EqualsLiteral("dragleave") ||
944 aEventTypeArg.EqualsLiteral("dragover") ||
945 aEventTypeArg.EqualsLiteral("drop") ||
946 aEventTypeArg.EqualsLiteral("dropend") ||
947 // editor input events
948 aEventTypeArg.EqualsLiteral("input") ||
949 aEventTypeArg.EqualsLiteral("beforeinput") ||
950 // focus events
951 aEventTypeArg.EqualsLiteral("blur") ||
952 aEventTypeArg.EqualsLiteral("focus") ||
953 aEventTypeArg.EqualsLiteral("focusin") ||
954 aEventTypeArg.EqualsLiteral("focusout") ||
955 // keyboard events
956 aEventTypeArg.EqualsLiteral("keydown") ||
957 aEventTypeArg.EqualsLiteral("keyup") ||
958 aEventTypeArg.EqualsLiteral("keypress") ||
959 // mouse events
960 aEventTypeArg.EqualsLiteral("click") ||
961 aEventTypeArg.EqualsLiteral("dblclick") ||
962 aEventTypeArg.EqualsLiteral("mousedown") ||
963 aEventTypeArg.EqualsLiteral("mouseup") ||
964 aEventTypeArg.EqualsLiteral("mouseenter") ||
965 aEventTypeArg.EqualsLiteral("mouseleave") ||
966 aEventTypeArg.EqualsLiteral("mouseover") ||
967 aEventTypeArg.EqualsLiteral("mouseout") ||
968 aEventTypeArg.EqualsLiteral("mousemove") ||
969 aEventTypeArg.EqualsLiteral("contextmenu") ||
970 // pointer events
971 aEventTypeArg.EqualsLiteral("pointerdown") ||
972 aEventTypeArg.EqualsLiteral("pointermove") ||
973 aEventTypeArg.EqualsLiteral("pointerup") ||
974 aEventTypeArg.EqualsLiteral("pointercancel") ||
975 aEventTypeArg.EqualsLiteral("pointerover") ||
976 aEventTypeArg.EqualsLiteral("pointerout") ||
977 aEventTypeArg.EqualsLiteral("pointerenter") ||
978 aEventTypeArg.EqualsLiteral("pointerleave") ||
979 aEventTypeArg.EqualsLiteral("gotpointercapture") ||
980 aEventTypeArg.EqualsLiteral("lostpointercapture") ||
981 // touch events
982 aEventTypeArg.EqualsLiteral("touchstart") ||
983 aEventTypeArg.EqualsLiteral("touchend") ||
984 aEventTypeArg.EqualsLiteral("touchmove") ||
985 aEventTypeArg.EqualsLiteral("touchcancel") ||
986 // UI legacy events
987 aEventTypeArg.EqualsLiteral("DOMFocusIn") ||
988 aEventTypeArg.EqualsLiteral("DOMFocusOut") ||
989 aEventTypeArg.EqualsLiteral("DOMActivate") ||
990 // wheel events
991 aEventTypeArg.EqualsLiteral("wheel");
994 void SetComposed(bool aComposed) { mFlags.mComposed = aComposed; }
996 void SetDefaultComposedInNativeAnonymousContent() {
997 // For compatibility concerns, we set mComposedInNativeAnonymousContent to
998 // false for those events we want to stop propagation.
1000 // nsVideoFrame may create anonymous image element which fires eLoad,
1001 // eLoadStart, eLoadEnd, eLoadError. We don't want these events cross
1002 // the boundary of NAC
1003 mFlags.mComposedInNativeAnonymousContent =
1004 mMessage != eLoad && mMessage != eLoadStart && mMessage != eLoadEnd &&
1005 mMessage != eLoadError;
1008 bool IsUserAction() const;
1011 /******************************************************************************
1012 * mozilla::WidgetGUIEvent
1013 ******************************************************************************/
1015 class WidgetGUIEvent : public WidgetEvent {
1016 protected:
1017 WidgetGUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1018 EventClassID aEventClassID)
1019 : WidgetEvent(aIsTrusted, aMessage, aEventClassID), mWidget(aWidget) {}
1021 WidgetGUIEvent() = default;
1023 public:
1024 virtual WidgetGUIEvent* AsGUIEvent() override { return this; }
1026 WidgetGUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget)
1027 : WidgetEvent(aIsTrusted, aMessage, eGUIEventClass), mWidget(aWidget) {}
1029 virtual WidgetEvent* Duplicate() const override {
1030 MOZ_ASSERT(mClass == eGUIEventClass,
1031 "Duplicate() must be overridden by sub class");
1032 // Not copying widget, it is a weak reference.
1033 WidgetGUIEvent* result = new WidgetGUIEvent(false, mMessage, nullptr);
1034 result->AssignGUIEventData(*this, true);
1035 result->mFlags = mFlags;
1036 return result;
1039 // Originator of the event
1040 nsCOMPtr<nsIWidget> mWidget;
1042 void AssignGUIEventData(const WidgetGUIEvent& aEvent, bool aCopyTargets) {
1043 AssignEventData(aEvent, aCopyTargets);
1044 // widget should be initialized with the constructor.
1048 /******************************************************************************
1049 * mozilla::Modifier
1051 * All modifier keys should be defined here. This is used for managing
1052 * modifier states for DOM Level 3 or later.
1053 ******************************************************************************/
1055 enum Modifier {
1056 MODIFIER_NONE = 0x0000,
1057 MODIFIER_ALT = 0x0001,
1058 MODIFIER_ALTGRAPH = 0x0002,
1059 MODIFIER_CAPSLOCK = 0x0004,
1060 MODIFIER_CONTROL = 0x0008,
1061 MODIFIER_FN = 0x0010,
1062 MODIFIER_FNLOCK = 0x0020,
1063 MODIFIER_META = 0x0040,
1064 MODIFIER_NUMLOCK = 0x0080,
1065 MODIFIER_SCROLLLOCK = 0x0100,
1066 MODIFIER_SHIFT = 0x0200,
1067 MODIFIER_SYMBOL = 0x0400,
1068 MODIFIER_SYMBOLLOCK = 0x0800,
1069 MODIFIER_OS = 0x1000
1072 /******************************************************************************
1073 * Modifier key names.
1074 ******************************************************************************/
1076 #define NS_DOM_KEYNAME_ALT "Alt"
1077 #define NS_DOM_KEYNAME_ALTGRAPH "AltGraph"
1078 #define NS_DOM_KEYNAME_CAPSLOCK "CapsLock"
1079 #define NS_DOM_KEYNAME_CONTROL "Control"
1080 #define NS_DOM_KEYNAME_FN "Fn"
1081 #define NS_DOM_KEYNAME_FNLOCK "FnLock"
1082 #define NS_DOM_KEYNAME_META "Meta"
1083 #define NS_DOM_KEYNAME_NUMLOCK "NumLock"
1084 #define NS_DOM_KEYNAME_SCROLLLOCK "ScrollLock"
1085 #define NS_DOM_KEYNAME_SHIFT "Shift"
1086 #define NS_DOM_KEYNAME_SYMBOL "Symbol"
1087 #define NS_DOM_KEYNAME_SYMBOLLOCK "SymbolLock"
1088 #define NS_DOM_KEYNAME_OS "OS"
1090 /******************************************************************************
1091 * mozilla::Modifiers
1092 ******************************************************************************/
1094 typedef uint16_t Modifiers;
1096 class MOZ_STACK_CLASS GetModifiersName final : public nsAutoCString {
1097 public:
1098 explicit GetModifiersName(Modifiers aModifiers) {
1099 if (aModifiers & MODIFIER_ALT) {
1100 AssignLiteral(NS_DOM_KEYNAME_ALT);
1102 if (aModifiers & MODIFIER_ALTGRAPH) {
1103 MaybeAppendSeparator();
1104 AppendLiteral(NS_DOM_KEYNAME_ALTGRAPH);
1106 if (aModifiers & MODIFIER_CAPSLOCK) {
1107 MaybeAppendSeparator();
1108 AppendLiteral(NS_DOM_KEYNAME_CAPSLOCK);
1110 if (aModifiers & MODIFIER_CONTROL) {
1111 MaybeAppendSeparator();
1112 AppendLiteral(NS_DOM_KEYNAME_CONTROL);
1114 if (aModifiers & MODIFIER_FN) {
1115 MaybeAppendSeparator();
1116 AppendLiteral(NS_DOM_KEYNAME_FN);
1118 if (aModifiers & MODIFIER_FNLOCK) {
1119 MaybeAppendSeparator();
1120 AppendLiteral(NS_DOM_KEYNAME_FNLOCK);
1122 if (aModifiers & MODIFIER_META) {
1123 MaybeAppendSeparator();
1124 AppendLiteral(NS_DOM_KEYNAME_META);
1126 if (aModifiers & MODIFIER_NUMLOCK) {
1127 MaybeAppendSeparator();
1128 AppendLiteral(NS_DOM_KEYNAME_NUMLOCK);
1130 if (aModifiers & MODIFIER_SCROLLLOCK) {
1131 MaybeAppendSeparator();
1132 AppendLiteral(NS_DOM_KEYNAME_SCROLLLOCK);
1134 if (aModifiers & MODIFIER_SHIFT) {
1135 MaybeAppendSeparator();
1136 AppendLiteral(NS_DOM_KEYNAME_SHIFT);
1138 if (aModifiers & MODIFIER_SYMBOL) {
1139 MaybeAppendSeparator();
1140 AppendLiteral(NS_DOM_KEYNAME_SYMBOL);
1142 if (aModifiers & MODIFIER_SYMBOLLOCK) {
1143 MaybeAppendSeparator();
1144 AppendLiteral(NS_DOM_KEYNAME_SYMBOLLOCK);
1146 if (aModifiers & MODIFIER_OS) {
1147 MaybeAppendSeparator();
1148 AppendLiteral(NS_DOM_KEYNAME_OS);
1150 if (IsEmpty()) {
1151 AssignLiteral("none");
1155 private:
1156 void MaybeAppendSeparator() {
1157 if (!IsEmpty()) {
1158 AppendLiteral(" | ");
1163 /******************************************************************************
1164 * mozilla::WidgetInputEvent
1165 ******************************************************************************/
1167 class WidgetInputEvent : public WidgetGUIEvent {
1168 protected:
1169 WidgetInputEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1170 EventClassID aEventClassID)
1171 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, aEventClassID),
1172 mModifiers(0) {}
1174 WidgetInputEvent() : mModifiers(0) {}
1176 public:
1177 virtual WidgetInputEvent* AsInputEvent() override { return this; }
1179 WidgetInputEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget)
1180 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eInputEventClass),
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 = new WidgetInputEvent(false, mMessage, nullptr);
1188 result->AssignInputEventData(*this, true);
1189 result->mFlags = mFlags;
1190 return result;
1194 * Returns a modifier of "Accel" virtual modifier which is used for shortcut
1195 * key.
1197 static Modifier AccelModifier();
1200 * GetModifier() returns a modifier flag which is activated by aDOMKeyName.
1202 static Modifier GetModifier(const nsAString& aDOMKeyName);
1204 // true indicates the accel key on the environment is down
1205 bool IsAccel() const { return ((mModifiers & AccelModifier()) != 0); }
1207 // true indicates the shift key is down
1208 bool IsShift() const { return ((mModifiers & MODIFIER_SHIFT) != 0); }
1209 // true indicates the control key is down
1210 bool IsControl() const { return ((mModifiers & MODIFIER_CONTROL) != 0); }
1211 // true indicates the alt key is down
1212 bool IsAlt() const { return ((mModifiers & MODIFIER_ALT) != 0); }
1213 // true indicates the meta key is down (or, on Mac, the Command key)
1214 bool IsMeta() const { return ((mModifiers & MODIFIER_META) != 0); }
1215 // true indicates the win key is down on Windows. Or the Super or Hyper key
1216 // is down on Linux.
1217 bool IsOS() const { return ((mModifiers & MODIFIER_OS) != 0); }
1218 // true indicates the alt graph key is down
1219 // NOTE: on Mac, the option key press causes both IsAlt() and IsAltGrpah()
1220 // return true.
1221 bool IsAltGraph() const { return ((mModifiers & MODIFIER_ALTGRAPH) != 0); }
1222 // true indicates the CapLock LED is turn on.
1223 bool IsCapsLocked() const { return ((mModifiers & MODIFIER_CAPSLOCK) != 0); }
1224 // true indicates the NumLock LED is turn on.
1225 bool IsNumLocked() const { return ((mModifiers & MODIFIER_NUMLOCK) != 0); }
1226 // true indicates the ScrollLock LED is turn on.
1227 bool IsScrollLocked() const {
1228 return ((mModifiers & MODIFIER_SCROLLLOCK) != 0);
1231 // true indicates the Fn key is down, but this is not supported by native
1232 // key event on any platform.
1233 bool IsFn() const { return ((mModifiers & MODIFIER_FN) != 0); }
1234 // true indicates the FnLock LED is turn on, but we don't know such
1235 // keyboards nor platforms.
1236 bool IsFnLocked() const { return ((mModifiers & MODIFIER_FNLOCK) != 0); }
1237 // true indicates the Symbol is down, but this is not supported by native
1238 // key event on any platforms.
1239 bool IsSymbol() const { return ((mModifiers & MODIFIER_SYMBOL) != 0); }
1240 // true indicates the SymbolLock LED is turn on, but we don't know such
1241 // keyboards nor platforms.
1242 bool IsSymbolLocked() const {
1243 return ((mModifiers & MODIFIER_SYMBOLLOCK) != 0);
1246 void InitBasicModifiers(bool aCtrlKey, bool aAltKey, bool aShiftKey,
1247 bool aMetaKey) {
1248 mModifiers = 0;
1249 if (aCtrlKey) {
1250 mModifiers |= MODIFIER_CONTROL;
1252 if (aAltKey) {
1253 mModifiers |= MODIFIER_ALT;
1255 if (aShiftKey) {
1256 mModifiers |= MODIFIER_SHIFT;
1258 if (aMetaKey) {
1259 mModifiers |= MODIFIER_META;
1263 Modifiers mModifiers;
1265 void AssignInputEventData(const WidgetInputEvent& aEvent, bool aCopyTargets) {
1266 AssignGUIEventData(aEvent, aCopyTargets);
1268 mModifiers = aEvent.mModifiers;
1272 /******************************************************************************
1273 * mozilla::InternalUIEvent
1275 * XXX Why this inherits WidgetGUIEvent rather than WidgetEvent?
1276 ******************************************************************************/
1278 class InternalUIEvent : public WidgetGUIEvent {
1279 protected:
1280 InternalUIEvent() : mDetail(0), mCausedByUntrustedEvent(false) {}
1282 InternalUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1283 EventClassID aEventClassID)
1284 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, aEventClassID),
1285 mDetail(0),
1286 mCausedByUntrustedEvent(false) {}
1288 InternalUIEvent(bool aIsTrusted, EventMessage aMessage,
1289 EventClassID aEventClassID)
1290 : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, aEventClassID),
1291 mDetail(0),
1292 mCausedByUntrustedEvent(false) {}
1294 public:
1295 virtual InternalUIEvent* AsUIEvent() override { return this; }
1298 * If the UIEvent is caused by another event (e.g., click event),
1299 * aEventCausesThisEvent should be the event. If there is no such event,
1300 * this should be nullptr.
1302 InternalUIEvent(bool aIsTrusted, EventMessage aMessage,
1303 const WidgetEvent* aEventCausesThisEvent)
1304 : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, eUIEventClass),
1305 mDetail(0),
1306 mCausedByUntrustedEvent(aEventCausesThisEvent &&
1307 !aEventCausesThisEvent->IsTrusted()) {}
1309 virtual WidgetEvent* Duplicate() const override {
1310 MOZ_ASSERT(mClass == eUIEventClass,
1311 "Duplicate() must be overridden by sub class");
1312 InternalUIEvent* result = new InternalUIEvent(false, mMessage, nullptr);
1313 result->AssignUIEventData(*this, true);
1314 result->mFlags = mFlags;
1315 return result;
1318 int32_t mDetail;
1319 // mCausedByUntrustedEvent is true if the event is caused by untrusted event.
1320 bool mCausedByUntrustedEvent;
1322 // If you check the event is a trusted event and NOT caused by an untrusted
1323 // event, IsTrustable() returns what you expected.
1324 bool IsTrustable() const { return IsTrusted() && !mCausedByUntrustedEvent; }
1326 void AssignUIEventData(const InternalUIEvent& aEvent, bool aCopyTargets) {
1327 AssignGUIEventData(aEvent, aCopyTargets);
1329 mDetail = aEvent.mDetail;
1330 mCausedByUntrustedEvent = aEvent.mCausedByUntrustedEvent;
1334 } // namespace mozilla
1336 #endif // mozilla_BasicEvents_h__