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); |
118 } | |
119 | |
120 void ImeAdapterAndroid::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
121 delete this; | |
122 } | |
123 | |
124 void ImeAdapterAndroid::SetRenderWidgetHostViewAndroid( | |
125 const WeakPtr<RenderWidgetHostViewAndroid> rwhva) { | |
126 rwhva_.reset(); | |
127 rwhva_ = rwhva; | |
128 JNIEnv* env = AttachCurrentThread(); | |
129 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | |
130 if (!obj.is_null()) | |
131 Java_ImeAdapter_onConnectedToRenderProcess(env, obj); | |
111 } | 132 } |
112 | 133 |
113 ImeAdapterAndroid::~ImeAdapterAndroid() { | 134 ImeAdapterAndroid::~ImeAdapterAndroid() { |
114 JNIEnv* env = AttachCurrentThread(); | 135 if (rwhva_) |
115 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 136 rwhva_->ConnectImeAdapter(nullptr); |
116 if (!obj.is_null()) | |
117 Java_ImeAdapter_detach(env, obj); | |
118 } | 137 } |
119 | 138 |
120 bool ImeAdapterAndroid::SendKeyEvent( | 139 bool ImeAdapterAndroid::SendKeyEvent( |
121 JNIEnv* env, | 140 JNIEnv* env, |
122 const JavaParamRef<jobject>&, | 141 const JavaParamRef<jobject>&, |
123 const JavaParamRef<jobject>& original_key_event, | 142 const JavaParamRef<jobject>& original_key_event, |
124 int type, | 143 int type, |
125 int modifiers, | 144 int modifiers, |
126 jlong time_ms, | 145 jlong time_ms, |
127 int key_code, | 146 int key_code, |
128 int scan_code, | 147 int scan_code, |
129 bool is_system_key, | 148 bool is_system_key, |
130 int unicode_char) { | 149 int unicode_char) { |
150 if (!rwhva_) | |
151 return false; | |
131 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( | 152 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( |
132 env, original_key_event, type, modifiers, time_ms, key_code, scan_code, | 153 env, original_key_event, type, modifiers, time_ms, key_code, scan_code, |
133 is_system_key, unicode_char); | 154 is_system_key, unicode_char); |
134 rwhva_->SendKeyEvent(event); | 155 rwhva_->SendKeyEvent(event); |
135 return true; | 156 return true; |
136 } | 157 } |
137 | 158 |
138 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | 159 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
139 const JavaParamRef<jobject>& obj, | 160 const JavaParamRef<jobject>& obj, |
140 const JavaParamRef<jobject>& text, | 161 const JavaParamRef<jobject>& text, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 | 214 |
194 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, | 215 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, |
195 const JavaParamRef<jobject>&) { | 216 const JavaParamRef<jobject>&) { |
196 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | 217 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
197 if (!rwhi) | 218 if (!rwhi) |
198 return; | 219 return; |
199 | 220 |
200 rwhi->ImeFinishComposingText(true); | 221 rwhi->ImeFinishComposingText(true); |
201 } | 222 } |
202 | 223 |
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() { | 224 void ImeAdapterAndroid::CancelComposition() { |
210 base::android::ScopedJavaLocalRef<jobject> obj = | 225 JNIEnv* env = AttachCurrentThread(); |
211 java_ime_adapter_.get(AttachCurrentThread()); | 226 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
212 if (!obj.is_null()) | 227 if (!obj.is_null()) |
213 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj); | 228 Java_ImeAdapter_cancelComposition(env, obj); |
214 } | 229 } |
215 | 230 |
216 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { | 231 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { |
217 base::android::ScopedJavaLocalRef<jobject> obj = | 232 JNIEnv* env = AttachCurrentThread(); |
218 java_ime_adapter_.get(AttachCurrentThread()); | 233 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
219 if (!obj.is_null()) { | 234 if (!obj.is_null()) { |
220 Java_ImeAdapter_focusedNodeChanged(AttachCurrentThread(), obj, | 235 Java_ImeAdapter_focusedNodeChanged(env, obj, is_editable_node); |
221 is_editable_node); | |
222 } | 236 } |
223 } | 237 } |
224 | 238 |
225 void ImeAdapterAndroid::SetEditableSelectionOffsets( | 239 void ImeAdapterAndroid::SetEditableSelectionOffsets( |
226 JNIEnv*, | 240 JNIEnv*, |
227 const JavaParamRef<jobject>&, | 241 const JavaParamRef<jobject>&, |
228 int start, | 242 int start, |
229 int end) { | 243 int end) { |
230 RenderFrameHost* rfh = GetFocusedFrame(); | 244 RenderFrameHost* rfh = GetFocusedFrame(); |
231 if (!rfh) | 245 if (!rfh) |
232 return; | 246 return; |
233 | 247 |
234 rfh->Send(new InputMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), start, | 248 rfh->Send(new InputMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), start, |
235 end)); | 249 end)); |
236 } | 250 } |
237 | 251 |
238 void ImeAdapterAndroid::SetCharacterBounds( | 252 void ImeAdapterAndroid::SetCharacterBounds( |
239 const std::vector<gfx::RectF>& character_bounds) { | 253 const std::vector<gfx::RectF>& character_bounds) { |
240 JNIEnv* env = AttachCurrentThread(); | 254 JNIEnv* env = AttachCurrentThread(); |
241 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 255 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
242 if (obj.is_null()) | 256 if (obj.is_null()) |
243 return; | 257 return; |
244 | 258 |
245 const size_t coordinates_array_size = character_bounds.size() * 4; | 259 const size_t coordinates_array_size = character_bounds.size() * 4; |
246 std::unique_ptr<float[]> coordinates_array(new float[coordinates_array_size]); | 260 std::unique_ptr<float[]> coordinates_array(new float[coordinates_array_size]); |
247 for (size_t i = 0; i < character_bounds.size(); ++i) { | 261 for (size_t i = 0; i < character_bounds.size(); ++i) { |
248 const gfx::RectF& rect = character_bounds[i]; | 262 const gfx::RectF& rect = character_bounds[i]; |
249 const size_t coordinates_array_index = i * 4; | 263 const size_t coordinates_array_index = i * 4; |
250 coordinates_array[coordinates_array_index + 0] = rect.x(); | 264 coordinates_array[coordinates_array_index + 0] = rect.x(); |
251 coordinates_array[coordinates_array_index + 1] = rect.y(); | 265 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, | 323 const base::android::JavaParamRef<jobject>& obj, |
310 bool immediate_request, | 324 bool immediate_request, |
311 bool monitor_request) { | 325 bool monitor_request) { |
312 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | 326 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
313 if (!rwhi) | 327 if (!rwhi) |
314 return; | 328 return; |
315 rwhi->Send(new InputMsg_RequestCompositionUpdate( | 329 rwhi->Send(new InputMsg_RequestCompositionUpdate( |
316 rwhi->GetRoutingID(), immediate_request, monitor_request)); | 330 rwhi->GetRoutingID(), immediate_request, monitor_request)); |
317 } | 331 } |
318 | 332 |
319 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, | |
320 const JavaParamRef<jobject>&) { | |
321 java_ime_adapter_.reset(); | |
322 } | |
323 | |
324 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { | 333 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { |
325 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 334 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
326 return rwhva_->GetFocusedWidget(); | 335 return rwhva_ ? rwhva_->GetFocusedWidget() : nullptr; |
boliu
2017/03/23 04:17:01
changwan: does everything here run on the UI threa
Jinsuk Kim
2017/03/23 07:25:10
I read that everything runs on UI thread. Threaded
Changwan Ryu
2017/03/23 23:45:19
Correct. Everything from ThreadedInput* to ImeAdap
Jinsuk Kim
2017/03/24 02:34:23
Thanks!
| |
327 } | 336 } |
328 | 337 |
329 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | 338 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { |
330 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 339 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
331 // We get the focused frame from the WebContents of the page. Although | 340 // We get the focused frame from the WebContents of the page. Although |
332 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way | 341 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way |
333 // to get a RenderFrameHost from its RWH. | 342 // to get a RenderFrameHost from its RWH. |
343 if (!rwhva_) | |
344 return nullptr; | |
334 RenderWidgetHostImpl* rwh = | 345 RenderWidgetHostImpl* rwh = |
335 RenderWidgetHostImpl::From(rwhva_->GetRenderWidgetHost()); | 346 RenderWidgetHostImpl::From(rwhva_->GetRenderWidgetHost()); |
336 if (!rwh || !rwh->delegate()) | 347 if (!rwh || !rwh->delegate()) |
337 return nullptr; | 348 return nullptr; |
338 | 349 |
339 if (auto* contents = rwh->delegate()->GetAsWebContents()) | 350 if (auto* contents = rwh->delegate()->GetAsWebContents()) |
340 return contents->GetFocusedFrame(); | 351 return contents->GetFocusedFrame(); |
341 | 352 |
342 return nullptr; | 353 return nullptr; |
343 } | 354 } |
(...skipping 11 matching lines...) Expand all Loading... | |
355 Java_ImeAdapter_populateUnderlinesFromSpans( | 366 Java_ImeAdapter_populateUnderlinesFromSpans( |
356 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 367 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
357 | 368 |
358 // Sort spans by |.startOffset|. | 369 // Sort spans by |.startOffset|. |
359 std::sort(underlines.begin(), underlines.end()); | 370 std::sort(underlines.begin(), underlines.end()); |
360 | 371 |
361 return underlines; | 372 return underlines; |
362 } | 373 } |
363 | 374 |
364 } // namespace content | 375 } // namespace content |
OLD | NEW |