| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/options2/chromeos/cros_language_options_handle
r2.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <set> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/bind_helpers.h" | |
| 13 #include "base/i18n/rtl.h" | |
| 14 #include "base/stringprintf.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/app/chrome_command_ids.h" | |
| 18 #include "chrome/browser/browser_process.h" | |
| 19 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | |
| 20 #include "chrome/browser/chromeos/input_method/input_method_util.h" | |
| 21 #include "chrome/browser/lifetime/application_lifetime.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | |
| 23 #include "chrome/browser/ui/browser.h" | |
| 24 #include "chrome/browser/ui/browser_finder.h" | |
| 25 #include "content/public/browser/navigation_controller.h" | |
| 26 #include "content/public/browser/user_metrics.h" | |
| 27 #include "content/public/browser/web_contents.h" | |
| 28 #include "grit/chromium_strings.h" | |
| 29 #include "grit/generated_resources.h" | |
| 30 #include "ui/base/l10n/l10n_util.h" | |
| 31 | |
| 32 using content::UserMetricsAction; | |
| 33 | |
| 34 namespace chromeos { | |
| 35 namespace options2 { | |
| 36 | |
| 37 CrosLanguageOptionsHandler::CrosLanguageOptionsHandler() { | |
| 38 } | |
| 39 | |
| 40 CrosLanguageOptionsHandler::~CrosLanguageOptionsHandler() { | |
| 41 } | |
| 42 | |
| 43 void CrosLanguageOptionsHandler::GetLocalizedValues( | |
| 44 DictionaryValue* localized_strings) { | |
| 45 ::options2::LanguageOptionsHandlerCommon::GetLocalizedValues( | |
| 46 localized_strings); | |
| 47 | |
| 48 RegisterTitle(localized_strings, "languagePage", | |
| 49 IDS_OPTIONS_SETTINGS_LANGUAGES_AND_INPUT_DIALOG_TITLE); | |
| 50 localized_strings->SetString("ok_button", l10n_util::GetStringUTF16(IDS_OK)); | |
| 51 localized_strings->SetString("configure", | |
| 52 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_CONFIGURE)); | |
| 53 localized_strings->SetString("input_method", | |
| 54 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD)); | |
| 55 localized_strings->SetString("please_add_another_input_method", | |
| 56 l10n_util::GetStringUTF16( | |
| 57 IDS_OPTIONS_SETTINGS_LANGUAGES_PLEASE_ADD_ANOTHER_INPUT_METHOD)); | |
| 58 localized_strings->SetString("input_method_instructions", | |
| 59 l10n_util::GetStringUTF16( | |
| 60 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_INSTRUCTIONS)); | |
| 61 localized_strings->SetString("switch_input_methods_hint", | |
| 62 l10n_util::GetStringUTF16( | |
| 63 IDS_OPTIONS_SETTINGS_LANGUAGES_SWITCH_INPUT_METHODS_HINT)); | |
| 64 localized_strings->SetString("select_previous_input_method_hint", | |
| 65 l10n_util::GetStringUTF16( | |
| 66 IDS_OPTIONS_SETTINGS_LANGUAGES_SELECT_PREVIOUS_INPUT_METHOD_HINT)); | |
| 67 localized_strings->SetString("restart_button", | |
| 68 l10n_util::GetStringUTF16( | |
| 69 IDS_OPTIONS_SETTINGS_LANGUAGES_SIGN_OUT_BUTTON)); | |
| 70 | |
| 71 input_method::InputMethodManager* manager = | |
| 72 input_method::InputMethodManager::GetInstance(); | |
| 73 // GetSupportedInputMethods() never return NULL. | |
| 74 scoped_ptr<input_method::InputMethodDescriptors> descriptors( | |
| 75 manager->GetSupportedInputMethods()); | |
| 76 localized_strings->Set("languageList", GetLanguageList(*descriptors)); | |
| 77 localized_strings->Set("inputMethodList", GetInputMethodList(*descriptors)); | |
| 78 } | |
| 79 | |
| 80 void CrosLanguageOptionsHandler::RegisterMessages() { | |
| 81 ::options2::LanguageOptionsHandlerCommon::RegisterMessages(); | |
| 82 | |
| 83 web_ui()->RegisterMessageCallback("inputMethodDisable", | |
| 84 base::Bind(&CrosLanguageOptionsHandler::InputMethodDisableCallback, | |
| 85 base::Unretained(this))); | |
| 86 web_ui()->RegisterMessageCallback("inputMethodEnable", | |
| 87 base::Bind(&CrosLanguageOptionsHandler::InputMethodEnableCallback, | |
| 88 base::Unretained(this))); | |
| 89 web_ui()->RegisterMessageCallback("inputMethodOptionsOpen", | |
| 90 base::Bind(&CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback, | |
| 91 base::Unretained(this))); | |
| 92 web_ui()->RegisterMessageCallback("uiLanguageRestart", | |
| 93 base::Bind(&CrosLanguageOptionsHandler::RestartCallback, | |
| 94 base::Unretained(this))); | |
| 95 } | |
| 96 | |
| 97 ListValue* CrosLanguageOptionsHandler::GetInputMethodList( | |
| 98 const input_method::InputMethodDescriptors& descriptors) { | |
| 99 input_method::InputMethodManager* manager = | |
| 100 input_method::InputMethodManager::GetInstance(); | |
| 101 | |
| 102 ListValue* input_method_list = new ListValue(); | |
| 103 | |
| 104 for (size_t i = 0; i < descriptors.size(); ++i) { | |
| 105 const input_method::InputMethodDescriptor& descriptor = | |
| 106 descriptors[i]; | |
| 107 const std::string language_code = descriptor.language_code(); | |
| 108 const std::string display_name = | |
| 109 manager->GetInputMethodUtil()->GetInputMethodDisplayNameFromId( | |
| 110 descriptor.id()); | |
| 111 | |
| 112 DictionaryValue* dictionary = new DictionaryValue(); | |
| 113 dictionary->SetString("id", descriptor.id()); | |
| 114 dictionary->SetString("displayName", display_name); | |
| 115 | |
| 116 // One input method can be associated with multiple languages, hence | |
| 117 // we use a dictionary here. | |
| 118 DictionaryValue* language_codes = new DictionaryValue(); | |
| 119 language_codes->SetBoolean(language_code, true); | |
| 120 // Check kExtraLanguages to see if there are languages associated with | |
| 121 // this input method. If these are present, add these. | |
| 122 for (size_t j = 0; j < input_method::kExtraLanguagesLength; ++j) { | |
| 123 const std::string extra_input_method_id = | |
| 124 input_method::kExtraLanguages[j].input_method_id; | |
| 125 const std::string extra_language_code = | |
| 126 input_method::kExtraLanguages[j].language_code; | |
| 127 if (extra_input_method_id == descriptor.id()) { | |
| 128 language_codes->SetBoolean(extra_language_code, true); | |
| 129 } | |
| 130 } | |
| 131 dictionary->Set("languageCodeSet", language_codes); | |
| 132 | |
| 133 input_method_list->Append(dictionary); | |
| 134 } | |
| 135 | |
| 136 return input_method_list; | |
| 137 } | |
| 138 | |
| 139 ListValue* CrosLanguageOptionsHandler::GetLanguageList( | |
| 140 const input_method::InputMethodDescriptors& descriptors) { | |
| 141 std::set<std::string> language_codes; | |
| 142 // Collect the language codes from the supported input methods. | |
| 143 for (size_t i = 0; i < descriptors.size(); ++i) { | |
| 144 const input_method::InputMethodDescriptor& descriptor = descriptors[i]; | |
| 145 const std::string language_code = descriptor.language_code(); | |
| 146 language_codes.insert(language_code); | |
| 147 } | |
| 148 // Collect the language codes from kExtraLanguages. | |
| 149 for (size_t i = 0; i < input_method::kExtraLanguagesLength; ++i) { | |
| 150 const char* language_code = | |
| 151 input_method::kExtraLanguages[i].language_code; | |
| 152 language_codes.insert(language_code); | |
| 153 } | |
| 154 | |
| 155 // Map of display name -> {language code, native_display_name}. | |
| 156 // In theory, we should be able to create a map that is sorted by | |
| 157 // display names using ICU comparator, but doing it is hard, thus we'll | |
| 158 // use an auxiliary vector to achieve the same result. | |
| 159 typedef std::pair<std::string, string16> LanguagePair; | |
| 160 typedef std::map<string16, LanguagePair> LanguageMap; | |
| 161 LanguageMap language_map; | |
| 162 // The auxiliary vector mentioned above. | |
| 163 std::vector<string16> display_names; | |
| 164 | |
| 165 // Build the list of display names, and build the language map. | |
| 166 for (std::set<std::string>::const_iterator iter = language_codes.begin(); | |
| 167 iter != language_codes.end(); ++iter) { | |
| 168 const string16 display_name = | |
| 169 input_method::InputMethodUtil::GetLanguageDisplayNameFromCode(*iter); | |
| 170 const string16 native_display_name = | |
| 171 input_method::InputMethodUtil::GetLanguageNativeDisplayNameFromCode( | |
| 172 *iter); | |
| 173 display_names.push_back(display_name); | |
| 174 language_map[display_name] = | |
| 175 std::make_pair(*iter, native_display_name); | |
| 176 } | |
| 177 DCHECK_EQ(display_names.size(), language_map.size()); | |
| 178 | |
| 179 // Sort display names using locale specific sorter. | |
| 180 l10n_util::SortStrings16(g_browser_process->GetApplicationLocale(), | |
| 181 &display_names); | |
| 182 | |
| 183 // Build the language list from the language map. | |
| 184 ListValue* language_list = new ListValue(); | |
| 185 for (size_t i = 0; i < display_names.size(); ++i) { | |
| 186 // Sets the directionality of the display language name. | |
| 187 string16 display_name(display_names[i]); | |
| 188 bool markup_removal = | |
| 189 base::i18n::UnadjustStringForLocaleDirection(&display_name); | |
| 190 DCHECK(markup_removal); | |
| 191 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name); | |
| 192 std::string directionality = has_rtl_chars ? "rtl" : "ltr"; | |
| 193 | |
| 194 const LanguagePair& pair = language_map[display_names[i]]; | |
| 195 DictionaryValue* dictionary = new DictionaryValue(); | |
| 196 dictionary->SetString("code", pair.first); | |
| 197 dictionary->SetString("displayName", display_names[i]); | |
| 198 dictionary->SetString("textDirection", directionality); | |
| 199 dictionary->SetString("nativeDisplayName", pair.second); | |
| 200 language_list->Append(dictionary); | |
| 201 } | |
| 202 | |
| 203 return language_list; | |
| 204 } | |
| 205 | |
| 206 string16 CrosLanguageOptionsHandler::GetProductName() { | |
| 207 return l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME); | |
| 208 } | |
| 209 | |
| 210 void CrosLanguageOptionsHandler::SetApplicationLocale( | |
| 211 const std::string& language_code) { | |
| 212 Profile::FromWebUI(web_ui())->ChangeAppLocale( | |
| 213 language_code, Profile::APP_LOCALE_CHANGED_VIA_SETTINGS); | |
| 214 } | |
| 215 | |
| 216 void CrosLanguageOptionsHandler::RestartCallback(const ListValue* args) { | |
| 217 content::RecordAction(UserMetricsAction("LanguageOptions_SignOut")); | |
| 218 browser::AttemptUserExit(); | |
| 219 } | |
| 220 | |
| 221 void CrosLanguageOptionsHandler::InputMethodDisableCallback( | |
| 222 const ListValue* args) { | |
| 223 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args)); | |
| 224 const std::string action = base::StringPrintf( | |
| 225 "LanguageOptions_DisableInputMethod_%s", input_method_id.c_str()); | |
| 226 content::RecordComputedAction(action); | |
| 227 } | |
| 228 | |
| 229 void CrosLanguageOptionsHandler::InputMethodEnableCallback( | |
| 230 const ListValue* args) { | |
| 231 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args)); | |
| 232 const std::string action = base::StringPrintf( | |
| 233 "LanguageOptions_EnableInputMethod_%s", input_method_id.c_str()); | |
| 234 content::RecordComputedAction(action); | |
| 235 } | |
| 236 | |
| 237 void CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback( | |
| 238 const ListValue* args) { | |
| 239 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args)); | |
| 240 const std::string action = base::StringPrintf( | |
| 241 "InputMethodOptions_Open_%s", input_method_id.c_str()); | |
| 242 content::RecordComputedAction(action); | |
| 243 } | |
| 244 | |
| 245 } // namespace options2 | |
| 246 } // namespace chromeos | |
| OLD | NEW |