| // 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/ime_adapter_android.h" | |
| #include <android/input.h> | |
| #include <algorithm> | |
| #include <vector> | |
| #include "base/android/jni_android.h" | |
| #include "base/android/jni_bytebuffer.h" | |
| #include "base/android/jni_string.h" | |
| #include "base/android/scoped_java_ref.h" | |
| #include "base/containers/span.h" | |
| #include "base/functional/bind.h" | |
| #include "base/functional/callback_helpers.h" | |
| #include "base/strings/utf_string_conversions.h" | |
| #include "base/time/time.h" | |
| #include "components/input/native_web_keyboard_event.h" | |
| #include "content/browser/android/ime_text_span.h" | |
| #include "content/browser/android/text_suggestion_host_android.h" | |
| #include "content/browser/renderer_host/render_frame_host_impl.h" | |
| #include "content/browser/renderer_host/render_view_host_delegate.h" | |
| #include "content/browser/renderer_host/render_widget_host_impl.h" | |
| #include "content/browser/renderer_host/render_widget_host_view_android.h" | |
| #include "content/browser/web_contents/web_contents_impl.h" | |
| #include "content/public/browser/browser_thread.h" | |
| #include "content/public/browser/render_frame_host.h" | |
| #include "content/public/common/content_features.h" | |
| #include "mojo/public/cpp/base/big_buffer.h" | |
| #include "third_party/blink/public/common/input/web_input_event.h" | |
| #include "third_party/blink/public/mojom/input/ime_host.mojom.h" | |
| #include "third_party/blink/public/mojom/input/stylus_writing_gesture.mojom.h" | |
| #include "third_party/blink/public/platform/web_text_input_type.h" | |
| // Must come after all headers that specialize FromJniType() / ToJniType(). | |
| #include "content/public/android/content_jni_headers/ImeAdapterImpl_jni.h" | |
| using base::android::AppendJavaStringArrayToStringVector; | |
| using base::android::AttachCurrentThread; | |
| using base::android::ConvertJavaStringToUTF16; | |
| using base::android::ConvertUTF16ToJavaString; | |
| using base::android::JavaRef; | |
| using base::android::ScopedJavaLocalRef; | |
| using base::android::ToJavaArrayOfStrings; | |
| namespace content { | |
| namespace { | |
| // Maps a java KeyEvent into a NativeWebKeyboardEvent. | |
| // |java_key_event| is used to maintain a globalref for KeyEvent. | |
| // |type| will determine the WebInputEvent type. | |
| // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create | |
| // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key | |
| // as a key press of character \r. | |
| input::NativeWebKeyboardEvent NativeWebKeyboardEventFromKeyEvent( | |
| JNIEnv* env, | |
| const base::android::JavaRef<jobject>& java_key_event, | |
| int type, | |
| int modifiers, | |
| int64_t time_ms, | |
| int key_code, | |
| int scan_code, | |
| bool is_system_key, | |
| int unicode_char) { | |
| return input::NativeWebKeyboardEvent( | |
| env, java_key_event, static_cast<blink::WebInputEvent::Type>(type), | |
| modifiers, base::TimeTicks() + base::Milliseconds(time_ms), key_code, | |
| scan_code, unicode_char, is_system_key); | |
| } | |
| } // anonymous namespace | |
| static int64_t JNI_ImeAdapterImpl_Create(JNIEnv* env, | |
| WebContents* web_contents) { | |
| CHECK(web_contents, base::NotFatalUntil::M159); | |
| auto* ime_adapter = new ImeAdapterAndroid(web_contents); | |
| return reinterpret_cast<intptr_t>(ime_adapter); | |
| } | |
| // Callback from Java to convert BackgroundColorSpan data to a | |
| // ui::ImeTextSpan instance, and append it to |ime_text_spans_ptr|. | |
| static void JNI_ImeAdapterImpl_AppendBackgroundColorSpan( | |
| JNIEnv*, | |
| int64_t ime_text_spans_ptr, | |
| int32_t start, | |
| int32_t end, | |
| int32_t background_color) { | |
| CHECK_GE(start, 0, base::NotFatalUntil::M159); | |
| CHECK_GE(end, 0, base::NotFatalUntil::M159); | |
| // Do not check |background_color|. | |
| std::vector<ui::ImeTextSpan>* ime_text_spans = | |
| reinterpret_cast<std::vector<ui::ImeTextSpan>*>(ime_text_spans_ptr); | |
| ime_text_spans->push_back(ui::ImeTextSpan( | |
| ui::ImeTextSpan::Type::kComposition, static_cast<unsigned>(start), | |
| static_cast<unsigned>(end), ui::ImeTextSpan::Thickness::kNone, | |
| ui::ImeTextSpan::UnderlineStyle::kNone, | |
| static_cast<unsigned>(background_color), SK_ColorTRANSPARENT, | |
| std::vector<std::string>())); | |
| } | |
| // Callback from Java to convert ForegroundColorSpan data to a | |
| // ui::ImeTextSpan instance, and append it to |ime_text_spans_ptr|. | |
| static void JNI_ImeAdapterImpl_AppendForegroundColorSpan( | |
| JNIEnv*, | |
| int64_t ime_text_spans_ptr, | |
| int32_t start, | |
| int32_t end, | |
| int32_t foreground_color) { | |
| CHECK_GE(start, 0, base::NotFatalUntil::M159); | |
| CHECK_GE(end, 0, base::NotFatalUntil::M159); | |
| // Do not check |foreground_color|. | |
| std::vector<ui::ImeTextSpan>* ime_text_spans = | |
| reinterpret_cast<std::vector<ui::ImeTextSpan>*>(ime_text_spans_ptr); | |
| ime_text_spans->push_back(ui::ImeTextSpan( | |
| ui::ImeTextSpan::Type::kComposition, static_cast<unsigned>(start), | |
| static_cast<unsigned>(end), ui::ImeTextSpan::Thickness::kNone, | |
| ui::ImeTextSpan::UnderlineStyle::kNone, SK_ColorTRANSPARENT, | |
| SK_ColorTRANSPARENT, std::vector<std::string>(), | |
| static_cast<unsigned>(foreground_color))); | |
| } | |
| // Callback from Java to convert SuggestionSpan data to a | |
| // ui::ImeTextSpan instance, and append it to |ime_text_spans_ptr|. | |
| static void JNI_ImeAdapterImpl_AppendSuggestionSpan( | |
| JNIEnv* env, | |
| int64_t ime_text_spans_ptr, | |
| int32_t start, | |
| int32_t end, | |
| int32_t type, | |
| bool remove_on_finish_composing, | |
| int32_t underline_color, | |
| int32_t suggestion_highlight_color, | |
| const JavaRef<jobjectArray>& suggestions, | |
| bool should_hide_suggestion_menu) { | |
| CHECK_GE(start, 0, base::NotFatalUntil::M159); | |
| CHECK_GE(end, 0, base::NotFatalUntil::M159); | |
| ui::ImeTextSpan::Type ui_type = | |
| mojo::EnumTraits<ui::mojom::ImeTextSpanType, ui::ImeTextSpan::Type>:: | |
| FromMojom(static_cast<ui::mojom::ImeTextSpanType>(type)); | |
| std::vector<ui::ImeTextSpan>* ime_text_spans = | |
| reinterpret_cast<std::vector<ui::ImeTextSpan>*>(ime_text_spans_ptr); | |
| std::vector<std::string> suggestions_vec; | |
| AppendJavaStringArrayToStringVector(env, suggestions, &suggestions_vec); | |
| ui::ImeTextSpan ime_text_span = ui::ImeTextSpan( | |
| ui_type, static_cast<unsigned>(start), static_cast<unsigned>(end), | |
| ui::ImeTextSpan::Thickness::kThick, | |
| ui::ImeTextSpan::UnderlineStyle::kSolid, SK_ColorTRANSPARENT, | |
| static_cast<unsigned>(suggestion_highlight_color), suggestions_vec, | |
| SK_ColorTRANSPARENT, should_hide_suggestion_menu); | |
| ime_text_span.remove_on_finish_composing = remove_on_finish_composing; | |
| ime_text_span.underline_color = static_cast<unsigned>(underline_color); | |
| ime_text_spans->push_back(ime_text_span); | |
| } | |
| // Callback from Java to convert UnderlineSpan data to a | |
| // ui::ImeTextSpan instance, and append it to |ime_text_spans_ptr|. | |
| static void JNI_ImeAdapterImpl_AppendUnderlineSpan(JNIEnv*, | |
| int64_t ime_text_spans_ptr, | |
| int32_t start, | |
| int32_t end) { | |
| CHECK_GE(start, 0, base::NotFatalUntil::M159); | |
| CHECK_GE(end, 0, base::NotFatalUntil::M159); | |
| std::vector<ui::ImeTextSpan>* ime_text_spans = | |
| reinterpret_cast<std::vector<ui::ImeTextSpan>*>(ime_text_spans_ptr); | |
| ime_text_spans->push_back(ui::ImeTextSpan( | |
| ui::ImeTextSpan::Type::kComposition, static_cast<unsigned>(start), | |
| static_cast<unsigned>(end), ui::ImeTextSpan::Thickness::kThin, | |
| ui::ImeTextSpan::UnderlineStyle::kSolid, SK_ColorTRANSPARENT, | |
| SK_ColorTRANSPARENT, std::vector<std::string>())); | |
| } | |
| ImeAdapterAndroid::ImeAdapterAndroid(WebContents* web_contents) | |
| : RenderWidgetHostConnector(web_contents), rwhva_(nullptr) { | |
| } | |
| ImeAdapterAndroid::~ImeAdapterAndroid() { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (!obj.is_null()) { | |
| Java_ImeAdapterImpl_destroyFromNative(env, obj); | |
| } | |
| } | |
| base::android::ScopedJavaLocalRef<jobject> ImeAdapterAndroid::GetJavaObject( | |
| JNIEnv* env) { | |
| return Java_ImeAdapterImpl_get(env, reinterpret_cast<intptr_t>(this)); | |
| } | |
| void ImeAdapterAndroid::UpdateRenderProcessConnection( | |
| RenderWidgetHostViewAndroid* old_rwhva, | |
| RenderWidgetHostViewAndroid* new_rwhva) { | |
| if (old_rwhva) | |
| old_rwhva->set_ime_adapter(nullptr); | |
| if (new_rwhva) { | |
| new_rwhva->set_ime_adapter(this); | |
| if (!old_rwhva && new_rwhva) { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (!obj.is_null()) | |
| Java_ImeAdapterImpl_onConnectedToRenderProcess(env, obj); | |
| } | |
| } | |
| rwhva_ = new_rwhva; | |
| // Must be called after the new rwhva has been set. | |
| SetImeRenderWidgetHost(); | |
| } | |
| void ImeAdapterAndroid::UpdateState(const ui::mojom::TextInputState& state) { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (obj.is_null()) | |
| return; | |
| base::android::ScopedJavaLocalRef<jobjectArray> j_ime_text_spans; | |
| if (base::FeatureList::IsEnabled( | |
| blink::features::kAndroidSpellcheckFullApiBlink)) { | |
| j_ime_text_spans = ToImeTextSpanJniArray(env, state.ime_text_spans_info); | |
| } | |
| ScopedJavaLocalRef<jstring> jstring_text = | |
| ConvertUTF16ToJavaString(env, state.value.value_or(std::u16string())); | |
| Java_ImeAdapterImpl_updateState( | |
| env, obj, static_cast<int>(state.type), state.flags, state.mode, | |
| static_cast<int>(state.action), state.show_ime_if_needed, | |
| state.always_hide_ime, jstring_text, state.selection.start(), | |
| state.selection.end(), | |
| state.composition ? state.composition.value().start() : -1, | |
| state.composition ? state.composition.value().end() : -1, | |
| state.reply_to_request, | |
| static_cast<int>(state.last_vk_visibility_request), | |
| static_cast<int>(state.vk_policy), state.node_id, j_ime_text_spans); | |
| } | |
| void ImeAdapterAndroid::UpdateOnTouchDown() { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (obj.is_null()) | |
| return; | |
| Java_ImeAdapterImpl_updateOnTouchDown(env, obj); | |
| } | |
| void ImeAdapterAndroid::UpdateFrameInfo( | |
| const gfx::SelectionBound& selection_start, | |
| float dip_scale, | |
| float content_offset_ypix) { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (obj.is_null()) | |
| return; | |
| const bool has_insertion_marker = | |
| selection_start.type() != gfx::SelectionBound::EMPTY; | |
| const bool is_insertion_marker_visible = selection_start.visible(); | |
| const float insertion_marker_horizontal = | |
| has_insertion_marker ? selection_start.edge_start().x() : 0.0f; | |
| const float insertion_marker_top = | |
| has_insertion_marker ? selection_start.edge_start().y() : 0.0f; | |
| const float insertion_marker_bottom = | |
| has_insertion_marker ? selection_start.edge_end().y() : 0.0f; | |
| Java_ImeAdapterImpl_updateFrameInfo( | |
| env, obj, dip_scale, content_offset_ypix, has_insertion_marker, | |
| is_insertion_marker_visible, insertion_marker_horizontal, | |
| insertion_marker_top, insertion_marker_bottom); | |
| } | |
| void ImeAdapterAndroid::OnRenderFrameMetadataChangedAfterActivation( | |
| const gfx::SizeF& new_viewport_size) { | |
| if (old_viewport_size_ == new_viewport_size) | |
| return; | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (obj.is_null()) | |
| return; | |
| const bool surface_height_reduced = | |
| new_viewport_size.width() == old_viewport_size_.width() && | |
| new_viewport_size.height() < old_viewport_size_.height(); | |
| old_viewport_size_ = new_viewport_size; | |
| Java_ImeAdapterImpl_onResizeScrollableViewport(env, obj, | |
| surface_height_reduced); | |
| } | |
| void ImeAdapterAndroid::Initialize(JNIEnv* env) { | |
| RenderWidgetHostConnector::Initialize(); | |
| } | |
| bool ImeAdapterAndroid::SendKeyEvent(JNIEnv* env, | |
| const JavaRef<jobject>& original_key_event, | |
| int type, | |
| int modifiers, | |
| int64_t time_ms, | |
| int key_code, | |
| int scan_code, | |
| bool is_system_key, | |
| int unicode_char) { | |
| if (!rwhva_) | |
| return false; | |
| input::NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( | |
| env, original_key_event, type, modifiers, time_ms, key_code, scan_code, | |
| is_system_key, unicode_char); | |
| rwhva_->SendKeyEvent(event); | |
| return true; | |
| } | |
| void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | |
| const JavaRef<jobject>& obj, | |
| const JavaRef<jobject>& text, | |
| const JavaRef<jstring>& text_str, | |
| int relative_cursor_pos, | |
| bool is_text_suggestion_selected) { | |
| RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
| if (!rwhi) | |
| return; | |
| std::u16string text16 = ConvertJavaStringToUTF16(env, text_str); | |
| std::vector<ui::ImeTextSpan> ime_text_spans = | |
| GetImeTextSpansFromJava(env, obj, text, text16); | |
| // Default to plain underline if we didn't find any span that we care about. | |
| if (ime_text_spans.empty()) { | |
| ime_text_spans.push_back(ui::ImeTextSpan( | |
| ui::ImeTextSpan::Type::kComposition, 0, text16.length(), | |
| ui::ImeTextSpan::Thickness::kThin, | |
| ui::ImeTextSpan::UnderlineStyle::kSolid, SK_ColorTRANSPARENT, | |
| SK_ColorTRANSPARENT, std::vector<std::string>())); | |
| } | |
| // relative_cursor_pos is as described in the Android API for | |
| // InputConnection#setComposingText, whereas the parameters for | |
| // ImeSetComposition are relative to the start of the composition. | |
| if (relative_cursor_pos > 0) | |
| relative_cursor_pos = text16.length() + relative_cursor_pos - 1; | |
| rwhi->ImeSetComposition(text16, ime_text_spans, gfx::Range::InvalidRange(), | |
| relative_cursor_pos, relative_cursor_pos, | |
| is_text_suggestion_selected | |
| ? blink::mojom::ImeState::kTextSuggestionSelected | |
| : blink::mojom::ImeState::kNone); | |
| } | |
| void ImeAdapterAndroid::CommitText(JNIEnv* env, | |
| const JavaRef<jobject>& obj, | |
| const JavaRef<jobject>& text, | |
| const JavaRef<jstring>& text_str, | |
| int relative_cursor_pos) { | |
| RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
| if (!rwhi) | |
| return; | |
| std::u16string text16 = ConvertJavaStringToUTF16(env, text_str); | |
| std::vector<ui::ImeTextSpan> ime_text_spans = | |
| GetImeTextSpansFromJava(env, obj, text, text16); | |
| // relative_cursor_pos is as described in the Android API for | |
| // InputConnection#commitText, whereas the parameters for | |
| // ImeConfirmComposition are relative to the end of the composition. | |
| if (relative_cursor_pos > 0) | |
| relative_cursor_pos--; | |
| else | |
| relative_cursor_pos -= text16.length(); | |
| rwhi->ImeCommitText(text16, ime_text_spans, gfx::Range::InvalidRange(), | |
| relative_cursor_pos); | |
| } | |
| void ImeAdapterAndroid::ReplaceText( | |
| JNIEnv* env, | |
| const base::android::JavaRef<jobject>& obj, | |
| int start, | |
| int end, | |
| const base::android::JavaRef<jobject>& text, | |
| const base::android::JavaRef<jstring>& text_str, | |
| int relative_cursor_pos) { | |
| RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
| if (!rwhi) { | |
| return; | |
| } | |
| std::u16string text16 = ConvertJavaStringToUTF16(env, text_str); | |
| std::vector<ui::ImeTextSpan> ime_text_spans = | |
| GetImeTextSpansFromJava(env, obj, text, text16); | |
| // relative_cursor_pos is as described in the Android API for | |
| // InputConnection#commitText, whereas the parameters for | |
| // ImeConfirmComposition are relative to the end of the composition. | |
| if (relative_cursor_pos > 0) { | |
| relative_cursor_pos--; | |
| } else { | |
| relative_cursor_pos -= text16.length(); | |
| } | |
| rwhi->ImeCommitText(text16, ime_text_spans, gfx::Range(start, end), | |
| relative_cursor_pos); | |
| } | |
| void ImeAdapterAndroid::FinishComposingText(JNIEnv* env) { | |
| RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
| if (!rwhi) | |
| return; | |
| rwhi->ImeFinishComposingText(true); | |
| } | |
| bool ImeAdapterAndroid::InsertMediaFromBytes( | |
| JNIEnv* env, | |
| int render_process_id, | |
| int render_frame_id, | |
| const base::android::JavaRef<jbyteArray>& bytes, | |
| const base::android::JavaRef<jstring>& extension) { | |
| RenderFrameHost* target_rfh = | |
| RenderFrameHost::FromID(render_process_id, render_frame_id); | |
| if (!target_rfh || GetFocusedFrame() != target_rfh) { | |
| return false; | |
| } | |
| auto* input_handler = GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) { | |
| return false; | |
| } | |
| if (bytes.is_null()) { | |
| return false; | |
| } | |
| size_t size = bytes.GetSize(env); | |
| if (size == 0) { | |
| return false; | |
| } | |
| mojo_base::BigBuffer big_buffer(size); | |
| bytes.CopyTo(env, big_buffer.data(), size); | |
| input_handler->PasteFromImageBytes( | |
| std::move(big_buffer), | |
| base::android::ConvertJavaStringToUTF8(env, extension)); | |
| return true; | |
| } | |
| ScopedJavaLocalRef<jobject> ImeAdapterAndroid::java_ime_adapter_for_testing( | |
| JNIEnv* env) { | |
| return GetJavaObject(env); | |
| } | |
| void ImeAdapterAndroid::CancelComposition() { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (!obj.is_null()) | |
| Java_ImeAdapterImpl_cancelComposition(env, obj); | |
| } | |
| void ImeAdapterAndroid::FocusedNodeChanged( | |
| bool is_editable_node, | |
| const gfx::Rect& node_bounds_in_screen) { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (!obj.is_null()) { | |
| Java_ImeAdapterImpl_focusedNodeChanged( | |
| env, obj, is_editable_node, node_bounds_in_screen.x(), | |
| node_bounds_in_screen.y(), node_bounds_in_screen.right(), | |
| node_bounds_in_screen.bottom()); | |
| } | |
| } | |
| bool ImeAdapterAndroid::ShouldInitiateStylusWriting() { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (!obj.is_null()) { | |
| return Java_ImeAdapterImpl_shouldInitiateStylusWriting(env, obj); | |
| } | |
| return false; | |
| } | |
| void ImeAdapterAndroid::OnEditElementFocusedForStylusWriting( | |
| const gfx::Rect& focused_edit_bounds, | |
| const gfx::Rect& caret_bounds) { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (!obj.is_null()) { | |
| gfx::Point caret_center = caret_bounds.CenterPoint(); | |
| Java_ImeAdapterImpl_onEditElementFocusedForStylusWriting( | |
| env, obj, focused_edit_bounds.x(), focused_edit_bounds.y(), | |
| focused_edit_bounds.right(), focused_edit_bounds.bottom(), | |
| caret_center.x(), caret_center.y()); | |
| } | |
| } | |
| void ImeAdapterAndroid::HandleStylusWritingGestureAction( | |
| JNIEnv* env, | |
| const int32_t id, | |
| const base::android::JavaRef<jobject>& jgesture_data_byte_buffer) { | |
| auto* input_handler = GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) | |
| return; | |
| blink::mojom::StylusWritingGestureDataPtr gesture_data; | |
| if (!blink::mojom::StylusWritingGestureData::Deserialize( | |
| base::android::JavaByteBufferToSpan(env, jgesture_data_byte_buffer), | |
| &gesture_data)) { | |
| NOTREACHED(); | |
| } | |
| input_handler->HandleStylusWritingGestureAction( | |
| std::move(gesture_data), | |
| base::BindOnce(&ImeAdapterAndroid::OnStylusWritingGestureActionCompleted, | |
| weak_factory_.GetWeakPtr(), id)); | |
| } | |
| void ImeAdapterAndroid::CancelPreviewGesture() { | |
| blink::mojom::FrameWidgetInputHandler* input_handler = | |
| GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) { | |
| return; | |
| } | |
| input_handler->CancelStylusGesturePreview(); | |
| } | |
| void ImeAdapterAndroid::OnStylusWritingGestureActionCompleted( | |
| int id, | |
| blink::mojom::HandwritingGestureResult result) { | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (!obj.is_null()) { | |
| Java_ImeAdapterImpl_onStylusWritingGestureActionCompleted(env, obj, id, | |
| (int)result); | |
| } | |
| } | |
| void ImeAdapterAndroid::SetImeRenderWidgetHost() { | |
| if (!rwhva_) { | |
| return; | |
| } | |
| JNIEnv* env = AttachCurrentThread(); | |
| ScopedJavaLocalRef<jobject> obj = GetJavaObject(env); | |
| if (obj.is_null()) { | |
| return; | |
| } | |
| // Use a pending remote so we can pass it to Blink. | |
| mojo::PendingRemote<blink::mojom::ImeRenderWidgetHost> ime_render_widget_host; | |
| auto receiver = ime_render_widget_host.InitWithNewPipeAndPassReceiver(); | |
| Java_ImeAdapterImpl_bindImeRenderHost(env, obj, | |
| receiver.PassPipe().release().value()); | |
| rwhva_->PassImeRenderWidgetHost(std::move(ime_render_widget_host)); | |
| } | |
| void ImeAdapterAndroid::AdvanceFocusForIME(JNIEnv* env, int32_t focus_type) { | |
| RenderFrameHostImpl* rfh = | |
| static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | |
| if (!rfh) | |
| return; | |
| rfh->GetAssociatedLocalFrame()->AdvanceFocusForIME( | |
| static_cast<blink::mojom::FocusType>(focus_type)); | |
| } | |
| ScopedJavaLocalRef<jobjectArray> ImeAdapterAndroid::GetSupportedMimeTypes( | |
| JNIEnv* env) { | |
| RenderFrameHostImpl* render_frame_host = | |
| static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | |
| if (!render_frame_host) { | |
| return ScopedJavaLocalRef<jobjectArray>(); | |
| } | |
| std::vector<std::string> supported_mime_types; | |
| if (render_frame_host->has_focused_richly_editable_element()) { | |
| supported_mime_types.push_back("image/*"); | |
| } | |
| return ToJavaArrayOfStrings(env, supported_mime_types); | |
| } | |
| void ImeAdapterAndroid::SetEditableSelectionOffsets(JNIEnv*, | |
| int start, | |
| int end) { | |
| auto* input_handler = GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) | |
| return; | |
| input_handler->SetEditableSelectionOffsets(start, end); | |
| } | |
| void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, int start, int end) { | |
| auto* input_handler = GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) | |
| return; | |
| std::vector<ui::ImeTextSpan> ime_text_spans; | |
| ime_text_spans.push_back(ui::ImeTextSpan( | |
| ui::ImeTextSpan::Type::kComposition, 0, end - start, | |
| ui::ImeTextSpan::Thickness::kThin, | |
| ui::ImeTextSpan::UnderlineStyle::kSolid, SK_ColorTRANSPARENT, | |
| SK_ColorTRANSPARENT, std::vector<std::string>())); | |
| input_handler->SetCompositionFromExistingText(start, end, ime_text_spans); | |
| } | |
| void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, int before, int after) { | |
| auto* input_handler = GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) | |
| return; | |
| input_handler->DeleteSurroundingText(before, after); | |
| } | |
| void ImeAdapterAndroid::DeleteSurroundingTextInCodePoints(JNIEnv*, | |
| int before, | |
| int after) { | |
| auto* input_handler = GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) | |
| return; | |
| input_handler->DeleteSurroundingTextInCodePoints(before, after); | |
| } | |
| bool ImeAdapterAndroid::RequestTextInputStateUpdate(JNIEnv* env) { | |
| RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
| if (!rwhi) | |
| return false; | |
| rwhi->GetWidgetInputHandler()->RequestTextInputStateUpdate(); | |
| return true; | |
| } | |
| void ImeAdapterAndroid::RequestCursorUpdate(JNIEnv* env, | |
| bool immediate_request, | |
| bool monitor_request) { | |
| RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
| if (!rwhi) | |
| return; | |
| rwhi->GetWidgetInputHandler()->RequestCompositionUpdates(immediate_request, | |
| monitor_request); | |
| } | |
| RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { | |
| CHECK_CURRENTLY_ON(BrowserThread::UI, base::NotFatalUntil::M159); | |
| return rwhva_ ? rwhva_->GetFocusedWidget() : nullptr; | |
| } | |
| RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | |
| CHECK_CURRENTLY_ON(BrowserThread::UI, base::NotFatalUntil::M159); | |
| // 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; | |
| } | |
| blink::mojom::FrameWidgetInputHandler* | |
| ImeAdapterAndroid::GetFocusedFrameWidgetInputHandler() { | |
| RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | |
| if (!rwhi) | |
| return nullptr; | |
| return rwhi->GetFrameWidgetInputHandler(); | |
| } | |
| std::vector<ui::ImeTextSpan> ImeAdapterAndroid::GetImeTextSpansFromJava( | |
| JNIEnv* env, | |
| const base::android::JavaRef<jobject>& obj, | |
| const base::android::JavaRef<jobject>& text, | |
| const std::u16string& text16) { | |
| std::vector<ui::ImeTextSpan> ime_text_spans; | |
| // Iterate over spans in |text|, dispatch those that we care about (e.g., | |
| // BackgroundColorSpan) to a matching callback (e.g., | |
| // AppendBackgroundColorSpan()), and populate |ime_text_spans|. | |
| Java_ImeAdapterImpl_populateImeTextSpansFromJava( | |
| env, obj, text, reinterpret_cast<int64_t>(&ime_text_spans)); | |
| std::sort(ime_text_spans.begin(), ime_text_spans.end(), | |
| [](const ui::ImeTextSpan& span1, const ui::ImeTextSpan& span2) { | |
| return span1.start_offset < span2.start_offset; | |
| }); | |
| return ime_text_spans; | |
| } | |
| void ImeAdapterAndroid::PerformSpellCheck(JNIEnv* env) { | |
| RenderFrameHostImpl* rfh = | |
| static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | |
| if (!rfh) { | |
| return; | |
| } | |
| rfh->GetAssociatedLocalFrame()->PerformFullContentSpellCheck(); | |
| } | |
| void ImeAdapterAndroid::AppendAutocorrectUnderlineSpan(JNIEnv* env, | |
| int32_t start, | |
| int32_t end) { | |
| if (!base::FeatureList::IsEnabled(features::kAndroidPkAutocorrectUnderline) && | |
| !base::FeatureList::IsEnabled( | |
| features::kAndroidPkAutocorrectUnderlineV2)) { | |
| return; | |
| } | |
| blink::mojom::FrameWidgetInputHandler* input_handler = | |
| GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) { | |
| return; | |
| } | |
| ui::ImeTextSpan ime_text_span = | |
| ui::ImeTextSpan(ui::ImeTextSpan::Type::kAutocorrect, | |
| /*start_offset=*/0, | |
| /*end_offset=*/static_cast<unsigned>(end - start), | |
| ui::ImeTextSpan::Thickness::kThick, | |
| ui::ImeTextSpan::UnderlineStyle::kDot); | |
| ime_text_span.underline_color = | |
| SkColorSetA(gfx::kGoogleGrey700, SK_AlphaOPAQUE * 0.7); | |
| input_handler->AddImeTextSpansToExistingText(start, end, {ime_text_span}); | |
| } | |
| void ImeAdapterAndroid::ClearAllAutocorrectUnderlineSpans(JNIEnv* env) { | |
| if (!base::FeatureList::IsEnabled(features::kAndroidPkAutocorrectUnderline) && | |
| !base::FeatureList::IsEnabled( | |
| features::kAndroidPkAutocorrectUnderlineV2)) { | |
| return; | |
| } | |
| blink::mojom::FrameWidgetInputHandler* input_handler = | |
| GetFocusedFrameWidgetInputHandler(); | |
| if (!input_handler) { | |
| return; | |
| } | |
| input_handler->ClearImeTextSpansByType(0, | |
| std::numeric_limits<uint32_t>::max(), | |
| ui::ImeTextSpan::Type::kAutocorrect); | |
| } | |
| } // namespace content | |
| DEFINE_JNI(ImeAdapterImpl) |