rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 1 | // 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 | |
| 14 | namespace 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). |
| 23 | class 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 Landay | 62d6137 | 2017-09-13 03:13:19 | [diff] [blame] | 42 | // 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); |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 48 | // 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 Landay | 62d6137 | 2017-09-13 03:13:19 | [diff] [blame] | 56 | void OnNewWordAddedToDictionary( |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 57 | 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 Landay | 62d6137 | 2017-09-13 03:13:19 | [diff] [blame] | 63 | 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. |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 67 | 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 Landay | 62d6137 | 2017-09-13 03:13:19 | [diff] [blame] | 72 | // 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); |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 79 | |
| 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 Landay | 62d6137 | 2017-09-13 03:13:19 | [diff] [blame] | 85 | void StartSuggestionMenuTimer(); |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 86 | // Called by browser-side code in response to an input event to stop the |
Ryan Landay | 62d6137 | 2017-09-13 03:13:19 | [diff] [blame] | 87 | // suggestion menu timer. |
| 88 | void StopSuggestionMenuTimer(); |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 89 | |
| 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 Landay | 62d6137 | 2017-09-13 03:13:19 | [diff] [blame] | 101 | void OnSuggestionMenuTimeout(); |
Jaebaek Seo | 872b57cc | 2017-09-27 05:00:35 | [diff] [blame] | 102 | double DpToPxIfNeeded(double value); |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 103 | |
| 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 Landay | 62d6137 | 2017-09-13 03:13:19 | [diff] [blame] | 109 | TimeoutMonitor suggestion_menu_timeout_; |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 110 | }; |
| 111 | |
rlanday | e4a6ec8 | 2017-07-26 01:03:20 | [diff] [blame] | 112 | } // namespace content |
| 113 | |
| 114 | #endif // CONTENT_BROWSER_ANDROID_TEXT_SUGGESTION_HOST_ANDROID_H_ |