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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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() {
static base::NoDestructor<SpellCheckHostChromeImpl::Binder> binder;
return *binder;
}
} // namespace
SpellCheckHostChromeImpl::SpellCheckHostChromeImpl(int render_process_id)
:
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
document_tag_(spellcheck_platform::GetDocumentTag()),
#endif
render_process_id_(render_process_id) {
}
SpellCheckHostChromeImpl::~SpellCheckHostChromeImpl() {
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
spellcheck_platform::CloseDocumentWithTag(document_tag_);
#endif
}
// static
void SpellCheckHostChromeImpl::Create(
int render_process_id,
mojo::PendingReceiver<spellcheck::mojom::SpellCheckHost> receiver) {
auto& binder = GetSpellCheckHostBinderOverride();
if (binder) {
binder.Run(render_process_id, std::move(receiver));
return;
}
mojo::MakeSelfOwnedReceiver(
std::make_unique<SpellCheckHostChromeImpl>(render_process_id),
std::move(receiver));
}
// static
void SpellCheckHostChromeImpl::OverrideBinderForTesting(Binder binder) {
GetSpellCheckHostBinderOverride() = std::move(binder);
}
void SpellCheckHostChromeImpl::NotifyChecked(const std::u16string& word,
bool misspelled) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
SpellcheckService* spellcheck = GetSpellcheckService();
if (!spellcheck)
return; // Teardown.
if (spellcheck->GetMetrics())
spellcheck->GetMetrics()->RecordCheckedWordStats(word, misspelled);
}
#if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
void SpellCheckHostChromeImpl::CallSpellingService(
const std::u16string& text,
CallSpellingServiceCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (text.empty()) {
std::move(callback).Run(false, std::vector<SpellCheckResult>());
mojo::ReportBadMessage("Requested spelling service with empty text");
return;
}
// Checks the user profile and sends a JSON-RPC request to the Spelling
// service if a user enables the "Use enhanced spell check" option. When
// a response is received (including an error) from the remote Spelling
// service, calls CallSpellingServiceDone.
auto* host = content::RenderProcessHost::FromID(render_process_id_);
if (!host) {
std::move(callback).Run(false, std::vector<SpellCheckResult>());
return;
}
client_.RequestTextCheck(
host->GetBrowserContext(), SpellingServiceClient::SPELLCHECK, text,
base::BindOnce(&SpellCheckHostChromeImpl::CallSpellingServiceDone,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void SpellCheckHostChromeImpl::CallSpellingServiceDone(
CallSpellingServiceCallback callback,
bool success,
const std::u16string& text,
const std::vector<SpellCheckResult>& service_results) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
SpellcheckService* spellcheck = GetSpellcheckService();
if (!spellcheck) { // Teardown.
std::move(callback).Run(false, std::vector<SpellCheckResult>());
return;
}
std::vector<SpellCheckResult> results = FilterCustomWordResults(
base::UTF16ToUTF8(text), *spellcheck->GetCustomDictionary(),
service_results);
std::move(callback).Run(success, results);
}
// static
std::vector<SpellCheckResult> SpellCheckHostChromeImpl::FilterCustomWordResults(
const std::string& text,
const SpellcheckCustomDictionary& custom_dictionary,
const std::vector<SpellCheckResult>& service_results) {
std::vector<SpellCheckResult> results;
for (const auto& result : service_results) {
const std::string word = text.substr(result.location, result.length);
if (!custom_dictionary.HasWord(word))
results.push_back(result);
}
return results;
}
#endif // BUILDFLAG(USE_RENDERER_SPELLCHECKER)
#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);
// Initialize the spellcheck service if needed. The service will send the
// language code for text breaking to the renderer. (Text breaking is required
// for the context menu to show spelling suggestions.) Initialization must
// happen on UI thread.
SpellcheckService* spellcheck = GetSpellcheckService();
if (!spellcheck) { // Teardown.
std::move(callback).Run({});
return;
}
// OK to store unretained |this| in a |SpellingRequest| owned by |this|.
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)) {
// Initialize the spellcheck service if needed. Initialization must
// happen on UI thread.
SpellcheckService* spellcheck = GetSpellcheckService();
if (!spellcheck) { // Teardown.
std::move(callback).Run(/*dictionaries=*/{}, /*custom_words=*/{},
/*enable=*/false);
return;
}
dictionaries_loaded_callback_ = std::move(callback);
spellcheck->InitializeDictionaries(
base::BindOnce(&SpellCheckHostChromeImpl::OnDictionariesInitialized,
weak_factory_.GetWeakPtr()));
return;
}
NOTREACHED();
}
void SpellCheckHostChromeImpl::OnDictionariesInitialized() {
DCHECK(dictionaries_loaded_callback_);
SpellcheckService* spellcheck = GetSpellcheckService();
if (!spellcheck) { // Teardown.
std::move(dictionaries_loaded_callback_)
.Run(/*dictionaries=*/{}, /*custom_words=*/{},
/*enable=*/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 // BUILDFLAG(IS_WIN)
void SpellCheckHostChromeImpl::OnRequestFinished(SpellingRequest* request) {
auto iterator = requests_.find(request);
requests_.erase(iterator);
}
// static
void SpellCheckHostChromeImpl::CombineResultsForTesting(
std::vector<SpellCheckResult>* remote_results,
const std::vector<SpellCheckResult>& local_results) {
SpellingRequest::CombineResults(remote_results, local_results);
}
#endif // BUILDFLAG(USE_BROWSER_SPELLCHECKER) &&
// BUILDFLAG(ENABLE_SPELLING_SERVICE)
SpellcheckService* SpellCheckHostChromeImpl::GetSpellcheckService() const {
auto* host = content::RenderProcessHost::FromID(render_process_id_);
if (!host)
return nullptr;
return SpellcheckServiceFactory::GetForContext(host->GetBrowserContext());
}
|