diff options
author | juliusa <juliusa@google.com> | 2015-07-23 13:12:25 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-23 20:13:38 +0000 |
commit | d356efaeed60d5f278c26439bd425fb4e1cc3d6c (patch) | |
tree | 9c6a267f16a5af68f92a1de2d1855d0a1ee7b00f | |
parent | d45448239b0d64a73c7712262beab45ac142152d (diff) | |
download | chromium_src-d356efaeed60d5f278c26439bd425fb4e1cc3d6c.zip chromium_src-d356efaeed60d5f278c26439bd425fb4e1cc3d6c.tar.gz chromium_src-d356efaeed60d5f278c26439bd425fb4e1cc3d6c.tar.bz2 |
Loads multiple dictionaries into the spellcheck system.
Rather than only utilizing the first element in the dictionary vectors,
start actually loading all of them. This makes components of the system
aware that they are holding multiple (possibly 0) dictionaries and makes
them handle that variability.
Note that multiple dictionaries are not actually being spellchecked
against yet.
BUG=5102
Review URL: https://codereview.chromium.org/1235233002
Cr-Commit-Position: refs/heads/master@{#340154}
-rw-r--r-- | chrome/browser/spellchecker/spellcheck_service.cc | 54 | ||||
-rw-r--r-- | chrome/browser/spellchecker/spellcheck_service.h | 4 | ||||
-rw-r--r-- | chrome/browser/ui/webui/options/language_options_handler_common.cc | 17 | ||||
-rw-r--r-- | chrome/browser/ui/webui/options/language_options_handler_common.h | 3 | ||||
-rw-r--r-- | chrome/chrome_common.gypi | 1 | ||||
-rw-r--r-- | chrome/common/OWNERS | 2 | ||||
-rw-r--r-- | chrome/common/spellcheck_bdict_language.h | 17 | ||||
-rw-r--r-- | chrome/common/spellcheck_messages.h | 11 | ||||
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck.cc | 36 | ||||
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck.h | 10 | ||||
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck_unittest.cc | 4 |
11 files changed, 104 insertions, 55 deletions
diff --git a/chrome/browser/spellchecker/spellcheck_service.cc b/chrome/browser/spellchecker/spellcheck_service.cc index 3e84263..5606978 100644 --- a/chrome/browser/spellchecker/spellcheck_service.cc +++ b/chrome/browser/spellchecker/spellcheck_service.cc @@ -17,6 +17,7 @@ #include "chrome/browser/spellchecker/spellcheck_platform.h" #include "chrome/browser/spellchecker/spelling_service_client.h" #include "chrome/common/pref_names.h" +#include "chrome/common/spellcheck_bdict_language.h" #include "chrome/common/spellcheck_common.h" #include "chrome/common/spellcheck_messages.h" #include "components/user_prefs/user_prefs.h" @@ -171,18 +172,21 @@ void SpellcheckService::InitForRenderer(content::RenderProcessHost* process) { return; PrefService* prefs = user_prefs::UserPrefs::Get(context); - IPC::PlatformFileForTransit file = IPC::InvalidPlatformFileForTransit(); - - if (hunspell_dictionaries_.front()->GetDictionaryFile().IsValid()) { - file = IPC::GetFileHandleForProcess( - hunspell_dictionaries_.front()->GetDictionaryFile().GetPlatformFile(), - process->GetHandle(), false); + std::vector<SpellCheckBDictLanguage> bdict_languages; + + for (const auto& hunspell_dictionary : hunspell_dictionaries_) { + bdict_languages.push_back(SpellCheckBDictLanguage()); + bdict_languages.back().language = hunspell_dictionary->GetLanguage(); + bdict_languages.back().file = + hunspell_dictionary->GetDictionaryFile().IsValid() + ? IPC::GetFileHandleForProcess( + hunspell_dictionary->GetDictionaryFile().GetPlatformFile(), + process->GetHandle(), false) + : IPC::InvalidPlatformFileForTransit(); } process->Send(new SpellCheckMsg_Init( - file, - custom_dictionary_->GetWords(), - hunspell_dictionaries_.front()->GetLanguage(), + bdict_languages, custom_dictionary_->GetWords(), prefs->GetBoolean(prefs::kEnableAutoSpellCorrect))); process->Send(new SpellCheckMsg_EnableSpellCheck( prefs->GetBoolean(prefs::kEnableContinuousSpellcheck))); @@ -196,9 +200,9 @@ SpellcheckCustomDictionary* SpellcheckService::GetCustomDictionary() { return custom_dictionary_.get(); } -SpellcheckHunspellDictionary* SpellcheckService::GetHunspellDictionary() { - return hunspell_dictionaries_.empty() ? nullptr - : hunspell_dictionaries_.front(); +const ScopedVector<SpellcheckHunspellDictionary>& +SpellcheckService::GetHunspellDictionaries() { + return hunspell_dictionaries_; } spellcheck::FeedbackSender* SpellcheckService::GetFeedbackSender() { @@ -289,23 +293,31 @@ void SpellcheckService::OnEnableAutoSpellCorrectChanged() { } void SpellcheckService::OnSpellCheckDictionariesChanged() { - if (!hunspell_dictionaries_.empty()) - hunspell_dictionaries_.front()->RemoveObserver(this); + for (auto& hunspell_dictionary : hunspell_dictionaries_) + hunspell_dictionary->RemoveObserver(this); + PrefService* prefs = user_prefs::UserPrefs::Get(context_); DCHECK(prefs); - std::string dictionary; - prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &dictionary); + const base::ListValue* dictionary_values = + prefs->GetList(prefs::kSpellCheckDictionaries); hunspell_dictionaries_.clear(); - hunspell_dictionaries_.push_back(new SpellcheckHunspellDictionary( - dictionary, context_->GetRequestContext(), this)); - hunspell_dictionaries_.front()->AddObserver(this); - hunspell_dictionaries_.front()->Load(); + for (const base::Value* dictionary_value : *dictionary_values) { + std::string dictionary; + dictionary_value->GetAsString(&dictionary); + hunspell_dictionaries_.push_back(new SpellcheckHunspellDictionary( + dictionary, context_->GetRequestContext(), this)); + hunspell_dictionaries_.back()->AddObserver(this); + hunspell_dictionaries_.back()->Load(); + } + + std::string feedback_language; + dictionary_values->GetString(0, &feedback_language); std::string language_code; std::string country_code; chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( - dictionary, &language_code, &country_code); + feedback_language, &language_code, &country_code); feedback_sender_->OnLanguageCountryChange(language_code, country_code); UpdateFeedbackSenderState(); } diff --git a/chrome/browser/spellchecker/spellcheck_service.h b/chrome/browser/spellchecker/spellcheck_service.h index 0aeac5f..86805ce 100644 --- a/chrome/browser/spellchecker/spellcheck_service.h +++ b/chrome/browser/spellchecker/spellcheck_service.h @@ -96,8 +96,8 @@ class SpellcheckService : public KeyedService, // Returns the instance of the custom dictionary. SpellcheckCustomDictionary* GetCustomDictionary(); - // Returns the instance of the Hunspell dictionary. - SpellcheckHunspellDictionary* GetHunspellDictionary(); + // Returns the instance of the vector of Hunspell dictionaries. + const ScopedVector<SpellcheckHunspellDictionary>& GetHunspellDictionaries(); // Returns the instance of the spelling service feedback sender. spellcheck::FeedbackSender* GetFeedbackSender(); 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 d955264b..e0fe9fd 100644 --- a/chrome/browser/ui/webui/options/language_options_handler_common.cc +++ b/chrome/browser/ui/webui/options/language_options_handler_common.cc @@ -12,6 +12,7 @@ #include "base/basictypes.h" #include "base/bind.h" #include "base/command_line.h" +#include "base/memory/scoped_vector.h" #include "base/prefs/pref_service.h" #include "base/strings/string_util.h" #include "base/strings/stringprintf.h" @@ -159,7 +160,7 @@ void LanguageOptionsHandlerCommon::GetLocalizedValues( } void LanguageOptionsHandlerCommon::Uninitialize() { - if (hunspell_dictionary_.get()) + if (hunspell_dictionary_) hunspell_dictionary_->RemoveObserver(this); hunspell_dictionary_.reset(); } @@ -232,6 +233,8 @@ void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback( const base::ListValue* args) { content::RecordAction(UserMetricsAction("LanguageOptions_Open")); RefreshHunspellDictionary(); + if (!hunspell_dictionary_) + return; if (hunspell_dictionary_->IsDownloadInProgress()) OnHunspellDictionaryDownloadBegin(); else if (hunspell_dictionary_->IsDownloadFailure()) @@ -292,18 +295,22 @@ void LanguageOptionsHandlerCommon::RetrySpellcheckDictionaryDownload( } void LanguageOptionsHandlerCommon::RefreshHunspellDictionary() { - if (hunspell_dictionary_.get()) + if (hunspell_dictionary_) hunspell_dictionary_->RemoveObserver(this); hunspell_dictionary_.reset(); SpellcheckService* service = SpellcheckServiceFactory::GetForContext( Profile::FromWebUI(web_ui())); - hunspell_dictionary_ = service->GetHunspellDictionary()->AsWeakPtr(); - hunspell_dictionary_->AddObserver(this); + const ScopedVector<SpellcheckHunspellDictionary>& dictionaries( + service->GetHunspellDictionaries()); + if (!dictionaries.empty()) { + hunspell_dictionary_ = dictionaries.front()->AsWeakPtr(); + hunspell_dictionary_->AddObserver(this); + } } base::WeakPtr<SpellcheckHunspellDictionary>& LanguageOptionsHandlerCommon::GetHunspellDictionary() { - if (!hunspell_dictionary_.get()) + if (!hunspell_dictionary_) RefreshHunspellDictionary(); return hunspell_dictionary_; } 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 f4aeec1..5488e66 100644 --- a/chrome/browser/ui/webui/options/language_options_handler_common.h +++ b/chrome/browser/ui/webui/options/language_options_handler_common.h @@ -84,7 +84,8 @@ class LanguageOptionsHandlerCommon // Updates the hunspell dictionary that is used for spellchecking. void RefreshHunspellDictionary(); - // Returns the hunspell dictionary that is used for spellchecking. Never null. + // 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. diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi index c2f6b61..77238fc 100644 --- a/chrome/chrome_common.gypi +++ b/chrome/chrome_common.gypi @@ -100,6 +100,7 @@ 'common/search_urls.h', 'common/secure_origin_whitelist.cc', 'common/secure_origin_whitelist.h', + 'common/spellcheck_bdict_language.h', 'common/spellcheck_common.cc', 'common/spellcheck_common.h', 'common/spellcheck_marker.h', diff --git a/chrome/common/OWNERS b/chrome/common/OWNERS index 8f40f81..fd13a2e8 100644 --- a/chrome/common/OWNERS +++ b/chrome/common/OWNERS @@ -25,6 +25,8 @@ per-file *_messages*.h=tsepez@chromium.org per-file *_messages*.h=wfh@chromium.org # Spellcheck files. Not using spellcheck* since it covers IPC messages too. +per-file spellcheck_bdict_language.h=groby@chromium.org +per-file spellcheck_bdict_language.h=rouslan@chromium.org per-file spellcheck_common.*=groby@chromium.org per-file spellcheck_common.*=rouslan@chromium.org per-file spellcheck_marker.h=groby@chromium.org diff --git a/chrome/common/spellcheck_bdict_language.h b/chrome/common/spellcheck_bdict_language.h new file mode 100644 index 0000000..2272c37 --- /dev/null +++ b/chrome/common/spellcheck_bdict_language.h @@ -0,0 +1,17 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_SPELLCHECK_BDICT_LANGUAGE_H_ +#define CHROME_COMMON_SPELLCHECK_BDICT_LANGUAGE_H_ + +#include <string> + +#include "ipc/ipc_platform_file.h" + +struct SpellCheckBDictLanguage { + IPC::PlatformFileForTransit file; + std::string language; +}; + +#endif // CHROME_COMMON_SPELLCHECK_BDICT_LANGUAGE_H_ diff --git a/chrome/common/spellcheck_messages.h b/chrome/common/spellcheck_messages.h index 545024d..dd2cd8b 100644 --- a/chrome/common/spellcheck_messages.h +++ b/chrome/common/spellcheck_messages.h @@ -5,6 +5,7 @@ // IPC messages for spellcheck. // Multiply-included message file, hence no include guard. +#include "chrome/common/spellcheck_bdict_language.h" #include "chrome/common/spellcheck_marker.h" #include "chrome/common/spellcheck_result.h" #include "ipc/ipc_message_macros.h" @@ -31,6 +32,11 @@ IPC_STRUCT_TRAITS_BEGIN(SpellCheckMarker) IPC_STRUCT_TRAITS_MEMBER(offset) IPC_STRUCT_TRAITS_END() +IPC_STRUCT_TRAITS_BEGIN(SpellCheckBDictLanguage) + IPC_STRUCT_TRAITS_MEMBER(file) + IPC_STRUCT_TRAITS_MEMBER(language) +IPC_STRUCT_TRAITS_END() + // Messages sent from the browser to the renderer. IPC_MESSAGE_CONTROL1(SpellCheckMsg_EnableSpellCheck, @@ -39,10 +45,9 @@ IPC_MESSAGE_CONTROL1(SpellCheckMsg_EnableSpellCheck, // Passes some initialization params from the browser to the renderer's // spellchecker. This can be called directly after startup or in (async) // response to a RequestDictionary ViewHost message. -IPC_MESSAGE_CONTROL4(SpellCheckMsg_Init, - IPC::PlatformFileForTransit /* bdict_file */, +IPC_MESSAGE_CONTROL3(SpellCheckMsg_Init, + std::vector<SpellCheckBDictLanguage> /* bdict_languages */, std::set<std::string> /* custom_dict_words */, - std::string /* language */, bool /* auto spell correct */) // Words have been added and removed in the custom dictionary; update the local diff --git a/chrome/renderer/spellchecker/spellcheck.cc b/chrome/renderer/spellchecker/spellcheck.cc index e28b5ab..f27f31e 100644 --- a/chrome/renderer/spellchecker/spellcheck.cc +++ b/chrome/renderer/spellchecker/spellcheck.cc @@ -20,6 +20,7 @@ #include "content/public/renderer/render_thread.h" #include "content/public/renderer/render_view.h" #include "content/public/renderer/render_view_visitor.h" +#include "ipc/ipc_platform_file.h" #include "third_party/WebKit/public/platform/WebString.h" #include "third_party/WebKit/public/platform/WebVector.h" #include "third_party/WebKit/public/web/WebTextCheckingCompletion.h" @@ -162,7 +163,6 @@ class SpellCheck::SpellcheckRequest { SpellCheck::SpellCheck() : auto_spell_correct_turned_on_(false), spellcheck_enabled_(true) { - languages_.push_back(new SpellcheckLanguage()); } SpellCheck::~SpellCheck() { @@ -185,11 +185,18 @@ bool SpellCheck::OnControlMessageReceived(const IPC::Message& message) { return handled; } -void SpellCheck::OnInit(IPC::PlatformFileForTransit bdict_file, - const std::set<std::string>& custom_words, - const std::string& language, - bool auto_spell_correct) { - Init(IPC::PlatformFileForTransitToFile(bdict_file), custom_words, language); +void SpellCheck::OnInit( + const std::vector<SpellCheckBDictLanguage>& bdict_languages, + const std::set<std::string>& custom_words, + bool auto_spell_correct) { + languages_.clear(); + for (const auto& bdict_language : bdict_languages) { + AddSpellcheckLanguage( + IPC::PlatformFileForTransitToFile(bdict_language.file), + bdict_language.language); + } + + custom_dictionary_.Init(custom_words); auto_spell_correct_turned_on_ = auto_spell_correct; #if !defined(OS_MACOSX) PostDelayedSpellCheckTask(pending_request_param_.release()); @@ -223,13 +230,12 @@ void SpellCheck::OnRequestDocumentMarkers() { new SpellCheckHostMsg_RespondDocumentMarkers(collector.markers())); } -// TODO(groby): Make sure we always have a spelling engine, even before Init() -// is called. -void SpellCheck::Init(base::File file, - const std::set<std::string>& custom_words, - const std::string& language) { - languages_.front()->Init(file.Pass(), language); - custom_dictionary_.Init(custom_words); +// TODO(groby): Make sure we always have a spelling engine, even before +// AddSpellcheckLanguage() is called. +void SpellCheck::AddSpellcheckLanguage(base::File file, + const std::string& language) { + languages_.push_back(new SpellcheckLanguage()); + languages_.back()->Init(file.Pass(), language); } bool SpellCheck::SpellCheckWord( @@ -373,7 +379,7 @@ void SpellCheck::RequestTextChecking( #endif bool SpellCheck::InitializeIfNeeded() { - return languages_.front()->InitializeIfNeeded(); + return languages_.empty() ? true : languages_.front()->InitializeIfNeeded(); } #if !defined(OS_MACOSX) // OSX doesn't have |pending_request_param_| @@ -391,7 +397,7 @@ void SpellCheck::PostDelayedSpellCheckTask(SpellcheckRequest* request) { void SpellCheck::PerformSpellCheck(SpellcheckRequest* param) { DCHECK(param); - if (!languages_.front()->IsEnabled()) { + if (languages_.empty() || !languages_.front()->IsEnabled()) { param->completion()->didCancelCheckingText(); } else { WebVector<blink::WebTextCheckingResult> results; diff --git a/chrome/renderer/spellchecker/spellcheck.h b/chrome/renderer/spellchecker/spellcheck.h index 2c9efae..714d4b4 100644 --- a/chrome/renderer/spellchecker/spellcheck.h +++ b/chrome/renderer/spellchecker/spellcheck.h @@ -18,8 +18,8 @@ #include "base/strings/string16.h" #include "chrome/renderer/spellchecker/custom_dictionary_engine.h" #include "content/public/renderer/render_process_observer.h" -#include "ipc/ipc_platform_file.h" +struct SpellCheckBDictLanguage; class SpellcheckLanguage; struct SpellCheckResult; @@ -50,10 +50,7 @@ class SpellCheck : public content::RenderProcessObserver, SpellCheck(); ~SpellCheck() override; - // TODO: Try to move that all to SpellcheckLanguage. - void Init(base::File file, - const std::set<std::string>& custom_words, - const std::string& language); + void AddSpellcheckLanguage(base::File file, const std::string& language); // If there is no dictionary file, then this requests one from the browser // and does not block. In this case it returns true. @@ -125,9 +122,8 @@ class SpellCheck : public content::RenderProcessObserver, bool OnControlMessageReceived(const IPC::Message& message) override; // Message handlers. - void OnInit(IPC::PlatformFileForTransit bdict_file, + void OnInit(const std::vector<SpellCheckBDictLanguage>& bdict_languages, const std::set<std::string>& custom_words, - const std::string& language, bool auto_spell_correct); void OnCustomDictionaryChanged(const std::set<std::string>& words_added, const std::set<std::string>& words_removed); diff --git a/chrome/renderer/spellchecker/spellcheck_unittest.cc b/chrome/renderer/spellchecker/spellcheck_unittest.cc index c4979ee..5d353fc 100644 --- a/chrome/renderer/spellchecker/spellcheck_unittest.cc +++ b/chrome/renderer/spellchecker/spellcheck_unittest.cc @@ -64,10 +64,12 @@ class SpellCheckTest : public testing::Test { #if defined(OS_MACOSX) // TODO(groby): Forcing spellcheck to use hunspell, even on OSX. // Instead, tests should exercise individual spelling engines. + spell_check_->languages_.push_back(new SpellcheckLanguage()); spell_check_->languages_.front()->platform_spelling_engine_.reset( new HunspellEngine); + spell_check_->languages_.front()->Init(file.Pass(), language); #endif - spell_check_->Init(file.Pass(), std::set<std::string>(), language); + spell_check_->AddSpellcheckLanguage(file.Pass(), language); } void EnableAutoCorrect(bool enable_autocorrect) { |