summaryrefslogtreecommitdiffstats
path: root/extensions/spellcheck/ipc/RemoteSpellCheckEngineChild.cpp
blob: 8971694db326b8faa591ef6b4ef35b74960f2280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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