Chromium Code Reviews| 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 void AppendBackgroundColorSpan(JNIEnv*, | |
|
aurimas (slooooooooow)
2014/06/10 18:36:18
Please add comments explaining what these function
huangs
2014/06/10 19:54:53
Done.
| |
| 93 jclass, | |
| 94 jlong underlines_ptr, | |
| 95 jint start, | |
| 96 jint end, | |
| 97 jint background_color) { | |
| 98 std::vector<blink::WebCompositionUnderline>* underlines = | |
| 99 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | |
| 100 underlines_ptr); | |
| 101 underlines->push_back(blink::WebCompositionUnderline( | |
| 102 start, end, SK_ColorTRANSPARENT, false, background_color)); | |
| 103 } | |
| 104 | |
| 105 void AppendUnderlineSpan(JNIEnv*, | |
| 106 jclass, | |
| 107 jlong underlines_ptr, | |
| 108 jint start, | |
| 109 jint end) { | |
| 110 std::vector<blink::WebCompositionUnderline>* underlines = | |
| 111 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( | |
| 112 underlines_ptr); | |
| 113 underlines->push_back(blink::WebCompositionUnderline( | |
| 114 start, end, SK_ColorBLACK, false, SK_ColorTRANSPARENT)); | |
| 115 } | |
| 116 | |
| 90 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva) | 117 ImeAdapterAndroid::ImeAdapterAndroid(RenderWidgetHostViewAndroid* rwhva) |
| 91 : rwhva_(rwhva) { | 118 : rwhva_(rwhva) { |
| 92 } | 119 } |
| 93 | 120 |
| 94 ImeAdapterAndroid::~ImeAdapterAndroid() { | 121 ImeAdapterAndroid::~ImeAdapterAndroid() { |
| 95 JNIEnv* env = AttachCurrentThread(); | 122 JNIEnv* env = AttachCurrentThread(); |
| 96 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); | 123 base::android::ScopedJavaLocalRef<jobject> obj = java_ime_adapter_.get(env); |
| 97 if (!obj.is_null()) | 124 if (!obj.is_null()) |
| 98 Java_ImeAdapter_detach(env, obj.obj()); | 125 Java_ImeAdapter_detach(env, obj.obj()); |
| 99 } | 126 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 // roundtrip back to java such synthetic event. | 158 // roundtrip back to java such synthetic event. |
| 132 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, | 159 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, |
| 133 time_ms / 1000.0, key_code, unicode_char, | 160 time_ms / 1000.0, key_code, unicode_char, |
| 134 is_system_key); | 161 is_system_key); |
| 135 char_event.skip_in_browser = key_down_text_insertion; | 162 char_event.skip_in_browser = key_down_text_insertion; |
| 136 rwhva_->SendKeyEvent(char_event); | 163 rwhva_->SendKeyEvent(char_event); |
| 137 } | 164 } |
| 138 return true; | 165 return true; |
| 139 } | 166 } |
| 140 | 167 |
| 141 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, jobject, jstring text, | 168 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
| 169 jobject obj, | |
| 170 jobject text, | |
| 171 jstring text_str, | |
| 142 int new_cursor_pos) { | 172 int new_cursor_pos) { |
| 143 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 173 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 144 if (!rwhi) | 174 if (!rwhi) |
| 145 return; | 175 return; |
| 146 | 176 |
| 147 base::string16 text16 = ConvertJavaStringToUTF16(env, text); | 177 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 178 | |
| 148 std::vector<blink::WebCompositionUnderline> underlines; | 179 std::vector<blink::WebCompositionUnderline> underlines; |
| 149 underlines.push_back( | 180 // Iterate over spans in |text|, dispatch those that we care about (e.g., |
| 150 blink::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK, | 181 // BackgroundColorSpan) to a matching callback (e.g., |
| 151 false)); | 182 // AppendBackgroundColorSpan()), and populate |underlines|. |
| 183 Java_ImeAdapter_populateUnderlinesFromSpans(env, obj, text, | |
| 184 reinterpret_cast<jlong>(&underlines)); | |
| 185 | |
| 186 // Default to plain underline if we didn't find any span that we care about. | |
| 187 if (underlines.empty()) { | |
| 188 underlines.push_back( | |
| 189 blink::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK, false, | |
| 190 SK_ColorTRANSPARENT)); | |
| 191 } | |
| 192 // Sort spans by |.startOffset|. | |
| 193 std::sort(underlines.begin(), underlines.end()); | |
| 194 | |
| 152 // new_cursor_position is as described in the Android API for | 195 // new_cursor_position is as described in the Android API for |
| 153 // InputConnection#setComposingText, whereas the parameters for | 196 // InputConnection#setComposingText, whereas the parameters for |
| 154 // ImeSetComposition are relative to the start of the composition. | 197 // ImeSetComposition are relative to the start of the composition. |
| 155 if (new_cursor_pos > 0) | 198 if (new_cursor_pos > 0) |
| 156 new_cursor_pos = text16.length() + new_cursor_pos - 1; | 199 new_cursor_pos = text16.length() + new_cursor_pos - 1; |
| 157 | 200 |
| 158 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); | 201 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); |
| 159 } | 202 } |
| 160 | 203 |
| 161 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text) { | 204 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text_str) { |
| 162 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 205 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 163 if (!rwhi) | 206 if (!rwhi) |
| 164 return; | 207 return; |
| 165 | 208 |
| 166 base::string16 text16 = ConvertJavaStringToUTF16(env, text); | 209 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 167 rwhi->ImeConfirmComposition(text16, gfx::Range::InvalidRange(), false); | 210 rwhi->ImeConfirmComposition(text16, gfx::Range::InvalidRange(), false); |
| 168 } | 211 } |
| 169 | 212 |
| 170 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, jobject) { | 213 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, jobject) { |
| 171 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 214 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
| 172 if (!rwhi) | 215 if (!rwhi) |
| 173 return; | 216 return; |
| 174 | 217 |
| 175 rwhi->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(), | 218 rwhi->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(), |
| 176 true); | 219 true); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 } | 251 } |
| 209 | 252 |
| 210 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, | 253 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, |
| 211 int start, int end) { | 254 int start, int end) { |
| 212 RenderFrameHost* rfh = GetFocusedFrame(); | 255 RenderFrameHost* rfh = GetFocusedFrame(); |
| 213 if (!rfh) | 256 if (!rfh) |
| 214 return; | 257 return; |
| 215 | 258 |
| 216 std::vector<blink::WebCompositionUnderline> underlines; | 259 std::vector<blink::WebCompositionUnderline> underlines; |
| 217 underlines.push_back( | 260 underlines.push_back( |
| 218 blink::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false)); | 261 blink::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false, |
| 262 SK_ColorTRANSPARENT)); | |
| 219 | 263 |
| 220 rfh->Send(new FrameMsg_SetCompositionFromExistingText( | 264 rfh->Send(new FrameMsg_SetCompositionFromExistingText( |
| 221 rfh->GetRoutingID(), start, end, underlines)); | 265 rfh->GetRoutingID(), start, end, underlines)); |
| 222 } | 266 } |
| 223 | 267 |
| 224 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, | 268 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, |
| 225 int before, int after) { | 269 int before, int after) { |
| 226 RenderFrameHostImpl* rfh = | 270 RenderFrameHostImpl* rfh = |
| 227 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | 271 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); |
| 228 if (rfh) | 272 if (rfh) |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 WebContents* ImeAdapterAndroid::GetWebContents() { | 335 WebContents* ImeAdapterAndroid::GetWebContents() { |
| 292 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 336 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); |
| 293 if (!rwh) | 337 if (!rwh) |
| 294 return NULL; | 338 return NULL; |
| 295 if (!rwh->IsRenderView()) | 339 if (!rwh->IsRenderView()) |
| 296 return NULL; | 340 return NULL; |
| 297 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 341 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
| 298 } | 342 } |
| 299 | 343 |
| 300 } // namespace content | 344 } // namespace content |
| OLD | NEW |