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"
13 #include "base/bind.h"
14 #include "base/bind_helpers.h"
15 #include "base/i18n/rtl.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h"
19 #include "chrome/app/chrome_command_ids.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chromeos/customization_document.h"
22 #include "chrome/browser/chromeos/input_method/input_method_util.h"
23 #include "chrome/browser/chromeos/login/user_manager.h"
24 #include "chrome/browser/extensions/extension_service.h"
25 #include "chrome/browser/extensions/extension_tab_util.h"
26 #include "chrome/browser/lifetime/application_lifetime.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_finder.h"
30 #include "chrome/browser/ui/browser_window.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/extensions/manifest_url_handler.h"
33 #include "chromeos/ime/component_extension_ime_manager.h"
34 #include "chromeos/ime/extension_ime_util.h"
35 #include "chromeos/ime/input_method_manager.h"
36 #include "content/public/browser/navigation_controller.h"
37 #include "content/public/browser/user_metrics.h"
38 #include "content/public/browser/web_contents.h"
39 #include "extensions/browser/extension_system.h"
40 #include "extensions/common/extension.h"
41 #include "grit/chromium_strings.h"
42 #include "grit/generated_resources.h"
43 #include "ui/base/l10n/l10n_util.h"
45 using base::UserMetricsAction
;
48 // TODO(zork): Remove this blacklist when fonts are added to Chrome OS.
49 // see: crbug.com/240586
51 bool IsBlacklisted(const std::string
& language_code
) {
52 return language_code
== "si"; // Sinhala
60 const char kVendorOtherLanguagesListDivider
[] =
61 "VENDOR_OTHER_LANGUAGES_LIST_DIVIDER";
63 CrosLanguageOptionsHandler::CrosLanguageOptionsHandler()
64 : composition_extension_appended_(false),
65 is_page_initialized_(false) {
66 input_method::InputMethodManager::Get()->GetComponentExtensionIMEManager()->
70 CrosLanguageOptionsHandler::~CrosLanguageOptionsHandler() {
71 input_method::InputMethodManager::Get()->GetComponentExtensionIMEManager()->
75 void CrosLanguageOptionsHandler::GetLocalizedValues(
76 base::DictionaryValue
* localized_strings
) {
77 ::options::LanguageOptionsHandlerCommon::GetLocalizedValues(
80 RegisterTitle(localized_strings
, "languagePage",
81 IDS_OPTIONS_SETTINGS_LANGUAGES_AND_INPUT_DIALOG_TITLE
);
82 localized_strings
->SetString("okButton", l10n_util::GetStringUTF16(IDS_OK
));
83 localized_strings
->SetString("configure",
84 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_CONFIGURE
));
85 localized_strings
->SetString("inputMethod",
86 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD
));
87 localized_strings
->SetString("pleaseAddAnotherInputMethod",
88 l10n_util::GetStringUTF16(
89 IDS_OPTIONS_SETTINGS_LANGUAGES_PLEASE_ADD_ANOTHER_INPUT_METHOD
));
90 localized_strings
->SetString("inputMethodInstructions",
91 l10n_util::GetStringUTF16(
92 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_INSTRUCTIONS
));
93 localized_strings
->SetString("switchInputMethodsHint",
94 l10n_util::GetStringUTF16(
95 IDS_OPTIONS_SETTINGS_LANGUAGES_SWITCH_INPUT_METHODS_HINT
));
96 localized_strings
->SetString("selectPreviousInputMethodHint",
97 l10n_util::GetStringUTF16(
98 IDS_OPTIONS_SETTINGS_LANGUAGES_SELECT_PREVIOUS_INPUT_METHOD_HINT
));
99 localized_strings
->SetString("restartButton",
100 l10n_util::GetStringUTF16(
101 IDS_OPTIONS_SETTINGS_LANGUAGES_SIGN_OUT_BUTTON
));
102 localized_strings
->SetString("extensionImeLable",
103 l10n_util::GetStringUTF16(
104 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_EXTENSION_IME
));
105 localized_strings
->SetString("extensionImeDescription",
106 l10n_util::GetStringUTF16(
107 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_EXTENSION_DESCRIPTION
));
108 localized_strings
->SetString("noInputMethods",
109 l10n_util::GetStringUTF16(
110 IDS_OPTIONS_SETTINGS_LANGUAGES_NO_INPUT_METHODS
));
112 input_method::InputMethodManager
* manager
=
113 input_method::InputMethodManager::Get();
114 // GetSupportedInputMethods() never return NULL.
115 scoped_ptr
<input_method::InputMethodDescriptors
> descriptors(
116 manager
->GetSupportedInputMethods());
117 localized_strings
->Set("languageList", GetAcceptLanguageList(*descriptors
));
118 localized_strings
->Set("inputMethodList", GetInputMethodList(*descriptors
));
120 input_method::InputMethodDescriptors ext_ime_descriptors
;
121 manager
->GetInputMethodExtensions(&ext_ime_descriptors
);
122 localized_strings
->Set("extensionImeList",
123 ConvertInputMethodDescriptosToIMEList(
124 ext_ime_descriptors
));
126 ComponentExtensionIMEManager
* component_extension_manager
=
127 input_method::InputMethodManager::Get()
128 ->GetComponentExtensionIMEManager();
129 if (component_extension_manager
->IsInitialized()) {
130 localized_strings
->Set(
131 "componentExtensionImeList",
132 ConvertInputMethodDescriptosToIMEList(
133 component_extension_manager
->GetAllIMEAsInputMethodDescriptor()));
134 composition_extension_appended_
= true;
136 // If component extension IME manager is not ready for use, it will be
137 // added in |InitializePage()|.
138 localized_strings
->Set("componentExtensionImeList",
139 new base::ListValue());
143 void CrosLanguageOptionsHandler::RegisterMessages() {
144 ::options::LanguageOptionsHandlerCommon::RegisterMessages();
146 web_ui()->RegisterMessageCallback("inputMethodDisable",
147 base::Bind(&CrosLanguageOptionsHandler::InputMethodDisableCallback
,
148 base::Unretained(this)));
149 web_ui()->RegisterMessageCallback("inputMethodEnable",
150 base::Bind(&CrosLanguageOptionsHandler::InputMethodEnableCallback
,
151 base::Unretained(this)));
152 web_ui()->RegisterMessageCallback("inputMethodOptionsOpen",
153 base::Bind(&CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback
,
154 base::Unretained(this)));
155 web_ui()->RegisterMessageCallback("uiLanguageRestart",
156 base::Bind(&CrosLanguageOptionsHandler::RestartCallback
,
157 base::Unretained(this)));
160 base::ListValue
* CrosLanguageOptionsHandler::GetInputMethodList(
161 const input_method::InputMethodDescriptors
& descriptors
) {
162 input_method::InputMethodManager
* manager
=
163 input_method::InputMethodManager::Get();
165 base::ListValue
* input_method_list
= new base::ListValue();
167 for (size_t i
= 0; i
< descriptors
.size(); ++i
) {
168 const input_method::InputMethodDescriptor
& descriptor
=
170 const std::string display_name
=
171 manager
->GetInputMethodUtil()->GetInputMethodDisplayNameFromId(
173 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
174 dictionary
->SetString("id", descriptor
.id());
175 dictionary
->SetString("displayName", display_name
);
177 // One input method can be associated with multiple languages, hence
178 // we use a dictionary here.
179 base::DictionaryValue
* languages
= new base::DictionaryValue();
180 for (size_t i
= 0; i
< descriptor
.language_codes().size(); ++i
) {
181 languages
->SetBoolean(descriptor
.language_codes().at(i
), true);
183 dictionary
->Set("languageCodeSet", languages
);
185 input_method_list
->Append(dictionary
);
188 return input_method_list
;
192 base::ListValue
* CrosLanguageOptionsHandler::GetLanguageListInternal(
193 const input_method::InputMethodDescriptors
& descriptors
,
194 const std::vector
<std::string
>& base_language_codes
,
195 const bool insert_divider
) {
196 const std::string app_locale
= g_browser_process
->GetApplicationLocale();
198 std::set
<std::string
> language_codes
;
199 // Collect the language codes from the supported input methods.
200 for (size_t i
= 0; i
< descriptors
.size(); ++i
) {
201 const input_method::InputMethodDescriptor
& descriptor
= descriptors
[i
];
202 const std::vector
<std::string
>& languages
=
203 descriptor
.language_codes();
204 for (size_t i
= 0; i
< languages
.size(); ++i
)
205 language_codes
.insert(languages
[i
]);
208 const StartupCustomizationDocument
* startup_manifest
=
209 StartupCustomizationDocument::GetInstance();
211 const std::vector
<std::string
>& configured_locales
=
212 startup_manifest
->configured_locales();
214 // Languages sort order.
215 std::map
<std::string
, int /* index */> language_index
;
216 for (size_t i
= 0; i
< configured_locales
.size(); ++i
) {
217 language_index
[configured_locales
[i
]] = i
;
220 // Map of display name -> {language code, native_display_name}.
221 // In theory, we should be able to create a map that is sorted by
222 // display names using ICU comparator, but doing it is hard, thus we'll
223 // use an auxiliary vector to achieve the same result.
224 typedef std::pair
<std::string
, base::string16
> LanguagePair
;
225 typedef std::map
<base::string16
, LanguagePair
> LanguageMap
;
226 LanguageMap language_map
;
228 // The auxiliary vector mentioned above. (except vendor locales)
229 std::vector
<base::string16
> display_names
;
231 // Separate vector of vendor locales.
232 std::vector
<base::string16
> configured_locales_display_names(
233 configured_locales
.size());
235 size_t configured_locales_count
= 0;
237 // Build the list of display names, and build the language map.
239 // The list of configured locales might have entries not in
240 // base_language_codes. If there are unsupported language variants,
241 // but they resolve to backup locale within base_language_codes, also
242 // add them to the list.
243 for (std::map
<std::string
, int>::const_iterator iter
= language_index
.begin();
244 iter
!= language_index
.end();
246 const std::string
& language_id
= iter
->first
;
247 const int language_idx
= iter
->second
;
249 const size_t dash_pos
= language_id
.find_first_of('-');
251 // Ignore non-specific codes.
252 if (dash_pos
== std::string::npos
|| dash_pos
== 0)
255 if (std::find(base_language_codes
.begin(),
256 base_language_codes
.end(),
257 language_id
) != base_language_codes
.end()) {
258 // Language is supported. No need to replace
261 std::string resolved_locale
;
262 if (!l10n_util::CheckAndResolveLocale(language_id
, &resolved_locale
))
265 if (std::find(base_language_codes
.begin(),
266 base_language_codes
.end(),
267 resolved_locale
) == base_language_codes
.end()) {
268 // Resolved locale is not supported.
272 const base::string16 display_name
=
273 l10n_util::GetDisplayNameForLocale(language_id
, app_locale
, true);
274 const base::string16 native_display_name
=
275 l10n_util::GetDisplayNameForLocale(
276 language_id
, language_id
, true);
278 language_map
[display_name
] =
279 std::make_pair(language_id
, native_display_name
);
281 configured_locales_display_names
[language_idx
] = display_name
;
282 ++configured_locales_count
;
285 // Translate language codes, generated from input methods.
286 for (std::set
<std::string
>::const_iterator iter
= language_codes
.begin();
287 iter
!= language_codes
.end(); ++iter
) {
288 // Exclude the language which is not in |base_langauge_codes| even it has
290 if (std::find(base_language_codes
.begin(),
291 base_language_codes
.end(),
292 *iter
) == base_language_codes
.end()) {
296 const base::string16 display_name
=
297 l10n_util::GetDisplayNameForLocale(*iter
, app_locale
, true);
298 const base::string16 native_display_name
=
299 l10n_util::GetDisplayNameForLocale(*iter
, *iter
, true);
301 language_map
[display_name
] =
302 std::make_pair(*iter
, native_display_name
);
304 const std::map
<std::string
, int>::const_iterator index_pos
=
305 language_index
.find(*iter
);
306 if (index_pos
!= language_index
.end()) {
307 base::string16
& stored_display_name
=
308 configured_locales_display_names
[index_pos
->second
];
309 if (stored_display_name
.empty()) {
310 stored_display_name
= display_name
;
311 ++configured_locales_count
;
314 display_names
.push_back(display_name
);
317 DCHECK_EQ(display_names
.size() + configured_locales_count
,
318 language_map
.size());
320 // Build the list of display names, and build the language map.
321 for (size_t i
= 0; i
< base_language_codes
.size(); ++i
) {
322 // Skip this language if it was already added.
323 if (language_codes
.find(base_language_codes
[i
]) != language_codes
.end())
326 // TODO(zork): Remove this blacklist when fonts are added to Chrome OS.
327 // see: crbug.com/240586
328 if (IsBlacklisted(base_language_codes
[i
]))
331 base::string16 display_name
=
332 l10n_util::GetDisplayNameForLocale(
333 base_language_codes
[i
], app_locale
, false);
334 base::string16 native_display_name
=
335 l10n_util::GetDisplayNameForLocale(
336 base_language_codes
[i
], base_language_codes
[i
], false);
337 language_map
[display_name
] =
338 std::make_pair(base_language_codes
[i
], native_display_name
);
340 const std::map
<std::string
, int>::const_iterator index_pos
=
341 language_index
.find(base_language_codes
[i
]);
342 if (index_pos
!= language_index
.end()) {
343 configured_locales_display_names
[index_pos
->second
] = display_name
;
344 ++configured_locales_count
;
346 display_names
.push_back(display_name
);
350 // Sort display names using locale specific sorter.
351 l10n_util::SortStrings16(app_locale
, &display_names
);
352 // Concatenate configured_locales_display_names and display_names.
353 // Insert special divider in between.
354 std::vector
<base::string16
> out_display_names
;
355 for (size_t i
= 0; i
< configured_locales_display_names
.size(); ++i
) {
356 if (configured_locales_display_names
[i
].size() == 0)
358 out_display_names
.push_back(configured_locales_display_names
[i
]);
361 base::string16 divider16
;
362 if (insert_divider
) {
363 divider16
= base::ASCIIToUTF16(
364 insert_divider
? "" : kVendorOtherLanguagesListDivider
);
365 out_display_names
.push_back(divider16
);
368 std::copy(display_names
.begin(),
370 std::back_inserter(out_display_names
));
372 // Build the language list from the language map.
373 base::ListValue
* language_list
= new base::ListValue();
374 for (size_t i
= 0; i
< out_display_names
.size(); ++i
) {
375 // Sets the directionality of the display language name.
376 base::string16
display_name(out_display_names
[i
]);
377 if (insert_divider
&& display_name
== divider16
) {
379 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
380 dictionary
->SetString("code", kVendorOtherLanguagesListDivider
);
381 language_list
->Append(dictionary
);
384 bool markup_removal
=
385 base::i18n::UnadjustStringForLocaleDirection(&display_name
);
386 DCHECK(markup_removal
);
387 bool has_rtl_chars
= base::i18n::StringContainsStrongRTLChars(display_name
);
388 std::string directionality
= has_rtl_chars
? "rtl" : "ltr";
390 const LanguagePair
& pair
= language_map
[out_display_names
[i
]];
391 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
392 dictionary
->SetString("code", pair
.first
);
393 dictionary
->SetString("displayName", out_display_names
[i
]);
394 dictionary
->SetString("textDirection", directionality
);
395 dictionary
->SetString("nativeDisplayName", pair
.second
);
396 language_list
->Append(dictionary
);
399 return language_list
;
403 base::ListValue
* CrosLanguageOptionsHandler::GetAcceptLanguageList(
404 const input_method::InputMethodDescriptors
& descriptors
) {
405 // Collect the language codes from the supported accept-languages.
406 const std::string app_locale
= g_browser_process
->GetApplicationLocale();
407 std::vector
<std::string
> accept_language_codes
;
408 l10n_util::GetAcceptLanguagesForLocale(app_locale
, &accept_language_codes
);
409 return GetLanguageListInternal(descriptors
, accept_language_codes
, false);
413 base::ListValue
* CrosLanguageOptionsHandler::GetUILanguageList(
414 const input_method::InputMethodDescriptors
& descriptors
) {
415 // Collect the language codes from the available locales.
416 return GetLanguageListInternal(
417 descriptors
, l10n_util::GetAvailableLocales(), true);
421 CrosLanguageOptionsHandler::ConvertInputMethodDescriptosToIMEList(
422 const input_method::InputMethodDescriptors
& descriptors
) {
423 scoped_ptr
<base::ListValue
> ime_ids_list(new base::ListValue());
424 for (size_t i
= 0; i
< descriptors
.size(); ++i
) {
425 const input_method::InputMethodDescriptor
& descriptor
= descriptors
[i
];
426 scoped_ptr
<base::DictionaryValue
> dictionary(new base::DictionaryValue());
427 dictionary
->SetString("id", descriptor
.id());
428 dictionary
->SetString("displayName", descriptor
.name());
429 dictionary
->SetString("optionsPage", descriptor
.options_page_url().spec());
430 scoped_ptr
<base::DictionaryValue
> language_codes(
431 new base::DictionaryValue());
432 for (size_t i
= 0; i
< descriptor
.language_codes().size(); ++i
)
433 language_codes
->SetBoolean(descriptor
.language_codes().at(i
), true);
434 dictionary
->Set("languageCodeSet", language_codes
.release());
435 ime_ids_list
->Append(dictionary
.release());
437 return ime_ids_list
.release();
440 base::string16
CrosLanguageOptionsHandler::GetProductName() {
441 return l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME
);
444 void CrosLanguageOptionsHandler::SetApplicationLocale(
445 const std::string
& language_code
) {
446 Profile
* profile
= Profile::FromWebUI(web_ui());
447 UserManager
* user_manager
= UserManager::Get();
449 // Only the primary user can change the locale.
450 User
* user
= user_manager
->GetUserByProfile(profile
);
451 if (user
&& user
->email() == user_manager
->GetPrimaryUser()->email()) {
452 profile
->ChangeAppLocale(language_code
,
453 Profile::APP_LOCALE_CHANGED_VIA_SETTINGS
);
457 void CrosLanguageOptionsHandler::RestartCallback(const base::ListValue
* args
) {
458 content::RecordAction(UserMetricsAction("LanguageOptions_SignOut"));
459 chrome::AttemptUserExit();
462 void CrosLanguageOptionsHandler::InputMethodDisableCallback(
463 const base::ListValue
* args
) {
464 const std::string input_method_id
=
465 base::UTF16ToASCII(ExtractStringValue(args
));
466 const std::string action
= base::StringPrintf(
467 "LanguageOptions_DisableInputMethod_%s", input_method_id
.c_str());
468 content::RecordComputedAction(action
);
471 void CrosLanguageOptionsHandler::InputMethodEnableCallback(
472 const base::ListValue
* args
) {
473 const std::string input_method_id
=
474 base::UTF16ToASCII(ExtractStringValue(args
));
475 const std::string action
= base::StringPrintf(
476 "LanguageOptions_EnableInputMethod_%s", input_method_id
.c_str());
477 content::RecordComputedAction(action
);
480 void CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback(
481 const base::ListValue
* args
) {
482 const std::string input_method_id
=
483 base::UTF16ToASCII(ExtractStringValue(args
));
484 const std::string extension_id
=
485 extension_ime_util::GetExtensionIDFromInputMethodID(input_method_id
);
486 if (extension_id
.empty())
489 const input_method::InputMethodDescriptor
* ime
=
490 input_method::InputMethodManager::Get()->GetInputMethodFromId(
495 Browser
* browser
= chrome::FindBrowserWithWebContents(
496 web_ui()->GetWebContents());
497 content::OpenURLParams
params(ime
->options_page_url(),
500 content::PAGE_TRANSITION_LINK
,
502 browser
->OpenURL(params
);
503 browser
->window()->Show();
504 content::WebContents
* web_contents
=
505 browser
->tab_strip_model()->GetActiveWebContents();
506 web_contents
->GetDelegate()->ActivateContents(web_contents
);
509 void CrosLanguageOptionsHandler::OnImeComponentExtensionInitialized() {
510 if (composition_extension_appended_
|| !is_page_initialized_
) {
511 // If an option page is not ready to call JavaScript, appending component
512 // extension IMEs will be done in InitializePage function later.
516 ComponentExtensionIMEManager
* manager
=
517 input_method::InputMethodManager::Get()
518 ->GetComponentExtensionIMEManager();
520 DCHECK(manager
->IsInitialized());
521 scoped_ptr
<base::ListValue
> ime_list(
522 ConvertInputMethodDescriptosToIMEList(
523 manager
->GetAllIMEAsInputMethodDescriptor()));
524 web_ui()->CallJavascriptFunction(
525 "options.LanguageOptions.onComponentManagerInitialized",
527 composition_extension_appended_
= true;
530 void CrosLanguageOptionsHandler::InitializePage() {
531 is_page_initialized_
= true;
532 if (composition_extension_appended_
)
535 ComponentExtensionIMEManager
* component_extension_manager
=
536 input_method::InputMethodManager::Get()
537 ->GetComponentExtensionIMEManager();
538 if (!component_extension_manager
->IsInitialized()) {
539 // If the component extension IME manager is not available yet, append the
540 // component extension list in |OnInitialized()|.
544 scoped_ptr
<base::ListValue
> ime_list(
545 ConvertInputMethodDescriptosToIMEList(
546 component_extension_manager
->GetAllIMEAsInputMethodDescriptor()));
547 web_ui()->CallJavascriptFunction(
548 "options.LanguageOptions.onComponentManagerInitialized",
550 composition_extension_appended_
= true;
553 } // namespace options
554 } // namespace chromeos