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