blob: 9d7fb7009238a9baf12223db5395a01b53510821 [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);
42 // Called from the Java text suggestion menu to have Blink delete the
43 // currently highlighted region of text that the open suggestion menu pertains
44 // to.
45 void DeleteActiveSuggestionRange(JNIEnv*,
46 const base::android::JavaParamRef<jobject>&);
47 // Called from the Java text suggestion menu to tell Blink that a word is
48 // being added to the dictionary (so Blink can clear the spell check markers
49 // for that word).
50 void NewWordAddedToDictionary(
51 JNIEnv*,
52 const base::android::JavaParamRef<jobject>&,
53 const base::android::JavaParamRef<jstring>& word);
54 // Called from the Java text suggestion menu to tell Blink that the user
55 // closed the menu without performing one of the available actions, so Blink
56 // can re-show the insertion caret and remove the suggestion range highlight.
57 void SuggestionMenuClosed(JNIEnv*,
58 const base::android::JavaParamRef<jobject>&);
59 // Called from Blink to tell the Java TextSuggestionHost to open the text
60 // suggestion menu.
61 void ShowSpellCheckSuggestionMenu(
62 double caret_x,
63 double caret_y,
64 const std::string& marked_text,
65 const std::vector<blink::mojom::SpellCheckSuggestionPtr>& suggestions);
66
67 // Called by browser-side code in response to an input event to stop the
68 // spell check menu timer and close the suggestion menu (if open).
69 void OnKeyEvent();
70 // Called by Blink when the user taps on a spell check marker and we might
71 // want to show the text suggestion menu after the double-tap timer expires.
72 void StartSpellCheckMenuTimer();
73 // Called by browser-side code in response to an input event to stop the
74 // spell check menu timer.
75 void StopSpellCheckMenuTimer();
76
77 // WebContentsObserver overrides
78 void OnInterfaceRequestFromFrame(
79 content::RenderFrameHost* render_frame_host,
80 const std::string& interface_name,
81 mojo::ScopedMessagePipeHandle* interface_pipe) override;
82
83 private:
84 RenderFrameHost* GetFocusedFrame();
85 const blink::mojom::TextSuggestionBackendPtr& GetTextSuggestionBackend();
86 // Used by the spell check menu timer to notify Blink that the timer has
87 // expired.
88 void OnSpellCheckMenuTimeout();
89
90 service_manager::BinderRegistry registry_;
91 // Current RenderWidgetHostView connected to this instance. Can be null.
92 RenderWidgetHostViewAndroid* rwhva_;
93 JavaObjectWeakGlobalRef java_text_suggestion_host_;
94 blink::mojom::TextSuggestionBackendPtr text_suggestion_backend_;
95 TimeoutMonitor spellcheck_menu_timeout_;
96};
97
98bool RegisterTextSuggestionHost(JNIEnv* env);
99
100} // namespace content
101
102#endif // CONTENT_BROWSER_ANDROID_TEXT_SUGGESTION_HOST_ANDROID_H_