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"
8 #include <android/input.h>
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_string.h"
13 #include "base/android/scoped_java_ref.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h"
16 #include "content/browser/frame_host/frame_tree.h"
17 #include "content/browser/frame_host/frame_tree_node.h"
18 #include "content/browser/frame_host/render_frame_host_impl.h"
19 #include "content/browser/renderer_host/render_view_host_delegate.h"
20 #include "content/browser/renderer_host/render_view_host_impl.h"
21 #include "content/browser/renderer_host/render_widget_host_impl.h"
22 #include "content/browser/renderer_host/render_widget_host_view_android.h"
23 #include "content/common/frame_messages.h"
24 #include "content/common/input_messages.h"
25 #include "content/common/view_messages.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/native_web_keyboard_event.h"
28 #include "content/public/browser/web_contents.h"
29 #include "jni/ImeAdapter_jni.h"
30 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
31 #include "third_party/WebKit/public/web/WebInputEvent.h"
32 #include "third_party/WebKit/public/web/WebTextInputType.h"
34 using base::android::AttachCurrentThread
;
35 using base::android::ConvertJavaStringToUTF16
;
40 // Maps a java KeyEvent into a NativeWebKeyboardEvent.
41 // |java_key_event| is used to maintain a globalref for KeyEvent.
42 // |action| will help determine the WebInputEvent type.
43 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create
44 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key
45 // as a key press of character \r.
46 NativeWebKeyboardEvent
NativeWebKeyboardEventFromKeyEvent(
48 jobject java_key_event
,
55 blink::WebInputEvent::Type type
= blink::WebInputEvent::Undefined
;
56 if (action
== AKEY_EVENT_ACTION_DOWN
)
57 type
= blink::WebInputEvent::RawKeyDown
;
58 else if (action
== AKEY_EVENT_ACTION_UP
)
59 type
= blink::WebInputEvent::KeyUp
;
61 NOTREACHED() << "Invalid Android key event action: " << action
;
62 return NativeWebKeyboardEvent(java_key_event
, type
, modifiers
,
63 time_ms
/ 1000.0, key_code
, unicode_char
, is_system_key
);
66 } // anonymous namespace
68 bool RegisterImeAdapter(JNIEnv
* env
) {
69 return RegisterNativesImpl(env
);
72 // Callback from Java to convert BackgroundColorSpan data to a
73 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|.
74 void AppendBackgroundColorSpan(JNIEnv
*,
79 jint background_color
) {
82 // Do not check |background_color|.
83 std::vector
<blink::WebCompositionUnderline
>* underlines
=
84 reinterpret_cast<std::vector
<blink::WebCompositionUnderline
>*>(
86 underlines
->push_back(
87 blink::WebCompositionUnderline(static_cast<unsigned>(start
),
88 static_cast<unsigned>(end
),
91 static_cast<unsigned>(background_color
)));
94 // Callback from Java to convert UnderlineSpan data to a
95 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|.
96 void AppendUnderlineSpan(JNIEnv
*,
103 std::vector
<blink::WebCompositionUnderline
>* underlines
=
104 reinterpret_cast<std::vector
<blink::WebCompositionUnderline
>*>(
106 underlines
->push_back(
107 blink::WebCompositionUnderline(static_cast<unsigned>(start
),
108 static_cast<unsigned>(end
),
111 SK_ColorTRANSPARENT
));
114 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid
* rwhva
)
118 ImeAdapterAndroid::~ImeAdapterAndroid() {
119 JNIEnv
* env
= AttachCurrentThread();
120 base::android::ScopedJavaLocalRef
<jobject
> obj
= java_ime_adapter_
.get(env
);
122 Java_ImeAdapter_detach(env
, obj
.obj());
125 bool ImeAdapterAndroid::SendSyntheticKeyEvent(JNIEnv
*,
132 NativeWebKeyboardEvent
event(static_cast<blink::WebInputEvent::Type
>(type
),
133 modifiers
, time_ms
/ 1000.0, key_code
,
134 text
, false /* is_system_key */);
135 rwhva_
->SendKeyEvent(event
);
139 bool ImeAdapterAndroid::SendKeyEvent(JNIEnv
* env
, jobject
,
140 jobject original_key_event
,
141 int action
, int modifiers
,
142 long time_ms
, int key_code
,
143 bool is_system_key
, int unicode_char
) {
144 NativeWebKeyboardEvent event
= NativeWebKeyboardEventFromKeyEvent(
145 env
, original_key_event
, action
, modifiers
,
146 time_ms
, key_code
, is_system_key
, unicode_char
);
147 bool key_down_text_insertion
=
148 event
.type
== blink::WebInputEvent::RawKeyDown
&& event
.text
[0];
149 // If we are going to follow up with a synthetic Char event, then that's the
150 // one we expect to test if it's handled or unhandled, so skip handling the
151 // "real" event in the browser.
152 event
.skip_in_browser
= key_down_text_insertion
;
153 rwhva_
->SendKeyEvent(event
);
154 if (key_down_text_insertion
) {
155 // Send a Char event, but without an os_event since we don't want to
156 // roundtrip back to java such synthetic event.
157 NativeWebKeyboardEvent
char_event(blink::WebInputEvent::Char
, modifiers
,
158 time_ms
/ 1000.0, key_code
, unicode_char
,
160 char_event
.skip_in_browser
= key_down_text_insertion
;
161 rwhva_
->SendKeyEvent(char_event
);
166 void ImeAdapterAndroid::SetComposingText(JNIEnv
* env
,
170 int new_cursor_pos
) {
171 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
175 base::string16 text16
= ConvertJavaStringToUTF16(env
, text_str
);
177 std::vector
<blink::WebCompositionUnderline
> underlines
;
178 // Iterate over spans in |text|, dispatch those that we care about (e.g.,
179 // BackgroundColorSpan) to a matching callback (e.g.,
180 // AppendBackgroundColorSpan()), and populate |underlines|.
181 Java_ImeAdapter_populateUnderlinesFromSpans(
182 env
, obj
, text
, reinterpret_cast<jlong
>(&underlines
));
184 // Default to plain underline if we didn't find any span that we care about.
185 if (underlines
.empty()) {
186 underlines
.push_back(blink::WebCompositionUnderline(
187 0, text16
.length(), SK_ColorBLACK
, false, SK_ColorTRANSPARENT
));
189 // Sort spans by |.startOffset|.
190 std::sort(underlines
.begin(), underlines
.end());
192 // new_cursor_position is as described in the Android API for
193 // InputConnection#setComposingText, whereas the parameters for
194 // ImeSetComposition are relative to the start of the composition.
195 if (new_cursor_pos
> 0)
196 new_cursor_pos
= text16
.length() + new_cursor_pos
- 1;
198 rwhi
->ImeSetComposition(text16
, underlines
, new_cursor_pos
, new_cursor_pos
);
201 void ImeAdapterAndroid::CommitText(JNIEnv
* env
, jobject
, jstring text_str
) {
202 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
206 base::string16 text16
= ConvertJavaStringToUTF16(env
, text_str
);
207 rwhi
->ImeConfirmComposition(text16
, gfx::Range::InvalidRange(), false);
210 void ImeAdapterAndroid::FinishComposingText(JNIEnv
* env
, jobject
) {
211 RenderWidgetHostImpl
* rwhi
= GetRenderWidgetHostImpl();
215 rwhi
->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(),
219 void ImeAdapterAndroid::AttachImeAdapter(JNIEnv
* env
, jobject java_object
) {
220 java_ime_adapter_
= JavaObjectWeakGlobalRef(env
, java_object
);
223 void ImeAdapterAndroid::CancelComposition() {
224 base::android::ScopedJavaLocalRef
<jobject
> obj
=
225 java_ime_adapter_
.get(AttachCurrentThread());
227 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj
.obj());
230 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node
) {
231 base::android::ScopedJavaLocalRef
<jobject
> obj
=
232 java_ime_adapter_
.get(AttachCurrentThread());
233 if (!obj
.is_null()) {
234 Java_ImeAdapter_focusedNodeChanged(AttachCurrentThread(),
240 void ImeAdapterAndroid::SetEditableSelectionOffsets(JNIEnv
*, jobject
,
241 int start
, int end
) {
242 RenderFrameHost
* rfh
= GetFocusedFrame();
246 rfh
->Send(new FrameMsg_SetEditableSelectionOffsets(rfh
->GetRoutingID(),
250 void ImeAdapterAndroid::SetComposingRegion(JNIEnv
*, jobject
,
251 int start
, int end
) {
252 RenderFrameHost
* rfh
= GetFocusedFrame();
256 std::vector
<blink::WebCompositionUnderline
> underlines
;
257 underlines
.push_back(blink::WebCompositionUnderline(
258 0, end
- start
, SK_ColorBLACK
, false, SK_ColorTRANSPARENT
));
260 rfh
->Send(new InputMsg_SetCompositionFromExistingText(
261 rfh
->GetRoutingID(), start
, end
, underlines
));
264 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv
*, jobject
,
265 int before
, int after
) {
266 RenderFrameHostImpl
* rfh
=
267 static_cast<RenderFrameHostImpl
*>(GetFocusedFrame());
269 rfh
->ExtendSelectionAndDelete(before
, after
);
272 void ImeAdapterAndroid::Unselect(JNIEnv
* env
, jobject
) {
273 WebContents
* wc
= GetWebContents();
278 void ImeAdapterAndroid::SelectAll(JNIEnv
* env
, jobject
) {
279 WebContents
* wc
= GetWebContents();
284 void ImeAdapterAndroid::Cut(JNIEnv
* env
, jobject
) {
285 WebContents
* wc
= GetWebContents();
290 void ImeAdapterAndroid::Copy(JNIEnv
* env
, jobject
) {
291 WebContents
* wc
= GetWebContents();
296 void ImeAdapterAndroid::Paste(JNIEnv
* env
, jobject
) {
297 WebContents
* wc
= GetWebContents();
302 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv
* env
, jobject
) {
303 java_ime_adapter_
.reset();
306 RenderWidgetHostImpl
* ImeAdapterAndroid::GetRenderWidgetHostImpl() {
307 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
309 RenderWidgetHost
* rwh
= rwhva_
->GetRenderWidgetHost();
313 return RenderWidgetHostImpl::From(rwh
);
316 RenderFrameHost
* ImeAdapterAndroid::GetFocusedFrame() {
317 RenderWidgetHostImpl
* rwh
= GetRenderWidgetHostImpl();
320 if (!rwh
->IsRenderView())
322 RenderViewHost
* rvh
= RenderViewHost::From(rwh
);
323 FrameTreeNode
* focused_frame
=
324 rvh
->GetDelegate()->GetFrameTree()->GetFocusedFrame();
328 return focused_frame
->current_frame_host();
331 WebContents
* ImeAdapterAndroid::GetWebContents() {
332 RenderWidgetHostImpl
* rwh
= GetRenderWidgetHostImpl();
335 if (!rwh
->IsRenderView())
337 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh
));
340 } // namespace content