Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / language_options_handler_common.cc
blob5c1131d660917698621d7a269e4f51023347a0cd
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/translate_manager.h"
25 #include "chrome/browser/translate/translate_prefs.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 "content/public/browser/user_metrics.h"
31 #include "content/public/browser/web_ui.h"
32 #include "grit/chromium_strings.h"
33 #include "grit/generated_resources.h"
34 #include "ui/base/l10n/l10n_util.h"
36 using base::UserMetricsAction;
38 namespace options {
40 LanguageOptionsHandlerCommon::LanguageOptionsHandlerCommon() {
43 LanguageOptionsHandlerCommon::~LanguageOptionsHandlerCommon() {
46 void LanguageOptionsHandlerCommon::GetLocalizedValues(
47 base::DictionaryValue* localized_strings) {
48 DCHECK(localized_strings);
49 static OptionsStringResource resources[] = {
50 { "addButton", IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_BUTTON },
51 { "languages", IDS_OPTIONS_SETTINGS_LANGUAGES_LANGUAGES },
52 { "addLanguageInstructions",
53 IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_LANGUAGE_INSTRUCTIONS },
54 { "cannotBeDisplayedInThisLanguage",
55 IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
56 IDS_PRODUCT_NAME },
57 { "isDisplayedInThisLanguage",
58 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
59 IDS_PRODUCT_NAME },
60 { "displayInThisLanguage",
61 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
62 IDS_PRODUCT_NAME },
63 { "restartRequired", IDS_OPTIONS_RELAUNCH_REQUIRED },
64 // OS X uses the OS native spellchecker so no need for these strings.
65 #if !defined(OS_MACOSX)
66 { "useThisForSpellChecking",
67 IDS_OPTIONS_SETTINGS_USE_THIS_FOR_SPELL_CHECKING },
68 { "cannotBeUsedForSpellChecking",
69 IDS_OPTIONS_SETTINGS_CANNOT_BE_USED_FOR_SPELL_CHECKING },
70 { "isUsedForSpellChecking",
71 IDS_OPTIONS_SETTINGS_IS_USED_FOR_SPELL_CHECKING },
72 { "enableSpellCheck", IDS_OPTIONS_ENABLE_SPELLCHECK },
73 { "enableAutoSpellCorrection",
74 IDS_OPTIONS_ENABLE_AUTO_SPELL_CORRECTION },
75 { "downloadingDictionary", IDS_OPTIONS_DICTIONARY_DOWNLOADING },
76 { "downloadFailed", IDS_OPTIONS_DICTIONARY_DOWNLOAD_FAILED },
77 { "retryButton", IDS_OPTIONS_DICTIONARY_DOWNLOAD_RETRY },
78 { "downloadFailHelp", IDS_OPTIONS_DICTIONARY_DOWNLOAD_FAIL_HELP },
79 #endif // !OS_MACOSX
80 { "addLanguageTitle", IDS_OPTIONS_LANGUAGES_ADD_TITLE },
81 { "addLanguageSelectLabel", IDS_OPTIONS_LANGUAGES_ADD_SELECT_LABEL },
82 { "restartButton", IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON },
83 { "offerToTranslateInThisLanguage",
84 IDS_OPTIONS_LANGUAGES_OFFER_TO_TRANSLATE_IN_THIS_LANGUAGE },
85 { "cannotTranslateInThisLanguage",
86 IDS_OPTIONS_LANGUAGES_CANNOT_TRANSLATE_IN_THIS_LANGUAGE },
89 #if defined(ENABLE_SETTINGS_APP)
90 static OptionsStringResource app_resources[] = {
91 { "cannotBeDisplayedInThisLanguage",
92 IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
93 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
94 { "isDisplayedInThisLanguage",
95 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
96 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
97 { "displayInThisLanguage",
98 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
99 IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
101 base::DictionaryValue* app_values = NULL;
102 CHECK(localized_strings->GetDictionary(kSettingsAppKey, &app_values));
103 RegisterStrings(app_values, app_resources, arraysize(app_resources));
104 #endif
106 RegisterStrings(localized_strings, resources, arraysize(resources));
108 // The following are resources, rather than local strings.
109 std::string application_locale = g_browser_process->GetApplicationLocale();
110 localized_strings->SetString("currentUiLanguageCode", application_locale);
111 std::string prospective_locale =
112 g_browser_process->local_state()->GetString(prefs::kApplicationLocale);
113 localized_strings->SetString("prospectiveUiLanguageCode",
114 !prospective_locale.empty() ? prospective_locale : application_locale);
115 localized_strings->Set("spellCheckLanguageCodeSet",
116 GetSpellCheckLanguageCodeSet());
117 localized_strings->Set("uiLanguageCodeSet", GetUILanguageCodeSet());
119 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
120 bool enable_spelling_auto_correct =
121 command_line.HasSwitch(switches::kEnableSpellingAutoCorrect);
122 localized_strings->SetBoolean("enableSpellingAutoCorrect",
123 enable_spelling_auto_correct);
125 Profile* profile = Profile::FromWebUI(web_ui());
126 PrefService* prefs = profile->GetPrefs();
127 std::string default_target_language =
128 TranslateManager::GetTargetLanguage(prefs);
129 localized_strings->SetString("defaultTargetLanguage",
130 default_target_language);
132 std::vector<std::string> languages;
133 TranslateManager::GetSupportedLanguages(&languages);
135 base::ListValue* languages_list = new base::ListValue();
136 for (std::vector<std::string>::iterator it = languages.begin();
137 it != languages.end(); ++it) {
138 languages_list->Append(new base::StringValue(*it));
141 localized_strings->Set("translateSupportedLanguages", languages_list);
144 void LanguageOptionsHandlerCommon::Uninitialize() {
145 if (hunspell_dictionary_.get())
146 hunspell_dictionary_->RemoveObserver(this);
147 hunspell_dictionary_.reset();
150 void LanguageOptionsHandlerCommon::RegisterMessages() {
151 web_ui()->RegisterMessageCallback("languageOptionsOpen",
152 base::Bind(
153 &LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback,
154 base::Unretained(this)));
155 web_ui()->RegisterMessageCallback("spellCheckLanguageChange",
156 base::Bind(
157 &LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback,
158 base::Unretained(this)));
159 web_ui()->RegisterMessageCallback("uiLanguageChange",
160 base::Bind(
161 &LanguageOptionsHandlerCommon::UiLanguageChangeCallback,
162 base::Unretained(this)));
163 web_ui()->RegisterMessageCallback("retryDictionaryDownload",
164 base::Bind(
165 &LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload,
166 base::Unretained(this)));
167 web_ui()->RegisterMessageCallback("updateLanguageList",
168 base::Bind(
169 &LanguageOptionsHandlerCommon::UpdateLanguageListCallback,
170 base::Unretained(this)));
173 void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized() {
176 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin() {
177 web_ui()->CallJavascriptFunction(
178 "options.LanguageOptions.onDictionaryDownloadBegin",
179 base::StringValue(GetHunspellDictionary()->GetLanguage()));
182 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadSuccess() {
183 web_ui()->CallJavascriptFunction(
184 "options.LanguageOptions.onDictionaryDownloadSuccess",
185 base::StringValue(GetHunspellDictionary()->GetLanguage()));
188 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadFailure() {
189 web_ui()->CallJavascriptFunction(
190 "options.LanguageOptions.onDictionaryDownloadFailure",
191 base::StringValue(GetHunspellDictionary()->GetLanguage()));
194 base::DictionaryValue* LanguageOptionsHandlerCommon::GetUILanguageCodeSet() {
195 base::DictionaryValue* dictionary = new base::DictionaryValue();
196 const std::vector<std::string>& available_locales =
197 l10n_util::GetAvailableLocales();
198 for (size_t i = 0; i < available_locales.size(); ++i)
199 dictionary->SetBoolean(available_locales[i], true);
200 return dictionary;
203 base::DictionaryValue*
204 LanguageOptionsHandlerCommon::GetSpellCheckLanguageCodeSet() {
205 base::DictionaryValue* dictionary = new base::DictionaryValue();
206 std::vector<std::string> spell_check_languages;
207 chrome::spellcheck_common::SpellCheckLanguages(&spell_check_languages);
208 for (size_t i = 0; i < spell_check_languages.size(); ++i) {
209 dictionary->SetBoolean(spell_check_languages[i], true);
211 return dictionary;
214 void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback(
215 const base::ListValue* args) {
216 content::RecordAction(UserMetricsAction("LanguageOptions_Open"));
217 RefreshHunspellDictionary();
218 if (hunspell_dictionary_->IsDownloadInProgress())
219 OnHunspellDictionaryDownloadBegin();
220 else if (hunspell_dictionary_->IsDownloadFailure())
221 OnHunspellDictionaryDownloadFailure();
222 else
223 OnHunspellDictionaryDownloadSuccess();
226 void LanguageOptionsHandlerCommon::UiLanguageChangeCallback(
227 const base::ListValue* args) {
228 const std::string language_code = UTF16ToASCII(ExtractStringValue(args));
229 CHECK(!language_code.empty());
230 const std::string action = base::StringPrintf(
231 "LanguageOptions_UiLanguageChange_%s", language_code.c_str());
232 content::RecordComputedAction(action);
233 SetApplicationLocale(language_code);
234 base::StringValue language_value(language_code);
235 web_ui()->CallJavascriptFunction("options.LanguageOptions.uiLanguageSaved",
236 language_value);
239 void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback(
240 const base::ListValue* args) {
241 const std::string language_code = UTF16ToASCII(ExtractStringValue(args));
242 CHECK(!language_code.empty());
243 const std::string action = base::StringPrintf(
244 "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str());
245 content::RecordComputedAction(action);
246 RefreshHunspellDictionary();
249 void LanguageOptionsHandlerCommon::UpdateLanguageListCallback(
250 const base::ListValue* args) {
251 CHECK_EQ(args->GetSize(), 1u);
252 const base::ListValue* language_list;
253 args->GetList(0, &language_list);
254 DCHECK(language_list);
256 std::vector<std::string> languages;
257 for (base::ListValue::const_iterator it = language_list->begin();
258 it != language_list->end(); ++it) {
259 std::string lang;
260 (*it)->GetAsString(&lang);
261 languages.push_back(lang);
264 Profile* profile = Profile::FromWebUI(web_ui());
265 PrefService* prefs = profile->GetPrefs();
266 TranslatePrefs translate_prefs(prefs);
267 translate_prefs.UpdateLanguageList(languages);
270 void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload(
271 const base::ListValue* args) {
272 GetHunspellDictionary()->RetryDownloadDictionary(
273 Profile::FromWebUI(web_ui())->GetRequestContext());
276 void LanguageOptionsHandlerCommon::RefreshHunspellDictionary() {
277 if (hunspell_dictionary_.get())
278 hunspell_dictionary_->RemoveObserver(this);
279 hunspell_dictionary_.reset();
280 SpellcheckService* service = SpellcheckServiceFactory::GetForContext(
281 Profile::FromWebUI(web_ui()));
282 hunspell_dictionary_ = service->GetHunspellDictionary()->AsWeakPtr();
283 hunspell_dictionary_->AddObserver(this);
286 base::WeakPtr<SpellcheckHunspellDictionary>&
287 LanguageOptionsHandlerCommon::GetHunspellDictionary() {
288 if (!hunspell_dictionary_.get())
289 RefreshHunspellDictionary();
290 return hunspell_dictionary_;
293 } // namespace options