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/BrowsingContext.h"
8 #include "mozilla/dom/UserActivation.h"
9 #include "mozilla/dom/UserActivationBinding.h"
10 #include "mozilla/dom/WindowGlobalChild.h"
12 #include "mozilla/TextEvents.h"
14 namespace mozilla::dom
{
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(UserActivation
, mWindow
)
17 NS_IMPL_CYCLE_COLLECTING_ADDREF(UserActivation
)
18 NS_IMPL_CYCLE_COLLECTING_RELEASE(UserActivation
)
20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UserActivation
)
21 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
22 NS_INTERFACE_MAP_ENTRY(nsISupports
)
25 UserActivation::UserActivation(nsPIDOMWindowInner
* aWindow
)
28 JSObject
* UserActivation::WrapObject(JSContext
* aCx
,
29 JS::Handle
<JSObject
*> aGivenProto
) {
30 return UserActivation_Binding::Wrap(aCx
, this, aGivenProto
);
33 // https://html.spec.whatwg.org/multipage/interaction.html#dom-useractivation-hasbeenactive
34 bool UserActivation::HasBeenActive() const {
35 // The hasBeenActive getter steps are to return true if this's relevant global
36 // object has sticky activation, and false otherwise.
38 WindowContext
* wc
= mWindow
->GetWindowContext();
39 return wc
&& wc
->HasBeenUserGestureActivated();
42 // https://html.spec.whatwg.org/multipage/interaction.html#dom-useractivation-isactive
43 bool UserActivation::IsActive() const {
44 // The isActive getter steps are to return true if this's relevant global
45 // object has transient activation, and false otherwise.
47 WindowContext
* wc
= mWindow
->GetWindowContext();
48 return wc
&& wc
->HasValidTransientUserGestureActivation();
53 // The current depth of user and keyboard inputs. sUserInputEventDepth
54 // is the number of any user input events, page load events and mouse over
55 // events. sUserKeyboardEventDepth is the number of keyboard input events.
56 // Incremented whenever we start handling a user input, decremented when we
57 // have finished handling a user input. This depth is *not* reset in case
58 // of nested event loops.
59 static int32_t sUserInputEventDepth
= 0;
60 static int32_t sUserKeyboardEventDepth
= 0;
62 // Time at which we began handling user input. Reset to the epoch
63 // once we have finished handling user input.
64 static TimeStamp sHandlingInputStart
;
66 // Time at which we began handling the latest user input. Not reset
67 // at the end of the input.
68 static TimeStamp sLatestUserInputStart
;
73 bool UserActivation::IsHandlingUserInput() { return sUserInputEventDepth
> 0; }
76 bool UserActivation::IsHandlingKeyboardInput() {
77 return sUserKeyboardEventDepth
> 0;
81 bool UserActivation::IsUserInteractionEvent(const WidgetEvent
* aEvent
) {
82 if (!aEvent
->IsTrusted()) {
86 switch (aEvent
->mMessage
) {
87 // eKeyboardEventClass
91 // Not all keyboard events are treated as user input, so that popups
92 // can't be opened, fullscreen mode can't be started, etc at
94 return aEvent
->AsKeyboardEvent()->CanTreatAsUserInput();
100 // ePointerEventClass
113 void UserActivation::StartHandlingUserInput(EventMessage aMessage
) {
114 ++sUserInputEventDepth
;
115 if (sUserInputEventDepth
== 1) {
116 sLatestUserInputStart
= sHandlingInputStart
= TimeStamp::Now();
118 if (WidgetEvent::IsKeyEventMessage(aMessage
)) {
119 ++sUserKeyboardEventDepth
;
124 void UserActivation::StopHandlingUserInput(EventMessage aMessage
) {
125 --sUserInputEventDepth
;
126 if (sUserInputEventDepth
== 0) {
127 sHandlingInputStart
= TimeStamp();
129 if (WidgetEvent::IsKeyEventMessage(aMessage
)) {
130 --sUserKeyboardEventDepth
;
135 TimeStamp
UserActivation::GetHandlingInputStart() {
136 return sHandlingInputStart
;
140 TimeStamp
UserActivation::LatestUserInputStart() {
141 return sLatestUserInputStart
;
144 //-----------------------------------------------------------------------------
145 // mozilla::dom::AutoHandlingUserInputStatePusher
146 //-----------------------------------------------------------------------------
148 AutoHandlingUserInputStatePusher::AutoHandlingUserInputStatePusher(
149 bool aIsHandlingUserInput
, WidgetEvent
* aEvent
)
150 : mMessage(aEvent
? aEvent
->mMessage
: eVoidEvent
),
151 mIsHandlingUserInput(aIsHandlingUserInput
) {
152 if (!aIsHandlingUserInput
) {
155 UserActivation::StartHandlingUserInput(mMessage
);
158 AutoHandlingUserInputStatePusher::~AutoHandlingUserInputStatePusher() {
159 if (!mIsHandlingUserInput
) {
162 UserActivation::StopHandlingUserInput(mMessage
);
165 } // namespace mozilla::dom