tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RemoteSpellCheckEngineParent.cpp (2963B)


      1 /* vim: set ts=2 sw=2 sts=2 tw=80: */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "RemoteSpellCheckEngineParent.h"
      7 #include "mozilla/mozSpellChecker.h"
      8 #include "nsServiceManagerUtils.h"
      9 
     10 namespace mozilla {
     11 
     12 RemoteSpellcheckEngineParent::RemoteSpellcheckEngineParent() {
     13  mSpellChecker = mozSpellChecker::Create();
     14 }
     15 
     16 RemoteSpellcheckEngineParent::~RemoteSpellcheckEngineParent() {}
     17 
     18 mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvSetDictionary(
     19    const nsACString& aDictionary, bool* success) {
     20  nsresult rv = mSpellChecker->SetCurrentDictionary(aDictionary);
     21  *success = NS_SUCCEEDED(rv);
     22  return IPC_OK();
     23 }
     24 
     25 mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvSetDictionaries(
     26    const nsTArray<nsCString>& aDictionaries,
     27    SetDictionariesResolver&& aResolve) {
     28  mSpellChecker->SetCurrentDictionaries(aDictionaries)
     29      ->Then(
     30          GetMainThreadSerialEventTarget(), __func__,
     31          [aResolve]() { aResolve(true); }, [aResolve]() { aResolve(false); });
     32  return IPC_OK();
     33 }
     34 
     35 mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvSetDictionaryFromList(
     36    nsTArray<nsCString>&& aList, SetDictionaryFromListResolver&& aResolve) {
     37  for (auto& dictionary : aList) {
     38    nsresult rv = mSpellChecker->SetCurrentDictionary(dictionary);
     39    if (NS_SUCCEEDED(rv)) {
     40      aResolve(std::tuple<const bool&, const nsACString&>(true, dictionary));
     41      return IPC_OK();
     42    }
     43  }
     44  aResolve(std::tuple<const bool&, const nsACString&>(false, ""_ns));
     45  return IPC_OK();
     46 }
     47 
     48 mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvCheckAsync(
     49    nsTArray<nsString>&& aWords, CheckAsyncResolver&& aResolve) {
     50  nsTArray<bool> misspells;
     51  misspells.SetCapacity(aWords.Length());
     52  for (auto& word : aWords) {
     53    bool misspelled;
     54    nsresult rv = mSpellChecker->CheckWord(word, &misspelled, nullptr);
     55    // If CheckWord failed, we can't tell whether the word is correctly spelled
     56    if (NS_FAILED(rv)) {
     57      misspelled = false;
     58    }
     59    misspells.AppendElement(misspelled);
     60  }
     61  aResolve(std::move(misspells));
     62  return IPC_OK();
     63 }
     64 
     65 mozilla::ipc::IPCResult RemoteSpellcheckEngineParent::RecvSuggest(
     66    const nsAString& aWord, uint32_t aCount, SuggestResolver&& aResolve) {
     67  nsTArray<nsString> suggestions;
     68  mSpellChecker->Suggest(aWord, aCount)
     69      ->Then(
     70          GetMainThreadSerialEventTarget(), __func__,
     71          [aResolve](CopyableTArray<nsString> aSuggestions) {
     72            aResolve(std::move(aSuggestions));
     73          },
     74          [aResolve](nsresult aError) {
     75            // No suggestions due to error
     76            nsTArray<nsString> suggestions;
     77            aResolve(std::move(suggestions));
     78          });
     79  return IPC_OK();
     80 }
     81 
     82 void RemoteSpellcheckEngineParent::ActorDestroy(ActorDestroyReason aWhy) {}
     83 
     84 }  // namespace mozilla