Bug 1477917 - Part 2. Use AString instead of wstring in spellchecker. r=masayuki

The language parameter of mozIPersonalDictionary is unused, so we should remove
this from parameter.  Then, no one uses mLanguage member of mozHunspell now.

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Makoto Kato 2018-07-26 03:13:51 +00:00
parent 229108931f
commit efbf30c79f
8 changed files with 76 additions and 77 deletions

View file

@ -128,44 +128,43 @@ mozHunspell::~mozHunspell()
delete mHunspell; delete mHunspell;
} }
NS_IMETHODIMP mozHunspell::GetDictionary(char16_t **aDictionary) NS_IMETHODIMP
mozHunspell::GetDictionary(nsAString& aDictionary)
{ {
NS_ENSURE_ARG_POINTER(aDictionary); aDictionary = mDictionary;
return NS_OK;
*aDictionary = ToNewUnicode(mDictionary);
return *aDictionary ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
} }
/* set the Dictionary. /* set the Dictionary.
* This also Loads the dictionary and initializes the converter using the dictionaries converter * This also Loads the dictionary and initializes the converter using the dictionaries converter
*/ */
NS_IMETHODIMP mozHunspell::SetDictionary(const char16_t *aDictionary) NS_IMETHODIMP
mozHunspell::SetDictionary(const nsAString& aDictionary)
{ {
NS_ENSURE_ARG_POINTER(aDictionary); if (aDictionary.IsEmpty()) {
if (nsDependentString(aDictionary).IsEmpty()) {
delete mHunspell; delete mHunspell;
mHunspell = nullptr; mHunspell = nullptr;
mDictionary.Truncate(); mDictionary.Truncate();
mAffixFileName.Truncate(); mAffixFileName.Truncate();
mLanguage.Truncate();
mDecoder = nullptr; mDecoder = nullptr;
mEncoder = nullptr; mEncoder = nullptr;
return NS_OK; return NS_OK;
} }
nsIURI* affFile = mDictionaries.GetWeak(nsDependentString(aDictionary)); nsIURI* affFile = mDictionaries.GetWeak(aDictionary);
if (!affFile) if (!affFile) {
return NS_ERROR_FILE_NOT_FOUND; return NS_ERROR_FILE_NOT_FOUND;
}
nsAutoCString dictFileName, affFileName; nsAutoCString dictFileName, affFileName;
nsresult rv = affFile->GetSpec(affFileName); nsresult rv = affFile->GetSpec(affFileName);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
if (mAffixFileName.Equals(affFileName.get())) if (mAffixFileName.Equals(affFileName)) {
return NS_OK; return NS_OK;
}
dictFileName = affFileName; dictFileName = affFileName;
int32_t dotPos = dictFileName.RFindChar('.'); int32_t dotPos = dictFileName.RFindChar('.');
@ -195,15 +194,6 @@ NS_IMETHODIMP mozHunspell::SetDictionary(const char16_t *aDictionary)
mEncoder = encoding->NewEncoder(); mEncoder = encoding->NewEncoder();
mDecoder = encoding->NewDecoderWithoutBOMHandling(); mDecoder = encoding->NewDecoderWithoutBOMHandling();
int32_t pos = mDictionary.FindChar('-');
if (pos == -1)
pos = mDictionary.FindChar('_');
if (pos == -1)
mLanguage.Assign(mDictionary);
else
mLanguage = Substring(mDictionary, 0, pos);
return NS_OK; return NS_OK;
} }
@ -324,7 +314,7 @@ mozHunspell::DictionariesChanged(bool aNotifyChildProcesses)
// Check if the current dictionary is still available. // Check if the current dictionary is still available.
// If not, try to replace it with another dictionary of the same language. // If not, try to replace it with another dictionary of the same language.
if (!mDictionary.IsEmpty()) { if (!mDictionary.IsEmpty()) {
nsresult rv = SetDictionary(mDictionary.get()); nsresult rv = SetDictionary(mDictionary);
if (NS_SUCCEEDED(rv)) if (NS_SUCCEEDED(rv))
return; return;
} }
@ -332,7 +322,7 @@ mozHunspell::DictionariesChanged(bool aNotifyChildProcesses)
// If the current dictionary has gone, and we don't have a good replacement, // If the current dictionary has gone, and we don't have a good replacement,
// set no current dictionary. // set no current dictionary.
if (!mDictionary.IsEmpty()) { if (!mDictionary.IsEmpty()) {
SetDictionary(EmptyString().get()); SetDictionary(EmptyString());
} }
} }
@ -391,21 +381,22 @@ mozHunspell::LoadDictionariesFromDir(nsIFile* aDir)
} }
nsresult nsresult
mozHunspell::ConvertCharset(const char16_t* aStr, std::string* aDst) mozHunspell::ConvertCharset(const nsAString& aStr, std::string& aDst)
{ {
NS_ENSURE_ARG_POINTER(aDst); if (NS_WARN_IF(!mEncoder)) {
NS_ENSURE_TRUE(mEncoder, NS_ERROR_NULL_POINTER); return NS_ERROR_NOT_INITIALIZED;
}
auto src = MakeStringSpan(aStr); auto src = MakeSpan(aStr.BeginReading(), aStr.Length());
CheckedInt<size_t> needed = CheckedInt<size_t> needed =
mEncoder->MaxBufferLengthFromUTF16WithoutReplacement(src.Length()); mEncoder->MaxBufferLengthFromUTF16WithoutReplacement(src.Length());
if (!needed.isValid()) { if (!needed.isValid()) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
aDst->resize(needed.value()); aDst.resize(needed.value());
char* dstPtr = &aDst->operator[](0); char* dstPtr = &aDst[0];
auto dst = MakeSpan(reinterpret_cast<uint8_t*>(dstPtr), needed.value()); auto dst = MakeSpan(reinterpret_cast<uint8_t*>(dstPtr), needed.value());
uint32_t result; uint32_t result;
@ -418,7 +409,7 @@ mozHunspell::ConvertCharset(const char16_t* aStr, std::string* aDst)
if (result != kInputEmpty) { if (result != kInputEmpty) {
return NS_ERROR_UENC_NOMAPPING; return NS_ERROR_UENC_NOMAPPING;
} }
aDst->resize(written); aDst.resize(written);
mEncoder->Encoding()->NewEncoderInto(*mEncoder); mEncoder->Encoding()->NewEncoderInto(*mEncoder);
return NS_OK; return NS_OK;
} }
@ -435,35 +426,43 @@ mozHunspell::CollectReports(nsIHandleReportCallback* aHandleReport,
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP mozHunspell::Check(const char16_t *aWord, bool *aResult) NS_IMETHODIMP
mozHunspell::Check(const nsAString& aWord, bool* aResult)
{ {
NS_ENSURE_ARG_POINTER(aWord); if (NS_WARN_IF(!aResult)) {
NS_ENSURE_ARG_POINTER(aResult); return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_TRUE(mHunspell, NS_ERROR_FAILURE); NS_ENSURE_TRUE(mHunspell, NS_ERROR_FAILURE);
std::string charsetWord; std::string charsetWord;
nsresult rv = ConvertCharset(aWord, &charsetWord); nsresult rv = ConvertCharset(aWord, charsetWord);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
*aResult = mHunspell->spell(charsetWord); *aResult = mHunspell->spell(charsetWord);
if (!*aResult && mPersonalDictionary) if (!*aResult && mPersonalDictionary)
rv = mPersonalDictionary->Check(aWord, mLanguage.get(), aResult); rv = mPersonalDictionary->Check(aWord, aResult);
return rv; return rv;
} }
NS_IMETHODIMP mozHunspell::Suggest(const char16_t *aWord, char16_t ***aSuggestions, uint32_t *aSuggestionCount) NS_IMETHODIMP
mozHunspell::Suggest(const nsAString& aWord, char16_t*** aSuggestions,
uint32_t* aSuggestionCount)
{ {
NS_ENSURE_ARG_POINTER(aSuggestions); if (NS_WARN_IF(!aSuggestions)) {
NS_ENSURE_ARG_POINTER(aSuggestionCount); return NS_ERROR_INVALID_ARG;
}
if (NS_WARN_IF(!aSuggestionCount)) {
return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_TRUE(mHunspell, NS_ERROR_FAILURE); NS_ENSURE_TRUE(mHunspell, NS_ERROR_FAILURE);
nsresult rv; nsresult rv;
*aSuggestionCount = 0; *aSuggestionCount = 0;
std::string charsetWord; std::string charsetWord;
rv = ConvertCharset(aWord, &charsetWord); rv = ConvertCharset(aWord, charsetWord);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
std::vector<std::string> suggestions = mHunspell->suggest(charsetWord); std::vector<std::string> suggestions = mHunspell->suggest(charsetWord);

View file

@ -99,7 +99,7 @@ public:
void LoadDictionaryList(bool aNotifyChildProcesses); void LoadDictionaryList(bool aNotifyChildProcesses);
// helper method for converting a word to the charset of the dictionary // helper method for converting a word to the charset of the dictionary
nsresult ConvertCharset(const char16_t* aStr, std::string* aDst); nsresult ConvertCharset(const nsAString& aStr, std::string& aDst);
NS_DECL_NSIMEMORYREPORTER NS_DECL_NSIMEMORYREPORTER
@ -115,7 +115,6 @@ protected:
// Hashtable matches dictionary name to .aff file // Hashtable matches dictionary name to .aff file
nsInterfaceHashtable<nsStringHashKey, nsIURI> mDictionaries; nsInterfaceHashtable<nsStringHashKey, nsIURI> mDictionaries;
nsString mDictionary; nsString mDictionary;
nsString mLanguage;
nsCString mAffixFileName; nsCString mAffixFileName;
// dynamic dirs used to search for dictionaries // dynamic dirs used to search for dictionaries

View file

@ -33,22 +33,22 @@ interface mozIPersonalDictionary : nsISupports {
/** /**
* Check a unicode string * Check a unicode string
*/ */
boolean check(in wstring word, in wstring lang); boolean check(in AString word);
/** /**
* Add a word to the personal dictionary * Add a word to the personal dictionary
*/ */
void addWord(in wstring word, in wstring lang); void addWord(in AString word);
/** /**
* Remove a word from the personal dictionary * Remove a word from the personal dictionary
*/ */
void removeWord(in wstring word, in wstring lang); void removeWord(in AString word);
/** /**
* Add a word to the ignore all list * Add a word to the ignore all list
*/ */
void ignoreWord(in wstring word); void ignoreWord(in AString word);
/** /**
* Clear the ignore list * Clear the ignore list

View file

@ -25,7 +25,7 @@ interface mozISpellCheckingEngine : nsISupports {
* If the dictionary is changed to no dictionary (the empty string), an * If the dictionary is changed to no dictionary (the empty string), an
* observer is allowed to set another dictionary before it returns. * observer is allowed to set another dictionary before it returns.
*/ */
attribute wstring dictionary; attribute AString dictionary;
/** /**
* the personal dictionary * the personal dictionary
@ -40,12 +40,13 @@ interface mozISpellCheckingEngine : nsISupports {
/** /**
* check a word * check a word
*/ */
boolean check(in wstring word); boolean check(in AString word);
/** /**
* get a list of suggestions for a misspelled word * get a list of suggestions for a misspelled word
*/ */
void suggest(in wstring word,[array, size_is(count)] out wstring suggestions, out uint32_t count); void suggest(in AString word, [array, size_is(count)] out wstring suggestions,
out uint32_t count);
/** /**
* Load dictionaries from the specified dir * Load dictionaries from the specified dir

View file

@ -313,7 +313,7 @@ void mozPersonalDictionary::SyncLoadInternal()
word.Append(c); word.Append(c);
if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true; if( (NS_OK != convStream->Read(&c, 1, &nRead)) || (nRead != 1)) done = true;
} }
mDictionaryTable.PutEntry(word.get()); mDictionaryTable.PutEntry(word);
} }
} while(!done); } while(!done);
} }
@ -396,9 +396,9 @@ NS_IMETHODIMP mozPersonalDictionary::GetWordList(nsIStringEnumerator **aWords)
return NS_NewAdoptingStringEnumerator(aWords, array); return NS_NewAdoptingStringEnumerator(aWords, array);
} }
NS_IMETHODIMP mozPersonalDictionary::Check(const char16_t *aWord, const char16_t *aLanguage, bool *aResult) NS_IMETHODIMP
mozPersonalDictionary::Check(const nsAString& aWord, bool* aResult)
{ {
NS_ENSURE_ARG_POINTER(aWord);
NS_ENSURE_ARG_POINTER(aResult); NS_ENSURE_ARG_POINTER(aResult);
WaitForLoad(); WaitForLoad();
@ -407,7 +407,8 @@ NS_IMETHODIMP mozPersonalDictionary::Check(const char16_t *aWord, const char16_t
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP mozPersonalDictionary::AddWord(const char16_t *aWord, const char16_t *aLang) NS_IMETHODIMP
mozPersonalDictionary::AddWord(const nsAString& aWord)
{ {
nsresult res; nsresult res;
WaitForLoad(); WaitForLoad();
@ -417,7 +418,8 @@ NS_IMETHODIMP mozPersonalDictionary::AddWord(const char16_t *aWord, const char16
return res; return res;
} }
NS_IMETHODIMP mozPersonalDictionary::RemoveWord(const char16_t *aWord, const char16_t *aLang) NS_IMETHODIMP
mozPersonalDictionary::RemoveWord(const nsAString& aWord)
{ {
nsresult res; nsresult res;
WaitForLoad(); WaitForLoad();
@ -427,10 +429,11 @@ NS_IMETHODIMP mozPersonalDictionary::RemoveWord(const char16_t *aWord, const cha
return res; return res;
} }
NS_IMETHODIMP mozPersonalDictionary::IgnoreWord(const char16_t *aWord) NS_IMETHODIMP
mozPersonalDictionary::IgnoreWord(const nsAString& aWord)
{ {
// avoid adding duplicate words to the ignore list // avoid adding duplicate words to the ignore list
if (aWord && !mIgnoreTable.GetEntry(aWord)) if (!mIgnoreTable.GetEntry(aWord))
mIgnoreTable.PutEntry(aWord); mIgnoreTable.PutEntry(aWord);
return NS_OK; return NS_OK;
} }

View file

@ -51,8 +51,8 @@ protected:
nsCOMPtr<nsIFile> mFile; nsCOMPtr<nsIFile> mFile;
mozilla::Monitor mMonitor; mozilla::Monitor mMonitor;
mozilla::Monitor mMonitorSave; mozilla::Monitor mMonitorSave;
nsTHashtable<nsUnicharPtrHashKey> mDictionaryTable; nsTHashtable<nsStringHashKey> mDictionaryTable;
nsTHashtable<nsUnicharPtrHashKey> mIgnoreTable; nsTHashtable<nsStringHashKey> mIgnoreTable;
private: private:
/* wait for the asynchronous load of the dictionary to be completed */ /* wait for the asynchronous load of the dictionary to be completed */

View file

@ -156,14 +156,14 @@ mozSpellChecker::CheckWord(const nsAString &aWord, bool *aIsMisspelled, nsTArray
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
} }
*aIsMisspelled = false; *aIsMisspelled = false;
result = mSpellCheckingEngine->Check(PromiseFlatString(aWord).get(), &correct); result = mSpellCheckingEngine->Check(aWord, &correct);
NS_ENSURE_SUCCESS(result, result); NS_ENSURE_SUCCESS(result, result);
if(!correct){ if(!correct){
if(aSuggestions){ if(aSuggestions){
uint32_t count,i; uint32_t count,i;
char16_t **words; char16_t **words;
result = mSpellCheckingEngine->Suggest(PromiseFlatString(aWord).get(), &words, &count); result = mSpellCheckingEngine->Suggest(aWord, &words, &count);
NS_ENSURE_SUCCESS(result, result); NS_ENSURE_SUCCESS(result, result);
nsString* suggestions = aSuggestions->AppendElements(count); nsString* suggestions = aSuggestions->AppendElements(count);
for(i=0;i<count;i++){ for(i=0;i<count;i++){
@ -272,8 +272,8 @@ mozSpellChecker::Replace(const nsAString &aOldWord, const nsAString &aNewWord, b
NS_IMETHODIMP NS_IMETHODIMP
mozSpellChecker::IgnoreAll(const nsAString &aWord) mozSpellChecker::IgnoreAll(const nsAString &aWord)
{ {
if(mPersonalDictionary){ if (mPersonalDictionary) {
mPersonalDictionary->IgnoreWord(PromiseFlatString(aWord).get()); mPersonalDictionary->IgnoreWord(aWord);
} }
return NS_OK; return NS_OK;
} }
@ -282,10 +282,10 @@ NS_IMETHODIMP
mozSpellChecker::AddWordToPersonalDictionary(const nsAString &aWord) mozSpellChecker::AddWordToPersonalDictionary(const nsAString &aWord)
{ {
nsresult res; nsresult res;
char16_t empty=0; if (NS_WARN_IF(!mPersonalDictionary)) {
if (!mPersonalDictionary) return NS_ERROR_NOT_INITIALIZED;
return NS_ERROR_NULL_POINTER; }
res = mPersonalDictionary->AddWord(PromiseFlatString(aWord).get(),&empty); res = mPersonalDictionary->AddWord(aWord);
return res; return res;
} }
@ -293,10 +293,10 @@ NS_IMETHODIMP
mozSpellChecker::RemoveWordFromPersonalDictionary(const nsAString &aWord) mozSpellChecker::RemoveWordFromPersonalDictionary(const nsAString &aWord)
{ {
nsresult res; nsresult res;
char16_t empty=0; if (NS_WARN_IF(!mPersonalDictionary)) {
if (!mPersonalDictionary) return NS_ERROR_NOT_INITIALIZED;
return NS_ERROR_NULL_POINTER; }
res = mPersonalDictionary->RemoveWord(PromiseFlatString(aWord).get(),&empty); res = mPersonalDictionary->RemoveWord(aWord);
return res; return res;
} }
@ -379,10 +379,7 @@ mozSpellChecker::GetCurrentDictionary(nsAString &aDictionary)
return NS_OK; return NS_OK;
} }
nsAutoString dictname; return mSpellCheckingEngine->GetDictionary(aDictionary);
mSpellCheckingEngine->GetDictionary(getter_Copies(dictname));
aDictionary = dictname;
return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -421,7 +418,7 @@ mozSpellChecker::SetCurrentDictionary(const nsAString &aDictionary)
// dictionary was set // dictionary was set
mSpellCheckingEngine = spellCheckingEngines[i]; mSpellCheckingEngine = spellCheckingEngines[i];
rv = mSpellCheckingEngine->SetDictionary(PromiseFlatString(aDictionary).get()); rv = mSpellCheckingEngine->SetDictionary(aDictionary);
if (NS_SUCCEEDED(rv)) { if (NS_SUCCEEDED(rv)) {
nsCOMPtr<mozIPersonalDictionary> personalDictionary = do_GetService("@mozilla.org/spellchecker/personaldictionary;1"); nsCOMPtr<mozIPersonalDictionary> personalDictionary = do_GetService("@mozilla.org/spellchecker/personaldictionary;1");

View file

@ -492,14 +492,14 @@ RemoteSpellChecker.prototype = {
let dictionary = Cc["@mozilla.org/spellchecker/personaldictionary;1"] let dictionary = Cc["@mozilla.org/spellchecker/personaldictionary;1"]
.getService(Ci.mozIPersonalDictionary); .getService(Ci.mozIPersonalDictionary);
dictionary.addWord(this._spellInfo.misspelling, ""); dictionary.addWord(this._spellInfo.misspelling);
this._spellInfo.target.sendAsyncMessage("InlineSpellChecker:recheck", {}); this._spellInfo.target.sendAsyncMessage("InlineSpellChecker:recheck", {});
}, },
undoAddToDictionary(word) { undoAddToDictionary(word) {
let dictionary = Cc["@mozilla.org/spellchecker/personaldictionary;1"] let dictionary = Cc["@mozilla.org/spellchecker/personaldictionary;1"]
.getService(Ci.mozIPersonalDictionary); .getService(Ci.mozIPersonalDictionary);
dictionary.removeWord(word, ""); dictionary.removeWord(word);
this._spellInfo.target.sendAsyncMessage("InlineSpellChecker:recheck", {}); this._spellInfo.target.sendAsyncMessage("InlineSpellChecker:recheck", {});
}, },