diff options
author | rouslan <rouslan@chromium.org> | 2015-09-10 15:47:22 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-09-10 22:48:02 +0000 |
commit | e9a2174ef2a35286f2a72a554a517f3aae9ee724 (patch) | |
tree | d485e329c8663e5c6ddd76e8a5db2415e30f4f59 | |
parent | 305f4a2cdd3862b17878bb7f4dc8d98c5d736853 (diff) | |
download | chromium_src-e9a2174ef2a35286f2a72a554a517f3aae9ee724.zip chromium_src-e9a2174ef2a35286f2a72a554a517f3aae9ee724.tar.gz chromium_src-e9a2174ef2a35286f2a72a554a517f3aae9ee724.tar.bz2 |
Reland: Adds multilingual support to the language_options_handler.
Originally committed in https://crrev.com/d649b32 by Julius Alexander and
reverted in https://crrev.com/df5cc80 due to GN not picking up changes in
language_options_handler. GN has been fixed, so relanding.
BUG=5102
Review URL: https://codereview.chromium.org/1331243002
Cr-Commit-Position: refs/heads/master@{#348269}
10 files changed, 115 insertions, 78 deletions
diff --git a/chrome/browser/extensions/api/language_settings_private/language_settings_private_delegate.cc b/chrome/browser/extensions/api/language_settings_private/language_settings_private_delegate.cc index 13b8940..6204394 100644 --- a/chrome/browser/extensions/api/language_settings_private/language_settings_private_delegate.cc +++ b/chrome/browser/extensions/api/language_settings_private/language_settings_private_delegate.cc @@ -129,19 +129,23 @@ void LanguageSettingsPrivateDelegate::Observe( StartOrStopListeningForSpellcheckChanges(); } -void LanguageSettingsPrivateDelegate::OnHunspellDictionaryInitialized() { +void LanguageSettingsPrivateDelegate::OnHunspellDictionaryInitialized( + const std::string& language) { BroadcastDictionariesChangedEvent(); } -void LanguageSettingsPrivateDelegate::OnHunspellDictionaryDownloadBegin() { +void LanguageSettingsPrivateDelegate::OnHunspellDictionaryDownloadBegin( + const std::string& language) { BroadcastDictionariesChangedEvent(); } -void LanguageSettingsPrivateDelegate::OnHunspellDictionaryDownloadSuccess() { +void LanguageSettingsPrivateDelegate::OnHunspellDictionaryDownloadSuccess( + const std::string& language) { BroadcastDictionariesChangedEvent(); } -void LanguageSettingsPrivateDelegate::OnHunspellDictionaryDownloadFailure() { +void LanguageSettingsPrivateDelegate::OnHunspellDictionaryDownloadFailure( + const std::string& language) { BroadcastDictionariesChangedEvent(); } diff --git a/chrome/browser/extensions/api/language_settings_private/language_settings_private_delegate.h b/chrome/browser/extensions/api/language_settings_private/language_settings_private_delegate.h index 8c314068..9a15ffc 100644 --- a/chrome/browser/extensions/api/language_settings_private/language_settings_private_delegate.h +++ b/chrome/browser/extensions/api/language_settings_private/language_settings_private_delegate.h @@ -55,10 +55,12 @@ class LanguageSettingsPrivateDelegate const content::NotificationDetails& details) override; // SpellcheckHunspellDictionary::Observer implementation. - void OnHunspellDictionaryInitialized() override; - void OnHunspellDictionaryDownloadBegin() override; - void OnHunspellDictionaryDownloadSuccess() override; - void OnHunspellDictionaryDownloadFailure() override; + void OnHunspellDictionaryInitialized(const std::string& language) override; + void OnHunspellDictionaryDownloadBegin(const std::string& language) override; + void OnHunspellDictionaryDownloadSuccess( + const std::string& language) override; + void OnHunspellDictionaryDownloadFailure( + const std::string& language) override; // SpellcheckCustomDictionary::Observer implementation. void OnCustomDictionaryLoaded() override; diff --git a/chrome/browser/resources/options/language_options.js b/chrome/browser/resources/options/language_options.js index 088f64f..6432ec4 100644 --- a/chrome/browser/resources/options/language_options.js +++ b/chrome/browser/resources/options/language_options.js @@ -226,7 +226,8 @@ cr.define('options', function() { PageManager.showPageByName('editDictionary'); }; $('dictionary-download-retry-button').onclick = function(e) { - chrome.send('retryDictionaryDownload'); + chrome.send('retryDictionaryDownload', + [e.currentTarget.languageCode]); }; } @@ -685,6 +686,7 @@ cr.define('options', function() { [spellCheckLanguageSection, dictionaryDownloadFailed], 1); if (this.spellcheckDictionaryDownloadFailures_ > 1) dictionaryDownloadFailHelp.hidden = false; + $('dictionary-download-retry-button').languageCode = languageCode; break; } diff --git a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc index 5463f40..aad12e1 100644 --- a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc +++ b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.cc @@ -139,6 +139,10 @@ void SpellcheckHunspellDictionary::Load() { void SpellcheckHunspellDictionary::RetryDownloadDictionary( net::URLRequestContextGetter* request_context_getter) { DCHECK_CURRENTLY_ON(BrowserThread::UI); + if (dictionary_file_.file.IsValid()) { + NOTREACHED(); + return; + } request_context_getter_ = request_context_getter; DownloadDictionary(GetDictionaryURL()); } @@ -236,7 +240,8 @@ void SpellcheckHunspellDictionary::DownloadDictionary(GURL url) { DCHECK(request_context_getter_); download_status_ = DOWNLOAD_IN_PROGRESS; - FOR_EACH_OBSERVER(Observer, observers_, OnHunspellDictionaryDownloadBegin()); + FOR_EACH_OBSERVER(Observer, observers_, + OnHunspellDictionaryDownloadBegin(language_)); fetcher_ = net::URLFetcher::Create(url, net::URLFetcher::GET, this); fetcher_->SetRequestContext(request_context_getter_); @@ -348,7 +353,7 @@ void SpellcheckHunspellDictionary::SaveDictionaryDataComplete( download_status_ = DOWNLOAD_NONE; FOR_EACH_OBSERVER(Observer, observers_, - OnHunspellDictionaryDownloadSuccess()); + OnHunspellDictionaryDownloadSuccess(language_)); Load(); } else { InformListenersOfDownloadFailure(); @@ -357,12 +362,13 @@ void SpellcheckHunspellDictionary::SaveDictionaryDataComplete( } void SpellcheckHunspellDictionary::InformListenersOfInitialization() { - FOR_EACH_OBSERVER(Observer, observers_, OnHunspellDictionaryInitialized()); + FOR_EACH_OBSERVER(Observer, observers_, + OnHunspellDictionaryInitialized(language_)); } void SpellcheckHunspellDictionary::InformListenersOfDownloadFailure() { download_status_ = DOWNLOAD_FAILED; FOR_EACH_OBSERVER(Observer, observers_, - OnHunspellDictionaryDownloadFailure()); + OnHunspellDictionaryDownloadFailure(language_)); } diff --git a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h index 2a37a55..a2c5068 100644 --- a/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h +++ b/chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h @@ -5,6 +5,8 @@ #ifndef CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_ #define CHROME_BROWSER_SPELLCHECKER_SPELLCHECK_HUNSPELL_DICTIONARY_H_ +#include <string> + #include "base/files/file.h" #include "base/files/file_path.h" #include "base/memory/scoped_ptr.h" @@ -32,16 +34,20 @@ class SpellcheckHunspellDictionary class Observer { public: // The dictionary has been initialized. - virtual void OnHunspellDictionaryInitialized() = 0; + virtual void OnHunspellDictionaryInitialized( + const std::string& language) = 0; // Dictionary download began. - virtual void OnHunspellDictionaryDownloadBegin() = 0; + virtual void OnHunspellDictionaryDownloadBegin( + const std::string& language) = 0; // Dictionary download succeeded. - virtual void OnHunspellDictionaryDownloadSuccess() = 0; + virtual void OnHunspellDictionaryDownloadSuccess( + const std::string& language) = 0; // Dictionary download failed. - virtual void OnHunspellDictionaryDownloadFailure() = 0; + virtual void OnHunspellDictionaryDownloadFailure( + const std::string& language) = 0; }; SpellcheckHunspellDictionary( diff --git a/chrome/browser/spellchecker/spellcheck_service.cc b/chrome/browser/spellchecker/spellcheck_service.cc index 03aeaf3..1cebb74 100644 --- a/chrome/browser/spellchecker/spellcheck_service.cc +++ b/chrome/browser/spellchecker/spellcheck_service.cc @@ -258,17 +258,21 @@ void SpellcheckService::OnCustomDictionaryChanged( } } -void SpellcheckService::OnHunspellDictionaryInitialized() { +void SpellcheckService::OnHunspellDictionaryInitialized( + const std::string& language) { InitForAllRenderers(); } -void SpellcheckService::OnHunspellDictionaryDownloadBegin() { +void SpellcheckService::OnHunspellDictionaryDownloadBegin( + const std::string& language) { } -void SpellcheckService::OnHunspellDictionaryDownloadSuccess() { +void SpellcheckService::OnHunspellDictionaryDownloadSuccess( + const std::string& language) { } -void SpellcheckService::OnHunspellDictionaryDownloadFailure() { +void SpellcheckService::OnHunspellDictionaryDownloadFailure( + const std::string& language) { } // static diff --git a/chrome/browser/spellchecker/spellcheck_service.h b/chrome/browser/spellchecker/spellcheck_service.h index 86805ce..81956f8 100644 --- a/chrome/browser/spellchecker/spellcheck_service.h +++ b/chrome/browser/spellchecker/spellcheck_service.h @@ -124,10 +124,12 @@ class SpellcheckService : public KeyedService, const SpellcheckCustomDictionary::Change& dictionary_change) override; // SpellcheckHunspellDictionary::Observer implementation. - void OnHunspellDictionaryInitialized() override; - void OnHunspellDictionaryDownloadBegin() override; - void OnHunspellDictionaryDownloadSuccess() override; - void OnHunspellDictionaryDownloadFailure() override; + void OnHunspellDictionaryInitialized(const std::string& language) override; + void OnHunspellDictionaryDownloadBegin(const std::string& language) override; + void OnHunspellDictionaryDownloadSuccess( + const std::string& language) override; + void OnHunspellDictionaryDownloadFailure( + const std::string& language) override; private: FRIEND_TEST_ALL_PREFIXES(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT); diff --git a/chrome/browser/ui/webui/options/language_options_dictionary_download_browsertest.js b/chrome/browser/ui/webui/options/language_options_dictionary_download_browsertest.js index 39aa9e1..e8a5b0f 100644 --- a/chrome/browser/ui/webui/options/language_options_dictionary_download_browsertest.js +++ b/chrome/browser/ui/webui/options/language_options_dictionary_download_browsertest.js @@ -92,7 +92,7 @@ TEST_F('LanguagesOptionsDictionaryDownloadWebUITest', TEST_F('LanguagesOptionsDictionaryDownloadWebUITest', 'testdictionaryDownloadRetry', function() { - this.mockHandler.expects(once()).retryDictionaryDownload(). + this.mockHandler.expects(once()).retryDictionaryDownload('en-US'). will(callFunction(function() { options.LanguageOptions.onDictionaryDownloadBegin('en-US'); })); diff --git a/chrome/browser/ui/webui/options/language_options_handler_common.cc b/chrome/browser/ui/webui/options/language_options_handler_common.cc index 12a75f9..d4599f6 100644 --- a/chrome/browser/ui/webui/options/language_options_handler_common.cc +++ b/chrome/browser/ui/webui/options/language_options_handler_common.cc @@ -157,9 +157,12 @@ void LanguageOptionsHandlerCommon::GetLocalizedValues( } void LanguageOptionsHandlerCommon::Uninitialize() { - if (hunspell_dictionary_) - hunspell_dictionary_->RemoveObserver(this); - hunspell_dictionary_.reset(); + SpellcheckService* service = GetSpellcheckService(); + if (!service) + return; + + for (auto dict : service->GetHunspellDictionaries()) + dict->RemoveObserver(this); } void LanguageOptionsHandlerCommon::RegisterMessages() { @@ -185,25 +188,29 @@ void LanguageOptionsHandlerCommon::RegisterMessages() { base::Unretained(this))); } -void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized() { +void LanguageOptionsHandlerCommon::OnHunspellDictionaryInitialized( + const std::string& language) { } -void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin() { +void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadBegin( + const std::string& language) { web_ui()->CallJavascriptFunction( "options.LanguageOptions.onDictionaryDownloadBegin", - base::StringValue(GetHunspellDictionary()->GetLanguage())); + base::StringValue(language)); } -void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadSuccess() { +void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadSuccess( + const std::string& language) { web_ui()->CallJavascriptFunction( "options.LanguageOptions.onDictionaryDownloadSuccess", - base::StringValue(GetHunspellDictionary()->GetLanguage())); + base::StringValue(language)); } -void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadFailure() { +void LanguageOptionsHandlerCommon::OnHunspellDictionaryDownloadFailure( + const std::string& language) { web_ui()->CallJavascriptFunction( "options.LanguageOptions.onDictionaryDownloadFailure", - base::StringValue(GetHunspellDictionary()->GetLanguage())); + base::StringValue(language)); } base::DictionaryValue* LanguageOptionsHandlerCommon::GetUILanguageCodeSet() { @@ -229,15 +236,21 @@ LanguageOptionsHandlerCommon::GetSpellCheckLanguageCodeSet() { void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback( const base::ListValue* args) { content::RecordAction(UserMetricsAction("LanguageOptions_Open")); - RefreshHunspellDictionary(); - if (!hunspell_dictionary_) + SpellcheckService* service = GetSpellcheckService(); + if (!service) return; - if (hunspell_dictionary_->IsDownloadInProgress()) - OnHunspellDictionaryDownloadBegin(); - else if (hunspell_dictionary_->IsDownloadFailure()) - OnHunspellDictionaryDownloadFailure(); - else - OnHunspellDictionaryDownloadSuccess(); + + for (auto dictionary : service->GetHunspellDictionaries()) { + dictionary->RemoveObserver(this); + dictionary->AddObserver(this); + + if (dictionary->IsDownloadInProgress()) + OnHunspellDictionaryDownloadBegin(dictionary->GetLanguage()); + else if (dictionary->IsDownloadFailure()) + OnHunspellDictionaryDownloadFailure(dictionary->GetLanguage()); + else + OnHunspellDictionaryDownloadSuccess(dictionary->GetLanguage()); + } } void LanguageOptionsHandlerCommon::UiLanguageChangeCallback( @@ -261,7 +274,15 @@ void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback( const std::string action = base::StringPrintf( "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str()); content::RecordComputedAction(action); - RefreshHunspellDictionary(); + + SpellcheckService* service = GetSpellcheckService(); + if (!service) + return; + + for (auto dictionary : service->GetHunspellDictionaries()) { + dictionary->RemoveObserver(this); + dictionary->AddObserver(this); + } } void LanguageOptionsHandlerCommon::UpdateLanguageListCallback( @@ -287,29 +308,22 @@ void LanguageOptionsHandlerCommon::UpdateLanguageListCallback( void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload( const base::ListValue* args) { - GetHunspellDictionary()->RetryDownloadDictionary( - Profile::FromWebUI(web_ui())->GetRequestContext()); -} + std::string language = base::UTF16ToUTF8(ExtractStringValue(args)); + SpellcheckService* service = GetSpellcheckService(); + if (!service) + return; -void LanguageOptionsHandlerCommon::RefreshHunspellDictionary() { - if (hunspell_dictionary_) - hunspell_dictionary_->RemoveObserver(this); - hunspell_dictionary_.reset(); - SpellcheckService* service = SpellcheckServiceFactory::GetForContext( - Profile::FromWebUI(web_ui())); - const ScopedVector<SpellcheckHunspellDictionary>& dictionaries( - service->GetHunspellDictionaries()); - if (!dictionaries.empty()) { - hunspell_dictionary_ = dictionaries.front()->AsWeakPtr(); - hunspell_dictionary_->AddObserver(this); + for (auto dictionary : service->GetHunspellDictionaries()) { + if (dictionary->GetLanguage() == language) { + dictionary->RetryDownloadDictionary( + Profile::FromWebUI(web_ui())->GetRequestContext()); + return; + } } } -base::WeakPtr<SpellcheckHunspellDictionary>& - LanguageOptionsHandlerCommon::GetHunspellDictionary() { - if (!hunspell_dictionary_) - RefreshHunspellDictionary(); - return hunspell_dictionary_; +SpellcheckService* LanguageOptionsHandlerCommon::GetSpellcheckService() { + return SpellcheckServiceFactory::GetForContext(Profile::FromWebUI(web_ui())); } } // namespace options diff --git a/chrome/browser/ui/webui/options/language_options_handler_common.h b/chrome/browser/ui/webui/options/language_options_handler_common.h index 5488e66..27fffd1 100644 --- a/chrome/browser/ui/webui/options/language_options_handler_common.h +++ b/chrome/browser/ui/webui/options/language_options_handler_common.h @@ -33,10 +33,12 @@ class LanguageOptionsHandlerCommon void RegisterMessages() override; // SpellcheckHunspellDictionary::Observer implementation. - void OnHunspellDictionaryInitialized() override; - void OnHunspellDictionaryDownloadBegin() override; - void OnHunspellDictionaryDownloadSuccess() override; - void OnHunspellDictionaryDownloadFailure() override; + void OnHunspellDictionaryInitialized(const std::string& language) override; + void OnHunspellDictionaryDownloadBegin(const std::string& language) override; + void OnHunspellDictionaryDownloadSuccess( + const std::string& language) override; + void OnHunspellDictionaryDownloadFailure( + const std::string& language) override; // The following static methods are public for ease of testing. @@ -75,21 +77,16 @@ class LanguageOptionsHandlerCommon void SpellCheckLanguageChangeCallback(const base::ListValue* args); // Called when the user clicks "Retry" button for a spellcheck dictionary that - // has failed to download. + // has failed to download. |args| will contain the language code as a string + // (ex. "fr"). void RetrySpellcheckDictionaryDownload(const base::ListValue* args); // Called when the user saves the language list preferences. void UpdateLanguageListCallback(const base::ListValue* args); - // Updates the hunspell dictionary that is used for spellchecking. - void RefreshHunspellDictionary(); - - // Returns the hunspell dictionary that is used for spellchecking. Null when - // no languages are selected for spellchecking. - base::WeakPtr<SpellcheckHunspellDictionary>& GetHunspellDictionary(); - - // The hunspell dictionary that is used for spellchecking. Might be null. - base::WeakPtr<SpellcheckHunspellDictionary> hunspell_dictionary_; + // Returns a pointer to the browser SpellcheckService. Can be null if the + // service has already shut down. + SpellcheckService* GetSpellcheckService(); DISALLOW_COPY_AND_ASSIGN(LanguageOptionsHandlerCommon); }; |