File: spell_checker.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,614 bytes parent folder | download | duplicates (7)
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
// 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.

#ifndef CHROMEOS_COMPONENTS_QUICK_ANSWERS_UTILS_SPELL_CHECKER_H_
#define CHROMEOS_COMPONENTS_QUICK_ANSWERS_UTILS_SPELL_CHECKER_H_

#include <memory>
#include <string>

#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chromeos/components/quick_answers/public/cpp/quick_answers_prefs.h"
#include "chromeos/components/quick_answers/public/cpp/quick_answers_state.h"
#include "chromeos/components/quick_answers/utils/spell_check_language.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace network {
class SharedURLLoaderFactory;
}  // namespace network

namespace quick_answers {

// Utility class for spell check.
class SpellChecker : public QuickAnswersStateObserver {
 public:
  using CheckSpellingCallback =
      base::OnceCallback<void(bool, const std::string&)>;
  using SpellCheckLanguageIterator =
      std::vector<std::unique_ptr<SpellCheckLanguage>>::iterator;

  explicit SpellChecker(
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);

  SpellChecker(const SpellChecker&) = delete;
  SpellChecker& operator=(const SpellChecker&) = delete;

  ~SpellChecker() override;

  // Check spelling of the given word, run |callback| with true if the word is
  // spelled correctly. Virtual for testing.
  virtual void CheckSpelling(const std::string& word,
                             CheckSpellingCallback callback);

  // QuickAnswersStateObserver:
  void OnSettingsEnabled(bool enabled) override;
  void OnConsentStatusUpdated(prefs::ConsentStatus status) override;
  void OnApplicationLocaleReady(const std::string& locale) override;
  void OnPreferredLanguagesChanged(
      const std::string& preferred_languages) override;
  void OnEligibilityChanged(bool eligible) override;
  void OnPrefsInitialized() override;

  base::WeakPtr<SpellChecker> GetWeakPtr() {
    return weak_factory_.GetWeakPtr();
  }

  std::vector<std::unique_ptr<SpellCheckLanguage>>&
  GetSpellcheckLanguagesForTesting() {
    return spellcheck_languages_;
  }

 private:
  // Check feature eligibility and correspondingly update languages list. If
  // |should_recreate_languages_list| is false, languages list will not be
  // updated as long as feature eligibility stay unchanged.
  // Called when the Quick answers states are updated.
  void CheckEligibilityAndUpdateLanguages(bool should_recreate_languages_list);

  // Collect spell check results from the language indicated by |iterator|. Run
  // |callback| with true if the word is found in the current language,
  // otherwise continue to check the next language.
  void CollectResults(const std::string& word,
                      CheckSpellingCallback callback,
                      SpellCheckLanguageIterator iterator,
                      const std::string& language,
                      int languages_list_version,
                      bool is_correct);

  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;

  // List of spell check languages.
  std::vector<std::unique_ptr<SpellCheckLanguage>> spellcheck_languages_;

  // Version of the languages list to invalidate pending calls if the languages
  // has ben updated.
  int languages_list_version_ = 0;

  base::ScopedObservation<QuickAnswersState, QuickAnswersStateObserver>
      quick_answers_state_observation_{this};

  base::WeakPtrFactory<SpellChecker> weak_factory_{this};
};

}  // namespace quick_answers

#endif  // CHROMEOS_COMPONENTS_QUICK_ANSWERS_UTILS_SPELL_CHECKER_H_