1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/ime_adapter_android.h"
7 #include <android/input.h>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "content/browser/frame_host/frame_tree.h"
15 #include "content/browser/frame_host/frame_tree_node.h"
16 #include "content/browser/frame_host/render_frame_host_impl.h"
17 #include "content/browser/renderer_host/render_view_host_delegate.h"
18 #include "content/browser/renderer_host/render_view_host_impl.h"
19 #include "content/browser/renderer_host/render_widget_host_impl.h"
20 #include "content/browser/renderer_host/render_widget_host_view_android.h"
21 #include "content/common/frame_messages.h"
22 #include "content/common/view_messages.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/native_web_keyboard_event.h"
25 #include "content/public/browser/web_contents.h"
26 #include "jni/ImeAdapter_jni.h"
27 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
28 #include "third_party/WebKit/public/web/WebInputEvent.h"
30 using base::android::AttachCurrentThread
;
31 using base::android::ConvertJavaStringToUTF16
;
36 // Maps a java KeyEvent into a NativeWebKeyboardEvent.
37 // |java_key_event| is used to maintain a globalref for KeyEvent.
38 // |action| will help determine the WebInputEvent type.
39 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create
40 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key
41 // as a key press of character \r.
42 NativeWebKeyboardEvent
NativeWebKeyboardEventFromKeyEvent(
44 jobject java_key_event
,
51 blink::WebInputEvent::Type type
= blink::WebInputEvent::Undefined
;
52 if (action
== AKEY_EVENT_ACTION_DOWN
)
53 type
= blink::WebInputEvent::RawKeyDown
;
54 else if (action
== AKEY_EVENT_ACTION_UP
)
55 type
= blink::WebInputEvent::KeyUp
;
56 return NativeWebKeyboardEvent(java_key_event
, type
, modifiers
,
57 time_ms
/ 1000.0, key_code
, unicode_char
, is_system_key
);
60 } // anonymous namespace
62 bool RegisterImeAdapter(JNIEnv
* env
) {
63 if (!RegisterNativesImpl(env
))
66 Java_ImeAdapter_initializeWebInputEvents(env
,
67 blink::WebInputEvent::RawKeyDown
,
68 blink::WebInputEvent::KeyUp
,
69 blink::WebInputEvent::Char
,
70 blink::WebInputEvent::ShiftKey
,
71 blink::WebInputEvent::AltKey
,
72 blink::WebInputEvent::ControlKey
,
73 blink::WebInputEvent::CapsLockOn
,
74 blink::WebInputEvent::NumLockOn
);
75 Java_ImeAdapter_initializeTextInputTypes(
77 ui::TEXT_INPUT_TYPE_NONE
,
78 ui::TEXT_INPUT_TYPE_TEXT
,
79 ui::TEXT_INPUT_TYPE_TEXT_AREA
,
80 ui::TEXT_INPUT_TYPE_PASSWORD
,
81 ui::TEXT_INPUT_TYPE_SEARCH
,
82 ui::TEXT_INPUT_TYPE_URL
,
83 ui::TEXT_INPUT_TYPE_EMAIL
,
84 ui::TEXT_INPUT_TYPE_TELEPHONE
,
85 ui::TEXT_INPUT_TYPE_NUMBER
,
86 ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE
);
90 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid
* rwhva
)
94 ImeAdapterAndroid::~ImeAdapterAndroid() {
95 JNIEnv
* env
= AttachCurrentThread();
96 base::android::ScopedJavaLocalRef
<jobject
> obj
= java_ime_adapter_
.get(env
);
98 Java_ImeAdapter_detach(env
, obj
.obj());
101 bool ImeAdapterAndroid::SendSyntheticKeyEvent(JNIEnv
*,
107 NativeWebKeyboardEvent
event(static_cast<blink::WebInputEvent::Type
>(type
),
108 0 /* modifiers */, time_ms
/ 1000.0, key_code
,
109 text
, false /* is_system_key */);
110 rwhva_
->SendKeyEvent(event
);
114 bool ImeAdapterAndroid::SendKeyEvent(JNIEnv
* env
, jobject
,
115 jobject original_key_event
,
116 int action
, int modifiers
,
117 long time_ms
, int key_code
,
118 bool is_system_key
, int unicode_char
) {
119 NativeWebKeyboardEvent event
= NativeWebKeyboardEventFromKeyEvent(
120 env
, original_key_event
, action
, modifiers
,
121 time_ms
, key_code
, is_system_key
, unicode_char
);
122 bool key_down_text_insertion
=
123 event
.type
== blink::WebInputEvent::RawKeyDown
&& event
.text
[0];
124 // If we are going to follow up with a synthetic Char event, then that's the
125 // one we expect to test if it's handled or unhandled, so skip handling the
126 // "real" event in the browser.
127 event
.skip_in_browser
= key_down_text_insertion
;
128 rwhva_
->SendKeyEvent(event
);
129 if (key_down_text_insertion
) {
130 // Send a Char event, but without an os_event since we don't want to
131 // roundtrip back to java such synthetic event.
132 NativeWebKeyboardEvent
char_event(blink::WebInputEvent::Char
, modifiers
,
133 time_ms
/ 1000.0, key_code
, unicode_char
,
135 char_event
.skip_in_browser
= key_down_text_insertion
;
136 rwhva_
->SendKeyEvent(char_event
);
141 void ImeAdapterAndroid::SetComposingText(JNIEnv
* env
, jobject
, jstring text
,
142 int new_cursor_pos
) {
143 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
147 base::string16 text16
= ConvertJavaStringToUTF16(env
, text
);
148 std::vector
<blink::WebCompositionUnderline
> underlines
;
149 underlines
.push_back(
150 blink::WebCompositionUnderline(0, text16
.length(), SK_ColorBLACK
,
152 // new_cursor_position is as described in the Android API for
153 // InputConnection#setComposingText, whereas the parameters for
154 // ImeSetComposition are relative to the start of the composition.
155 if (new_cursor_pos
> 0)
156 new_cursor_pos
= text16
.length() + new_cursor_pos
- 1;
158 rwhi
->ImeSetComposition(text16
, underlines
, new_cursor_pos
, new_cursor_pos
);
161 void ImeAdapterAndroid::CommitText(JNIEnv
* env
, jobject
, jstring text
) {
162 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
166 base::string16 text16
= ConvertJavaStringToUTF16(env
, text
);
167 rwhi
->ImeConfirmComposition(text16
, gfx::Range::InvalidRange(), false);
170 void ImeAdapterAndroid::FinishComposingText(JNIEnv
* env
, jobject
) {
171 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
175 rwhi
->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(),
179 void ImeAdapterAndroid::AttachImeAdapter(JNIEnv
* env
, jobject java_object
) {
180 java_ime_adapter_
= JavaObjectWeakGlobalRef(env
, java_object
);
183 void ImeAdapterAndroid::CancelComposition() {
184 base::android::ScopedJavaLocalRef
<jobject
> obj
=
185 java_ime_adapter_
.get(AttachCurrentThread());
187 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj
.obj());
190 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node
) {
191 base::android::ScopedJavaLocalRef
<jobject
> obj
=
192 java_ime_adapter_
.get(AttachCurrentThread());
193 if (!obj
.is_null()) {
194 Java_ImeAdapter_focusedNodeChanged(AttachCurrentThread(),
200 void ImeAdapterAndroid::SetEditableSelectionOffsets(JNIEnv
*, jobject
,
201 int start
, int end
) {
202 RenderFrameHost
* rfh
= GetFocusedFrame();
206 rfh
->Send(new FrameMsg_SetEditableSelectionOffsets(rfh
->GetRoutingID(),
210 void ImeAdapterAndroid::SetComposingRegion(JNIEnv
*, jobject
,
211 int start
, int end
) {
212 RenderFrameHost
* rfh
= GetFocusedFrame();
216 std::vector
<blink::WebCompositionUnderline
> underlines
;
217 underlines
.push_back(
218 blink::WebCompositionUnderline(0, end
- start
, SK_ColorBLACK
, false));
220 rfh
->Send(new FrameMsg_SetCompositionFromExistingText(
221 rfh
->GetRoutingID(), start
, end
, underlines
));
224 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv
*, jobject
,
225 int before
, int after
) {
226 RenderFrameHostImpl
* rfh
=
227 static_cast<RenderFrameHostImpl
*>(GetFocusedFrame());
229 rfh
->ExtendSelectionAndDelete(before
, after
);
232 void ImeAdapterAndroid::Unselect(JNIEnv
* env
, jobject
) {
233 WebContents
* wc
= GetWebContents();
238 void ImeAdapterAndroid::SelectAll(JNIEnv
* env
, jobject
) {
239 WebContents
* wc
= GetWebContents();
244 void ImeAdapterAndroid::Cut(JNIEnv
* env
, jobject
) {
245 WebContents
* wc
= GetWebContents();
250 void ImeAdapterAndroid::Copy(JNIEnv
* env
, jobject
) {
251 WebContents
* wc
= GetWebContents();
256 void ImeAdapterAndroid::Paste(JNIEnv
* env
, jobject
) {
257 WebContents
* wc
= GetWebContents();
262 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv
* env
, jobject
) {
263 java_ime_adapter_
.reset();
266 RenderWidgetHostImpl
* ImeAdapterAndroid::GetRenderWidgetHostImpl() {
267 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
269 RenderWidgetHost
* rwh
= rwhva_
->GetRenderWidgetHost();
273 return RenderWidgetHostImpl::From(rwh
);
276 RenderFrameHost
* ImeAdapterAndroid::GetFocusedFrame() {
277 RenderWidgetHostImpl
* rwh
= GetRenderWidgetHostImpl();
280 if (!rwh
->IsRenderView())
282 RenderViewHost
* rvh
= RenderViewHost::From(rwh
);
283 FrameTreeNode
* focused_frame
=
284 rvh
->GetDelegate()->GetFrameTree()->GetFocusedFrame();
288 return focused_frame
->current_frame_host();
291 WebContents
* ImeAdapterAndroid::GetWebContents() {
292 RenderWidgetHostImpl
* rwh
= GetRenderWidgetHostImpl();
295 if (!rwh
->IsRenderView())
297 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh
));
300 } // namespace content