#include "chrome/browser/spellchecker/spell_check_host_chrome_impl.h"
#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "components/spellcheck/browser/spellcheck_host_metrics.h"
#include "components/spellcheck/browser/spellcheck_platform.h"
#include "components/spellcheck/common/spellcheck_features.h"
#include "components/spellcheck/spellcheck_buildflags.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER) && BUILDFLAG(ENABLE_SPELLING_SERVICE)
#include "chrome/browser/spellchecker/spelling_request.h"
#endif
namespace {
SpellCheckHostChromeImpl::Binder& GetSpellCheckHostBinderOverride() { … }
}
SpellCheckHostChromeImpl::SpellCheckHostChromeImpl(int render_process_id)
: … { … }
SpellCheckHostChromeImpl::~SpellCheckHostChromeImpl() { … }
void SpellCheckHostChromeImpl::Create(
int render_process_id,
mojo::PendingReceiver<spellcheck::mojom::SpellCheckHost> receiver) { … }
void SpellCheckHostChromeImpl::OverrideBinderForTesting(Binder binder) { … }
void SpellCheckHostChromeImpl::NotifyChecked(const std::u16string& word,
bool misspelled) { … }
#if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
void SpellCheckHostChromeImpl::CallSpellingService(
const std::u16string& text,
CallSpellingServiceCallback callback) { … }
void SpellCheckHostChromeImpl::CallSpellingServiceDone(
CallSpellingServiceCallback callback,
bool success,
const std::u16string& text,
const std::vector<SpellCheckResult>& service_results) const { … }
std::vector<SpellCheckResult> SpellCheckHostChromeImpl::FilterCustomWordResults(
const std::string& text,
const SpellcheckCustomDictionary& custom_dictionary,
const std::vector<SpellCheckResult>& service_results) { … }
#endif
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER) && BUILDFLAG(ENABLE_SPELLING_SERVICE)
void SpellCheckHostChromeImpl::CheckSpelling(const std::u16string& word,
CheckSpellingCallback callback) {
bool correct = spellcheck_platform::CheckSpelling(word, document_tag_);
std::move(callback).Run(correct);
}
void SpellCheckHostChromeImpl::FillSuggestionList(
const std::u16string& word,
FillSuggestionListCallback callback) {
std::vector<std::u16string> suggestions;
spellcheck_platform::FillSuggestionList(word, &suggestions);
std::move(callback).Run(suggestions);
}
void SpellCheckHostChromeImpl::RequestTextCheck(
const std::u16string& text,
RequestTextCheckCallback callback) {
DCHECK(!text.empty());
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
SpellcheckService* spellcheck = GetSpellcheckService();
if (!spellcheck) {
std::move(callback).Run({});
return;
}
requests_.insert(std::make_unique<SpellingRequest>(
spellcheck->platform_spell_checker(), &client_, text, render_process_id_,
document_tag_, std::move(callback),
base::BindOnce(&SpellCheckHostChromeImpl::OnRequestFinished,
base::Unretained(this))));
}
#if BUILDFLAG(IS_WIN)
void SpellCheckHostChromeImpl::InitializeDictionaries(
InitializeDictionariesCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (base::FeatureList::IsEnabled(
spellcheck::kWinDelaySpellcheckServiceInit)) {
SpellcheckService* spellcheck = GetSpellcheckService();
if (!spellcheck) {
std::move(callback).Run({}, {},
false);
return;
}
dictionaries_loaded_callback_ = std::move(callback);
spellcheck->InitializeDictionaries(
base::BindOnce(&SpellCheckHostChromeImpl::OnDictionariesInitialized,
weak_factory_.GetWeakPtr()));
return;
}
NOTREACHED_IN_MIGRATION();
std::move(callback).Run({}, {},
false);
}
void SpellCheckHostChromeImpl::OnDictionariesInitialized() {
DCHECK(dictionaries_loaded_callback_);
SpellcheckService* spellcheck = GetSpellcheckService();
if (!spellcheck) {
std::move(dictionaries_loaded_callback_)
.Run({}, {},
false);
return;
}
const bool enable = spellcheck->IsSpellcheckEnabled();
std::vector<spellcheck::mojom::SpellCheckBDictLanguagePtr> dictionaries;
std::vector<std::string> custom_words;
if (enable) {
for (const auto& hunspell_dictionary :
spellcheck->GetHunspellDictionaries()) {
dictionaries.push_back(spellcheck::mojom::SpellCheckBDictLanguage::New(
hunspell_dictionary->GetDictionaryFile().Duplicate(),
hunspell_dictionary->GetLanguage()));
}
SpellcheckCustomDictionary* custom_dictionary =
spellcheck->GetCustomDictionary();
custom_words.assign(custom_dictionary->GetWords().begin(),
custom_dictionary->GetWords().end());
}
std::move(dictionaries_loaded_callback_)
.Run(std::move(dictionaries), custom_words, enable);
}
#endif
void SpellCheckHostChromeImpl::OnRequestFinished(SpellingRequest* request) {
auto iterator = requests_.find(request);
requests_.erase(iterator);
}
void SpellCheckHostChromeImpl::CombineResultsForTesting(
std::vector<SpellCheckResult>* remote_results,
const std::vector<SpellCheckResult>& local_results) {
SpellingRequest::CombineResults(remote_results, local_results);
}
#endif
SpellcheckService* SpellCheckHostChromeImpl::GetSpellcheckService() const { … }