OLD | NEW |
| (Empty) |
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 #include "content/browser/android/ime_adapter_android.h" | |
6 | |
7 #include <android/input.h> | |
8 #include <algorithm> | |
9 #include <vector> | |
10 | |
11 #include "base/android/jni_android.h" | |
12 #include "base/android/jni_array.h" | |
13 #include "base/android/jni_string.h" | |
14 #include "base/android/scoped_java_ref.h" | |
15 #include "base/strings/utf_string_conversions.h" | |
16 #include "base/time/time.h" | |
17 #include "content/browser/frame_host/interstitial_page_impl.h" | |
18 #include "content/browser/frame_host/render_frame_host_impl.h" | |
19 #include "content/browser/renderer_host/render_view_host_delegate.h" | |
20 #include "content/browser/renderer_host/render_widget_host_impl.h" | |
21 #include "content/browser/renderer_host/render_widget_host_view_android.h" | |
22 #include "content/browser/web_contents/web_contents_impl.h" | |
23 #include "content/common/input_messages.h" | |
24 #include "content/common/view_messages.h" | |
25 #include "content/public/browser/browser_thread.h" | |
26 #include "content/public/browser/native_web_keyboard_event.h" | |
27 #include "content/public/browser/web_contents.h" | |
28 #include "jni/ImeAdapter_jni.h" | |
29 #include "third_party/WebKit/public/platform/WebInputEvent.h" | |
30 #include "third_party/WebKit/public/platform/WebTextInputType.h" | |
31 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" | |
32 | |
33 using base::android::AttachCurrentThread; | |
34 using base::android::ConvertJavaStringToUTF16; | |
35 using base::android::ConvertUTF8ToJavaString; | |
36 using base::android::JavaParamRef; | |
37 using base::android::ScopedJavaLocalRef; | |
38 | |
39 namespace content { | |
40 namespace { | |
41 | |
42 // Maps a java KeyEvent into a NativeWebKeyboardEvent. | |
43 // |java_key_event| is used to maintain a globalref for KeyEvent. | |
44 // |type| will determine the WebInputEvent type. | |
45 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create | |
46 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key | |
47 // as a key press of character \r. | |
48 NativeWebKeyboardEvent NativeWebKeyboardEventFromKeyEvent( | |
49 JNIEnv* env, | |
50 const base::android::JavaRef<jobject>& java_key_event, | |
51 int type, | |
52 int modifiers, | |
53 jlong time_ms, | |
54 int key_code, | |
55 int scan_code, | |
56 bool is_system_key, | |
57 int unicode_char) { | |
58 return NativeWebKeyboardEvent(env, java_key_event, | |
59 static_cast<blink::WebInputEvent::Type>(type), | |
60 modifiers, time_ms / 1000.0, key_code, | |
61 scan_code, unicode_char, is_system_key); | |
62 } | |
63 | |
64 } // anonymous namespace | |
65 | |
66 bool RegisterImeAdapter(JNIEnv* env) { | |
67 return RegisterNativesImpl(env); | |
68 } | |
69 | |
70 jlong Init(JNIEnv* env, | |
71 const JavaParamRef<jobject>& obj, | |
72 const JavaParamRef<jobject>& jweb_contents) { | |
73 WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents); | |
74 DCHECK(web_contents); | |
75 return reinterpret_cast<intptr_t>( | |
76 new ImeAdapterAndroid(env, obj, web_contents)); | |
77 } | |
78 | |
79 // Callback from Java to convert BackgroundColorSpan data to a | |
80 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. | |
81 void AppendBackgroundColorSpan(JNIEnv*, | |
82 const JavaParamRef<jclass>&, | |
83 jlong underlines_ptr, | |
84 jint start, | |
85 jint end, | |
86 jint background_color) { | |
87 DCHECK_GE(start, 0); | |
88 DCHECK_GE(end, 0); | |
89 // Do not check |background_color|. | |
90 std::vector<blink::WebCompositionUnderline>* underlines = | |
91 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | |
92 underlines_ptr); | |
93 underlines->push_back(blink::WebCompositionUnderline( | |
94 static_cast<unsigned>(start), static_cast<unsigned>(end), | |
95 SK_ColorTRANSPARENT, false, static_cast<unsigned>(background_color))); | |
96 } | |
97 | |
98 // Callback from Java to convert UnderlineSpan data to a | |
99 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. | |
100 void AppendUnderlineSpan(JNIEnv*, | |
101 const JavaParamRef<jclass>&, | |
102 jlong underlines_ptr, | |
103 jint start, | |
104 jint end) { | |
105 DCHECK_GE(start, 0); | |
106 DCHECK_GE(end, 0); | |
107 std::vector<blink::WebCompositionUnderline>* underlines = | |
108 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | |
109 underlines_ptr); | |
110 underlines->push_back(blink::WebCompositionUnderline( | |
111 static_cast<unsigned>(start), static_cast<unsigned>(end), SK_ColorBLACK, | |
112 false, SK_ColorTRANSPARENT)); | |
113 } | |
114 | |
115 ImeAdapterAndroid::ImeAdapterAndroid(JNIEnv* env, | |
116 const JavaParamRef<jobject>& obj, | |
117 WebContents* web_contents) | |
118 : WebContentsObserver(web_contents), rwhva_(nullptr) { | |
119 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, obj); | |
120 } | |
121 | |
122 ImeAdapterAndroid::~ImeAdapterAndroid() { | |
123 JNIEnv* env = AttachCurrentThread(); | |
124 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | |
125 if (!obj.is_null()) | |
126 Java_ImeAdapter_destroy(env, obj); | |
127 | |
128 UpdateRenderProcessConnection(nullptr); | |
129 } | |
130 | |
131 RenderWidgetHostViewAndroid* ImeAdapterAndroid::GetRenderWidgetHostViewAndroid() | |
132 const { | |
133 RenderWidgetHostView* rwhv = web_contents()->GetRenderWidgetHostView(); | |
134 WebContentsImpl* web_contents_impl = | |
135 static_cast<WebContentsImpl*>(web_contents()); | |
136 if (web_contents_impl->ShowingInterstitialPage()) { | |
137 rwhv = web_contents_impl->GetInterstitialPage() | |
138 ->GetMainFrame() | |
139 ->GetRenderViewHost() | |
140 ->GetWidget() | |
141 ->GetView(); | |
142 } | |
143 return static_cast<RenderWidgetHostViewAndroid*>(rwhv); | |
144 } | |
145 | |
146 void ImeAdapterAndroid::RenderViewReady() { | |
147 UpdateRenderProcessConnection(GetRenderWidgetHostViewAndroid()); | |
148 | |
149 JNIEnv* env = AttachCurrentThread(); | |
150 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | |
151 if (!obj.is_null()) | |
152 Java_ImeAdapter_onConnectedToRenderProcess(env, obj); | |
153 } | |
154 | |
155 void ImeAdapterAndroid::RenderViewHostChanged(RenderViewHost* old_host, | |
156 RenderViewHost* new_host) { | |
157 if (new_host) { | |
158 UpdateRenderProcessConnection(static_cast<RenderWidgetHostViewAndroid*>( | |
159 new_host->GetWidget()->GetView())); | |
160 } else { | |
161 UpdateRenderProcessConnection(nullptr); | |
162 } | |
163 } | |
164 | |
165 void ImeAdapterAndroid::DidAttachInterstitialPage() { | |
166 UpdateRenderProcessConnection(GetRenderWidgetHostViewAndroid()); | |
167 } | |
168 | |
169 void ImeAdapterAndroid::DidDetachInterstitialPage() { | |
170 UpdateRenderProcessConnection(GetRenderWidgetHostViewAndroid()); | |
171 } | |
172 | |
173 void ImeAdapterAndroid::WebContentsDestroyed() { | |
174 delete this; | |
175 } | |
176 | |
177 void ImeAdapterAndroid::UpdateRenderProcessConnection( | |
178 RenderWidgetHostViewAndroid* new_rwhva) { | |
179 if (rwhva_.get() == new_rwhva) | |
180 return; | |
181 if (rwhva_) | |
182 rwhva_->set_ime_adapter(nullptr); | |
183 if (new_rwhva) { | |
184 new_rwhva->set_ime_adapter(this); | |
185 rwhva_ = new_rwhva->GetWeakPtrAndroid(); | |
186 } else { | |
187 rwhva_.reset(); | |
188 } | |
189 } | |
190 | |
191 void ImeAdapterAndroid::UpdateState(const TextInputState& state) { | |
192 JNIEnv* env = AttachCurrentThread(); | |
193 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | |
194 if (obj.is_null()) | |
195 return; | |
196 | |
197 ScopedJavaLocalRef<jstring> jstring_text = | |
198 ConvertUTF8ToJavaString(env, state.value); | |
199 Java_ImeAdapter_updateState(env, obj, static_cast<int>(state.type), | |
200 state.flags, state.mode, state.show_ime_if_needed, | |
201 jstring_text, state.selection_start, | |
202 state.selection_end, state.composition_start, | |
203 state.composition_end, state.reply_to_request); | |
204 } | |
205 | |
206 bool ImeAdapterAndroid::SendKeyEvent( | |
207 JNIEnv* env, | |
208 const JavaParamRef<jobject>&, | |
209 const JavaParamRef<jobject>& original_key_event, | |
210 int type, | |
211 int modifiers, | |
212 jlong time_ms, | |
213 int key_code, | |
214 int scan_code, | |
215 bool is_system_key, | |
216 int unicode_char) { | |
217 if (!rwhva_) | |
218 return false; | |
219 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( | |
220 env, original_key_event, type, modifiers, time_ms, key_code, scan_code, | |
221 is_system_key, unicode_char); | |
222 rwhva_->SendKeyEvent(event); | |
223 return true; | |
224 } | |
225 | |
226 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | |
227 const JavaParamRef<jobject>& obj, | |
228 const JavaParamRef<jobject>& text, | |
229 const JavaParamRef<jstring>& text_str, | |
230 int relative_cursor_pos) { | |
231 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
232 if (!rwhi) | |
233 return; | |
234 | |
235 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | |
236 | |
237 std::vector<blink::WebCompositionUnderline> underlines = | |
238 GetUnderlinesFromSpans(env, obj, text, text16); | |
239 | |
240 // Default to plain underline if we didn't find any span that we care about. | |
241 if (underlines.empty()) { | |
242 underlines.push_back(blink::WebCompositionUnderline( | |
243 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | |
244 } | |
245 | |
246 // relative_cursor_pos is as described in the Android API for | |
247 // InputConnection#setComposingText, whereas the parameters for | |
248 // ImeSetComposition are relative to the start of the composition. | |
249 if (relative_cursor_pos > 0) | |
250 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; | |
251 | |
252 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), | |
253 relative_cursor_pos, relative_cursor_pos); | |
254 } | |
255 | |
256 void ImeAdapterAndroid::CommitText(JNIEnv* env, | |
257 const JavaParamRef<jobject>& obj, | |
258 const JavaParamRef<jobject>& text, | |
259 const JavaParamRef<jstring>& text_str, | |
260 int relative_cursor_pos) { | |
261 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
262 if (!rwhi) | |
263 return; | |
264 | |
265 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | |
266 | |
267 std::vector<blink::WebCompositionUnderline> underlines = | |
268 GetUnderlinesFromSpans(env, obj, text, text16); | |
269 | |
270 // relative_cursor_pos is as described in the Android API for | |
271 // InputConnection#commitText, whereas the parameters for | |
272 // ImeConfirmComposition are relative to the end of the composition. | |
273 if (relative_cursor_pos > 0) | |
274 relative_cursor_pos--; | |
275 else | |
276 relative_cursor_pos -= text16.length(); | |
277 | |
278 rwhi->ImeCommitText(text16, underlines, gfx::Range::InvalidRange(), | |
279 relative_cursor_pos); | |
280 } | |
281 | |
282 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, | |
283 const JavaParamRef<jobject>&) { | |
284 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
285 if (!rwhi) | |
286 return; | |
287 | |
288 rwhi->ImeFinishComposingText(true); | |
289 } | |
290 | |
291 void ImeAdapterAndroid::CancelComposition() { | |
292 JNIEnv* env = AttachCurrentThread(); | |
293 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | |
294 if (!obj.is_null()) | |
295 Java_ImeAdapter_cancelComposition(env, obj); | |
296 } | |
297 | |
298 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { | |
299 JNIEnv* env = AttachCurrentThread(); | |
300 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | |
301 if (!obj.is_null()) { | |
302 Java_ImeAdapter_focusedNodeChanged(env, obj, is_editable_node); | |
303 } | |
304 } | |
305 | |
306 void ImeAdapterAndroid::SetEditableSelectionOffsets( | |
307 JNIEnv*, | |
308 const JavaParamRef<jobject>&, | |
309 int start, | |
310 int end) { | |
311 RenderFrameHost* rfh = GetFocusedFrame(); | |
312 if (!rfh) | |
313 return; | |
314 | |
315 rfh->Send(new InputMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), start, | |
316 end)); | |
317 } | |
318 | |
319 void ImeAdapterAndroid::SetCharacterBounds( | |
320 const std::vector<gfx::RectF>& character_bounds) { | |
321 JNIEnv* env = AttachCurrentThread(); | |
322 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | |
323 if (obj.is_null()) | |
324 return; | |
325 | |
326 const size_t coordinates_array_size = character_bounds.size() * 4; | |
327 std::unique_ptr<float[]> coordinates_array(new float[coordinates_array_size]); | |
328 for (size_t i = 0; i < character_bounds.size(); ++i) { | |
329 const gfx::RectF& rect = character_bounds[i]; | |
330 const size_t coordinates_array_index = i * 4; | |
331 coordinates_array[coordinates_array_index + 0] = rect.x(); | |
332 coordinates_array[coordinates_array_index + 1] = rect.y(); | |
333 coordinates_array[coordinates_array_index + 2] = rect.right(); | |
334 coordinates_array[coordinates_array_index + 3] = rect.bottom(); | |
335 } | |
336 Java_ImeAdapter_setCharacterBounds( | |
337 env, obj, | |
338 base::android::ToJavaFloatArray(env, coordinates_array.get(), | |
339 coordinates_array_size)); | |
340 } | |
341 | |
342 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, | |
343 const JavaParamRef<jobject>&, | |
344 int start, | |
345 int end) { | |
346 RenderFrameHost* rfh = GetFocusedFrame(); | |
347 if (!rfh) | |
348 return; | |
349 | |
350 std::vector<blink::WebCompositionUnderline> underlines; | |
351 underlines.push_back(blink::WebCompositionUnderline( | |
352 0, end - start, SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | |
353 | |
354 rfh->Send(new InputMsg_SetCompositionFromExistingText( | |
355 rfh->GetRoutingID(), start, end, underlines)); | |
356 } | |
357 | |
358 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, | |
359 const JavaParamRef<jobject>&, | |
360 int before, | |
361 int after) { | |
362 RenderFrameHostImpl* rfh = | |
363 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | |
364 if (rfh) | |
365 rfh->DeleteSurroundingText(before, after); | |
366 } | |
367 | |
368 void ImeAdapterAndroid::DeleteSurroundingTextInCodePoints( | |
369 JNIEnv*, | |
370 const JavaParamRef<jobject>&, | |
371 int before, | |
372 int after) { | |
373 RenderFrameHostImpl* rfh = | |
374 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | |
375 if (rfh) | |
376 rfh->DeleteSurroundingTextInCodePoints(before, after); | |
377 } | |
378 | |
379 bool ImeAdapterAndroid::RequestTextInputStateUpdate( | |
380 JNIEnv* env, | |
381 const JavaParamRef<jobject>&) { | |
382 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
383 if (!rwhi) | |
384 return false; | |
385 rwhi->Send(new InputMsg_RequestTextInputStateUpdate(rwhi->GetRoutingID())); | |
386 return true; | |
387 } | |
388 | |
389 void ImeAdapterAndroid::RequestCursorUpdate( | |
390 JNIEnv* env, | |
391 const base::android::JavaParamRef<jobject>& obj, | |
392 bool immediate_request, | |
393 bool monitor_request) { | |
394 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
395 if (!rwhi) | |
396 return; | |
397 rwhi->Send(new InputMsg_RequestCompositionUpdates( | |
398 rwhi->GetRoutingID(), immediate_request, monitor_request)); | |
399 } | |
400 | |
401 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { | |
402 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
403 return rwhva_ ? rwhva_->GetFocusedWidget() : nullptr; | |
404 } | |
405 | |
406 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | |
407 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
408 // We get the focused frame from the WebContents of the page. Although | |
409 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way | |
410 // to get a RenderFrameHost from its RWH. | |
411 if (!rwhva_) | |
412 return nullptr; | |
413 RenderWidgetHostImpl* rwh = | |
414 RenderWidgetHostImpl::From(rwhva_->GetRenderWidgetHost()); | |
415 if (!rwh || !rwh->delegate()) | |
416 return nullptr; | |
417 | |
418 if (auto* contents = rwh->delegate()->GetAsWebContents()) | |
419 return contents->GetFocusedFrame(); | |
420 | |
421 return nullptr; | |
422 } | |
423 | |
424 std::vector<blink::WebCompositionUnderline> | |
425 ImeAdapterAndroid::GetUnderlinesFromSpans( | |
426 JNIEnv* env, | |
427 const base::android::JavaParamRef<jobject>& obj, | |
428 const base::android::JavaParamRef<jobject>& text, | |
429 const base::string16& text16) { | |
430 std::vector<blink::WebCompositionUnderline> underlines; | |
431 // Iterate over spans in |text|, dispatch those that we care about (e.g., | |
432 // BackgroundColorSpan) to a matching callback (e.g., | |
433 // AppendBackgroundColorSpan()), and populate |underlines|. | |
434 Java_ImeAdapter_populateUnderlinesFromSpans( | |
435 env, obj, text, reinterpret_cast<jlong>(&underlines)); | |
436 | |
437 // Sort spans by |.startOffset|. | |
438 std::sort(underlines.begin(), underlines.end()); | |
439 | |
440 return underlines; | |
441 } | |
442 | |
443 } // namespace content | |
OLD | NEW |