Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / language_options_handler_common.cc
blob0cfd34b19f4b776814e7b828d538ae14b6fa879b
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/prefs/pref_service.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/values.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/spellchecker/spellcheck_factory.h"
23 #include "chrome/browser/spellchecker/spellcheck_service.h"
24 #include "chrome/browser/translate/chrome_translate_client.h"
25 #include "chrome/browser/translate/translate_service.h"
26 #include "chrome/browser/ui/browser_list.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h"
29 #include "chrome/common/spellcheck_common.h"
30 #include "chrome/grit/chromium_strings.h"
31 #include "chrome/grit/generated_resources.h"
32 #include "components/translate/core/browser/translate_download_manager.h"
33 #include "components/translate/core/browser/translate_prefs.h"
34 #include "content/public/browser/user_metrics.h"
35 #include "content/public/browser/web_ui.h"
36 #include "ui/base/l10n/l10n_util.h"
38 using base::UserMetricsAction;
40 namespace options {
42 LanguageOptionsHandlerCommon::LanguageOptionsHandlerCommon() {
45 LanguageOptionsHandlerCommon::~LanguageOptionsHandlerCommon() {
48 void LanguageOptionsHandlerCommon::GetLocalizedValues(
49 base::DictionaryValue* localized_strings) {
50 DCHECK(localized_strings);
51 static OptionsStringResource resources[] = {
52 { "addButton", IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_BUTTON },
53 { "languages", IDS_OPTIONS_SETTINGS_LANGUAGES_LANGUAGES },
54 { "addLanguageInstructions",
55 IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_LANGUAGE_INSTRUCTIONS },
56 { "cannotBeDisplayedInThisLanguage",
57 IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
58 IDS_PRODUCT_NAME },
59 { "isDisplayedInThisLanguage",
60 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
61 IDS_PRODUCT_NAME },
62 { "displayInThisLanguage",
63 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
64 IDS_PRODUCT_NAME },
65 { "restartRequired", IDS_OPTIONS_RELAUNCH_REQUIRED },
66 // OS X uses the OS native spellchecker so no need for these strings.
67 #if !defined(OS_MACOSX)
68 { "useThisForSpellChecking",
69 IDS_OPTIONS_SETTINGS_USE_THIS_FOR_SPELL_CHECKING },
70 { "cannotBeUsedForSpellChecking",
71 IDS_OPTIONS_SETTINGS_CANNOT_BE_USED_FOR_SPELL_CHECKING },
72 { "isUsedForSpellChecking",
73 IDS_OPTIONS_SETTINGS_IS_USED_FOR_SPELL_CHECKING },
74 { "enableSpellCheck", IDS_OPTIONS_ENABLE_SPELLCHECK },
75 { "enableAutoSpellCorrection",
76 IDS_OPTIONS_ENABLE_AUTO_SPELL_CORRECTION },
77 { "downloadingDictionary", IDS_OPTIONS_DICTIONARY_DOWNLOADING },
78 { "downloadFailed", IDS_OPTIONS_DICTIONARY_DOWNLOAD_FAILED },
79 { "retryButton", IDS_OPTIONS_DICTIONARY_DOWNLOAD_RETRY },
80 { "downloadFailHelp", IDS_OPTIONS_DICTIONARY_DOWNLOAD_FAIL_HELP },
81 #endif // !OS_MACOSX
82 { "addLanguageTitle", IDS_OPTIONS_LANGUAGES_ADD_TITLE },
83 { "addLanguageSelectLabel", IDS_OPTIONS_LANGUAGES_ADD_SELECT_LABEL },
84 { "restartButton", IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON },
85 { "offerToTranslateInThisLanguage",
86 IDS_OPTIONS_LANGUAGES_OFFER_TO_TRANSLATE_IN_THIS_LANGUAGE },
87 { "cannotTranslateInThisLanguage",
88 IDS_OPTIONS_LANGUAGES_CANNOT_TRANSLATE_IN_THIS_LANGUAGE },
91 #if defined(ENABLE_SETTINGS_APP)
92 static OptionsStringResource app_resources[] = {
93 { "cannotBeDisplayedInThisLanguage",
94 IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
95 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
96 { "isDisplayedInThisLanguage",
97 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
98 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
99 { "displayInThisLanguage",
100 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
101 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
103 base::DictionaryValue* app_values = NULL;
104 CHECK(localized_strings->GetDictionary(kSettingsAppKey, &app_values));
105 RegisterStrings(app_values, app_resources, arraysize(app_resources));
106 #endif
108 RegisterStrings(localized_strings, resources, arraysize(resources));
110 // The following are resources, rather than local strings.
111 std::string application_locale = g_browser_process->GetApplicationLocale();
112 localized_strings->SetString("currentUiLanguageCode", application_locale);
113 std::string prospective_locale =
114 g_browser_process->local_state()->GetString(prefs::kApplicationLocale);
115 localized_strings->SetString("prospectiveUiLanguageCode",
116 !prospective_locale.empty() ? prospective_locale : application_locale);
117 localized_strings->Set("spellCheckLanguageCodeSet",
118 GetSpellCheckLanguageCodeSet());
119 localized_strings->Set("uiLanguageCodeSet", GetUILanguageCodeSet());
121 const base::CommandLine& command_line =
122 *base::CommandLine::ForCurrentProcess();
123 bool enable_spelling_auto_correct =
124 command_line.HasSwitch(switches::kEnableSpellingAutoCorrect);
125 localized_strings->SetBoolean("enableSpellingAutoCorrect",
126 enable_spelling_auto_correct);
128 Profile* profile = Profile::FromWebUI(web_ui());
129 PrefService* prefs = profile->GetPrefs();
130 std::string default_target_language =
131 TranslateService::GetTargetLanguage(prefs);
132 localized_strings->SetString("defaultTargetLanguage",
133 default_target_language);
135 std::vector<std::string> languages;
136 translate::TranslateDownloadManager::GetSupportedLanguages(&languages);
138 base::ListValue* languages_list = new base::ListValue();
139 for (std::vector<std::string>::iterator it = languages.begin();
140 it != languages.end(); ++it) {
141 languages_list->Append(new base::StringValue(*it));
144 localized_strings->Set("translateSupportedLanguages", languages_list);
147 void LanguageOptionsHandlerCommon::Uninitialize() {
148 if (hunspell_dictionary_.get())
149 hunspell_dictionary_->RemoveObserver(this);
150 hunspell_dictionary_.reset();
153 void LanguageOptionsHandlerCommon::RegisterMessages() {
154 web_ui()->RegisterMessageCallback("languageOptionsOpen",
155 base::Bind(
156 &LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback,
157 base::Unretained(this)));
158 web_ui()->RegisterMessageCallback("spellCheckLanguageChange",
159 base::Bind(
160 &LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback,
161 base::Unretained(this)));
162 web_ui()->RegisterMessageCallback("uiLanguageChange",
163 base::Bind(
164 &LanguageOptionsHandlerCommon::UiLanguageChangeCallback,
165 base::Unretained(this)));
166 web_ui()->RegisterMessageCallback("retryDictionaryDownload",
167 base::Bind(
168 &LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload,
169 base::Unretained(this)));
170 web_ui()->RegisterMessageCallback("updateLanguageList",
171 base::Bind(
172 &LanguageOptionsHandlerCommon::UpdateLanguageListCallback,
173 base::Unretained(this)));
176 void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized() {
179 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin() {
180 web_ui()->CallJavascriptFunction(
181 "options.LanguageOptions.onDictionaryDownloadBegin",
182 base::StringValue(GetHunspellDictionary()->GetLanguage()));
185 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadSuccess() {
186 web_ui()->CallJavascriptFunction(
187 "options.LanguageOptions.onDictionaryDownloadSuccess",
188 base::StringValue(GetHunspellDictionary()->GetLanguage()));
191 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadFailure() {
192 web_ui()->CallJavascriptFunction(
193 "options.LanguageOptions.onDictionaryDownloadFailure",
194 base::StringValue(GetHunspellDictionary()->GetLanguage()));
197 base::DictionaryValue* LanguageOptionsHandlerCommon::GetUILanguageCodeSet() {
198 base::DictionaryValue* dictionary = new base::DictionaryValue();
199 const std::vector<std::string>& available_locales =
200 l10n_util::GetAvailableLocales();
201 for (size_t i = 0; i < available_locales.size(); ++i)
202 dictionary->SetBoolean(available_locales[i], true);
203 return dictionary;
206 base::DictionaryValue*
207 LanguageOptionsHandlerCommon::GetSpellCheckLanguageCodeSet() {
208 base::DictionaryValue* dictionary = new base::DictionaryValue();
209 std::vector<std::string> spell_check_languages;
210 chrome::spellcheck_common::SpellCheckLanguages(&spell_check_languages);
211 for (size_t i = 0; i < spell_check_languages.size(); ++i) {
212 dictionary->SetBoolean(spell_check_languages[i], true);
214 return dictionary;
217 void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback(
218 const base::ListValue* args) {
219 content::RecordAction(UserMetricsAction("LanguageOptions_Open"));
220 RefreshHunspellDictionary();
221 if (hunspell_dictionary_->IsDownloadInProgress())
222 OnHunspellDictionaryDownloadBegin();
223 else if (hunspell_dictionary_->IsDownloadFailure())
224 OnHunspellDictionaryDownloadFailure();
225 else
226 OnHunspellDictionaryDownloadSuccess();
229 void LanguageOptionsHandlerCommon::UiLanguageChangeCallback(
230 const base::ListValue* args) {
231 const std::string language_code =
232 base::UTF16ToASCII(ExtractStringValue(args));
233 CHECK(!language_code.empty());
234 const std::string action = base::StringPrintf(
235 "LanguageOptions_UiLanguageChange_%s", language_code.c_str());
236 content::RecordComputedAction(action);
237 SetApplicationLocale(language_code);
238 base::StringValue language_value(language_code);
239 web_ui()->CallJavascriptFunction("options.LanguageOptions.uiLanguageSaved",
240 language_value);
243 void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback(
244 const base::ListValue* args) {
245 const std::string language_code =
246 base::UTF16ToASCII(ExtractStringValue(args));
247 CHECK(!language_code.empty());
248 const std::string action = base::StringPrintf(
249 "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str());
250 content::RecordComputedAction(action);
251 RefreshHunspellDictionary();
254 void LanguageOptionsHandlerCommon::UpdateLanguageListCallback(
255 const base::ListValue* args) {
256 CHECK_EQ(args->GetSize(), 1u);
257 const base::ListValue* language_list;
258 args->GetList(0, &language_list);
259 DCHECK(language_list);
261 std::vector<std::string> languages;
262 for (base::ListValue::const_iterator it = language_list->begin();
263 it != language_list->end(); ++it) {
264 std::string lang;
265 (*it)->GetAsString(&lang);
266 languages.push_back(lang);
269 Profile* profile = Profile::FromWebUI(web_ui());
270 scoped_ptr<translate::TranslatePrefs> translate_prefs =
271 ChromeTranslateClient::CreateTranslatePrefs(profile->GetPrefs());
272 translate_prefs->UpdateLanguageList(languages);
275 void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload(
276 const base::ListValue* args) {
277 GetHunspellDictionary()->RetryDownloadDictionary(
278 Profile::FromWebUI(web_ui())->GetRequestContext());
281 void LanguageOptionsHandlerCommon::RefreshHunspellDictionary() {
282 if (hunspell_dictionary_.get())
283 hunspell_dictionary_->RemoveObserver(this);
284 hunspell_dictionary_.reset();
285 SpellcheckService* service = SpellcheckServiceFactory::GetForContext(
286 Profile::FromWebUI(web_ui()));
287 hunspell_dictionary_ = service->GetHunspellDictionary()->AsWeakPtr();
288 hunspell_dictionary_->AddObserver(this);
291 base::WeakPtr<SpellcheckHunspellDictionary>&
292 LanguageOptionsHandlerCommon::GetHunspellDictionary() {
293 if (!hunspell_dictionary_.get())
294 RefreshHunspellDictionary();
295 return hunspell_dictionary_;
298 } // namespace options