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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // roundtrip back to java such synthetic event. | 133 // roundtrip back to java such synthetic event. |
132 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, | 134 NativeWebKeyboardEvent char_event(blink::WebInputEvent::Char, modifiers, |
133 time_ms / 1000.0, key_code, unicode_char, | 135 time_ms / 1000.0, key_code, unicode_char, |
134 is_system_key); | 136 is_system_key); |
135 char_event.skip_in_browser = key_down_text_insertion; | 137 char_event.skip_in_browser = key_down_text_insertion; |
136 rwhva_->SendKeyEvent(char_event); | 138 rwhva_->SendKeyEvent(char_event); |
137 } | 139 } |
138 return true; | 140 return true; |
139 } | 141 } |
140 | 142 |
141 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, jobject, jstring text, | 143 void ImeAdapterAndroid::AppendBackgroundColorSpan(JNIEnv*, |
| 144 jobject, |
| 145 long underlines_ptr, |
| 146 int start, |
| 147 int end, |
| 148 int background_color) { |
| 149 std::vector<blink::WebCompositionUnderline>* underlines = |
| 150 reinterpret_cast<std::vector<blink::WebCompositionUnderline>*>( |
| 151 underlines_ptr); |
| 152 underlines->push_back(blink::WebCompositionUnderline( |
| 153 start, end, SK_ColorBLACK, false, background_color)); |
| 154 } |
| 155 |
| 156 void ImeAdapterAndroid::SetComposingText(JNIEnv* env, |
| 157 jobject obj, |
| 158 jobject text, |
| 159 jstring text_str, |
142 int new_cursor_pos) { | 160 int new_cursor_pos) { |
143 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 161 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
144 if (!rwhi) | 162 if (!rwhi) |
145 return; | 163 return; |
146 | 164 |
147 base::string16 text16 = ConvertJavaStringToUTF16(env, text); | 165 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
| 166 |
148 std::vector<blink::WebCompositionUnderline> underlines; | 167 std::vector<blink::WebCompositionUnderline> underlines; |
149 underlines.push_back( | 168 // Iterate over spans in |text|, dispatch those that we care about (e.g., |
150 blink::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK, | 169 // BackgroundColorSpan) to a matching callback (e.g., |
151 false)); | 170 // AppendBackgroundColorSpan()), and populate |underlines|. |
| 171 Java_ImeAdapter_populateUnderlinesFromSpans(env, obj, text, |
| 172 reinterpret_cast<jlong>(&underlines)); |
| 173 |
| 174 // Default to plain underline if we didn't find any span that we care about. |
| 175 if (underlines.empty()) { |
| 176 underlines.push_back( |
| 177 blink::WebCompositionUnderline(0, text16.length(), SK_ColorBLACK, false, |
| 178 SK_ColorTRANSPARENT)); |
| 179 } |
| 180 // Sort spans by |.startOffset|. |
| 181 std::sort(underlines.begin(), underlines.end()); |
| 182 |
152 // new_cursor_position is as described in the Android API for | 183 // new_cursor_position is as described in the Android API for |
153 // InputConnection#setComposingText, whereas the parameters for | 184 // InputConnection#setComposingText, whereas the parameters for |
154 // ImeSetComposition are relative to the start of the composition. | 185 // ImeSetComposition are relative to the start of the composition. |
155 if (new_cursor_pos > 0) | 186 if (new_cursor_pos > 0) |
156 new_cursor_pos = text16.length() + new_cursor_pos - 1; | 187 new_cursor_pos = text16.length() + new_cursor_pos - 1; |
157 | 188 |
158 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); | 189 rwhi->ImeSetComposition(text16, underlines, new_cursor_pos, new_cursor_pos); |
159 } | 190 } |
160 | 191 |
161 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text) { | 192 void ImeAdapterAndroid::CommitText(JNIEnv* env, jobject, jstring text_str) { |
162 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 193 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
163 if (!rwhi) | 194 if (!rwhi) |
164 return; | 195 return; |
165 | 196 |
166 base::string16 text16 = ConvertJavaStringToUTF16(env, text); | 197 base::string16 text16 = ConvertJavaStringToUTF16(env, text_str); |
167 rwhi->ImeConfirmComposition(text16, gfx::Range::InvalidRange(), false); | 198 rwhi->ImeConfirmComposition(text16, gfx::Range::InvalidRange(), false); |
168 } | 199 } |
169 | 200 |
170 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, jobject) { | 201 void ImeAdapterAndroid::FinishComposingText(JNIEnv* env, jobject) { |
171 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); | 202 RenderWidgetHostImpl* rwhi = GetRenderWidgetHostImpl(); |
172 if (!rwhi) | 203 if (!rwhi) |
173 return; | 204 return; |
174 | 205 |
175 rwhi->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(), | 206 rwhi->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(), |
176 true); | 207 true); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } | 239 } |
209 | 240 |
210 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, | 241 void ImeAdapterAndroid::SetComposingRegion(JNIEnv*, jobject, |
211 int start, int end) { | 242 int start, int end) { |
212 RenderFrameHost* rfh = GetFocusedFrame(); | 243 RenderFrameHost* rfh = GetFocusedFrame(); |
213 if (!rfh) | 244 if (!rfh) |
214 return; | 245 return; |
215 | 246 |
216 std::vector<blink::WebCompositionUnderline> underlines; | 247 std::vector<blink::WebCompositionUnderline> underlines; |
217 underlines.push_back( | 248 underlines.push_back( |
218 blink::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false)); | 249 blink::WebCompositionUnderline(0, end - start, SK_ColorBLACK, false, |
| 250 SK_ColorTRANSPARENT)); |
219 | 251 |
220 rfh->Send(new FrameMsg_SetCompositionFromExistingText( | 252 rfh->Send(new FrameMsg_SetCompositionFromExistingText( |
221 rfh->GetRoutingID(), start, end, underlines)); | 253 rfh->GetRoutingID(), start, end, underlines)); |
222 } | 254 } |
223 | 255 |
224 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, | 256 void ImeAdapterAndroid::DeleteSurroundingText(JNIEnv*, jobject, |
225 int before, int after) { | 257 int before, int after) { |
226 RenderFrameHostImpl* rfh = | 258 RenderFrameHostImpl* rfh = |
227 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); | 259 static_cast<RenderFrameHostImpl*>(GetFocusedFrame()); |
228 if (rfh) | 260 if (rfh) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 WebContents* ImeAdapterAndroid::GetWebContents() { | 323 WebContents* ImeAdapterAndroid::GetWebContents() { |
292 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); | 324 RenderWidgetHostImpl* rwh = GetRenderWidgetHostImpl(); |
293 if (!rwh) | 325 if (!rwh) |
294 return NULL; | 326 return NULL; |
295 if (!rwh->IsRenderView()) | 327 if (!rwh->IsRenderView()) |
296 return NULL; | 328 return NULL; |
297 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); | 329 return WebContents::FromRenderViewHost(RenderViewHost::From(rwh)); |
298 } | 330 } |
299 | 331 |
300 } // namespace content | 332 } // namespace content |
OLD | NEW |