OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/ime_adapter_android.h" | 5 #include "content/browser/renderer_host/ime_adapter_android.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <android/input.h> | 8 #include <android/input.h> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "content/browser/renderer_host/render_widget_host_view_android.h" | 23 #include "content/browser/renderer_host/render_widget_host_view_android.h" |
24 #include "content/common/input_messages.h" | 24 #include "content/common/input_messages.h" |
25 #include "content/common/view_messages.h" | 25 #include "content/common/view_messages.h" |
26 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/native_web_keyboard_event.h" | 27 #include "content/public/browser/native_web_keyboard_event.h" |
28 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
29 #include "jni/ImeAdapter_jni.h" | 29 #include "jni/ImeAdapter_jni.h" |
30 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 30 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
31 #include "third_party/WebKit/public/platform/WebTextInputType.h" | 31 #include "third_party/WebKit/public/platform/WebTextInputType.h" |
32 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" | 32 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" |
| 33 #include "third_party/WebKit/public/web/WebTextSuggestionInfo.h" |
33 | 34 |
34 using base::android::AttachCurrentThread; | 35 using base::android::AttachCurrentThread; |
35 using base::android::ConvertJavaStringToUTF16; | 36 using base::android::ConvertJavaStringToUTF16; |
| 37 using base::android::ConvertUTF8ToJavaString; |
| 38 using base::android::GetClass; |
| 39 using base::android::MethodID; |
36 using base::android::JavaParamRef; | 40 using base::android::JavaParamRef; |
| 41 using base::android::ScopedJavaLocalRef; |
37 | 42 |
38 namespace content { | 43 namespace content { |
39 namespace { | 44 namespace { |
40 | 45 |
41 // Maps a java KeyEvent into a NativeWebKeyboardEvent. | 46 // Maps a java KeyEvent into a NativeWebKeyboardEvent. |
42 // |java_key_event| is used to maintain a globalref for KeyEvent. | 47 // |java_key_event| is used to maintain a globalref for KeyEvent. |
43 // |type| will determine the WebInputEvent type. | 48 // |type| will determine the WebInputEvent type. |
44 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create | 49 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create |
45 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key | 50 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key |
46 // as a key press of character \r. | 51 // as a key press of character \r. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | 86 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
82 underlines_ptr); | 87 underlines_ptr); |
83 underlines->push_back( | 88 underlines->push_back( |
84 blink::WebCompositionUnderline(static_cast<unsigned>(start), | 89 blink::WebCompositionUnderline(static_cast<unsigned>(start), |
85 static_cast<unsigned>(end), | 90 static_cast<unsigned>(end), |
86 SK_ColorTRANSPARENT, | 91 SK_ColorTRANSPARENT, |
87 false, | 92 false, |
88 static_cast<unsigned>(background_color))); | 93 static_cast<unsigned>(background_color))); |
89 } | 94 } |
90 | 95 |
| 96 // Callback from Java to convert SuggestionSpan data to a |
| 97 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. |
| 98 void AppendSuggestionSpan(JNIEnv* env, |
| 99 const JavaParamRef<jclass>&, |
| 100 jlong underlines_ptr, |
| 101 jint start, |
| 102 jint end, |
| 103 jint underline_color, |
| 104 jint flags, |
| 105 const JavaParamRef<jobjectArray>& suggestions) { |
| 106 DCHECK_GE(start, 0); |
| 107 DCHECK_GE(end, 0); |
| 108 std::vector<blink::WebCompositionUnderline>* underlines = |
| 109 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
| 110 underlines_ptr); |
| 111 |
| 112 std::vector<std::string> suggestions_vec; |
| 113 AppendJavaStringArrayToStringVector(env, suggestions, &suggestions_vec); |
| 114 |
| 115 underlines->push_back(blink::WebCompositionUnderline( |
| 116 static_cast<unsigned>(start), static_cast<unsigned>(end), |
| 117 static_cast<unsigned>(underline_color), true, SK_ColorTRANSPARENT, |
| 118 suggestions_vec)); |
| 119 } |
| 120 |
91 // Callback from Java to convert UnderlineSpan data to a | 121 // Callback from Java to convert UnderlineSpan data to a |
92 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. | 122 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. |
93 void AppendUnderlineSpan(JNIEnv*, | 123 void AppendUnderlineSpan(JNIEnv*, |
94 const JavaParamRef<jclass>&, | 124 const JavaParamRef<jclass>&, |
95 jlong underlines_ptr, | 125 jlong underlines_ptr, |
96 jint start, | 126 jint start, |
97 jint end) { | 127 jint end) { |
98 DCHECK_GE(start, 0); | 128 DCHECK_GE(start, 0); |
99 DCHECK_GE(end, 0); | 129 DCHECK_GE(end, 0); |
100 std::vector<blink::WebCompositionUnderline>* underlines = | 130 std::vector<blink::WebCompositionUnderline>* underlines = |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 coordinates_array[coordinates_array_index + 0] = rect.x(); | 282 coordinates_array[coordinates_array_index + 0] = rect.x(); |
253 coordinates_array[coordinates_array_index + 1] = rect.y(); | 283 coordinates_array[coordinates_array_index + 1] = rect.y(); |
254 coordinates_array[coordinates_array_index + 2] = rect.right(); | 284 coordinates_array[coordinates_array_index + 2] = rect.right(); |
255 coordinates_array[coordinates_array_index + 3] = rect.bottom(); | 285 coordinates_array[coordinates_array_index + 3] = rect.bottom(); |
256 } | 286 } |
257 Java_ImeAdapter_setCharacterBounds( | 287 Java_ImeAdapter_setCharacterBounds( |
258 env, obj, base::android::ToJavaFloatArray(env, coordinates_array.get(), | 288 env, obj, base::android::ToJavaFloatArray(env, coordinates_array.get(), |
259 coordinates_array_size)); | 289 coordinates_array_size)); |
260 } | 290 } |
261 | 291 |
| 292 void ImeAdapterAndroid::ShowTextSuggestionMenu( |
| 293 const std::vector<blink::WebTextSuggestionInfo>& suggestion_infos) { |
| 294 JNIEnv* env = AttachCurrentThread(); |
| 295 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 296 |
| 297 ScopedJavaLocalRef<jclass> clazz = |
| 298 GetClass(env, "org/chromium/content/browser/input/SuggestionInfo"); |
| 299 jmethodID constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( |
| 300 env, clazz.obj(), "<init>", |
| 301 "(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); |
| 302 ScopedJavaLocalRef<jobjectArray> suggestion_infos_for_java( |
| 303 env, env->NewObjectArray(suggestion_infos.size(), clazz.obj(), nullptr)); |
| 304 |
| 305 for (size_t i = 0; i < suggestion_infos.size(); i++) { |
| 306 const blink::WebTextSuggestionInfo& suggestion_info = suggestion_infos[i]; |
| 307 ScopedJavaLocalRef<jobject> suggestion_info_for_java( |
| 308 env, |
| 309 env->NewObject( |
| 310 clazz.obj(), constructor, suggestion_info.documentMarkerID, |
| 311 suggestion_info.suggestionIndex, |
| 312 ConvertUTF8ToJavaString(env, suggestion_info.prefix).obj(), |
| 313 ConvertUTF8ToJavaString(env, suggestion_info.suggestion).obj(), |
| 314 ConvertUTF8ToJavaString(env, suggestion_info.suffix).obj())); |
| 315 env->SetObjectArrayElement(suggestion_infos_for_java.obj(), i, |
| 316 suggestion_info_for_java.obj()); |
| 317 } |
| 318 |
| 319 Java_ImeAdapter_showSuggestionMenu(env, obj, suggestion_infos_for_java.obj()); |
| 320 } |
| 321 |
262 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, | 322 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, |
263 const JavaParamRef<jobject>&, | 323 const JavaParamRef<jobject>&, |
264 int start, | 324 int start, |
265 int end) { | 325 int end) { |
266 RenderFrameHost* rfh = GetFocusedFrame(); | 326 RenderFrameHost* rfh = GetFocusedFrame(); |
267 if (!rfh) | 327 if (!rfh) |
268 return; | 328 return; |
269 | 329 |
270 std::vector<blink::WebCompositionUnderline> underlines; | 330 std::vector<blink::WebCompositionUnderline> underlines; |
271 underlines.push_back(blink::WebCompositionUnderline( | 331 underlines.push_back(blink::WebCompositionUnderline( |
(...skipping 28 matching lines...) Expand all Loading... |
300 const base::android::JavaParamRef<jobject>& obj, | 360 const base::android::JavaParamRef<jobject>& obj, |
301 bool immediate_request, | 361 bool immediate_request, |
302 bool monitor_request) { | 362 bool monitor_request) { |
303 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 363 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
304 if (!rwhi) | 364 if (!rwhi) |
305 return; | 365 return; |
306 rwhi->Send(new InputMsg_RequestCompositionUpdate( | 366 rwhi->Send(new InputMsg_RequestCompositionUpdate( |
307 rwhi->GetRoutingID(), immediate_request, monitor_request)); | 367 rwhi->GetRoutingID(), immediate_request, monitor_request)); |
308 } | 368 } |
309 | 369 |
| 370 void ImeAdapterAndroid::ApplySuggestionReplacement(JNIEnv*, |
| 371 const JavaParamRef<jobject>&, |
| 372 int documentMarkerID, |
| 373 int suggestionIndex) { |
| 374 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 375 if (!rwhi) |
| 376 return; |
| 377 |
| 378 rwhi->ApplySuggestionReplacement(documentMarkerID, suggestionIndex); |
| 379 } |
| 380 |
| 381 void ImeAdapterAndroid::DeleteSuggestionHighlight( |
| 382 JNIEnv*, |
| 383 const JavaParamRef<jobject>&) { |
| 384 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 385 if (!rwhi) |
| 386 return; |
| 387 |
| 388 rwhi->DeleteSuggestionHighlight(); |
| 389 } |
| 390 |
| 391 void ImeAdapterAndroid::SuggestionMenuClosed(JNIEnv*, |
| 392 const JavaParamRef<jobject>&) { |
| 393 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 394 if (!rwhi) |
| 395 return; |
| 396 |
| 397 rwhi->SuggestionMenuClosed(); |
| 398 } |
| 399 |
310 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, | 400 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, |
311 const JavaParamRef<jobject>&) { | 401 const JavaParamRef<jobject>&) { |
312 java_ime_adapter_.reset(); | 402 java_ime_adapter_.reset(); |
313 } | 403 } |
314 | 404 |
315 RenderWidgetHostImpl* ImeAdapterAndroid::GetRenderWidgetHostImpl() { | 405 RenderWidgetHostImpl* ImeAdapterAndroid::GetRenderWidgetHostImpl() { |
316 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 406 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
317 DCHECK(rwhva_); | 407 DCHECK(rwhva_); |
318 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost(); | 408 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost(); |
319 if (!rwh) | 409 if (!rwh) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 Java_ImeAdapter_populateUnderlinesFromSpans( | 447 Java_ImeAdapter_populateUnderlinesFromSpans( |
358 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 448 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
359 | 449 |
360 // Sort spans by |.startOffset|. | 450 // Sort spans by |.startOffset|. |
361 std::sort(underlines.begin(), underlines.end()); | 451 std::sort(underlines.begin(), underlines.end()); |
362 | 452 |
363 return underlines; | 453 return underlines; |
364 } | 454 } |
365 | 455 |
366 } // namespace content | 456 } // namespace content |
OLD | NEW |