language_options_handler_common.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.
4
5#include "chrome/browser/ui/webui/options/language_options_handler_common.h"
6
7#include <map>
8#include <string>
9#include <utility>
10#include <vector>
11
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/ui/browser_list.h"
26#include "chrome/common/chrome_switches.h"
27#include "chrome/common/pref_names.h"
28#include "chrome/common/spellcheck_common.h"
29#include "content/public/browser/user_metrics.h"
30#include "content/public/browser/web_ui.h"
31#include "grit/chromium_strings.h"
32#include "grit/generated_resources.h"
33#include "ui/base/l10n/l10n_util.h"
34
35using content::UserMetricsAction;
36
37namespace options {
38
39LanguageOptionsHandlerCommon::LanguageOptionsHandlerCommon() {
40}
41
42LanguageOptionsHandlerCommon::~LanguageOptionsHandlerCommon() {
43}
44
45void LanguageOptionsHandlerCommon::GetLocalizedValues(
46    DictionaryValue* localized_strings) {
47  DCHECK(localized_strings);
48  static OptionsStringResource resources[] = {
49    { "addButton", IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_BUTTON },
50    { "languages", IDS_OPTIONS_SETTINGS_LANGUAGES_LANGUAGES },
51    { "addLanguageInstructions",
52      IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_LANGUAGE_INSTRUCTIONS },
53    { "cannotBeDisplayedInThisLanguage",
54      IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
55      IDS_PRODUCT_NAME },
56    { "isDisplayedInThisLanguage",
57      IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
58      IDS_PRODUCT_NAME },
59    { "displayInThisLanguage",
60      IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
61      IDS_PRODUCT_NAME },
62    { "restartRequired", IDS_OPTIONS_RELAUNCH_REQUIRED },
63  // OS X uses the OS native spellchecker so no need for these strings.
64#if !defined(OS_MACOSX)
65    { "useThisForSpellChecking",
66      IDS_OPTIONS_SETTINGS_USE_THIS_FOR_SPELL_CHECKING },
67    { "cannotBeUsedForSpellChecking",
68      IDS_OPTIONS_SETTINGS_CANNOT_BE_USED_FOR_SPELL_CHECKING },
69    { "isUsedForSpellChecking",
70      IDS_OPTIONS_SETTINGS_IS_USED_FOR_SPELL_CHECKING },
71    { "enableSpellCheck", IDS_OPTIONS_ENABLE_SPELLCHECK },
72    { "enableAutoSpellCorrection",
73      IDS_OPTIONS_ENABLE_AUTO_SPELL_CORRECTION },
74    { "downloadingDictionary", IDS_OPTIONS_DICTIONARY_DOWNLOADING },
75    { "downloadFailed", IDS_OPTIONS_DICTIONARY_DOWNLOAD_FAILED },
76    { "retryButton", IDS_OPTIONS_DICTIONARY_DOWNLOAD_RETRY },
77    { "downloadFailHelp", IDS_OPTIONS_DICTIONARY_DOWNLOAD_FAIL_HELP },
78#endif  // !OS_MACOSX
79    { "addLanguageTitle", IDS_OPTIONS_LANGUAGES_ADD_TITLE },
80    { "addLanguageSelectLabel", IDS_OPTIONS_LANGUAGES_ADD_SELECT_LABEL },
81    { "restartButton", IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON },
82    { "dontTranslateInThisLanguage",
83      IDS_OPTIONS_LANGUAGES_DONT_TRANSLATE_IN_THIS_LANGUAGE },
84    { "cannotTranslateInThisLanguage",
85      IDS_OPTIONS_LANGUAGES_CANNOT_TRANSLATE_IN_THIS_LANGUAGE },
86  };
87
88#if defined(ENABLE_SETTINGS_APP)
89  static OptionsStringResource app_resources[] = {
90    { "cannotBeDisplayedInThisLanguage",
91      IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
92      IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
93    { "isDisplayedInThisLanguage",
94      IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
95      IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
96    { "displayInThisLanguage",
97      IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
98      IDS_SETTINGS_APP_LAUNCHER_PRODUCT_NAME },
99  };
100  DictionaryValue* app_values = NULL;
101  CHECK(localized_strings->GetDictionary(kSettingsAppKey, &app_values));
102  RegisterStrings(app_values, app_resources, arraysize(app_resources));
103#endif
104
105  RegisterStrings(localized_strings, resources, arraysize(resources));
106
107  // The following are resources, rather than local strings.
108  std::string application_locale = g_browser_process->GetApplicationLocale();
109  localized_strings->SetString("currentUiLanguageCode", application_locale);
110  std::string prospective_locale =
111      g_browser_process->local_state()->GetString(prefs::kApplicationLocale);
112  localized_strings->SetString("prospectiveUiLanguageCode",
113      !prospective_locale.empty() ? prospective_locale : application_locale);
114  localized_strings->Set("spellCheckLanguageCodeSet",
115                         GetSpellCheckLanguageCodeSet());
116  localized_strings->Set("uiLanguageCodeSet", GetUILanguageCodeSet());
117
118  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
119  bool enable_spelling_auto_correct =
120      command_line.HasSwitch(switches::kEnableSpellingAutoCorrect);
121  localized_strings->SetBoolean("enableSpellingAutoCorrect",
122                                enable_spelling_auto_correct);
123  bool enable_translate_settings =
124      command_line.HasSwitch(switches::kEnableTranslateSettings);
125  localized_strings->SetBoolean("enableTranslateSettings",
126                                enable_translate_settings);
127
128  Profile* profile = Profile::FromWebUI(web_ui());
129  PrefService* prefs = profile->GetPrefs();
130  std::string default_target_language =
131      TranslateManager::GetTargetLanguage(prefs);
132  localized_strings->SetString("defaultTargetLanguage",
133                               default_target_language);
134
135  std::vector<std::string> languages;
136  TranslateManager::GetSupportedLanguages(&languages);
137
138  ListValue* languages_list = new ListValue();
139  for (std::vector<std::string>::iterator it = languages.begin();
140       it != languages.end(); ++it) {
141    languages_list->Append(new StringValue(*it));
142  }
143
144  localized_strings->Set("translateSupportedLanguages", languages_list);
145}
146
147void LanguageOptionsHandlerCommon::Uninitialize() {
148  if (hunspell_dictionary_.get())
149    hunspell_dictionary_->RemoveObserver(this);
150  hunspell_dictionary_.reset();
151}
152
153void 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}
171
172void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized() {
173}
174
175void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin() {
176  web_ui()->CallJavascriptFunction(
177      "options.LanguageOptions.onDictionaryDownloadBegin",
178      StringValue(GetHunspellDictionary()->GetLanguage()));
179}
180
181void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadSuccess() {
182  web_ui()->CallJavascriptFunction(
183      "options.LanguageOptions.onDictionaryDownloadSuccess",
184      StringValue(GetHunspellDictionary()->GetLanguage()));
185}
186
187void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadFailure() {
188  web_ui()->CallJavascriptFunction(
189      "options.LanguageOptions.onDictionaryDownloadFailure",
190      StringValue(GetHunspellDictionary()->GetLanguage()));
191}
192
193DictionaryValue* LanguageOptionsHandlerCommon::GetUILanguageCodeSet() {
194  DictionaryValue* dictionary = new DictionaryValue();
195  const std::vector<std::string>& available_locales =
196      l10n_util::GetAvailableLocales();
197  for (size_t i = 0; i < available_locales.size(); ++i)
198    dictionary->SetBoolean(available_locales[i], true);
199  return dictionary;
200}
201
202DictionaryValue* LanguageOptionsHandlerCommon::GetSpellCheckLanguageCodeSet() {
203  DictionaryValue* dictionary = new DictionaryValue();
204  std::vector<std::string> spell_check_languages;
205  chrome::spellcheck_common::SpellCheckLanguages(&spell_check_languages);
206  for (size_t i = 0; i < spell_check_languages.size(); ++i) {
207    dictionary->SetBoolean(spell_check_languages[i], true);
208  }
209  return dictionary;
210}
211
212void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback(
213    const ListValue* args) {
214  content::RecordAction(UserMetricsAction("LanguageOptions_Open"));
215  RefreshHunspellDictionary();
216  if (hunspell_dictionary_->IsDownloadInProgress())
217    OnHunspellDictionaryDownloadBegin();
218  else if (hunspell_dictionary_->IsDownloadFailure())
219    OnHunspellDictionaryDownloadFailure();
220  else
221    OnHunspellDictionaryDownloadSuccess();
222}
223
224void LanguageOptionsHandlerCommon::UiLanguageChangeCallback(
225    const ListValue* args) {
226  const std::string language_code = UTF16ToASCII(ExtractStringValue(args));
227  CHECK(!language_code.empty());
228  const std::string action = base::StringPrintf(
229      "LanguageOptions_UiLanguageChange_%s", language_code.c_str());
230  content::RecordComputedAction(action);
231  SetApplicationLocale(language_code);
232  StringValue language_value(language_code);
233  web_ui()->CallJavascriptFunction("options.LanguageOptions.uiLanguageSaved",
234                                   language_value);
235}
236
237void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback(
238    const ListValue* args) {
239  const std::string language_code = UTF16ToASCII(ExtractStringValue(args));
240  CHECK(!language_code.empty());
241  const std::string action = base::StringPrintf(
242      "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str());
243  content::RecordComputedAction(action);
244  RefreshHunspellDictionary();
245}
246
247void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload(
248    const ListValue* args) {
249  GetHunspellDictionary()->RetryDownloadDictionary(
250      Profile::FromWebUI(web_ui())->GetRequestContext());
251}
252
253void LanguageOptionsHandlerCommon::RefreshHunspellDictionary() {
254  if (hunspell_dictionary_.get())
255    hunspell_dictionary_->RemoveObserver(this);
256  hunspell_dictionary_.reset();
257  hunspell_dictionary_ = SpellcheckServiceFactory::GetForProfile(
258      Profile::FromWebUI(web_ui()))->GetHunspellDictionary()->AsWeakPtr();
259  hunspell_dictionary_->AddObserver(this);
260}
261
262base::WeakPtr<SpellcheckHunspellDictionary>&
263    LanguageOptionsHandlerCommon::GetHunspellDictionary() {
264  if (!hunspell_dictionary_.get())
265    RefreshHunspellDictionary();
266  return hunspell_dictionary_;
267}
268
269}  // namespace options
270