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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 underlines->push_back( | 103 underlines->push_back( |
104 blink::WebCompositionUnderline(static_cast<unsigned>(start), | 104 blink::WebCompositionUnderline(static_cast<unsigned>(start), |
105 static_cast<unsigned>(end), | 105 static_cast<unsigned>(end), |
106 SK_ColorBLACK, | 106 SK_ColorBLACK, |
107 false, | 107 false, |
108 SK_ColorTRANSPARENT)); | 108 SK_ColorTRANSPARENT)); |
109 } | 109 } |
110 | 110 |
111 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva) | 111 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva) |
112 : rwhva_(rwhva) { | 112 : rwhva_(rwhva) { |
113 DCHECK(rwhva_); | |
113 } | 114 } |
114 | 115 |
115 ImeAdapterAndroid::~ImeAdapterAndroid() { | 116 ImeAdapterAndroid::~ImeAdapterAndroid() { |
116 JNIEnv* env = AttachCurrentThread(); | 117 JNIEnv* env = AttachCurrentThread(); |
117 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 118 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
118 if (!obj.is_null()) | 119 if (!obj.is_null()) |
119 Java_ImeAdapter_detach(env, obj); | 120 Java_ImeAdapter_detach(env, obj); |
120 } | 121 } |
121 | 122 |
122 bool ImeAdapterAndroid::SendKeyEvent( | 123 bool ImeAdapterAndroid::SendKeyEvent( |
(...skipping 12 matching lines...) Expand all Loading... | |
135 time_ms / 1000.0, key_code, scan_code, is_system_key, unicode_char); | 136 time_ms / 1000.0, key_code, scan_code, is_system_key, unicode_char); |
136 rwhva_->SendKeyEvent(event); | 137 rwhva_->SendKeyEvent(event); |
137 return true; | 138 return true; |
138 } | 139 } |
139 | 140 |
140 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | 141 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
141 const JavaParamRef<jobject>& obj, | 142 const JavaParamRef<jobject>& obj, |
142 const JavaParamRef<jobject>& text, | 143 const JavaParamRef<jobject>& text, |
143 const JavaParamRef<jstring>& text_str, | 144 const JavaParamRef<jstring>& text_str, |
144 int relative_cursor_pos) { | 145 int relative_cursor_pos) { |
145 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 146 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
146 if (!rwhi) | 147 if (!rwhi) |
147 return; | 148 return; |
148 | 149 |
149 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 150 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
150 | 151 |
151 std::vector<blink::WebCompositionUnderline> underlines = | 152 std::vector<blink::WebCompositionUnderline> underlines = |
152 GetUnderlinesFromSpans(env, obj, text, text16); | 153 GetUnderlinesFromSpans(env, obj, text, text16); |
153 | 154 |
154 // Default to plain underline if we didn't find any span that we care about. | 155 // Default to plain underline if we didn't find any span that we care about. |
155 if (underlines.empty()) { | 156 if (underlines.empty()) { |
156 underlines.push_back(blink::WebCompositionUnderline( | 157 underlines.push_back(blink::WebCompositionUnderline( |
157 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | 158 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); |
158 } | 159 } |
159 | 160 |
160 // relative_cursor_pos is as described in the Android API for | 161 // relative_cursor_pos is as described in the Android API for |
161 // InputConnection#setComposingText, whereas the parameters for | 162 // InputConnection#setComposingText, whereas the parameters for |
162 // ImeSetComposition are relative to the start of the composition. | 163 // ImeSetComposition are relative to the start of the composition. |
163 if (relative_cursor_pos > 0) | 164 if (relative_cursor_pos > 0) |
164 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; | 165 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; |
165 | 166 |
166 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), | 167 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), |
167 relative_cursor_pos, relative_cursor_pos); | 168 relative_cursor_pos, relative_cursor_pos); |
168 } | 169 } |
169 | 170 |
170 void ImeAdapterAndroid::CommitText(JNIEnv* env, | 171 void ImeAdapterAndroid::CommitText(JNIEnv* env, |
171 const JavaParamRef<jobject>& obj, | 172 const JavaParamRef<jobject>& obj, |
172 const JavaParamRef<jobject>& text, | 173 const JavaParamRef<jobject>& text, |
173 const JavaParamRef<jstring>& text_str, | 174 const JavaParamRef<jstring>& text_str, |
174 int relative_cursor_pos) { | 175 int relative_cursor_pos) { |
175 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 176 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
176 if (!rwhi) | 177 if (!rwhi) |
177 return; | 178 return; |
178 | 179 |
179 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 180 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
180 | 181 |
181 std::vector<blink::WebCompositionUnderline> underlines = | 182 std::vector<blink::WebCompositionUnderline> underlines = |
182 GetUnderlinesFromSpans(env, obj, text, text16); | 183 GetUnderlinesFromSpans(env, obj, text, text16); |
183 | 184 |
184 // relative_cursor_pos is as described in the Android API for | 185 // relative_cursor_pos is as described in the Android API for |
185 // InputConnection#commitText, whereas the parameters for | 186 // InputConnection#commitText, whereas the parameters for |
186 // ImeConfirmComposition are relative to the end of the composition. | 187 // ImeConfirmComposition are relative to the end of the composition. |
187 if (relative_cursor_pos > 0) | 188 if (relative_cursor_pos > 0) |
188 relative_cursor_pos--; | 189 relative_cursor_pos--; |
189 else | 190 else |
190 relative_cursor_pos -= text16.length(); | 191 relative_cursor_pos -= text16.length(); |
191 | 192 |
192 rwhi->ImeCommitText(text16, underlines, gfx::Range::InvalidRange(), | 193 rwhi->ImeCommitText(text16, underlines, gfx::Range::InvalidRange(), |
193 relative_cursor_pos); | 194 relative_cursor_pos); |
194 } | 195 } |
195 | 196 |
196 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, | 197 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, |
197 const JavaParamRef<jobject>&) { | 198 const JavaParamRef<jobject>&) { |
198 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 199 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
199 if (!rwhi) | 200 if (!rwhi) |
200 return; | 201 return; |
201 | 202 |
202 rwhi->ImeFinishComposingText(true); | 203 rwhi->ImeFinishComposingText(true); |
203 } | 204 } |
204 | 205 |
205 void ImeAdapterAndroid::AttachImeAdapter( | 206 void ImeAdapterAndroid::AttachImeAdapter( |
206 JNIEnv* env, | 207 JNIEnv* env, |
207 const JavaParamRef<jobject>& java_object) { | 208 const JavaParamRef<jobject>& java_object) { |
208 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object); | 209 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 int after) { | 282 int after) { |
282 RenderFrameHostImpl* rfh = | 283 RenderFrameHostImpl* rfh = |
283 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | 284 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); |
284 if (rfh) | 285 if (rfh) |
285 rfh->DeleteSurroundingText(before, after); | 286 rfh->DeleteSurroundingText(before, after); |
286 } | 287 } |
287 | 288 |
288 bool ImeAdapterAndroid::RequestTextInputStateUpdate( | 289 bool ImeAdapterAndroid::RequestTextInputStateUpdate( |
289 JNIEnv* env, | 290 JNIEnv* env, |
290 const JavaParamRef<jobject>&) { | 291 const JavaParamRef<jobject>&) { |
291 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 292 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
292 if (!rwhi) | 293 if (!rwhi) |
293 return false; | 294 return false; |
294 rwhi->Send(new InputMsg_RequestTextInputStateUpdate(rwhi->GetRoutingID())); | 295 rwhi->Send(new InputMsg_RequestTextInputStateUpdate(rwhi->GetRoutingID())); |
295 return true; | 296 return true; |
296 } | 297 } |
297 | 298 |
298 void ImeAdapterAndroid::RequestCursorUpdate( | 299 void ImeAdapterAndroid::RequestCursorUpdate( |
299 JNIEnv* env, | 300 JNIEnv* env, |
300 const base::android::JavaParamRef<jobject>& obj, | 301 const base::android::JavaParamRef<jobject>& obj, |
301 bool immediate_request, | 302 bool immediate_request, |
302 bool monitor_request) { | 303 bool monitor_request) { |
303 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 304 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
304 if (!rwhi) | 305 if (!rwhi) |
305 return; | 306 return; |
306 rwhi->Send(new InputMsg_RequestCompositionUpdate( | 307 rwhi->Send(new InputMsg_RequestCompositionUpdate( |
307 rwhi->GetRoutingID(), immediate_request, monitor_request)); | 308 rwhi->GetRoutingID(), immediate_request, monitor_request)); |
308 } | 309 } |
309 | 310 |
310 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, | 311 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, |
311 const JavaParamRef<jobject>&) { | 312 const JavaParamRef<jobject>&) { |
312 java_ime_adapter_.reset(); | 313 java_ime_adapter_.reset(); |
313 } | 314 } |
314 | 315 |
315 RenderWidgetHostImpl* ImeAdapterAndroid::GetRenderWidgetHostImpl() { | 316 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { |
316 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 317 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
317 DCHECK(rwhva_); | 318 return rwhva_->GetFocusedWidget(); |
318 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost(); | 319 } |
320 | |
321 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | |
322 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
323 RenderWidgetHostImpl* rwh = | |
324 RenderWidgetHostImpl::From(rwhva_->GetRenderWidgetHost()); | |
Charlie Reis
2017/01/27 01:07:22
This would effectively revert boliu's https://code
EhsanK
2017/01/27 16:16:47
Acknowledged. Thanks for finding out about those C
| |
319 if (!rwh) | 325 if (!rwh) |
320 return nullptr; | 326 return nullptr; |
321 | 327 |
322 return RenderWidgetHostImpl::From(rwh); | |
323 } | |
324 | |
325 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | |
326 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | |
327 if (!rwh) | |
328 return nullptr; | |
329 RenderViewHost* rvh = RenderViewHost::From(rwh); | 328 RenderViewHost* rvh = RenderViewHost::From(rwh); |
330 if (!rvh) | 329 if (!rvh) |
331 return nullptr; | 330 return nullptr; |
331 | |
332 FrameTreeNode* focused_frame = | 332 FrameTreeNode* focused_frame = |
333 rvh->GetDelegate()->GetFrameTree()->GetFocusedFrame(); | 333 rvh->GetDelegate()->GetFrameTree()->GetFocusedFrame(); |
334 if (!focused_frame) | 334 if (!focused_frame) |
335 return nullptr; | 335 return nullptr; |
336 | 336 |
337 return focused_frame->current_frame_host(); | 337 return focused_frame->current_frame_host(); |
338 } | 338 } |
339 | 339 |
340 WebContents* ImeAdapterAndroid::GetWebContents() { | 340 WebContents* ImeAdapterAndroid::GetWebContents() { |
341 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 341 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
342 if (!rwh) | 342 RenderWidgetHostImpl* rwh = |
343 RenderWidgetHostImpl::From(rwhva_->GetRenderWidgetHost()); | |
Charlie Reis
2017/01/27 01:07:22
As above, please include a comment.
EhsanK
2017/01/27 16:16:47
Acknowledged.
| |
344 if (!rwh || !rwh->delegate()) | |
343 return nullptr; | 345 return nullptr; |
344 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 346 |
347 return rwh->delegate()->GetAsWebContents(); | |
345 } | 348 } |
346 | 349 |
347 std::vector<blink::WebCompositionUnderline> | 350 std::vector<blink::WebCompositionUnderline> |
348 ImeAdapterAndroid::GetUnderlinesFromSpans( | 351 ImeAdapterAndroid::GetUnderlinesFromSpans( |
349 JNIEnv* env, | 352 JNIEnv* env, |
350 const base::android::JavaParamRef<jobject>& obj, | 353 const base::android::JavaParamRef<jobject>& obj, |
351 const base::android::JavaParamRef<jobject>& text, | 354 const base::android::JavaParamRef<jobject>& text, |
352 const base::string16& text16) { | 355 const base::string16& text16) { |
353 std::vector<blink::WebCompositionUnderline> underlines; | 356 std::vector<blink::WebCompositionUnderline> underlines; |
354 // Iterate over spans in |text|, dispatch those that we care about (e.g., | 357 // Iterate over spans in |text|, dispatch those that we care about (e.g., |
355 // BackgroundColorSpan) to a matching callback (e.g., | 358 // BackgroundColorSpan) to a matching callback (e.g., |
356 // AppendBackgroundColorSpan()), and populate |underlines|. | 359 // AppendBackgroundColorSpan()), and populate |underlines|. |
357 Java_ImeAdapter_populateUnderlinesFromSpans( | 360 Java_ImeAdapter_populateUnderlinesFromSpans( |
358 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 361 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
359 | 362 |
360 // Sort spans by |.startOffset|. | 363 // Sort spans by |.startOffset|. |
361 std::sort(underlines.begin(), underlines.end()); | 364 std::sort(underlines.begin(), underlines.end()); |
362 | 365 |
363 return underlines; | 366 return underlines; |
364 } | 367 } |
365 | 368 |
366 } // namespace content | 369 } // namespace content |
OLD | NEW |