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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/* -*- 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_AccessibleCaretEventHub_h
#define mozilla_AccessibleCaretEventHub_h
#include "LayoutConstants.h"
#include "mozilla/EventForwards.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/WeakPtr.h"
#include "nsCOMPtr.h"
#include "nsDocShell.h"
#include "nsIReflowObserver.h"
#include "nsIScrollObserver.h"
#include "nsPoint.h"
#include "mozilla/RefPtr.h"
#include "nsWeakReference.h"
class nsITimer;
namespace mozilla {
class AccessibleCaretManager;
class PresShell;
class WidgetKeyboardEvent;
class WidgetMouseEvent;
class WidgetTouchEvent;
// -----------------------------------------------------------------------------
// Each PresShell holds a shared pointer to an AccessibleCaretEventHub; each
// AccessibleCaretEventHub holds a unique pointer to an AccessibleCaretManager.
// Thus, there's one AccessibleCaretManager per PresShell.
//
// AccessibleCaretEventHub implements a state pattern. It receives events from
// PresShell and callbacks by observers and listeners, and then relays them to
// the current concrete state which calls necessary event-handling methods in
// AccessibleCaretManager.
//
// We separate AccessibleCaretEventHub from AccessibleCaretManager to make the
// state transitions in AccessibleCaretEventHub testable. We put (nearly) all
// the operations involving PresShell, Selection, and AccessibleCaret
// manipulation in AccessibleCaretManager so that we can mock methods in
// AccessibleCaretManager in gtest. We test the correctness of the state
// transitions by giving events, callbacks, and the return values by mocked
// methods of AccessibleCaretEventHub. See TestAccessibleCaretEventHub.cpp.
//
// Besides dealing with real events, AccessibleCaretEventHub could also
// synthesize fake long-tap events and inject those events to itself on the
// platform lacks eMouseLongTap. Turn on this preference
// "layout.accessiblecaret.use_long_tap_injector" for the fake long-tap events.
//
// Please see the in-tree document for state transition diagram and more
// information.
// HTML: https://firefox-source-docs.mozilla.org/layout/AccessibleCaret.html
// Source rst: layout/docs/AccessibleCaret.rst
//
class AccessibleCaretEventHub : public nsIReflowObserver,
public nsIScrollObserver,
public nsSupportsWeakReference {
public:
explicit AccessibleCaretEventHub(PresShell* aPresShell);
void Init();
void Terminate();
MOZ_CAN_RUN_SCRIPT
nsEventStatus HandleEvent(WidgetEvent* aEvent);
// Call this function to notify the blur event happened.
MOZ_CAN_RUN_SCRIPT
void NotifyBlur(bool aIsLeavingDocument);
NS_DECL_ISUPPORTS
// nsIReflowObserver
MOZ_CAN_RUN_SCRIPT_BOUNDARY
NS_IMETHOD Reflow(DOMHighResTimeStamp start, DOMHighResTimeStamp end) final;
MOZ_CAN_RUN_SCRIPT_BOUNDARY
NS_IMETHOD ReflowInterruptible(DOMHighResTimeStamp start,
DOMHighResTimeStamp end) final;
// Override nsIScrollObserver methods.
MOZ_CAN_RUN_SCRIPT_BOUNDARY
virtual void ScrollPositionChanged() override;
MOZ_CAN_RUN_SCRIPT
virtual void AsyncPanZoomStarted() override;
MOZ_CAN_RUN_SCRIPT
virtual void AsyncPanZoomStopped() override;
// Base state
class State;
State* GetState() const;
MOZ_CAN_RUN_SCRIPT
void OnSelectionChange(dom::Document* aDocument, dom::Selection* aSelection,
int16_t aReason);
bool ShouldDisableApz() const;
protected:
virtual ~AccessibleCaretEventHub() = default;
#define MOZ_DECL_STATE_CLASS_GETTER(aClassName) \
class aClassName; \
static State* aClassName();
#define MOZ_IMPL_STATE_CLASS_GETTER(aClassName) \
AccessibleCaretEventHub::State* AccessibleCaretEventHub::aClassName() { \
static class aClassName singleton; \
return &singleton; \
}
// Concrete state getters
MOZ_DECL_STATE_CLASS_GETTER(NoActionState)
MOZ_DECL_STATE_CLASS_GETTER(PressCaretState)
MOZ_DECL_STATE_CLASS_GETTER(DragCaretState)
MOZ_DECL_STATE_CLASS_GETTER(PressNoCaretState)
MOZ_DECL_STATE_CLASS_GETTER(ScrollState)
MOZ_DECL_STATE_CLASS_GETTER(LongTapState)
void SetState(State* aState);
MOZ_CAN_RUN_SCRIPT
nsEventStatus HandleMouseEvent(WidgetMouseEvent* aEvent);
MOZ_CAN_RUN_SCRIPT
nsEventStatus HandleTouchEvent(WidgetTouchEvent* aEvent);
MOZ_CAN_RUN_SCRIPT
nsEventStatus HandleKeyboardEvent(WidgetKeyboardEvent* aEvent);
virtual nsPoint GetTouchEventPosition(WidgetTouchEvent* aEvent,
int32_t aIdentifier) const;
virtual nsPoint GetMouseEventPosition(WidgetMouseEvent* aEvent) const;
bool MoveDistanceIsLarge(const nsPoint& aPoint) const;
void LaunchLongTapInjector();
void CancelLongTapInjector();
MOZ_CAN_RUN_SCRIPT_BOUNDARY
static void FireLongTap(nsITimer* aTimer, void* aAccessibleCaretEventHub);
void LaunchScrollEndInjector();
void CancelScrollEndInjector();
MOZ_CAN_RUN_SCRIPT_BOUNDARY
static void FireScrollEnd(nsITimer* aTimer, void* aAccessibleCaretEventHub);
// Member variables
State* mState = NoActionState();
// Will be set to nullptr in Terminate().
PresShell* MOZ_NON_OWNING_REF mPresShell = nullptr;
UniquePtr<AccessibleCaretManager> mManager;
WeakPtr<nsDocShell> mDocShell;
// Use this timer for injecting a long tap event when APZ is disabled. If APZ
// is enabled, it will send long tap event to us.
nsCOMPtr<nsITimer> mLongTapInjectorTimer;
// Last mouse button down event or touch start event point.
nsPoint mPressPoint{NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE};
// For filter multitouch event
int32_t mActiveTouchId = kInvalidTouchId;
// Flag to indicate the class has been initialized.
bool mInitialized = false;
// Flag to avoid calling Reflow() callback recursively.
bool mIsInReflowCallback = false;
static const int32_t kMoveStartToleranceInPixel = 5;
static const int32_t kInvalidTouchId = -1;
static const int32_t kDefaultTouchId = 0; // For mouse event
};
// -----------------------------------------------------------------------------
// The base class for concrete states. A concrete state should inherit from this
// class, and override the methods to handle the events or callbacks. A concrete
// state is also responsible for transforming itself to the next concrete state.
//
class AccessibleCaretEventHub::State {
public:
virtual const char* Name() const { return ""; }
MOZ_CAN_RUN_SCRIPT
virtual nsEventStatus OnPress(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint, int32_t aTouchId,
EventClassID aEventClass) {
return nsEventStatus_eIgnore;
}
MOZ_CAN_RUN_SCRIPT
virtual nsEventStatus OnMove(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint,
WidgetMouseEvent::Reason aReason) {
return nsEventStatus_eIgnore;
}
MOZ_CAN_RUN_SCRIPT
virtual nsEventStatus OnRelease(AccessibleCaretEventHub* aContext) {
return nsEventStatus_eIgnore;
}
MOZ_CAN_RUN_SCRIPT
virtual nsEventStatus OnLongTap(AccessibleCaretEventHub* aContext,
const nsPoint& aPoint) {
return nsEventStatus_eIgnore;
}
MOZ_CAN_RUN_SCRIPT
virtual void OnScrollStart(AccessibleCaretEventHub* aContext) {}
MOZ_CAN_RUN_SCRIPT
virtual void OnScrollEnd(AccessibleCaretEventHub* aContext) {}
MOZ_CAN_RUN_SCRIPT
virtual void OnScrollPositionChanged(AccessibleCaretEventHub* aContext) {}
MOZ_CAN_RUN_SCRIPT
virtual void OnBlur(AccessibleCaretEventHub* aContext,
bool aIsLeavingDocument) {}
MOZ_CAN_RUN_SCRIPT
virtual void OnSelectionChanged(AccessibleCaretEventHub* aContext,
dom::Document* aDoc, dom::Selection* aSel,
int16_t aReason) {}
MOZ_CAN_RUN_SCRIPT
virtual void OnReflow(AccessibleCaretEventHub* aContext) {}
virtual void Enter(AccessibleCaretEventHub* aContext) {}
virtual void Leave(AccessibleCaretEventHub* aContext) {}
explicit State() = default;
virtual ~State() = default;
State(const State&) = delete;
State& operator=(const State&) = delete;
};
} // namespace mozilla
#endif // mozilla_AccessibleCaretEventHub_h
|