Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / ime_adapter_android.cc
blob95dc541a2fafec7faadd54003a95bee002b4e78b
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.h"
14 #include "content/browser/renderer_host/render_widget_host_impl.h"
15 #include "content/browser/renderer_host/render_widget_host_view_android.h"
16 #include "content/common/view_messages.h"
17 #include "content/public/browser/native_web_keyboard_event.h"
18 #include "jni/ImeAdapter_jni.h"
19 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
20 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 using base::android::AttachCurrentThread;
23 using base::android::ConvertJavaStringToUTF16;
25 namespace content {
26 namespace {
28 // Maps a java KeyEvent into a NativeWebKeyboardEvent.
29 // |java_key_event| is used to maintain a globalref for KeyEvent.
30 // |action| will help determine the WebInputEvent type.
31 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create
32 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key
33 // as a key press of character \r.
34 NativeWebKeyboardEvent NativeWebKeyboardEventFromKeyEvent(
35 JNIEnv* env,
36 jobject java_key_event,
37 int action,
38 int modifiers,
39 long time_ms,
40 int key_code,
41 bool is_system_key,
42 int unicode_char) {
43 WebKit::WebInputEvent::Type type = WebKit::WebInputEvent::Undefined;
44 if (action == AKEY_EVENT_ACTION_DOWN)
45 type = WebKit::WebInputEvent::RawKeyDown;
46 else if (action == AKEY_EVENT_ACTION_UP)
47 type = WebKit::WebInputEvent::KeyUp;
48 return NativeWebKeyboardEvent(java_key_event, type, modifiers,
49 time_ms, key_code, unicode_char, is_system_key);
52 } // anonymous namespace
54 bool RegisterImeAdapter(JNIEnv* env) {
55 if (!RegisterNativesImpl(env))
56 return false;
58 Java_ImeAdapter_initializeWebInputEvents(env,
59 WebKit::WebInputEvent::RawKeyDown,
60 WebKit::WebInputEvent::KeyUp,
61 WebKit::WebInputEvent::Char,
62 WebKit::WebInputEvent::ShiftKey,
63 WebKit::WebInputEvent::AltKey,
64 WebKit::WebInputEvent::ControlKey,
65 WebKit::WebInputEvent::CapsLockOn,
66 WebKit::WebInputEvent::NumLockOn);
67 // TODO(miguelg): remove date time related enums after
68 // https://bugs.webkit.org/show_bug.cgi?id=100935.
69 Java_ImeAdapter_initializeTextInputTypes(
70 env,
71 ui::TEXT_INPUT_TYPE_NONE,
72 ui::TEXT_INPUT_TYPE_TEXT,
73 ui::TEXT_INPUT_TYPE_TEXT_AREA,
74 ui::TEXT_INPUT_TYPE_PASSWORD,
75 ui::TEXT_INPUT_TYPE_SEARCH,
76 ui::TEXT_INPUT_TYPE_URL,
77 ui::TEXT_INPUT_TYPE_EMAIL,
78 ui::TEXT_INPUT_TYPE_TELEPHONE,
79 ui::TEXT_INPUT_TYPE_NUMBER,
80 ui::TEXT_INPUT_TYPE_DATE,
81 ui::TEXT_INPUT_TYPE_DATE_TIME,
82 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL,
83 ui::TEXT_INPUT_TYPE_MONTH,
84 ui::TEXT_INPUT_TYPE_TIME,
85 ui::TEXT_INPUT_TYPE_WEEK,
86 ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE);
87 return true;
90 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva)
91 : rwhva_(rwhva) {
94 ImeAdapterAndroid::~ImeAdapterAndroid() {
95 JNIEnv* env = AttachCurrentThread();
96 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
97 if (!obj.is_null())
98 Java_ImeAdapter_detach(env, obj.obj());
101 bool ImeAdapterAndroid::SendSyntheticKeyEvent(JNIEnv*,
102 jobject,
103 int type,
104 long time_ms,
105 int key_code,
106 int text) {
107 NativeWebKeyboardEvent event(static_cast<WebKit::WebInputEvent::Type>(type),
108 0 /* modifiers */, time_ms / 1000.0, key_code,
109 text, false /* is_system_key */);
110 rwhva_->SendKeyEvent(event);
111 return true;
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 rwhva_->SendKeyEvent(event);
123 if (event.type == WebKit::WebInputEvent::RawKeyDown && event.text[0]) {
124 // Send a Char event, but without an os_event since we don't want to
125 // roundtrip back to java such synthetic event.
126 NativeWebKeyboardEvent char_event(event);
127 char_event.os_event = NULL;
128 event.type = WebKit::WebInputEvent::Char;
129 rwhva_->SendKeyEvent(event);
131 return true;
134 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, jobject, jstring text,
135 int new_cursor_pos) {
136 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
137 rwhva_->GetRenderWidgetHost());
138 if (!rwhi)
139 return;
141 string16 text16 = ConvertJavaStringToUTF16(env, text);
142 std::vector<WebKit::WebCompositionUnderline> underlines;
143 underlines.push_back(
144 WebKit::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK,
145 false));
146 // new_cursor_position is as described in the Android API for
147 // InputConnection#setComposingText, whereas the parameters for
148 // ImeSetComposition are relative to the start of the composition.
149 if (new_cursor_pos > 0)
150 new_cursor_pos = text16.length() + new_cursor_pos - 1;
152 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos);
155 void ImeAdapterAndroid::ImeBatchStateChanged(JNIEnv* env,
156 jobject,
157 jboolean is_begin) {
158 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
159 rwhva_->GetRenderWidgetHost());
160 if (!rwhi)
161 return;
163 rwhi->Send(new ViewMsg_ImeBatchStateChanged(rwhi->GetRoutingID(), is_begin));
166 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text) {
167 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
168 rwhva_->GetRenderWidgetHost());
169 if (!rwhi)
170 return;
172 string16 text16 = ConvertJavaStringToUTF16(env, text);
173 rwhi->ImeConfirmComposition(text16);
176 void ImeAdapterAndroid::AttachImeAdapter(JNIEnv* env, jobject java_object) {
177 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object);
180 void ImeAdapterAndroid::CancelComposition() {
181 base::android::ScopedJavaLocalRef<jobject> obj =
182 java_ime_adapter_.get(AttachCurrentThread());
183 if (!obj.is_null())
184 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj.obj());
187 void ImeAdapterAndroid::SetEditableSelectionOffsets(JNIEnv*, jobject,
188 int start, int end) {
189 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
190 rwhva_->GetRenderWidgetHost());
191 if (!rwhi)
192 return;
194 rwhi->Send(new ViewMsg_SetEditableSelectionOffsets(rwhi->GetRoutingID(),
195 start, end));
198 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject,
199 int start, int end) {
200 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
201 rwhva_->GetRenderWidgetHost());
202 if (!rwhi)
203 return;
205 std::vector<WebKit::WebCompositionUnderline> underlines;
206 underlines.push_back(
207 WebKit::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false));
209 rwhi->Send(new ViewMsg_SetCompositionFromExistingText(
210 rwhi->GetRoutingID(), start, end, underlines));
213 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject,
214 int before, int after) {
215 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
216 rwhva_->GetRenderWidgetHost());
217 if (!rwhi)
218 return;
220 rwhi->Send(new ViewMsg_ExtendSelectionAndDelete(rwhi->GetRoutingID(),
221 before, after));
224 void ImeAdapterAndroid::Unselect(JNIEnv* env, jobject) {
225 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
226 rwhva_->GetRenderWidgetHost());
227 if (!rwhi)
228 return;
230 rwhi->Unselect();
233 void ImeAdapterAndroid::SelectAll(JNIEnv* env, jobject) {
234 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
235 rwhva_->GetRenderWidgetHost());
236 if (!rwhi)
237 return;
239 rwhi->SelectAll();
242 void ImeAdapterAndroid::Cut(JNIEnv* env, jobject) {
243 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
244 rwhva_->GetRenderWidgetHost());
245 if (!rwhi)
246 return;
248 rwhi->Cut();
251 void ImeAdapterAndroid::Copy(JNIEnv* env, jobject) {
252 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
253 rwhva_->GetRenderWidgetHost());
254 if (!rwhi)
255 return;
257 rwhi->Copy();
260 void ImeAdapterAndroid::Paste(JNIEnv* env, jobject) {
261 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(
262 rwhva_->GetRenderWidgetHost());
263 if (!rwhi)
264 return;
266 rwhi->Paste();
269 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, jobject) {
270 java_ime_adapter_.reset();
273 } // namespace content