spellcheck.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/spellcheck/renderer/spellcheck.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <algorithm>
  8. #include <memory>
  9. #include <utility>
  10. #include "base/bind.h"
  11. #include "base/check_op.h"
  12. #include "base/command_line.h"
  13. #include "base/location.h"
  14. #include "base/notreached.h"
  15. #include "base/observer_list.h"
  16. #include "base/task/single_thread_task_runner.h"
  17. #include "base/threading/thread_task_runner_handle.h"
  18. #include "base/time/time.h"
  19. #include "build/build_config.h"
  20. #include "components/spellcheck/common/spellcheck_common.h"
  21. #include "components/spellcheck/common/spellcheck_features.h"
  22. #include "components/spellcheck/common/spellcheck_result.h"
  23. #include "components/spellcheck/renderer/spellcheck_language.h"
  24. #include "components/spellcheck/renderer/spellcheck_provider.h"
  25. #include "components/spellcheck/renderer/spellcheck_renderer_metrics.h"
  26. #include "components/spellcheck/spellcheck_buildflags.h"
  27. #include "content/public/renderer/render_frame.h"
  28. #include "content/public/renderer/render_frame_visitor.h"
  29. #include "content/public/renderer/render_thread.h"
  30. #include "third_party/blink/public/platform/web_string.h"
  31. #include "third_party/blink/public/platform/web_vector.h"
  32. #include "third_party/blink/public/web/web_local_frame.h"
  33. #include "third_party/blink/public/web/web_text_checking_completion.h"
  34. #include "third_party/blink/public/web/web_text_checking_result.h"
  35. #include "third_party/blink/public/web/web_text_decoration_type.h"
  36. using blink::WebVector;
  37. using blink::WebString;
  38. using blink::WebTextCheckingResult;
  39. using blink::WebTextDecorationType;
  40. namespace {
  41. const int kNoOffset = 0;
  42. const int kNoTag = 0;
  43. class UpdateSpellcheckEnabled : public content::RenderFrameVisitor {
  44. public:
  45. explicit UpdateSpellcheckEnabled(bool enabled) : enabled_(enabled) {}
  46. UpdateSpellcheckEnabled(const UpdateSpellcheckEnabled&) = delete;
  47. UpdateSpellcheckEnabled& operator=(const UpdateSpellcheckEnabled&) = delete;
  48. bool Visit(content::RenderFrame* render_frame) override;
  49. private:
  50. bool enabled_; // New spellcheck-enabled state.
  51. };
  52. bool UpdateSpellcheckEnabled::Visit(content::RenderFrame* render_frame) {
  53. if (!enabled_) {
  54. if (render_frame && render_frame->GetWebFrame())
  55. render_frame->GetWebFrame()->RemoveSpellingMarkers();
  56. }
  57. return true;
  58. }
  59. WebVector<WebString> ConvertToWebStringFromUtf8(
  60. const std::set<std::string>& words) {
  61. WebVector<WebString> result(words.size());
  62. std::transform(words.begin(), words.end(), result.begin(),
  63. [](const std::string& w) { return WebString::FromUTF8(w); });
  64. return result;
  65. }
  66. bool IsApostrophe(char16_t c) {
  67. const char16_t kApostrophe = 0x27;
  68. const char16_t kRightSingleQuotationMark = 0x2019;
  69. return c == kApostrophe || c == kRightSingleQuotationMark;
  70. }
  71. // Makes sure that the apostrophes in the |spelling_suggestion| are the same
  72. // type as in the |misspelled_word| and in the same order. Ignore differences in
  73. // the number of apostrophes.
  74. void PreserveOriginalApostropheTypes(const std::u16string& misspelled_word,
  75. std::u16string* spelling_suggestion) {
  76. auto it = spelling_suggestion->begin();
  77. for (const char16_t& c : misspelled_word) {
  78. if (IsApostrophe(c)) {
  79. it = std::find_if(it, spelling_suggestion->end(), IsApostrophe);
  80. if (it == spelling_suggestion->end())
  81. return;
  82. *it++ = c;
  83. }
  84. }
  85. }
  86. std::vector<WebString> FilterReplacementSuggestions(
  87. const std::u16string& misspelled_word,
  88. const std::vector<std::u16string>& replacements) {
  89. std::vector<WebString> replacements_filtered;
  90. for (std::u16string replacement : replacements) {
  91. // Use the same types of apostrophes as in the mispelled word.
  92. PreserveOriginalApostropheTypes(misspelled_word, &replacement);
  93. // Ignore suggestions that are just changing the apostrophe type
  94. // (straight vs. typographical)
  95. if (replacement == misspelled_word)
  96. continue;
  97. replacements_filtered.push_back(WebString::FromUTF16(replacement));
  98. }
  99. return replacements_filtered;
  100. }
  101. } // namespace
  102. class SpellCheck::SpellcheckRequest {
  103. public:
  104. SpellcheckRequest(
  105. const std::u16string& text,
  106. std::unique_ptr<blink::WebTextCheckingCompletion> completion)
  107. : text_(text),
  108. completion_(std::move(completion)),
  109. start_ticks_(base::TimeTicks::Now()) {
  110. DCHECK(completion_);
  111. }
  112. SpellcheckRequest(const SpellcheckRequest&) = delete;
  113. SpellcheckRequest& operator=(const SpellcheckRequest&) = delete;
  114. ~SpellcheckRequest() {}
  115. std::u16string text() { return text_; }
  116. blink::WebTextCheckingCompletion* completion() { return completion_.get(); }
  117. base::TimeTicks start_ticks() { return start_ticks_; }
  118. private:
  119. std::u16string text_; // Text to be checked in this task.
  120. // The interface to send the misspelled ranges to WebKit.
  121. std::unique_ptr<blink::WebTextCheckingCompletion> completion_;
  122. // The time ticks at which this request was created
  123. base::TimeTicks start_ticks_;
  124. };
  125. // Initializes SpellCheck object.
  126. // spellcheck_enabled_ currently MUST be set to true, due to peculiarities of
  127. // the initialization sequence.
  128. // Since it defaults to true, newly created SpellCheckProviders will enable
  129. // spellchecking. After the first word is typed, the provider requests a check,
  130. // which in turn triggers the delayed initialization sequence in SpellCheck.
  131. // This does send a message to the browser side, which triggers the creation
  132. // of the SpellcheckService. That does create the observer for the preference
  133. // responsible for enabling/disabling checking, which allows subsequent changes
  134. // to that preference to be sent to all SpellCheckProviders.
  135. // Setting |spellcheck_enabled_| to false by default prevents that mechanism,
  136. // and as such the SpellCheckProviders will never be notified of different
  137. // values.
  138. // TODO(groby): Simplify this.
  139. SpellCheck::SpellCheck(
  140. service_manager::LocalInterfaceProvider* embedder_provider)
  141. : embedder_provider_(embedder_provider), spellcheck_enabled_(true) {
  142. DCHECK(embedder_provider);
  143. }
  144. SpellCheck::~SpellCheck() = default;
  145. void SpellCheck::BindReceiver(
  146. mojo::PendingReceiver<spellcheck::mojom::SpellChecker> receiver) {
  147. receivers_.Add(this, std::move(receiver));
  148. }
  149. void SpellCheck::Initialize(
  150. std::vector<spellcheck::mojom::SpellCheckBDictLanguagePtr> dictionaries,
  151. const std::vector<std::string>& custom_words,
  152. bool enable) {
  153. languages_.clear();
  154. for (const auto& dictionary : dictionaries)
  155. AddSpellcheckLanguage(std::move(dictionary->file), dictionary->language);
  156. custom_dictionary_.Init(
  157. std::set<std::string>(custom_words.begin(), custom_words.end()));
  158. #if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  159. if (!spellcheck::UseBrowserSpellChecker()) {
  160. PostDelayedSpellCheckTask(pending_request_param_.release());
  161. }
  162. #endif
  163. spellcheck_enabled_ = enable;
  164. UpdateSpellcheckEnabled updater(enable);
  165. content::RenderFrame::ForEach(&updater);
  166. }
  167. void SpellCheck::CustomDictionaryChanged(
  168. const std::vector<std::string>& words_added,
  169. const std::vector<std::string>& words_removed) {
  170. const std::set<std::string> added(words_added.begin(), words_added.end());
  171. NotifyDictionaryObservers(ConvertToWebStringFromUtf8(added));
  172. custom_dictionary_.OnCustomDictionaryChanged(
  173. added, std::set<std::string>(words_removed.begin(), words_removed.end()));
  174. }
  175. // TODO(groby): Make sure we always have a spelling engine, even before
  176. // AddSpellcheckLanguage() is called.
  177. void SpellCheck::AddSpellcheckLanguage(base::File file,
  178. const std::string& language) {
  179. languages_.push_back(
  180. std::make_unique<SpellcheckLanguage>(embedder_provider_));
  181. languages_.back()->Init(std::move(file), language);
  182. }
  183. bool SpellCheck::SpellCheckWord(const char16_t* text_begin,
  184. size_t position_in_text,
  185. size_t text_length,
  186. int tag,
  187. size_t* misspelling_start,
  188. size_t* misspelling_len,
  189. std::nullptr_t null_suggestions_ptr) {
  190. return SpellCheckWord(
  191. text_begin, position_in_text, text_length, tag, misspelling_start,
  192. misspelling_len,
  193. static_cast<spellcheck::PerLanguageSuggestions*>(nullptr));
  194. }
  195. bool SpellCheck::SpellCheckWord(
  196. const char16_t* text_begin,
  197. size_t position_in_text,
  198. size_t text_length,
  199. int tag,
  200. size_t* misspelling_start,
  201. size_t* misspelling_len,
  202. std::vector<std::u16string>* optional_suggestions) {
  203. if (!optional_suggestions) {
  204. return SpellCheckWord(text_begin, position_in_text, text_length, tag,
  205. misspelling_start, misspelling_len, nullptr);
  206. }
  207. bool result;
  208. spellcheck::PerLanguageSuggestions per_language_suggestions;
  209. result = SpellCheckWord(text_begin, position_in_text, text_length, tag,
  210. misspelling_start, misspelling_len,
  211. &per_language_suggestions);
  212. spellcheck::FillSuggestions(per_language_suggestions, optional_suggestions);
  213. return result;
  214. }
  215. bool SpellCheck::SpellCheckWord(
  216. const char16_t* text_begin,
  217. size_t position_in_text,
  218. size_t text_length,
  219. int tag,
  220. size_t* misspelling_start,
  221. size_t* misspelling_len,
  222. spellcheck::PerLanguageSuggestions* optional_per_language_suggestions) {
  223. DCHECK(text_length >= position_in_text);
  224. DCHECK(misspelling_start && misspelling_len) << "Out vars must be given.";
  225. // Do nothing if we need to delay initialization. (Rather than blocking,
  226. // report the word as correctly spelled.)
  227. if (InitializeIfNeeded())
  228. return true;
  229. // To prevent an infinite loop below, ensure that at least one language is
  230. // enabled before starting the check. If no language is enabled, we should
  231. // never report a spelling mistake, so return true here.
  232. if (EnabledLanguageCount() == 0) {
  233. return true;
  234. }
  235. // These are for holding misspelling or skippable word positions and lengths
  236. // between calls to SpellcheckLanguage::SpellCheckWord.
  237. size_t possible_misspelling_start;
  238. size_t possible_misspelling_len;
  239. // The longest sequence of text that all languages agree is skippable.
  240. size_t agreed_skippable_len;
  241. // A vector of vectors containing spelling suggestions from different
  242. // languages.
  243. std::vector<std::vector<std::u16string>> suggestions_list;
  244. // A vector to hold a language's misspelling suggestions between spellcheck
  245. // calls.
  246. std::vector<std::u16string> language_suggestions;
  247. // This loop only advances if all languages agree that a sequence of text is
  248. // skippable.
  249. for (; position_in_text <= text_length;
  250. position_in_text += agreed_skippable_len) {
  251. // Reseting |agreed_skippable_len| to the worst-case length each time
  252. // prevents some unnecessary iterations.
  253. agreed_skippable_len = text_length;
  254. *misspelling_start = 0;
  255. *misspelling_len = 0;
  256. suggestions_list.clear();
  257. for (auto language = languages_.begin(); language != languages_.end();) {
  258. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  259. if (!(*language)->IsEnabled()) {
  260. // In the case of hybrid spell checking on Windows, languages that are
  261. // handled on the browser side are marked as disabled on the renderer
  262. // side. We do not want to return IS_CORRECT for those languages, so we
  263. // simply skip them.
  264. language++;
  265. continue;
  266. }
  267. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  268. language_suggestions.clear();
  269. SpellcheckLanguage::SpellcheckWordResult result =
  270. (*language)->SpellCheckWord(
  271. text_begin, position_in_text, text_length, tag,
  272. &possible_misspelling_start, &possible_misspelling_len,
  273. optional_per_language_suggestions ? &language_suggestions
  274. : nullptr);
  275. switch (result) {
  276. case SpellcheckLanguage::SpellcheckWordResult::IS_CORRECT:
  277. *misspelling_start = 0;
  278. *misspelling_len = 0;
  279. return true;
  280. case SpellcheckLanguage::SpellcheckWordResult::IS_SKIPPABLE:
  281. agreed_skippable_len =
  282. std::min(agreed_skippable_len, possible_misspelling_len);
  283. // If true, this means the spellchecker moved past a word that was
  284. // previously determined to be misspelled or skippable, which means
  285. // another spellcheck language marked it as correct.
  286. if (position_in_text != possible_misspelling_start) {
  287. *misspelling_len = 0;
  288. position_in_text = possible_misspelling_start;
  289. suggestions_list.clear();
  290. language = languages_.begin();
  291. } else {
  292. language++;
  293. }
  294. break;
  295. case SpellcheckLanguage::SpellcheckWordResult::IS_MISSPELLED:
  296. *misspelling_start = possible_misspelling_start;
  297. *misspelling_len = possible_misspelling_len;
  298. // If true, this means the spellchecker moved past a word that was
  299. // previously determined to be misspelled or skippable, which means
  300. // another spellcheck language marked it as correct.
  301. if (position_in_text != *misspelling_start) {
  302. suggestions_list.clear();
  303. language = languages_.begin();
  304. position_in_text = *misspelling_start;
  305. } else {
  306. suggestions_list.push_back(language_suggestions);
  307. language++;
  308. }
  309. break;
  310. }
  311. }
  312. // If |*misspelling_len| is non-zero, that means at least one language
  313. // marked a word misspelled and no other language considered it correct.
  314. if (*misspelling_len != 0) {
  315. if (optional_per_language_suggestions) {
  316. optional_per_language_suggestions->swap(suggestions_list);
  317. }
  318. return false;
  319. }
  320. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  321. // If we're performing a hybrid spell check, we're only interested in
  322. // knowing whether some Hunspell languages considered this text range as
  323. // correctly spelled. If no misspellings were found, but the entire text was
  324. // skipped, it means that no Hunspell language considered this text
  325. // correct, so we should return false here.
  326. if (spellcheck::UseBrowserSpellChecker() &&
  327. EnabledLanguageCount() != LanguageCount() &&
  328. agreed_skippable_len == text_length) {
  329. return false;
  330. }
  331. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  332. }
  333. NOTREACHED();
  334. return true;
  335. }
  336. #if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  337. bool SpellCheck::SpellCheckParagraph(
  338. const std::u16string& text,
  339. WebVector<WebTextCheckingResult>* results) {
  340. DCHECK(results);
  341. std::vector<WebTextCheckingResult> textcheck_results;
  342. size_t length = text.length();
  343. size_t position_in_text = 0;
  344. // Spellcheck::SpellCheckWord() automatically breaks text into words and
  345. // checks the spellings of the extracted words. This function sets the
  346. // position and length of the first misspelled word and returns false when
  347. // the text includes misspelled words. Therefore, we just repeat calling the
  348. // function until it returns true to check the whole text.
  349. size_t misspelling_start = 0;
  350. size_t misspelling_length = 0;
  351. while (position_in_text <= length) {
  352. if (SpellCheckWord(text.c_str(), position_in_text, length, kNoTag,
  353. &misspelling_start, &misspelling_length, nullptr)) {
  354. results->Assign(textcheck_results);
  355. return true;
  356. }
  357. if (!custom_dictionary_.SpellCheckWord(text, misspelling_start,
  358. misspelling_length)) {
  359. textcheck_results.push_back(
  360. WebTextCheckingResult(blink::kWebTextDecorationTypeSpelling,
  361. base::checked_cast<int>(misspelling_start),
  362. base::checked_cast<int>(misspelling_length)));
  363. }
  364. position_in_text = misspelling_start + misspelling_length;
  365. }
  366. results->Assign(textcheck_results);
  367. return false;
  368. }
  369. void SpellCheck::RequestTextChecking(
  370. const std::u16string& text,
  371. std::unique_ptr<blink::WebTextCheckingCompletion> completion) {
  372. // Clean up the previous request before starting a new request.
  373. if (pending_request_param_)
  374. pending_request_param_->completion()->DidCancelCheckingText();
  375. pending_request_param_ =
  376. std::make_unique<SpellcheckRequest>(text, std::move(completion));
  377. // We will check this text after we finish loading the hunspell dictionary.
  378. if (InitializeIfNeeded())
  379. return;
  380. PostDelayedSpellCheckTask(pending_request_param_.release());
  381. }
  382. #endif
  383. bool SpellCheck::InitializeIfNeeded() {
  384. if (languages_.empty())
  385. return true;
  386. bool initialize_if_needed = false;
  387. for (auto& language : languages_)
  388. initialize_if_needed |= language->InitializeIfNeeded();
  389. return initialize_if_needed;
  390. }
  391. #if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  392. void SpellCheck::PostDelayedSpellCheckTask(SpellcheckRequest* request) {
  393. if (!request)
  394. return;
  395. base::ThreadTaskRunnerHandle::Get()->PostTask(
  396. FROM_HERE, base::BindOnce(&SpellCheck::PerformSpellCheck, AsWeakPtr(),
  397. base::Owned(request)));
  398. }
  399. #endif
  400. #if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
  401. void SpellCheck::PerformSpellCheck(SpellcheckRequest* param) {
  402. DCHECK(param);
  403. if (languages_.empty() ||
  404. std::find_if(languages_.begin(), languages_.end(),
  405. [](std::unique_ptr<SpellcheckLanguage>& language) {
  406. return !language->IsEnabled();
  407. }) != languages_.end()) {
  408. param->completion()->DidCancelCheckingText();
  409. } else {
  410. WebVector<blink::WebTextCheckingResult> results;
  411. SpellCheckParagraph(param->text(), &results);
  412. param->completion()->DidFinishCheckingText(results);
  413. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  414. spellcheck_renderer_metrics::RecordSpellcheckDuration(
  415. base::TimeTicks::Now() - param->start_ticks(),
  416. /*used_hunspell=*/true, /*used_native=*/false);
  417. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  418. }
  419. }
  420. #endif
  421. void SpellCheck::CreateTextCheckingResults(
  422. ResultFilter filter,
  423. int line_offset,
  424. const std::u16string& line_text,
  425. const std::vector<SpellCheckResult>& spellcheck_results,
  426. WebVector<WebTextCheckingResult>* textcheck_results) {
  427. DCHECK(!line_text.empty());
  428. std::vector<WebTextCheckingResult> results;
  429. for (const SpellCheckResult& spellcheck_result : spellcheck_results) {
  430. DCHECK_LE(static_cast<size_t>(spellcheck_result.location),
  431. line_text.length());
  432. DCHECK_LE(static_cast<size_t>(spellcheck_result.location +
  433. spellcheck_result.length),
  434. line_text.length());
  435. const std::u16string& misspelled_word =
  436. line_text.substr(spellcheck_result.location, spellcheck_result.length);
  437. const std::vector<std::u16string>& replacements =
  438. spellcheck_result.replacements;
  439. SpellCheckResult::Decoration decoration = spellcheck_result.decoration;
  440. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  441. // Ignore words that are in a script not supported by any of the enabled
  442. // spellcheck languages.
  443. if (spellcheck::UseBrowserSpellChecker() &&
  444. !IsWordInSupportedScript(misspelled_word)) {
  445. continue;
  446. }
  447. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  448. // Ignore words in custom dictionary.
  449. if (custom_dictionary_.SpellCheckWord(misspelled_word, 0,
  450. misspelled_word.length())) {
  451. continue;
  452. }
  453. std::vector<WebString> replacements_filtered =
  454. FilterReplacementSuggestions(misspelled_word, replacements);
  455. // If the spellchecker suggested replacements, but they were all just
  456. // changing apostrophe styles, ignore this misspelling. If there were never
  457. // any suggested replacements, keep the misspelling.
  458. if (replacements_filtered.empty() && !replacements.empty())
  459. continue;
  460. if (filter == USE_HUNSPELL_FOR_GRAMMAR) {
  461. // Double-check misspelled words with Hunspell and attach grammar markers
  462. // to them if Hunspell tells us they are correct words, i.e. they are
  463. // probably contextually-misspelled words.
  464. size_t unused_misspelling_start = 0;
  465. size_t unused_misspelling_length = 0;
  466. if (decoration == SpellCheckResult::SPELLING &&
  467. SpellCheckWord(misspelled_word.c_str(), kNoOffset,
  468. misspelled_word.length(), kNoTag,
  469. &unused_misspelling_start, &unused_misspelling_length,
  470. nullptr)) {
  471. decoration = SpellCheckResult::GRAMMAR;
  472. }
  473. }
  474. #if BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  475. else if (filter == USE_HUNSPELL_FOR_HYBRID_CHECK &&
  476. spellcheck::UseBrowserSpellChecker() &&
  477. EnabledLanguageCount() > 0) {
  478. // Remove the suggestions that were generated by the native spell checker,
  479. // otherwise Blink will cache them without asking for the suggestions
  480. // from Hunspell.
  481. replacements_filtered.clear();
  482. // The native spell checker was not able to check all locales. Double-
  483. // check misspelled words with Hunspell for the unchecked locales
  484. // and remove the results if Hunspell tells us the words are correctly
  485. // spelled in those locales.
  486. size_t unused_misspelling_start = 0;
  487. size_t unused_misspelling_length = 0;
  488. if (SpellCheckWord(misspelled_word.c_str(), kNoOffset,
  489. misspelled_word.length(), kNoTag,
  490. &unused_misspelling_start, &unused_misspelling_length,
  491. nullptr)) {
  492. // Correctly spelled in a Hunspell locale. If enhanced spell check was
  493. // used, turn the spelling mistake into a grammar mistake (local and
  494. // remote checks disagree, so the word is probably only contextually
  495. // misspelled). If enhanced spell check wasn't used, remove this
  496. // misspelling.
  497. if (spellcheck_result.spelling_service_used) {
  498. decoration = SpellCheckResult::GRAMMAR;
  499. } else {
  500. continue;
  501. }
  502. }
  503. }
  504. #endif // BUILDFLAG(IS_WIN) && BUILDFLAG(USE_BROWSER_SPELLCHECKER)
  505. results.push_back(
  506. WebTextCheckingResult(static_cast<WebTextDecorationType>(decoration),
  507. line_offset + spellcheck_result.location,
  508. spellcheck_result.length, replacements_filtered));
  509. }
  510. textcheck_results->Assign(results);
  511. }
  512. bool SpellCheck::IsSpellcheckEnabled() {
  513. #if BUILDFLAG(IS_ANDROID)
  514. if (!spellcheck::IsAndroidSpellCheckFeatureEnabled()) return false;
  515. #endif
  516. return spellcheck_enabled_;
  517. }
  518. void SpellCheck::AddDictionaryUpdateObserver(
  519. DictionaryUpdateObserver* observer) {
  520. return dictionary_update_observers_.AddObserver(observer);
  521. }
  522. void SpellCheck::RemoveDictionaryUpdateObserver(
  523. DictionaryUpdateObserver* observer) {
  524. return dictionary_update_observers_.RemoveObserver(observer);
  525. }
  526. size_t SpellCheck::LanguageCount() {
  527. return languages_.size();
  528. }
  529. size_t SpellCheck::EnabledLanguageCount() {
  530. return std::count_if(languages_.begin(), languages_.end(),
  531. [](std::unique_ptr<SpellcheckLanguage>& language) {
  532. return language->IsEnabled();
  533. });
  534. }
  535. void SpellCheck::NotifyDictionaryObservers(
  536. const WebVector<WebString>& words_added) {
  537. for (auto& observer : dictionary_update_observers_) {
  538. observer.OnDictionaryUpdated(words_added);
  539. }
  540. }
  541. bool SpellCheck::IsWordInSupportedScript(const std::u16string& word) const {
  542. return std::find_if(languages_.begin(), languages_.end(),
  543. [word](const auto& language) {
  544. return language->IsTextInSameScript(word);
  545. }) != languages_.end();
  546. }