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

content / browser / android / text_suggestion_host_android.h [blame]

// Copyright 2017 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_ANDROID_TEXT_SUGGESTION_HOST_ANDROID_H_
#define CONTENT_BROWSER_ANDROID_TEXT_SUGGESTION_HOST_ANDROID_H_

#include "base/memory/raw_ptr.h"
#include "components/input/timeout_monitor.h"
#include "content/browser/android/render_widget_host_connector.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/mojom/input/input_host.mojom.h"
#include "third_party/blink/public/mojom/input/input_messages.mojom.h"

namespace content {

class TextSuggestionHostMojoImplAndroid;

// This class, along with its Java counterpart TextSuggestionHost, is used to
// implement the Android text suggestion menu that appears when you tap a
// misspelled word. This class creates the Android implementation of
// mojom::TextSuggestionHost, which is used to communicate back-and-forth with
// Blink side code (these are separate classes due to lifecycle considerations;
// this class is created by ImeAdapterAndroid ctor and destroyed together with
// WebContents. Mojo code takes ownership of mojom::TextSuggestionHost).
class TextSuggestionHostAndroid : public RenderWidgetHostConnector {
 public:
  static void Create(JNIEnv* env, WebContents* web_contents);
  TextSuggestionHostAndroid(JNIEnv* env,
                            WebContents* web_contents);
  ~TextSuggestionHostAndroid() override;

  // RenderWidgetHostConnector implementation.
  void UpdateRenderProcessConnection(
      RenderWidgetHostViewAndroid* old_rwhva,
      RenderWidgetHostViewAndroid* new_rhwva) override;

  // Called from the Java text suggestion menu to have Blink apply a spell
  // check suggestion.
  void ApplySpellCheckSuggestion(
      JNIEnv*,
      const base::android::JavaParamRef<jobject>&,
      const base::android::JavaParamRef<jstring>& replacement);
  // Called from the Java text suggestion menu to have Blink apply a text
  // suggestion.
  void ApplyTextSuggestion(JNIEnv*,
                           const base::android::JavaParamRef<jobject>&,
                           int marker_tag,
                           int suggestion_index);
  // Called from the Java text suggestion menu to have Blink delete the
  // currently highlighted region of text that the open suggestion menu pertains
  // to.
  void DeleteActiveSuggestionRange(JNIEnv*,
                                   const base::android::JavaParamRef<jobject>&);
  // Called from the Java text suggestion menu to tell Blink that a word is
  // being added to the dictionary (so Blink can clear the spell check markers
  // for that word).
  void OnNewWordAddedToDictionary(
      JNIEnv*,
      const base::android::JavaParamRef<jobject>&,
      const base::android::JavaParamRef<jstring>& word);
  // Called from the Java text suggestion menu to tell Blink that the user
  // closed the menu without performing one of the available actions, so Blink
  // can re-show the insertion caret and remove the suggestion range highlight.
  void OnSuggestionMenuClosed(JNIEnv*,
                              const base::android::JavaParamRef<jobject>&);
  // Called from Blink to tell the Java TextSuggestionHost to open the spell
  // check suggestion menu.
  void ShowSpellCheckSuggestionMenu(
      double caret_x,
      double caret_y,
      const std::string& marked_text,
      const std::vector<blink::mojom::SpellCheckSuggestionPtr>& suggestions);
  // Called from Blink to tell the Java TextSuggestionHost to open the text
  // suggestion menu.
  void ShowTextSuggestionMenu(
      double caret_x,
      double caret_y,
      const std::string& marked_text,
      const std::vector<blink::mojom::TextSuggestionPtr>& suggestions);

  // Called by browser-side code in response to an input event to stop the
  // spell check menu timer and close the suggestion menu (if open).
  void OnKeyEvent();
  // Called by Blink when the user taps on a spell check marker and we might
  // want to show the text suggestion menu after the double-tap timer expires.
  void StartSuggestionMenuTimer();
  // Called by browser-side code in response to an input event to stop the
  // suggestion menu timer.
  void StopSuggestionMenuTimer();

  void BindTextSuggestionHost(
      mojo::PendingReceiver<blink::mojom::TextSuggestionHost> receiver);

 private:
  RenderFrameHost* GetFocusedFrame();
  base::android::ScopedJavaLocalRef<jobject> GetJavaTextSuggestionHost();
  const mojo::Remote<blink::mojom::TextSuggestionBackend>&
  GetTextSuggestionBackend();
  // Used by the spell check menu timer to notify Blink that the timer has
  // expired.
  void OnSuggestionMenuTimeout();

  // Current RenderWidgetHostView connected to this instance. Can be null.
  raw_ptr<RenderWidgetHostViewAndroid> rwhva_;
  JavaObjectWeakGlobalRef java_text_suggestion_host_;
  mojo::Remote<blink::mojom::TextSuggestionBackend> text_suggestion_backend_;
  std::unique_ptr<TextSuggestionHostMojoImplAndroid> text_suggestion_impl_;
  input::TimeoutMonitor suggestion_menu_timeout_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_ANDROID_TEXT_SUGGESTION_HOST_ANDROID_H_