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

content / browser / renderer_host / input / touch_selection_controller_client_aura.h [blame]

// Copyright 2015 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_INPUT_TOUCH_SELECTION_CONTROLLER_CLIENT_AURA_H_
#define CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_CLIENT_AURA_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/timer/timer.h"
#include "content/common/content_export.h"
#include "content/public/browser/touch_selection_controller_client_manager.h"
#include "ui/touch_selection/touch_selection_controller.h"
#include "ui/touch_selection/touch_selection_menu_runner.h"

namespace ui {
class TouchSelectionMagnifierAura;
}

namespace content {
struct ContextMenuParams;
class RenderWidgetHostViewAura;

// An implementation of |TouchSelectionControllerClient| to be used in Aura's
// implementation of touch selection for contents.
class CONTENT_EXPORT TouchSelectionControllerClientAura
    : public ui::TouchSelectionControllerClient,
      public ui::TouchSelectionMenuClient,
      public TouchSelectionControllerClientManager {
 public:
  explicit TouchSelectionControllerClientAura(RenderWidgetHostViewAura* rwhva);

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

  ~TouchSelectionControllerClientAura() override;

  // Called when |rwhva_|'s window is moved, to update the quick menu's
  // position.
  void OnWindowMoved();

  // Called on first touch down/last touch up to hide/show the quick menu.
  void OnTouchDown();
  void OnTouchUp();

  // Called when touch scroll starts/completes to hide/show touch handles and
  // the quick menu.
  void OnScrollStarted();
  void OnScrollCompleted();

  // Gives an opportunity to the client to handle context menu request and show
  // the quick menu instead, if appropriate. Returns |true| to indicate that no
  // further handling is needed.
  // TODO(mohsen): This is to match Chrome on Android behavior. However, it is
  // better not to send context menu request from the renderer in this case and
  // instead decide in the client about showing the quick menu in response to
  // selection events. (http://crbug.com/548245)
  virtual bool HandleContextMenu(const ContextMenuParams& params);

  virtual void UpdateClientSelectionBounds(const gfx::SelectionBound& start,
                                           const gfx::SelectionBound& end);

  // TouchSelectionControllerClientManager.
  void DidStopFlinging() override;
  void OnSwipeToMoveCursorBegin() override;
  void OnSwipeToMoveCursorEnd() override;
  void OnClientHitTestRegionUpdated(
      ui::TouchSelectionControllerClient* client) override;
  void UpdateClientSelectionBounds(
      const gfx::SelectionBound& start,
      const gfx::SelectionBound& end,
      ui::TouchSelectionControllerClient* client,
      ui::TouchSelectionMenuClient* menu_client) override;
  void InvalidateClient(ui::TouchSelectionControllerClient* client) override;
  ui::TouchSelectionController* GetTouchSelectionController() override;
  void AddObserver(
      TouchSelectionControllerClientManager::Observer* observer) override;
  void RemoveObserver(
      TouchSelectionControllerClientManager::Observer* observer) override;

 private:
  friend class TestTouchSelectionControllerClientAura;
  class EnvEventObserver;
  class EnvPreTargetHandler;

  bool IsQuickMenuAvailable() const;
  void ShowQuickMenu();
  void UpdateQuickMenu();
  void ShowMagnifier();
  void HideMagnifier();

  // ui::TouchSelectionControllerClient:
  bool SupportsAnimation() const override;
  void SetNeedsAnimate() override;
  void MoveCaret(const gfx::PointF& position) override;
  void MoveRangeSelectionExtent(const gfx::PointF& extent) override;
  void SelectBetweenCoordinates(const gfx::PointF& base,
                                const gfx::PointF& extent) override;
  void OnSelectionEvent(ui::SelectionEventType event) override;
  void OnDragUpdate(const ui::TouchSelectionDraggable::Type type,
                    const gfx::PointF& position) override;
  std::unique_ptr<ui::TouchHandleDrawable> CreateDrawable() override;
  void DidScroll() override;

  // ui::TouchSelectionMenuClient:
  bool IsCommandIdEnabled(int command_id) const override;
  void ExecuteCommand(int command_id, int event_flags) override;
  void RunContextMenu() override;
  bool ShouldShowQuickMenu() override;
  std::u16string GetSelectedText() override;

  // Not owned, non-null for the lifetime of this object.
  raw_ptr<RenderWidgetHostViewAura> rwhva_;

  class InternalClient final : public TouchSelectionControllerClient {
   public:
    explicit InternalClient(RenderWidgetHostViewAura* rwhva) : rwhva_(rwhva) {}
    ~InternalClient() final {}

    bool SupportsAnimation() const final;
    void SetNeedsAnimate() final;
    void MoveCaret(const gfx::PointF& position) final;
    void MoveRangeSelectionExtent(const gfx::PointF& extent) final;
    void SelectBetweenCoordinates(const gfx::PointF& base,
                                  const gfx::PointF& extent) final;
    void OnSelectionEvent(ui::SelectionEventType event) final;
    void OnDragUpdate(const ui::TouchSelectionDraggable::Type type,
                      const gfx::PointF& position) final;
    std::unique_ptr<ui::TouchHandleDrawable> CreateDrawable() final;
    void DidScroll() override;

   private:
    raw_ptr<RenderWidgetHostViewAura, DanglingUntriaged> rwhva_;
  } internal_client_;

  // Keep track of which client interface to use.
  raw_ptr<TouchSelectionControllerClient> active_client_;
  raw_ptr<TouchSelectionMenuClient> active_menu_client_;
  gfx::SelectionBound manager_selection_start_;
  gfx::SelectionBound manager_selection_end_;

  base::ObserverList<TouchSelectionControllerClientManager::Observer>
      observers_;

  base::RetainingOneShotTimer quick_menu_timer_;
  bool quick_menu_requested_;
  bool touch_down_;
  bool scroll_in_progress_;
  bool handle_drag_in_progress_;

  bool show_quick_menu_immediately_for_test_;

  // An event observer that deactivates touch selection on certain input events.
  std::unique_ptr<EnvEventObserver> env_event_observer_;

  // Magnifier which is shown when touch dragging to adjust the selection.
  std::unique_ptr<ui::TouchSelectionMagnifierAura> touch_selection_magnifier_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_INPUT_TOUCH_SELECTION_CONTROLLER_CLIENT_AURA_H_