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"
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
;
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
;
56 const int product_id
= IDS_PRODUCT_NAME
;
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
,
67 { "isDisplayedInThisLanguage",
68 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE
,
70 { "displayInThisLanguage",
71 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE
,
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
},
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
));
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 if (hunspell_dictionary_
)
161 hunspell_dictionary_
->RemoveObserver(this);
162 hunspell_dictionary_
.reset();
165 void LanguageOptionsHandlerCommon::RegisterMessages() {
166 web_ui()->RegisterMessageCallback("languageOptionsOpen",
168 &LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback
,
169 base::Unretained(this)));
170 web_ui()->RegisterMessageCallback("spellCheckLanguageChange",
172 &LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback
,
173 base::Unretained(this)));
174 web_ui()->RegisterMessageCallback("uiLanguageChange",
176 &LanguageOptionsHandlerCommon::UiLanguageChangeCallback
,
177 base::Unretained(this)));
178 web_ui()->RegisterMessageCallback("retryDictionaryDownload",
180 &LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload
,
181 base::Unretained(this)));
182 web_ui()->RegisterMessageCallback("updateLanguageList",
184 &LanguageOptionsHandlerCommon::UpdateLanguageListCallback
,
185 base::Unretained(this)));
188 void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized() {
191 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin() {
192 web_ui()->CallJavascriptFunction(
193 "options.LanguageOptions.onDictionaryDownloadBegin",
194 base::StringValue(GetHunspellDictionary()->GetLanguage()));
197 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadSuccess() {
198 web_ui()->CallJavascriptFunction(
199 "options.LanguageOptions.onDictionaryDownloadSuccess",
200 base::StringValue(GetHunspellDictionary()->GetLanguage()));
203 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadFailure() {
204 web_ui()->CallJavascriptFunction(
205 "options.LanguageOptions.onDictionaryDownloadFailure",
206 base::StringValue(GetHunspellDictionary()->GetLanguage()));
209 base::DictionaryValue
* LanguageOptionsHandlerCommon::GetUILanguageCodeSet() {
210 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
211 const std::vector
<std::string
>& available_locales
=
212 l10n_util::GetAvailableLocales();
213 for (size_t i
= 0; i
< available_locales
.size(); ++i
)
214 dictionary
->SetBoolean(available_locales
[i
], true);
218 base::DictionaryValue
*
219 LanguageOptionsHandlerCommon::GetSpellCheckLanguageCodeSet() {
220 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
221 std::vector
<std::string
> spell_check_languages
;
222 chrome::spellcheck_common::SpellCheckLanguages(&spell_check_languages
);
223 for (size_t i
= 0; i
< spell_check_languages
.size(); ++i
) {
224 dictionary
->SetBoolean(spell_check_languages
[i
], true);
229 void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback(
230 const base::ListValue
* args
) {
231 content::RecordAction(UserMetricsAction("LanguageOptions_Open"));
232 RefreshHunspellDictionary();
233 if (!hunspell_dictionary_
)
235 if (hunspell_dictionary_
->IsDownloadInProgress())
236 OnHunspellDictionaryDownloadBegin();
237 else if (hunspell_dictionary_
->IsDownloadFailure())
238 OnHunspellDictionaryDownloadFailure();
240 OnHunspellDictionaryDownloadSuccess();
243 void LanguageOptionsHandlerCommon::UiLanguageChangeCallback(
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_UiLanguageChange_%s", language_code
.c_str());
250 content::RecordComputedAction(action
);
251 SetApplicationLocale(language_code
);
252 base::StringValue
language_value(language_code
);
253 web_ui()->CallJavascriptFunction("options.LanguageOptions.uiLanguageSaved",
257 void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback(
258 const base::ListValue
* args
) {
259 const std::string language_code
=
260 base::UTF16ToASCII(ExtractStringValue(args
));
261 const std::string action
= base::StringPrintf(
262 "LanguageOptions_SpellCheckLanguageChange_%s", language_code
.c_str());
263 content::RecordComputedAction(action
);
264 RefreshHunspellDictionary();
267 void LanguageOptionsHandlerCommon::UpdateLanguageListCallback(
268 const base::ListValue
* args
) {
269 CHECK_EQ(args
->GetSize(), 1u);
270 const base::ListValue
* language_list
;
271 args
->GetList(0, &language_list
);
272 DCHECK(language_list
);
274 std::vector
<std::string
> languages
;
275 for (base::ListValue::const_iterator it
= language_list
->begin();
276 it
!= language_list
->end(); ++it
) {
278 (*it
)->GetAsString(&lang
);
279 languages
.push_back(lang
);
282 Profile
* profile
= Profile::FromWebUI(web_ui());
283 scoped_ptr
<translate::TranslatePrefs
> translate_prefs
=
284 ChromeTranslateClient::CreateTranslatePrefs(profile
->GetPrefs());
285 translate_prefs
->UpdateLanguageList(languages
);
288 void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload(
289 const base::ListValue
* args
) {
290 GetHunspellDictionary()->RetryDownloadDictionary(
291 Profile::FromWebUI(web_ui())->GetRequestContext());
294 void LanguageOptionsHandlerCommon::RefreshHunspellDictionary() {
295 if (hunspell_dictionary_
)
296 hunspell_dictionary_
->RemoveObserver(this);
297 hunspell_dictionary_
.reset();
298 SpellcheckService
* service
= SpellcheckServiceFactory::GetForContext(
299 Profile::FromWebUI(web_ui()));
300 const ScopedVector
<SpellcheckHunspellDictionary
>& dictionaries(
301 service
->GetHunspellDictionaries());
302 if (!dictionaries
.empty()) {
303 hunspell_dictionary_
= dictionaries
.front()->AsWeakPtr();
304 hunspell_dictionary_
->AddObserver(this);
308 base::WeakPtr
<SpellcheckHunspellDictionary
>&
309 LanguageOptionsHandlerCommon::GetHunspellDictionary() {
310 if (!hunspell_dictionary_
)
311 RefreshHunspellDictionary();
312 return hunspell_dictionary_
;
315 } // namespace options