tor-browser

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

SpeechSynthesisParent.cpp (5999B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "SpeechSynthesisParent.h"
      6 
      7 #include "nsSynthVoiceRegistry.h"
      8 
      9 namespace mozilla::dom {
     10 
     11 SpeechSynthesisParent::SpeechSynthesisParent() {
     12  MOZ_COUNT_CTOR(SpeechSynthesisParent);
     13 }
     14 
     15 SpeechSynthesisParent::~SpeechSynthesisParent() {
     16  MOZ_COUNT_DTOR(SpeechSynthesisParent);
     17 }
     18 
     19 void SpeechSynthesisParent::ActorDestroy(ActorDestroyReason aWhy) {
     20  // Implement me! Bug 1005141
     21 }
     22 
     23 bool SpeechSynthesisParent::SendInit() {
     24  return nsSynthVoiceRegistry::GetInstance()->SendInitialVoicesAndState(this);
     25 }
     26 
     27 PSpeechSynthesisRequestParent*
     28 SpeechSynthesisParent::AllocPSpeechSynthesisRequestParent(
     29    const nsAString& aText, const nsAString& aLang, const nsAString& aUri,
     30    const float& aVolume, const float& aRate, const float& aPitch,
     31    const bool& aShouldResistFingerprinting) {
     32  RefPtr<SpeechTaskParent> task =
     33      new SpeechTaskParent(aVolume, aText, aShouldResistFingerprinting);
     34  SpeechSynthesisRequestParent* actor = new SpeechSynthesisRequestParent(task);
     35  return actor;
     36 }
     37 
     38 bool SpeechSynthesisParent::DeallocPSpeechSynthesisRequestParent(
     39    PSpeechSynthesisRequestParent* aActor) {
     40  delete aActor;
     41  return true;
     42 }
     43 
     44 mozilla::ipc::IPCResult
     45 SpeechSynthesisParent::RecvPSpeechSynthesisRequestConstructor(
     46    PSpeechSynthesisRequestParent* aActor, const nsAString& aText,
     47    const nsAString& aLang, const nsAString& aUri, const float& aVolume,
     48    const float& aRate, const float& aPitch,
     49    const bool& aShouldResistFingerprinting) {
     50  MOZ_ASSERT(aActor);
     51  SpeechSynthesisRequestParent* actor =
     52      static_cast<SpeechSynthesisRequestParent*>(aActor);
     53  nsSynthVoiceRegistry::GetInstance()->Speak(aText, aLang, aUri, aVolume, aRate,
     54                                             aPitch, actor->mTask);
     55  return IPC_OK();
     56 }
     57 
     58 // SpeechSynthesisRequestParent
     59 
     60 SpeechSynthesisRequestParent::SpeechSynthesisRequestParent(
     61    SpeechTaskParent* aTask)
     62    : mTask(aTask) {
     63  mTask->mActor = this;
     64  MOZ_COUNT_CTOR(SpeechSynthesisRequestParent);
     65 }
     66 
     67 SpeechSynthesisRequestParent::~SpeechSynthesisRequestParent() {
     68  if (mTask) {
     69    mTask->mActor = nullptr;
     70    // If we still have a task, cancel it.
     71    mTask->Cancel();
     72  }
     73  MOZ_COUNT_DTOR(SpeechSynthesisRequestParent);
     74 }
     75 
     76 void SpeechSynthesisRequestParent::ActorDestroy(ActorDestroyReason aWhy) {
     77  // Implement me! Bug 1005141
     78 }
     79 
     80 mozilla::ipc::IPCResult SpeechSynthesisRequestParent::RecvPause() {
     81  MOZ_ASSERT(mTask);
     82  mTask->Pause();
     83  return IPC_OK();
     84 }
     85 
     86 mozilla::ipc::IPCResult SpeechSynthesisRequestParent::Recv__delete__() {
     87  MOZ_ASSERT(mTask);
     88  mTask->mActor = nullptr;
     89  mTask = nullptr;
     90  return IPC_OK();
     91 }
     92 
     93 mozilla::ipc::IPCResult SpeechSynthesisRequestParent::RecvResume() {
     94  MOZ_ASSERT(mTask);
     95  mTask->Resume();
     96  return IPC_OK();
     97 }
     98 
     99 mozilla::ipc::IPCResult SpeechSynthesisRequestParent::RecvCancel() {
    100  MOZ_ASSERT(mTask);
    101  mTask->Cancel();
    102  return IPC_OK();
    103 }
    104 
    105 mozilla::ipc::IPCResult SpeechSynthesisRequestParent::RecvForceEnd() {
    106  MOZ_ASSERT(mTask);
    107  mTask->ForceEnd();
    108  return IPC_OK();
    109 }
    110 
    111 mozilla::ipc::IPCResult SpeechSynthesisRequestParent::RecvSetAudioOutputVolume(
    112    const float& aVolume) {
    113  MOZ_ASSERT(mTask);
    114  mTask->SetAudioOutputVolume(aVolume);
    115  return IPC_OK();
    116 }
    117 
    118 // SpeechTaskParent
    119 
    120 nsresult SpeechTaskParent::DispatchStartImpl(const nsAString& aUri) {
    121  if (!mActor) {
    122    // Child is already gone.
    123    return NS_OK;
    124  }
    125 
    126  if (NS_WARN_IF(!(mActor->SendOnStart(aUri)))) {
    127    return NS_ERROR_FAILURE;
    128  }
    129 
    130  return NS_OK;
    131 }
    132 
    133 nsresult SpeechTaskParent::DispatchEndImpl(float aElapsedTime,
    134                                           uint32_t aCharIndex) {
    135  if (!mActor) {
    136    // Child is already gone.
    137    return NS_OK;
    138  }
    139 
    140  if (NS_WARN_IF(!(mActor->SendOnEnd(false, aElapsedTime, aCharIndex)))) {
    141    return NS_ERROR_FAILURE;
    142  }
    143 
    144  return NS_OK;
    145 }
    146 
    147 nsresult SpeechTaskParent::DispatchPauseImpl(float aElapsedTime,
    148                                             uint32_t aCharIndex) {
    149  if (!mActor) {
    150    // Child is already gone.
    151    return NS_OK;
    152  }
    153 
    154  if (NS_WARN_IF(!(mActor->SendOnPause(aElapsedTime, aCharIndex)))) {
    155    return NS_ERROR_FAILURE;
    156  }
    157 
    158  return NS_OK;
    159 }
    160 
    161 nsresult SpeechTaskParent::DispatchResumeImpl(float aElapsedTime,
    162                                              uint32_t aCharIndex) {
    163  if (!mActor) {
    164    // Child is already gone.
    165    return NS_OK;
    166  }
    167 
    168  if (NS_WARN_IF(!(mActor->SendOnResume(aElapsedTime, aCharIndex)))) {
    169    return NS_ERROR_FAILURE;
    170  }
    171 
    172  return NS_OK;
    173 }
    174 
    175 nsresult SpeechTaskParent::DispatchErrorImpl(float aElapsedTime,
    176                                             uint32_t aCharIndex) {
    177  if (!mActor) {
    178    // Child is already gone.
    179    return NS_OK;
    180  }
    181 
    182  if (NS_WARN_IF(!(mActor->SendOnEnd(true, aElapsedTime, aCharIndex)))) {
    183    return NS_ERROR_FAILURE;
    184  }
    185 
    186  return NS_OK;
    187 }
    188 
    189 nsresult SpeechTaskParent::DispatchBoundaryImpl(const nsAString& aName,
    190                                                float aElapsedTime,
    191                                                uint32_t aCharIndex,
    192                                                uint32_t aCharLength,
    193                                                uint8_t argc) {
    194  if (!mActor) {
    195    // Child is already gone.
    196    return NS_OK;
    197  }
    198 
    199  if (NS_WARN_IF(!(mActor->SendOnBoundary(aName, aElapsedTime, aCharIndex,
    200                                          aCharLength, argc)))) {
    201    return NS_ERROR_FAILURE;
    202  }
    203 
    204  return NS_OK;
    205 }
    206 
    207 nsresult SpeechTaskParent::DispatchMarkImpl(const nsAString& aName,
    208                                            float aElapsedTime,
    209                                            uint32_t aCharIndex) {
    210  if (!mActor) {
    211    // Child is already gone.
    212    return NS_OK;
    213  }
    214 
    215  if (NS_WARN_IF(!(mActor->SendOnMark(aName, aElapsedTime, aCharIndex)))) {
    216    return NS_ERROR_FAILURE;
    217  }
    218 
    219  return NS_OK;
    220 }
    221 
    222 }  // namespace mozilla::dom