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