| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/android/ime_adapter_android.h" | 5 #include "content/browser/android/ime_adapter_android.h" |
| 6 | 6 |
| 7 #include <android/input.h> | 7 #include <android/input.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_array.h" | 12 #include "base/android/jni_array.h" |
| 13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 14 #include "base/android/scoped_java_ref.h" | 14 #include "base/android/scoped_java_ref.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "content/browser/android/text_suggestion_host_impl.h" |
| 17 #include "content/browser/frame_host/interstitial_page_impl.h" | 18 #include "content/browser/frame_host/interstitial_page_impl.h" |
| 18 #include "content/browser/frame_host/render_frame_host_impl.h" | 19 #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_view_host_delegate.h" |
| 20 #include "content/browser/renderer_host/render_widget_host_impl.h" | 21 #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/renderer_host/render_widget_host_view_android.h" |
| 22 #include "content/browser/web_contents/web_contents_impl.h" | 23 #include "content/browser/web_contents/web_contents_impl.h" |
| 23 #include "content/common/input_messages.h" | 24 #include "content/common/input_messages.h" |
| 24 #include "content/common/view_messages.h" | 25 #include "content/common/view_messages.h" |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/native_web_keyboard_event.h" | 27 #include "content/public/browser/native_web_keyboard_event.h" |
| 27 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 28 #include "jni/ImeAdapter_jni.h" | 29 #include "jni/ImeAdapter_jni.h" |
| 30 #include "services/service_manager/public/cpp/interface_provider.h" |
| 29 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 31 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 30 #include "third_party/WebKit/public/platform/WebTextInputType.h" | 32 #include "third_party/WebKit/public/platform/WebTextInputType.h" |
| 31 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" | 33 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" |
| 34 #include "third_party/WebKit/public/web/WebTextSuggestionInfo.h" |
| 32 | 35 |
| 33 using base::android::AttachCurrentThread; | 36 using base::android::AttachCurrentThread; |
| 34 using base::android::ConvertJavaStringToUTF16; | 37 using base::android::ConvertJavaStringToUTF16; |
| 38 using base::android::ConvertUTF8ToJavaString; |
| 39 using base::android::GetClass; |
| 40 using base::android::MethodID; |
| 35 using base::android::JavaParamRef; | 41 using base::android::JavaParamRef; |
| 36 using base::android::ScopedJavaLocalRef; | 42 using base::android::ScopedJavaLocalRef; |
| 37 | 43 |
| 38 namespace content { | 44 namespace content { |
| 39 namespace { | 45 namespace { |
| 40 | 46 |
| 41 // Maps a java KeyEvent into a NativeWebKeyboardEvent. | 47 // Maps a java KeyEvent into a NativeWebKeyboardEvent. |
| 42 // |java_key_event| is used to maintain a globalref for KeyEvent. | 48 // |java_key_event| is used to maintain a globalref for KeyEvent. |
| 43 // |type| will determine the WebInputEvent type. | 49 // |type| will determine the WebInputEvent type. |
| 44 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create | 50 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 DCHECK_GE(end, 0); | 93 DCHECK_GE(end, 0); |
| 88 // Do not check |background_color|. | 94 // Do not check |background_color|. |
| 89 std::vector<blink::WebCompositionUnderline>* underlines = | 95 std::vector<blink::WebCompositionUnderline>* underlines = |
| 90 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | 96 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
| 91 underlines_ptr); | 97 underlines_ptr); |
| 92 underlines->push_back(blink::WebCompositionUnderline( | 98 underlines->push_back(blink::WebCompositionUnderline( |
| 93 static_cast<unsigned>(start), static_cast<unsigned>(end), | 99 static_cast<unsigned>(start), static_cast<unsigned>(end), |
| 94 SK_ColorTRANSPARENT, false, static_cast<unsigned>(background_color))); | 100 SK_ColorTRANSPARENT, false, static_cast<unsigned>(background_color))); |
| 95 } | 101 } |
| 96 | 102 |
| 103 // Callback from Java to convert SuggestionSpan data to a |
| 104 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. |
| 105 void AppendSuggestionSpan(JNIEnv* env, |
| 106 const JavaParamRef<jclass>&, |
| 107 jlong underlines_ptr, |
| 108 jint start, |
| 109 jint end, |
| 110 jint underline_color, |
| 111 jint flags, |
| 112 const JavaParamRef<jobjectArray>& suggestions) { |
| 113 DCHECK_GE(start, 0); |
| 114 DCHECK_GE(end, 0); |
| 115 std::vector<blink::WebCompositionUnderline>* underlines = |
| 116 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
| 117 underlines_ptr); |
| 118 |
| 119 std::vector<std::string> suggestions_vec; |
| 120 AppendJavaStringArrayToStringVector(env, suggestions, &suggestions_vec); |
| 121 |
| 122 underlines->push_back(blink::WebCompositionUnderline( |
| 123 static_cast<unsigned>(start), static_cast<unsigned>(end), |
| 124 static_cast<unsigned>(underline_color), true, SK_ColorTRANSPARENT, |
| 125 suggestions_vec)); |
| 126 } |
| 127 |
| 97 // Callback from Java to convert UnderlineSpan data to a | 128 // Callback from Java to convert UnderlineSpan data to a |
| 98 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. | 129 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. |
| 99 void AppendUnderlineSpan(JNIEnv*, | 130 void AppendUnderlineSpan(JNIEnv*, |
| 100 const JavaParamRef<jclass>&, | 131 const JavaParamRef<jclass>&, |
| 101 jlong underlines_ptr, | 132 jlong underlines_ptr, |
| 102 jint start, | 133 jint start, |
| 103 jint end) { | 134 jint end) { |
| 104 DCHECK_GE(start, 0); | 135 DCHECK_GE(start, 0); |
| 105 DCHECK_GE(end, 0); | 136 DCHECK_GE(end, 0); |
| 106 std::vector<blink::WebCompositionUnderline>* underlines = | 137 std::vector<blink::WebCompositionUnderline>* underlines = |
| 107 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | 138 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
| 108 underlines_ptr); | 139 underlines_ptr); |
| 109 underlines->push_back(blink::WebCompositionUnderline( | 140 underlines->push_back(blink::WebCompositionUnderline( |
| 110 static_cast<unsigned>(start), static_cast<unsigned>(end), SK_ColorBLACK, | 141 static_cast<unsigned>(start), static_cast<unsigned>(end), SK_ColorBLACK, |
| 111 false, SK_ColorTRANSPARENT)); | 142 false, SK_ColorTRANSPARENT)); |
| 112 } | 143 } |
| 113 | 144 |
| 114 ImeAdapterAndroid::ImeAdapterAndroid(JNIEnv* env, | 145 ImeAdapterAndroid::ImeAdapterAndroid(JNIEnv* env, |
| 115 const JavaParamRef<jobject>& obj, | 146 const JavaParamRef<jobject>& obj, |
| 116 WebContents* web_contents) | 147 WebContents* web_contents) |
| 117 : WebContentsObserver(web_contents), rwhva_(nullptr) { | 148 : WebContentsObserver(web_contents), rwhva_(nullptr) { |
| 118 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, obj); | 149 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, obj); |
| 150 |
| 151 RenderFrameHost* rfh = GetFocusedFrame(); |
| 152 if (!rfh) |
| 153 return; |
| 154 |
| 155 rfh->GetInterfaceRegistry()->AddInterface( |
| 156 base::Bind(&TextSuggestionHostImpl::create, base::Unretained(this))); |
| 119 } | 157 } |
| 120 | 158 |
| 121 ImeAdapterAndroid::~ImeAdapterAndroid() { | 159 ImeAdapterAndroid::~ImeAdapterAndroid() { |
| 122 JNIEnv* env = AttachCurrentThread(); | 160 JNIEnv* env = AttachCurrentThread(); |
| 123 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 161 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 124 if (!obj.is_null()) | 162 if (!obj.is_null()) |
| 125 Java_ImeAdapter_destroy(env, obj); | 163 Java_ImeAdapter_destroy(env, obj); |
| 126 | 164 |
| 127 UpdateRenderProcessConnection(nullptr); | 165 UpdateRenderProcessConnection(nullptr); |
| 128 } | 166 } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 if (!obj.is_null()) | 316 if (!obj.is_null()) |
| 279 Java_ImeAdapter_cancelComposition(env, obj); | 317 Java_ImeAdapter_cancelComposition(env, obj); |
| 280 } | 318 } |
| 281 | 319 |
| 282 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { | 320 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { |
| 283 JNIEnv* env = AttachCurrentThread(); | 321 JNIEnv* env = AttachCurrentThread(); |
| 284 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 322 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 285 if (!obj.is_null()) { | 323 if (!obj.is_null()) { |
| 286 Java_ImeAdapter_focusedNodeChanged(env, obj, is_editable_node); | 324 Java_ImeAdapter_focusedNodeChanged(env, obj, is_editable_node); |
| 287 } | 325 } |
| 326 |
| 327 RenderFrameHost* rfh = GetFocusedFrame(); |
| 328 if (rfh) { |
| 329 rfh->GetInterfaceRegistry()->AddInterface( |
| 330 base::Bind(&TextSuggestionHostImpl::create, base::Unretained(this))); |
| 331 } |
| 288 } | 332 } |
| 289 | 333 |
| 290 void ImeAdapterAndroid::SetEditableSelectionOffsets( | 334 void ImeAdapterAndroid::SetEditableSelectionOffsets( |
| 291 JNIEnv*, | 335 JNIEnv*, |
| 292 const JavaParamRef<jobject>&, | 336 const JavaParamRef<jobject>&, |
| 293 int start, | 337 int start, |
| 294 int end) { | 338 int end) { |
| 295 RenderFrameHost* rfh = GetFocusedFrame(); | 339 RenderFrameHost* rfh = GetFocusedFrame(); |
| 296 if (!rfh) | 340 if (!rfh) |
| 297 return; | 341 return; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 316 coordinates_array[coordinates_array_index + 1] = rect.y(); | 360 coordinates_array[coordinates_array_index + 1] = rect.y(); |
| 317 coordinates_array[coordinates_array_index + 2] = rect.right(); | 361 coordinates_array[coordinates_array_index + 2] = rect.right(); |
| 318 coordinates_array[coordinates_array_index + 3] = rect.bottom(); | 362 coordinates_array[coordinates_array_index + 3] = rect.bottom(); |
| 319 } | 363 } |
| 320 Java_ImeAdapter_setCharacterBounds( | 364 Java_ImeAdapter_setCharacterBounds( |
| 321 env, obj, | 365 env, obj, |
| 322 base::android::ToJavaFloatArray(env, coordinates_array.get(), | 366 base::android::ToJavaFloatArray(env, coordinates_array.get(), |
| 323 coordinates_array_size)); | 367 coordinates_array_size)); |
| 324 } | 368 } |
| 325 | 369 |
| 370 void ImeAdapterAndroid::ShowTextSuggestionMenu( |
| 371 const std::vector<blink::mojom::TextSuggestionInfoPtr>& suggestion_infos) { |
| 372 JNIEnv* env = AttachCurrentThread(); |
| 373 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 374 |
| 375 ScopedJavaLocalRef<jclass> clazz = |
| 376 GetClass(env, "org/chromium/content/browser/input/SuggestionInfo"); |
| 377 jmethodID constructor = MethodID::Get<MethodID::TYPE_INSTANCE>( |
| 378 env, clazz.obj(), "<init>", |
| 379 "(JILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); |
| 380 ScopedJavaLocalRef<jobjectArray> suggestion_infos_for_java( |
| 381 env, env->NewObjectArray(suggestion_infos.size(), clazz.obj(), nullptr)); |
| 382 |
| 383 for (size_t i = 0; i < suggestion_infos.size(); i++) { |
| 384 const blink::mojom::TextSuggestionInfoPtr& suggestion_info_ptr = |
| 385 suggestion_infos[i]; |
| 386 |
| 387 ScopedJavaLocalRef<jobject> suggestion_info_for_java( |
| 388 env, |
| 389 env->NewObject( |
| 390 clazz.obj(), constructor, suggestion_info_ptr->suggestionID, |
| 391 suggestion_info_ptr->suggestionIndex, |
| 392 ConvertUTF8ToJavaString(env, suggestion_info_ptr->prefix).obj(), |
| 393 ConvertUTF8ToJavaString(env, suggestion_info_ptr->suggestion).obj(), |
| 394 ConvertUTF8ToJavaString(env, suggestion_info_ptr->suffix).obj())); |
| 395 env->SetObjectArrayElement(suggestion_infos_for_java.obj(), i, |
| 396 suggestion_info_for_java.obj()); |
| 397 } |
| 398 |
| 399 Java_ImeAdapter_showSuggestionMenu(env, obj, suggestion_infos_for_java.obj()); |
| 400 } |
| 401 |
| 326 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, | 402 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, |
| 327 const JavaParamRef<jobject>&, | 403 const JavaParamRef<jobject>&, |
| 328 int start, | 404 int start, |
| 329 int end) { | 405 int end) { |
| 330 RenderFrameHost* rfh = GetFocusedFrame(); | 406 RenderFrameHost* rfh = GetFocusedFrame(); |
| 331 if (!rfh) | 407 if (!rfh) |
| 332 return; | 408 return; |
| 333 | 409 |
| 334 std::vector<blink::WebCompositionUnderline> underlines; | 410 std::vector<blink::WebCompositionUnderline> underlines; |
| 335 underlines.push_back(blink::WebCompositionUnderline( | 411 underlines.push_back(blink::WebCompositionUnderline( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 const base::android::JavaParamRef<jobject>& obj, | 451 const base::android::JavaParamRef<jobject>& obj, |
| 376 bool immediate_request, | 452 bool immediate_request, |
| 377 bool monitor_request) { | 453 bool monitor_request) { |
| 378 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | 454 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
| 379 if (!rwhi) | 455 if (!rwhi) |
| 380 return; | 456 return; |
| 381 rwhi->Send(new InputMsg_RequestCompositionUpdates( | 457 rwhi->Send(new InputMsg_RequestCompositionUpdates( |
| 382 rwhi->GetRoutingID(), immediate_request, monitor_request)); | 458 rwhi->GetRoutingID(), immediate_request, monitor_request)); |
| 383 } | 459 } |
| 384 | 460 |
| 461 void ImeAdapterAndroid::ApplySuggestionReplacement(JNIEnv*, |
| 462 const JavaParamRef<jobject>&, |
| 463 long documentMarkerID, |
| 464 int suggestionIndex) { |
| 465 const blink::mojom::TextSuggestionBackendPtr& text_suggestion_backend = |
| 466 GetTextSuggestionBackend(); |
| 467 if (!text_suggestion_backend) |
| 468 return; |
| 469 |
| 470 text_suggestion_backend->ApplySuggestionReplacement(documentMarkerID, |
| 471 suggestionIndex); |
| 472 } |
| 473 |
| 474 void ImeAdapterAndroid::DeleteSuggestionHighlight( |
| 475 JNIEnv*, |
| 476 const JavaParamRef<jobject>&) { |
| 477 const blink::mojom::TextSuggestionBackendPtr& text_suggestion_backend = |
| 478 GetTextSuggestionBackend(); |
| 479 if (!text_suggestion_backend) |
| 480 return; |
| 481 |
| 482 text_suggestion_backend->DeleteSuggestionHighlight(); |
| 483 } |
| 484 |
| 485 void ImeAdapterAndroid::SuggestionMenuClosed(JNIEnv*, |
| 486 const JavaParamRef<jobject>&) { |
| 487 const blink::mojom::TextSuggestionBackendPtr& text_suggestion_backend = |
| 488 GetTextSuggestionBackend(); |
| 489 if (!text_suggestion_backend) |
| 490 return; |
| 491 |
| 492 text_suggestion_backend->SuggestionMenuClosed(); |
| 493 } |
| 494 |
| 385 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { | 495 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { |
| 386 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 496 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 387 return rwhva_ ? rwhva_->GetFocusedWidget() : nullptr; | 497 return rwhva_ ? rwhva_->GetFocusedWidget() : nullptr; |
| 388 } | 498 } |
| 389 | 499 |
| 390 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | 500 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { |
| 391 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 501 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 392 // We get the focused frame from the WebContents of the page. Although | 502 // We get the focused frame from the WebContents of the page. Although |
| 393 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way | 503 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way |
| 394 // to get a RenderFrameHost from its RWH. | 504 // to get a RenderFrameHost from its RWH. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 417 // AppendBackgroundColorSpan()), and populate |underlines|. | 527 // AppendBackgroundColorSpan()), and populate |underlines|. |
| 418 Java_ImeAdapter_populateUnderlinesFromSpans( | 528 Java_ImeAdapter_populateUnderlinesFromSpans( |
| 419 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 529 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
| 420 | 530 |
| 421 // Sort spans by |.startOffset|. | 531 // Sort spans by |.startOffset|. |
| 422 std::sort(underlines.begin(), underlines.end()); | 532 std::sort(underlines.begin(), underlines.end()); |
| 423 | 533 |
| 424 return underlines; | 534 return underlines; |
| 425 } | 535 } |
| 426 | 536 |
| 537 const blink::mojom::TextSuggestionBackendPtr& |
| 538 ImeAdapterAndroid::GetTextSuggestionBackend() { |
| 539 if (!text_suggestion_backend_) { |
| 540 RenderFrameHost* rfh = GetFocusedFrame(); |
| 541 if (rfh) { |
| 542 rfh->GetRemoteInterfaces()->GetInterface( |
| 543 mojo::MakeRequest(&text_suggestion_backend_)); |
| 544 } |
| 545 } |
| 546 |
| 547 return text_suggestion_backend_; |
| 548 } |
| 549 |
| 427 } // namespace content | 550 } // namespace content |
| OLD | NEW |