Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / language_options_handler_common.cc
blobd4599f6c5808df2b5010203b3bd3851325c7cabc
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/language_options_handler_common.h"
7 #include <map>
8 #include <string>
9 #include <utility>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/command_line.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/values.h"
21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/spellchecker/spellcheck_factory.h"
24 #include "chrome/browser/spellchecker/spellcheck_service.h"
25 #include "chrome/browser/translate/chrome_translate_client.h"
26 #include "chrome/browser/translate/translate_service.h"
27 #include "chrome/browser/ui/browser_list.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h"
30 #include "chrome/common/spellcheck_common.h"
31 #include "chrome/grit/chromium_strings.h"
32 #include "chrome/grit/generated_resources.h"
33 #include "components/translate/core/browser/translate_download_manager.h"
34 #include "components/translate/core/browser/translate_prefs.h"
35 #include "content/public/browser/user_metrics.h"
36 #include "content/public/browser/web_ui.h"
37 #include "ui/base/l10n/l10n_util.h"
39 using base::UserMetricsAction;
41 namespace options {
43 LanguageOptionsHandlerCommon::LanguageOptionsHandlerCommon() {
46 LanguageOptionsHandlerCommon::~LanguageOptionsHandlerCommon() {
49 void LanguageOptionsHandlerCommon::GetLocalizedValues(
50 base::DictionaryValue* localized_strings) {
51 DCHECK(localized_strings);
53 #if defined(OS_CHROMEOS)
54 const int product_id = IDS_PRODUCT_OS_NAME;
55 #else
56 const int product_id = IDS_PRODUCT_NAME;
57 #endif
59 static OptionsStringResource resources[] = {
60 { "addButton", IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_BUTTON },
61 { "languages", IDS_OPTIONS_SETTINGS_LANGUAGES_LANGUAGES },
62 { "addLanguageInstructions",
63 IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_LANGUAGE_INSTRUCTIONS },
64 { "cannotBeDisplayedInThisLanguage",
65 IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
66 product_id },
67 { "isDisplayedInThisLanguage",
68 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
69 product_id },
70 { "displayInThisLanguage",
71 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
72 product_id },
73 { "restartRequired", IDS_OPTIONS_RELAUNCH_REQUIRED },
74 // OS X uses the OS native spellchecker so no need for these strings.
75 #if !defined(OS_MACOSX)
76 { "useThisForSpellChecking",
77 IDS_OPTIONS_SETTINGS_USE_THIS_FOR_SPELL_CHECKING },
78 { "cannotBeUsedForSpellChecking",
79 IDS_OPTIONS_SETTINGS_CANNOT_BE_USED_FOR_SPELL_CHECKING },
80 { "isUsedForSpellChecking",
81 IDS_OPTIONS_SETTINGS_IS_USED_FOR_SPELL_CHECKING },
82 { "enableSpellCheck", IDS_OPTIONS_ENABLE_SPELLCHECK },
83 { "enableAutoSpellCorrection",
84 IDS_OPTIONS_ENABLE_AUTO_SPELL_CORRECTION },
85 { "downloadingDictionary", IDS_OPTIONS_DICTIONARY_DOWNLOADING },
86 { "downloadFailed", IDS_OPTIONS_DICTIONARY_DOWNLOAD_FAILED },
87 { "retryButton", IDS_OPTIONS_DICTIONARY_DOWNLOAD_RETRY },
88 { "downloadFailHelp", IDS_OPTIONS_DICTIONARY_DOWNLOAD_FAIL_HELP },
89 #endif // !OS_MACOSX
90 { "addLanguageTitle", IDS_OPTIONS_LANGUAGES_ADD_TITLE },
91 { "addLanguageSelectLabel", IDS_OPTIONS_LANGUAGES_ADD_SELECT_LABEL },
92 { "restartButton", IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON },
93 { "offerToTranslateInThisLanguage",
94 IDS_OPTIONS_LANGUAGES_OFFER_TO_TRANSLATE_IN_THIS_LANGUAGE },
95 { "cannotTranslateInThisLanguage",
96 IDS_OPTIONS_LANGUAGES_CANNOT_TRANSLATE_IN_THIS_LANGUAGE },
99 #if defined(ENABLE_SETTINGS_APP)
100 static OptionsStringResource app_resources[] = {
101 { "cannotBeDisplayedInThisLanguage",
102 IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
103 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
104 { "isDisplayedInThisLanguage",
105 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
106 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
107 { "displayInThisLanguage",
108 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
109 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
111 base::DictionaryValue* app_values = NULL;
112 CHECK(localized_strings->GetDictionary(kSettingsAppKey, &app_values));
113 RegisterStrings(app_values, app_resources, arraysize(app_resources));
114 #endif
116 RegisterStrings(localized_strings, resources, arraysize(resources));
118 // The following are resources, rather than local strings.
119 std::string application_locale = g_browser_process->GetApplicationLocale();
120 localized_strings->SetString("currentUiLanguageCode", application_locale);
121 std::string prospective_locale =
122 g_browser_process->local_state()->GetString(prefs::kApplicationLocale);
123 localized_strings->SetString("prospectiveUiLanguageCode",
124 !prospective_locale.empty() ? prospective_locale : application_locale);
125 localized_strings->Set("spellCheckLanguageCodeSet",
126 GetSpellCheckLanguageCodeSet());
127 localized_strings->Set("uiLanguageCodeSet", GetUILanguageCodeSet());
129 const base::CommandLine& command_line =
130 *base::CommandLine::ForCurrentProcess();
131 bool enable_spelling_auto_correct =
132 command_line.HasSwitch(switches::kEnableSpellingAutoCorrect);
133 localized_strings->SetBoolean("enableSpellingAutoCorrect",
134 enable_spelling_auto_correct);
136 localized_strings->SetBoolean(
137 "enableMultilingualSpellChecker",
138 chrome::spellcheck_common::IsMultilingualSpellcheckEnabled());
140 Profile* profile = Profile::FromWebUI(web_ui());
141 PrefService* prefs = profile->GetPrefs();
142 std::string default_target_language =
143 TranslateService::GetTargetLanguage(prefs);
144 localized_strings->SetString("defaultTargetLanguage",
145 default_target_language);
147 std::vector<std::string> languages;
148 translate::TranslateDownloadManager::GetSupportedLanguages(&languages);
150 base::ListValue* languages_list = new base::ListValue();
151 for (std::vector<std::string>::iterator it = languages.begin();
152 it != languages.end(); ++it) {
153 languages_list->Append(new base::StringValue(*it));
156 localized_strings->Set("translateSupportedLanguages", languages_list);
159 void LanguageOptionsHandlerCommon::Uninitialize() {
160 SpellcheckService* service = GetSpellcheckService();
161 if (!service)
162 return;
164 for (auto dict : service->GetHunspellDictionaries())
165 dict->RemoveObserver(this);
168 void LanguageOptionsHandlerCommon::RegisterMessages() {
169 web_ui()->RegisterMessageCallback("languageOptionsOpen",
170 base::Bind(
171 &LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback,
172 base::Unretained(this)));
173 web_ui()->RegisterMessageCallback("spellCheckLanguageChange",
174 base::Bind(
175 &LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback,
176 base::Unretained(this)));
177 web_ui()->RegisterMessageCallback("uiLanguageChange",
178 base::Bind(
179 &LanguageOptionsHandlerCommon::UiLanguageChangeCallback,
180 base::Unretained(this)));
181 web_ui()->RegisterMessageCallback("retryDictionaryDownload",
182 base::Bind(
183 &LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload,
184 base::Unretained(this)));
185 web_ui()->RegisterMessageCallback("updateLanguageList",
186 base::Bind(
187 &LanguageOptionsHandlerCommon::UpdateLanguageListCallback,
188 base::Unretained(this)));
191 void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized(
192 const std::string& language) {
195 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin(
196 const std::string& language) {
197 web_ui()->CallJavascriptFunction(
198 "options.LanguageOptions.onDictionaryDownloadBegin",
199 base::StringValue(language));
202 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadSuccess(
203 const std::string& language) {
204 web_ui()->CallJavascriptFunction(
205 "options.LanguageOptions.onDictionaryDownloadSuccess",
206 base::StringValue(language));
209 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadFailure(
210 const std::string& language) {
211 web_ui()->CallJavascriptFunction(
212 "options.LanguageOptions.onDictionaryDownloadFailure",
213 base::StringValue(language));
216 base::DictionaryValue* LanguageOptionsHandlerCommon::GetUILanguageCodeSet() {
217 base::DictionaryValue* dictionary = new base::DictionaryValue();
218 const std::vector<std::string>& available_locales =
219 l10n_util::GetAvailableLocales();
220 for (size_t i = 0; i < available_locales.size(); ++i)
221 dictionary->SetBoolean(available_locales[i], true);
222 return dictionary;
225 base::DictionaryValue*
226 LanguageOptionsHandlerCommon::GetSpellCheckLanguageCodeSet() {
227 base::DictionaryValue* dictionary = new base::DictionaryValue();
228 std::vector<std::string> spell_check_languages;
229 chrome::spellcheck_common::SpellCheckLanguages(&spell_check_languages);
230 for (size_t i = 0; i < spell_check_languages.size(); ++i) {
231 dictionary->SetBoolean(spell_check_languages[i], true);
233 return dictionary;
236 void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback(
237 const base::ListValue* args) {
238 content::RecordAction(UserMetricsAction("LanguageOptions_Open"));
239 SpellcheckService* service = GetSpellcheckService();
240 if (!service)
241 return;
243 for (auto dictionary : service->GetHunspellDictionaries()) {
244 dictionary->RemoveObserver(this);
245 dictionary->AddObserver(this);
247 if (dictionary->IsDownloadInProgress())
248 OnHunspellDictionaryDownloadBegin(dictionary->GetLanguage());
249 else if (dictionary->IsDownloadFailure())
250 OnHunspellDictionaryDownloadFailure(dictionary->GetLanguage());
251 else
252 OnHunspellDictionaryDownloadSuccess(dictionary->GetLanguage());
256 void LanguageOptionsHandlerCommon::UiLanguageChangeCallback(
257 const base::ListValue* args) {
258 const std::string language_code =
259 base::UTF16ToASCII(ExtractStringValue(args));
260 CHECK(!language_code.empty());
261 const std::string action = base::StringPrintf(
262 "LanguageOptions_UiLanguageChange_%s", language_code.c_str());
263 content::RecordComputedAction(action);
264 SetApplicationLocale(language_code);
265 base::StringValue language_value(language_code);
266 web_ui()->CallJavascriptFunction("options.LanguageOptions.uiLanguageSaved",
267 language_value);
270 void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback(
271 const base::ListValue* args) {
272 const std::string language_code =
273 base::UTF16ToASCII(ExtractStringValue(args));
274 const std::string action = base::StringPrintf(
275 "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str());
276 content::RecordComputedAction(action);
278 SpellcheckService* service = GetSpellcheckService();
279 if (!service)
280 return;
282 for (auto dictionary : service->GetHunspellDictionaries()) {
283 dictionary->RemoveObserver(this);
284 dictionary->AddObserver(this);
288 void LanguageOptionsHandlerCommon::UpdateLanguageListCallback(
289 const base::ListValue* args) {
290 CHECK_EQ(args->GetSize(), 1u);
291 const base::ListValue* language_list;
292 args->GetList(0, &language_list);
293 DCHECK(language_list);
295 std::vector<std::string> languages;
296 for (base::ListValue::const_iterator it = language_list->begin();
297 it != language_list->end(); ++it) {
298 std::string lang;
299 (*it)->GetAsString(&lang);
300 languages.push_back(lang);
303 Profile* profile = Profile::FromWebUI(web_ui());
304 scoped_ptr<translate::TranslatePrefs> translate_prefs =
305 ChromeTranslateClient::CreateTranslatePrefs(profile->GetPrefs());
306 translate_prefs->UpdateLanguageList(languages);
309 void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload(
310 const base::ListValue* args) {
311 std::string language = base::UTF16ToUTF8(ExtractStringValue(args));
312 SpellcheckService* service = GetSpellcheckService();
313 if (!service)
314 return;
316 for (auto dictionary : service->GetHunspellDictionaries()) {
317 if (dictionary->GetLanguage() == language) {
318 dictionary->RetryDownloadDictionary(
319 Profile::FromWebUI(web_ui())->GetRequestContext());
320 return;
325 SpellcheckService* LanguageOptionsHandlerCommon::GetSpellcheckService() {
326 return SpellcheckServiceFactory::GetForContext(Profile::FromWebUI(web_ui()));
329 } // namespace options