1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/quick_answers/utils/spell_checker.h"
#include "base/logging.h"
#include "base/strings/string_split.h"
#include "base/types/expected.h"
#include "chromeos/components/quick_answers/public/cpp/quick_answers_prefs.h"
#include "chromeos/constants/chromeos_features.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/base/l10n/l10n_util.h"
namespace quick_answers {
namespace {
bool ShouldDownloadDictionaries() {
if (QuickAnswersState::IsEnabled()) {
return true;
}
// Spell checker dictionaries are required to show a consent UI by Quick
// Answers code. Note that `IsEligibleAs` always returns false if current
// feature type does not match specified feature type, e.g., this
// `IsEligibleAs` always returns false for `kHmr` case.
return QuickAnswersState::IsEligibleAs(
QuickAnswersState::FeatureType::kQuickAnswers) &&
QuickAnswersState::GetConsentStatusAs(
QuickAnswersState::FeatureType::kQuickAnswers) ==
quick_answers::prefs::ConsentStatus::kUnknown;
}
} // namespace
SpellChecker::SpellChecker(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: url_loader_factory_(url_loader_factory) {
quick_answers_state_observation_.Observe(QuickAnswersState::Get());
}
SpellChecker::~SpellChecker() {
spellcheck_languages_.clear();
}
void SpellChecker::CheckSpelling(const std::string& word,
CheckSpellingCallback callback) {
auto iterator = spellcheck_languages_.begin();
if (iterator == spellcheck_languages_.end()) {
std::move(callback).Run(false, std::string());
return;
}
iterator->get()->CheckSpelling(
word,
base::BindOnce(&SpellChecker::CollectResults, base::Unretained(this),
word, std::move(callback), iterator,
iterator->get()->language(), languages_list_version_));
}
void SpellChecker::OnSettingsEnabled(bool enabled) {
CheckEligibilityAndUpdateLanguages(/*should_recreate_languages_list=*/false);
}
void SpellChecker::OnConsentStatusUpdated(prefs::ConsentStatus status) {
CheckEligibilityAndUpdateLanguages(/*should_recreate_languages_list=*/false);
}
void SpellChecker::OnApplicationLocaleReady(const std::string& locale) {
CheckEligibilityAndUpdateLanguages(/*should_recreate_languages_list=*/true);
}
void SpellChecker::OnPreferredLanguagesChanged(
const std::string& preferred_languages) {
CheckEligibilityAndUpdateLanguages(/*should_recreate_languages_list=*/true);
}
void SpellChecker::OnEligibilityChanged(bool eligible) {
CheckEligibilityAndUpdateLanguages(/*should_recreate_languages_list=*/false);
}
void SpellChecker::OnPrefsInitialized() {
CheckEligibilityAndUpdateLanguages(/*should_recreate_languages_list=*/false);
}
void SpellChecker::CheckEligibilityAndUpdateLanguages(
bool should_recreate_languages_list) {
// Still waiting for all of the states to be ready.
// TODO(b/340628526): remove this once all `QuickAnswersState` field become
// able to handle uinitialized cases. Callers should not need to care
// `QuickAnswersState` instance level initialization state.
if (!QuickAnswersState::Get()->prefs_initialized()) {
return;
}
if (!ShouldDownloadDictionaries()) {
spellcheck_languages_.clear();
languages_list_version_++;
return;
}
// Return if languages list is already initialized and no need to recreate.
if (!spellcheck_languages_.empty() && !should_recreate_languages_list) {
return;
}
// Add application language.
std::set<std::string> languages;
languages.insert(std::string(
l10n_util::GetLanguage(QuickAnswersState::Get()->application_locale())));
// Add preferred languages if supported.
auto preferred_languages_list =
base::SplitString(QuickAnswersState::Get()->preferred_languages(), ",",
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
for (const std::string& locale : preferred_languages_list) {
auto language = l10n_util::GetLanguage(locale);
if (QuickAnswersState::Get()->IsSupportedLanguage(language))
languages.insert(std::string(language));
}
spellcheck_languages_.clear();
languages_list_version_++;
for (auto language : languages) {
spellcheck_languages_.push_back(
std::make_unique<SpellCheckLanguage>(url_loader_factory_));
spellcheck_languages_.back()->Initialize(language);
}
}
void SpellChecker::CollectResults(const std::string& word,
CheckSpellingCallback callback,
SpellCheckLanguageIterator iterator,
const std::string& language,
int languages_list_version,
bool is_correct) {
if (is_correct) {
std::move(callback).Run(true, language);
return;
}
// The languages list has been updated, return false for the current call.
if (languages_list_version != languages_list_version_) {
std::move(callback).Run(false, std::string());
return;
}
iterator++;
if (iterator == spellcheck_languages_.end()) {
std::move(callback).Run(false, std::string());
return;
}
iterator->get()->CheckSpelling(
word,
base::BindOnce(&SpellChecker::CollectResults, base::Unretained(this),
word, std::move(callback), iterator,
iterator->get()->language(), languages_list_version));
}
} // namespace quick_answers
|