Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / chromeos / cros_language_options_handler.cc
blob6aa5f18b66b34634ec19baa72ac2581c7371d28f
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 "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/chromeos/input_method/input_method_util.h"
14 #include "chrome/browser/chromeos/profiles/profile_helper.h"
15 #include "chrome/browser/extensions/extension_tab_util.h"
16 #include "chrome/browser/lifetime/application_lifetime.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/browser/ui/webui/chromeos/login/l10n_util.h"
23 #include "chrome/grit/chromium_strings.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "components/user_manager/user_manager.h"
26 #include "components/user_manager/user_type.h"
27 #include "content/public/browser/navigation_controller.h"
28 #include "content/public/browser/user_metrics.h"
29 #include "content/public/browser/web_contents.h"
30 #include "extensions/browser/extension_registry.h"
31 #include "extensions/common/extension.h"
32 #include "extensions/common/manifest_url_handlers.h"
33 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
34 #include "ui/base/ime/chromeos/extension_ime_util.h"
35 #include "ui/base/ime/chromeos/input_method_manager.h"
36 #include "ui/base/l10n/l10n_util.h"
38 using base::UserMetricsAction;
40 namespace chromeos {
41 namespace options {
43 CrosLanguageOptionsHandler::CrosLanguageOptionsHandler() {
46 CrosLanguageOptionsHandler::~CrosLanguageOptionsHandler() {
49 void CrosLanguageOptionsHandler::GetLocalizedValues(
50 base::DictionaryValue* localized_strings) {
51 ::options::LanguageOptionsHandlerCommon::GetLocalizedValues(
52 localized_strings);
54 RegisterTitle(localized_strings, "languagePage",
55 IDS_OPTIONS_SETTINGS_LANGUAGES_AND_INPUT_DIALOG_TITLE);
56 localized_strings->SetString("okButton", l10n_util::GetStringUTF16(IDS_OK));
57 localized_strings->SetString("configure",
58 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_CONFIGURE));
59 localized_strings->SetString("inputMethod",
60 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD));
61 localized_strings->SetString("pleaseAddAnotherInputMethod",
62 l10n_util::GetStringUTF16(
63 IDS_OPTIONS_SETTINGS_LANGUAGES_PLEASE_ADD_ANOTHER_INPUT_METHOD));
64 localized_strings->SetString("inputMethodInstructions",
65 l10n_util::GetStringUTF16(
66 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_INSTRUCTIONS));
67 localized_strings->SetString("switchInputMethodsHint",
68 l10n_util::GetStringUTF16(
69 IDS_OPTIONS_SETTINGS_LANGUAGES_SWITCH_INPUT_METHODS_HINT));
70 localized_strings->SetString("selectPreviousInputMethodHint",
71 l10n_util::GetStringUTF16(
72 IDS_OPTIONS_SETTINGS_LANGUAGES_SELECT_PREVIOUS_INPUT_METHOD_HINT));
73 localized_strings->SetString("restartButton",
74 l10n_util::GetStringUTF16(
75 IDS_OPTIONS_SETTINGS_LANGUAGES_SIGN_OUT_BUTTON));
76 localized_strings->SetString("extensionImeLable",
77 l10n_util::GetStringUTF16(
78 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_EXTENSION_IME));
79 localized_strings->SetString("extensionImeDescription",
80 l10n_util::GetStringUTF16(
81 IDS_OPTIONS_SETTINGS_LANGUAGES_INPUT_METHOD_EXTENSION_DESCRIPTION));
82 localized_strings->SetString("noInputMethods",
83 l10n_util::GetStringUTF16(
84 IDS_OPTIONS_SETTINGS_LANGUAGES_NO_INPUT_METHODS));
86 // GetSupportedInputMethods() never returns NULL.
87 localized_strings->Set("languageList", GetAcceptLanguageList().release());
88 localized_strings->Set("inputMethodList", GetInputMethodList());
90 input_method::InputMethodManager* manager =
91 input_method::InputMethodManager::Get();
92 input_method::InputMethodDescriptors ext_ime_descriptors;
93 manager->GetActiveIMEState()->GetInputMethodExtensions(&ext_ime_descriptors);
95 base::ListValue* ext_ime_list = ConvertInputMethodDescriptorsToIMEList(
96 ext_ime_descriptors);
97 AddImeProvider(ext_ime_list);
98 localized_strings->Set("extensionImeList", ext_ime_list);
100 ComponentExtensionIMEManager* component_extension_manager =
101 input_method::InputMethodManager::Get()
102 ->GetComponentExtensionIMEManager();
103 localized_strings->Set(
104 "componentExtensionImeList",
105 ConvertInputMethodDescriptorsToIMEList(
106 component_extension_manager->GetAllIMEAsInputMethodDescriptor()));
109 void CrosLanguageOptionsHandler::RegisterMessages() {
110 ::options::LanguageOptionsHandlerCommon::RegisterMessages();
112 web_ui()->RegisterMessageCallback("inputMethodDisable",
113 base::Bind(&CrosLanguageOptionsHandler::InputMethodDisableCallback,
114 base::Unretained(this)));
115 web_ui()->RegisterMessageCallback("inputMethodEnable",
116 base::Bind(&CrosLanguageOptionsHandler::InputMethodEnableCallback,
117 base::Unretained(this)));
118 web_ui()->RegisterMessageCallback("inputMethodOptionsOpen",
119 base::Bind(&CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback,
120 base::Unretained(this)));
121 web_ui()->RegisterMessageCallback("uiLanguageRestart",
122 base::Bind(&CrosLanguageOptionsHandler::RestartCallback,
123 base::Unretained(this)));
126 // static
127 base::ListValue* CrosLanguageOptionsHandler::GetInputMethodList() {
128 input_method::InputMethodManager* manager =
129 input_method::InputMethodManager::Get();
130 // GetSupportedInputMethods() never return NULL.
131 scoped_ptr<input_method::InputMethodDescriptors> descriptors(
132 manager->GetSupportedInputMethods());
134 base::ListValue* input_method_list = new base::ListValue();
136 for (size_t i = 0; i < descriptors->size(); ++i) {
137 const input_method::InputMethodDescriptor& descriptor =
138 (*descriptors)[i];
139 const std::string display_name =
140 manager->GetInputMethodUtil()->GetInputMethodDisplayNameFromId(
141 descriptor.id());
142 base::DictionaryValue* dictionary = new base::DictionaryValue();
143 dictionary->SetString("id", descriptor.id());
144 dictionary->SetString("displayName", display_name);
146 // One input method can be associated with multiple languages, hence
147 // we use a dictionary here.
148 base::DictionaryValue* languages = new base::DictionaryValue();
149 for (size_t i = 0; i < descriptor.language_codes().size(); ++i) {
150 languages->SetBoolean(descriptor.language_codes().at(i), true);
152 dictionary->Set("languageCodeSet", languages);
154 input_method_list->Append(dictionary);
157 return input_method_list;
160 base::ListValue*
161 CrosLanguageOptionsHandler::ConvertInputMethodDescriptorsToIMEList(
162 const input_method::InputMethodDescriptors& descriptors) {
163 input_method::InputMethodUtil* util =
164 input_method::InputMethodManager::Get()->GetInputMethodUtil();
165 scoped_ptr<base::ListValue> ime_ids_list(new base::ListValue());
166 for (size_t i = 0; i < descriptors.size(); ++i) {
167 const input_method::InputMethodDescriptor& descriptor = descriptors[i];
168 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue());
169 dictionary->SetString("id", descriptor.id());
170 dictionary->SetString(
171 "displayName", util->GetLocalizedDisplayName(descriptor));
172 dictionary->SetString("optionsPage", descriptor.options_page_url().spec());
173 scoped_ptr<base::DictionaryValue> language_codes(
174 new base::DictionaryValue());
175 for (size_t i = 0; i < descriptor.language_codes().size(); ++i)
176 language_codes->SetBoolean(descriptor.language_codes().at(i), true);
177 dictionary->Set("languageCodeSet", language_codes.release());
178 ime_ids_list->Append(dictionary.release());
180 return ime_ids_list.release();
183 base::string16 CrosLanguageOptionsHandler::GetProductName() {
184 return l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME);
187 void CrosLanguageOptionsHandler::SetApplicationLocale(
188 const std::string& language_code) {
189 Profile* profile = Profile::FromWebUI(web_ui());
190 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
192 // Secondary users and public session users cannot change the locale.
193 const user_manager::User* user =
194 ProfileHelper::Get()->GetUserByProfile(profile);
195 if (user &&
196 user->email() == user_manager->GetPrimaryUser()->email() &&
197 user->GetType() != user_manager::USER_TYPE_PUBLIC_ACCOUNT) {
198 profile->ChangeAppLocale(language_code,
199 Profile::APP_LOCALE_CHANGED_VIA_SETTINGS);
203 void CrosLanguageOptionsHandler::RestartCallback(const base::ListValue* args) {
204 content::RecordAction(UserMetricsAction("LanguageOptions_SignOut"));
205 chrome::AttemptUserExit();
208 void CrosLanguageOptionsHandler::InputMethodDisableCallback(
209 const base::ListValue* args) {
210 const std::string input_method_id =
211 base::UTF16ToASCII(ExtractStringValue(args));
212 const std::string action = base::StringPrintf(
213 "LanguageOptions_DisableInputMethod_%s", input_method_id.c_str());
214 content::RecordComputedAction(action);
217 void CrosLanguageOptionsHandler::InputMethodEnableCallback(
218 const base::ListValue* args) {
219 const std::string input_method_id =
220 base::UTF16ToASCII(ExtractStringValue(args));
221 const std::string action = base::StringPrintf(
222 "LanguageOptions_EnableInputMethod_%s", input_method_id.c_str());
223 content::RecordComputedAction(action);
226 void CrosLanguageOptionsHandler::InputMethodOptionsOpenCallback(
227 const base::ListValue* args) {
228 const std::string input_method_id =
229 base::UTF16ToASCII(ExtractStringValue(args));
230 const std::string extension_id =
231 extension_ime_util::GetExtensionIDFromInputMethodID(input_method_id);
232 if (extension_id.empty())
233 return;
235 const input_method::InputMethodDescriptor* ime =
236 input_method::InputMethodManager::Get()
237 ->GetActiveIMEState()
238 ->GetInputMethodFromId(input_method_id);
239 if (!ime)
240 return;
242 Browser* browser = chrome::FindBrowserWithWebContents(
243 web_ui()->GetWebContents());
244 content::OpenURLParams params(ime->options_page_url(),
245 content::Referrer(),
246 SINGLETON_TAB,
247 ui::PAGE_TRANSITION_LINK,
248 false);
249 browser->OpenURL(params);
252 void CrosLanguageOptionsHandler::AddImeProvider(base::ListValue* list) {
253 Profile* profile = Profile::FromWebUI(web_ui());
254 const extensions::ExtensionSet& enabled_extensions =
255 extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
256 for (size_t i = 0; i < list->GetSize(); ++i) {
257 base::DictionaryValue* entry;
258 list->GetDictionary(i, &entry);
260 std::string input_method_id;
261 entry->GetString("id", &input_method_id);
263 std::string extension_id =
264 extension_ime_util::GetExtensionIDFromInputMethodID(input_method_id);
265 const extensions::Extension* extension =
266 enabled_extensions.GetByID(extension_id);
267 if (extension)
268 entry->SetString("extensionName", extension->name());
272 } // namespace options
273 } // namespace chromeos