Skip to content

Commit

Permalink
Bug 1330912 - Part 1. Add async version of SetCurrentDictionary using…
Browse files Browse the repository at this point in the history
… list. r=Ehsan

Now, mozInlineSpellChecker::UpdateCurrentDictionary uses several sync IPC message to update spellcheck dictionary.  So I want async IPC to set dictionary.

Also, since nsEditorSpellCheck::DictionaryFetched calls SetCurrentDictionary several times (max: 6 times), so to reduce calls, we should use list of dictionary.

MozReview-Commit-ID: EVMAJxpMT2X
  • Loading branch information
makotokato committed Apr 10, 2017
1 parent 91c7c97 commit bff2155
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 2 deletions.
8 changes: 8 additions & 0 deletions editor/txtsvc/nsISpellChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef nsISpellChecker_h__
#define nsISpellChecker_h__

#include "mozilla/MozPromise.h"
#include "nsISupports.h"
#include "nsTArray.h"

Expand Down Expand Up @@ -114,6 +115,13 @@ class nsISpellChecker : public nsISupports{
* empty string, spellchecker will be disabled.
*/
NS_IMETHOD SetCurrentDictionary(const nsAString &aDictionary) = 0;

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

NS_DEFINE_STATIC_IID_ACCESSOR(nsISpellChecker, NS_ISPELLCHECKER_IID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ parent:
sync CheckAndSuggest(nsString aWord) returns (bool aIsMisspelled, nsString[] aSuggestions);

sync SetDictionary(nsString aDictionary) returns (bool success);

async SetDictionaryFromList(nsString[] aList, intptr_t aPromiseId);

child:
async NotifyOfCurrentDictionary(nsString aDictionary, intptr_t aPromiseId);
};

} // namespace mozilla
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* 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/. */

#include "mozilla/UniquePtr.h"
#include "RemoteSpellCheckEngineChild.h"

namespace mozilla {
Expand All @@ -18,4 +19,37 @@ RemoteSpellcheckEngineChild::~RemoteSpellcheckEngineChild()
mOwner->DeleteRemoteEngine();
}

RefPtr<GenericPromise>
RemoteSpellcheckEngineChild::SetCurrentDictionaryFromList(
const nsTArray<nsString>& aList)
{
MozPromiseHolder<GenericPromise>* promiseHolder =
new MozPromiseHolder<GenericPromise>();
if (!SendSetDictionaryFromList(
aList,
reinterpret_cast<intptr_t>(promiseHolder))) {
delete promiseHolder;
return GenericPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
}
// promiseHolder will removed by receive message
return promiseHolder->Ensure(__func__);
}

mozilla::ipc::IPCResult
RemoteSpellcheckEngineChild::RecvNotifyOfCurrentDictionary(
const nsString& aDictionary,
const intptr_t& aId)
{
MozPromiseHolder<GenericPromise>* promiseHolder =
reinterpret_cast<MozPromiseHolder<GenericPromise>*>(aId);
mOwner->mCurrentDictionary = aDictionary;
if (aDictionary.IsEmpty()) {
promiseHolder->RejectIfExists(NS_ERROR_NOT_AVAILABLE, __func__);
} else {
promiseHolder->ResolveIfExists(true, __func__);
}
delete promiseHolder;
return IPC_OK();
}

} //namespace mozilla
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef RemoteSpellcheckEngineChild_h_
#define RemoteSpellcheckEngineChild_h_

#include "mozilla/MozPromise.h"
#include "mozilla/PRemoteSpellcheckEngineChild.h"
#include "mozSpellChecker.h"

Expand All @@ -18,6 +19,13 @@ class RemoteSpellcheckEngineChild : public mozilla::PRemoteSpellcheckEngineChild
explicit RemoteSpellcheckEngineChild(mozSpellChecker *aOwner);
virtual ~RemoteSpellcheckEngineChild();

virtual mozilla::ipc::IPCResult RecvNotifyOfCurrentDictionary(
const nsString& aDictionary,
const intptr_t& aPromiseId) override;

RefPtr<GenericPromise> SetCurrentDictionaryFromList(
const nsTArray<nsString>& aList);

private:
mozSpellChecker *mOwner;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "RemoteSpellCheckEngineParent.h"
#include "mozilla/Unused.h"
#include "nsISpellChecker.h"
#include "nsServiceManagerUtils.h"

Expand All @@ -28,6 +29,23 @@ RemoteSpellcheckEngineParent::RecvSetDictionary(
return IPC_OK();
}

mozilla::ipc::IPCResult
RemoteSpellcheckEngineParent::RecvSetDictionaryFromList(
nsTArray<nsString>&& aList,
const intptr_t& aPromiseId)
{
for (auto& dictionary : aList) {
MOZ_ASSERT(!dictionary.IsEmpty());
nsresult rv = mSpellChecker->SetCurrentDictionary(dictionary);
if (NS_SUCCEEDED(rv)) {
Unused << SendNotifyOfCurrentDictionary(dictionary, aPromiseId);
return IPC_OK();
}
}
Unused << SendNotifyOfCurrentDictionary(EmptyString(), aPromiseId);
return IPC_OK();
}

mozilla::ipc::IPCResult
RemoteSpellcheckEngineParent::RecvCheck(
const nsString& aWord,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "mozilla/PRemoteSpellcheckEngineParent.h"
#include "nsCOMPtr.h"
#include "nsTArray.h"

class nsISpellChecker;

Expand All @@ -23,6 +24,10 @@ class RemoteSpellcheckEngineParent : public PRemoteSpellcheckEngineParent
virtual mozilla::ipc::IPCResult RecvSetDictionary(const nsString& aDictionary,
bool* success) override;

virtual mozilla::ipc::IPCResult RecvSetDictionaryFromList(
nsTArray<nsString>&& aList,
const intptr_t& aPromiseId) override;

virtual mozilla::ipc::IPCResult RecvCheck(const nsString& aWord, bool* aIsMisspelled) override;

virtual mozilla::ipc::IPCResult RecvCheckAndSuggest(const nsString& aWord,
Expand Down
23 changes: 23 additions & 0 deletions extensions/spellcheck/src/mozSpellChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "nsXULAppAPI.h"

using mozilla::dom::ContentChild;
using mozilla::GenericPromise;
using mozilla::PRemoteSpellcheckEngineChild;
using mozilla::RemoteSpellcheckEngineChild;

Expand Down Expand Up @@ -429,6 +430,28 @@ mozSpellChecker::SetCurrentDictionary(const nsAString &aDictionary)
return NS_ERROR_NOT_AVAILABLE;
}

NS_IMETHODIMP_(RefPtr<GenericPromise>)
mozSpellChecker::SetCurrentDictionaryFromList(const nsTArray<nsString>& aList)
{
if (aList.IsEmpty()) {
return GenericPromise::CreateAndReject(NS_ERROR_INVALID_ARG, __func__);
}

if (XRE_IsContentProcess()) {
// mCurrentDictionary will be set by RemoteSpellCheckEngineChild
return mEngine->SetCurrentDictionaryFromList(aList);
}

for (auto& dictionary : aList) {
nsresult rv = SetCurrentDictionary(dictionary);
if (NS_SUCCEEDED(rv)) {
return GenericPromise::CreateAndResolve(true, __func__);
}
}
// We could not find any engine with the requested dictionary
return GenericPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE, __func__);
}

nsresult
mozSpellChecker::SetupDoc(int32_t *outBlockOffset)
{
Expand Down
7 changes: 5 additions & 2 deletions extensions/spellcheck/src/mozSpellChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "RemoteSpellCheckEngineChild.h"

namespace mozilla {
class PRemoteSpellcheckEngineChild;
class RemoteSpellcheckEngineChild;
} // namespace mozilla

Expand Down Expand Up @@ -48,6 +47,8 @@ class mozSpellChecker : public nsISpellChecker
NS_IMETHOD GetDictionaryList(nsTArray<nsString> *aDictionaryList) override;
NS_IMETHOD GetCurrentDictionary(nsAString &aDictionary) override;
NS_IMETHOD SetCurrentDictionary(const nsAString &aDictionary) override;
NS_IMETHOD_(RefPtr<mozilla::GenericPromise>)
SetCurrentDictionaryFromList(const nsTArray<nsString>& aList) override;

void DeleteRemoteEngine() {
mEngine = nullptr;
Expand All @@ -71,6 +72,8 @@ class mozSpellChecker : public nsISpellChecker

nsresult GetEngineList(nsCOMArray<mozISpellCheckingEngine> *aDictionaryList);

mozilla::PRemoteSpellcheckEngineChild *mEngine;
mozilla::RemoteSpellcheckEngineChild *mEngine;

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

0 comments on commit bff2155

Please sign in to comment.