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 13 matching lines...) Expand all Loading... |
24 #include "content/public/browser/native_web_keyboard_event.h" | 24 #include "content/public/browser/native_web_keyboard_event.h" |
25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
26 #include "jni/ImeAdapter_jni.h" | 26 #include "jni/ImeAdapter_jni.h" |
27 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 27 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
28 #include "third_party/WebKit/public/platform/WebTextInputType.h" | 28 #include "third_party/WebKit/public/platform/WebTextInputType.h" |
29 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" | 29 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" |
30 | 30 |
31 using base::android::AttachCurrentThread; | 31 using base::android::AttachCurrentThread; |
32 using base::android::ConvertJavaStringToUTF16; | 32 using base::android::ConvertJavaStringToUTF16; |
33 using base::android::JavaParamRef; | 33 using base::android::JavaParamRef; |
| 34 using base::android::ScopedJavaLocalRef; |
| 35 using base::WeakPtr; |
34 | 36 |
35 namespace content { | 37 namespace content { |
36 namespace { | 38 namespace { |
37 | 39 |
38 // Maps a java KeyEvent into a NativeWebKeyboardEvent. | 40 // Maps a java KeyEvent into a NativeWebKeyboardEvent. |
39 // |java_key_event| is used to maintain a globalref for KeyEvent. | 41 // |java_key_event| is used to maintain a globalref for KeyEvent. |
40 // |type| will determine the WebInputEvent type. | 42 // |type| will determine the WebInputEvent type. |
41 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create | 43 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create |
42 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key | 44 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key |
43 // as a key press of character \r. | 45 // as a key press of character \r. |
(...skipping 12 matching lines...) Expand all Loading... |
56 modifiers, time_ms / 1000.0, key_code, | 58 modifiers, time_ms / 1000.0, key_code, |
57 scan_code, unicode_char, is_system_key); | 59 scan_code, unicode_char, is_system_key); |
58 } | 60 } |
59 | 61 |
60 } // anonymous namespace | 62 } // anonymous namespace |
61 | 63 |
62 bool RegisterImeAdapter(JNIEnv* env) { | 64 bool RegisterImeAdapter(JNIEnv* env) { |
63 return RegisterNativesImpl(env); | 65 return RegisterNativesImpl(env); |
64 } | 66 } |
65 | 67 |
| 68 jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 69 ImeAdapterAndroid* adapter = new ImeAdapterAndroid(env, obj); |
| 70 return reinterpret_cast<intptr_t>(adapter); |
| 71 } |
| 72 |
66 // Callback from Java to convert BackgroundColorSpan data to a | 73 // Callback from Java to convert BackgroundColorSpan data to a |
67 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. | 74 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. |
68 void AppendBackgroundColorSpan(JNIEnv*, | 75 void AppendBackgroundColorSpan(JNIEnv*, |
69 const JavaParamRef<jclass>&, | 76 const JavaParamRef<jclass>&, |
70 jlong underlines_ptr, | 77 jlong underlines_ptr, |
71 jint start, | 78 jint start, |
72 jint end, | 79 jint end, |
73 jint background_color) { | 80 jint background_color) { |
74 DCHECK_GE(start, 0); | 81 DCHECK_GE(start, 0); |
75 DCHECK_GE(end, 0); | 82 DCHECK_GE(end, 0); |
(...skipping 22 matching lines...) Expand all Loading... |
98 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | 105 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
99 underlines_ptr); | 106 underlines_ptr); |
100 underlines->push_back( | 107 underlines->push_back( |
101 blink::WebCompositionUnderline(static_cast<unsigned>(start), | 108 blink::WebCompositionUnderline(static_cast<unsigned>(start), |
102 static_cast<unsigned>(end), | 109 static_cast<unsigned>(end), |
103 SK_ColorBLACK, | 110 SK_ColorBLACK, |
104 false, | 111 false, |
105 SK_ColorTRANSPARENT)); | 112 SK_ColorTRANSPARENT)); |
106 } | 113 } |
107 | 114 |
108 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva) | 115 ImeAdapterAndroid::ImeAdapterAndroid(JNIEnv* env, |
109 : rwhva_(rwhva) { | 116 const JavaParamRef<jobject>& obj) { |
110 DCHECK(rwhva_); | 117 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, obj); |
111 } | 118 } |
112 | 119 |
113 ImeAdapterAndroid::~ImeAdapterAndroid() { | 120 ImeAdapterAndroid::~ImeAdapterAndroid() { |
114 JNIEnv* env = AttachCurrentThread(); | 121 JNIEnv* env = AttachCurrentThread(); |
115 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 122 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
116 if (!obj.is_null()) | 123 if (!obj.is_null()) |
117 Java_ImeAdapter_detach(env, obj); | 124 Java_ImeAdapter_destroy(env, obj); |
| 125 |
| 126 if (rwhva_) |
| 127 rwhva_->ConnectImeAdapter(nullptr); |
| 128 } |
| 129 |
| 130 void ImeAdapterAndroid::SetRenderWidgetHostViewAndroid( |
| 131 const base::WeakPtr<RenderWidgetHostViewAndroid> rwhva) { |
| 132 rwhva_.reset(); |
| 133 rwhva_ = rwhva; |
| 134 JNIEnv* env = AttachCurrentThread(); |
| 135 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 136 if (!obj.is_null()) |
| 137 Java_ImeAdapter_onConnectedToRenderProcess(env, obj); |
118 } | 138 } |
119 | 139 |
120 bool ImeAdapterAndroid::SendKeyEvent( | 140 bool ImeAdapterAndroid::SendKeyEvent( |
121 JNIEnv* env, | 141 JNIEnv* env, |
122 const JavaParamRef<jobject>&, | 142 const JavaParamRef<jobject>&, |
123 const JavaParamRef<jobject>& original_key_event, | 143 const JavaParamRef<jobject>& original_key_event, |
124 int type, | 144 int type, |
125 int modifiers, | 145 int modifiers, |
126 jlong time_ms, | 146 jlong time_ms, |
127 int key_code, | 147 int key_code, |
128 int scan_code, | 148 int scan_code, |
129 bool is_system_key, | 149 bool is_system_key, |
130 int unicode_char) { | 150 int unicode_char) { |
| 151 if (!rwhva_) |
| 152 return false; |
131 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( | 153 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( |
132 env, original_key_event, type, modifiers, time_ms, key_code, scan_code, | 154 env, original_key_event, type, modifiers, time_ms, key_code, scan_code, |
133 is_system_key, unicode_char); | 155 is_system_key, unicode_char); |
134 rwhva_->SendKeyEvent(event); | 156 rwhva_->SendKeyEvent(event); |
135 return true; | 157 return true; |
136 } | 158 } |
137 | 159 |
138 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | 160 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
139 const JavaParamRef<jobject>& obj, | 161 const JavaParamRef<jobject>& obj, |
140 const JavaParamRef<jobject>& text, | 162 const JavaParamRef<jobject>& text, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 215 |
194 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, | 216 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, |
195 const JavaParamRef<jobject>&) { | 217 const JavaParamRef<jobject>&) { |
196 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | 218 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
197 if (!rwhi) | 219 if (!rwhi) |
198 return; | 220 return; |
199 | 221 |
200 rwhi->ImeFinishComposingText(true); | 222 rwhi->ImeFinishComposingText(true); |
201 } | 223 } |
202 | 224 |
203 void ImeAdapterAndroid::AttachImeAdapter( | |
204 JNIEnv* env, | |
205 const JavaParamRef<jobject>& java_object) { | |
206 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object); | |
207 } | |
208 | |
209 void ImeAdapterAndroid::CancelComposition() { | 225 void ImeAdapterAndroid::CancelComposition() { |
210 base::android::ScopedJavaLocalRef<jobject> obj = | 226 JNIEnv* env = AttachCurrentThread(); |
211 java_ime_adapter_.get(AttachCurrentThread()); | 227 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
212 if (!obj.is_null()) | 228 if (!obj.is_null()) |
213 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj); | 229 Java_ImeAdapter_cancelComposition(env, obj); |
214 } | 230 } |
215 | 231 |
216 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { | 232 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { |
217 base::android::ScopedJavaLocalRef<jobject> obj = | 233 JNIEnv* env = AttachCurrentThread(); |
218 java_ime_adapter_.get(AttachCurrentThread()); | 234 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
219 if (!obj.is_null()) { | 235 if (!obj.is_null()) { |
220 Java_ImeAdapter_focusedNodeChanged(AttachCurrentThread(), obj, | 236 Java_ImeAdapter_focusedNodeChanged(env, obj, is_editable_node); |
221 is_editable_node); | |
222 } | 237 } |
223 } | 238 } |
224 | 239 |
225 void ImeAdapterAndroid::SetEditableSelectionOffsets( | 240 void ImeAdapterAndroid::SetEditableSelectionOffsets( |
226 JNIEnv*, | 241 JNIEnv*, |
227 const JavaParamRef<jobject>&, | 242 const JavaParamRef<jobject>&, |
228 int start, | 243 int start, |
229 int end) { | 244 int end) { |
230 RenderFrameHost* rfh = GetFocusedFrame(); | 245 RenderFrameHost* rfh = GetFocusedFrame(); |
231 if (!rfh) | 246 if (!rfh) |
232 return; | 247 return; |
233 | 248 |
234 rfh->Send(new InputMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), start, | 249 rfh->Send(new InputMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), start, |
235 end)); | 250 end)); |
236 } | 251 } |
237 | 252 |
238 void ImeAdapterAndroid::SetCharacterBounds( | 253 void ImeAdapterAndroid::SetCharacterBounds( |
239 const std::vector<gfx::RectF>& character_bounds) { | 254 const std::vector<gfx::RectF>& character_bounds) { |
240 JNIEnv* env = AttachCurrentThread(); | 255 JNIEnv* env = AttachCurrentThread(); |
241 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 256 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
242 if (obj.is_null()) | 257 if (obj.is_null()) |
243 return; | 258 return; |
244 | 259 |
245 const size_t coordinates_array_size = character_bounds.size() * 4; | 260 const size_t coordinates_array_size = character_bounds.size() * 4; |
246 std::unique_ptr<float[]> coordinates_array(new float[coordinates_array_size]); | 261 std::unique_ptr<float[]> coordinates_array(new float[coordinates_array_size]); |
247 for (size_t i = 0; i < character_bounds.size(); ++i) { | 262 for (size_t i = 0; i < character_bounds.size(); ++i) { |
248 const gfx::RectF& rect = character_bounds[i]; | 263 const gfx::RectF& rect = character_bounds[i]; |
249 const size_t coordinates_array_index = i * 4; | 264 const size_t coordinates_array_index = i * 4; |
250 coordinates_array[coordinates_array_index + 0] = rect.x(); | 265 coordinates_array[coordinates_array_index + 0] = rect.x(); |
251 coordinates_array[coordinates_array_index + 1] = rect.y(); | 266 coordinates_array[coordinates_array_index + 1] = rect.y(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 const base::android::JavaParamRef<jobject>& obj, | 324 const base::android::JavaParamRef<jobject>& obj, |
310 bool immediate_request, | 325 bool immediate_request, |
311 bool monitor_request) { | 326 bool monitor_request) { |
312 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | 327 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
313 if (!rwhi) | 328 if (!rwhi) |
314 return; | 329 return; |
315 rwhi->Send(new InputMsg_RequestCompositionUpdate( | 330 rwhi->Send(new InputMsg_RequestCompositionUpdate( |
316 rwhi->GetRoutingID(), immediate_request, monitor_request)); | 331 rwhi->GetRoutingID(), immediate_request, monitor_request)); |
317 } | 332 } |
318 | 333 |
319 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, | |
320 const JavaParamRef<jobject>&) { | |
321 java_ime_adapter_.reset(); | |
322 } | |
323 | |
324 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { | 334 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { |
325 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 335 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
326 return rwhva_->GetFocusedWidget(); | 336 return rwhva_ ? rwhva_->GetFocusedWidget() : nullptr; |
327 } | 337 } |
328 | 338 |
329 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | 339 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { |
330 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 340 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
331 // We get the focused frame from the WebContents of the page. Although | 341 // We get the focused frame from the WebContents of the page. Although |
332 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way | 342 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way |
333 // to get a RenderFrameHost from its RWH. | 343 // to get a RenderFrameHost from its RWH. |
| 344 if (!rwhva_) |
| 345 return nullptr; |
334 RenderWidgetHostImpl* rwh = | 346 RenderWidgetHostImpl* rwh = |
335 RenderWidgetHostImpl::From(rwhva_->GetRenderWidgetHost()); | 347 RenderWidgetHostImpl::From(rwhva_->GetRenderWidgetHost()); |
336 if (!rwh || !rwh->delegate()) | 348 if (!rwh || !rwh->delegate()) |
337 return nullptr; | 349 return nullptr; |
338 | 350 |
339 if (auto* contents = rwh->delegate()->GetAsWebContents()) | 351 if (auto* contents = rwh->delegate()->GetAsWebContents()) |
340 return contents->GetFocusedFrame(); | 352 return contents->GetFocusedFrame(); |
341 | 353 |
342 return nullptr; | 354 return nullptr; |
343 } | 355 } |
(...skipping 11 matching lines...) Expand all Loading... |
355 Java_ImeAdapter_populateUnderlinesFromSpans( | 367 Java_ImeAdapter_populateUnderlinesFromSpans( |
356 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 368 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
357 | 369 |
358 // Sort spans by |.startOffset|. | 370 // Sort spans by |.startOffset|. |
359 std::sort(underlines.begin(), underlines.end()); | 371 std::sort(underlines.begin(), underlines.end()); |
360 | 372 |
361 return underlines; | 373 return underlines; |
362 } | 374 } |
363 | 375 |
364 } // namespace content | 376 } // namespace content |
OLD | NEW |