| 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 : connector_(new Connector(web_contents, this)), 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 } | 125 } |
| 130 | 126 |
| 131 RenderWidgetHostViewAndroid* ImeAdapterAndroid::GetRenderWidgetHostViewAndroid() | 127 // ImeAdapterAndroid::Connector methods |
| 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 | 128 |
| 146 void ImeAdapterAndroid::RenderViewReady() { | 129 ImeAdapterAndroid::Connector::Connector(WebContents* web_contents, |
| 147 UpdateRenderProcessConnection(GetRenderWidgetHostViewAndroid()); | 130 ImeAdapterAndroid* ime_adapter) |
| 131 : RenderWidgetHostConnector(web_contents), ime_adapter_(ime_adapter) {} |
| 148 | 132 |
| 149 JNIEnv* env = AttachCurrentThread(); | 133 void ImeAdapterAndroid::Connector::UpdateRenderWidgetHostView( |
| 150 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 134 RenderWidgetHostViewAndroid* old_rwhva, |
| 151 if (!obj.is_null()) | 135 RenderWidgetHostViewAndroid* new_rwhva) { |
| 152 Java_ImeAdapter_onConnectedToRenderProcess(env, obj); | 136 if (old_rwhva) |
| 153 } | 137 old_rwhva->set_ime_adapter(nullptr); |
| 154 | 138 if (new_rwhva) { |
| 155 void ImeAdapterAndroid::RenderViewHostChanged(RenderViewHost* old_host, | 139 new_rwhva->set_ime_adapter(ime_adapter_); |
| 156 RenderViewHost* new_host) { | 140 ime_adapter_->SetRenderWidgetHostView(new_rwhva); |
| 157 if (new_host) { | |
| 158 UpdateRenderProcessConnection(static_cast<RenderWidgetHostViewAndroid*>( | |
| 159 new_host->GetWidget()->GetView())); | |
| 160 } else { | |
| 161 UpdateRenderProcessConnection(nullptr); | |
| 162 } | 141 } |
| 163 } | 142 } |
| 164 | 143 |
| 165 void ImeAdapterAndroid::DidAttachInterstitialPage() { | 144 void ImeAdapterAndroid::SetRenderWidgetHostView( |
| 166 UpdateRenderProcessConnection(GetRenderWidgetHostViewAndroid()); | 145 RenderWidgetHostViewAndroid* rwhva) { |
| 167 } | 146 if (rwhva_ == rwhva) |
| 168 | |
| 169 void ImeAdapterAndroid::DidDetachInterstitialPage() { | |
| 170 UpdateRenderProcessConnection(GetRenderWidgetHostViewAndroid()); | |
| 171 } | |
| 172 | |
| 173 void ImeAdapterAndroid::WebContentsDestroyed() { | |
| 174 delete this; | |
| 175 } | |
| 176 | |
| 177 void ImeAdapterAndroid::UpdateRenderProcessConnection( | |
| 178 RenderWidgetHostViewAndroid* new_rwhva) { | |
| 179 if (rwhva_.get() == new_rwhva) | |
| 180 return; | 147 return; |
| 181 if (rwhva_) | 148 if (!rwhva_ && rwhva) { |
| 182 rwhva_->set_ime_adapter(nullptr); | 149 JNIEnv* env = AttachCurrentThread(); |
| 183 if (new_rwhva) { | 150 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 184 new_rwhva->set_ime_adapter(this); | 151 if (!obj.is_null()) |
| 185 rwhva_ = new_rwhva->GetWeakPtrAndroid(); | 152 Java_ImeAdapter_onConnectedToRenderProcess(env, obj); |
| 186 } else { | |
| 187 rwhva_.reset(); | |
| 188 } | 153 } |
| 154 rwhva_ = rwhva; |
| 189 } | 155 } |
| 190 | 156 |
| 191 void ImeAdapterAndroid::UpdateState(const TextInputState& state) { | 157 void ImeAdapterAndroid::UpdateState(const TextInputState& state) { |
| 192 JNIEnv* env = AttachCurrentThread(); | 158 JNIEnv* env = AttachCurrentThread(); |
| 193 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 159 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 194 if (obj.is_null()) | 160 if (obj.is_null()) |
| 195 return; | 161 return; |
| 196 | 162 |
| 197 ScopedJavaLocalRef<jstring> jstring_text = | 163 ScopedJavaLocalRef<jstring> jstring_text = |
| 198 ConvertUTF8ToJavaString(env, state.value); | 164 ConvertUTF8ToJavaString(env, state.value); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 Java_ImeAdapter_populateUnderlinesFromSpans( | 400 Java_ImeAdapter_populateUnderlinesFromSpans( |
| 435 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 401 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
| 436 | 402 |
| 437 // Sort spans by |.startOffset|. | 403 // Sort spans by |.startOffset|. |
| 438 std::sort(underlines.begin(), underlines.end()); | 404 std::sort(underlines.begin(), underlines.end()); |
| 439 | 405 |
| 440 return underlines; | 406 return underlines; |
| 441 } | 407 } |
| 442 | 408 |
| 443 } // namespace content | 409 } // namespace content |
| OLD | NEW |