Bug 1402822 - Support multiple dictionaries in mozSpellChecker; r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D140240
This commit is contained in:
Dan Minor 2022-03-23 13:53:37 +00:00
Родитель 4bfde31d98
Коммит c6176cbf35
2 изменённых файлов: 81 добавлений и 23 удалений

Просмотреть файл

@ -396,31 +396,31 @@ nsresult mozSpellChecker::GetDictionaryList(
return NS_OK;
}
nsresult mozSpellChecker::GetCurrentDictionary(nsACString& aDictionary) {
nsresult mozSpellChecker::GetCurrentDictionaries(
nsTArray<nsCString>& aDictionaries) {
if (XRE_IsContentProcess()) {
aDictionary = mCurrentDictionary;
aDictionaries = mCurrentDictionaries.Clone();
return NS_OK;
}
if (!mSpellCheckingEngine) {
aDictionary.Truncate();
aDictionaries.Clear();
return NS_OK;
}
return mSpellCheckingEngine->GetDictionary(aDictionary);
return mSpellCheckingEngine->GetDictionaries(aDictionaries);
}
nsresult mozSpellChecker::SetCurrentDictionary(const nsACString& aDictionary) {
nsresult mozSpellChecker::SetCurrentDictionary(const nsCString& aDictionary) {
if (XRE_IsContentProcess()) {
nsCString wrappedDict = nsCString(aDictionary);
mCurrentDictionaries.Clear();
bool isSuccess;
mEngine->SendSetDictionary(wrappedDict, &isSuccess);
mEngine->SendSetDictionary(aDictionary, &isSuccess);
if (!isSuccess) {
mCurrentDictionary.Truncate();
return NS_ERROR_NOT_AVAILABLE;
}
mCurrentDictionary = wrappedDict;
mCurrentDictionaries.AppendElement(aDictionary);
return NS_OK;
}
@ -438,18 +438,19 @@ nsresult mozSpellChecker::SetCurrentDictionary(const nsACString& aDictionary) {
rv = GetEngineList(&spellCheckingEngines);
NS_ENSURE_SUCCESS(rv, rv);
nsTArray<nsCString> dictionaries;
dictionaries.AppendElement(aDictionary);
for (int32_t i = 0; i < spellCheckingEngines.Count(); i++) {
// We must set mSpellCheckingEngine before we call SetDictionary, since
// SetDictionary calls back to this spell checker to check if the
// We must set mSpellCheckingEngine before we call SetDictionaries, since
// SetDictionaries calls back to this spell checker to check if the
// dictionary was set
mSpellCheckingEngine = spellCheckingEngines[i];
rv = mSpellCheckingEngine->SetDictionary(aDictionary);
rv = mSpellCheckingEngine->SetDictionaries(dictionaries);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<mozIPersonalDictionary> personalDictionary =
do_GetService("@mozilla.org/spellchecker/personaldictionary;1");
mSpellCheckingEngine->SetPersonalDictionary(personalDictionary.get());
mSpellCheckingEngine->SetPersonalDictionary(personalDictionary);
mConverter = new mozEnglishWordUtils;
return NS_OK;
@ -462,6 +463,54 @@ nsresult mozSpellChecker::SetCurrentDictionary(const nsACString& aDictionary) {
return NS_ERROR_NOT_AVAILABLE;
}
RefPtr<GenericPromise> mozSpellChecker::SetCurrentDictionaries(
const nsTArray<nsCString>& aDictionaries) {
if (XRE_IsContentProcess()) {
// mCurrentDictionaries will be set by RemoteSpellCheckEngineChild
return mEngine->SetCurrentDictionaries(aDictionaries);
}
// Calls to mozISpellCheckingEngine::SetDictionary might destroy us
RefPtr<mozSpellChecker> kungFuDeathGrip = this;
mSpellCheckingEngine = nullptr;
if (aDictionaries.IsEmpty()) {
return GenericPromise::CreateAndResolve(true, __func__);
}
nsresult rv;
nsCOMArray<mozISpellCheckingEngine> spellCheckingEngines;
rv = GetEngineList(&spellCheckingEngines);
if (NS_FAILED(rv)) {
return GenericPromise::CreateAndReject(rv, __func__);
}
for (int32_t i = 0; i < spellCheckingEngines.Count(); i++) {
// We must set mSpellCheckingEngine before we call SetDictionaries, since
// SetDictionaries calls back to this spell checker to check if the
// dictionary was set
mSpellCheckingEngine = spellCheckingEngines[i];
rv = mSpellCheckingEngine->SetDictionaries(aDictionaries);
if (NS_SUCCEEDED(rv)) {
mCurrentDictionaries = aDictionaries.Clone();
nsCOMPtr<mozIPersonalDictionary> personalDictionary =
do_GetService("@mozilla.org/spellchecker/personaldictionary;1");
mSpellCheckingEngine->SetPersonalDictionary(personalDictionary);
mConverter = new mozEnglishWordUtils;
return GenericPromise::CreateAndResolve(true, __func__);
}
}
mSpellCheckingEngine = nullptr;
// We could not find any engine with the requested dictionary
return GenericPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE, __func__);
}
RefPtr<GenericPromise> mozSpellChecker::SetCurrentDictionaryFromList(
const nsTArray<nsCString>& aList) {
if (aList.IsEmpty()) {
@ -469,7 +518,7 @@ RefPtr<GenericPromise> mozSpellChecker::SetCurrentDictionaryFromList(
}
if (XRE_IsContentProcess()) {
// mCurrentDictionary will be set by RemoteSpellCheckEngineChild
// mCurrentDictionaries will be set by RemoteSpellCheckEngineChild
return mEngine->SetCurrentDictionaryFromList(aList);
}

Просмотреть файл

@ -131,20 +131,29 @@ class mozSpellChecker final {
nsresult GetDictionaryList(nsTArray<nsCString>* aDictionaryList);
/**
* Returns a string representing the current dictionary.
* @param aDictionary will contain the name of the dictionary.
* 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 GetCurrentDictionary(nsACString& aDictionary);
nsresult GetCurrentDictionaries(nsTArray<nsCString>& aDictionaries);
/**
* Tells the spellchecker to use a specific dictionary.
* 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
* empty string, spellchecker will be disabled.
* by GetDictionaryList() or an empty string . If aDictionary is
* an empty array, the spellchecker will be disabled.
*/
nsresult SetCurrentDictionary(const nsACString& aDictionary);
nsresult SetCurrentDictionary(const nsCString& 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.
@ -170,7 +179,7 @@ class mozSpellChecker final {
nsCOMPtr<mozISpellCheckingEngine> mSpellCheckingEngine;
bool mFromStart;
nsCString mCurrentDictionary;
nsTArray<nsCString> mCurrentDictionaries;
MOZ_CAN_RUN_SCRIPT
nsresult SetupDoc(int32_t* outBlockOffset);