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

Side by Side Diff: ui/base/ime/input_method_chromeos.cc

Issue 1257603006: Refactoring for the InputMethod & InputMethodDelegate interfaces. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix test failures on windows. Created 5 years, 4 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "ui/base/ime/input_method_chromeos.h" 5 #include "ui/base/ime/input_method_chromeos.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstring> 8 #include <cstring>
9 #include <set> 9 #include <set>
10 #include <vector> 10 #include <vector>
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 InputMethodBase::OnBlur(); 64 InputMethodBase::OnBlur();
65 OnTextInputTypeChanged(GetTextInputClient()); 65 OnTextInputTypeChanged(GetTextInputClient());
66 } 66 }
67 67
68 bool InputMethodChromeOS::OnUntranslatedIMEMessage( 68 bool InputMethodChromeOS::OnUntranslatedIMEMessage(
69 const base::NativeEvent& event, 69 const base::NativeEvent& event,
70 NativeEventResult* result) { 70 NativeEventResult* result) {
71 return false; 71 return false;
72 } 72 }
73 73
74 void InputMethodChromeOS::ProcessKeyEventDone(const ui::KeyEvent* event, 74 void InputMethodChromeOS::ProcessKeyEventDone(ui::KeyEvent* event,
75 bool is_handled) { 75 bool is_handled) {
76 DCHECK(event); 76 DCHECK(event);
77 if (event->type() == ET_KEY_PRESSED) { 77 if (event->type() == ET_KEY_PRESSED) {
78 if (is_handled) { 78 if (is_handled) {
79 // IME event has a priority to be handled, so that character composer 79 // IME event has a priority to be handled, so that character composer
80 // should be reset. 80 // should be reset.
81 character_composer_.Reset(); 81 character_composer_.Reset();
82 } else { 82 } else {
83 // If IME does not handle key event, passes keyevent to character composer 83 // If IME does not handle key event, passes keyevent to character composer
84 // to be able to compose complex characters. 84 // to be able to compose complex characters.
85 is_handled = ExecuteCharacterComposer(*event); 85 is_handled = ExecuteCharacterComposer(*event);
86 } 86 }
87 } 87 }
88 88
89 if (event->type() == ET_KEY_PRESSED || event->type() == ET_KEY_RELEASED) 89 if (event->type() == ET_KEY_PRESSED || event->type() == ET_KEY_RELEASED)
90 ProcessKeyEventPostIME(*event, is_handled); 90 ProcessKeyEventPostIME(event, is_handled);
91 91
92 handling_key_event_ = false; 92 handling_key_event_ = false;
93 } 93 }
94 94
95 bool InputMethodChromeOS::DispatchKeyEvent(const ui::KeyEvent& event) { 95 void InputMethodChromeOS::DispatchKeyEvent(ui::KeyEvent* event) {
96 DCHECK(event.IsKeyEvent()); 96 DCHECK(event->IsKeyEvent());
97 DCHECK(!(event.flags() & ui::EF_IS_SYNTHESIZED)); 97 DCHECK(!(event->flags() & ui::EF_IS_SYNTHESIZED));
98 DCHECK(system_toplevel_window_focused()); 98 DCHECK(system_toplevel_window_focused());
99 99
100 // For linux_chromeos, the ime keyboard cannot track the caps lock state by 100 // For linux_chromeos, the ime keyboard cannot track the caps lock state by
101 // itself, so need to call SetCapsLockEnabled() method to reflect the caps 101 // itself, so need to call SetCapsLockEnabled() method to reflect the caps
102 // lock state by the key event. 102 // lock state by the key event.
103 if (!base::SysInfo::IsRunningOnChromeOS()) { 103 if (!base::SysInfo::IsRunningOnChromeOS()) {
104 chromeos::input_method::InputMethodManager* manager = 104 chromeos::input_method::InputMethodManager* manager =
105 chromeos::input_method::InputMethodManager::Get(); 105 chromeos::input_method::InputMethodManager::Get();
106 if (manager) { 106 if (manager) {
107 chromeos::input_method::ImeKeyboard* keyboard = manager->GetImeKeyboard(); 107 chromeos::input_method::ImeKeyboard* keyboard = manager->GetImeKeyboard();
108 if (keyboard && event.type() == ui::ET_KEY_PRESSED) { 108 if (keyboard && event->type() == ui::ET_KEY_PRESSED) {
109 bool caps = (event.key_code() == ui::VKEY_CAPITAL) 109 bool caps = (event->key_code() == ui::VKEY_CAPITAL)
110 ? !keyboard->CapsLockIsEnabled() 110 ? !keyboard->CapsLockIsEnabled()
111 : (event.flags() & EF_CAPS_LOCK_DOWN); 111 : (event->flags() & EF_CAPS_LOCK_DOWN);
112 keyboard->SetCapsLockEnabled(caps); 112 keyboard->SetCapsLockEnabled(caps);
113 } 113 }
114 } 114 }
115 } 115 }
116 116
117 // If |context_| is not usable, then we can only dispatch the key event as is. 117 // If |context_| is not usable, then we can only dispatch the key event as is.
118 // We only dispatch the key event to input method when the |context_| is an 118 // We only dispatch the key event to input method when the |context_| is an
119 // normal input field (not a password field). 119 // normal input field (not a password field).
120 // Note: We need to send the key event to ibus even if the |context_| is not 120 // Note: We need to send the key event to ibus even if the |context_| is not
121 // enabled, so that ibus can have a chance to enable the |context_|. 121 // enabled, so that ibus can have a chance to enable the |context_|.
122 if (!IsNonPasswordInputFieldFocused() || !GetEngine()) { 122 if (!IsNonPasswordInputFieldFocused() || !GetEngine()) {
123 if (event.type() == ET_KEY_PRESSED) { 123 if (event->type() == ET_KEY_PRESSED) {
124 if (ExecuteCharacterComposer(event)) { 124 if (ExecuteCharacterComposer(*event)) {
125 // Treating as PostIME event if character composer handles key event and 125 // Treating as PostIME event if character composer handles key event and
126 // generates some IME event, 126 // generates some IME event,
127 ProcessKeyEventPostIME(event, true); 127 ProcessKeyEventPostIME(event, true);
128 return true; 128 return;
129 } 129 }
130 ProcessUnfilteredKeyPressEvent(event); 130 ProcessUnfilteredKeyPressEvent(event);
131 } else { 131 } else {
132 DispatchKeyEventPostIME(event); 132 ignore_result(DispatchKeyEventPostIME(event));
133 } 133 }
134 return true; 134 return;
135 } 135 }
136 136
137 handling_key_event_ = true; 137 handling_key_event_ = true;
138 if (GetEngine()->IsInterestedInKeyEvent()) { 138 if (GetEngine()->IsInterestedInKeyEvent()) {
139 GetEngine()->ProcessKeyEvent( 139 GetEngine()->ProcessKeyEvent(
140 event, 140 *event,
141 base::Bind(&InputMethodChromeOS::ProcessKeyEventDone, 141 base::Bind(&InputMethodChromeOS::ProcessKeyEventDone,
142 weak_ptr_factory_.GetWeakPtr(), 142 weak_ptr_factory_.GetWeakPtr(),
143 // Pass the ownership of the new copied event. 143 // Pass the ownership of the new copied event.
144 base::Owned(new ui::KeyEvent(event)))); 144 base::Owned(new ui::KeyEvent(*event))));
145 } else { 145 } else {
146 ProcessKeyEventDone(&event, false); 146 ProcessKeyEventDone(event, false);
147 } 147 }
148 return true;
149 } 148 }
150 149
151 void InputMethodChromeOS::OnTextInputTypeChanged( 150 void InputMethodChromeOS::OnTextInputTypeChanged(
152 const TextInputClient* client) { 151 const TextInputClient* client) {
153 if (!IsTextInputClientFocused(client)) 152 if (!IsTextInputClientFocused(client))
154 return; 153 return;
155 154
156 UpdateContextFocusState(); 155 UpdateContextFocusState();
157 156
158 chromeos::IMEEngineHandlerInterface* engine = GetEngine(); 157 chromeos::IMEEngineHandlerInterface* engine = GetEngine();
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 candidate_window->FocusStateChanged(IsNonPasswordInputFieldFocused()); 328 candidate_window->FocusStateChanged(IsNonPasswordInputFieldFocused());
330 329
331 chromeos::IMEEngineHandlerInterface::InputContext context( 330 chromeos::IMEEngineHandlerInterface::InputContext context(
332 GetTextInputType(), GetTextInputMode(), GetTextInputFlags()); 331 GetTextInputType(), GetTextInputMode(), GetTextInputFlags());
333 chromeos::IMEBridge::Get()->SetCurrentInputContext(context); 332 chromeos::IMEBridge::Get()->SetCurrentInputContext(context);
334 333
335 if (!IsTextInputTypeNone()) 334 if (!IsTextInputTypeNone())
336 OnCaretBoundsChanged(GetTextInputClient()); 335 OnCaretBoundsChanged(GetTextInputClient());
337 } 336 }
338 337
339 void InputMethodChromeOS::ProcessKeyEventPostIME(const ui::KeyEvent& event, 338 void InputMethodChromeOS::ProcessKeyEventPostIME(ui::KeyEvent* event,
340 bool handled) { 339 bool handled) {
341 TextInputClient* client = GetTextInputClient(); 340 TextInputClient* client = GetTextInputClient();
342 if (!client) { 341 if (!client) {
343 // As ibus works asynchronously, there is a chance that the focused client 342 // As ibus works asynchronously, there is a chance that the focused client
344 // loses focus before this method gets called. 343 // loses focus before this method gets called.
345 DispatchKeyEventPostIME(event); 344 ignore_result(DispatchKeyEventPostIME(event));
346 return; 345 return;
347 } 346 }
348 347
349 if (event.type() == ET_KEY_PRESSED && handled) { 348 if (event->type() == ET_KEY_PRESSED && handled) {
350 if (ProcessFilteredKeyPressEvent(event)) { 349 ProcessFilteredKeyPressEvent(event);
James Su 2015/07/31 11:43:58 I think the result of DispatchKeyEventPostIME() ca
Shu Chen 2015/08/03 01:44:47 ChromeOS uses InputMethodEventHandler so destructi
350 if (event->stopped_propagation()) {
351 ResetContext(); 351 ResetContext();
352 return; 352 return;
353 } 353 }
354 } 354 }
355 355
356 // In case the focus was changed by the key event. The |context_| should have 356 // In case the focus was changed by the key event. The |context_| should have
357 // been reset when the focused window changed. 357 // been reset when the focused window changed.
358 if (client != GetTextInputClient()) 358 if (client != GetTextInputClient())
359 return; 359 return;
360 360
361 if (HasInputMethodResult()) 361 if (HasInputMethodResult())
362 ProcessInputMethodResult(event, handled); 362 ProcessInputMethodResult(event, handled);
363 363
364 // In case the focus was changed when sending input method results to the 364 // In case the focus was changed when sending input method results to the
365 // focused window. 365 // focused window.
366 if (client != GetTextInputClient()) 366 if (client != GetTextInputClient())
367 return; 367 return;
368 368
369 if (handled) 369 if (handled)
370 return; // IME handled the key event. do not forward. 370 return; // IME handled the key event. do not forward.
371 371
372 if (event.type() == ET_KEY_PRESSED) 372 if (event->type() == ET_KEY_PRESSED)
373 ProcessUnfilteredKeyPressEvent(event); 373 ProcessUnfilteredKeyPressEvent(event);
374 else if (event.type() == ET_KEY_RELEASED) 374 else if (event->type() == ET_KEY_RELEASED)
375 DispatchKeyEventPostIME(event); 375 ignore_result(DispatchKeyEventPostIME(event));
376 } 376 }
377 377
378 bool InputMethodChromeOS::ProcessFilteredKeyPressEvent( 378 void InputMethodChromeOS::ProcessFilteredKeyPressEvent(ui::KeyEvent* event) {
379 const ui::KeyEvent& event) { 379 if (NeedInsertChar()) {
380 if (NeedInsertChar()) 380 ignore_result(DispatchKeyEventPostIME(event));
381 return DispatchKeyEventPostIME(event); 381 return;
382 const ui::KeyEvent fabricated_event(ET_KEY_PRESSED, 382 }
383 VKEY_PROCESSKEY, 383 ui::KeyEvent fabricated_event(ET_KEY_PRESSED,
384 event.flags()); 384 VKEY_PROCESSKEY,
385 return DispatchKeyEventPostIME(fabricated_event); 385 event->flags());
386 ignore_result(DispatchKeyEventPostIME(&fabricated_event));
387 if (fabricated_event.stopped_propagation())
388 event->StopPropagation();
386 } 389 }
387 390
388 void InputMethodChromeOS::ProcessUnfilteredKeyPressEvent( 391 void InputMethodChromeOS::ProcessUnfilteredKeyPressEvent(
389 const ui::KeyEvent& event) { 392 ui::KeyEvent* event) {
390 const TextInputClient* prev_client = GetTextInputClient(); 393 const TextInputClient* prev_client = GetTextInputClient();
391 if (DispatchKeyEventPostIME(event)) { 394 ignore_result(DispatchKeyEventPostIME(event));
James Su 2015/07/31 11:43:58 I think this result shouldn't be ignored.
Shu Chen 2015/08/03 01:44:47 ditto.
395 if (event->stopped_propagation()) {
392 ResetContext(); 396 ResetContext();
393 return; 397 return;
394 } 398 }
395 399
396 // We shouldn't dispatch the character anymore if the key event dispatch 400 // We shouldn't dispatch the character anymore if the key event dispatch
397 // caused focus change. For example, in the following scenario, 401 // caused focus change. For example, in the following scenario,
398 // 1. visit a web page which has a <textarea>. 402 // 1. visit a web page which has a <textarea>.
399 // 2. click Omnibox. 403 // 2. click Omnibox.
400 // 3. enable Korean IME, press A, then press Tab to move the focus to the web 404 // 3. enable Korean IME, press A, then press Tab to move the focus to the web
401 // page. 405 // page.
402 // We should return here not to send the Tab key event to RWHV. 406 // We should return here not to send the Tab key event to RWHV.
403 TextInputClient* client = GetTextInputClient(); 407 TextInputClient* client = GetTextInputClient();
404 if (!client || client != prev_client) 408 if (!client || client != prev_client)
405 return; 409 return;
406 410
407 // If a key event was not filtered by |context_| and |character_composer_|, 411 // If a key event was not filtered by |context_| and |character_composer_|,
408 // then it means the key event didn't generate any result text. So we need 412 // then it means the key event didn't generate any result text. So we need
409 // to send corresponding character to the focused text input client. 413 // to send corresponding character to the focused text input client.
410 uint16 ch = event.GetCharacter(); 414 uint16 ch = event->GetCharacter();
411 if (ch) 415 if (ch)
412 client->InsertChar(ch, event.flags()); 416 client->InsertChar(ch, event->flags());
413 } 417 }
414 418
415 void InputMethodChromeOS::ProcessInputMethodResult(const ui::KeyEvent& event, 419 void InputMethodChromeOS::ProcessInputMethodResult(ui::KeyEvent* event,
416 bool handled) { 420 bool handled) {
417 TextInputClient* client = GetTextInputClient(); 421 TextInputClient* client = GetTextInputClient();
418 DCHECK(client); 422 DCHECK(client);
419 423
420 if (result_text_.length()) { 424 if (result_text_.length()) {
421 if (handled && NeedInsertChar()) { 425 if (handled && NeedInsertChar()) {
422 for (base::string16::const_iterator i = result_text_.begin(); 426 for (base::string16::const_iterator i = result_text_.begin();
423 i != result_text_.end(); ++i) { 427 i != result_text_.end(); ++i) {
424 client->InsertChar(*i, event.flags()); 428 client->InsertChar(*i, event->flags());
425 } 429 }
426 } else { 430 } else {
427 client->InsertText(result_text_); 431 client->InsertText(result_text_);
428 composing_text_ = false; 432 composing_text_ = false;
429 } 433 }
430 } 434 }
431 435
432 if (composition_changed_ && !IsTextInputTypeNone()) { 436 if (composition_changed_ && !IsTextInputTypeNone()) {
433 if (composition_.text.length()) { 437 if (composition_.text.length()) {
434 composing_text_ = true; 438 composing_text_ = true;
(...skipping 18 matching lines...) Expand all
453 bool InputMethodChromeOS::HasInputMethodResult() const { 457 bool InputMethodChromeOS::HasInputMethodResult() const {
454 return result_text_.length() || composition_changed_; 458 return result_text_.length() || composition_changed_;
455 } 459 }
456 460
457 void InputMethodChromeOS::SendFakeProcessKeyEvent(bool pressed) const { 461 void InputMethodChromeOS::SendFakeProcessKeyEvent(bool pressed) const {
458 if (!GetTextInputClient()) 462 if (!GetTextInputClient())
459 return; 463 return;
460 KeyEvent evt(pressed ? ET_KEY_PRESSED : ET_KEY_RELEASED, 464 KeyEvent evt(pressed ? ET_KEY_PRESSED : ET_KEY_RELEASED,
461 pressed ? VKEY_PROCESSKEY : VKEY_UNKNOWN, 465 pressed ? VKEY_PROCESSKEY : VKEY_UNKNOWN,
462 EF_IME_FABRICATED_KEY); 466 EF_IME_FABRICATED_KEY);
463 DispatchKeyEventPostIME(evt); 467 ignore_result(DispatchKeyEventPostIME(&evt));
James Su 2015/07/31 11:43:58 I think this result shouldn't be ignored, and evt.
Shu Chen 2015/08/03 01:44:47 ditto & Done.
464 } 468 }
465 469
466 void InputMethodChromeOS::CommitText(const std::string& text) { 470 void InputMethodChromeOS::CommitText(const std::string& text) {
467 if (text.empty()) 471 if (text.empty())
468 return; 472 return;
469 473
470 // We need to receive input method result even if the text input type is 474 // We need to receive input method result even if the text input type is
471 // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct 475 // TEXT_INPUT_TYPE_NONE, to make sure we can always send correct
472 // character for each key event to the focused text input client. 476 // character for each key event to the focused text input client.
473 if (!GetTextInputClient()) 477 if (!GetTextInputClient())
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 bool InputMethodChromeOS::IsNonPasswordInputFieldFocused() { 674 bool InputMethodChromeOS::IsNonPasswordInputFieldFocused() {
671 TextInputType type = GetTextInputType(); 675 TextInputType type = GetTextInputType();
672 return (type != TEXT_INPUT_TYPE_NONE) && (type != TEXT_INPUT_TYPE_PASSWORD); 676 return (type != TEXT_INPUT_TYPE_NONE) && (type != TEXT_INPUT_TYPE_PASSWORD);
673 } 677 }
674 678
675 bool InputMethodChromeOS::IsInputFieldFocused() { 679 bool InputMethodChromeOS::IsInputFieldFocused() {
676 return GetTextInputType() != TEXT_INPUT_TYPE_NONE; 680 return GetTextInputType() != TEXT_INPUT_TYPE_NONE;
677 } 681 }
678 682
679 } // namespace ui 683 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698