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 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 const std::string& language_code) { | 609 const std::string& language_code) { |
608 std::vector<std::string> candidates; | 610 std::vector<std::string> candidates; |
609 GetInputMethodIdsFromLanguageCode( | 611 GetInputMethodIdsFromLanguageCode( |
610 language_code, input_method::kKeyboardLayoutsOnly, &candidates); | 612 language_code, input_method::kKeyboardLayoutsOnly, &candidates); |
611 if (candidates.size()) | 613 if (candidates.size()) |
612 return candidates.front(); | 614 return candidates.front(); |
613 | 615 |
614 return std::string(); | 616 return std::string(); |
615 } | 617 } |
616 | 618 |
617 std::string InputMethodUtil::GetHardwareInputMethodId() const { | 619 void InputMethodUtil::UpdateHardwareLayoutCache() { |
618 const std::string input_method_id = delegate_->GetHardwareKeyboardLayout(); | 620 DCHECK(thread_checker_.CalledOnValidThread()); |
| 621 hardware_layouts_.clear(); |
| 622 hardware_login_layouts_.clear(); |
| 623 Tokenize(delegate_->GetHardwareKeyboardLayouts(), ",", &hardware_layouts_); |
619 | 624 |
620 if (input_method_id.empty()) { | 625 for (size_t i = 0; i < hardware_layouts_.size(); ++i) { |
| 626 if (IsLoginKeyboard(hardware_layouts_[i])) |
| 627 hardware_login_layouts_.push_back(hardware_layouts_[i]); |
| 628 } |
| 629 if (hardware_layouts_.empty()) { |
621 // This is totally fine if it's empty. The hardware keyboard layout is | 630 // This is totally fine if it's empty. The hardware keyboard layout is |
622 // not stored if startup_manifest.json (OEM customization data) is not | 631 // not stored if startup_manifest.json (OEM customization data) is not |
623 // present (ex. Cr48 doen't have that file). | 632 // present (ex. Cr48 doen't have that file). |
624 return GetFallbackInputMethodDescriptor().id(); | 633 hardware_layouts_.push_back(GetFallbackInputMethodDescriptor().id()); |
625 } | 634 } |
626 return input_method_id; | 635 |
| 636 if (hardware_login_layouts_.empty()) |
| 637 hardware_login_layouts_.push_back(GetFallbackInputMethodDescriptor().id()); |
627 } | 638 } |
628 | 639 |
629 std::string InputMethodUtil::GetHardwareLoginInputMethodId() const { | 640 void InputMethodUtil::SetHardwareKeyboardLayoutForTesting( |
630 const std::string input_method_id = GetHardwareInputMethodId(); | 641 const std::string& layout) { |
| 642 delegate_->SetHardwareKeyboardLayoutForTesting(layout); |
| 643 UpdateHardwareLayoutCache(); |
| 644 } |
631 | 645 |
632 if (!IsLoginKeyboard(input_method_id)) | 646 const std::vector<std::string>& |
633 return GetFallbackInputMethodDescriptor().id(); | 647 InputMethodUtil::GetHardwareInputMethodIds() { |
| 648 DCHECK(thread_checker_.CalledOnValidThread()); |
| 649 // Once the initialization is done, at least one input method should be set. |
| 650 if (hardware_layouts_.empty()) |
| 651 UpdateHardwareLayoutCache(); |
| 652 return hardware_layouts_; |
| 653 } |
634 | 654 |
635 return input_method_id; | 655 const std::vector<std::string>& |
| 656 InputMethodUtil::GetHardwareLoginInputMethodIds() { |
| 657 DCHECK(thread_checker_.CalledOnValidThread()); |
| 658 // Once the initialization is done, at least one input method should be set. |
| 659 if (hardware_login_layouts_.empty()) |
| 660 UpdateHardwareLayoutCache(); |
| 661 return hardware_login_layouts_; |
636 } | 662 } |
637 | 663 |
638 bool InputMethodUtil::IsLoginKeyboard(const std::string& input_method_id) | 664 bool InputMethodUtil::IsLoginKeyboard(const std::string& input_method_id) |
639 const { | 665 const { |
640 const InputMethodDescriptor* ime = | 666 const InputMethodDescriptor* ime = |
641 GetInputMethodDescriptorFromId(input_method_id); | 667 GetInputMethodDescriptorFromId(input_method_id); |
642 return ime ? ime->is_login_keyboard() : false; | 668 return ime ? ime->is_login_keyboard() : false; |
643 } | 669 } |
644 | 670 |
645 void InputMethodUtil::SetComponentExtensions( | 671 void InputMethodUtil::SetComponentExtensions( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 if (IsKeyboardLayout(input_method.id())) { | 725 if (IsKeyboardLayout(input_method.id())) { |
700 xkb_id_to_descriptor_.insert( | 726 xkb_id_to_descriptor_.insert( |
701 std::make_pair(input_method.GetPreferredKeyboardLayout(), | 727 std::make_pair(input_method.GetPreferredKeyboardLayout(), |
702 input_method)); | 728 input_method)); |
703 } | 729 } |
704 } | 730 } |
705 } | 731 } |
706 | 732 |
707 } // namespace input_method | 733 } // namespace input_method |
708 } // namespace chromeos | 734 } // namespace chromeos |
OLD | NEW |