| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/language_options_handler_common.h" | 5 #include "chrome/browser/ui/webui/options/language_options_handler_common.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 22 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 23 #include "chrome/browser/spellchecker/spellcheck_service.h" | 23 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 24 #include "chrome/browser/translate/translate_manager.h" | 24 #include "chrome/browser/translate/translate_manager.h" |
| 25 #include "chrome/browser/translate/translate_prefs.h" |
| 25 #include "chrome/browser/ui/browser_list.h" | 26 #include "chrome/browser/ui/browser_list.h" |
| 26 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/common/spellcheck_common.h" | 29 #include "chrome/common/spellcheck_common.h" |
| 29 #include "content/public/browser/user_metrics.h" | 30 #include "content/public/browser/user_metrics.h" |
| 30 #include "content/public/browser/web_ui.h" | 31 #include "content/public/browser/web_ui.h" |
| 31 #include "grit/chromium_strings.h" | 32 #include "grit/chromium_strings.h" |
| 32 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
| 34 | 35 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 &LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback, | 157 &LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback, |
| 157 base::Unretained(this))); | 158 base::Unretained(this))); |
| 158 web_ui()->RegisterMessageCallback("uiLanguageChange", | 159 web_ui()->RegisterMessageCallback("uiLanguageChange", |
| 159 base::Bind( | 160 base::Bind( |
| 160 &LanguageOptionsHandlerCommon::UiLanguageChangeCallback, | 161 &LanguageOptionsHandlerCommon::UiLanguageChangeCallback, |
| 161 base::Unretained(this))); | 162 base::Unretained(this))); |
| 162 web_ui()->RegisterMessageCallback("retryDictionaryDownload", | 163 web_ui()->RegisterMessageCallback("retryDictionaryDownload", |
| 163 base::Bind( | 164 base::Bind( |
| 164 &LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload, | 165 &LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload, |
| 165 base::Unretained(this))); | 166 base::Unretained(this))); |
| 167 web_ui()->RegisterMessageCallback("updateLanguageList", |
| 168 base::Bind( |
| 169 &LanguageOptionsHandlerCommon::UpdateLanguageListCallback, |
| 170 base::Unretained(this))); |
| 166 } | 171 } |
| 167 | 172 |
| 168 void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized() { | 173 void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized() { |
| 169 } | 174 } |
| 170 | 175 |
| 171 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin() { | 176 void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin() { |
| 172 web_ui()->CallJavascriptFunction( | 177 web_ui()->CallJavascriptFunction( |
| 173 "options.LanguageOptions.onDictionaryDownloadBegin", | 178 "options.LanguageOptions.onDictionaryDownloadBegin", |
| 174 StringValue(GetHunspellDictionary()->GetLanguage())); | 179 StringValue(GetHunspellDictionary()->GetLanguage())); |
| 175 } | 180 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback( | 238 void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback( |
| 234 const ListValue* args) { | 239 const ListValue* args) { |
| 235 const std::string language_code = UTF16ToASCII(ExtractStringValue(args)); | 240 const std::string language_code = UTF16ToASCII(ExtractStringValue(args)); |
| 236 CHECK(!language_code.empty()); | 241 CHECK(!language_code.empty()); |
| 237 const std::string action = base::StringPrintf( | 242 const std::string action = base::StringPrintf( |
| 238 "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str()); | 243 "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str()); |
| 239 content::RecordComputedAction(action); | 244 content::RecordComputedAction(action); |
| 240 RefreshHunspellDictionary(); | 245 RefreshHunspellDictionary(); |
| 241 } | 246 } |
| 242 | 247 |
| 248 void LanguageOptionsHandlerCommon::UpdateLanguageListCallback( |
| 249 const ListValue* args) { |
| 250 CHECK_EQ(args->GetSize(), 1u); |
| 251 const ListValue* language_list; |
| 252 args->GetList(0, &language_list); |
| 253 DCHECK(language_list); |
| 254 |
| 255 std::vector<std::string> languages; |
| 256 for (ListValue::const_iterator it = language_list->begin(); |
| 257 it != language_list->end(); ++it) { |
| 258 std::string lang; |
| 259 (*it)->GetAsString(&lang); |
| 260 languages.push_back(lang); |
| 261 } |
| 262 |
| 263 Profile* profile = Profile::FromWebUI(web_ui()); |
| 264 PrefService* prefs = profile->GetPrefs(); |
| 265 TranslatePrefs translate_prefs(prefs); |
| 266 translate_prefs.UpdateLanguageList(languages); |
| 267 } |
| 268 |
| 243 void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload( | 269 void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload( |
| 244 const ListValue* args) { | 270 const ListValue* args) { |
| 245 GetHunspellDictionary()->RetryDownloadDictionary( | 271 GetHunspellDictionary()->RetryDownloadDictionary( |
| 246 Profile::FromWebUI(web_ui())->GetRequestContext()); | 272 Profile::FromWebUI(web_ui())->GetRequestContext()); |
| 247 } | 273 } |
| 248 | 274 |
| 249 void LanguageOptionsHandlerCommon::RefreshHunspellDictionary() { | 275 void LanguageOptionsHandlerCommon::RefreshHunspellDictionary() { |
| 250 if (hunspell_dictionary_.get()) | 276 if (hunspell_dictionary_.get()) |
| 251 hunspell_dictionary_->RemoveObserver(this); | 277 hunspell_dictionary_->RemoveObserver(this); |
| 252 hunspell_dictionary_.reset(); | 278 hunspell_dictionary_.reset(); |
| 253 hunspell_dictionary_ = SpellcheckServiceFactory::GetForProfile( | 279 hunspell_dictionary_ = SpellcheckServiceFactory::GetForProfile( |
| 254 Profile::FromWebUI(web_ui()))->GetHunspellDictionary()->AsWeakPtr(); | 280 Profile::FromWebUI(web_ui()))->GetHunspellDictionary()->AsWeakPtr(); |
| 255 hunspell_dictionary_->AddObserver(this); | 281 hunspell_dictionary_->AddObserver(this); |
| 256 } | 282 } |
| 257 | 283 |
| 258 base::WeakPtr<SpellcheckHunspellDictionary>& | 284 base::WeakPtr<SpellcheckHunspellDictionary>& |
| 259 LanguageOptionsHandlerCommon::GetHunspellDictionary() { | 285 LanguageOptionsHandlerCommon::GetHunspellDictionary() { |
| 260 if (!hunspell_dictionary_.get()) | 286 if (!hunspell_dictionary_.get()) |
| 261 RefreshHunspellDictionary(); | 287 RefreshHunspellDictionary(); |
| 262 return hunspell_dictionary_; | 288 return hunspell_dictionary_; |
| 263 } | 289 } |
| 264 | 290 |
| 265 } // namespace options | 291 } // namespace options |
| OLD | NEW |