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
{
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
;
35 bool UserActivation::IsHandlingUserInput() { return sUserInputEventDepth
> 0; }
38 bool UserActivation::IsHandlingKeyboardInput() {
39 return sUserKeyboardEventDepth
> 0;
43 bool UserActivation::IsUserInteractionEvent(const WidgetEvent
* aEvent
) {
44 if (!aEvent
->IsTrusted()) {
48 switch (aEvent
->mMessage
) {
49 // eKeyboardEventClass
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
56 return aEvent
->AsKeyboardEvent()->CanTreatAsUserInput();
75 void UserActivation::StartHandlingUserInput(EventMessage aMessage
) {
76 ++sUserInputEventDepth
;
77 if (sUserInputEventDepth
== 1) {
78 sLatestUserInputStart
= sHandlingInputStart
= TimeStamp::Now();
80 if (WidgetEvent::IsKeyEventMessage(aMessage
)) {
81 ++sUserKeyboardEventDepth
;
86 void UserActivation::StopHandlingUserInput(EventMessage aMessage
) {
87 --sUserInputEventDepth
;
88 if (sUserInputEventDepth
== 0) {
89 sHandlingInputStart
= TimeStamp();
91 if (WidgetEvent::IsKeyEventMessage(aMessage
)) {
92 --sUserKeyboardEventDepth
;
97 TimeStamp
UserActivation::GetHandlingInputStart() {
98 return sHandlingInputStart
;
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
) {
117 UserActivation::StartHandlingUserInput(mMessage
);
120 AutoHandlingUserInputStatePusher::~AutoHandlingUserInputStatePusher() {
121 if (!mIsHandlingUserInput
) {
124 UserActivation::StopHandlingUserInput(mMessage
);
127 } // namespace mozilla::dom