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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 time_ms / 1000.0, key_code, scan_code, is_system_key, unicode_char); | 135 time_ms / 1000.0, key_code, scan_code, is_system_key, unicode_char); |
136 rwhva_->SendKeyEvent(event); | 136 rwhva_->SendKeyEvent(event); |
137 return true; | 137 return true; |
138 } | 138 } |
139 | 139 |
140 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, | 140 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
141 const JavaParamRef<jobject>& obj, | 141 const JavaParamRef<jobject>& obj, |
142 const JavaParamRef<jobject>& text, | 142 const JavaParamRef<jobject>& text, |
143 const JavaParamRef<jstring>& text_str, | 143 const JavaParamRef<jstring>& text_str, |
144 int relative_cursor_pos) { | 144 int relative_cursor_pos) { |
145 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 145 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
146 if (!rwhi) | 146 if (!rwhi) |
147 return; | 147 return; |
148 | 148 |
149 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 149 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
150 | 150 |
151 std::vector<blink::WebCompositionUnderline> underlines = | 151 std::vector<blink::WebCompositionUnderline> underlines = |
152 GetUnderlinesFromSpans(env, obj, text, text16); | 152 GetUnderlinesFromSpans(env, obj, text, text16); |
153 | 153 |
154 // Default to plain underline if we didn't find any span that we care about. | 154 // Default to plain underline if we didn't find any span that we care about. |
155 if (underlines.empty()) { | 155 if (underlines.empty()) { |
156 underlines.push_back(blink::WebCompositionUnderline( | 156 underlines.push_back(blink::WebCompositionUnderline( |
157 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | 157 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); |
158 } | 158 } |
159 | 159 |
160 // relative_cursor_pos is as described in the Android API for | 160 // relative_cursor_pos is as described in the Android API for |
161 // InputConnection#setComposingText, whereas the parameters for | 161 // InputConnection#setComposingText, whereas the parameters for |
162 // ImeSetComposition are relative to the start of the composition. | 162 // ImeSetComposition are relative to the start of the composition. |
163 if (relative_cursor_pos > 0) | 163 if (relative_cursor_pos > 0) |
164 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; | 164 relative_cursor_pos = text16.length() + relative_cursor_pos - 1; |
165 | 165 |
166 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), | 166 rwhi->ImeSetComposition(text16, underlines, gfx::Range::InvalidRange(), |
167 relative_cursor_pos, relative_cursor_pos); | 167 relative_cursor_pos, relative_cursor_pos); |
168 } | 168 } |
169 | 169 |
170 void ImeAdapterAndroid::CommitText(JNIEnv* env, | 170 void ImeAdapterAndroid::CommitText(JNIEnv* env, |
171 const JavaParamRef<jobject>& obj, | 171 const JavaParamRef<jobject>& obj, |
172 const JavaParamRef<jobject>& text, | 172 const JavaParamRef<jobject>& text, |
173 const JavaParamRef<jstring>& text_str, | 173 const JavaParamRef<jstring>& text_str, |
174 int relative_cursor_pos) { | 174 int relative_cursor_pos) { |
175 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 175 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
176 if (!rwhi) | 176 if (!rwhi) |
177 return; | 177 return; |
178 | 178 |
179 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); | 179 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
180 | 180 |
181 std::vector<blink::WebCompositionUnderline> underlines = | 181 std::vector<blink::WebCompositionUnderline> underlines = |
182 GetUnderlinesFromSpans(env, obj, text, text16); | 182 GetUnderlinesFromSpans(env, obj, text, text16); |
183 | 183 |
184 // relative_cursor_pos is as described in the Android API for | 184 // relative_cursor_pos is as described in the Android API for |
185 // InputConnection#commitText, whereas the parameters for | 185 // InputConnection#commitText, whereas the parameters for |
186 // ImeConfirmComposition are relative to the end of the composition. | 186 // ImeConfirmComposition are relative to the end of the composition. |
187 if (relative_cursor_pos > 0) | 187 if (relative_cursor_pos > 0) |
188 relative_cursor_pos--; | 188 relative_cursor_pos--; |
189 else | 189 else |
190 relative_cursor_pos -= text16.length(); | 190 relative_cursor_pos -= text16.length(); |
191 | 191 |
192 rwhi->ImeCommitText(text16, underlines, gfx::Range::InvalidRange(), | 192 rwhi->ImeCommitText(text16, underlines, gfx::Range::InvalidRange(), |
193 relative_cursor_pos); | 193 relative_cursor_pos); |
194 } | 194 } |
195 | 195 |
196 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, | 196 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, |
197 const JavaParamRef<jobject>&) { | 197 const JavaParamRef<jobject>&) { |
198 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 198 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
199 if (!rwhi) | 199 if (!rwhi) |
200 return; | 200 return; |
201 | 201 |
202 rwhi->ImeFinishComposingText(true); | 202 rwhi->ImeFinishComposingText(true); |
203 } | 203 } |
204 | 204 |
205 void ImeAdapterAndroid::AttachImeAdapter( | 205 void ImeAdapterAndroid::AttachImeAdapter( |
206 JNIEnv* env, | 206 JNIEnv* env, |
207 const JavaParamRef<jobject>& java_object) { | 207 const JavaParamRef<jobject>& java_object) { |
208 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object); | 208 java_ime_adapter_ = JavaObjectWeakGlobalRef(env, java_object); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 int after) { | 281 int after) { |
282 RenderFrameHostImpl* rfh = | 282 RenderFrameHostImpl* rfh = |
283 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | 283 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); |
284 if (rfh) | 284 if (rfh) |
285 rfh->DeleteSurroundingText(before, after); | 285 rfh->DeleteSurroundingText(before, after); |
286 } | 286 } |
287 | 287 |
288 bool ImeAdapterAndroid::RequestTextInputStateUpdate( | 288 bool ImeAdapterAndroid::RequestTextInputStateUpdate( |
289 JNIEnv* env, | 289 JNIEnv* env, |
290 const JavaParamRef<jobject>&) { | 290 const JavaParamRef<jobject>&) { |
291 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 291 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
292 if (!rwhi) | 292 if (!rwhi) |
293 return false; | 293 return false; |
294 rwhi->Send(new InputMsg_RequestTextInputStateUpdate(rwhi->GetRoutingID())); | 294 rwhi->Send(new InputMsg_RequestTextInputStateUpdate(rwhi->GetRoutingID())); |
295 return true; | 295 return true; |
296 } | 296 } |
297 | 297 |
298 void ImeAdapterAndroid::RequestCursorUpdate( | 298 void ImeAdapterAndroid::RequestCursorUpdate( |
299 JNIEnv* env, | 299 JNIEnv* env, |
300 const base::android::JavaParamRef<jobject>& obj, | 300 const base::android::JavaParamRef<jobject>& obj, |
301 bool immediate_request, | 301 bool immediate_request, |
302 bool monitor_request) { | 302 bool monitor_request) { |
303 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 303 RenderWidgetHostImpl* rwhi = GetFocusedWidget(); |
304 if (!rwhi) | 304 if (!rwhi) |
305 return; | 305 return; |
306 rwhi->Send(new InputMsg_RequestCompositionUpdate( | 306 rwhi->Send(new InputMsg_RequestCompositionUpdate( |
307 rwhi->GetRoutingID(), immediate_request, monitor_request)); | 307 rwhi->GetRoutingID(), immediate_request, monitor_request)); |
308 } | 308 } |
309 | 309 |
310 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, | 310 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, |
311 const JavaParamRef<jobject>&) { | 311 const JavaParamRef<jobject>&) { |
312 java_ime_adapter_.reset(); | 312 java_ime_adapter_.reset(); |
313 } | 313 } |
314 | 314 |
315 RenderWidgetHostImpl* ImeAdapterAndroid::GetRenderWidgetHostImpl() { | 315 RenderWidgetHostImpl* ImeAdapterAndroid::GetPageRenderWidgetHostImpl() { |
316 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 316 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
317 DCHECK(rwhva_); | 317 DCHECK(rwhva_); |
318 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost(); | 318 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost(); |
Charlie Reis
2017/01/25 22:00:22
Isn't this the RWH associated with RenderWidgetHos
EhsanK
2017/01/26 19:16:29
I think |rwhva_| is always a RenderWidgetHostViewA
Charlie Reis
2017/01/27 01:07:22
Ah yes, I was confused about how RWHVs work (with
EhsanK
2017/01/27 16:16:46
Acknowledged.
| |
319 if (!rwh) | 319 if (!rwh) |
320 return nullptr; | 320 return nullptr; |
321 | 321 |
322 return RenderWidgetHostImpl::From(rwh); | 322 return RenderWidgetHostImpl::From(rwh); |
323 } | 323 } |
324 | 324 |
325 RenderWidgetHostImpl* ImeAdapterAndroid::GetFocusedWidget() { | |
326 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
EhsanK
2017/01/26 19:16:29
If it wasn't for this threading DCHECK, I could re
Charlie Reis
2017/01/27 01:07:22
That DCHECK isn't strictly necessary, but given th
EhsanK
2017/01/27 16:16:46
Acknowledged.
| |
327 DCHECK(rwhva_); | |
EhsanK
2017/01/25 19:27:47
This DCHECK here and everywhere else in this file
Charlie Reis
2017/01/25 22:00:22
Looks like the only previous use of it is in GetPa
EhsanK
2017/01/26 19:16:29
Acknowledged.
| |
328 return rwhva_->GetFocusedWidget(); | |
Charlie Reis
2017/01/25 22:00:22
I'm getting confused looking at RenderWidgetHostVi
EhsanK
2017/01/26 19:16:29
Since |rwhva_| is a RenderWidgetHostViewAndroid an
Charlie Reis
2017/01/27 01:07:22
Thanks-- that was the same mixup I had above.
EhsanK
2017/01/27 16:16:47
Acknowledged.
| |
329 } | |
330 | |
325 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { | 331 RenderFrameHost* ImeAdapterAndroid::GetFocusedFrame() { |
326 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 332 RenderWidgetHostImpl* rwh = GetPageRenderWidgetHostImpl(); |
Charlie Reis
2017/01/25 22:00:22
What's the reason for GetFocusedFrame() use GetPag
EhsanK
2017/01/26 19:16:29
I don't think we can get to RenderFrameHostImpl fr
Charlie Reis
2017/01/27 01:07:21
Ok.
EhsanK
2017/01/27 16:16:47
Acknowledged.
| |
327 if (!rwh) | 333 if (!rwh) |
328 return nullptr; | 334 return nullptr; |
329 RenderViewHost* rvh = RenderViewHost::From(rwh); | 335 RenderViewHost* rvh = RenderViewHost::From(rwh); |
330 if (!rvh) | 336 if (!rvh) |
331 return nullptr; | 337 return nullptr; |
332 FrameTreeNode* focused_frame = | 338 FrameTreeNode* focused_frame = |
333 rvh->GetDelegate()->GetFrameTree()->GetFocusedFrame(); | 339 rvh->GetDelegate()->GetFrameTree()->GetFocusedFrame(); |
334 if (!focused_frame) | 340 if (!focused_frame) |
335 return nullptr; | 341 return nullptr; |
336 | 342 |
337 return focused_frame->current_frame_host(); | 343 return focused_frame->current_frame_host(); |
338 } | 344 } |
339 | 345 |
340 WebContents* ImeAdapterAndroid::GetWebContents() { | 346 WebContents* ImeAdapterAndroid::GetWebContents() { |
341 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 347 RenderWidgetHostImpl* rwh = GetPageRenderWidgetHostImpl(); |
Charlie Reis
2017/01/25 22:00:22
Again, just wondering whether we need GetPageRende
EhsanK
2017/01/26 19:16:29
We actually don't since RenderWidgetHostImpl::From
Charlie Reis
2017/01/27 01:07:21
Sure.
EhsanK
2017/01/27 16:16:46
Acknowledged.
| |
342 if (!rwh) | 348 if (!rwh) |
343 return nullptr; | 349 return nullptr; |
344 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 350 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
345 } | 351 } |
346 | 352 |
347 std::vector<blink::WebCompositionUnderline> | 353 std::vector<blink::WebCompositionUnderline> |
348 ImeAdapterAndroid::GetUnderlinesFromSpans( | 354 ImeAdapterAndroid::GetUnderlinesFromSpans( |
349 JNIEnv* env, | 355 JNIEnv* env, |
350 const base::android::JavaParamRef<jobject>& obj, | 356 const base::android::JavaParamRef<jobject>& obj, |
351 const base::android::JavaParamRef<jobject>& text, | 357 const base::android::JavaParamRef<jobject>& text, |
352 const base::string16& text16) { | 358 const base::string16& text16) { |
353 std::vector<blink::WebCompositionUnderline> underlines; | 359 std::vector<blink::WebCompositionUnderline> underlines; |
354 // Iterate over spans in |text|, dispatch those that we care about (e.g., | 360 // Iterate over spans in |text|, dispatch those that we care about (e.g., |
355 // BackgroundColorSpan) to a matching callback (e.g., | 361 // BackgroundColorSpan) to a matching callback (e.g., |
356 // AppendBackgroundColorSpan()), and populate |underlines|. | 362 // AppendBackgroundColorSpan()), and populate |underlines|. |
357 Java_ImeAdapter_populateUnderlinesFromSpans( | 363 Java_ImeAdapter_populateUnderlinesFromSpans( |
358 env, obj, text, reinterpret_cast<jlong>(&underlines)); | 364 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
359 | 365 |
360 // Sort spans by |.startOffset|. | 366 // Sort spans by |.startOffset|. |
361 std::sort(underlines.begin(), underlines.end()); | 367 std::sort(underlines.begin(), underlines.end()); |
362 | 368 |
363 return underlines; | 369 return underlines; |
364 } | 370 } |
365 | 371 |
366 } // namespace content | 372 } // namespace content |
OLD | NEW |