diff options
Diffstat (limited to 'extensions/spellcheck/ipc')
6 files changed, 320 insertions, 0 deletions
diff --git a/extensions/spellcheck/ipc/PRemoteSpellcheckEngine.ipdl b/extensions/spellcheck/ipc/PRemoteSpellcheckEngine.ipdl new file mode 100644 index 0000000000..c327d97e65 --- /dev/null +++ b/extensions/spellcheck/ipc/PRemoteSpellcheckEngine.ipdl @@ -0,0 +1,46 @@ +/* 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/. */ + +include protocol PContent; + +include "mozilla/RemoteSpellCheckEngineParent.h"; +include "mozilla/RemoteSpellCheckEngineChild.h"; + +namespace mozilla { + +[ManualDealloc, ChildImpl="RemoteSpellcheckEngineChild", ParentImpl="RemoteSpellcheckEngineParent"] +sync protocol PRemoteSpellcheckEngine { + manager PContent; + +parent: + async __delete__(); + + async CheckAsync(nsString[] aWord) returns (bool[] aIsMisspelled); + + sync SetDictionary(nsCString aDictionary) returns (bool success); + + /* + * Set multiple current dictionaries from a list of dictionary names. + * + * @aDictionaries An array of dictionary names to use. If the array is empty, + * no dictionary will be used. + * @aSuccess true if setting the dictionaries succeeded, false otherwise. + */ + async SetDictionaries(nsCString[] aDictionaries) returns (bool success); + + async Suggest(nsString aWord, uint32_t aCount) returns (nsString[] aSuggestions); + + /* + * Set current dictionary from list of dictionary name. + * + * @aList A list of dictionary name. If a string into this list is + * empty string, dictionary selection is reset + * @aSuccess true if setting dictionary is successful + * @aDictionary Return current dictionary name that set by this method. + */ + async SetDictionaryFromList(nsCString[] aList) + returns (bool aSuccess, nsCString aDictionary); +}; + +} // namespace mozilla diff --git a/extensions/spellcheck/ipc/RemoteSpellCheckEngineChild.cpp b/extensions/spellcheck/ipc/RemoteSpellCheckEngineChild.cpp new file mode 100644 index 0000000000..8971694db3 --- /dev/null +++ b/extensions/spellcheck/ipc/RemoteSpellCheckEngineChild.cpp @@ -0,0 +1,84 @@ +/* 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/. */ + +#include "mozilla/UniquePtr.h" +#include "RemoteSpellCheckEngineChild.h" + +namespace mozilla { + +RemoteSpellcheckEngineChild::RemoteSpellcheckEngineChild( + mozSpellChecker* aOwner) + : mOwner(aOwner) {} + +RemoteSpellcheckEngineChild::~RemoteSpellcheckEngineChild() { + // null out the owner's SpellcheckEngineChild to prevent state corruption + // during shutdown + mOwner->DeleteRemoteEngine(); +} + +RefPtr<GenericPromise> RemoteSpellcheckEngineChild::SetCurrentDictionaries( + const nsTArray<nsCString>& aDictionaries) { + RefPtr<mozSpellChecker> spellChecker = mOwner; + + return SendSetDictionaries(aDictionaries) + ->Then( + GetMainThreadSerialEventTarget(), __func__, + [spellChecker, dictionaries = aDictionaries.Clone()](bool&& aParam) { + if (aParam) { + spellChecker->mCurrentDictionaries = dictionaries.Clone(); + return GenericPromise::CreateAndResolve(true, __func__); + } + spellChecker->mCurrentDictionaries.Clear(); + return GenericPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE, + __func__); + }, + [spellChecker](ResponseRejectReason&& aReason) { + spellChecker->mCurrentDictionaries.Clear(); + return GenericPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE, + __func__); + }); +} + +RefPtr<GenericPromise> +RemoteSpellcheckEngineChild::SetCurrentDictionaryFromList( + const nsTArray<nsCString>& aList) { + RefPtr<mozSpellChecker> spellChecker = mOwner; + + return SendSetDictionaryFromList(aList)->Then( + GetMainThreadSerialEventTarget(), __func__, + [spellChecker](std::tuple<bool, nsCString>&& aParam) { + if (!std::get<0>(aParam)) { + spellChecker->mCurrentDictionaries.Clear(); + return GenericPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE, + __func__); + } + spellChecker->mCurrentDictionaries.Clear(); + spellChecker->mCurrentDictionaries.AppendElement( + std::move(std::get<1>(aParam))); + return GenericPromise::CreateAndResolve(true, __func__); + }, + [spellChecker](ResponseRejectReason&& aReason) { + spellChecker->mCurrentDictionaries.Clear(); + return GenericPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE, + __func__); + }); +} + +RefPtr<CheckWordPromise> RemoteSpellcheckEngineChild::CheckWords( + const nsTArray<nsString>& aWords) { + RefPtr<mozSpellChecker> kungFuDeathGrip = mOwner; + + return SendCheckAsync(aWords)->Then( + GetMainThreadSerialEventTarget(), __func__, + [kungFuDeathGrip](nsTArray<bool>&& aIsMisspelled) { + return CheckWordPromise::CreateAndResolve(std::move(aIsMisspelled), + __func__); + }, + [kungFuDeathGrip](mozilla::ipc::ResponseRejectReason&& aReason) { + return CheckWordPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE, + __func__); + }); +} + +} // namespace mozilla diff --git a/extensions/spellcheck/ipc/RemoteSpellCheckEngineChild.h b/extensions/spellcheck/ipc/RemoteSpellCheckEngineChild.h new file mode 100644 index 0000000000..1579908f46 --- /dev/null +++ b/extensions/spellcheck/ipc/RemoteSpellCheckEngineChild.h @@ -0,0 +1,36 @@ +/* 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 RemoteSpellcheckEngineChild_h_ +#define RemoteSpellcheckEngineChild_h_ + +#include "mozilla/MozPromise.h" +#include "mozilla/mozSpellChecker.h" +#include "mozilla/PRemoteSpellcheckEngineChild.h" + +class mozSpellChecker; + +namespace mozilla { + +class RemoteSpellcheckEngineChild + : public mozilla::PRemoteSpellcheckEngineChild { + public: + explicit RemoteSpellcheckEngineChild(mozSpellChecker* aOwner); + virtual ~RemoteSpellcheckEngineChild(); + + RefPtr<GenericPromise> SetCurrentDictionaries( + const nsTArray<nsCString>& aDictionaries); + + RefPtr<GenericPromise> SetCurrentDictionaryFromList( + const nsTArray<nsCString>& aList); + + RefPtr<CheckWordPromise> CheckWords(const nsTArray<nsString>& aWords); + + private: + mozSpellChecker* mOwner; +}; + +} // namespace mozilla + +#endif // RemoteSpellcheckEngineChild_h_ diff --git a/extensions/spellcheck/ipc/RemoteSpellCheckEngineParent.cpp b/extensions/spellcheck/ipc/RemoteSpellCheckEngineParent.cpp new file mode 100644 index 0000000000..e9f341d524 --- /dev/null +++ b/extensions/spellcheck/ipc/RemoteSpellCheckEngineParent.cpp @@ -0,0 +1,85 @@ +/* vim: set ts=2 sw=2 sts=2 tw=80: */ +/* 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/. */ + +#include "RemoteSpellCheckEngineParent.h" +#include "mozilla/Unused.h" +#include "mozilla/mozSpellChecker.h" +#include "nsServiceManagerUtils.h" + +namespace mozilla { + +RemoteSpellcheckEngineParent::RemoteSpellcheckEngineParent() { + mSpellChecker = mozSpellChecker::Create(); +} + +RemoteSpellcheckEngineParent::~RemoteSpellcheckEngineParent() {} + +mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvSetDictionary( + const nsACString& aDictionary, bool* success) { + nsresult rv = mSpellChecker->SetCurrentDictionary(aDictionary); + *success = NS_SUCCEEDED(rv); + return IPC_OK(); +} + +mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvSetDictionaries( + const nsTArray<nsCString>& aDictionaries, + SetDictionariesResolver&& aResolve) { + mSpellChecker->SetCurrentDictionaries(aDictionaries) + ->Then( + GetMainThreadSerialEventTarget(), __func__, + [aResolve]() { aResolve(true); }, [aResolve]() { aResolve(false); }); + return IPC_OK(); +} + +mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvSetDictionaryFromList( + nsTArray<nsCString>&& aList, SetDictionaryFromListResolver&& aResolve) { + for (auto& dictionary : aList) { + nsresult rv = mSpellChecker->SetCurrentDictionary(dictionary); + if (NS_SUCCEEDED(rv)) { + aResolve(std::tuple<const bool&, const nsACString&>(true, dictionary)); + return IPC_OK(); + } + } + aResolve(std::tuple<const bool&, const nsACString&>(false, ""_ns)); + return IPC_OK(); +} + +mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvCheckAsync( + nsTArray<nsString>&& aWords, CheckAsyncResolver&& aResolve) { + nsTArray<bool> misspells; + misspells.SetCapacity(aWords.Length()); + for (auto& word : aWords) { + bool misspelled; + nsresult rv = mSpellChecker->CheckWord(word, &misspelled, nullptr); + // If CheckWord failed, we can't tell whether the word is correctly spelled + if (NS_FAILED(rv)) { + misspelled = false; + } + misspells.AppendElement(misspelled); + } + aResolve(std::move(misspells)); + return IPC_OK(); +} + +mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvSuggest( + const nsAString& aWord, uint32_t aCount, SuggestResolver&& aResolve) { + nsTArray<nsString> suggestions; + mSpellChecker->Suggest(aWord, aCount) + ->Then( + GetMainThreadSerialEventTarget(), __func__, + [aResolve](CopyableTArray<nsString> aSuggestions) { + aResolve(std::move(aSuggestions)); + }, + [aResolve](nsresult aError) { + // No suggestions due to error + nsTArray<nsString> suggestions; + aResolve(std::move(suggestions)); + }); + return IPC_OK(); +} + +void RemoteSpellcheckEngineParent::ActorDestroy(ActorDestroyReason aWhy) {} + +} // namespace mozilla diff --git a/extensions/spellcheck/ipc/RemoteSpellCheckEngineParent.h b/extensions/spellcheck/ipc/RemoteSpellCheckEngineParent.h new file mode 100644 index 0000000000..06f4a7e8f5 --- /dev/null +++ b/extensions/spellcheck/ipc/RemoteSpellCheckEngineParent.h @@ -0,0 +1,46 @@ +/* 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 RemoteSpellcheckEngineParent_h_ +#define RemoteSpellcheckEngineParent_h_ + +#include "mozilla/PRemoteSpellcheckEngineParent.h" +#include "nsCOMPtr.h" +#include "nsTArray.h" + +class mozSpellChecker; + +namespace mozilla { + +class RemoteSpellcheckEngineParent : public PRemoteSpellcheckEngineParent { + public: + RemoteSpellcheckEngineParent(); + + virtual ~RemoteSpellcheckEngineParent(); + + virtual void ActorDestroy(ActorDestroyReason aWhy) override; + + virtual mozilla::ipc::IPCResult RecvSetDictionary( + const nsACString& aDictionary, bool* success); + + virtual mozilla::ipc::IPCResult RecvSetDictionaries( + const nsTArray<nsCString>& aDictionaries, + SetDictionariesResolver&& aResolve); + + virtual mozilla::ipc::IPCResult RecvSetDictionaryFromList( + nsTArray<nsCString>&& aList, SetDictionaryFromListResolver&& aResolve); + + virtual mozilla::ipc::IPCResult RecvCheckAsync(nsTArray<nsString>&& aWord, + CheckAsyncResolver&& aResolve); + + virtual mozilla::ipc::IPCResult RecvSuggest(const nsAString& aWord, + uint32_t aCount, + SuggestResolver&& aResolve); + + private: + RefPtr<mozSpellChecker> mSpellChecker; +}; + +} // namespace mozilla + +#endif diff --git a/extensions/spellcheck/ipc/moz.build b/extensions/spellcheck/ipc/moz.build new file mode 100644 index 0000000000..0e424dc72e --- /dev/null +++ b/extensions/spellcheck/ipc/moz.build @@ -0,0 +1,23 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +UNIFIED_SOURCES += [ + "RemoteSpellCheckEngineChild.cpp", + "RemoteSpellCheckEngineParent.cpp", +] + +FINAL_LIBRARY = "xul" + +include("/ipc/chromium/chromium-config.mozbuild") + +IPDL_SOURCES = [ + "PRemoteSpellcheckEngine.ipdl", +] + +EXPORTS.mozilla += [ + "RemoteSpellCheckEngineChild.h", + "RemoteSpellCheckEngineParent.h", +] |