Bug 1502661 - Part 2. Add async method to check word by spellchecker. r=masayuki

For mozInlineSpellChecker, I would like to add promse-based method to check
word. But this method is

- On content process, it works by async.
- On chrome process, it works by sync.

Some reftests with non-e10s don't wait that spellchecker is finished correctly
due to race condition of spellchecker dictionary update.  So it uses sync'd
promise on chrome.

Differential Revision: https://phabricator.services.mozilla.com/D14836

--HG--
extra : rebase_source : 63d8fb1b3a6f8754f5b7604ea920c1a26bcd4f5b
This commit is contained in:
Makoto Kato 2018-12-18 20:23:44 +09:00
parent 65e177f953
commit 7bc8f8b726
4 changed files with 62 additions and 1 deletions

View File

@ -441,6 +441,16 @@ EditorSpellCheck::CheckCurrentWordNoSuggest(const nsAString& aSuggestedWord,
return mSpellChecker->CheckWord(aSuggestedWord, aIsMisspelled, nullptr);
}
RefPtr<CheckWordPromise> EditorSpellCheck::CheckCurrentWordsNoSuggest(
const nsTArray<nsString>& aSuggestedWords) {
if (NS_WARN_IF(!mSpellChecker)) {
return CheckWordPromise::CreateAndReject(NS_ERROR_NOT_INITIALIZED,
__func__);
}
return mSpellChecker->CheckWords(aSuggestedWords);
}
NS_IMETHODIMP
EditorSpellCheck::ReplaceWord(const nsAString& aMisspelledWord,
const nsAString& aReplaceWord,

View File

@ -6,7 +6,8 @@
#ifndef mozilla_EditorSpellCheck_h
#define mozilla_EditorSpellCheck_h
#include "nsCOMPtr.h" // for nsCOMPtr
#include "mozilla/mozSpellChecker.h" // for mozilla::CheckWordPromise
#include "nsCOMPtr.h" // for nsCOMPtr
#include "nsCycleCollectionParticipant.h"
#include "nsIEditorSpellCheck.h" // for NS_DECL_NSIEDITORSPELLCHECK, etc
#include "nsISupportsImpl.h"
@ -42,6 +43,19 @@ class EditorSpellCheck final : public nsIEditorSpellCheck {
mozSpellChecker* GetSpellChecker();
/**
* Like CheckCurrentWord, checks the word you give it, returning true via
* promise if it's misspelled.
* This is faster than CheckCurrentWord because it does not compute
* any suggestions.
*
* Watch out: this does not clear any suggestions left over from previous
* calls to CheckCurrentWord, so there may be suggestions, but they will be
* invalid.
*/
RefPtr<mozilla::CheckWordPromise> CheckCurrentWordsNoSuggest(
const nsTArray<nsString>& aSuggestedWords);
protected:
virtual ~EditorSpellCheck();

View File

@ -116,6 +116,34 @@ nsresult mozSpellChecker::NextMisspelledWord(nsAString &aWord,
return NS_OK;
}
RefPtr<CheckWordPromise> mozSpellChecker::CheckWords(
const nsTArray<nsString> &aWords) {
if (XRE_IsContentProcess()) {
return mEngine->SendCheckAsync(aWords)->Then(
GetMainThreadSerialEventTarget(), __func__,
[](nsTArray<bool> &&aIsMisspelled) {
return CheckWordPromise::CreateAndResolve(std::move(aIsMisspelled),
__func__);
},
[](mozilla::ipc::ResponseRejectReason &&aReason) {
return CheckWordPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE,
__func__);
});
}
nsTArray<bool> misspells;
misspells.SetCapacity(aWords.Length());
for (auto &word : aWords) {
bool misspelled;
nsresult rv = CheckWord(word, &misspelled, nullptr);
if (NS_WARN_IF(NS_FAILED(rv))) {
return CheckWordPromise::CreateAndReject(rv, __func__);
}
misspells.AppendElement(misspelled);
}
return CheckWordPromise::CreateAndResolve(std::move(misspells), __func__);
}
nsresult mozSpellChecker::CheckWord(const nsAString &aWord, bool *aIsMisspelled,
nsTArray<nsString> *aSuggestions) {
nsresult result;

View File

@ -6,6 +6,7 @@
#ifndef mozSpellChecker_h__
#define mozSpellChecker_h__
#include "mozilla/MozPromise.h"
#include "nsCOMPtr.h"
#include "nsCOMArray.h"
#include "nsString.h"
@ -20,6 +21,7 @@ class mozEnglishWordUtils;
namespace mozilla {
class RemoteSpellcheckEngineChild;
class TextServicesDocument;
typedef MozPromise<nsTArray<bool>, nsresult, false> CheckWordPromise;
} // namespace mozilla
class mozSpellChecker final {
@ -63,6 +65,13 @@ class mozSpellChecker final {
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);
/**
* Replaces the old word with the specified new word.
* @param aOldWord is the word to be replaced.