summaryrefslogtreecommitdiffstats
path: root/dom/base/UserActivation.h
blob: 1ebdddccfa6fa49cd119dc51fbe6ca00de9f8618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_UserActivation_h
#define mozilla_dom_UserActivation_h

#include "mozilla/Assertions.h"
#include "mozilla/EventForwards.h"
#include "mozilla/TimeStamp.h"
#include "nsCycleCollectionParticipant.h"
#include "nsWrapperCache.h"
#include "nsPIDOMWindow.h"

namespace IPC {
template <class P>
struct ParamTraits;
}  // namespace IPC

namespace mozilla::dom {

class UserActivation final : public nsISupports, public nsWrapperCache {
 public:
  // WebIDL UserActivation

  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(UserActivation)

  explicit UserActivation(nsPIDOMWindowInner* aWindow);

  nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
  JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) final;

  bool HasBeenActive() const;
  bool IsActive() const;

  // End of WebIDL UserActivation

  enum class State : uint8_t {
    // Not activated.
    None,
    // It is considered as has-been-activated, but not transient-activated given
    // that it is being consumed.
    HasBeenActivated,
    // It is considered as has-been-activated, and also transient-activated if
    // haven't timed out.
    FullActivated,
    EndGuard_
  };

  class StateAndModifiers;

  // Modifier keys held while the user activation.
  class Modifiers {
   public:
    static constexpr uint8_t Shift = 0x10;
    static constexpr uint8_t Meta = 0x20;
    static constexpr uint8_t Control = 0x40;
    static constexpr uint8_t Alt = 0x80;

    static constexpr uint8_t Mask = 0xF0;

    constexpr Modifiers() = default;
    explicit constexpr Modifiers(uint8_t aModifiers) : mModifiers(aModifiers) {}

    static constexpr Modifiers None() { return Modifiers(0); }

    void SetShift() { mModifiers |= Shift; }
    void SetMeta() { mModifiers |= Meta; }
    void SetControl() { mModifiers |= Control; }
    void SetAlt() { mModifiers |= Alt; }

    bool IsShift() const { return mModifiers & Shift; }
    bool IsMeta() const { return mModifiers & Meta; }
    bool IsControl() const { return mModifiers & Control; }
    bool IsAlt() const { return mModifiers & Alt; }

   private:
    uint8_t mModifiers = 0;

    friend class StateAndModifiers;
    template <class P>
    friend struct IPC::ParamTraits;
  };

  // State and Modifiers encoded into single data, for WindowContext field.
  class StateAndModifiers {
   public:
    using DataT = uint8_t;

    constexpr StateAndModifiers() = default;
    explicit constexpr StateAndModifiers(DataT aStateAndModifiers)
        : mStateAndModifiers(aStateAndModifiers) {}

    DataT GetRawData() const { return mStateAndModifiers; }

    State GetState() const { return State(RawState()); }
    void SetState(State aState) {
      MOZ_ASSERT((uint8_t(aState) & Modifiers::Mask) == 0);
      mStateAndModifiers = uint8_t(aState) | RawModifiers();
    }

    Modifiers GetModifiers() const { return Modifiers(RawModifiers()); }
    void SetModifiers(Modifiers aModifiers) {
      mStateAndModifiers = RawState() | aModifiers.mModifiers;
    }

   private:
    uint8_t RawState() const { return mStateAndModifiers & ~Modifiers::Mask; }

    uint8_t RawModifiers() const {
      return mStateAndModifiers & Modifiers::Mask;
    }

    uint8_t mStateAndModifiers = 0;
  };

  /**
   * Returns true if the current code is being executed as a result of
   * user input or keyboard input.  The former includes anything that is
   * initiated by user, with the exception of page load events or mouse
   * over events.  And the latter returns true when one of the user inputs
   * is an input from keyboard.  If these methods are called from asynchronously
   * executed code, such as during layout reflows, it will return false.
   */
  static bool IsHandlingUserInput();
  static bool IsHandlingKeyboardInput();

  /**
   * Returns true if the event is considered as user interaction event. I.e.,
   * enough obvious input to allow to open popup, etc. Otherwise, returns false.
   */
  static bool IsUserInteractionEvent(const WidgetEvent* aEvent);

  /**
   * StartHandlingUserInput() is called when we start to handle a user input.
   * StopHandlingUserInput() is called when we finish handling a user input.
   * If the caller knows which input event caused that, it should set
   * aMessage to the event message.  Otherwise, set eVoidEvent.
   * Note that StopHandlingUserInput() caller should call it with exactly same
   * event message as its corresponding StartHandlingUserInput() call because
   * these methods may count the number of specific event message.
   */
  static void StartHandlingUserInput(EventMessage aMessage);
  static void StopHandlingUserInput(EventMessage aMessage);

  static TimeStamp GetHandlingInputStart();

  /**
   * Get the timestamp at which the latest user input was handled.
   *
   * Guaranteed to be monotonic. Until the first user input, return
   * the epoch.
   */
  static TimeStamp LatestUserInputStart();

 private:
  ~UserActivation() = default;

  nsCOMPtr<nsPIDOMWindowInner> mWindow;
};

/**
 * This class is used while processing real user input. During this time, popups
 * are allowed. For mousedown events, mouse capturing is also permitted.
 */
class MOZ_RAII AutoHandlingUserInputStatePusher final {
 public:
  explicit AutoHandlingUserInputStatePusher(bool aIsHandlingUserInput,
                                            WidgetEvent* aEvent = nullptr);
  ~AutoHandlingUserInputStatePusher();

 protected:
  EventMessage mMessage;
  bool mIsHandlingUserInput;
};

}  // namespace mozilla::dom

#endif  // mozilla_dom_UserActivation_h