Bug 1253840 - Remove .ini file as it's no longer necessary by passing on all platform...
[gecko.git] / widget / BasicEvents.h
blob704905a3d2310f1cec9943a6f8eb0641fd150674
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 // If the event is being handled in target phase, returns true.
186 inline bool InTargetPhase() const {
187 return (mInBubblingPhase && mInCapturePhase);
191 * Helper methods for methods of DOM Event.
193 inline void StopPropagation() { mPropagationStopped = true; }
194 inline void StopImmediatePropagation() {
195 StopPropagation();
196 mImmediatePropagationStopped = true;
198 inline void PreventDefault(bool aCalledByDefaultHandler = true) {
199 if (!mCancelable) {
200 return;
202 mDefaultPrevented = true;
203 // Note that even if preventDefault() has already been called by chrome,
204 // a call of preventDefault() by content needs to overwrite
205 // mDefaultPreventedByContent to true because in such case, defaultPrevented
206 // must be true when web apps check it after they call preventDefault().
207 if (aCalledByDefaultHandler) {
208 StopCrossProcessForwarding();
209 mDefaultPreventedByChrome = true;
210 } else {
211 mDefaultPreventedByContent = true;
214 // This should be used only before dispatching events into the DOM tree.
215 inline void PreventDefaultBeforeDispatch(
216 CrossProcessForwarding aCrossProcessForwarding) {
217 if (!mCancelable) {
218 return;
220 mDefaultPrevented = true;
221 if (aCrossProcessForwarding == CrossProcessForwarding::eStop) {
222 StopCrossProcessForwarding();
225 inline bool DefaultPrevented() const { return mDefaultPrevented; }
226 inline bool DefaultPreventedByContent() const {
227 MOZ_ASSERT(!mDefaultPreventedByContent || DefaultPrevented());
228 return mDefaultPreventedByContent;
230 inline bool IsTrusted() const { return mIsTrusted; }
231 inline bool PropagationStopped() const { return mPropagationStopped; }
233 // Helper methods to access flags managing state of propagation between
234 // processes.
237 * Prevent to be dispatched to remote process.
239 inline void StopCrossProcessForwarding() {
240 MOZ_ASSERT(!mPostedToRemoteProcess);
241 mNoRemoteProcessDispatch = true;
242 mWantReplyFromContentProcess = false;
245 * Return true if the event shouldn't be dispatched to remote process.
247 inline bool IsCrossProcessForwardingStopped() const {
248 return mNoRemoteProcessDispatch;
251 * Mark the event as waiting reply from remote process.
252 * If the caller needs to win other keyboard event handlers in chrome,
253 * the caller should call StopPropagation() too.
254 * Otherwise, if the caller just needs to know if the event is consumed by
255 * either content or chrome, it should just call this because the event
256 * may be reserved by chrome and it needs to be dispatched into the DOM
257 * tree in chrome for checking if it's reserved before being sent to any
258 * remote processes.
260 inline void MarkAsWaitingReplyFromRemoteProcess() {
261 MOZ_ASSERT(!mPostedToRemoteProcess);
262 mNoRemoteProcessDispatch = false;
263 mWantReplyFromContentProcess = true;
266 * Reset "waiting reply from remote process" state. This is useful when
267 * you dispatch a copy of an event coming from different process.
269 inline void ResetWaitingReplyFromRemoteProcessState() {
270 if (IsWaitingReplyFromRemoteProcess()) {
271 // FYI: mWantReplyFromContentProcess is also used for indicating
272 // "handled in remote process" state. Therefore, only when
273 // IsWaitingReplyFromRemoteProcess() returns true, this should
274 // reset the flag.
275 mWantReplyFromContentProcess = false;
279 * Return true if the event handler should wait reply event. I.e., if this
280 * returns true, any event handler should do nothing with the event.
282 inline bool IsWaitingReplyFromRemoteProcess() const {
283 return !mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
286 * Mark the event as already handled in the remote process. This should be
287 * called when initializing reply events.
289 inline void MarkAsHandledInRemoteProcess() {
290 mNoRemoteProcessDispatch = true;
291 mWantReplyFromContentProcess = true;
292 mPostedToRemoteProcess = false;
295 * Return true if the event has already been handled in the remote process.
297 inline bool IsHandledInRemoteProcess() const {
298 return mNoRemoteProcessDispatch && mWantReplyFromContentProcess;
301 * Return true if the event should be sent back to its parent process.
303 inline bool WantReplyFromContentProcess() const {
304 MOZ_ASSERT(!XRE_IsParentProcess());
305 return IsWaitingReplyFromRemoteProcess();
308 * Mark the event has already posted to a remote process.
310 inline void MarkAsPostedToRemoteProcess() {
311 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
312 mPostedToRemoteProcess = true;
315 * Reset the cross process dispatching state. This should be used when a
316 * process receives the event because the state is in the sender.
318 inline void ResetCrossProcessDispatchingState() {
319 MOZ_ASSERT(!IsCrossProcessForwardingStopped());
320 mPostedToRemoteProcess = false;
321 // Ignore propagation state in the remote process if it's marked as
322 // "waiting reply from remote process" because the process needs to
323 // stop propagation in the process until receiving a reply event.
324 // Note that the propagation stopped flag is important for the reply event
325 // handler in the main process because it's used for making whether it's
326 // ignored by the remote process or not.
327 if (!XRE_IsParentProcess() && IsWaitingReplyFromRemoteProcess()) {
328 mPropagationStopped = mImmediatePropagationStopped = false;
330 // mDispatchedAtLeastOnce indicates the state in current process.
331 mDispatchedAtLeastOnce = false;
334 * Return true if the event has been posted to a remote process.
335 * Note that MarkAsPostedToRemoteProcess() is called by
336 * ParamTraits<mozilla::WidgetEvent>. Therefore, it *might* be possible
337 * that posting the event failed even if this returns true. But that must
338 * really rare. If that'd be problem for you, you should unmark this in
339 * BrowserParent or somewhere.
341 inline bool HasBeenPostedToRemoteProcess() const {
342 return mPostedToRemoteProcess;
345 * Return true if the event came from another process.
347 inline bool CameFromAnotherProcess() const { return mCameFromAnotherProcess; }
349 * Mark the event as coming from another process.
351 inline void MarkAsComingFromAnotherProcess() {
352 mCameFromAnotherProcess = true;
355 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
356 * content because it shouldn't be cancelable.
358 inline void MarkAsReservedByChrome() {
359 MOZ_ASSERT(!mPostedToRemoteProcess);
360 mIsReservedByChrome = true;
361 // For reserved commands (such as Open New Tab), we don't need to wait for
362 // the content to answer, neither to give a chance for content to override
363 // its behavior.
364 StopCrossProcessForwarding();
365 // If the event is reserved by chrome, we shouldn't expose the event to
366 // web contents because such events shouldn't be cancelable. So, it's not
367 // good behavior to fire such events but to ignore the defaultPrevented
368 // attribute value in chrome.
369 mOnlySystemGroupDispatchInContent = true;
372 * Return true if the event is reserved by chrome.
374 inline bool IsReservedByChrome() const {
375 MOZ_ASSERT(!mIsReservedByChrome || (IsCrossProcessForwardingStopped() &&
376 mOnlySystemGroupDispatchInContent));
377 return mIsReservedByChrome;
380 inline void Clear() { SetRawFlags(0); }
381 // Get if either the instance's bit or the aOther's bit is true, the
382 // instance's bit becomes true. In other words, this works like:
383 // eventFlags |= aOther;
384 inline void Union(const BaseEventFlags& aOther) {
385 RawFlags rawFlags = GetRawFlags() | aOther.GetRawFlags();
386 SetRawFlags(rawFlags);
389 private:
390 typedef uint64_t RawFlags;
392 inline void SetRawFlags(RawFlags aRawFlags) {
393 static_assert(sizeof(BaseEventFlags) <= sizeof(RawFlags),
394 "mozilla::EventFlags must not be bigger than the RawFlags");
395 memcpy(this, &aRawFlags, sizeof(BaseEventFlags));
397 inline RawFlags GetRawFlags() const {
398 RawFlags result = 0;
399 memcpy(&result, this, sizeof(BaseEventFlags));
400 return result;
404 /******************************************************************************
405 * mozilla::EventFlags
406 ******************************************************************************/
408 struct EventFlags : public BaseEventFlags {
409 EventFlags() { Clear(); }
412 /******************************************************************************
413 * mozilla::WidgetEventTime
414 ******************************************************************************/
416 class WidgetEventTime {
417 public:
418 // Timestamp when the message was created.
419 TimeStamp mTimeStamp;
421 WidgetEventTime() : mTimeStamp(TimeStamp::Now()) {}
423 explicit WidgetEventTime(TimeStamp aTimeStamp) : mTimeStamp(aTimeStamp) {}
425 void AssignEventTime(const WidgetEventTime& aOther) {
426 mTimeStamp = aOther.mTimeStamp;
430 /******************************************************************************
431 * mozilla::WidgetEvent
432 ******************************************************************************/
434 class WidgetEvent : public WidgetEventTime {
435 private:
436 void SetDefaultCancelableAndBubbles() {
437 switch (mClass) {
438 case eEditorInputEventClass:
439 mFlags.mCancelable = false;
440 mFlags.mBubbles = mFlags.mIsTrusted;
441 break;
442 case eMouseEventClass:
443 mFlags.mCancelable =
444 (mMessage != eMouseEnter && mMessage != eMouseLeave);
445 mFlags.mBubbles = (mMessage != eMouseEnter && mMessage != eMouseLeave);
446 break;
447 case ePointerEventClass:
448 mFlags.mCancelable =
449 (mMessage != ePointerEnter && mMessage != ePointerLeave &&
450 mMessage != ePointerCancel && mMessage != ePointerGotCapture &&
451 mMessage != ePointerLostCapture);
452 mFlags.mBubbles =
453 (mMessage != ePointerEnter && mMessage != ePointerLeave);
454 break;
455 case eDragEventClass:
456 mFlags.mCancelable = (mMessage != eDragExit && mMessage != eDragLeave &&
457 mMessage != eDragEnd);
458 mFlags.mBubbles = true;
459 break;
460 case eSMILTimeEventClass:
461 mFlags.mCancelable = false;
462 mFlags.mBubbles = false;
463 break;
464 case eTransitionEventClass:
465 case eAnimationEventClass:
466 mFlags.mCancelable = false;
467 mFlags.mBubbles = true;
468 break;
469 case eCompositionEventClass:
470 // XXX compositionstart is cancelable in draft of DOM3 Events.
471 // However, it doesn't make sense for us, we cannot cancel
472 // composition when we send compositionstart event.
473 mFlags.mCancelable = false;
474 mFlags.mBubbles = true;
475 break;
476 default:
477 if (mMessage == eResize || mMessage == eMozVisualResize ||
478 mMessage == eMozVisualScroll || mMessage == eEditorInput ||
479 mMessage == eFormSelect) {
480 mFlags.mCancelable = false;
481 } else {
482 mFlags.mCancelable = true;
484 mFlags.mBubbles = true;
485 break;
489 protected:
490 WidgetEvent(bool aIsTrusted, EventMessage aMessage,
491 EventClassID aEventClassID)
492 : WidgetEventTime(),
493 mClass(aEventClassID),
494 mMessage(aMessage),
495 mRefPoint(0, 0),
496 mLastRefPoint(0, 0),
497 mFocusSequenceNumber(0),
498 mSpecifiedEventType(nullptr),
499 mPath(nullptr),
500 mLayersId(layers::LayersId{0}) {
501 MOZ_COUNT_CTOR(WidgetEvent);
502 mFlags.Clear();
503 mFlags.mIsTrusted = aIsTrusted;
504 SetDefaultCancelableAndBubbles();
505 SetDefaultComposed();
506 SetDefaultComposedInNativeAnonymousContent();
509 WidgetEvent() : WidgetEventTime(), mPath(nullptr) {
510 MOZ_COUNT_CTOR(WidgetEvent);
513 public:
514 WidgetEvent(bool aIsTrusted, EventMessage aMessage)
515 : WidgetEvent(aIsTrusted, aMessage, eBasicEventClass) {}
517 MOZ_COUNTED_DTOR_VIRTUAL(WidgetEvent)
519 WidgetEvent(const WidgetEvent& aOther) : WidgetEventTime() {
520 MOZ_COUNT_CTOR(WidgetEvent);
521 *this = aOther;
523 WidgetEvent& operator=(const WidgetEvent& aOther) = default;
525 WidgetEvent(WidgetEvent&& aOther)
526 : WidgetEventTime(std::move(aOther)),
527 mClass(aOther.mClass),
528 mMessage(aOther.mMessage),
529 mRefPoint(std::move(aOther.mRefPoint)),
530 mLastRefPoint(std::move(aOther.mLastRefPoint)),
531 mFocusSequenceNumber(aOther.mFocusSequenceNumber),
532 mFlags(std::move(aOther.mFlags)),
533 mSpecifiedEventType(std::move(aOther.mSpecifiedEventType)),
534 mSpecifiedEventTypeString(std::move(aOther.mSpecifiedEventTypeString)),
535 mTarget(std::move(aOther.mTarget)),
536 mCurrentTarget(std::move(aOther.mCurrentTarget)),
537 mOriginalTarget(std::move(aOther.mOriginalTarget)),
538 mRelatedTarget(std::move(aOther.mRelatedTarget)),
539 mOriginalRelatedTarget(std::move(aOther.mOriginalRelatedTarget)),
540 mPath(std::move(aOther.mPath)) {
541 MOZ_COUNT_CTOR(WidgetEvent);
543 WidgetEvent& operator=(WidgetEvent&& aOther) = default;
545 virtual WidgetEvent* Duplicate() const {
546 MOZ_ASSERT(mClass == eBasicEventClass,
547 "Duplicate() must be overridden by sub class");
548 WidgetEvent* result = new WidgetEvent(false, mMessage);
549 result->AssignEventData(*this, true);
550 result->mFlags = mFlags;
551 return result;
554 EventClassID mClass;
555 EventMessage mMessage;
556 // Relative to the widget of the event, or if there is no widget then it is
557 // in screen coordinates. Not modified by layout code.
558 // This is in visual coordinates, i.e. the correct RelativeTo value that
559 // expresses what this is relative to is `{viewportFrame, Visual}`, where
560 // `viewportFrame` is the viewport frame of the widget's root document.
561 LayoutDeviceIntPoint mRefPoint;
562 // The previous mRefPoint, if known, used to calculate mouse movement deltas.
563 LayoutDeviceIntPoint mLastRefPoint;
564 // The sequence number of the last potentially focus changing event handled
565 // by APZ. This is used to track when that event has been processed by
566 // content, and focus can be reconfirmed for async keyboard scrolling.
567 uint64_t mFocusSequenceNumber;
568 // See BaseEventFlags definition for the detail.
569 BaseEventFlags mFlags;
571 // If JS creates an event with unknown event type or known event type but
572 // for different event interface, the event type is stored to this.
573 // NOTE: This is always used if the instance is a WidgetCommandEvent instance
574 // or "input" event is dispatched with dom::Event class.
575 RefPtr<nsAtom> mSpecifiedEventType;
577 // nsAtom isn't available on non-main thread due to unsafe. Therefore,
578 // mSpecifiedEventTypeString is used instead of mSpecifiedEventType if
579 // the event is created in non-main thread.
580 nsString mSpecifiedEventTypeString;
582 // Event targets, needed by DOM Events
583 // Note that when you need event target for DOM event, you should use
584 // Get*DOMEventTarget() instead of accessing these members directly.
585 nsCOMPtr<dom::EventTarget> mTarget;
586 nsCOMPtr<dom::EventTarget> mCurrentTarget;
587 nsCOMPtr<dom::EventTarget> mOriginalTarget;
589 /// The possible related target
590 nsCOMPtr<dom::EventTarget> mRelatedTarget;
591 nsCOMPtr<dom::EventTarget> mOriginalRelatedTarget;
593 nsTArray<EventTargetChainItem>* mPath;
595 // The LayersId of the content process that this event should be
596 // dispatched to. This field is only used in the chrome process
597 // and doesn't get remoted to child processes.
598 layers::LayersId mLayersId;
600 dom::EventTarget* GetDOMEventTarget() const;
601 dom::EventTarget* GetCurrentDOMEventTarget() const;
602 dom::EventTarget* GetOriginalDOMEventTarget() const;
604 void AssignEventData(const WidgetEvent& aEvent, bool aCopyTargets) {
605 // mClass should be initialized with the constructor.
606 // mMessage should be initialized with the constructor.
607 mRefPoint = aEvent.mRefPoint;
608 // mLastRefPoint doesn't need to be copied.
609 mFocusSequenceNumber = aEvent.mFocusSequenceNumber;
610 // mLayersId intentionally not copied, since it's not used within content
611 AssignEventTime(aEvent);
612 // mFlags should be copied manually if it's necessary.
613 mSpecifiedEventType = aEvent.mSpecifiedEventType;
614 // mSpecifiedEventTypeString should be copied manually if it's necessary.
615 mTarget = aCopyTargets ? aEvent.mTarget : nullptr;
616 mCurrentTarget = aCopyTargets ? aEvent.mCurrentTarget : nullptr;
617 mOriginalTarget = aCopyTargets ? aEvent.mOriginalTarget : nullptr;
618 mRelatedTarget = aCopyTargets ? aEvent.mRelatedTarget : nullptr;
619 mOriginalRelatedTarget =
620 aCopyTargets ? aEvent.mOriginalRelatedTarget : nullptr;
624 * Helper methods for methods of DOM Event.
626 void StopPropagation() { mFlags.StopPropagation(); }
627 void StopImmediatePropagation() { mFlags.StopImmediatePropagation(); }
628 void PreventDefault(bool aCalledByDefaultHandler = true,
629 nsIPrincipal* aPrincipal = nullptr);
631 void PreventDefaultBeforeDispatch(
632 CrossProcessForwarding aCrossProcessForwarding) {
633 mFlags.PreventDefaultBeforeDispatch(aCrossProcessForwarding);
635 bool DefaultPrevented() const { return mFlags.DefaultPrevented(); }
636 bool DefaultPreventedByContent() const {
637 return mFlags.DefaultPreventedByContent();
639 bool IsTrusted() const { return mFlags.IsTrusted(); }
640 bool PropagationStopped() const { return mFlags.PropagationStopped(); }
643 * Prevent to be dispatched to remote process.
645 inline void StopCrossProcessForwarding() {
646 mFlags.StopCrossProcessForwarding();
649 * Return true if the event shouldn't be dispatched to remote process.
651 inline bool IsCrossProcessForwardingStopped() const {
652 return mFlags.IsCrossProcessForwardingStopped();
655 * Mark the event as waiting reply from remote process.
656 * Note that this also stops immediate propagation in current process.
658 inline void MarkAsWaitingReplyFromRemoteProcess() {
659 mFlags.MarkAsWaitingReplyFromRemoteProcess();
662 * Reset "waiting reply from remote process" state. This is useful when
663 * you dispatch a copy of an event coming from different process.
665 inline void ResetWaitingReplyFromRemoteProcessState() {
666 mFlags.ResetWaitingReplyFromRemoteProcessState();
669 * Return true if the event handler should wait reply event. I.e., if this
670 * returns true, any event handler should do nothing with the event.
672 inline bool IsWaitingReplyFromRemoteProcess() const {
673 return mFlags.IsWaitingReplyFromRemoteProcess();
676 * Mark the event as already handled in the remote process. This should be
677 * called when initializing reply events.
679 inline void MarkAsHandledInRemoteProcess() {
680 mFlags.MarkAsHandledInRemoteProcess();
683 * Return true if the event has already been handled in the remote process.
684 * I.e., if this returns true, the event is a reply event.
686 inline bool IsHandledInRemoteProcess() const {
687 return mFlags.IsHandledInRemoteProcess();
690 * Return true if the event should be sent back to its parent process.
691 * So, usual event handlers shouldn't call this.
693 inline bool WantReplyFromContentProcess() const {
694 return mFlags.WantReplyFromContentProcess();
697 * Mark the event has already posted to a remote process.
699 inline void MarkAsPostedToRemoteProcess() {
700 mFlags.MarkAsPostedToRemoteProcess();
703 * Reset the cross process dispatching state. This should be used when a
704 * process receives the event because the state is in the sender.
706 inline void ResetCrossProcessDispatchingState() {
707 mFlags.ResetCrossProcessDispatchingState();
710 * Return true if the event has been posted to a remote process.
712 inline bool HasBeenPostedToRemoteProcess() const {
713 return mFlags.HasBeenPostedToRemoteProcess();
716 * Return true if the event came from another process.
718 inline bool CameFromAnotherProcess() const {
719 return mFlags.CameFromAnotherProcess();
722 * Mark the event as coming from another process.
724 inline void MarkAsComingFromAnotherProcess() {
725 mFlags.MarkAsComingFromAnotherProcess();
728 * Mark the event is reserved by chrome. I.e., shouldn't be dispatched to
729 * content because it shouldn't be cancelable.
731 inline void MarkAsReservedByChrome() { mFlags.MarkAsReservedByChrome(); }
733 * Return true if the event is reserved by chrome.
735 inline bool IsReservedByChrome() const { return mFlags.IsReservedByChrome(); }
738 * Utils for checking event types
742 * As*Event() returns the pointer of the instance only when the instance is
743 * the class or one of its derived class.
745 #define NS_ROOT_EVENT_CLASS(aPrefix, aName)
746 #define NS_EVENT_CLASS(aPrefix, aName) \
747 virtual aPrefix##aName* As##aName(); \
748 const aPrefix##aName* As##aName() const;
750 #include "mozilla/EventClassList.h"
752 #undef NS_EVENT_CLASS
753 #undef NS_ROOT_EVENT_CLASS
756 * Returns true if the event is a query content event.
758 bool IsQueryContentEvent() const;
760 * Returns true if the event is a selection event.
762 bool IsSelectionEvent() const;
764 * Returns true if the event is a content command event.
766 bool IsContentCommandEvent() const;
769 * Returns true if the event mMessage is one of mouse events.
771 bool HasMouseEventMessage() const;
773 * Returns true if the event mMessage is one of drag events.
775 bool HasDragEventMessage() const;
777 * Returns true if aMessage or mMessage is one of key events.
779 static bool IsKeyEventMessage(EventMessage aMessage);
780 bool HasKeyEventMessage() const { return IsKeyEventMessage(mMessage); }
782 * Returns true if the event mMessage is one of composition events or text
783 * event.
785 bool HasIMEEventMessage() const;
788 * Returns true if the event can be sent to remote process.
790 bool CanBeSentToRemoteProcess() const;
792 * Returns true if the original target is a remote process and the event
793 * will be posted to the remote process later.
795 bool WillBeSentToRemoteProcess() const;
797 * Returns true if the event is related to IME handling. It includes
798 * IME events, query content events and selection events.
799 * Be careful when you use this.
801 bool IsIMERelatedEvent() const;
804 * Whether the event should be handled by the frame of the mouse cursor
805 * position or not. When it should be handled there (e.g., the mouse events),
806 * this returns true.
808 bool IsUsingCoordinates() const;
810 * Whether the event should be handled by the focused DOM window in the
811 * same top level window's or not. E.g., key events, IME related events
812 * (including the query content events, they are used in IME transaction)
813 * should be handled by the (last) focused window rather than the dispatched
814 * window.
816 * NOTE: Even if this returns true, the event isn't going to be handled by the
817 * application level active DOM window which is on another top level window.
818 * So, when the event is fired on a deactive window, the event is going to be
819 * handled by the last focused DOM window in the last focused window.
821 bool IsTargetedAtFocusedWindow() const;
823 * Whether the event should be handled by the focused content or not. E.g.,
824 * key events, IME related events and other input events which are not handled
825 * by the frame of the mouse cursor position.
827 * NOTE: Even if this returns true, the event isn't going to be handled by the
828 * application level active DOM window which is on another top level window.
829 * So, when the event is fired on a deactive window, the event is going to be
830 * handled by the last focused DOM element of the last focused DOM window in
831 * the last focused window.
833 bool IsTargetedAtFocusedContent() const;
835 * Whether the event should cause a DOM event.
837 bool IsAllowedToDispatchDOMEvent() const;
839 * Whether the event should be dispatched in system group.
841 bool IsAllowedToDispatchInSystemGroup() const;
843 * Whether the event should be blocked for fingerprinting resistance.
845 bool IsBlockedForFingerprintingResistance() const;
847 * Initialize mComposed
849 void SetDefaultComposed() {
850 switch (mClass) {
851 case eClipboardEventClass:
852 mFlags.mComposed = true;
853 break;
854 case eCompositionEventClass:
855 mFlags.mComposed =
856 mMessage == eCompositionStart || mMessage == eCompositionUpdate ||
857 mMessage == eCompositionChange || mMessage == eCompositionEnd;
858 break;
859 case eDragEventClass:
860 // All drag & drop events are composed
861 mFlags.mComposed = mMessage == eDrag || mMessage == eDragEnd ||
862 mMessage == eDragEnter || mMessage == eDragExit ||
863 mMessage == eDragLeave || mMessage == eDragOver ||
864 mMessage == eDragStart || mMessage == eDrop;
865 break;
866 case eEditorInputEventClass:
867 mFlags.mComposed =
868 mMessage == eEditorInput || mMessage == eEditorBeforeInput;
869 break;
870 case eFocusEventClass:
871 mFlags.mComposed = mMessage == eBlur || mMessage == eFocus ||
872 mMessage == eFocusOut || mMessage == eFocusIn;
873 break;
874 case eKeyboardEventClass:
875 mFlags.mComposed =
876 mMessage == eKeyDown || mMessage == eKeyUp || mMessage == eKeyPress;
877 break;
878 case eMouseEventClass:
879 mFlags.mComposed =
880 mMessage == eMouseClick || mMessage == eMouseDoubleClick ||
881 mMessage == eMouseAuxClick || mMessage == eMouseDown ||
882 mMessage == eMouseUp || mMessage == eMouseOver ||
883 mMessage == eMouseOut || mMessage == eMouseMove ||
884 mMessage == eContextMenu || mMessage == eXULPopupShowing ||
885 mMessage == eXULPopupHiding || mMessage == eXULPopupShown ||
886 mMessage == eXULPopupHidden;
887 break;
888 case ePointerEventClass:
889 // All pointer events are composed
890 mFlags.mComposed =
891 mMessage == ePointerDown || mMessage == ePointerMove ||
892 mMessage == ePointerUp || mMessage == ePointerCancel ||
893 mMessage == ePointerOver || mMessage == ePointerOut ||
894 mMessage == ePointerGotCapture || mMessage == ePointerLostCapture;
895 break;
896 case eTouchEventClass:
897 // All touch events are composed
898 mFlags.mComposed = mMessage == eTouchStart || mMessage == eTouchEnd ||
899 mMessage == eTouchMove || mMessage == eTouchCancel;
900 break;
901 case eUIEventClass:
902 mFlags.mComposed = mMessage == eLegacyDOMFocusIn ||
903 mMessage == eLegacyDOMFocusOut ||
904 mMessage == eLegacyDOMActivate;
905 break;
906 case eWheelEventClass:
907 // All wheel events are composed
908 mFlags.mComposed = mMessage == eWheel;
909 break;
910 case eMouseScrollEventClass:
911 // Legacy mouse scroll events are composed too, for consistency with
912 // wheel.
913 mFlags.mComposed = mMessage == eLegacyMouseLineOrPageScroll ||
914 mMessage == eLegacyMousePixelScroll;
915 break;
916 default:
917 mFlags.mComposed = false;
918 break;
922 void SetComposed(const nsAString& aEventTypeArg) {
923 mFlags.mComposed = // composition events
924 aEventTypeArg.EqualsLiteral("compositionstart") ||
925 aEventTypeArg.EqualsLiteral("compositionupdate") ||
926 aEventTypeArg.EqualsLiteral("compositionend") ||
927 aEventTypeArg.EqualsLiteral("text") ||
928 // drag and drop events
929 aEventTypeArg.EqualsLiteral("dragstart") ||
930 aEventTypeArg.EqualsLiteral("drag") ||
931 aEventTypeArg.EqualsLiteral("dragenter") ||
932 aEventTypeArg.EqualsLiteral("dragexit") ||
933 aEventTypeArg.EqualsLiteral("dragleave") ||
934 aEventTypeArg.EqualsLiteral("dragover") ||
935 aEventTypeArg.EqualsLiteral("drop") ||
936 aEventTypeArg.EqualsLiteral("dropend") ||
937 // editor input events
938 aEventTypeArg.EqualsLiteral("input") ||
939 aEventTypeArg.EqualsLiteral("beforeinput") ||
940 // focus events
941 aEventTypeArg.EqualsLiteral("blur") ||
942 aEventTypeArg.EqualsLiteral("focus") ||
943 aEventTypeArg.EqualsLiteral("focusin") ||
944 aEventTypeArg.EqualsLiteral("focusout") ||
945 // keyboard events
946 aEventTypeArg.EqualsLiteral("keydown") ||
947 aEventTypeArg.EqualsLiteral("keyup") ||
948 aEventTypeArg.EqualsLiteral("keypress") ||
949 // mouse events
950 aEventTypeArg.EqualsLiteral("click") ||
951 aEventTypeArg.EqualsLiteral("dblclick") ||
952 aEventTypeArg.EqualsLiteral("mousedown") ||
953 aEventTypeArg.EqualsLiteral("mouseup") ||
954 aEventTypeArg.EqualsLiteral("mouseenter") ||
955 aEventTypeArg.EqualsLiteral("mouseleave") ||
956 aEventTypeArg.EqualsLiteral("mouseover") ||
957 aEventTypeArg.EqualsLiteral("mouseout") ||
958 aEventTypeArg.EqualsLiteral("mousemove") ||
959 aEventTypeArg.EqualsLiteral("contextmenu") ||
960 // pointer events
961 aEventTypeArg.EqualsLiteral("pointerdown") ||
962 aEventTypeArg.EqualsLiteral("pointermove") ||
963 aEventTypeArg.EqualsLiteral("pointerup") ||
964 aEventTypeArg.EqualsLiteral("pointercancel") ||
965 aEventTypeArg.EqualsLiteral("pointerover") ||
966 aEventTypeArg.EqualsLiteral("pointerout") ||
967 aEventTypeArg.EqualsLiteral("pointerenter") ||
968 aEventTypeArg.EqualsLiteral("pointerleave") ||
969 aEventTypeArg.EqualsLiteral("gotpointercapture") ||
970 aEventTypeArg.EqualsLiteral("lostpointercapture") ||
971 // touch events
972 aEventTypeArg.EqualsLiteral("touchstart") ||
973 aEventTypeArg.EqualsLiteral("touchend") ||
974 aEventTypeArg.EqualsLiteral("touchmove") ||
975 aEventTypeArg.EqualsLiteral("touchcancel") ||
976 // UI legacy events
977 aEventTypeArg.EqualsLiteral("DOMFocusIn") ||
978 aEventTypeArg.EqualsLiteral("DOMFocusOut") ||
979 aEventTypeArg.EqualsLiteral("DOMActivate") ||
980 // wheel events
981 aEventTypeArg.EqualsLiteral("wheel");
984 void SetComposed(bool aComposed) { mFlags.mComposed = aComposed; }
986 void SetDefaultComposedInNativeAnonymousContent() {
987 // For compatibility concerns, we set mComposedInNativeAnonymousContent to
988 // false for those events we want to stop propagation.
990 // nsVideoFrame may create anonymous image element which fires eLoad,
991 // eLoadStart, eLoadEnd, eLoadError. We don't want these events cross
992 // the boundary of NAC
993 mFlags.mComposedInNativeAnonymousContent =
994 mMessage != eLoad && mMessage != eLoadStart && mMessage != eLoadEnd &&
995 mMessage != eLoadError;
998 bool IsUserAction() const;
1001 /******************************************************************************
1002 * mozilla::WidgetGUIEvent
1003 ******************************************************************************/
1005 class WidgetGUIEvent : public WidgetEvent {
1006 protected:
1007 WidgetGUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1008 EventClassID aEventClassID)
1009 : WidgetEvent(aIsTrusted, aMessage, aEventClassID), mWidget(aWidget) {}
1011 WidgetGUIEvent() = default;
1013 public:
1014 virtual WidgetGUIEvent* AsGUIEvent() override { return this; }
1016 WidgetGUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget)
1017 : WidgetEvent(aIsTrusted, aMessage, eGUIEventClass), mWidget(aWidget) {}
1019 virtual WidgetEvent* Duplicate() const override {
1020 MOZ_ASSERT(mClass == eGUIEventClass,
1021 "Duplicate() must be overridden by sub class");
1022 // Not copying widget, it is a weak reference.
1023 WidgetGUIEvent* result = new WidgetGUIEvent(false, mMessage, nullptr);
1024 result->AssignGUIEventData(*this, true);
1025 result->mFlags = mFlags;
1026 return result;
1029 // Originator of the event
1030 nsCOMPtr<nsIWidget> mWidget;
1032 void AssignGUIEventData(const WidgetGUIEvent& aEvent, bool aCopyTargets) {
1033 AssignEventData(aEvent, aCopyTargets);
1034 // widget should be initialized with the constructor.
1038 /******************************************************************************
1039 * mozilla::Modifier
1041 * All modifier keys should be defined here. This is used for managing
1042 * modifier states for DOM Level 3 or later.
1043 ******************************************************************************/
1045 enum Modifier {
1046 MODIFIER_NONE = 0x0000,
1047 MODIFIER_ALT = 0x0001,
1048 MODIFIER_ALTGRAPH = 0x0002,
1049 MODIFIER_CAPSLOCK = 0x0004,
1050 MODIFIER_CONTROL = 0x0008,
1051 MODIFIER_FN = 0x0010,
1052 MODIFIER_FNLOCK = 0x0020,
1053 MODIFIER_META = 0x0040,
1054 MODIFIER_NUMLOCK = 0x0080,
1055 MODIFIER_SCROLLLOCK = 0x0100,
1056 MODIFIER_SHIFT = 0x0200,
1057 MODIFIER_SYMBOL = 0x0400,
1058 MODIFIER_SYMBOLLOCK = 0x0800,
1059 MODIFIER_OS = 0x1000
1062 /******************************************************************************
1063 * Modifier key names.
1064 ******************************************************************************/
1066 #define NS_DOM_KEYNAME_ALT "Alt"
1067 #define NS_DOM_KEYNAME_ALTGRAPH "AltGraph"
1068 #define NS_DOM_KEYNAME_CAPSLOCK "CapsLock"
1069 #define NS_DOM_KEYNAME_CONTROL "Control"
1070 #define NS_DOM_KEYNAME_FN "Fn"
1071 #define NS_DOM_KEYNAME_FNLOCK "FnLock"
1072 #define NS_DOM_KEYNAME_META "Meta"
1073 #define NS_DOM_KEYNAME_NUMLOCK "NumLock"
1074 #define NS_DOM_KEYNAME_SCROLLLOCK "ScrollLock"
1075 #define NS_DOM_KEYNAME_SHIFT "Shift"
1076 #define NS_DOM_KEYNAME_SYMBOL "Symbol"
1077 #define NS_DOM_KEYNAME_SYMBOLLOCK "SymbolLock"
1078 #define NS_DOM_KEYNAME_OS "OS"
1080 /******************************************************************************
1081 * mozilla::Modifiers
1082 ******************************************************************************/
1084 typedef uint16_t Modifiers;
1086 class MOZ_STACK_CLASS GetModifiersName final : public nsAutoCString {
1087 public:
1088 explicit GetModifiersName(Modifiers aModifiers) {
1089 if (aModifiers & MODIFIER_ALT) {
1090 AssignLiteral(NS_DOM_KEYNAME_ALT);
1092 if (aModifiers & MODIFIER_ALTGRAPH) {
1093 MaybeAppendSeparator();
1094 AppendLiteral(NS_DOM_KEYNAME_ALTGRAPH);
1096 if (aModifiers & MODIFIER_CAPSLOCK) {
1097 MaybeAppendSeparator();
1098 AppendLiteral(NS_DOM_KEYNAME_CAPSLOCK);
1100 if (aModifiers & MODIFIER_CONTROL) {
1101 MaybeAppendSeparator();
1102 AppendLiteral(NS_DOM_KEYNAME_CONTROL);
1104 if (aModifiers & MODIFIER_FN) {
1105 MaybeAppendSeparator();
1106 AppendLiteral(NS_DOM_KEYNAME_FN);
1108 if (aModifiers & MODIFIER_FNLOCK) {
1109 MaybeAppendSeparator();
1110 AppendLiteral(NS_DOM_KEYNAME_FNLOCK);
1112 if (aModifiers & MODIFIER_META) {
1113 MaybeAppendSeparator();
1114 AppendLiteral(NS_DOM_KEYNAME_META);
1116 if (aModifiers & MODIFIER_NUMLOCK) {
1117 MaybeAppendSeparator();
1118 AppendLiteral(NS_DOM_KEYNAME_NUMLOCK);
1120 if (aModifiers & MODIFIER_SCROLLLOCK) {
1121 MaybeAppendSeparator();
1122 AppendLiteral(NS_DOM_KEYNAME_SCROLLLOCK);
1124 if (aModifiers & MODIFIER_SHIFT) {
1125 MaybeAppendSeparator();
1126 AppendLiteral(NS_DOM_KEYNAME_SHIFT);
1128 if (aModifiers & MODIFIER_SYMBOL) {
1129 MaybeAppendSeparator();
1130 AppendLiteral(NS_DOM_KEYNAME_SYMBOL);
1132 if (aModifiers & MODIFIER_SYMBOLLOCK) {
1133 MaybeAppendSeparator();
1134 AppendLiteral(NS_DOM_KEYNAME_SYMBOLLOCK);
1136 if (aModifiers & MODIFIER_OS) {
1137 MaybeAppendSeparator();
1138 AppendLiteral(NS_DOM_KEYNAME_OS);
1140 if (IsEmpty()) {
1141 AssignLiteral("none");
1145 private:
1146 void MaybeAppendSeparator() {
1147 if (!IsEmpty()) {
1148 AppendLiteral(" | ");
1153 /******************************************************************************
1154 * mozilla::WidgetInputEvent
1155 ******************************************************************************/
1157 class WidgetInputEvent : public WidgetGUIEvent {
1158 protected:
1159 WidgetInputEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1160 EventClassID aEventClassID)
1161 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, aEventClassID),
1162 mModifiers(0) {}
1164 WidgetInputEvent() : mModifiers(0) {}
1166 public:
1167 virtual WidgetInputEvent* AsInputEvent() override { return this; }
1169 WidgetInputEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget)
1170 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, eInputEventClass),
1171 mModifiers(0) {}
1173 virtual WidgetEvent* Duplicate() const override {
1174 MOZ_ASSERT(mClass == eInputEventClass,
1175 "Duplicate() must be overridden by sub class");
1176 // Not copying widget, it is a weak reference.
1177 WidgetInputEvent* result = new WidgetInputEvent(false, mMessage, nullptr);
1178 result->AssignInputEventData(*this, true);
1179 result->mFlags = mFlags;
1180 return result;
1184 * Returns a modifier of "Accel" virtual modifier which is used for shortcut
1185 * key.
1187 static Modifier AccelModifier();
1190 * GetModifier() returns a modifier flag which is activated by aDOMKeyName.
1192 static Modifier GetModifier(const nsAString& aDOMKeyName);
1194 // true indicates the accel key on the environment is down
1195 bool IsAccel() const { return ((mModifiers & AccelModifier()) != 0); }
1197 // true indicates the shift key is down
1198 bool IsShift() const { return ((mModifiers & MODIFIER_SHIFT) != 0); }
1199 // true indicates the control key is down
1200 bool IsControl() const { return ((mModifiers & MODIFIER_CONTROL) != 0); }
1201 // true indicates the alt key is down
1202 bool IsAlt() const { return ((mModifiers & MODIFIER_ALT) != 0); }
1203 // true indicates the meta key is down (or, on Mac, the Command key)
1204 bool IsMeta() const { return ((mModifiers & MODIFIER_META) != 0); }
1205 // true indicates the win key is down on Windows. Or the Super or Hyper key
1206 // is down on Linux.
1207 bool IsOS() const { return ((mModifiers & MODIFIER_OS) != 0); }
1208 // true indicates the alt graph key is down
1209 // NOTE: on Mac, the option key press causes both IsAlt() and IsAltGrpah()
1210 // return true.
1211 bool IsAltGraph() const { return ((mModifiers & MODIFIER_ALTGRAPH) != 0); }
1212 // true indicates the CapLock LED is turn on.
1213 bool IsCapsLocked() const { return ((mModifiers & MODIFIER_CAPSLOCK) != 0); }
1214 // true indicates the NumLock LED is turn on.
1215 bool IsNumLocked() const { return ((mModifiers & MODIFIER_NUMLOCK) != 0); }
1216 // true indicates the ScrollLock LED is turn on.
1217 bool IsScrollLocked() const {
1218 return ((mModifiers & MODIFIER_SCROLLLOCK) != 0);
1221 // true indicates the Fn key is down, but this is not supported by native
1222 // key event on any platform.
1223 bool IsFn() const { return ((mModifiers & MODIFIER_FN) != 0); }
1224 // true indicates the FnLock LED is turn on, but we don't know such
1225 // keyboards nor platforms.
1226 bool IsFnLocked() const { return ((mModifiers & MODIFIER_FNLOCK) != 0); }
1227 // true indicates the Symbol is down, but this is not supported by native
1228 // key event on any platforms.
1229 bool IsSymbol() const { return ((mModifiers & MODIFIER_SYMBOL) != 0); }
1230 // true indicates the SymbolLock LED is turn on, but we don't know such
1231 // keyboards nor platforms.
1232 bool IsSymbolLocked() const {
1233 return ((mModifiers & MODIFIER_SYMBOLLOCK) != 0);
1236 void InitBasicModifiers(bool aCtrlKey, bool aAltKey, bool aShiftKey,
1237 bool aMetaKey) {
1238 mModifiers = 0;
1239 if (aCtrlKey) {
1240 mModifiers |= MODIFIER_CONTROL;
1242 if (aAltKey) {
1243 mModifiers |= MODIFIER_ALT;
1245 if (aShiftKey) {
1246 mModifiers |= MODIFIER_SHIFT;
1248 if (aMetaKey) {
1249 mModifiers |= MODIFIER_META;
1253 Modifiers mModifiers;
1255 void AssignInputEventData(const WidgetInputEvent& aEvent, bool aCopyTargets) {
1256 AssignGUIEventData(aEvent, aCopyTargets);
1258 mModifiers = aEvent.mModifiers;
1262 /******************************************************************************
1263 * mozilla::InternalUIEvent
1265 * XXX Why this inherits WidgetGUIEvent rather than WidgetEvent?
1266 ******************************************************************************/
1268 class InternalUIEvent : public WidgetGUIEvent {
1269 protected:
1270 InternalUIEvent() : mDetail(0), mCausedByUntrustedEvent(false) {}
1272 InternalUIEvent(bool aIsTrusted, EventMessage aMessage, nsIWidget* aWidget,
1273 EventClassID aEventClassID)
1274 : WidgetGUIEvent(aIsTrusted, aMessage, aWidget, aEventClassID),
1275 mDetail(0),
1276 mCausedByUntrustedEvent(false) {}
1278 InternalUIEvent(bool aIsTrusted, EventMessage aMessage,
1279 EventClassID aEventClassID)
1280 : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, aEventClassID),
1281 mDetail(0),
1282 mCausedByUntrustedEvent(false) {}
1284 public:
1285 virtual InternalUIEvent* AsUIEvent() override { return this; }
1288 * If the UIEvent is caused by another event (e.g., click event),
1289 * aEventCausesThisEvent should be the event. If there is no such event,
1290 * this should be nullptr.
1292 InternalUIEvent(bool aIsTrusted, EventMessage aMessage,
1293 const WidgetEvent* aEventCausesThisEvent)
1294 : WidgetGUIEvent(aIsTrusted, aMessage, nullptr, eUIEventClass),
1295 mDetail(0),
1296 mCausedByUntrustedEvent(aEventCausesThisEvent &&
1297 !aEventCausesThisEvent->IsTrusted()) {}
1299 virtual WidgetEvent* Duplicate() const override {
1300 MOZ_ASSERT(mClass == eUIEventClass,
1301 "Duplicate() must be overridden by sub class");
1302 InternalUIEvent* result = new InternalUIEvent(false, mMessage, nullptr);
1303 result->AssignUIEventData(*this, true);
1304 result->mFlags = mFlags;
1305 return result;
1308 int32_t mDetail;
1309 // mCausedByUntrustedEvent is true if the event is caused by untrusted event.
1310 bool mCausedByUntrustedEvent;
1312 // If you check the event is a trusted event and NOT caused by an untrusted
1313 // event, IsTrustable() returns what you expected.
1314 bool IsTrustable() const { return IsTrusted() && !mCausedByUntrustedEvent; }
1316 void AssignUIEventData(const InternalUIEvent& aEvent, bool aCopyTargets) {
1317 AssignGUIEventData(aEvent, aCopyTargets);
1319 mDetail = aEvent.mDetail;
1320 mCausedByUntrustedEvent = aEvent.mCausedByUntrustedEvent;
1324 } // namespace mozilla
1326 #endif // mozilla_BasicEvents_h__