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) { |
131 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( | 150 NativeWebKeyboardEvent event = NativeWebKeyboardEventFromKeyEvent( |
132 env, original_key_event, type, modifiers, time_ms, key_code, scan_code, | 151 env, original_key_event, type, modifiers, time_ms, key_code, scan_code, |
133 is_system_key, unicode_char); | 152 is_system_key, unicode_char); |
134 rwhva_->SendKeyEvent(event); | 153 rwhva_->SendKeyEvent(event); |
boliu
2017/03/22 16:55:02
this is a weakptr now, all these accesses need nul
Jinsuk Kim
2017/03/23 01:17:49
Add null checking to all. Chatted online to keep t
| |
135 return true; | 154 return true; |
136 } | 155 } |
137 | 156 |
138 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | 157 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
139 const JavaParamRef<jobject>& obj, | 158 const JavaParamRef<jobject>& obj, |
140 const JavaParamRef<jobject>& text, | 159 const JavaParamRef<jobject>& text, |
141 const JavaParamRef<jstring>& text_str, | 160 const JavaParamRef<jstring>& text_str, |
142 int relative_cursor_pos) { | 161 int relative_cursor_pos) { |
143 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | 162 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
144 if (!rwhi) | 163 if (!rwhi) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 | 212 |
194 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, | 213 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, |
195 const JavaParamRef<jobject>&) { | 214 const JavaParamRef<jobject>&) { |
196 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | 215 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
197 if (!rwhi) | 216 if (!rwhi) |
198 return; | 217 return; |
199 | 218 |
200 rwhi->ImeFinishComposingText(true); | 219 rwhi->ImeFinishComposingText(true); |
201 } | 220 } |
202 | 221 |
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() { | 222 void ImeAdapterAndroid::CancelComposition() { |
210 base::android::ScopedJavaLocalRef<jobject> obj = | 223 JNIEnv* env = AttachCurrentThread(); |
211 java_ime_adapter_.get(AttachCurrentThread()); | 224 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
212 if (!obj.is_null()) | 225 if (!obj.is_null()) |
213 Java_ImeAdapter_cancelComposition(AttachCurrentThread(), obj); | 226 Java_ImeAdapter_cancelComposition(env, obj); |
214 } | 227 } |
215 | 228 |
216 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { | 229 void ImeAdapterAndroid::FocusedNodeChanged(bool is_editable_node) { |
217 base::android::ScopedJavaLocalRef<jobject> obj = | 230 JNIEnv* env = AttachCurrentThread(); |
218 java_ime_adapter_.get(AttachCurrentThread()); | 231 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
219 if (!obj.is_null()) { | 232 if (!obj.is_null()) { |
220 Java_ImeAdapter_focusedNodeChanged(AttachCurrentThread(), obj, | 233 Java_ImeAdapter_focusedNodeChanged(env, obj, is_editable_node); |
221 is_editable_node); | |
222 } | 234 } |
223 } | 235 } |
224 | 236 |
225 void ImeAdapterAndroid::SetEditableSelectionOffsets( | 237 void ImeAdapterAndroid::SetEditableSelectionOffsets( |
226 JNIEnv*, | 238 JNIEnv*, |
227 const JavaParamRef<jobject>&, | 239 const JavaParamRef<jobject>&, |
228 int start, | 240 int start, |
229 int end) { | 241 int end) { |
230 RenderFrameHost* rfh = GetFocusedFrame(); | 242 RenderFrameHost* rfh = GetFocusedFrame(); |
231 if (!rfh) | 243 if (!rfh) |
232 return; | 244 return; |
233 | 245 |
234 rfh->Send(new InputMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), start, | 246 rfh->Send(new InputMsg_SetEditableSelectionOffsets(rfh->GetRoutingID(), start, |
235 end)); | 247 end)); |
236 } | 248 } |
237 | 249 |
238 void ImeAdapterAndroid::SetCharacterBounds( | 250 void ImeAdapterAndroid::SetCharacterBounds( |
239 const std::vector<gfx::RectF>& character_bounds) { | 251 const std::vector<gfx::RectF>& character_bounds) { |
240 JNIEnv* env = AttachCurrentThread(); | 252 JNIEnv* env = AttachCurrentThread(); |
241 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 253 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
242 if (obj.is_null()) | 254 if (obj.is_null()) |
243 return; | 255 return; |
244 | 256 |
245 const size_t coordinates_array_size = character_bounds.size() * 4; | 257 const size_t coordinates_array_size = character_bounds.size() * 4; |
246 std::unique_ptr<float[]> coordinates_array(new float[coordinates_array_size]); | 258 std::unique_ptr<float[]> coordinates_array(new float[coordinates_array_size]); |
247 for (size_t i = 0; i < character_bounds.size(); ++i) { | 259 for (size_t i = 0; i < character_bounds.size(); ++i) { |
248 const gfx::RectF& rect = character_bounds[i]; | 260 const gfx::RectF& rect = character_bounds[i]; |
249 const size_t coordinates_array_index = i * 4; | 261 const size_t coordinates_array_index = i * 4; |
250 coordinates_array[coordinates_array_index + 0] = rect.x(); | 262 coordinates_array[coordinates_array_index + 0] = rect.x(); |
251 coordinates_array[coordinates_array_index + 1] = rect.y(); | 263 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, | 321 const base::android::JavaParamRef<jobject>& obj, |
310 bool immediate_request, | 322 bool immediate_request, |
311 bool monitor_request) { | 323 bool monitor_request) { |
312 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); | 324 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
313 if (!rwhi) | 325 if (!rwhi) |
314 return; | 326 return; |
315 rwhi->Send(new InputMsg_RequestCompositionUpdate( | 327 rwhi->Send(new InputMsg_RequestCompositionUpdate( |
316 rwhi->GetRoutingID(), immediate_request, monitor_request)); | 328 rwhi->GetRoutingID(), immediate_request, monitor_request)); |
317 } | 329 } |
318 | 330 |
319 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, | |
320 const JavaParamRef<jobject>&) { | |
321 java_ime_adapter_.reset(); | |
322 } | |
323 | |
324 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { | 331 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { |
325 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 332 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
326 return rwhva_->GetFocusedWidget(); | 333 return rwhva_->GetFocusedWidget(); |
327 } | 334 } |
328 | 335 |
329 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | 336 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { |
330 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 337 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
331 // We get the focused frame from the WebContents of the page. Although | 338 // We get the focused frame from the WebContents of the page. Although |
332 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way | 339 // |rwhva_->GetFocusedWidget()| does a similar thing, there is no direct way |
333 // to get a RenderFrameHost from its RWH. | 340 // to get a RenderFrameHost from its RWH. |
(...skipping 21 matching lines...) Expand all Loading... | |
355 Java_ImeAdapter_populateUnderlinesFromSpans( | 362 Java_ImeAdapter_populateUnderlinesFromSpans( |
356 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 363 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
357 | 364 |
358 // Sort spans by |.startOffset|. | 365 // Sort spans by |.startOffset|. |
359 std::sort(underlines.begin(), underlines.end()); | 366 std::sort(underlines.begin(), underlines.end()); |
360 | 367 |
361 return underlines; | 368 return underlines; |
362 } | 369 } |
363 | 370 |
364 } // namespace content | 371 } // namespace content |
OLD | NEW |