1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "mozilla/UniquePtr.h"
6 #include "RemoteSpellCheckEngineChild.h"
7 
8 namespace mozilla {
9 
RemoteSpellcheckEngineChild(mozSpellChecker * aOwner)10 RemoteSpellcheckEngineChild::RemoteSpellcheckEngineChild(
11     mozSpellChecker* aOwner)
12     : mOwner(aOwner) {}
13 
~RemoteSpellcheckEngineChild()14 RemoteSpellcheckEngineChild::~RemoteSpellcheckEngineChild() {
15   // null out the owner's SpellcheckEngineChild to prevent state corruption
16   // during shutdown
17   mOwner->DeleteRemoteEngine();
18 }
19 
20 RefPtr<GenericPromise>
SetCurrentDictionaryFromList(const nsTArray<nsCString> & aList)21 RemoteSpellcheckEngineChild::SetCurrentDictionaryFromList(
22     const nsTArray<nsCString>& aList) {
23   RefPtr<mozSpellChecker> spellChecker = mOwner;
24 
25   return SendSetDictionaryFromList(aList)->Then(
26       GetMainThreadSerialEventTarget(), __func__,
27       [spellChecker](Tuple<bool, nsCString>&& aParam) {
28         if (!Get<0>(aParam)) {
29           spellChecker->mCurrentDictionary.Truncate();
30           return GenericPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE,
31                                                  __func__);
32         }
33         spellChecker->mCurrentDictionary = std::move(Get<1>(aParam));
34         return GenericPromise::CreateAndResolve(true, __func__);
35       },
36       [spellChecker](ResponseRejectReason&& aReason) {
37         spellChecker->mCurrentDictionary.Truncate();
38         return GenericPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE,
39                                                __func__);
40       });
41 }
42 
CheckWords(const nsTArray<nsString> & aWords)43 RefPtr<CheckWordPromise> RemoteSpellcheckEngineChild::CheckWords(
44     const nsTArray<nsString>& aWords) {
45   RefPtr<mozSpellChecker> kungFuDeathGrip = mOwner;
46 
47   return SendCheckAsync(aWords)->Then(
48       GetMainThreadSerialEventTarget(), __func__,
49       [kungFuDeathGrip](nsTArray<bool>&& aIsMisspelled) {
50         return CheckWordPromise::CreateAndResolve(std::move(aIsMisspelled),
51                                                   __func__);
52       },
53       [kungFuDeathGrip](mozilla::ipc::ResponseRejectReason&& aReason) {
54         return CheckWordPromise::CreateAndReject(NS_ERROR_NOT_AVAILABLE,
55                                                  __func__);
56       });
57 }
58 
59 }  // namespace mozilla
60