| 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 <android/input.h> | 8 #include <android/input.h> |
| 9 #include <vector> |
| 8 | 10 |
| 9 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 11 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 14 #include "content/browser/frame_host/frame_tree.h" | 16 #include "content/browser/frame_host/frame_tree.h" |
| 15 #include "content/browser/frame_host/frame_tree_node.h" | 17 #include "content/browser/frame_host/frame_tree_node.h" |
| 16 #include "content/browser/frame_host/render_frame_host_impl.h" | 18 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 17 #include "content/browser/renderer_host/render_view_host_delegate.h" | 19 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 ui::TEXT_INPUT_TYPE_PASSWORD, | 82 ui::TEXT_INPUT_TYPE_PASSWORD, |
| 81 ui::TEXT_INPUT_TYPE_SEARCH, | 83 ui::TEXT_INPUT_TYPE_SEARCH, |
| 82 ui::TEXT_INPUT_TYPE_URL, | 84 ui::TEXT_INPUT_TYPE_URL, |
| 83 ui::TEXT_INPUT_TYPE_EMAIL, | 85 ui::TEXT_INPUT_TYPE_EMAIL, |
| 84 ui::TEXT_INPUT_TYPE_TELEPHONE, | 86 ui::TEXT_INPUT_TYPE_TELEPHONE, |
| 85 ui::TEXT_INPUT_TYPE_NUMBER, | 87 ui::TEXT_INPUT_TYPE_NUMBER, |
| 86 ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE); | 88 ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE); |
| 87 return true; | 89 return true; |
| 88 } | 90 } |
| 89 | 91 |
| 92 // Callback from Java to convert BackgroundColorSpan data to a |
| 93 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. |
| 94 void AppendBackgroundColorSpan(JNIEnv*, |
| 95 jclass, |
| 96 jlong underlines_ptr, |
| 97 jint start, |
| 98 jint end, |
| 99 jint background_color) { |
| 100 DCHECK(start >= 0); |
| 101 DCHECK(end >= 0); |
| 102 // Do not check |background_color|. |
| 103 std::vector<blink::WebCompositionUnderline>* underlines = |
| 104 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
| 105 underlines_ptr); |
| 106 underlines->push_back( |
| 107 blink::WebCompositionUnderline(static_cast<unsigned>(start), |
| 108 static_cast<unsigned>(end), |
| 109 SK_ColorTRANSPARENT, |
| 110 false, |
| 111 static_cast<unsigned>(background_color))); |
| 112 } |
| 113 |
| 114 // Callback from Java to convert UnderlineSpan data to a |
| 115 // blink::WebCompositionUnderline instance, and append it to |underlines_ptr|. |
| 116 void AppendUnderlineSpan(JNIEnv*, |
| 117 jclass, |
| 118 jlong underlines_ptr, |
| 119 jint start, |
| 120 jint end) { |
| 121 DCHECK(start >= 0); |
| 122 DCHECK(end >= 0); |
| 123 std::vector<blink::WebCompositionUnderline>* underlines = |
| 124 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
| 125 underlines_ptr); |
| 126 underlines->push_back( |
| 127 blink::WebCompositionUnderline(static_cast<unsigned>(start), |
| 128 static_cast<unsigned>(end), |
| 129 SK_ColorBLACK, |
| 130 false, |
| 131 SK_ColorTRANSPARENT)); |
| 132 } |
| 133 |
| 90 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva) | 134 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva) |
| 91 : rwhva_(rwhva) { | 135 : rwhva_(rwhva) { |
| 92 } | 136 } |
| 93 | 137 |
| 94 ImeAdapterAndroid::~ImeAdapterAndroid() { | 138 ImeAdapterAndroid::~ImeAdapterAndroid() { |
| 95 JNIEnv* env = AttachCurrentThread(); | 139 JNIEnv* env = AttachCurrentThread(); |
| 96 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 140 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 97 if (!obj.is_null()) | 141 if (!obj.is_null()) |
| 98 Java_ImeAdapter_detach(env, obj.obj()); | 142 Java_ImeAdapter_detach(env, obj.obj()); |
| 99 } | 143 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 // roundtrip back to java such synthetic event. | 175 // roundtrip back to java such synthetic event. |
| 132 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, | 176 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, |
| 133 time_ms / 1000.0, key_code, unicode_char, | 177 time_ms / 1000.0, key_code, unicode_char, |
| 134 is_system_key); | 178 is_system_key); |
| 135 char_event.skip_in_browser = key_down_text_insertion; | 179 char_event.skip_in_browser = key_down_text_insertion; |
| 136 rwhva_->SendKeyEvent(char_event); | 180 rwhva_->SendKeyEvent(char_event); |
| 137 } | 181 } |
| 138 return true; | 182 return true; |
| 139 } | 183 } |
| 140 | 184 |
| 141 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, jobject, jstring text, | 185 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
| 186 jobject obj, |
| 187 jobject text, |
| 188 jstring text_str, |
| 142 int new_cursor_pos) { | 189 int new_cursor_pos) { |
| 143 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 190 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 144 if (!rwhi) | 191 if (!rwhi) |
| 145 return; | 192 return; |
| 146 | 193 |
| 147 base::string16 text16 = ConvertJavaStringToUTF16(env, text); | 194 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 195 |
| 148 std::vector<blink::WebCompositionUnderline> underlines; | 196 std::vector<blink::WebCompositionUnderline> underlines; |
| 149 underlines.push_back( | 197 // Iterate over spans in |text|, dispatch those that we care about (e.g., |
| 150 blink::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK, | 198 // BackgroundColorSpan) to a matching callback (e.g., |
| 151 false)); | 199 // AppendBackgroundColorSpan()), and populate |underlines|. |
| 200 Java_ImeAdapter_populateUnderlinesFromSpans( |
| 201 env, obj, text, reinterpret_cast<jlong>(&underlines)); |
| 202 |
| 203 // Default to plain underline if we didn't find any span that we care about. |
| 204 if (underlines.empty()) { |
| 205 underlines.push_back(blink::WebCompositionUnderline( |
| 206 0, text16.length(), SK_ColorBLACK, false, SK_ColorTRANSPARENT)); |
| 207 } |
| 208 // Sort spans by |.startOffset|. |
| 209 std::sort(underlines.begin(), underlines.end()); |
| 210 |
| 152 // new_cursor_position is as described in the Android API for | 211 // new_cursor_position is as described in the Android API for |
| 153 // InputConnection#setComposingText, whereas the parameters for | 212 // InputConnection#setComposingText, whereas the parameters for |
| 154 // ImeSetComposition are relative to the start of the composition. | 213 // ImeSetComposition are relative to the start of the composition. |
| 155 if (new_cursor_pos > 0) | 214 if (new_cursor_pos > 0) |
| 156 new_cursor_pos = text16.length() + new_cursor_pos - 1; | 215 new_cursor_pos = text16.length() + new_cursor_pos - 1; |
| 157 | 216 |
| 158 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); | 217 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); |
| 159 } | 218 } |
| 160 | 219 |
| 161 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text) { | 220 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text_str) { |
| 162 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 221 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 163 if (!rwhi) | 222 if (!rwhi) |
| 164 return; | 223 return; |
| 165 | 224 |
| 166 base::string16 text16 = ConvertJavaStringToUTF16(env, text); | 225 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 167 rwhi->ImeConfirmComposition(text16, gfx::Range::InvalidRange(), false); | 226 rwhi->ImeConfirmComposition(text16, gfx::Range::InvalidRange(), false); |
| 168 } | 227 } |
| 169 | 228 |
| 170 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, jobject) { | 229 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, jobject) { |
| 171 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 230 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 172 if (!rwhi) | 231 if (!rwhi) |
| 173 return; | 232 return; |
| 174 | 233 |
| 175 rwhi->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(), | 234 rwhi->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(), |
| 176 true); | 235 true); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 207 start, end)); | 266 start, end)); |
| 208 } | 267 } |
| 209 | 268 |
| 210 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, | 269 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, |
| 211 int start, int end) { | 270 int start, int end) { |
| 212 RenderFrameHost* rfh = GetFocusedFrame(); | 271 RenderFrameHost* rfh = GetFocusedFrame(); |
| 213 if (!rfh) | 272 if (!rfh) |
| 214 return; | 273 return; |
| 215 | 274 |
| 216 std::vector<blink::WebCompositionUnderline> underlines; | 275 std::vector<blink::WebCompositionUnderline> underlines; |
| 217 underlines.push_back( | 276 underlines.push_back(blink::WebCompositionUnderline( |
| 218 blink::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false)); | 277 0, end - start, SK_ColorBLACK, false, SK_ColorTRANSPARENT)); |
| 219 | 278 |
| 220 rfh->Send(new FrameMsg_SetCompositionFromExistingText( | 279 rfh->Send(new FrameMsg_SetCompositionFromExistingText( |
| 221 rfh->GetRoutingID(), start, end, underlines)); | 280 rfh->GetRoutingID(), start, end, underlines)); |
| 222 } | 281 } |
| 223 | 282 |
| 224 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, | 283 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, |
| 225 int before, int after) { | 284 int before, int after) { |
| 226 RenderFrameHostImpl* rfh = | 285 RenderFrameHostImpl* rfh = |
| 227 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | 286 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); |
| 228 if (rfh) | 287 if (rfh) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 WebContents* ImeAdapterAndroid::GetWebContents() { | 350 WebContents* ImeAdapterAndroid::GetWebContents() { |
| 292 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 351 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); |
| 293 if (!rwh) | 352 if (!rwh) |
| 294 return NULL; | 353 return NULL; |
| 295 if (!rwh->IsRenderView()) | 354 if (!rwh->IsRenderView()) |
| 296 return NULL; | 355 return NULL; |
| 297 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 356 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
| 298 } | 357 } |
| 299 | 358 |
| 300 } // namespace content | 359 } // namespace content |
| OLD | NEW |