tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mozSpellChecker.h (6732B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef mozSpellChecker_h__
      7 #define mozSpellChecker_h__
      8 
      9 #include "mozilla/MozPromise.h"
     10 #include "nsCOMPtr.h"
     11 #include "nsCOMArray.h"
     12 #include "nsString.h"
     13 #include "mozIPersonalDictionary.h"
     14 #include "mozISpellCheckingEngine.h"
     15 #include "nsClassHashtable.h"
     16 #include "nsTArray.h"
     17 #include "nsCycleCollectionParticipant.h"
     18 
     19 class mozEnglishWordUtils;
     20 
     21 namespace mozilla {
     22 class RemoteSpellcheckEngineChild;
     23 class TextServicesDocument;
     24 typedef MozPromise<CopyableTArray<bool>, nsresult, false> CheckWordPromise;
     25 typedef MozPromise<CopyableTArray<nsString>, nsresult, false>
     26    SuggestionsPromise;
     27 }  // namespace mozilla
     28 
     29 class mozSpellChecker final {
     30 public:
     31  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(mozSpellChecker)
     32  NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(mozSpellChecker)
     33 
     34  static already_AddRefed<mozSpellChecker> Create() {
     35    RefPtr<mozSpellChecker> spellChecker = new mozSpellChecker();
     36    nsresult rv = spellChecker->Init();
     37    NS_ENSURE_SUCCESS(rv, nullptr);
     38    return spellChecker.forget();
     39  }
     40 
     41  /**
     42   * Tells the spellchecker what document to check.
     43   * @param aDoc is the document to check.
     44   * @param aFromStartOfDoc If true, start check from beginning of document,
     45   * if false, start check from current cursor position.
     46   */
     47  nsresult SetDocument(mozilla::TextServicesDocument* aTextServicesDocument,
     48                       bool aFromStartofDoc);
     49 
     50  /**
     51   * Selects (hilites) the next misspelled word in the document.
     52   * @param aWord will contain the misspelled word.
     53   * @param aSuggestions is an array of nsStrings, that represent the
     54   * suggested replacements for the misspelled word.
     55   */
     56  MOZ_CAN_RUN_SCRIPT
     57  nsresult NextMisspelledWord(nsAString& aWord,
     58                              nsTArray<nsString>& aSuggestions);
     59 
     60  /**
     61   * Checks if a word is misspelled. No document is required to use this method.
     62   * @param aWord is the word to check.
     63   * @param aIsMisspelled will be set to true if the word is misspelled.
     64   * @param aSuggestions is an array of nsStrings which represent the
     65   * suggested replacements for the misspelled word. The array will be empty
     66   * in chrome process if there aren't any suggestions. If suggestions is
     67   * unnecessary, use CheckWords of async version.
     68   */
     69  nsresult CheckWord(const nsAString& aWord, bool* aIsMisspelled,
     70                     nsTArray<nsString>* aSuggestions);
     71 
     72  /**
     73   * This is a flavor of CheckWord, is async version of CheckWord.
     74   * @Param aWords is array of words to check
     75   */
     76  RefPtr<mozilla::CheckWordPromise> CheckWords(
     77      const nsTArray<nsString>& aWords);
     78 
     79  /*
     80   * Checks if a word is misspelled, then get suggestion words if existed.
     81   */
     82  RefPtr<mozilla::SuggestionsPromise> Suggest(const nsAString& aWord,
     83                                              uint32_t aMaxCount);
     84 
     85  /**
     86   * Replaces the old word with the specified new word.
     87   * @param aOldWord is the word to be replaced.
     88   * @param aNewWord is the word that is to replace old word.
     89   * @param aAllOccurrences will replace all occurrences of old
     90   * word, in the document, with new word when it is true. If
     91   * false, it will replace the 1st occurrence only!
     92   */
     93  MOZ_CAN_RUN_SCRIPT
     94  nsresult Replace(const nsAString& aOldWord, const nsAString& aNewWord,
     95                   bool aAllOccurrences);
     96 
     97  /**
     98   * Ignores all occurrences of the specified word in the document.
     99   * @param aWord is the word to ignore.
    100   */
    101  nsresult IgnoreAll(const nsAString& aWord);
    102 
    103  /**
    104   * Add a word to the user's personal dictionary.
    105   * @param aWord is the word to add.
    106   */
    107  nsresult AddWordToPersonalDictionary(const nsAString& aWord);
    108 
    109  /**
    110   * Remove a word from the user's personal dictionary.
    111   * @param aWord is the word to remove.
    112   */
    113  nsresult RemoveWordFromPersonalDictionary(const nsAString& aWord);
    114 
    115  /**
    116   * Returns the list of strings representing the dictionaries
    117   * the spellchecker supports. It was suggested that the strings
    118   * returned be in the RFC 1766 format. This format looks something
    119   * like <ISO 639 language code>-<ISO 3166 country code>.
    120   * For example: en-US
    121   * @param aDictionaryList is an array of nsStrings that represent the
    122   * dictionaries supported by the spellchecker.
    123   */
    124  nsresult GetDictionaryList(nsTArray<nsCString>* aDictionaryList);
    125 
    126  /**
    127   * Returns a string representing the current dictionaries.
    128   * @param aDictionaries will contain the names of the dictionaries.
    129   * This name is the same string that is in the list returned
    130   * by GetDictionaryList().
    131   */
    132  nsresult GetCurrentDictionaries(nsTArray<nsCString>& aDictionaries);
    133 
    134  /**
    135   * Tells the spellchecker to use the specified dictionary.
    136   * @param aDictionary a string that is in the list returned
    137   * by GetDictionaryList() or an empty string . If aDictionary is
    138   * an empty array, the spellchecker will be disabled.
    139   */
    140  nsresult SetCurrentDictionary(const nsACString& aDictionary);
    141 
    142  /**
    143   * Tells the spellchecker to use the specified dictionaries.
    144   * @param aDictionaries an array of strings that is in the list returned
    145   * by GetDictionaryList() or an empty array. If aDictionaries is
    146   * an empty array, the spellchecker will be disabled.
    147   */
    148  RefPtr<mozilla::GenericPromise> SetCurrentDictionaries(
    149      const nsTArray<nsCString>& aDictionaries);
    150 
    151  /**
    152   * Tells the spellchecker to use a specific dictionary from list.
    153   * @param aList  a preferred dictionary list
    154   */
    155  RefPtr<mozilla::GenericPromise> SetCurrentDictionaryFromList(
    156      const nsTArray<nsCString>& aList);
    157 
    158  void DeleteRemoteEngine() { mEngine = nullptr; }
    159 
    160  mozilla::TextServicesDocument* GetTextServicesDocument();
    161 
    162 protected:
    163  mozSpellChecker();
    164  virtual ~mozSpellChecker();
    165 
    166  nsresult Init();
    167 
    168  RefPtr<mozEnglishWordUtils> mConverter;
    169  RefPtr<mozilla::TextServicesDocument> mTextServicesDocument;
    170  nsCOMPtr<mozIPersonalDictionary> mPersonalDictionary;
    171 
    172  nsCOMPtr<mozISpellCheckingEngine> mSpellCheckingEngine;
    173  bool mFromStart;
    174 
    175  nsTArray<nsCString> mCurrentDictionaries;
    176 
    177  MOZ_CAN_RUN_SCRIPT
    178  nsresult SetupDoc(int32_t* outBlockOffset);
    179 
    180  nsresult GetCurrentBlockIndex(
    181      mozilla::TextServicesDocument* aTextServicesDocument,
    182      int32_t* aOutBlockIndex);
    183 
    184  nsresult GetEngineList(nsCOMArray<mozISpellCheckingEngine>* aDictionaryList);
    185 
    186  mozilla::RemoteSpellcheckEngineChild* mEngine;
    187 
    188  friend class mozilla::RemoteSpellcheckEngineChild;
    189 };
    190 #endif  // mozSpellChecker_h__