summaryrefslogtreecommitdiffstats
path: root/extensions/spellcheck/src/mozSpellChecker.h
blob: aec4a95d65f6dd35e3915f5e97dbc897e8284b43 (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozSpellChecker_h__
#define mozSpellChecker_h__

#include "mozilla/MozPromise.h"
#include "nsCOMPtr.h"
#include "nsCOMArray.h"
#include "nsString.h"
#include "mozIPersonalDictionary.h"
#include "mozISpellCheckingEngine.h"
#include "nsClassHashtable.h"
#include "nsTArray.h"
#include "nsCycleCollectionParticipant.h"

class mozEnglishWordUtils;

namespace mozilla {
class RemoteSpellcheckEngineChild;
class TextServicesDocument;
typedef MozPromise<CopyableTArray<bool>, nsresult, false> CheckWordPromise;
typedef MozPromise<CopyableTArray<nsString>, nsresult, false>
    SuggestionsPromise;
}  // namespace mozilla

class mozSpellChecker final {
 public:
  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(mozSpellChecker)
  NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(mozSpellChecker)

  static already_AddRefed<mozSpellChecker> Create() {
    RefPtr<mozSpellChecker> spellChecker = new mozSpellChecker();
    nsresult rv = spellChecker->Init();
    NS_ENSURE_SUCCESS(rv, nullptr);
    return spellChecker.forget();
  }

  /**
   * Tells the spellchecker what document to check.
   * @param aDoc is the document to check.
   * @param aFromStartOfDoc If true, start check from beginning of document,
   * if false, start check from current cursor position.
   */
  nsresult SetDocument(mozilla::TextServicesDocument* aTextServicesDocument,
                       bool aFromStartofDoc);

  /**
   * Selects (hilites) the next misspelled word in the document.
   * @param aWord will contain the misspelled word.
   * @param aSuggestions is an array of nsStrings, that represent the
   * suggested replacements for the misspelled word.
   */
  MOZ_CAN_RUN_SCRIPT
  nsresult NextMisspelledWord(nsAString& aWord,
                              nsTArray<nsString>& aSuggestions);

  /**
   * Checks if a word is misspelled. No document is required to use this method.
   * @param aWord is the word to check.
   * @param aIsMisspelled will be set to true if the word is misspelled.
   * @param aSuggestions is an array of nsStrings which represent the
   * suggested replacements for the misspelled word. The array will be empty
   * in chrome process if there aren't any suggestions. If suggestions is
   * unnecessary, use CheckWords of async version.
   */
  nsresult CheckWord(const nsAString& aWord, bool* aIsMisspelled,
                     nsTArray<nsString>* aSuggestions);

  /**
   * This is a flavor of CheckWord, is async version of CheckWord.
   * @Param aWords is array of words to check
   */
  RefPtr<mozilla::CheckWordPromise> CheckWords(
      const nsTArray<nsString>& aWords);

  /*
   * Checks if a word is misspelled, then get suggestion words if existed.
   */
  RefPtr<mozilla::SuggestionsPromise> Suggest(const nsAString& aWord,
                                              uint32_t aMaxCount);

  /**
   * Replaces the old word with the specified new word.
   * @param aOldWord is the word to be replaced.
   * @param aNewWord is the word that is to replace old word.
   * @param aAllOccurrences will replace all occurrences of old
   * word, in the document, with new word when it is true. If
   * false, it will replace the 1st occurrence only!
   */
  MOZ_CAN_RUN_SCRIPT
  nsresult Replace(const nsAString& aOldWord, const nsAString& aNewWord,
                   bool aAllOccurrences);

  /**
   * Ignores all occurrences of the specified word in the document.
   * @param aWord is the word to ignore.
   */
  nsresult IgnoreAll(const nsAString& aWord);

  /**
   * Add a word to the user's personal dictionary.
   * @param aWord is the word to add.
   */
  nsresult AddWordToPersonalDictionary(const nsAString& aWord);

  /**
   * Remove a word from the user's personal dictionary.
   * @param aWord is the word to remove.
   */
  nsresult RemoveWordFromPersonalDictionary(const nsAString& aWord);

  /**
   * Returns the list of words in the user's personal dictionary.
   * @param aWordList is an array of nsStrings that represent the
   * list of words in the user's personal dictionary.
   */
  nsresult GetPersonalDictionary(nsTArray<nsString>* aWordList);

  /**
   * Returns the list of strings representing the dictionaries
   * the spellchecker supports. It was suggested that the strings
   * returned be in the RFC 1766 format. This format looks something
   * like <ISO 639 language code>-<ISO 3166 country code>.
   * For example: en-US
   * @param aDictionaryList is an array of nsStrings that represent the
   * dictionaries supported by the spellchecker.
   */
  nsresult GetDictionaryList(nsTArray<nsCString>* aDictionaryList);

  /**
   * Returns a string representing the current dictionaries.
   * @param aDictionaries will contain the names of the dictionaries.
   * This name is the same string that is in the list returned
   * by GetDictionaryList().
   */
  nsresult GetCurrentDictionaries(nsTArray<nsCString>& aDictionaries);

  /**
   * Tells the spellchecker to use the specified dictionary.
   * @param aDictionary a string that is in the list returned
   * by GetDictionaryList() or an empty string . If aDictionary is
   * an empty array, the spellchecker will be disabled.
   */
  nsresult SetCurrentDictionary(const nsACString& aDictionary);

  /**
   * Tells the spellchecker to use the specified dictionaries.
   * @param aDictionaries an array of strings that is in the list returned
   * by GetDictionaryList() or an empty array. If aDictionaries is
   * an empty array, the spellchecker will be disabled.
   */
  RefPtr<mozilla::GenericPromise> SetCurrentDictionaries(
      const nsTArray<nsCString>& aDictionaries);

  /**
   * Tells the spellchecker to use a specific dictionary from list.
   * @param aList  a preferred dictionary list
   */
  RefPtr<mozilla::GenericPromise> SetCurrentDictionaryFromList(
      const nsTArray<nsCString>& aList);

  void DeleteRemoteEngine() { mEngine = nullptr; }

  mozilla::TextServicesDocument* GetTextServicesDocument();

 protected:
  mozSpellChecker();
  virtual ~mozSpellChecker();

  nsresult Init();

  RefPtr<mozEnglishWordUtils> mConverter;
  RefPtr<mozilla::TextServicesDocument> mTextServicesDocument;
  nsCOMPtr<mozIPersonalDictionary> mPersonalDictionary;

  nsCOMPtr<mozISpellCheckingEngine> mSpellCheckingEngine;
  bool mFromStart;

  nsTArray<nsCString> mCurrentDictionaries;

  MOZ_CAN_RUN_SCRIPT
  nsresult SetupDoc(int32_t* outBlockOffset);

  nsresult GetCurrentBlockIndex(
      mozilla::TextServicesDocument* aTextServicesDocument,
      int32_t* aOutBlockIndex);

  nsresult GetEngineList(nsCOMArray<mozISpellCheckingEngine>* aDictionaryList);

  mozilla::RemoteSpellcheckEngineChild* mEngine;

  friend class mozilla::RemoteSpellcheckEngineChild;
};
#endif  // mozSpellChecker_h__