OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/renderer_host/ime_adapter_android.h" | 5 #include "content/browser/renderer_host/ime_adapter_android.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <android/input.h> | 8 #include <android/input.h> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 using base::android::AttachCurrentThread; | 36 using base::android::AttachCurrentThread; |
37 using base::android::ConvertJavaStringToUTF16; | 37 using base::android::ConvertJavaStringToUTF16; |
38 using base::android::JavaParamRef; | 38 using base::android::JavaParamRef; |
39 | 39 |
40 namespace content { | 40 namespace content { |
41 namespace { | 41 namespace { |
42 | 42 |
43 // Maps a java KeyEvent into a NativeWebKeyboardEvent. | 43 // Maps a java KeyEvent into a NativeWebKeyboardEvent. |
44 // |java_key_event| is used to maintain a globalref for KeyEvent. | 44 // |java_key_event| is used to maintain a globalref for KeyEvent. |
45 // |action| will help determine the WebInputEvent type. | 45 // |type| will determine the WebInputEvent type. |
46 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create | 46 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create |
47 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key | 47 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key |
48 // as a key press of character \r. | 48 // as a key press of character \r. |
49 NativeWebKeyboardEvent NativeWebKeyboardEventFromKeyEvent( | 49 NativeWebKeyboardEvent NativeWebKeyboardEventFromKeyEvent( |
50 JNIEnv* env, | 50 JNIEnv* env, |
51 const base::android::JavaRef<jobject>& java_key_event, | 51 const base::android::JavaRef<jobject>& java_key_event, |
52 int action, | 52 int type, |
53 int modifiers, | 53 int modifiers, |
54 long time_ms, | 54 long time_ms, |
55 int key_code, | 55 int key_code, |
56 int scan_code, | 56 int scan_code, |
57 bool is_system_key, | 57 bool is_system_key, |
58 int unicode_char) { | 58 int unicode_char) { |
59 blink::WebInputEvent::Type type = blink::WebInputEvent::Undefined; | 59 return NativeWebKeyboardEvent(env, java_key_event, |
60 if (action == AKEY_EVENT_ACTION_DOWN) | 60 static_cast<blink::WebInputEvent::Type>(type), |
61 type = blink::WebInputEvent::RawKeyDown; | 61 modifiers, time_ms / 1000.0, key_code, |
62 else if (action == AKEY_EVENT_ACTION_UP) | 62 scan_code, unicode_char, is_system_key); |
63 type = blink::WebInputEvent::KeyUp; | |
64 else | |
65 NOTREACHED() << "Invalid Android key event action: " << action; | |
66 return NativeWebKeyboardEvent(env, java_key_event, type, modifiers, | |
67 time_ms / 1000.0, key_code, scan_code, | |
68 unicode_char, is_system_key); | |
69 } | 63 } |
70 | 64 |
71 } // anonymous namespace | 65 } // anonymous namespace |
72 | 66 |
73 bool RegisterImeAdapter(JNIEnv* env) { | 67 bool RegisterImeAdapter(JNIEnv* env) { |
74 return RegisterNativesImpl(env); | 68 return RegisterNativesImpl(env); |
75 } | 69 } |
76 | 70 |
77 // Callback from Java to convert BackgroundColorSpan data to a | 71 // Callback from Java to convert BackgroundColorSpan data to a |
78 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. | 72 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 : rwhva_(rwhva) { | 114 : rwhva_(rwhva) { |
121 } | 115 } |
122 | 116 |
123 ImeAdapterAndroid::~ImeAdapterAndroid() { | 117 ImeAdapterAndroid::~ImeAdapterAndroid() { |
124 JNIEnv* env = AttachCurrentThread(); | 118 JNIEnv* env = AttachCurrentThread(); |
125 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 119 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
126 if (!obj.is_null()) | 120 if (!obj.is_null()) |
127 Java_ImeAdapter_detach(env, obj); | 121 Java_ImeAdapter_detach(env, obj); |
128 } | 122 } |
129 | 123 |
130 bool ImeAdapterAndroid::SendSyntheticKeyEvent(JNIEnv*, | |
131 const JavaParamRef<jobject>&, | |
132 int type, | |
133 long time_ms, | |
134 int key_code, | |
135 int modifiers, | |
136 int text) { | |
137 NativeWebKeyboardEvent event(static_cast<blink::WebInputEvent::Type>(type), | |
138 modifiers, time_ms / 1000.0, key_code, 0, | |
139 text, false /* is_system_key */); | |
140 rwhva_->SendKeyEvent(event); | |
141 return true; | |
142 } | |
143 | |
144 bool ImeAdapterAndroid::SendKeyEvent( | 124 bool ImeAdapterAndroid::SendKeyEvent( |
145 JNIEnv* env, | 125 JNIEnv* env, |
146 const JavaParamRef<jobject>&, | 126 const JavaParamRef<jobject>&, |
147 const JavaParamRef<jobject>& original_key_event, | 127 const JavaParamRef<jobject>& original_key_event, |
148 int action, | 128 int type, |
149 int modifiers, | 129 int modifiers, |
150 long time_ms, | 130 long time_ms, |
151 int key_code, | 131 int key_code, |
152 int scan_code, | 132 int scan_code, |
153 bool is_system_key, | 133 bool is_system_key, |
154 int unicode_char) { | 134 int unicode_char) { |
155 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( | 135 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( |
156 env, original_key_event, action, modifiers, | 136 env, original_key_event, type, modifiers, |
157 time_ms, key_code, scan_code, is_system_key, unicode_char); | 137 time_ms / 1000.0, key_code, scan_code, is_system_key, unicode_char); |
158 bool key_down_text_insertion = | |
159 event.type == blink::WebInputEvent::RawKeyDown && event.text[0]; | |
160 // If we are going to follow up with a synthetic Char event, then that's the | |
161 // one we expect to test if it's handled or unhandled, so skip handling the | |
162 // "real" event in the browser. | |
163 event.skip_in_browser = key_down_text_insertion; | |
164 rwhva_->SendKeyEvent(event); | 138 rwhva_->SendKeyEvent(event); |
165 if (key_down_text_insertion) { | |
166 // Send a Char event, but without an os_event since we don't want to | |
167 // roundtrip back to java such synthetic event. | |
168 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, | |
169 time_ms / 1000.0, key_code, scan_code, | |
170 unicode_char, | |
171 is_system_key); | |
172 char_event.skip_in_browser = key_down_text_insertion; | |
173 rwhva_->SendKeyEvent(char_event); | |
174 } | |
175 return true; | 139 return true; |
176 } | 140 } |
177 | 141 |
178 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | 142 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
179 const JavaParamRef<jobject>& obj, | 143 const JavaParamRef<jobject>& obj, |
180 const JavaParamRef<jobject>& text, | 144 const JavaParamRef<jobject>& text, |
181 const JavaParamRef<jstring>& text_str, | 145 const JavaParamRef<jstring>& text_str, |
182 int new_cursor_pos) { | 146 int new_cursor_pos) { |
183 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 147 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
184 if (!rwhi) | 148 if (!rwhi) |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 } | 338 } |
375 | 339 |
376 WebContents* ImeAdapterAndroid::GetWebContents() { | 340 WebContents* ImeAdapterAndroid::GetWebContents() { |
377 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 341 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); |
378 if (!rwh) | 342 if (!rwh) |
379 return nullptr; | 343 return nullptr; |
380 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 344 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
381 } | 345 } |
382 | 346 |
383 } // namespace content | 347 } // namespace content |
OLD | NEW |