| 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 } | 117 } |
| 118 | 118 |
| 119 ImeAdapterAndroid::~ImeAdapterAndroid() { | 119 ImeAdapterAndroid::~ImeAdapterAndroid() { |
| 120 JNIEnv* env = AttachCurrentThread(); | 120 JNIEnv* env = AttachCurrentThread(); |
| 121 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 121 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 122 if (!obj.is_null()) | 122 if (!obj.is_null()) |
| 123 Java_ImeAdapter_detach(env, obj.obj()); | 123 Java_ImeAdapter_detach(env, obj.obj()); |
| 124 } | 124 } |
| 125 | 125 |
| 126 bool ImeAdapterAndroid::SendSyntheticKeyEvent(JNIEnv*, | 126 bool ImeAdapterAndroid::SendSyntheticKeyEvent(JNIEnv*, |
| 127 jobject, | 127 const JavaParamRef<jobject>&, |
| 128 int type, | 128 int type, |
| 129 long time_ms, | 129 long time_ms, |
| 130 int key_code, | 130 int key_code, |
| 131 int modifiers, | 131 int modifiers, |
| 132 int text) { | 132 int text) { |
| 133 NativeWebKeyboardEvent event(static_cast<blink::WebInputEvent::Type>(type), | 133 NativeWebKeyboardEvent event(static_cast<blink::WebInputEvent::Type>(type), |
| 134 modifiers, time_ms / 1000.0, key_code, 0, | 134 modifiers, time_ms / 1000.0, key_code, 0, |
| 135 text, false /* is_system_key */); | 135 text, false /* is_system_key */); |
| 136 rwhva_->SendKeyEvent(event); | 136 rwhva_->SendKeyEvent(event); |
| 137 return true; | 137 return true; |
| 138 } | 138 } |
| 139 | 139 |
| 140 bool ImeAdapterAndroid::SendKeyEvent(JNIEnv* env, jobject, | 140 bool ImeAdapterAndroid::SendKeyEvent( |
| 141 jobject original_key_event, | 141 JNIEnv* env, |
| 142 int action, int modifiers, | 142 const JavaParamRef<jobject>&, |
| 143 long time_ms, int key_code, | 143 const JavaParamRef<jobject>& original_key_event, |
| 144 int scan_code, bool is_system_key, | 144 int action, |
| 145 int unicode_char) { | 145 int modifiers, |
| 146 long time_ms, |
| 147 int key_code, |
| 148 int scan_code, |
| 149 bool is_system_key, |
| 150 int unicode_char) { |
| 146 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( | 151 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( |
| 147 env, original_key_event, action, modifiers, | 152 env, original_key_event, action, modifiers, |
| 148 time_ms, key_code, scan_code, is_system_key, unicode_char); | 153 time_ms, key_code, scan_code, is_system_key, unicode_char); |
| 149 bool key_down_text_insertion = | 154 bool key_down_text_insertion = |
| 150 event.type == blink::WebInputEvent::RawKeyDown && event.text[0]; | 155 event.type == blink::WebInputEvent::RawKeyDown && event.text[0]; |
| 151 // If we are going to follow up with a synthetic Char event, then that's the | 156 // If we are going to follow up with a synthetic Char event, then that's the |
| 152 // one we expect to test if it's handled or unhandled, so skip handling the | 157 // one we expect to test if it's handled or unhandled, so skip handling the |
| 153 // "real" event in the browser. | 158 // "real" event in the browser. |
| 154 event.skip_in_browser = key_down_text_insertion; | 159 event.skip_in_browser = key_down_text_insertion; |
| 155 rwhva_->SendKeyEvent(event); | 160 rwhva_->SendKeyEvent(event); |
| 156 if (key_down_text_insertion) { | 161 if (key_down_text_insertion) { |
| 157 // Send a Char event, but without an os_event since we don't want to | 162 // Send a Char event, but without an os_event since we don't want to |
| 158 // roundtrip back to java such synthetic event. | 163 // roundtrip back to java such synthetic event. |
| 159 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, | 164 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, |
| 160 time_ms / 1000.0, key_code, scan_code, | 165 time_ms / 1000.0, key_code, scan_code, |
| 161 unicode_char, | 166 unicode_char, |
| 162 is_system_key); | 167 is_system_key); |
| 163 char_event.skip_in_browser = key_down_text_insertion; | 168 char_event.skip_in_browser = key_down_text_insertion; |
| 164 rwhva_->SendKeyEvent(char_event); | 169 rwhva_->SendKeyEvent(char_event); |
| 165 } | 170 } |
| 166 return true; | 171 return true; |
| 167 } | 172 } |
| 168 | 173 |
| 169 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | 174 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
| 170 jobject obj, | 175 const JavaParamRef<jobject>& obj, |
| 171 jobject text, | 176 const JavaParamRef<jobject>& text, |
| 172 jstring text_str, | 177 const JavaParamRef<jstring>& text_str, |
| 173 int new_cursor_pos) { | 178 int new_cursor_pos) { |
| 174 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 179 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 175 if (!rwhi) | 180 if (!rwhi) |
| 176 return; | 181 return; |
| 177 | 182 |
| 178 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 183 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 179 | 184 |
| 180 std::vector<blink::WebCompositionUnderline> underlines; | 185 std::vector<blink::WebCompositionUnderline> underlines; |
| 181 // Iterate over spans in |text|, dispatch those that we care about (e.g., | 186 // Iterate over spans in |text|, dispatch those that we care about (e.g., |
| 182 // BackgroundColorSpan) to a matching callback (e.g., | 187 // BackgroundColorSpan) to a matching callback (e.g., |
| (...skipping 11 matching lines...) Expand all Loading... |
| 194 | 199 |
| 195 // new_cursor_position is as described in the Android API for | 200 // new_cursor_position is as described in the Android API for |
| 196 // InputConnection#setComposingText, whereas the parameters for | 201 // InputConnection#setComposingText, whereas the parameters for |
| 197 // ImeSetComposition are relative to the start of the composition. | 202 // ImeSetComposition are relative to the start of the composition. |
| 198 if (new_cursor_pos > 0) | 203 if (new_cursor_pos > 0) |
| 199 new_cursor_pos = text16.length() + new_cursor_pos - 1; | 204 new_cursor_pos = text16.length() + new_cursor_pos - 1; |
| 200 | 205 |
| 201 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); | 206 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); |
| 202 } | 207 } |
| 203 | 208 |
| 204 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text_str) { | 209 void ImeAdapterAndroid::CommitText(JNIEnv* env, |
| 210 const JavaParamRef<jobject>&, |
| 211 const JavaParamRef<jstring>& text_str) { |
| 205 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 212 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 206 if (!rwhi) | 213 if (!rwhi) |
| 207 return; | 214 return; |
| 208 | 215 |
| 209 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 216 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 210 rwhi->ImeConfirmComposition(text16, gfx::Range::InvalidRange(), false); | 217 rwhi->ImeConfirmComposition(text16, gfx::Range::InvalidRange(), false); |
| 211 } | 218 } |
| 212 | 219 |
| 213 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, jobject) { | 220 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, |
| 221 const JavaParamRef<jobject>&) { |
| 214 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 222 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 215 if (!rwhi) | 223 if (!rwhi) |
| 216 return; | 224 return; |
| 217 | 225 |
| 218 rwhi->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(), | 226 rwhi->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(), |
| 219 true); | 227 true); |
| 220 } | 228 } |
| 221 | 229 |
| 222 void ImeAdapterAndroid::AttachImeAdapter(JNIEnv* env, jobject java_object) { | 230 void ImeAdapterAndroid::AttachImeAdapter( |
| 231 JNIEnv* env, |
| 232 const JavaParamRef<jobject>& java_object) { |
| 223 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object); | 233 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object); |
| 224 } | 234 } |
| 225 | 235 |
| 226 void ImeAdapterAndroid::CancelComposition() { | 236 void ImeAdapterAndroid::CancelComposition() { |
| 227 base::android::ScopedJavaLocalRef<jobject> obj = | 237 base::android::ScopedJavaLocalRef<jobject> obj = |
| 228 java_ime_adapter_.get(AttachCurrentThread()); | 238 java_ime_adapter_.get(AttachCurrentThread()); |
| 229 if (!obj.is_null()) | 239 if (!obj.is_null()) |
| 230 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj.obj()); | 240 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj.obj()); |
| 231 } | 241 } |
| 232 | 242 |
| 233 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { | 243 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { |
| 234 base::android::ScopedJavaLocalRef<jobject> obj = | 244 base::android::ScopedJavaLocalRef<jobject> obj = |
| 235 java_ime_adapter_.get(AttachCurrentThread()); | 245 java_ime_adapter_.get(AttachCurrentThread()); |
| 236 if (!obj.is_null()) { | 246 if (!obj.is_null()) { |
| 237 Java_ImeAdapter_focusedNodeChanged(AttachCurrentThread(), | 247 Java_ImeAdapter_focusedNodeChanged(AttachCurrentThread(), |
| 238 obj.obj(), | 248 obj.obj(), |
| 239 is_editable_node); | 249 is_editable_node); |
| 240 } | 250 } |
| 241 } | 251 } |
| 242 | 252 |
| 243 void ImeAdapterAndroid::SetEditableSelectionOffsets(JNIEnv*, jobject, | 253 void ImeAdapterAndroid::SetEditableSelectionOffsets( |
| 244 int start, int end) { | 254 JNIEnv*, |
| 255 const JavaParamRef<jobject>&, |
| 256 int start, |
| 257 int end) { |
| 245 RenderFrameHost* rfh = GetFocusedFrame(); | 258 RenderFrameHost* rfh = GetFocusedFrame(); |
| 246 if (!rfh) | 259 if (!rfh) |
| 247 return; | 260 return; |
| 248 | 261 |
| 249 rfh->Send(new FrameMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), | 262 rfh->Send(new FrameMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), |
| 250 start, end)); | 263 start, end)); |
| 251 } | 264 } |
| 252 | 265 |
| 253 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, | 266 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, |
| 254 int start, int end) { | 267 const JavaParamRef<jobject>&, |
| 268 int start, |
| 269 int end) { |
| 255 RenderFrameHost* rfh = GetFocusedFrame(); | 270 RenderFrameHost* rfh = GetFocusedFrame(); |
| 256 if (!rfh) | 271 if (!rfh) |
| 257 return; | 272 return; |
| 258 | 273 |
| 259 std::vector<blink::WebCompositionUnderline> underlines; | 274 std::vector<blink::WebCompositionUnderline> underlines; |
| 260 underlines.push_back(blink::WebCompositionUnderline( | 275 underlines.push_back(blink::WebCompositionUnderline( |
| 261 0, end - start, SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | 276 0, end - start, SK_ColorBLACK, false, SK_ColorTRANSPARENT)); |
| 262 | 277 |
| 263 rfh->Send(new InputMsg_SetCompositionFromExistingText( | 278 rfh->Send(new InputMsg_SetCompositionFromExistingText( |
| 264 rfh->GetRoutingID(), start, end, underlines)); | 279 rfh->GetRoutingID(), start, end, underlines)); |
| 265 } | 280 } |
| 266 | 281 |
| 267 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, | 282 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, |
| 268 int before, int after) { | 283 const JavaParamRef<jobject>&, |
| 284 int before, |
| 285 int after) { |
| 269 RenderFrameHostImpl* rfh = | 286 RenderFrameHostImpl* rfh = |
| 270 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | 287 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); |
| 271 if (rfh) | 288 if (rfh) |
| 272 rfh->ExtendSelectionAndDelete(before, after); | 289 rfh->ExtendSelectionAndDelete(before, after); |
| 273 } | 290 } |
| 274 | 291 |
| 275 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, jobject) { | 292 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, |
| 293 const JavaParamRef<jobject>&) { |
| 276 java_ime_adapter_.reset(); | 294 java_ime_adapter_.reset(); |
| 277 } | 295 } |
| 278 | 296 |
| 279 RenderWidgetHostImpl* ImeAdapterAndroid::GetRenderWidgetHostImpl() { | 297 RenderWidgetHostImpl* ImeAdapterAndroid::GetRenderWidgetHostImpl() { |
| 280 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 298 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 281 DCHECK(rwhva_); | 299 DCHECK(rwhva_); |
| 282 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost(); | 300 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost(); |
| 283 if (!rwh) | 301 if (!rwh) |
| 284 return nullptr; | 302 return nullptr; |
| 285 | 303 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 302 } | 320 } |
| 303 | 321 |
| 304 WebContents* ImeAdapterAndroid::GetWebContents() { | 322 WebContents* ImeAdapterAndroid::GetWebContents() { |
| 305 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 323 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); |
| 306 if (!rwh) | 324 if (!rwh) |
| 307 return nullptr; | 325 return nullptr; |
| 308 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 326 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
| 309 } | 327 } |
| 310 | 328 |
| 311 } // namespace content | 329 } // namespace content |
| OLD | NEW |