| 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/frame_host/interstitial_page_impl.h" | |
| 18 #include "content/browser/frame_host/render_frame_host_impl.h" | 17 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 19 #include "content/browser/renderer_host/render_view_host_delegate.h" | 18 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 20 #include "content/browser/renderer_host/render_widget_host_impl.h" | 19 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 21 #include "content/browser/renderer_host/render_widget_host_view_android.h" | 20 #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" | 21 #include "content/common/input_messages.h" |
| 24 #include "content/common/view_messages.h" | 22 #include "content/common/view_messages.h" |
| 25 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/native_web_keyboard_event.h" | 24 #include "content/public/browser/native_web_keyboard_event.h" |
| 27 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 28 #include "jni/ImeAdapter_jni.h" | 26 #include "jni/ImeAdapter_jni.h" |
| 29 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 27 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 30 #include "third_party/WebKit/public/platform/WebTextInputType.h" | 28 #include "third_party/WebKit/public/platform/WebTextInputType.h" |
| 31 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" | 29 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" |
| 32 | 30 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | 106 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
| 109 underlines_ptr); | 107 underlines_ptr); |
| 110 underlines->push_back(blink::WebCompositionUnderline( | 108 underlines->push_back(blink::WebCompositionUnderline( |
| 111 static_cast<unsigned>(start), static_cast<unsigned>(end), SK_ColorBLACK, | 109 static_cast<unsigned>(start), static_cast<unsigned>(end), SK_ColorBLACK, |
| 112 false, SK_ColorTRANSPARENT)); | 110 false, SK_ColorTRANSPARENT)); |
| 113 } | 111 } |
| 114 | 112 |
| 115 ImeAdapterAndroid::ImeAdapterAndroid(JNIEnv* env, | 113 ImeAdapterAndroid::ImeAdapterAndroid(JNIEnv* env, |
| 116 const JavaParamRef<jobject>& obj, | 114 const JavaParamRef<jobject>& obj, |
| 117 WebContents* web_contents) | 115 WebContents* web_contents) |
| 118 : WebContentsObserver(web_contents), rwhva_(nullptr) { | 116 : RenderWidgetHostConnector(web_contents), rwhva_(nullptr) { |
| 119 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, obj); | 117 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, obj); |
| 120 } | 118 } |
| 121 | 119 |
| 122 ImeAdapterAndroid::~ImeAdapterAndroid() { | 120 ImeAdapterAndroid::~ImeAdapterAndroid() { |
| 123 JNIEnv* env = AttachCurrentThread(); | 121 JNIEnv* env = AttachCurrentThread(); |
| 124 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 122 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 125 if (!obj.is_null()) | 123 if (!obj.is_null()) |
| 126 Java_ImeAdapter_destroy(env, obj); | 124 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 } | 125 } |
| 176 | 126 |
| 177 void ImeAdapterAndroid::UpdateRenderProcessConnection( | 127 void ImeAdapterAndroid::UpdateRenderProcessConnection( |
| 128 RenderWidgetHostViewAndroid* old_rwhva, |
| 178 RenderWidgetHostViewAndroid* new_rwhva) { | 129 RenderWidgetHostViewAndroid* new_rwhva) { |
| 179 if (rwhva_.get() == new_rwhva) | 130 if (old_rwhva) |
| 180 return; | 131 old_rwhva->set_ime_adapter(nullptr); |
| 181 if (rwhva_) | |
| 182 rwhva_->set_ime_adapter(nullptr); | |
| 183 if (new_rwhva) { | 132 if (new_rwhva) { |
| 184 new_rwhva->set_ime_adapter(this); | 133 new_rwhva->set_ime_adapter(this); |
| 185 rwhva_ = new_rwhva->GetWeakPtrAndroid(); | 134 if (!old_rwhva && new_rwhva) { |
| 186 } else { | 135 JNIEnv* env = AttachCurrentThread(); |
| 187 rwhva_.reset(); | 136 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 137 if (!obj.is_null()) |
| 138 Java_ImeAdapter_onConnectedToRenderProcess(env, obj); |
| 139 } |
| 188 } | 140 } |
| 141 rwhva_ = new_rwhva; |
| 189 } | 142 } |
| 190 | 143 |
| 191 void ImeAdapterAndroid::UpdateState(const TextInputState& state) { | 144 void ImeAdapterAndroid::UpdateState(const TextInputState& state) { |
| 192 JNIEnv* env = AttachCurrentThread(); | 145 JNIEnv* env = AttachCurrentThread(); |
| 193 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 146 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 194 if (obj.is_null()) | 147 if (obj.is_null()) |
| 195 return; | 148 return; |
| 196 | 149 |
| 197 ScopedJavaLocalRef<jstring> jstring_text = | 150 ScopedJavaLocalRef<jstring> jstring_text = |
| 198 ConvertUTF8ToJavaString(env, state.value); | 151 ConvertUTF8ToJavaString(env, state.value); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 Java_ImeAdapter_populateUnderlinesFromSpans( | 387 Java_ImeAdapter_populateUnderlinesFromSpans( |
| 435 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 388 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
| 436 | 389 |
| 437 // Sort spans by |.startOffset|. | 390 // Sort spans by |.startOffset|. |
| 438 std::sort(underlines.begin(), underlines.end()); | 391 std::sort(underlines.begin(), underlines.end()); |
| 439 | 392 |
| 440 return underlines; | 393 return underlines; |
| 441 } | 394 } |
| 442 | 395 |
| 443 } // namespace content | 396 } // namespace content |
| OLD | NEW |