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