Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / base / UserActivation.cpp
blob95dc5fc7d0ecbc0e921cd5279f8232c788404654
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/UserActivation.h"
9 #include "mozilla/TextEvents.h"
11 namespace mozilla::dom {
13 namespace {
15 // The current depth of user and keyboard inputs. sUserInputEventDepth
16 // is the number of any user input events, page load events and mouse over
17 // events. sUserKeyboardEventDepth is the number of keyboard input events.
18 // Incremented whenever we start handling a user input, decremented when we
19 // have finished handling a user input. This depth is *not* reset in case
20 // of nested event loops.
21 static int32_t sUserInputEventDepth = 0;
22 static int32_t sUserKeyboardEventDepth = 0;
24 // Time at which we began handling user input. Reset to the epoch
25 // once we have finished handling user input.
26 static TimeStamp sHandlingInputStart;
28 // Time at which we began handling the latest user input. Not reset
29 // at the end of the input.
30 static TimeStamp sLatestUserInputStart;
32 } // namespace
34 /* static */
35 bool UserActivation::IsHandlingUserInput() { return sUserInputEventDepth > 0; }
37 /* static */
38 bool UserActivation::IsHandlingKeyboardInput() {
39 return sUserKeyboardEventDepth > 0;
42 /* static */
43 bool UserActivation::IsUserInteractionEvent(const WidgetEvent* aEvent) {
44 if (!aEvent->IsTrusted()) {
45 return false;
48 switch (aEvent->mMessage) {
49 // eKeyboardEventClass
50 case eKeyPress:
51 case eKeyDown:
52 case eKeyUp:
53 // Not all keyboard events are treated as user input, so that popups
54 // can't be opened, fullscreen mode can't be started, etc at
55 // unexpected time.
56 return aEvent->AsKeyboardEvent()->CanTreatAsUserInput();
57 // eBasicEventClass
58 // eMouseEventClass
59 case eMouseClick:
60 case eMouseDown:
61 case eMouseUp:
62 // ePointerEventClass
63 case ePointerDown:
64 case ePointerUp:
65 // eTouchEventClass
66 case eTouchStart:
67 case eTouchEnd:
68 return true;
69 default:
70 return false;
74 /* static */
75 void UserActivation::StartHandlingUserInput(EventMessage aMessage) {
76 ++sUserInputEventDepth;
77 if (sUserInputEventDepth == 1) {
78 sLatestUserInputStart = sHandlingInputStart = TimeStamp::Now();
80 if (WidgetEvent::IsKeyEventMessage(aMessage)) {
81 ++sUserKeyboardEventDepth;
85 /* static */
86 void UserActivation::StopHandlingUserInput(EventMessage aMessage) {
87 --sUserInputEventDepth;
88 if (sUserInputEventDepth == 0) {
89 sHandlingInputStart = TimeStamp();
91 if (WidgetEvent::IsKeyEventMessage(aMessage)) {
92 --sUserKeyboardEventDepth;
96 /* static */
97 TimeStamp UserActivation::GetHandlingInputStart() {
98 return sHandlingInputStart;
101 /* static */
102 TimeStamp UserActivation::LatestUserInputStart() {
103 return sLatestUserInputStart;
106 //-----------------------------------------------------------------------------
107 // mozilla::dom::AutoHandlingUserInputStatePusher
108 //-----------------------------------------------------------------------------
110 AutoHandlingUserInputStatePusher::AutoHandlingUserInputStatePusher(
111 bool aIsHandlingUserInput, WidgetEvent* aEvent)
112 : mMessage(aEvent ? aEvent->mMessage : eVoidEvent),
113 mIsHandlingUserInput(aIsHandlingUserInput) {
114 if (!aIsHandlingUserInput) {
115 return;
117 UserActivation::StartHandlingUserInput(mMessage);
120 AutoHandlingUserInputStatePusher::~AutoHandlingUserInputStatePusher() {
121 if (!mIsHandlingUserInput) {
122 return;
124 UserActivation::StopHandlingUserInput(mMessage);
127 } // namespace mozilla::dom