Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(345)

Side by Side Diff: content/browser/renderer_host/ime_adapter_android.cc

Issue 2650113004: [WIP] Add support for Android SuggestionSpans when editing text (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 12 matching lines...) Expand all
23 #include "content/browser/renderer_host/render_widget_host_view_android.h" 23 #include "content/browser/renderer_host/render_widget_host_view_android.h"
24 #include "content/common/input_messages.h" 24 #include "content/common/input_messages.h"
25 #include "content/common/view_messages.h" 25 #include "content/common/view_messages.h"
26 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/native_web_keyboard_event.h" 27 #include "content/public/browser/native_web_keyboard_event.h"
28 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
29 #include "jni/ImeAdapter_jni.h" 29 #include "jni/ImeAdapter_jni.h"
30 #include "third_party/WebKit/public/platform/WebInputEvent.h" 30 #include "third_party/WebKit/public/platform/WebInputEvent.h"
31 #include "third_party/WebKit/public/platform/WebTextInputType.h" 31 #include "third_party/WebKit/public/platform/WebTextInputType.h"
32 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" 32 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
33 #include "third_party/WebKit/public/web/WebTextSuggestionInfo.h"
rlanday 2017/01/25 02:27:23 One thing I'm curious about is that there's a lint
33 34
34 using base::android::AttachCurrentThread; 35 using base::android::AttachCurrentThread;
35 using base::android::ConvertJavaStringToUTF16; 36 using base::android::ConvertJavaStringToUTF16;
37 using base::android::ConvertUTF8ToJavaString;
38 using base::android::GetClass;
39 using base::android::MethodID;
36 using base::android::JavaParamRef; 40 using base::android::JavaParamRef;
41 using base::android::ScopedJavaLocalRef;
37 42
38 namespace content { 43 namespace content {
39 namespace { 44 namespace {
40 45
41 // Maps a java KeyEvent into a NativeWebKeyboardEvent. 46 // Maps a java KeyEvent into a NativeWebKeyboardEvent.
42 // |java_key_event| is used to maintain a globalref for KeyEvent. 47 // |java_key_event| is used to maintain a globalref for KeyEvent.
43 // |type| will determine the WebInputEvent type. 48 // |type| will determine the WebInputEvent type.
44 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create 49 // type, |modifiers|, |time_ms|, |key_code|, |unicode_char| is used to create
45 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key 50 // WebKeyboardEvent. |key_code| is also needed ad need to treat the enter key
46 // as a key press of character \r. 51 // as a key press of character \r.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( 86 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>(
82 underlines_ptr); 87 underlines_ptr);
83 underlines->push_back( 88 underlines->push_back(
84 blink::WebCompositionUnderline(static_cast<unsigned>(start), 89 blink::WebCompositionUnderline(static_cast<unsigned>(start),
85 static_cast<unsigned>(end), 90 static_cast<unsigned>(end),
86 SK_ColorTRANSPARENT, 91 SK_ColorTRANSPARENT,
87 false, 92 false,
88 static_cast<unsigned>(background_color))); 93 static_cast<unsigned>(background_color)));
89 } 94 }
90 95
96 // Callback from Java to convert SuggestionSpan data to a
97 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|.
98 void AppendSuggestionSpan(JNIEnv* env,
99 const JavaParamRef<jclass>&,
100 jlong underlines_ptr,
101 jint start,
102 jint end,
103 jint underline_color,
104 jint flags,
105 const JavaParamRef<jobjectArray>& suggestions) {
106 DCHECK_GE(start, 0);
107 DCHECK_GE(end, 0);
108 std::vector<blink::WebCompositionUnderline>* underlines =
109 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>(
110 underlines_ptr);
111
112 std::vector<std::string> suggestions_vec;
113 AppendJavaStringArrayToStringVector(env, suggestions, &suggestions_vec);
114
115 underlines->push_back(blink::WebCompositionUnderline(
116 static_cast<unsigned>(start), static_cast<unsigned>(end),
117 static_cast<unsigned>(underline_color), true, SK_ColorTRANSPARENT,
118 suggestions_vec));
119 }
120
91 // Callback from Java to convert UnderlineSpan data to a 121 // Callback from Java to convert UnderlineSpan data to a
92 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. 122 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|.
93 void AppendUnderlineSpan(JNIEnv*, 123 void AppendUnderlineSpan(JNIEnv*,
94 const JavaParamRef<jclass>&, 124 const JavaParamRef<jclass>&,
95 jlong underlines_ptr, 125 jlong underlines_ptr,
96 jint start, 126 jint start,
97 jint end) { 127 jint end) {
98 DCHECK_GE(start, 0); 128 DCHECK_GE(start, 0);
99 DCHECK_GE(end, 0); 129 DCHECK_GE(end, 0);
100 std::vector<blink::WebCompositionUnderline>* underlines = 130 std::vector<blink::WebCompositionUnderline>* underlines =
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 coordinates_array[coordinates_array_index + 0] = rect.x(); 282 coordinates_array[coordinates_array_index + 0] = rect.x();
253 coordinates_array[coordinates_array_index + 1] = rect.y(); 283 coordinates_array[coordinates_array_index + 1] = rect.y();
254 coordinates_array[coordinates_array_index + 2] = rect.right(); 284 coordinates_array[coordinates_array_index + 2] = rect.right();
255 coordinates_array[coordinates_array_index + 3] = rect.bottom(); 285 coordinates_array[coordinates_array_index + 3] = rect.bottom();
256 } 286 }
257 Java_ImeAdapter_setCharacterBounds( 287 Java_ImeAdapter_setCharacterBounds(
258 env, obj, base::android::ToJavaFloatArray(env, coordinates_array.get(), 288 env, obj, base::android::ToJavaFloatArray(env, coordinates_array.get(),
259 coordinates_array_size)); 289 coordinates_array_size));
260 } 290 }
261 291
292 void ImeAdapterAndroid::ShowTextSuggestionMenu(
293 const std::vector<blink::WebTextSuggestionInfo>& suggestion_infos) {
294 JNIEnv* env = AttachCurrentThread();
295 ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env);
296
297 ScopedJavaLocalRef<jclass> clazz = GetClass(
298 env, "org/chromium/content/browser/input/ImeAdapter$SuggestionInfo");
299 jmethodID constructor = MethodID::Get<MethodID::TYPE_INSTANCE>(
300 env, clazz.obj(), "<init>",
301 "(Lorg/chromium/content/browser/input/ImeAdapter;IILjava/lang/"
302 "String;Ljava/lang/String;Ljava/lang/String;)V");
303 ScopedJavaLocalRef<jobjectArray> suggestion_infos_for_java(
304 env, env->NewObjectArray(suggestion_infos.size(), clazz.obj(), nullptr));
305
306 for (size_t i = 0; i < suggestion_infos.size(); i++) {
307 const blink::WebTextSuggestionInfo& suggestion_info = suggestion_infos[i];
308 ScopedJavaLocalRef<jobject> suggestion_info_for_java(
309 env,
310 env->NewObject(
311 clazz.obj(), constructor, obj.obj(),
312 suggestion_info.documentMarkerID, suggestion_info.suggestionIndex,
313 ConvertUTF8ToJavaString(env, suggestion_info.prefix).obj(),
314 ConvertUTF8ToJavaString(env, suggestion_info.suggestion).obj(),
315 ConvertUTF8ToJavaString(env, suggestion_info.suffix).obj()));
316 env->SetObjectArrayElement(suggestion_infos_for_java.obj(), i,
317 suggestion_info_for_java.obj());
318 }
319
320 Java_ImeAdapter_showSuggestionMenu(env, obj, suggestion_infos_for_java.obj());
321 }
322
262 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, 323 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*,
263 const JavaParamRef<jobject>&, 324 const JavaParamRef<jobject>&,
264 int start, 325 int start,
265 int end) { 326 int end) {
266 RenderFrameHost* rfh = GetFocusedFrame(); 327 RenderFrameHost* rfh = GetFocusedFrame();
267 if (!rfh) 328 if (!rfh)
268 return; 329 return;
269 330
270 std::vector<blink::WebCompositionUnderline> underlines; 331 std::vector<blink::WebCompositionUnderline> underlines;
271 underlines.push_back(blink::WebCompositionUnderline( 332 underlines.push_back(blink::WebCompositionUnderline(
(...skipping 28 matching lines...) Expand all
300 const base::android::JavaParamRef<jobject>& obj, 361 const base::android::JavaParamRef<jobject>& obj,
301 bool immediate_request, 362 bool immediate_request,
302 bool monitor_request) { 363 bool monitor_request) {
303 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); 364 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl();
304 if (!rwhi) 365 if (!rwhi)
305 return; 366 return;
306 rwhi->Send(new InputMsg_RequestCompositionUpdate( 367 rwhi->Send(new InputMsg_RequestCompositionUpdate(
307 rwhi->GetRoutingID(), immediate_request, monitor_request)); 368 rwhi->GetRoutingID(), immediate_request, monitor_request));
308 } 369 }
309 370
371 void ImeAdapterAndroid::ApplySuggestionReplacement(JNIEnv*,
372 const JavaParamRef<jobject>&,
373 int documentMarkerID,
374 int suggestionIndex) {
375 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl();
376 if (!rwhi)
377 return;
378
379 rwhi->ImeApplySuggestionReplacement(documentMarkerID, suggestionIndex);
380 }
381
382 void ImeAdapterAndroid::DeleteSuggestionHighlight(
383 JNIEnv*,
384 const JavaParamRef<jobject>&) {
385 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl();
386 if (!rwhi)
387 return;
388
389 rwhi->ImeDeleteSuggestionHighlight();
390 }
391
392 void ImeAdapterAndroid::CloseSuggestionMenu(JNIEnv*,
393 const JavaParamRef<jobject>&) {
394 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl();
395 if (!rwhi)
396 return;
397
398 rwhi->ImeCloseSuggestionMenu();
399 }
400
310 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env, 401 void ImeAdapterAndroid::ResetImeAdapter(JNIEnv* env,
311 const JavaParamRef<jobject>&) { 402 const JavaParamRef<jobject>&) {
312 java_ime_adapter_.reset(); 403 java_ime_adapter_.reset();
313 } 404 }
314 405
315 RenderWidgetHostImpl* ImeAdapterAndroid::GetRenderWidgetHostImpl() { 406 RenderWidgetHostImpl* ImeAdapterAndroid::GetRenderWidgetHostImpl() {
316 DCHECK_CURRENTLY_ON(BrowserThread::UI); 407 DCHECK_CURRENTLY_ON(BrowserThread::UI);
317 DCHECK(rwhva_); 408 DCHECK(rwhva_);
318 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost(); 409 RenderWidgetHost* rwh = rwhva_->GetRenderWidgetHost();
319 if (!rwh) 410 if (!rwh)
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 Java_ImeAdapter_populateUnderlinesFromSpans( 448 Java_ImeAdapter_populateUnderlinesFromSpans(
358 env, obj, text, reinterpret_cast<jlong>(&underlines)); 449 env, obj, text, reinterpret_cast<jlong>(&underlines));
359 450
360 // Sort spans by |.startOffset|. 451 // Sort spans by |.startOffset|.
361 std::sort(underlines.begin(), underlines.end()); 452 std::sort(underlines.begin(), underlines.end());
362 453
363 return underlines; 454 return underlines;
364 } 455 }
365 456
366 } // namespace content 457 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698