Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/events/PointerEventHandler.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef mozilla_PointerEventHandler_h
8
#define mozilla_PointerEventHandler_h
9
10
#include "mozilla/EventForwards.h"
11
#include "mozilla/MouseEvents.h"
12
#include "mozilla/TouchEvents.h"
13
14
class nsIFrame;
15
class nsIContent;
16
17
namespace mozilla {
18
19
class PresShell;
20
21
class PointerCaptureInfo final
22
{
23
public:
24
  nsCOMPtr<nsIContent> mPendingContent;
25
  nsCOMPtr<nsIContent> mOverrideContent;
26
27
  explicit PointerCaptureInfo(nsIContent* aPendingContent)
28
    : mPendingContent(aPendingContent)
29
0
  {
30
0
    MOZ_COUNT_CTOR(PointerCaptureInfo);
31
0
  }
32
33
  ~PointerCaptureInfo()
34
0
  {
35
0
    MOZ_COUNT_DTOR(PointerCaptureInfo);
36
0
  }
37
38
  bool Empty()
39
0
  {
40
0
    return !(mPendingContent || mOverrideContent);
41
0
  }
42
};
43
44
class PointerEventHandler final
45
{
46
public:
47
  // Called in PresShell::Initialize to initialize pointer event related
48
  // preferences.
49
  static void Initialize();
50
51
  // Called in nsLayoutStatics::Initialize/Shutdown to initialize pointer event
52
  // related static variables.
53
  static void InitializeStatics();
54
  static void ReleaseStatics();
55
56
  // Return the preference value of pointer event enabled.
57
  static bool IsPointerEventEnabled();
58
59
  // Return the preference value of implicit capture.
60
  static bool IsPointerEventImplicitCaptureForTouchEnabled();
61
62
  // Called in ESM::PreHandleEvent to update current active pointers in a hash
63
  // table.
64
  static void UpdateActivePointerState(WidgetMouseEvent* aEvent);
65
66
  // Got/release pointer capture of the specified pointer by the content.
67
  static void SetPointerCaptureById(uint32_t aPointerId, nsIContent* aContent);
68
  static void ReleasePointerCaptureById(uint32_t aPointerId);
69
  static void ReleaseAllPointerCapture();
70
71
  // Get the pointer captured info of the specified pointer.
72
  static PointerCaptureInfo* GetPointerCaptureInfo(uint32_t aPointerId);
73
74
  // GetPointerInfo returns true if pointer with aPointerId is situated in
75
  // device, false otherwise.
76
  // aActiveState is additional information, which shows state of pointer like
77
  // button state for mouse.
78
  static bool GetPointerInfo(uint32_t aPointerId, bool& aActiveState);
79
80
  // CheckPointerCaptureState checks cases, when got/lostpointercapture events
81
  // should be fired.
82
  static void MaybeProcessPointerCapture(WidgetGUIEvent* aEvent);
83
  static void ProcessPointerCaptureForMouse(WidgetMouseEvent* aEvent);
84
  static void ProcessPointerCaptureForTouch(WidgetTouchEvent* aEvent);
85
  static void CheckPointerCaptureState(WidgetPointerEvent* aEvent);
86
87
  // Implicitly get and release capture of current pointer for touch.
88
  static void ImplicitlyCapturePointer(nsIFrame* aFrame, WidgetEvent* aEvent);
89
  static void ImplicitlyReleasePointerCapture(WidgetEvent* aEvent);
90
91
  /**
92
   * GetPointerCapturingContent returns a target content which captures the
93
   * pointer. It's applied to mouse or pointer event (except mousedown and
94
   * pointerdown). When capturing, return the content. Otherwise, nullptr.
95
   *
96
   * @param aEvent               A mouse event or pointer event which may be
97
   *                             captured.
98
   *
99
   * @return                     Target content for aEvent.
100
   */
101
  static nsIContent* GetPointerCapturingContent(WidgetGUIEvent* aEvent);
102
103
  static nsIContent* GetPointerCapturingContent(uint32_t aPointerId);
104
105
  // Release pointer capture if captured by the specified content or it's
106
  // descendant. This is called to handle the case that the pointer capturing
107
  // content or it's parent is removed from the document.
108
  static void ReleaseIfCaptureByDescendant(nsIContent* aContent);
109
110
  /*
111
   * This function handles the case when content had called preventDefault on
112
   * the active pointer. In that case we have to prevent firing subsequent mouse
113
   * to content. We check the flag PointerInfo::mPreventMouseEventByContent and
114
   * call PreventDefault(false) to stop default behaviors and stop firing mouse
115
   * events to content and chrome.
116
   *
117
   * note: mouse transition events are excluded
118
   * note: we have to clean mPreventMouseEventByContent on pointerup for those
119
   *       devices support hover
120
   * note: we don't suppress firing mouse events to chrome and system group
121
   *       handlers because they may implement default behaviors
122
   */
123
  static void PreHandlePointerEventsPreventDefault(
124
                WidgetPointerEvent* aPointerEvent,
125
                WidgetGUIEvent* aMouseOrTouchEvent);
126
127
  /*
128
   * This function handles the preventDefault behavior of pointerdown. When user
129
   * preventDefault on pointerdown, We have to mark the active pointer to
130
   * prevent sebsequent mouse events (except mouse transition events) and
131
   * default behaviors.
132
   *
133
   * We add mPreventMouseEventByContent flag in PointerInfo to represent the
134
   * active pointer won't firing compatible mouse events. It's set to true when
135
   * content preventDefault on pointerdown
136
   */
137
  static void PostHandlePointerEventsPreventDefault(
138
                WidgetPointerEvent* aPointerEvent,
139
                WidgetGUIEvent* aMouseOrTouchEvent);
140
141
  MOZ_CAN_RUN_SCRIPT
142
  static void DispatchPointerFromMouseOrTouch(PresShell* aShell,
143
                                              nsIFrame* aFrame,
144
                                              nsIContent* aContent,
145
                                              WidgetGUIEvent* aEvent,
146
                                              bool aDontRetargetEvents,
147
                                              nsEventStatus* aStatus,
148
                                              nsIContent** aTargetContent);
149
150
  static void InitPointerEventFromMouse(WidgetPointerEvent* aPointerEvent,
151
                                        WidgetMouseEvent* aMouseEvent,
152
                                        EventMessage aMessage);
153
154
  static void InitPointerEventFromTouch(WidgetPointerEvent* aPointerEvent,
155
                                        WidgetTouchEvent* aTouchEvent,
156
                                        mozilla::dom::Touch* aTouch,
157
                                        bool aIsPrimary);
158
159
  static bool ShouldGeneratePointerEventFromMouse(WidgetGUIEvent* aEvent)
160
0
  {
161
0
    return aEvent->mMessage == eMouseDown || aEvent->mMessage == eMouseUp ||
162
0
           aEvent->mMessage == eMouseMove;
163
0
  }
164
165
  static bool ShouldGeneratePointerEventFromTouch(WidgetGUIEvent* aEvent)
166
0
  {
167
0
    return aEvent->mMessage == eTouchStart || aEvent->mMessage == eTouchMove ||
168
0
           aEvent->mMessage == eTouchEnd || aEvent->mMessage == eTouchCancel ||
169
0
           aEvent->mMessage == eTouchPointerCancel;
170
0
  }
171
172
private:
173
  // GetPointerType returns pointer type like mouse, pen or touch for pointer
174
  // event with pointerId. The return value must be one of
175
  // MouseEvent_Binding::MOZ_SOURCE_*
176
  static uint16_t GetPointerType(uint32_t aPointerId);
177
178
  // GetPointerPrimaryState returns state of attribute isPrimary for pointer
179
  // event with pointerId
180
  static bool GetPointerPrimaryState(uint32_t aPointerId);
181
182
  static void DispatchGotOrLostPointerCaptureEvent(
183
                bool aIsGotCapture,
184
                const WidgetPointerEvent* aPointerEvent,
185
                nsIContent* aCaptureTarget);
186
};
187
188
} // namespace mozilla
189
190
#endif // mozilla_PointerEventHandler_h