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

Side by Side Diff: chrome/browser/chromeos/input_method/input_method_engine.cc

Issue 1562733002: Make InputMethodEngine to inherit from new added class InputMethodEngineBase. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove function CheckProfile(). Created 4 years, 11 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 2013 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/chromeos/input_method/input_method_engine.h" 5 #include "chrome/browser/chromeos/input_method/input_method_engine.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #undef FocusIn 9 #undef FocusIn
10 #undef FocusOut 10 #undef FocusOut
(...skipping 30 matching lines...) Expand all
41 #include "ui/keyboard/keyboard_util.h" 41 #include "ui/keyboard/keyboard_util.h"
42 42
43 namespace chromeos { 43 namespace chromeos {
44 44
45 namespace { 45 namespace {
46 46
47 const char kErrorNotActive[] = "IME is not active"; 47 const char kErrorNotActive[] = "IME is not active";
48 const char kErrorWrongContext[] = "Context is not active"; 48 const char kErrorWrongContext[] = "Context is not active";
49 const char kCandidateNotFound[] = "Candidate not found"; 49 const char kCandidateNotFound[] = "Candidate not found";
50 50
51 // Notifies InputContextHandler that the composition is changed.
52 void UpdateComposition(const ui::CompositionText& composition_text,
53 uint32_t cursor_pos,
54 bool is_visible) {
55 ui::IMEInputContextHandlerInterface* input_context =
56 ui::IMEBridge::Get()->GetInputContextHandler();
57 if (input_context)
58 input_context->UpdateCompositionText(composition_text, cursor_pos,
59 is_visible);
60 }
61
62 // Returns the length of characters of a UTF-8 string with unknown string
63 // length. Cannot apply faster algorithm to count characters in an utf-8
64 // string without knowing the string length, so just does a full scan.
65 size_t GetUtf8StringLength(const char* s) {
66 size_t ret = 0;
67 while (*s) {
68 if ((*s & 0xC0) != 0x80)
69 ret++;
70 ++s;
71 }
72 return ret;
73 }
74
75 std::string GetKeyFromEvent(const ui::KeyEvent& event) {
76 const std::string code = event.GetCodeString();
77 if (base::StartsWith(code, "Control", base::CompareCase::SENSITIVE))
78 return "Ctrl";
79 if (base::StartsWith(code, "Shift", base::CompareCase::SENSITIVE))
80 return "Shift";
81 if (base::StartsWith(code, "Alt", base::CompareCase::SENSITIVE))
82 return "Alt";
83 if (base::StartsWith(code, "Arrow", base::CompareCase::SENSITIVE))
84 return code.substr(5);
85 if (code == "Escape")
86 return "Esc";
87 if (code == "Backspace" || code == "Tab" || code == "Enter" ||
88 code == "CapsLock" || code == "Power")
89 return code;
90 // Cases for media keys.
91 switch (event.key_code()) {
92 case ui::VKEY_BROWSER_BACK:
93 case ui::VKEY_F1:
94 return "HistoryBack";
95 case ui::VKEY_BROWSER_FORWARD:
96 case ui::VKEY_F2:
97 return "HistoryForward";
98 case ui::VKEY_BROWSER_REFRESH:
99 case ui::VKEY_F3:
100 return "BrowserRefresh";
101 case ui::VKEY_MEDIA_LAUNCH_APP2:
102 case ui::VKEY_F4:
103 return "ChromeOSFullscreen";
104 case ui::VKEY_MEDIA_LAUNCH_APP1:
105 case ui::VKEY_F5:
106 return "ChromeOSSwitchWindow";
107 case ui::VKEY_BRIGHTNESS_DOWN:
108 case ui::VKEY_F6:
109 return "BrightnessDown";
110 case ui::VKEY_BRIGHTNESS_UP:
111 case ui::VKEY_F7:
112 return "BrightnessUp";
113 case ui::VKEY_VOLUME_MUTE:
114 case ui::VKEY_F8:
115 return "AudioVolumeMute";
116 case ui::VKEY_VOLUME_DOWN:
117 case ui::VKEY_F9:
118 return "AudioVolumeDown";
119 case ui::VKEY_VOLUME_UP:
120 case ui::VKEY_F10:
121 return "AudioVolumeUp";
122 default:
123 break;
124 }
125 uint16_t ch = 0;
126 // Ctrl+? cases, gets key value for Ctrl is not down.
127 if (event.flags() & ui::EF_CONTROL_DOWN) {
128 ui::KeyEvent event_no_ctrl(event.type(), event.key_code(),
129 event.flags() ^ ui::EF_CONTROL_DOWN);
130 ch = event_no_ctrl.GetCharacter();
131 } else {
132 ch = event.GetCharacter();
133 }
134 return base::UTF16ToUTF8(base::string16(1, ch));
135 }
136
137 void GetExtensionKeyboardEventFromKeyEvent(
138 const ui::KeyEvent& event,
139 InputMethodEngine::KeyboardEvent* ext_event) {
140 DCHECK(event.type() == ui::ET_KEY_RELEASED ||
141 event.type() == ui::ET_KEY_PRESSED);
142 DCHECK(ext_event);
143 ext_event->type = (event.type() == ui::ET_KEY_RELEASED) ? "keyup" : "keydown";
144
145 if (event.code() == ui::DomCode::NONE)
146 ext_event->code = ui::KeyboardCodeToDomKeycode(event.key_code());
147 else
148 ext_event->code = event.GetCodeString();
149 ext_event->key_code = static_cast<int>(event.key_code());
150 ext_event->alt_key = event.IsAltDown();
151 ext_event->ctrl_key = event.IsControlDown();
152 ext_event->shift_key = event.IsShiftDown();
153 ext_event->caps_lock = event.IsCapsLockOn();
154 ext_event->key = GetKeyFromEvent(event);
155 }
156
157 } // namespace 51 } // namespace
158 52
159 InputMethodEngine::InputMethodEngine() 53 InputMethodEngine::InputMethodEngine()
160 : current_input_type_(ui::TEXT_INPUT_TYPE_NONE), 54 : candidate_window_(new ui::CandidateWindow()), window_visible_(false) {}
161 context_id_(0),
162 next_context_id_(1),
163 composition_text_(new ui::CompositionText()),
164 composition_cursor_(0),
165 candidate_window_(new ui::CandidateWindow()),
166 window_visible_(false),
167 sent_key_event_(NULL),
168 profile_(NULL) {}
169 55
170 InputMethodEngine::~InputMethodEngine() {} 56 InputMethodEngine::~InputMethodEngine() {}
171 57
172 void InputMethodEngine::Initialize(scoped_ptr<ui::IMEEngineObserver> observer,
173 const char* extension_id,
174 Profile* profile) {
175 DCHECK(observer) << "Observer must not be null.";
176
177 // TODO(komatsu): It is probably better to set observer out of Initialize.
178 observer_ = std::move(observer);
179 extension_id_ = extension_id;
180 profile_ = profile;
181 }
182
183 const std::string& InputMethodEngine::GetActiveComponentId() const {
184 return active_component_id_;
185 }
186
187 bool InputMethodEngine::SetComposition(
188 int context_id,
189 const char* text,
190 int selection_start,
191 int selection_end,
192 int cursor,
193 const std::vector<SegmentInfo>& segments,
194 std::string* error) {
195 if (!IsActive()) {
196 *error = kErrorNotActive;
197 return false;
198 }
199 if (context_id != context_id_ || context_id_ == -1) {
200 *error = kErrorWrongContext;
201 return false;
202 }
203
204 composition_cursor_ = cursor;
205 composition_text_.reset(new ui::CompositionText());
206 composition_text_->text = base::UTF8ToUTF16(text);
207
208 composition_text_->selection.set_start(selection_start);
209 composition_text_->selection.set_end(selection_end);
210
211 // TODO: Add support for displaying selected text in the composition string.
212 for (std::vector<SegmentInfo>::const_iterator segment = segments.begin();
213 segment != segments.end(); ++segment) {
214 ui::CompositionUnderline underline;
215
216 switch (segment->style) {
217 case SEGMENT_STYLE_UNDERLINE:
218 underline.color = SK_ColorBLACK;
219 break;
220 case SEGMENT_STYLE_DOUBLE_UNDERLINE:
221 underline.color = SK_ColorBLACK;
222 underline.thick = true;
223 break;
224 case SEGMENT_STYLE_NO_UNDERLINE:
225 underline.color = SK_ColorTRANSPARENT;
226 break;
227 default:
228 continue;
229 }
230
231 underline.start_offset = segment->start;
232 underline.end_offset = segment->end;
233 composition_text_->underlines.push_back(underline);
234 }
235
236 // TODO(nona): Makes focus out mode configuable, if necessary.
237 UpdateComposition(*composition_text_, composition_cursor_, true);
238 return true;
239 }
240
241 bool InputMethodEngine::ClearComposition(int context_id, std::string* error) {
242 if (!IsActive()) {
243 *error = kErrorNotActive;
244 return false;
245 }
246 if (context_id != context_id_ || context_id_ == -1) {
247 *error = kErrorWrongContext;
248 return false;
249 }
250
251 composition_cursor_ = 0;
252 composition_text_.reset(new ui::CompositionText());
253 UpdateComposition(*composition_text_, composition_cursor_, false);
254 return true;
255 }
256
257 bool InputMethodEngine::CommitText(int context_id,
258 const char* text,
259 std::string* error) {
260 if (!IsActive()) {
261 // TODO: Commit the text anyways.
262 *error = kErrorNotActive;
263 return false;
264 }
265 if (context_id != context_id_ || context_id_ == -1) {
266 *error = kErrorWrongContext;
267 return false;
268 }
269
270 ui::IMEBridge::Get()->GetInputContextHandler()->CommitText(text);
271
272 // Records histograms for committed characters.
273 if (!composition_text_->text.empty()) {
274 size_t len = GetUtf8StringLength(text);
275 UMA_HISTOGRAM_CUSTOM_COUNTS("InputMethod.CommitLength",
276 len, 1, 25, 25);
277 composition_text_.reset(new ui::CompositionText());
278 }
279 return true;
280 }
281
282 bool InputMethodEngine::SendKeyEvents( 58 bool InputMethodEngine::SendKeyEvents(
283 int context_id, 59 int context_id,
284 const std::vector<KeyboardEvent>& events) { 60 const std::vector<KeyboardEvent>& events) {
285 if (!IsActive()) { 61 if (!IsActive()) {
286 return false; 62 return false;
287 } 63 }
288 // context_id == 0, means sending key events to non-input field. 64 // context_id == 0, means sending key events to non-input field.
289 // context_id_ == -1, means the focus is not in an input field. 65 // context_id_ == -1, means the focus is not in an input field.
290 if (context_id != 0 && (context_id != context_id_ || context_id_ == -1)) { 66 if (context_id != 0 && (context_id != context_id_ || context_id_ == -1)) {
291 return false; 67 return false;
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 232
457 ui::ime::InputMethodMenuManager::GetInstance() 233 ui::ime::InputMethodMenuManager::GetInstance()
458 ->SetCurrentInputMethodMenuItemList(menu_item_list); 234 ->SetCurrentInputMethodMenuItemList(menu_item_list);
459 return true; 235 return true;
460 } 236 }
461 237
462 bool InputMethodEngine::IsActive() const { 238 bool InputMethodEngine::IsActive() const {
463 return !active_component_id_.empty(); 239 return !active_component_id_.empty();
464 } 240 }
465 241
466 bool InputMethodEngine::DeleteSurroundingText(int context_id,
467 int offset,
468 size_t number_of_chars,
469 std::string* error) {
470 if (!IsActive()) {
471 *error = kErrorNotActive;
472 return false;
473 }
474 if (context_id != context_id_ || context_id_ == -1) {
475 *error = kErrorWrongContext;
476 return false;
477 }
478
479 // TODO(nona): Return false if there is ongoing composition.
480
481 ui::IMEInputContextHandlerInterface* input_context =
482 ui::IMEBridge::Get()->GetInputContextHandler();
483 if (input_context)
484 input_context->DeleteSurroundingText(offset, number_of_chars);
485
486 return true;
487 }
488
489 void InputMethodEngine::HideInputView() { 242 void InputMethodEngine::HideInputView() {
490 keyboard::KeyboardController* keyboard_controller = 243 keyboard::KeyboardController* keyboard_controller =
491 keyboard::KeyboardController::GetInstance(); 244 keyboard::KeyboardController::GetInstance();
492 if (keyboard_controller) { 245 if (keyboard_controller) {
493 keyboard_controller->HideKeyboard( 246 keyboard_controller->HideKeyboard(
494 keyboard::KeyboardController::HIDE_REASON_MANUAL); 247 keyboard::KeyboardController::HIDE_REASON_MANUAL);
495 } 248 }
496 } 249 }
497 250
498 void InputMethodEngine::SetCompositionBounds(
499 const std::vector<gfx::Rect>& bounds) {
500 if (!CheckProfile())
501 return;
502 observer_->OnCompositionBoundsChanged(bounds);
503 }
504
505 void InputMethodEngine::EnableInputView() { 251 void InputMethodEngine::EnableInputView() {
506 keyboard::SetOverrideContentUrl(input_method::InputMethodManager::Get() 252 keyboard::SetOverrideContentUrl(input_method::InputMethodManager::Get()
507 ->GetActiveIMEState() 253 ->GetActiveIMEState()
508 ->GetCurrentInputMethod() 254 ->GetCurrentInputMethod()
509 .input_view_url()); 255 .input_view_url());
510 keyboard::KeyboardController* keyboard_controller = 256 keyboard::KeyboardController* keyboard_controller =
511 keyboard::KeyboardController::GetInstance(); 257 keyboard::KeyboardController::GetInstance();
512 if (keyboard_controller) 258 if (keyboard_controller)
513 keyboard_controller->Reload(); 259 keyboard_controller->Reload();
514 } 260 }
515 261
516 void InputMethodEngine::FocusIn(
517 const ui::IMEEngineHandlerInterface::InputContext& input_context) {
518 if (!CheckProfile())
519 return;
520 current_input_type_ = input_context.type;
521
522 if (!IsActive() || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
523 return;
524
525 context_id_ = next_context_id_;
526 ++next_context_id_;
527
528 observer_->OnFocus(ui::IMEEngineHandlerInterface::InputContext(
529 context_id_, input_context.type, input_context.mode,
530 input_context.flags));
531 }
532
533 void InputMethodEngine::FocusOut() {
534 if (!CheckProfile())
535 return;
536 if (!IsActive() || current_input_type_ == ui::TEXT_INPUT_TYPE_NONE)
537 return;
538
539 current_input_type_ = ui::TEXT_INPUT_TYPE_NONE;
540
541 int context_id = context_id_;
542 context_id_ = -1;
543 observer_->OnBlur(context_id);
544 }
545 262
546 void InputMethodEngine::Enable(const std::string& component_id) { 263 void InputMethodEngine::Enable(const std::string& component_id) {
547 if (!CheckProfile()) 264 InputMethodEngineBase::Enable(component_id);
548 return;
549 DCHECK(!component_id.empty());
550 active_component_id_ = component_id;
551 observer_->OnActivate(component_id);
552 const ui::IMEEngineHandlerInterface::InputContext& input_context =
553 ui::IMEBridge::Get()->GetCurrentInputContext();
554 current_input_type_ = input_context.type;
555 FocusIn(input_context);
556 EnableInputView(); 265 EnableInputView();
557 } 266 }
558 267
559 void InputMethodEngine::Disable() {
560 if (!CheckProfile())
561 return;
562 active_component_id_.clear();
563 ui::IMEBridge::Get()->GetInputContextHandler()->CommitText(
564 base::UTF16ToUTF8(composition_text_->text));
565 composition_text_.reset(new ui::CompositionText());
566 observer_->OnDeactivated(active_component_id_);
567 }
568
569 void InputMethodEngine::PropertyActivate(const std::string& property_name) { 268 void InputMethodEngine::PropertyActivate(const std::string& property_name) {
570 if (!CheckProfile())
571 return;
572 observer_->OnMenuItemActivated(active_component_id_, property_name); 269 observer_->OnMenuItemActivated(active_component_id_, property_name);
573 } 270 }
574 271
575 void InputMethodEngine::Reset() {
576 if (!CheckProfile())
577 return;
578 composition_text_.reset(new ui::CompositionText());
579 observer_->OnReset(active_component_id_);
580 }
581
582 bool InputMethodEngine::IsInterestedInKeyEvent() const {
583 return observer_->IsInterestedInKeyEvent();
584 }
585
586 void InputMethodEngine::ProcessKeyEvent(const ui::KeyEvent& key_event,
587 KeyEventDoneCallback& callback) {
588 if (!CheckProfile())
589 return;
590
591 KeyboardEvent ext_event;
592 GetExtensionKeyboardEventFromKeyEvent(key_event, &ext_event);
593
594 // If the given key event is equal to the key event sent by
595 // SendKeyEvents, this engine ID is propagated to the extension IME.
596 // Note, this check relies on that ui::KeyEvent is propagated as
597 // reference without copying.
598 if (&key_event == sent_key_event_)
599 ext_event.extension_id = extension_id_;
600
601 observer_->OnKeyEvent(active_component_id_, ext_event, callback);
602 }
603
604 void InputMethodEngine::CandidateClicked(uint32_t index) { 272 void InputMethodEngine::CandidateClicked(uint32_t index) {
605 if (!CheckProfile())
606 return;
607 if (index > candidate_ids_.size()) { 273 if (index > candidate_ids_.size()) {
608 return; 274 return;
609 } 275 }
610 276
611 // Only left button click is supported at this moment. 277 // Only left button click is supported at this moment.
612 observer_->OnCandidateClicked(active_component_id_, candidate_ids_.at(index), 278 observer_->OnCandidateClicked(active_component_id_, candidate_ids_.at(index),
613 ui::IMEEngineObserver::MOUSE_BUTTON_LEFT); 279 ui::IMEEngineObserver::MOUSE_BUTTON_LEFT);
614 } 280 }
615 281
616 void InputMethodEngine::SetSurroundingText(const std::string& text,
617 uint32_t cursor_pos,
618 uint32_t anchor_pos,
619 uint32_t offset_pos) {
620 if (!CheckProfile())
621 return;
622 observer_->OnSurroundingTextChanged(
623 active_component_id_, text, static_cast<int>(cursor_pos),
624 static_cast<int>(anchor_pos), static_cast<int>(offset_pos));
625 }
626
627 bool InputMethodEngine::CheckProfile() const {
628 Profile* active_profile = ProfileManager::GetActiveUserProfile();
629 return active_profile == profile_ || profile_->IsSameProfile(active_profile);
630 }
631
632 // TODO(uekawa): rename this method to a more reasonable name. 282 // TODO(uekawa): rename this method to a more reasonable name.
633 void InputMethodEngine::MenuItemToProperty( 283 void InputMethodEngine::MenuItemToProperty(
634 const MenuItem& item, 284 const MenuItem& item,
635 ui::ime::InputMethodMenuItem* property) { 285 ui::ime::InputMethodMenuItem* property) {
636 property->key = item.id; 286 property->key = item.id;
637 287
638 if (item.modified & MENU_ITEM_MODIFIED_LABEL) { 288 if (item.modified & MENU_ITEM_MODIFIED_LABEL) {
639 property->label = item.label; 289 property->label = item.label;
640 } 290 }
641 if (item.modified & MENU_ITEM_MODIFIED_VISIBLE) { 291 if (item.modified & MENU_ITEM_MODIFIED_VISIBLE) {
(...skipping 23 matching lines...) Expand all
665 // TODO(nona): Implement it. 315 // TODO(nona): Implement it.
666 break; 316 break;
667 } 317 }
668 } 318 }
669 } 319 }
670 320
671 // TODO(nona): Support item.children. 321 // TODO(nona): Support item.children.
672 } 322 }
673 323
674 } // namespace chromeos 324 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/input_method/input_method_engine.h ('k') | chrome/browser/input_method/OWNERS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698