blob: c32b42238dee4fcfd07d98e1ad8a401df1c31bbb [file] [log] [blame]
rlandaye4a6ec82017-07-26 01:03:201// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_ANDROID_TEXT_SUGGESTION_HOST_ANDROID_H_
6#define CONTENT_BROWSER_ANDROID_TEXT_SUGGESTION_HOST_ANDROID_H_
7
8#include "content/browser/android/render_widget_host_connector.h"
9#include "content/browser/renderer_host/input/timeout_monitor.h"
10#include "services/service_manager/public/cpp/binder_registry.h"
11#include "third_party/WebKit/public/platform/input_host.mojom.h"
12#include "third_party/WebKit/public/platform/input_messages.mojom.h"
13
14namespace content {
15
16// This class, along with its Java counterpart TextSuggestionHost, is used to
17// implement the Android text suggestion menu that appears when you tap a
18// misspelled word. This class creates the Android implementation of
19// mojom::TextSuggestionHost, which is used to communicate back-and-forth with
20// Blink side code (these are separate classes due to lifecycle considerations;
21// this class needs to be constructed from Java code, but Mojo code wants to
22// take ownership of mojom::TextSuggestionHost).
23class TextSuggestionHostAndroid : public RenderWidgetHostConnector,
24 public WebContentsObserver {
25 public:
26 TextSuggestionHostAndroid(JNIEnv* env,
27 const base::android::JavaParamRef<jobject>& obj,
28 WebContents* web_contents);
29 ~TextSuggestionHostAndroid() override;
30
31 // RenderWidgetHostConnector implementation.
32 void UpdateRenderProcessConnection(
33 RenderWidgetHostViewAndroid* old_rwhva,
34 RenderWidgetHostViewAndroid* new_rhwva) override;
35
36 // Called from the Java text suggestion menu to have Blink apply a spell
37 // check suggestion.
38 void ApplySpellCheckSuggestion(
39 JNIEnv*,
40 const base::android::JavaParamRef<jobject>&,
41 const base::android::JavaParamRef<jstring>& replacement);
Ryan Landay62d61372017-09-13 03:13:1942 // Called from the Java text suggestion menu to have Blink apply a text
43 // suggestion.
44 void ApplyTextSuggestion(JNIEnv*,
45 const base::android::JavaParamRef<jobject>&,
46 int marker_tag,
47 int suggestion_index);
rlandaye4a6ec82017-07-26 01:03:2048 // Called from the Java text suggestion menu to have Blink delete the
49 // currently highlighted region of text that the open suggestion menu pertains
50 // to.
51 void DeleteActiveSuggestionRange(JNIEnv*,
52 const base::android::JavaParamRef<jobject>&);
53 // Called from the Java text suggestion menu to tell Blink that a word is
54 // being added to the dictionary (so Blink can clear the spell check markers
55 // for that word).
Ryan Landay62d61372017-09-13 03:13:1956 void OnNewWordAddedToDictionary(
rlandaye4a6ec82017-07-26 01:03:2057 JNIEnv*,
58 const base::android::JavaParamRef<jobject>&,
59 const base::android::JavaParamRef<jstring>& word);
60 // Called from the Java text suggestion menu to tell Blink that the user
61 // closed the menu without performing one of the available actions, so Blink
62 // can re-show the insertion caret and remove the suggestion range highlight.
Ryan Landay62d61372017-09-13 03:13:1963 void OnSuggestionMenuClosed(JNIEnv*,
64 const base::android::JavaParamRef<jobject>&);
65 // Called from Blink to tell the Java TextSuggestionHost to open the spell
66 // check suggestion menu.
rlandaye4a6ec82017-07-26 01:03:2067 void ShowSpellCheckSuggestionMenu(
68 double caret_x,
69 double caret_y,
70 const std::string& marked_text,
71 const std::vector<blink::mojom::SpellCheckSuggestionPtr>& suggestions);
Ryan Landay62d61372017-09-13 03:13:1972 // Called from Blink to tell the Java TextSuggestionHost to open the text
73 // suggestion menu.
74 void ShowTextSuggestionMenu(
75 double caret_x,
76 double caret_y,
77 const std::string& marked_text,
78 const std::vector<blink::mojom::TextSuggestionPtr>& suggestions);
rlandaye4a6ec82017-07-26 01:03:2079
80 // Called by browser-side code in response to an input event to stop the
81 // spell check menu timer and close the suggestion menu (if open).
82 void OnKeyEvent();
83 // Called by Blink when the user taps on a spell check marker and we might
84 // want to show the text suggestion menu after the double-tap timer expires.
Ryan Landay62d61372017-09-13 03:13:1985 void StartSuggestionMenuTimer();
rlandaye4a6ec82017-07-26 01:03:2086 // Called by browser-side code in response to an input event to stop the
Ryan Landay62d61372017-09-13 03:13:1987 // suggestion menu timer.
88 void StopSuggestionMenuTimer();
rlandaye4a6ec82017-07-26 01:03:2089
90 // WebContentsObserver overrides
91 void OnInterfaceRequestFromFrame(
92 content::RenderFrameHost* render_frame_host,
93 const std::string& interface_name,
94 mojo::ScopedMessagePipeHandle* interface_pipe) override;
95
96 private:
97 RenderFrameHost* GetFocusedFrame();
98 const blink::mojom::TextSuggestionBackendPtr& GetTextSuggestionBackend();
99 // Used by the spell check menu timer to notify Blink that the timer has
100 // expired.
Ryan Landay62d61372017-09-13 03:13:19101 void OnSuggestionMenuTimeout();
Jaebaek Seo872b57cc2017-09-27 05:00:35102 double DpToPxIfNeeded(double value);
rlandaye4a6ec82017-07-26 01:03:20103
104 service_manager::BinderRegistry registry_;
105 // Current RenderWidgetHostView connected to this instance. Can be null.
106 RenderWidgetHostViewAndroid* rwhva_;
107 JavaObjectWeakGlobalRef java_text_suggestion_host_;
108 blink::mojom::TextSuggestionBackendPtr text_suggestion_backend_;
Ryan Landay62d61372017-09-13 03:13:19109 TimeoutMonitor suggestion_menu_timeout_;
rlandaye4a6ec82017-07-26 01:03:20110};
111
rlandaye4a6ec82017-07-26 01:03:20112} // namespace content
113
114#endif // CONTENT_BROWSER_ANDROID_TEXT_SUGGESTION_HOST_ANDROID_H_