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