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 "chrome/browser/chromeos/input_method/input_method_util.h" | 5 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
18 #include "chromeos/ime/component_extension_ime_manager.h" | 18 #include "chromeos/ime/component_extension_ime_manager.h" |
19 #include "chromeos/ime/extension_ime_util.h" | 19 #include "chromeos/ime/extension_ime_util.h" |
| 20 // For SetHardwareKeyboardLayoutForTesting. |
| 21 #include "chromeos/ime/fake_input_method_delegate.h" |
20 #include "chromeos/ime/input_method_delegate.h" | 22 #include "chromeos/ime/input_method_delegate.h" |
21 // TODO(nona): move this header from this file. | 23 // TODO(nona): move this header from this file. |
22 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 // A mapping from an input method id to a string for the language indicator. The | 28 // A mapping from an input method id to a string for the language indicator. The |
27 // mapping is necessary since some input methods belong to the same language. | 29 // mapping is necessary since some input methods belong to the same language. |
28 // For example, both "xkb:us::eng" and "xkb:us:dvorak:eng" are for US English. | 30 // For example, both "xkb:us::eng" and "xkb:us:dvorak:eng" are for US English. |
29 const struct { | 31 const struct { |
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 const std::string& language_code) { | 604 const std::string& language_code) { |
603 std::vector<std::string> candidates; | 605 std::vector<std::string> candidates; |
604 GetInputMethodIdsFromLanguageCode( | 606 GetInputMethodIdsFromLanguageCode( |
605 language_code, input_method::kKeyboardLayoutsOnly, &candidates); | 607 language_code, input_method::kKeyboardLayoutsOnly, &candidates); |
606 if (candidates.size()) | 608 if (candidates.size()) |
607 return candidates.front(); | 609 return candidates.front(); |
608 | 610 |
609 return std::string(); | 611 return std::string(); |
610 } | 612 } |
611 | 613 |
612 std::string InputMethodUtil::GetHardwareInputMethodId() const { | 614 void InputMethodUtil::UpdateHardwareLayoutCache() { |
613 const std::string input_method_id = delegate_->GetHardwareKeyboardLayout(); | 615 DCHECK(thread_checker_.CalledOnValidThread()); |
| 616 hardware_layouts_.clear(); |
| 617 hardware_login_layouts_.clear(); |
| 618 Tokenize(delegate_->GetHardwareKeyboardLayouts(), ",", &hardware_layouts_); |
614 | 619 |
615 if (input_method_id.empty()) { | 620 for (size_t i = 0; i < hardware_layouts_.size(); ++i) { |
| 621 if (IsLoginKeyboard(hardware_layouts_[i])) |
| 622 hardware_login_layouts_.push_back(hardware_layouts_[i]); |
| 623 } |
| 624 if (hardware_layouts_.empty()) { |
616 // This is totally fine if it's empty. The hardware keyboard layout is | 625 // This is totally fine if it's empty. The hardware keyboard layout is |
617 // not stored if startup_manifest.json (OEM customization data) is not | 626 // not stored if startup_manifest.json (OEM customization data) is not |
618 // present (ex. Cr48 doen't have that file). | 627 // present (ex. Cr48 doen't have that file). |
619 return GetFallbackInputMethodDescriptor().id(); | 628 hardware_layouts_.push_back(GetFallbackInputMethodDescriptor().id()); |
620 } | 629 } |
621 return input_method_id; | 630 |
| 631 if (hardware_login_layouts_.empty()) |
| 632 hardware_login_layouts_.push_back(GetFallbackInputMethodDescriptor().id()); |
622 } | 633 } |
623 | 634 |
624 std::string InputMethodUtil::GetHardwareLoginInputMethodId() const { | 635 void InputMethodUtil::SetHardwareKeyboardLayoutForTesting( |
625 const std::string input_method_id = GetHardwareInputMethodId(); | 636 const std::string& layout) { |
| 637 delegate_->SetHardwareKeyboardLayoutForTesting(layout); |
| 638 UpdateHardwareLayoutCache(); |
| 639 } |
626 | 640 |
627 if (!IsLoginKeyboard(input_method_id)) | 641 const std::vector<std::string>& |
628 return GetFallbackInputMethodDescriptor().id(); | 642 InputMethodUtil::GetHardwareInputMethodIds() { |
| 643 DCHECK(thread_checker_.CalledOnValidThread()); |
| 644 // Once the initialization is done, at least one input method should be set. |
| 645 if (hardware_layouts_.empty()) |
| 646 UpdateHardwareLayoutCache(); |
| 647 return hardware_layouts_; |
| 648 } |
629 | 649 |
630 return input_method_id; | 650 const std::vector<std::string>& |
| 651 InputMethodUtil::GetHardwareLoginInputMethodIds() { |
| 652 DCHECK(thread_checker_.CalledOnValidThread()); |
| 653 // Once the initialization is done, at least one input method should be set. |
| 654 if (hardware_login_layouts_.empty()) |
| 655 UpdateHardwareLayoutCache(); |
| 656 return hardware_login_layouts_; |
631 } | 657 } |
632 | 658 |
633 bool InputMethodUtil::IsLoginKeyboard(const std::string& input_method_id) | 659 bool InputMethodUtil::IsLoginKeyboard(const std::string& input_method_id) |
634 const { | 660 const { |
635 const InputMethodDescriptor* ime = | 661 const InputMethodDescriptor* ime = |
636 GetInputMethodDescriptorFromId(input_method_id); | 662 GetInputMethodDescriptorFromId(input_method_id); |
637 return ime ? ime->is_login_keyboard() : false; | 663 return ime ? ime->is_login_keyboard() : false; |
638 } | 664 } |
639 | 665 |
640 void InputMethodUtil::SetComponentExtensions( | 666 void InputMethodUtil::SetComponentExtensions( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 if (IsKeyboardLayout(input_method.id())) { | 721 if (IsKeyboardLayout(input_method.id())) { |
696 xkb_id_to_descriptor_.insert( | 722 xkb_id_to_descriptor_.insert( |
697 std::make_pair(input_method.GetPreferredKeyboardLayout(), | 723 std::make_pair(input_method.GetPreferredKeyboardLayout(), |
698 input_method)); | 724 input_method)); |
699 } | 725 } |
700 } | 726 } |
701 } | 727 } |
702 | 728 |
703 } // namespace input_method | 729 } // namespace input_method |
704 } // namespace chromeos | 730 } // namespace chromeos |
OLD | NEW |