michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "SpeechSynthesisParent.h" michael@0: #include "nsSynthVoiceRegistry.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: SpeechSynthesisParent::SpeechSynthesisParent() michael@0: { michael@0: MOZ_COUNT_CTOR(SpeechSynthesisParent); michael@0: } michael@0: michael@0: SpeechSynthesisParent::~SpeechSynthesisParent() michael@0: { michael@0: MOZ_COUNT_DTOR(SpeechSynthesisParent); michael@0: } michael@0: michael@0: bool michael@0: SpeechSynthesisParent::RecvReadVoiceList(InfallibleTArray* aVoices, michael@0: InfallibleTArray* aDefaults) michael@0: { michael@0: nsSynthVoiceRegistry::GetInstance()->SendVoices(aVoices, aDefaults); michael@0: return true; michael@0: } michael@0: michael@0: PSpeechSynthesisRequestParent* michael@0: SpeechSynthesisParent::AllocPSpeechSynthesisRequestParent(const nsString& aText, michael@0: const nsString& aLang, michael@0: const nsString& aUri, michael@0: const float& aVolume, michael@0: const float& aRate, michael@0: const float& aPitch) michael@0: { michael@0: nsRefPtr task = new SpeechTaskParent(aVolume, aText); michael@0: SpeechSynthesisRequestParent* actor = new SpeechSynthesisRequestParent(task); michael@0: return actor; michael@0: } michael@0: michael@0: bool michael@0: SpeechSynthesisParent::DeallocPSpeechSynthesisRequestParent(PSpeechSynthesisRequestParent* aActor) michael@0: { michael@0: delete aActor; michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: SpeechSynthesisParent::RecvPSpeechSynthesisRequestConstructor(PSpeechSynthesisRequestParent* aActor, michael@0: const nsString& aText, michael@0: const nsString& aLang, michael@0: const nsString& aUri, michael@0: const float& aVolume, michael@0: const float& aRate, michael@0: const float& aPitch) michael@0: { michael@0: MOZ_ASSERT(aActor); michael@0: SpeechSynthesisRequestParent* actor = michael@0: static_cast(aActor); michael@0: nsSynthVoiceRegistry::GetInstance()->Speak(aText, aLang, aUri, aRate, michael@0: aPitch, actor->mTask); michael@0: return true; michael@0: } michael@0: michael@0: // SpeechSynthesisRequestParent michael@0: michael@0: SpeechSynthesisRequestParent::SpeechSynthesisRequestParent(SpeechTaskParent* aTask) michael@0: : mTask(aTask) michael@0: { michael@0: mTask->mActor = this; michael@0: MOZ_COUNT_CTOR(SpeechSynthesisRequestParent); michael@0: } michael@0: michael@0: SpeechSynthesisRequestParent::~SpeechSynthesisRequestParent() michael@0: { michael@0: if (mTask && mTask->mActor) { michael@0: mTask->mActor = nullptr; michael@0: } michael@0: michael@0: MOZ_COUNT_DTOR(SpeechSynthesisRequestParent); michael@0: } michael@0: michael@0: bool michael@0: SpeechSynthesisRequestParent::RecvPause() michael@0: { michael@0: MOZ_ASSERT(mTask); michael@0: mTask->Pause(); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: SpeechSynthesisRequestParent::RecvResume() michael@0: { michael@0: MOZ_ASSERT(mTask); michael@0: mTask->Resume(); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: SpeechSynthesisRequestParent::RecvCancel() michael@0: { michael@0: MOZ_ASSERT(mTask); michael@0: mTask->Cancel(); michael@0: return true; michael@0: } michael@0: michael@0: // SpeechTaskParent michael@0: michael@0: nsresult michael@0: SpeechTaskParent::DispatchStartImpl() michael@0: { michael@0: MOZ_ASSERT(mActor); michael@0: NS_ENSURE_TRUE(mActor->SendOnStart(), NS_ERROR_FAILURE); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: SpeechTaskParent::DispatchEndImpl(float aElapsedTime, uint32_t aCharIndex) michael@0: { michael@0: MOZ_ASSERT(mActor); michael@0: SpeechSynthesisRequestParent* actor = mActor; michael@0: mActor = nullptr; michael@0: NS_ENSURE_TRUE(actor->Send__delete__(actor, false, aElapsedTime, aCharIndex), michael@0: NS_ERROR_FAILURE); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: SpeechTaskParent::DispatchPauseImpl(float aElapsedTime, uint32_t aCharIndex) michael@0: { michael@0: MOZ_ASSERT(mActor); michael@0: NS_ENSURE_TRUE(mActor->SendOnPause(aElapsedTime, aCharIndex), NS_ERROR_FAILURE); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: SpeechTaskParent::DispatchResumeImpl(float aElapsedTime, uint32_t aCharIndex) michael@0: { michael@0: MOZ_ASSERT(mActor); michael@0: NS_ENSURE_TRUE(mActor->SendOnResume(aElapsedTime, aCharIndex), NS_ERROR_FAILURE); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: SpeechTaskParent::DispatchErrorImpl(float aElapsedTime, uint32_t aCharIndex) michael@0: { michael@0: MOZ_ASSERT(mActor); michael@0: SpeechSynthesisRequestParent* actor = mActor; michael@0: mActor = nullptr; michael@0: NS_ENSURE_TRUE(actor->Send__delete__(actor, true, aElapsedTime, aCharIndex), michael@0: NS_ERROR_FAILURE); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: SpeechTaskParent::DispatchBoundaryImpl(const nsAString& aName, michael@0: float aElapsedTime, uint32_t aCharIndex) michael@0: { michael@0: MOZ_ASSERT(mActor); michael@0: NS_ENSURE_TRUE(mActor->SendOnBoundary(nsString(aName), aElapsedTime, aCharIndex), michael@0: NS_ERROR_FAILURE); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: SpeechTaskParent::DispatchMarkImpl(const nsAString& aName, michael@0: float aElapsedTime, uint32_t aCharIndex) michael@0: { michael@0: MOZ_ASSERT(mActor); michael@0: NS_ENSURE_TRUE(mActor->SendOnMark(nsString(aName), aElapsedTime, aCharIndex), michael@0: NS_ERROR_FAILURE); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla