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
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326

content / browser / renderer_host / render_widget_host_view_event_handler.h [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_EVENT_HANDLER_H_
#define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_EVENT_HANDLER_H_

#include <memory>
#include <optional>

#include "base/containers/flat_set.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "components/input/native_web_keyboard_event.h"
#include "content/browser/renderer_host/input/mouse_wheel_phase_handler.h"
#include "content/common/content_export.h"
#include "third_party/blink/public/mojom/input/pointer_lock_result.mojom.h"
#include "ui/aura/scoped_enable_unadjusted_mouse_events.h"
#include "ui/aura/scoped_keyboard_hook.h"
#include "ui/events/event_handler.h"
#include "ui/events/gestures/motion_event_aura.h"
#include "ui/latency/latency_info.h"

namespace aura {
class Window;
}  // namespace aura

namespace blink {
class WebMouseEvent;
class WebMouseWheelEvent;
class WebTouchEvent;
}  // namespace blink

namespace ui {
enum class DomCode : uint32_t;
class TextInputClient;
class TouchSelectionController;
}

namespace content {

struct ContextMenuParams;
class OverscrollController;
class RenderWidgetHostImpl;
class RenderWidgetHostViewBase;
class TouchSelectionControllerClientAura;

// Provides an implementation of ui::EventHandler for use with
// RenderWidgetHostViewBase. A delegate is required in order to provide platform
// specific functionality.
//
// After processing events they will be forwarded to the provided
// RenderWidgetHostImpl.
//
// This does not implement ui::TextInputClient, which some
// RenderWidgetHostViewBase classes do.
// RenderWidgetHostViewEventHandler::Delegate implementations may have
// overlapping functionality with the ui::TextInputClient.
class CONTENT_EXPORT RenderWidgetHostViewEventHandler
    : public ui::EventHandler {
 public:
  // An interface to provide platform specific logic needed for event handling.
  class Delegate {
   public:
    Delegate();

    Delegate(const Delegate&) = delete;
    Delegate& operator=(const Delegate&) = delete;

    // Converts |rect| from window coordinate to screen coordinate.
    virtual gfx::Rect ConvertRectToScreen(const gfx::Rect& rect) const = 0;
    // Call keybindings handler against the event and send matched edit commands
    // to the renderer instead. |update_event| (if non-null) is set to indicate
    // whether ui::KeyEvent::SetHandled() should be called on the underlying
    // ui::KeyEvent.
    virtual void ForwardKeyboardEventWithLatencyInfo(
        const input::NativeWebKeyboardEvent& event,
        const ui::LatencyInfo& latency,
        bool* update_event) = 0;
    // Returns whether the widget needs to grab mouse capture to work properly.
    virtual bool NeedsMouseCapture() = 0;
    virtual void SetTooltipsEnabled(bool enable) = 0;
    // Sends shutdown request.
    virtual void Shutdown() = 0;

    ui::TouchSelectionController* selection_controller() const {
      return selection_controller_.get();
    }

    TouchSelectionControllerClientAura* selection_controller_client() const {
      return selection_controller_client_.get();
    }

    OverscrollController* overscroll_controller() const {
      return overscroll_controller_.get();
    }

   protected:
    virtual ~Delegate();

    std::unique_ptr<TouchSelectionControllerClientAura>
        selection_controller_client_;
    std::unique_ptr<ui::TouchSelectionController> selection_controller_;
    std::unique_ptr<OverscrollController> overscroll_controller_;
  };

  RenderWidgetHostViewEventHandler(RenderWidgetHostImpl* host,
                                   RenderWidgetHostViewBase* host_view,
                                   Delegate* delegate);

  RenderWidgetHostViewEventHandler(const RenderWidgetHostViewEventHandler&) =
      delete;
  RenderWidgetHostViewEventHandler& operator=(
      const RenderWidgetHostViewEventHandler&) = delete;

  ~RenderWidgetHostViewEventHandler() override;

  // Set child popup's host view, and event handler, in order to redirect input.
  void SetPopupChild(RenderWidgetHostViewBase* popup_child_host_view,
                     ui::EventHandler* popup_child_event_handler);
  // Begin tracking a host window, such as when RenderWidgetHostViewBase is
  // fullscreen.
  void TrackHost(aura::Window* reference_window);

  MouseWheelPhaseHandler& mouse_wheel_phase_handler() {
    return mouse_wheel_phase_handler_;
  }

#if BUILDFLAG(IS_WIN)
  // Sets the ContextMenuParams when a context menu is triggered. Required for
  // subsequent event processing.
  void SetContextMenuParams(const ContextMenuParams& params);
#endif  // BUILDFLAG(IS_WIN)

  bool accept_return_character() { return accept_return_character_; }
  bool mouse_locked() { return mouse_locked_; }
  bool mouse_locked_unadjusted_movement() {
    return mouse_locked_ && mouse_locked_unadjusted_movement_;
  }
  const ui::MotionEventAura& pointer_state() const { return pointer_state_; }
  void set_focus_on_mouse_down_or_key_event(
      bool focus_on_mouse_down_or_key_event) {
    set_focus_on_mouse_down_or_key_event_ = focus_on_mouse_down_or_key_event;
  }
  void set_window(aura::Window* window) { window_ = window; }

  // Lock/Unlock processing of future mouse pointer events.
  blink::mojom::PointerLockResult LockPointer(bool request_unadjusted_movement);
  // Change the current lock to have the given unadjusted_movement.
  blink::mojom::PointerLockResult ChangePointerLock(
      bool request_unadjusted_movement);
  void UnlockPointer();

  // Start/Stop processing of future system keyboard events.
  bool LockKeyboard(std::optional<base::flat_set<ui::DomCode>> codes);
  void UnlockKeyboard();
  bool IsKeyboardLocked() const;

  // ui::EventHandler:
  void OnKeyEvent(ui::KeyEvent* event) override;
  void OnMouseEvent(ui::MouseEvent* event) override;
  void OnScrollEvent(ui::ScrollEvent* event) override;
  void OnTouchEvent(ui::TouchEvent* event) override;
  void OnGestureEvent(ui::GestureEvent* event) override;

  void GestureEventAck(const blink::WebGestureEvent& event,
                       blink::mojom::InputEventResultState ack_result);

  // Used to set the mouse_wheel_phase_handler_ timer timeout for testing.
  void set_mouse_wheel_wheel_phase_handler_timeout(base::TimeDelta timeout) {
    mouse_wheel_phase_handler_.set_mouse_wheel_end_dispatch_timeout(timeout);
  }

  // Used in testing for setting the max time to wait for momentum phase began
  // after a scroll phase end.
  void set_max_time_between_phase_ended_and_momentum_phase_began(
      base::TimeDelta timeout) {
    mouse_wheel_phase_handler_
        .set_max_time_between_phase_ended_and_momentum_phase_began(timeout);
  }

 private:
  FRIEND_TEST_ALL_PREFIXES(InputMethodResultAuraTest,
                           FinishImeCompositionSession);
  FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
                           KeyEventRoutingWithKeyboardLockActiveForOneKey);
  FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
                           KeyEventRoutingWithKeyboardLockActiveForEscKey);
  FRIEND_TEST_ALL_PREFIXES(RenderWidgetHostViewAuraTest,
                           KeyEventRoutingWithKeyboardLockActiveForAllKeys);
  FRIEND_TEST_ALL_PREFIXES(
      RenderWidgetHostViewAuraTest,
      KeyEventRoutingKeyboardLockAndChildPopupWithInputGrab);
  FRIEND_TEST_ALL_PREFIXES(
      RenderWidgetHostViewAuraTest,
      KeyEventRoutingKeyboardLockAndChildPopupWithoutInputGrab);
  friend class MockPointerLockRenderWidgetHostView;
  friend class FakeRenderWidgetHostViewAura;

  // Returns true if the |event| passed in can be forwarded to the renderer.
  bool CanRendererHandleEvent(const ui::MouseEvent* event,
                              bool mouse_locked,
                              bool selection_popup) const;

  // Confirm existing composition text in the webpage and ask the input method
  // to cancel its ongoing composition session.
  void FinishImeCompositionSession();

  // Forwards a mouse event to this view's parent window delegate.
  void ForwardMouseEventToParent(ui::MouseEvent* event);

  // Performs gesture handling needed for touch text selection. Sets event as
  // handled if it should not be further processed.
  void HandleGestureForTouchSelection(ui::GestureEvent* event);

  // Performs gesture ack handling needed for swipe-to-move-cursor gestures.
  void HandleSwipeToMoveCursorGestureAck(const blink::WebGestureEvent& event);

  // Handles mouse event handling while the mouse is locked via LockMouse.
  void HandleMouseEventWhileLocked(ui::MouseEvent* event);

  // This method computes movementX/Y and keeps track of mouse location for
  // mouse lock on all mouse move events.
  // |ui_mouse_event| contains the mouse event received.
  // |event| contains the WebMouseEvent being modified.
  void ModifyEventMovementAndCoords(const ui::MouseEvent& ui_mouse_event,
                                    blink::WebMouseEvent* event);

  // This method moves cursor to window center for pointer lock.
  // In Windows, a non-null |event| is used for creating the synthesize move to
  // update blink side states.
  void MoveCursorToCenter(ui::MouseEvent* event);

  // Helper function to set keyboard focus to the main window.
  void SetKeyboardFocus();

  // Helper method to determine if, in mouse locked mode, the cursor should be
  // moved to center.
  bool ShouldMoveToCenter(gfx::PointF mouse_screen_position);

  // Return whether the event is a synthesized move from |MoveCursorTo|.
  bool MatchesSynthesizedMovePosition(const blink::WebMouseEvent& event);

  // Returns true when we can hit test input events with location data to be
  // sent to the targeted RenderWidgetHost.
  bool ShouldRouteEvents() const;

  // Directs events to the |host_|.
  void ProcessMouseEvent(const blink::WebMouseEvent& event,
                         const ui::LatencyInfo& latency);
  void ProcessMouseWheelEvent(const blink::WebMouseWheelEvent& event,
                              const ui::LatencyInfo& latency);
  void ProcessTouchEvent(const blink::WebTouchEvent& event,
                         const ui::LatencyInfo& latency);

  // Returns true if event is a reserved key for an active KeyboardLock request.
  bool IsKeyLocked(const ui::KeyEvent& event);

  void HandleMouseWheelEvent(ui::MouseEvent* event);

  // Whether return characters should be passed on to the RenderWidgetHostImpl.
  bool accept_return_character_ = false;

  // Deactivates keyboard lock when destroyed.
  std::unique_ptr<aura::ScopedKeyboardHook> scoped_keyboard_hook_;

  // While the mouse is locked, the cursor is hidden from the user. Mouse events
  // are still generated. However, the position they report is the last known
  // mouse position just as mouse lock was entered; the movement they report
  // indicates what the change in position of the mouse would be had it not been
  // locked.
  bool mouse_locked_ = false;

  // Use to track whether pointer lock is in the unadjusted movement mode and
  // mousemoves are using unadjusted movement value (without mouse
  // accelerations) from OS, i.e. WM_INPUT on Windows. Deactivates raw input
  // mode when destroyed.
  std::unique_ptr<aura::ScopedEnableUnadjustedMouseEvents>
      mouse_locked_unadjusted_movement_;

  // Whether pinch-to-zoom should be enabled and pinch events forwarded to the
  // renderer.
  const bool pinch_zoom_enabled_;

  // This flag when set ensures that we send over a notification to blink that
  // the current view has focus.
  bool set_focus_on_mouse_down_or_key_event_ = false;

  // Used to record the last position of the mouse.
  // While the mouse is locked, they store the last known position just as mouse
  // lock was entered.
  // Relative to the upper-left corner of the view.
  gfx::PointF unlocked_mouse_position_;
  // Relative to the upper-left corner of the screen.
  gfx::PointF unlocked_global_mouse_position_;
  // Last cursor position relative to screen. Used to compute movementX/Y.
  gfx::PointF global_mouse_position_;
  // In mouse locked mode, we synthetically move the mouse cursor to the center
  // of the window when it reaches the window borders to avoid it going outside.
  // This value is used to differentiate between these synthetic mouse move
  // events vs. normal mouse move events.
  std::optional<gfx::Point> synthetic_move_position_;

  // Whether a swipe-to-move-cursor gesture is activated.
  bool swipe_to_move_cursor_activated_ = false;

  // Stores the current state of the active pointers targeting this
  // object.
  ui::MotionEventAura pointer_state_;

  // The following are not owned. They should outlive |this|
  const raw_ptr<RenderWidgetHostImpl> host_;
  // Should create |this| and own it.
  const raw_ptr<RenderWidgetHostViewBase> host_view_;
  // Optional, used to redirect events to a popup and associated handler.
  raw_ptr<RenderWidgetHostViewBase> popup_child_host_view_ = nullptr;
  raw_ptr<ui::EventHandler> popup_child_event_handler_ = nullptr;
  const raw_ptr<Delegate> delegate_;
  raw_ptr<aura::Window> window_ = nullptr;
  MouseWheelPhaseHandler mouse_wheel_phase_handler_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_EVENT_HANDLER_H_