| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/chromeos/login/l10n_util.h" |
| 6 |
| 7 #include <set> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 15 #include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler
.h" |
| 16 #include "chromeos/ime/component_extension_ime_manager.h" |
| 17 #include "chromeos/ime/input_method_descriptor.h" |
| 18 #include "chromeos/ime/input_method_manager.h" |
| 19 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 |
| 22 namespace chromeos { |
| 23 |
| 24 namespace { |
| 25 |
| 26 base::DictionaryValue* CreateInputMethodsEntry( |
| 27 const input_method::InputMethodDescriptor& method, |
| 28 const std::string selected) { |
| 29 input_method::InputMethodUtil* util = |
| 30 input_method::InputMethodManager::Get()->GetInputMethodUtil(); |
| 31 const std::string& ime_id = method.id(); |
| 32 scoped_ptr<base::DictionaryValue> input_method(new base::DictionaryValue); |
| 33 input_method->SetString("value", ime_id); |
| 34 input_method->SetString("title", util->GetInputMethodLongName(method)); |
| 35 input_method->SetBoolean("selected", ime_id == selected); |
| 36 return input_method.release(); |
| 37 } |
| 38 |
| 39 // Returns true if element was inserted. |
| 40 bool InsertString(const std::string& str, std::set<std::string>& to) { |
| 41 const std::pair<std::set<std::string>::iterator, bool> result = |
| 42 to.insert(str); |
| 43 return result.second; |
| 44 } |
| 45 |
| 46 void AddOptgroupOtherLayouts(base::ListValue* input_methods_list) { |
| 47 scoped_ptr<base::DictionaryValue> optgroup(new base::DictionaryValue); |
| 48 optgroup->SetString( |
| 49 "optionGroupName", |
| 50 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_KEYBOARD_LAYOUTS)); |
| 51 input_methods_list->Append(optgroup.release()); |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 scoped_ptr<base::ListValue> GetUILanguageList( |
| 57 const std::vector<std::string>* most_relevant_language_codes, |
| 58 const std::string& selected) { |
| 59 ComponentExtensionIMEManager* manager = |
| 60 input_method::InputMethodManager::Get()-> |
| 61 GetComponentExtensionIMEManager(); |
| 62 input_method::InputMethodDescriptors descriptors; |
| 63 if (manager->IsInitialized()) |
| 64 descriptors = manager->GetXkbIMEAsInputMethodDescriptor(); |
| 65 scoped_ptr<base::ListValue> languages_list( |
| 66 options::CrosLanguageOptionsHandler::GetUILanguageList( |
| 67 descriptors, |
| 68 most_relevant_language_codes)); |
| 69 for (size_t i = 0; i < languages_list->GetSize(); ++i) { |
| 70 base::DictionaryValue* language_info = NULL; |
| 71 if (!languages_list->GetDictionary(i, &language_info)) |
| 72 NOTREACHED(); |
| 73 |
| 74 std::string value; |
| 75 language_info->GetString("code", &value); |
| 76 std::string display_name; |
| 77 language_info->GetString("displayName", &display_name); |
| 78 std::string native_name; |
| 79 language_info->GetString("nativeDisplayName", &native_name); |
| 80 |
| 81 // If it's an option group divider, add field name. |
| 82 if (value == options::kMostRelevantLanguagesDivider) { |
| 83 language_info->SetString( |
| 84 "optionGroupName", |
| 85 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_LANGUAGES)); |
| 86 } |
| 87 if (display_name != native_name) { |
| 88 display_name = base::StringPrintf("%s - %s", |
| 89 display_name.c_str(), |
| 90 native_name.c_str()); |
| 91 } |
| 92 |
| 93 language_info->SetString("value", value); |
| 94 language_info->SetString("title", display_name); |
| 95 if (value == selected) |
| 96 language_info->SetBoolean("selected", true); |
| 97 } |
| 98 return languages_list.Pass(); |
| 99 } |
| 100 |
| 101 scoped_ptr<base::ListValue> GetLoginKeyboardLayouts( |
| 102 const std::string& locale, |
| 103 const std::string& selected) { |
| 104 scoped_ptr<base::ListValue> input_methods_list(new base::ListValue); |
| 105 input_method::InputMethodManager* manager = |
| 106 input_method::InputMethodManager::Get(); |
| 107 input_method::InputMethodUtil* util = manager->GetInputMethodUtil(); |
| 108 if (!manager->GetComponentExtensionIMEManager()->IsInitialized()) { |
| 109 input_method::InputMethodDescriptor fallback = |
| 110 util->GetFallbackInputMethodDescriptor(); |
| 111 input_methods_list->Append( |
| 112 CreateInputMethodsEntry(fallback, fallback.id())); |
| 113 return input_methods_list.Pass(); |
| 114 } |
| 115 |
| 116 const std::vector<std::string>& hardware_login_input_methods = |
| 117 util->GetHardwareLoginInputMethodIds(); |
| 118 manager->EnableLoginLayouts(locale, hardware_login_input_methods); |
| 119 |
| 120 scoped_ptr<input_method::InputMethodDescriptors> input_methods( |
| 121 manager->GetActiveInputMethods()); |
| 122 std::set<std::string> input_methods_added; |
| 123 |
| 124 for (std::vector<std::string>::const_iterator i = |
| 125 hardware_login_input_methods.begin(); |
| 126 i != hardware_login_input_methods.end(); |
| 127 ++i) { |
| 128 const input_method::InputMethodDescriptor* ime = |
| 129 util->GetInputMethodDescriptorFromId(*i); |
| 130 // Do not crash in case of misconfiguration. |
| 131 if (ime) { |
| 132 input_methods_added.insert(*i); |
| 133 input_methods_list->Append(CreateInputMethodsEntry(*ime, selected)); |
| 134 } else { |
| 135 NOTREACHED(); |
| 136 } |
| 137 } |
| 138 |
| 139 bool optgroup_added = false; |
| 140 for (size_t i = 0; i < input_methods->size(); ++i) { |
| 141 // Makes sure the id is in legacy xkb id format. |
| 142 const std::string& ime_id = (*input_methods)[i].id(); |
| 143 if (!InsertString(ime_id, input_methods_added)) |
| 144 continue; |
| 145 if (!optgroup_added) { |
| 146 optgroup_added = true; |
| 147 AddOptgroupOtherLayouts(input_methods_list.get()); |
| 148 } |
| 149 input_methods_list->Append(CreateInputMethodsEntry((*input_methods)[i], |
| 150 selected)); |
| 151 } |
| 152 |
| 153 // "xkb:us::eng" should always be in the list of available layouts. |
| 154 const std::string us_keyboard_id = |
| 155 util->GetFallbackInputMethodDescriptor().id(); |
| 156 if (input_methods_added.find(us_keyboard_id) == input_methods_added.end()) { |
| 157 const input_method::InputMethodDescriptor* us_eng_descriptor = |
| 158 util->GetInputMethodDescriptorFromId(us_keyboard_id); |
| 159 DCHECK(us_eng_descriptor); |
| 160 if (!optgroup_added) { |
| 161 optgroup_added = true; |
| 162 AddOptgroupOtherLayouts(input_methods_list.get()); |
| 163 } |
| 164 input_methods_list->Append(CreateInputMethodsEntry(*us_eng_descriptor, |
| 165 selected)); |
| 166 } |
| 167 return input_methods_list.Pass(); |
| 168 } |
| 169 |
| 170 } // namespace chromeos |
| OLD | NEW |