Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1195)

Unified Diff: content/browser/renderer_host/ime_adapter_android.cc

Issue 2752113005: Let ImeAdapterAndroid have the same lifecycle as its Java peer (Closed)
Patch Set: background rwhva Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/renderer_host/ime_adapter_android.cc
diff --git a/content/browser/renderer_host/ime_adapter_android.cc b/content/browser/renderer_host/ime_adapter_android.cc
index bb8a938a2b37f261362f96e56819d518d5ee921f..1055871ad88e80d6920fe72187d7de3b3da8f27a 100644
--- a/content/browser/renderer_host/ime_adapter_android.cc
+++ b/content/browser/renderer_host/ime_adapter_android.cc
@@ -31,6 +31,8 @@
using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF16;
using base::android::JavaParamRef;
+using base::android::ScopedJavaLocalRef;
+using base::WeakPtr;
namespace content {
namespace {
@@ -63,6 +65,11 @@ bool RegisterImeAdapter(JNIEnv* env) {
return RegisterNativesImpl(env);
}
+jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
+ ImeAdapterAndroid* adapter = new ImeAdapterAndroid(env, obj);
+ return reinterpret_cast<intptr_t>(adapter);
+}
+
// Callback from Java to convert BackgroundColorSpan data to a
// blink::WebCompositionUnderline instance, and append it to |underlines_ptr|.
void AppendBackgroundColorSpan(JNIEnv*,
@@ -105,16 +112,29 @@ void AppendUnderlineSpan(JNIEnv*,
SK_ColorTRANSPARENT));
}
-ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva)
- : rwhva_(rwhva) {
- DCHECK(rwhva_);
+ImeAdapterAndroid::ImeAdapterAndroid(JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ java_ime_adapter_ = JavaObjectWeakGlobalRef(env, obj);
}
ImeAdapterAndroid::~ImeAdapterAndroid() {
JNIEnv* env = AttachCurrentThread();
- base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
+ ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
+ if (!obj.is_null())
+ Java_ImeAdapter_destroy(env, obj);
+
+ if (rwhva_)
+ rwhva_->ConnectImeAdapter(nullptr);
+}
+
+void ImeAdapterAndroid::SetRenderWidgetHostViewAndroid(
+ const base::WeakPtr<RenderWidgetHostViewAndroid> rwhva) {
+ rwhva_.reset();
+ rwhva_ = rwhva;
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
if (!obj.is_null())
- Java_ImeAdapter_detach(env, obj);
+ Java_ImeAdapter_onConnectedToRenderProcess(env, obj);
}
bool ImeAdapterAndroid::SendKeyEvent(
@@ -128,6 +148,8 @@ bool ImeAdapterAndroid::SendKeyEvent(
int scan_code,
bool is_system_key,
int unicode_char) {
+ if (!rwhva_)
+ return false;
NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent(
env, original_key_event, type, modifiers, time_ms, key_code, scan_code,
is_system_key, unicode_char);
@@ -200,25 +222,18 @@ void ImeAdapterAndroid::FinishComposingText(JNIEnv* env,
rwhi->ImeFinishComposingText(true);
}
-void ImeAdapterAndroid::AttachImeAdapter(
- JNIEnv* env,
- const JavaParamRef<jobject>& java_object) {
- java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object);
-}
-
void ImeAdapterAndroid::CancelComposition() {
- base::android::ScopedJavaLocalRef<jobject> obj =
- java_ime_adapter_.get(AttachCurrentThread());
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
if (!obj.is_null())
- Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj);
+ Java_ImeAdapter_cancelComposition(env, obj);
}
void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) {
- base::android::ScopedJavaLocalRef<jobject> obj =
- java_ime_adapter_.get(AttachCurrentThread());
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
if (!obj.is_null()) {
- Java_ImeAdapter_focusedNodeChanged(AttachCurrentThread(), obj,
- is_editable_node);
+ Java_ImeAdapter_focusedNodeChanged(env, obj, is_editable_node);
}
}
@@ -238,7 +253,7 @@ void ImeAdapterAndroid::SetEditableSelectionOffsets(
void ImeAdapterAndroid::SetCharacterBounds(
const std::vector<gfx::RectF>& character_bounds) {
JNIEnv* env = AttachCurrentThread();
- base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
+ ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
if (obj.is_null())
return;
@@ -316,14 +331,9 @@ void ImeAdapterAndroid::RequestCursorUpdate(
rwhi->GetRoutingID(), immediate_request, monitor_request));
}
-void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env,
- const JavaParamRef<jobject>&) {
- java_ime_adapter_.reset();
-}
-
RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
- return rwhva_->GetFocusedWidget();
+ return rwhva_ ? rwhva_->GetFocusedWidget() : nullptr;
}
RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() {
@@ -331,6 +341,8 @@ RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() {
// 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 (!rwh || !rwh->delegate())

Powered by Google App Engine
This is Rietveld 408576698