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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
content / browser / android / text_suggestion_host_android.cc [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.
#include "content/browser/android/text_suggestion_host_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/functional/bind.h"
#include "content/browser/android/text_suggestion_host_mojo_impl_android.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "ui/gfx/android/view_configuration.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "content/public/android/content_jni_headers/SuggestionInfo_jni.h"
#include "content/public/android/content_jni_headers/TextSuggestionHost_jni.h"
using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF8ToJavaString;
using base::android::GetClass;
using base::android::JavaParamRef;
using base::android::MethodID;
using base::android::ScopedJavaLocalRef;
using base::android::ToJavaArrayOfStrings;
namespace content {
namespace {
const size_t kMaxNumberOfSuggestions = 5;
} // namespace
void TextSuggestionHostAndroid::Create(JNIEnv* env, WebContents* web_contents) {
auto* text_suggestion_host = new TextSuggestionHostAndroid(env, web_contents);
text_suggestion_host->Initialize();
}
TextSuggestionHostAndroid::TextSuggestionHostAndroid(JNIEnv* env,
WebContents* web_contents)
: RenderWidgetHostConnector(web_contents),
rwhva_(nullptr),
suggestion_menu_timeout_(
base::BindRepeating(
&TextSuggestionHostAndroid::OnSuggestionMenuTimeout,
base::Unretained(this)),
GetUIThreadTaskRunner({BrowserTaskType::kUserInput})) {}
TextSuggestionHostAndroid::~TextSuggestionHostAndroid() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_text_suggestion_host_.get(env);
if (!obj.is_null())
Java_TextSuggestionHost_onNativeDestroyed(env, obj);
}
void TextSuggestionHostAndroid::UpdateRenderProcessConnection(
RenderWidgetHostViewAndroid* old_rwhva,
RenderWidgetHostViewAndroid* new_rwhva) {
text_suggestion_backend_.reset();
if (old_rwhva)
old_rwhva->set_text_suggestion_host(nullptr);
if (new_rwhva)
new_rwhva->set_text_suggestion_host(this);
rwhva_ = new_rwhva;
}
void TextSuggestionHostAndroid::ApplySpellCheckSuggestion(
JNIEnv* env,
const JavaParamRef<jobject>&,
const base::android::JavaParamRef<jstring>& replacement) {
const mojo::Remote<blink::mojom::TextSuggestionBackend>&
text_suggestion_backend = GetTextSuggestionBackend();
if (!text_suggestion_backend)
return;
text_suggestion_backend->ApplySpellCheckSuggestion(
ConvertJavaStringToUTF8(env, replacement));
}
void TextSuggestionHostAndroid::ApplyTextSuggestion(
JNIEnv*,
const JavaParamRef<jobject>&,
int marker_tag,
int suggestion_index) {
const mojo::Remote<blink::mojom::TextSuggestionBackend>&
text_suggestion_backend = GetTextSuggestionBackend();
if (!text_suggestion_backend)
return;
text_suggestion_backend->ApplyTextSuggestion(marker_tag, suggestion_index);
}
void TextSuggestionHostAndroid::DeleteActiveSuggestionRange(
JNIEnv*,
const JavaParamRef<jobject>&) {
const mojo::Remote<blink::mojom::TextSuggestionBackend>&
text_suggestion_backend = GetTextSuggestionBackend();
if (!text_suggestion_backend)
return;
text_suggestion_backend->DeleteActiveSuggestionRange();
}
void TextSuggestionHostAndroid::OnNewWordAddedToDictionary(
JNIEnv* env,
const JavaParamRef<jobject>&,
const base::android::JavaParamRef<jstring>& word) {
const mojo::Remote<blink::mojom::TextSuggestionBackend>&
text_suggestion_backend = GetTextSuggestionBackend();
if (!text_suggestion_backend)
return;
text_suggestion_backend->OnNewWordAddedToDictionary(
ConvertJavaStringToUTF8(env, word));
}
void TextSuggestionHostAndroid::OnSuggestionMenuClosed(
JNIEnv*,
const JavaParamRef<jobject>&) {
const mojo::Remote<blink::mojom::TextSuggestionBackend>&
text_suggestion_backend = GetTextSuggestionBackend();
if (!text_suggestion_backend)
return;
text_suggestion_backend->OnSuggestionMenuClosed();
}
ScopedJavaLocalRef<jobject>
TextSuggestionHostAndroid::GetJavaTextSuggestionHost() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_text_suggestion_host_.get(env);
if (obj.is_null()) {
obj = Java_TextSuggestionHost_create(
env, RenderWidgetHostConnector::web_contents()->GetJavaWebContents(),
reinterpret_cast<intptr_t>(this));
java_text_suggestion_host_ = JavaObjectWeakGlobalRef(env, obj);
}
return obj;
}
void TextSuggestionHostAndroid::ShowSpellCheckSuggestionMenu(
double caret_x,
double caret_y,
const std::string& marked_text,
const std::vector<blink::mojom::SpellCheckSuggestionPtr>& suggestions) {
std::vector<std::string> suggestion_strings;
// Enforce kMaxNumberOfSuggestions here in case the renderer is hijacked and
// tries to send bad input.
for (size_t i = 0; i < suggestions.size() && i < kMaxNumberOfSuggestions; ++i)
suggestion_strings.push_back(suggestions[i]->suggestion);
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = GetJavaTextSuggestionHost();
if (obj.is_null())
return;
Java_TextSuggestionHost_showSpellCheckSuggestionMenu(
env, obj, caret_x, caret_y, ConvertUTF8ToJavaString(env, marked_text),
ToJavaArrayOfStrings(env, suggestion_strings));
}
void TextSuggestionHostAndroid::ShowTextSuggestionMenu(
double caret_x,
double caret_y,
const std::string& marked_text,
const std::vector<blink::mojom::TextSuggestionPtr>& suggestions) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = GetJavaTextSuggestionHost();
if (obj.is_null())
return;
// Enforce kMaxNumberOfSuggestions here in case the renderer is hijacked and
// tries to send bad input.
size_t suggestion_count =
std::min(suggestions.size(), kMaxNumberOfSuggestions);
ScopedJavaLocalRef<jobjectArray> jsuggestion_infos =
Java_SuggestionInfo_createArray(env, suggestion_count);
for (size_t i = 0; i < suggestion_count; ++i) {
const blink::mojom::TextSuggestionPtr& suggestion_ptr = suggestions[i];
Java_SuggestionInfo_createSuggestionInfoAndPutInArray(
env, jsuggestion_infos, i, suggestion_ptr->marker_tag,
suggestion_ptr->suggestion_index,
ConvertUTF8ToJavaString(env, suggestion_ptr->prefix),
ConvertUTF8ToJavaString(env, suggestion_ptr->suggestion),
ConvertUTF8ToJavaString(env, suggestion_ptr->suffix));
}
Java_TextSuggestionHost_showTextSuggestionMenu(
env, obj, caret_x, caret_y, ConvertUTF8ToJavaString(env, marked_text),
jsuggestion_infos);
}
void TextSuggestionHostAndroid::StartSuggestionMenuTimer() {
suggestion_menu_timeout_.Stop();
suggestion_menu_timeout_.Start(
base::Milliseconds(gfx::ViewConfiguration::GetDoubleTapTimeoutInMs()));
}
void TextSuggestionHostAndroid::OnKeyEvent() {
suggestion_menu_timeout_.Stop();
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_text_suggestion_host_.get(env);
if (obj.is_null())
return;
Java_TextSuggestionHost_hidePopups(env, obj);
}
void TextSuggestionHostAndroid::StopSuggestionMenuTimer() {
suggestion_menu_timeout_.Stop();
}
void TextSuggestionHostAndroid::BindTextSuggestionHost(
mojo::PendingReceiver<blink::mojom::TextSuggestionHost> receiver) {
text_suggestion_impl_ =
TextSuggestionHostMojoImplAndroid::Create(this, std::move(receiver));
}
RenderFrameHost* TextSuggestionHostAndroid::GetFocusedFrame() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// We get the focused frame from the WebContents of the page. Although
// |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way
// to get a RenderFrameHost from its RWH.
if (!rwhva_)
return nullptr;
RenderWidgetHostImpl* rwh =
RenderWidgetHostImpl::From(rwhva_->GetRenderWidgetHost());
if (auto* contents = WebContentsImpl::FromRenderWidgetHostImpl(rwh))
return contents->GetFocusedFrame();
return nullptr;
}
const mojo::Remote<blink::mojom::TextSuggestionBackend>&
TextSuggestionHostAndroid::GetTextSuggestionBackend() {
if (!text_suggestion_backend_) {
if (RenderFrameHost* rfh = GetFocusedFrame()) {
rfh->GetRemoteInterfaces()->GetInterface(
text_suggestion_backend_.BindNewPipeAndPassReceiver());
}
}
return text_suggestion_backend_;
}
void TextSuggestionHostAndroid::OnSuggestionMenuTimeout() {
const mojo::Remote<blink::mojom::TextSuggestionBackend>&
text_suggestion_backend = GetTextSuggestionBackend();
if (!text_suggestion_backend)
return;
text_suggestion_backend->SuggestionMenuTimeoutCallback(
kMaxNumberOfSuggestions);
}
} // namespace content