OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/base/ime/remote_input_method_win.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include "base/command_line.h" | |
11 #include "base/macros.h" | |
12 #include "base/observer_list.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "base/win/scoped_handle.h" | |
15 #include "ui/base/ime/input_method.h" | |
16 #include "ui/base/ime/input_method_delegate.h" | |
17 #include "ui/base/ime/input_method_observer.h" | |
18 #include "ui/base/ime/remote_input_method_delegate_win.h" | |
19 #include "ui/base/ime/text_input_client.h" | |
20 #include "ui/base/ime/win/tsf_input_scope.h" | |
21 #include "ui/base/ui_base_switches.h" | |
22 #include "ui/events/event.h" | |
23 #include "ui/events/event_utils.h" | |
24 #include "ui/gfx/geometry/rect.h" | |
25 | |
26 namespace ui { | |
27 namespace { | |
28 | |
29 const LANGID kFallbackLangID = | |
30 MAKELANGID(LANG_NEUTRAL, SUBLANG_UI_CUSTOM_DEFAULT); | |
31 | |
32 InputMethod* g_public_interface_ = NULL; | |
33 RemoteInputMethodPrivateWin* g_private_interface_ = NULL; | |
34 | |
35 void RegisterInstance(InputMethod* public_interface, | |
36 RemoteInputMethodPrivateWin* private_interface) { | |
37 CHECK(g_public_interface_ == NULL) | |
38 << "Only one instance is supported at the same time"; | |
39 CHECK(g_private_interface_ == NULL) | |
40 << "Only one instance is supported at the same time"; | |
41 g_public_interface_ = public_interface; | |
42 g_private_interface_ = private_interface; | |
43 } | |
44 | |
45 RemoteInputMethodPrivateWin* GetPrivate(InputMethod* public_interface) { | |
46 if (g_public_interface_ != public_interface) | |
47 return NULL; | |
48 return g_private_interface_; | |
49 } | |
50 | |
51 void UnregisterInstance(InputMethod* public_interface) { | |
52 RemoteInputMethodPrivateWin* private_interface = GetPrivate(public_interface); | |
53 if (g_public_interface_ == public_interface && | |
54 g_private_interface_ == private_interface) { | |
55 g_public_interface_ = NULL; | |
56 g_private_interface_ = NULL; | |
57 } | |
58 } | |
59 | |
60 std::string GetLocaleString(LCID Locale_id, LCTYPE locale_type) { | |
61 wchar_t buffer[16] = {}; | |
62 | |
63 //|chars_written| includes NUL terminator. | |
64 const int chars_written = | |
65 GetLocaleInfo(Locale_id, locale_type, buffer, arraysize(buffer)); | |
66 if (chars_written <= 1 || static_cast<int>(arraysize(buffer)) < chars_written) | |
67 return std::string(); | |
68 std::string result; | |
69 base::WideToUTF8(buffer, chars_written - 1, &result); | |
70 return result; | |
71 } | |
72 | |
73 std::vector<int32_t> GetInputScopesAsInt(TextInputType text_input_type, | |
74 TextInputMode text_input_mode) { | |
75 std::vector<int32_t> result; | |
76 // An empty vector represents |text_input_type| is TEXT_INPUT_TYPE_NONE. | |
77 if (text_input_type == TEXT_INPUT_TYPE_NONE) | |
78 return result; | |
79 | |
80 const std::vector<InputScope>& input_scopes = | |
81 tsf_inputscope::GetInputScopes(text_input_type, text_input_mode); | |
82 result.reserve(input_scopes.size()); | |
83 for (size_t i = 0; i < input_scopes.size(); ++i) | |
84 result.push_back(static_cast<int32_t>(input_scopes[i])); | |
85 return result; | |
86 } | |
87 | |
88 std::vector<gfx::Rect> GetCompositionCharacterBounds( | |
89 const TextInputClient* client) { | |
90 if (!client) | |
91 return std::vector<gfx::Rect>(); | |
92 | |
93 std::vector<gfx::Rect> bounds; | |
94 if (client->HasCompositionText()) { | |
95 gfx::Range range; | |
96 if (client->GetCompositionTextRange(&range)) { | |
97 for (uint32_t i = 0; i < range.length(); ++i) { | |
98 gfx::Rect rect; | |
99 if (!client->GetCompositionCharacterBounds(i, &rect)) | |
100 break; | |
101 bounds.push_back(rect); | |
102 } | |
103 } | |
104 } | |
105 | |
106 // Use the caret bounds as a fallback if no composition character bounds is | |
107 // available. One typical use case is PPAPI Flash, which does not support | |
108 // GetCompositionCharacterBounds at all. crbug.com/133472 | |
109 if (bounds.empty()) | |
110 bounds.push_back(client->GetCaretBounds()); | |
111 return bounds; | |
112 } | |
113 | |
114 class RemoteInputMethodWin : public InputMethod, | |
115 public RemoteInputMethodPrivateWin { | |
116 public: | |
117 explicit RemoteInputMethodWin(internal::InputMethodDelegate* delegate) | |
118 : delegate_(delegate), | |
119 remote_delegate_(NULL), | |
120 text_input_client_(NULL), | |
121 is_candidate_popup_open_(false), | |
122 is_ime_(false), | |
123 langid_(kFallbackLangID) { | |
124 RegisterInstance(this, this); | |
125 } | |
126 | |
127 ~RemoteInputMethodWin() override { | |
128 FOR_EACH_OBSERVER(InputMethodObserver, | |
129 observer_list_, | |
130 OnInputMethodDestroyed(this)); | |
131 UnregisterInstance(this); | |
132 } | |
133 | |
134 private: | |
135 // Overridden from InputMethod: | |
136 void SetDelegate(internal::InputMethodDelegate* delegate) override { | |
137 delegate_ = delegate; | |
138 } | |
139 | |
140 void OnFocus() override {} | |
141 | |
142 void OnBlur() override {} | |
143 | |
144 bool OnUntranslatedIMEMessage(const base::NativeEvent& event, | |
145 NativeEventResult* result) override { | |
146 return false; | |
147 } | |
148 | |
149 void SetFocusedTextInputClient(TextInputClient* client) override { | |
150 std::vector<int32_t> prev_input_scopes; | |
151 std::swap(input_scopes_, prev_input_scopes); | |
152 std::vector<gfx::Rect> prev_bounds; | |
153 std::swap(composition_character_bounds_, prev_bounds); | |
154 if (client) { | |
155 input_scopes_ = GetInputScopesAsInt(client->GetTextInputType(), | |
156 client->GetTextInputMode()); | |
157 composition_character_bounds_ = GetCompositionCharacterBounds(client); | |
158 } | |
159 | |
160 const bool text_input_client_changed = text_input_client_ != client; | |
161 text_input_client_ = client; | |
162 if (text_input_client_changed) { | |
163 FOR_EACH_OBSERVER(InputMethodObserver, | |
164 observer_list_, | |
165 OnTextInputStateChanged(client)); | |
166 } | |
167 | |
168 if (!remote_delegate_ || (prev_input_scopes == input_scopes_ && | |
169 prev_bounds == composition_character_bounds_)) | |
170 return; | |
171 remote_delegate_->OnTextInputClientUpdated(input_scopes_, | |
172 composition_character_bounds_); | |
173 } | |
174 | |
175 void DetachTextInputClient(TextInputClient* client) override { | |
176 if (text_input_client_ != client) | |
177 return; | |
178 SetFocusedTextInputClient(NULL); | |
179 } | |
180 | |
181 TextInputClient* GetTextInputClient() const override { | |
182 return text_input_client_; | |
183 } | |
184 | |
185 void DispatchKeyEvent(ui::KeyEvent* event) override { | |
186 if (event->HasNativeEvent()) { | |
187 const base::NativeEvent& native_key_event = event->native_event(); | |
188 if (native_key_event.message == WM_CHAR && text_input_client_) { | |
189 text_input_client_->InsertChar(*event); | |
190 event->StopPropagation(); | |
191 } | |
192 return; | |
193 } | |
194 | |
195 if (event->is_char()) { | |
196 if (text_input_client_) { | |
197 text_input_client_->InsertChar(*event); | |
198 } | |
199 event->StopPropagation(); | |
200 return; | |
201 } | |
202 if (delegate_) | |
203 ignore_result(delegate_->DispatchKeyEventPostIME(event)); | |
204 } | |
205 | |
206 void OnTextInputTypeChanged(const TextInputClient* client) override { | |
207 if (!text_input_client_ || text_input_client_ != client) | |
208 return; | |
209 std::vector<int32_t> prev_input_scopes; | |
210 std::swap(input_scopes_, prev_input_scopes); | |
211 input_scopes_ = GetInputScopesAsInt(client->GetTextInputType(), | |
212 client->GetTextInputMode()); | |
213 if (input_scopes_ != prev_input_scopes && remote_delegate_) { | |
214 remote_delegate_->OnTextInputClientUpdated( | |
215 input_scopes_, composition_character_bounds_); | |
216 } | |
217 } | |
218 | |
219 void OnCaretBoundsChanged(const TextInputClient* client) override { | |
220 if (!text_input_client_ || text_input_client_ != client) | |
221 return; | |
222 std::vector<gfx::Rect> prev_rects; | |
223 std::swap(composition_character_bounds_, prev_rects); | |
224 composition_character_bounds_ = GetCompositionCharacterBounds(client); | |
225 if (composition_character_bounds_ != prev_rects && remote_delegate_) { | |
226 remote_delegate_->OnTextInputClientUpdated( | |
227 input_scopes_, composition_character_bounds_); | |
228 } | |
229 } | |
230 | |
231 void CancelComposition(const TextInputClient* client) override { | |
232 if (CanSendRemoteNotification(client)) | |
233 remote_delegate_->CancelComposition(); | |
234 } | |
235 | |
236 void OnInputLocaleChanged() override {} | |
237 | |
238 std::string GetInputLocale() override { | |
239 const LCID locale_id = MAKELCID(langid_, SORT_DEFAULT); | |
240 std::string language = | |
241 GetLocaleString(locale_id, LOCALE_SISO639LANGNAME); | |
242 if (SUBLANGID(langid_) == SUBLANG_NEUTRAL || language.empty()) | |
243 return language; | |
244 const std::string& region = | |
245 GetLocaleString(locale_id, LOCALE_SISO3166CTRYNAME); | |
246 if (region.empty()) | |
247 return language; | |
248 return language.append(1, '-').append(region); | |
249 } | |
250 | |
251 TextInputType GetTextInputType() const override { | |
252 return text_input_client_ ? text_input_client_->GetTextInputType() | |
253 : TEXT_INPUT_TYPE_NONE; | |
254 } | |
255 | |
256 TextInputMode GetTextInputMode() const override { | |
257 return text_input_client_ ? text_input_client_->GetTextInputMode() | |
258 : TEXT_INPUT_MODE_DEFAULT; | |
259 } | |
260 | |
261 int GetTextInputFlags() const override { | |
262 return text_input_client_ ? text_input_client_->GetTextInputFlags() | |
263 : 0; | |
264 } | |
265 | |
266 bool CanComposeInline() const override { | |
267 return text_input_client_ ? text_input_client_->CanComposeInline() : true; | |
268 } | |
269 | |
270 bool IsCandidatePopupOpen() const override { | |
271 return is_candidate_popup_open_; | |
272 } | |
273 | |
274 void ShowImeIfNeeded() override {} | |
275 | |
276 void AddObserver(InputMethodObserver* observer) override { | |
277 observer_list_.AddObserver(observer); | |
278 } | |
279 | |
280 void RemoveObserver(InputMethodObserver* observer) override { | |
281 observer_list_.RemoveObserver(observer); | |
282 } | |
283 | |
284 // Overridden from RemoteInputMethodPrivateWin: | |
285 void SetRemoteDelegate( | |
286 internal::RemoteInputMethodDelegateWin* delegate) override { | |
287 remote_delegate_ = delegate; | |
288 | |
289 // Sync initial state. | |
290 if (remote_delegate_) { | |
291 remote_delegate_->OnTextInputClientUpdated( | |
292 input_scopes_, composition_character_bounds_); | |
293 } | |
294 } | |
295 | |
296 void OnCandidatePopupChanged(bool visible) override { | |
297 is_candidate_popup_open_ = visible; | |
298 } | |
299 | |
300 void OnInputSourceChanged(LANGID langid, bool /*is_ime*/) override { | |
301 // Note: Currently |is_ime| is not utilized yet. | |
302 const bool changed = (langid_ != langid); | |
303 langid_ = langid; | |
304 if (changed && GetTextInputClient()) | |
305 GetTextInputClient()->OnInputMethodChanged(); | |
306 } | |
307 | |
308 void OnCompositionChanged(const CompositionText& composition_text) override { | |
309 if (!text_input_client_) | |
310 return; | |
311 text_input_client_->SetCompositionText(composition_text); | |
312 } | |
313 | |
314 void OnTextCommitted(const base::string16& text) override { | |
315 if (!text_input_client_) | |
316 return; | |
317 if (text_input_client_->GetTextInputType() == TEXT_INPUT_TYPE_NONE) { | |
318 // According to the comment in text_input_client.h, | |
319 // TextInputClient::InsertText should never be called when the | |
320 // text input type is TEXT_INPUT_TYPE_NONE. | |
321 | |
322 for (size_t i = 0; i < text.size(); ++i) { | |
323 ui::KeyEvent char_event(text[i], static_cast<ui::KeyboardCode>(text[i]), | |
324 ui::EF_NONE); | |
325 text_input_client_->InsertChar(char_event); | |
326 } | |
327 return; | |
328 } | |
329 text_input_client_->InsertText(text); | |
330 } | |
331 | |
332 bool CanSendRemoteNotification( | |
333 const TextInputClient* text_input_client) const { | |
334 return text_input_client_ && | |
335 text_input_client_ == text_input_client && | |
336 remote_delegate_; | |
337 } | |
338 | |
339 base::ObserverList<InputMethodObserver> observer_list_; | |
340 | |
341 internal::InputMethodDelegate* delegate_; | |
342 internal::RemoteInputMethodDelegateWin* remote_delegate_; | |
343 | |
344 TextInputClient* text_input_client_; | |
345 std::vector<int32_t> input_scopes_; | |
346 std::vector<gfx::Rect> composition_character_bounds_; | |
347 bool is_candidate_popup_open_; | |
348 bool is_ime_; | |
349 LANGID langid_; | |
350 | |
351 DISALLOW_COPY_AND_ASSIGN(RemoteInputMethodWin); | |
352 }; | |
353 | |
354 } // namespace | |
355 | |
356 bool IsRemoteInputMethodWinRequired(gfx::AcceleratedWidget widget) { | |
357 // If the remote input method is already registered then don't do it again. | |
358 if (ui::g_public_interface_ && ui::g_private_interface_) | |
359 return false; | |
360 | |
361 DWORD process_id = 0; | |
362 if (GetWindowThreadProcessId(widget, &process_id) == 0) | |
363 return false; | |
364 base::win::ScopedHandle process_handle(::OpenProcess( | |
365 PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id)); | |
366 if (!process_handle.IsValid()) | |
367 return false; | |
368 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
369 switches::kViewerConnect); | |
370 } | |
371 | |
372 RemoteInputMethodPrivateWin::RemoteInputMethodPrivateWin() {} | |
373 | |
374 scoped_ptr<InputMethod> CreateRemoteInputMethodWin( | |
375 internal::InputMethodDelegate* delegate) { | |
376 return make_scoped_ptr(new RemoteInputMethodWin(delegate)); | |
377 } | |
378 | |
379 // static | |
380 RemoteInputMethodPrivateWin* RemoteInputMethodPrivateWin::Get( | |
381 InputMethod* input_method) { | |
382 return GetPrivate(input_method); | |
383 } | |
384 | |
385 } // namespace ui | |
OLD | NEW |