Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / chromeos / cros_language_options_handler.cc
blobfb236883afe9c298d1c6d3f7f55e4177e55305ec
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.
5 #include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler.h"
7 #include <map>
8 #include <set>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/i18n/rtl.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/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_util.h"
20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_system.h"
22 #include "chrome/browser/extensions/extension_tab_util.h"
23 #include "chrome/browser/lifetime/application_lifetime.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_finder.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/tabs/tab_strip_model.h"
29 #include "chrome/common/extensions/manifest_url_handler.h"
30 #include "chromeos/ime/component_extension_ime_manager.h"
31 #include "chromeos/ime/extension_ime_util.h"
32 #include "chromeos/ime/input_method_manager.h"
33 #include "content/public/browser/navigation_controller.h"
34 #include "content/public/browser/user_metrics.h"
35 #include "content/public/browser/web_contents.h"
36 #include "extensions/common/extension.h"
37 #include "grit/chromium_strings.h"
38 #include "grit/generated_resources.h"
39 #include "ui/base/l10n/l10n_util.h"
41 using base::UserMetricsAction;
43 namespace {
44 // TODO(zork): Remove this blacklist when fonts are added to Chrome OS.
45 // see: crbug.com/240586
47 bool IsBlacklisted(const std::string& language_code) {
48 return language_code == "si"; // Sinhala
51 } // namespace
53 namespace chromeos {
54 namespace options {
56 CrosLanguageOptionsHandler::CrosLanguageOptionsHandler()
57 : composition_extension_appended_(false),
58 is_page_initialized_(false) {
59 input_method::InputMethodManager::Get()->GetComponentExtensionIMEManager()->
60 AddObserver(this);
63 CrosLanguageOptionsHandler::~CrosLanguageOptionsHandler() {
64 input_method::InputMethodManager::Get()->GetComponentExtensionIMEManager()->
65 RemoveObserver(this);
68 void CrosLanguageOptionsHandler::GetLocalizedValues(
69 base::DictionaryValue* localized_strings) {
70 ::options::LanguageOptionsHandlerCommon::GetLocalizedValues(
71 localized_strings);
73 RegisterTitle(localized_strings, "languagePage",
74 IDS_OPTIONS_SETTINGS_LANGUAGES_AND_INPUT_DIALOG_TITLE);
75 localized_strings->SetString("okButton", l10n_util::GetStringUTF16(IDS_OK));
76 localized_strings->SetString("configure",
77 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_CONFIGURE));
78 localized_strings->SetString("inputMethod",
79 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD));
80 localized_strings->SetString("pleaseAddAnotherInputMethod",
81 l10n_util::GetStringUTF16(
82 IDS_OPTIONS_SETTINGS_LANGUAGES_PLEASE_ADD_ANOTHER_INPUT_METHOD));
83 localized_strings->SetString("inputMethodInstructions",
84 l10n_util::GetStringUTF16(
85 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_INSTRUCTIONS));
86 localized_strings->SetString("switchInputMethodsHint",
87 l10n_util::GetStringUTF16(
88 IDS_OPTIONS_SETTINGS_LANGUAGES_SWITCH_INPUT_METHODS_HINT));
89 localized_strings->SetString("selectPreviousInputMethodHint",
90 l10n_util::GetStringUTF16(
91 IDS_OPTIONS_SETTINGS_LANGUAGES_SELECT_PREVIOUS_INPUT_METHOD_HINT));
92 localized_strings->SetString("restartButton",
93 l10n_util::GetStringUTF16(
94 IDS_OPTIONS_SETTINGS_LANGUAGES_SIGN_OUT_BUTTON));
95 localized_strings->SetString("extensionImeLable",
96 l10n_util::GetStringUTF16(
97 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_EXTENSION_IME));
98 localized_strings->SetString("extensionImeDescription",
99 l10n_util::GetStringUTF16(
100 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_EXTENSION_DESCRIPTION));
101 localized_strings->SetString("noInputMethods",
102 l10n_util::GetStringUTF16(
103 IDS_OPTIONS_SETTINGS_LANGUAGES_NO_INPUT_METHODS));
105 input_method::InputMethodManager* manager =
106 input_method::InputMethodManager::Get();
107 // GetSupportedInputMethods() never return NULL.
108 scoped_ptr<input_method::InputMethodDescriptors> descriptors(
109 manager->GetSupportedInputMethods());
110 localized_strings->Set("languageList", GetAcceptLanguageList(*descriptors));
111 localized_strings->Set("inputMethodList", GetInputMethodList(*descriptors));
113 input_method::InputMethodDescriptors ext_ime_descriptors;
114 manager->GetInputMethodExtensions(&ext_ime_descriptors);
115 localized_strings->Set("extensionImeList",
116 ConvertInputMethodDescriptosToIMEList(
117 ext_ime_descriptors));
119 ComponentExtensionIMEManager* component_extension_manager =
120 input_method::InputMethodManager::Get()
121 ->GetComponentExtensionIMEManager();
122 if (component_extension_manager->IsInitialized()) {
123 localized_strings->Set(
124 "componentExtensionImeList",
125 ConvertInputMethodDescriptosToIMEList(
126 component_extension_manager->GetAllIMEAsInputMethodDescriptor()));
127 composition_extension_appended_ = true;
128 } else {
129 // If component extension IME manager is not ready for use, it will be
130 // added in |InitializePage()|.
131 localized_strings->Set("componentExtensionImeList",
132 new base::ListValue());
136 void CrosLanguageOptionsHandler::RegisterMessages() {
137 ::options::LanguageOptionsHandlerCommon::RegisterMessages();
139 web_ui()->RegisterMessageCallback("inputMethodDisable",
140 base::Bind(&CrosLanguageOptionsHandler::InputMethodDisableCallback,
141 base::Unretained(this)));
142 web_ui()->RegisterMessageCallback("inputMethodEnable",
143 base::Bind(&CrosLanguageOptionsHandler::InputMethodEnableCallback,
144 base::Unretained(this)));
145 web_ui()->RegisterMessageCallback("inputMethodOptionsOpen",
146 base::Bind(&CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback,
147 base::Unretained(this)));
148 web_ui()->RegisterMessageCallback("uiLanguageRestart",
149 base::Bind(&CrosLanguageOptionsHandler::RestartCallback,
150 base::Unretained(this)));
153 base::ListValue* CrosLanguageOptionsHandler::GetInputMethodList(
154 const input_method::InputMethodDescriptors& descriptors) {
155 input_method::InputMethodManager* manager =
156 input_method::InputMethodManager::Get();
158 base::ListValue* input_method_list = new base::ListValue();
160 for (size_t i = 0; i < descriptors.size(); ++i) {
161 const input_method::InputMethodDescriptor& descriptor =
162 descriptors[i];
163 const std::string display_name =
164 manager->GetInputMethodUtil()->GetInputMethodDisplayNameFromId(
165 descriptor.id());
166 base::DictionaryValue* dictionary = new base::DictionaryValue();
167 dictionary->SetString("id", descriptor.id());
168 dictionary->SetString("displayName", display_name);
170 // One input method can be associated with multiple languages, hence
171 // we use a dictionary here.
172 base::DictionaryValue* languages = new base::DictionaryValue();
173 for (size_t i = 0; i < descriptor.language_codes().size(); ++i) {
174 languages->SetBoolean(descriptor.language_codes().at(i), true);
176 dictionary->Set("languageCodeSet", languages);
178 input_method_list->Append(dictionary);
181 return input_method_list;
184 // static
185 base::ListValue* CrosLanguageOptionsHandler::GetLanguageListInternal(
186 const input_method::InputMethodDescriptors& descriptors,
187 const std::vector<std::string>& base_language_codes) {
188 const std::string app_locale = g_browser_process->GetApplicationLocale();
190 std::set<std::string> language_codes;
191 // Collect the language codes from the supported input methods.
192 for (size_t i = 0; i < descriptors.size(); ++i) {
193 const input_method::InputMethodDescriptor& descriptor = descriptors[i];
194 const std::vector<std::string>& languages =
195 descriptor.language_codes();
196 for (size_t i = 0; i < languages.size(); ++i)
197 language_codes.insert(languages[i]);
200 // Map of display name -> {language code, native_display_name}.
201 // In theory, we should be able to create a map that is sorted by
202 // display names using ICU comparator, but doing it is hard, thus we'll
203 // use an auxiliary vector to achieve the same result.
204 typedef std::pair<std::string, base::string16> LanguagePair;
205 typedef std::map<base::string16, LanguagePair> LanguageMap;
206 LanguageMap language_map;
207 // The auxiliary vector mentioned above.
208 std::vector<base::string16> display_names;
210 // Build the list of display names, and build the language map.
211 for (std::set<std::string>::const_iterator iter = language_codes.begin();
212 iter != language_codes.end(); ++iter) {
213 // Exclude the language which is not in |base_langauge_codes| even it has
214 // input methods.
215 if (std::find(base_language_codes.begin(),
216 base_language_codes.end(),
217 *iter) == base_language_codes.end()) {
218 continue;
221 const base::string16 display_name =
222 l10n_util::GetDisplayNameForLocale(*iter, app_locale, true);
223 const base::string16 native_display_name =
224 l10n_util::GetDisplayNameForLocale(*iter, *iter, true);
226 display_names.push_back(display_name);
227 language_map[display_name] =
228 std::make_pair(*iter, native_display_name);
230 DCHECK_EQ(display_names.size(), language_map.size());
232 // Build the list of display names, and build the language map.
233 for (size_t i = 0; i < base_language_codes.size(); ++i) {
234 // Skip this language if it was already added.
235 if (language_codes.find(base_language_codes[i]) != language_codes.end())
236 continue;
238 // TODO(zork): Remove this blacklist when fonts are added to Chrome OS.
239 // see: crbug.com/240586
240 if (IsBlacklisted(base_language_codes[i]))
241 continue;
243 base::string16 display_name =
244 l10n_util::GetDisplayNameForLocale(
245 base_language_codes[i], app_locale, false);
246 base::string16 native_display_name =
247 l10n_util::GetDisplayNameForLocale(
248 base_language_codes[i], base_language_codes[i], false);
249 display_names.push_back(display_name);
250 language_map[display_name] =
251 std::make_pair(base_language_codes[i], native_display_name);
254 // Sort display names using locale specific sorter.
255 l10n_util::SortStrings16(app_locale, &display_names);
257 // Build the language list from the language map.
258 base::ListValue* language_list = new base::ListValue();
259 for (size_t i = 0; i < display_names.size(); ++i) {
260 // Sets the directionality of the display language name.
261 base::string16 display_name(display_names[i]);
262 bool markup_removal =
263 base::i18n::UnadjustStringForLocaleDirection(&display_name);
264 DCHECK(markup_removal);
265 bool has_rtl_chars = base::i18n::StringContainsStrongRTLChars(display_name);
266 std::string directionality = has_rtl_chars ? "rtl" : "ltr";
268 const LanguagePair& pair = language_map[display_names[i]];
269 base::DictionaryValue* dictionary = new base::DictionaryValue();
270 dictionary->SetString("code", pair.first);
271 dictionary->SetString("displayName", display_names[i]);
272 dictionary->SetString("textDirection", directionality);
273 dictionary->SetString("nativeDisplayName", pair.second);
274 language_list->Append(dictionary);
277 return language_list;
280 // static
281 base::ListValue* CrosLanguageOptionsHandler::GetAcceptLanguageList(
282 const input_method::InputMethodDescriptors& descriptors) {
283 // Collect the language codes from the supported accept-languages.
284 const std::string app_locale = g_browser_process->GetApplicationLocale();
285 std::vector<std::string> accept_language_codes;
286 l10n_util::GetAcceptLanguagesForLocale(app_locale, &accept_language_codes);
287 return GetLanguageListInternal(descriptors, accept_language_codes);
290 // static
291 base::ListValue* CrosLanguageOptionsHandler::GetUILanguageList(
292 const input_method::InputMethodDescriptors& descriptors) {
293 // Collect the language codes from the available locales.
294 return GetLanguageListInternal(descriptors, l10n_util::GetAvailableLocales());
297 base::ListValue*
298 CrosLanguageOptionsHandler::ConvertInputMethodDescriptosToIMEList(
299 const input_method::InputMethodDescriptors& descriptors) {
300 scoped_ptr<base::ListValue> ime_ids_list(new base::ListValue());
301 for (size_t i = 0; i < descriptors.size(); ++i) {
302 const input_method::InputMethodDescriptor& descriptor = descriptors[i];
303 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue());
304 dictionary->SetString("id", descriptor.id());
305 dictionary->SetString("displayName", descriptor.name());
306 dictionary->SetString("optionsPage", descriptor.options_page_url().spec());
307 scoped_ptr<base::DictionaryValue> language_codes(
308 new base::DictionaryValue());
309 for (size_t i = 0; i < descriptor.language_codes().size(); ++i)
310 language_codes->SetBoolean(descriptor.language_codes().at(i), true);
311 dictionary->Set("languageCodeSet", language_codes.release());
312 ime_ids_list->Append(dictionary.release());
314 return ime_ids_list.release();
317 base::string16 CrosLanguageOptionsHandler::GetProductName() {
318 return l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME);
321 void CrosLanguageOptionsHandler::SetApplicationLocale(
322 const std::string& language_code) {
323 Profile::FromWebUI(web_ui())->ChangeAppLocale(
324 language_code, Profile::APP_LOCALE_CHANGED_VIA_SETTINGS);
327 void CrosLanguageOptionsHandler::RestartCallback(const base::ListValue* args) {
328 content::RecordAction(UserMetricsAction("LanguageOptions_SignOut"));
329 chrome::AttemptUserExit();
332 void CrosLanguageOptionsHandler::InputMethodDisableCallback(
333 const base::ListValue* args) {
334 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args));
335 const std::string action = base::StringPrintf(
336 "LanguageOptions_DisableInputMethod_%s", input_method_id.c_str());
337 content::RecordComputedAction(action);
340 void CrosLanguageOptionsHandler::InputMethodEnableCallback(
341 const base::ListValue* args) {
342 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args));
343 const std::string action = base::StringPrintf(
344 "LanguageOptions_EnableInputMethod_%s", input_method_id.c_str());
345 content::RecordComputedAction(action);
348 void CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback(
349 const base::ListValue* args) {
350 const std::string input_method_id = UTF16ToASCII(ExtractStringValue(args));
351 const std::string action = base::StringPrintf(
352 "InputMethodOptions_Open_%s", input_method_id.c_str());
353 content::RecordComputedAction(action);
355 const std::string extension_id =
356 extension_ime_util::GetExtensionIDFromInputMethodID(input_method_id);
357 if (extension_id.empty())
358 return;
360 const input_method::InputMethodDescriptor* ime =
361 input_method::InputMethodManager::Get()->GetInputMethodFromId(
362 input_method_id);
363 if (!ime)
364 return;
366 Browser* browser = chrome::FindBrowserWithWebContents(
367 web_ui()->GetWebContents());
368 content::OpenURLParams params(ime->options_page_url(),
369 content::Referrer(),
370 SINGLETON_TAB,
371 content::PAGE_TRANSITION_LINK,
372 false);
373 browser->OpenURL(params);
374 browser->window()->Show();
375 content::WebContents* web_contents =
376 browser->tab_strip_model()->GetActiveWebContents();
377 web_contents->GetDelegate()->ActivateContents(web_contents);
380 void CrosLanguageOptionsHandler::OnInitialized() {
381 if (composition_extension_appended_ || !is_page_initialized_) {
382 // If an option page is not ready to call JavaScript, appending component
383 // extension IMEs will be done in InitializePage function later.
384 return;
387 ComponentExtensionIMEManager* manager =
388 input_method::InputMethodManager::Get()
389 ->GetComponentExtensionIMEManager();
391 DCHECK(manager->IsInitialized());
392 scoped_ptr<base::ListValue> ime_list(
393 ConvertInputMethodDescriptosToIMEList(
394 manager->GetAllIMEAsInputMethodDescriptor()));
395 web_ui()->CallJavascriptFunction(
396 "options.LanguageOptions.onComponentManagerInitialized",
397 *ime_list);
398 composition_extension_appended_ = true;
401 void CrosLanguageOptionsHandler::InitializePage() {
402 is_page_initialized_ = true;
403 if (composition_extension_appended_)
404 return;
406 ComponentExtensionIMEManager* component_extension_manager =
407 input_method::InputMethodManager::Get()
408 ->GetComponentExtensionIMEManager();
409 if (!component_extension_manager->IsInitialized()) {
410 // If the component extension IME manager is not available yet, append the
411 // component extension list in |OnInitialized()|.
412 return;
415 scoped_ptr<base::ListValue> ime_list(
416 ConvertInputMethodDescriptosToIMEList(
417 component_extension_manager->GetAllIMEAsInputMethodDescriptor()));
418 web_ui()->CallJavascriptFunction(
419 "options.LanguageOptions.onComponentManagerInitialized",
420 *ime_list);
421 composition_extension_appended_ = true;
424 } // namespace options
425 } // namespace chromeos