1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsCycleCollectionParticipant.h"
11 #include "mozilla/dom/SpeechSynthesisEvent.h"
12 #include "mozilla/dom/SpeechSynthesisUtteranceBinding.h"
13 #include "SpeechSynthesisUtterance.h"
14 #include "SpeechSynthesisVoice.h"
18 namespace mozilla::dom
{
20 NS_IMPL_CYCLE_COLLECTION_INHERITED(SpeechSynthesisUtterance
,
21 DOMEventTargetHelper
, mVoice
);
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechSynthesisUtterance
)
24 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper
)
26 NS_IMPL_ADDREF_INHERITED(SpeechSynthesisUtterance
, DOMEventTargetHelper
)
27 NS_IMPL_RELEASE_INHERITED(SpeechSynthesisUtterance
, DOMEventTargetHelper
)
29 SpeechSynthesisUtterance::SpeechSynthesisUtterance(
30 nsPIDOMWindowInner
* aOwnerWindow
, const nsAString
& text
)
31 : DOMEventTargetHelper(aOwnerWindow
),
37 mShouldResistFingerprinting(
38 aOwnerWindow
->AsGlobal()->ShouldResistFingerprinting(
39 RFPTarget::SpeechSynthesis
)) {}
41 SpeechSynthesisUtterance::~SpeechSynthesisUtterance() = default;
43 JSObject
* SpeechSynthesisUtterance::WrapObject(
44 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
45 return SpeechSynthesisUtterance_Binding::Wrap(aCx
, this, aGivenProto
);
48 nsISupports
* SpeechSynthesisUtterance::GetParentObject() const {
49 return GetOwnerGlobal();
52 already_AddRefed
<SpeechSynthesisUtterance
>
53 SpeechSynthesisUtterance::Constructor(GlobalObject
& aGlobal
, ErrorResult
& aRv
) {
54 return Constructor(aGlobal
, u
""_ns
, aRv
);
57 already_AddRefed
<SpeechSynthesisUtterance
>
58 SpeechSynthesisUtterance::Constructor(GlobalObject
& aGlobal
,
59 const nsAString
& aText
,
61 nsCOMPtr
<nsPIDOMWindowInner
> win
= do_QueryInterface(aGlobal
.GetAsSupports());
64 aRv
.Throw(NS_ERROR_FAILURE
);
68 RefPtr
<SpeechSynthesisUtterance
> object
=
69 new SpeechSynthesisUtterance(win
, aText
);
70 return object
.forget();
73 void SpeechSynthesisUtterance::GetText(nsString
& aResult
) const {
77 void SpeechSynthesisUtterance::SetText(const nsAString
& aText
) {
81 void SpeechSynthesisUtterance::GetLang(nsString
& aResult
) const {
85 void SpeechSynthesisUtterance::SetLang(const nsAString
& aLang
) {
89 SpeechSynthesisVoice
* SpeechSynthesisUtterance::GetVoice() const {
93 void SpeechSynthesisUtterance::SetVoice(SpeechSynthesisVoice
* aVoice
) {
97 float SpeechSynthesisUtterance::Volume() const { return mVolume
; }
99 void SpeechSynthesisUtterance::SetVolume(float aVolume
) {
100 mVolume
= std::max
<float>(std::min
<float>(aVolume
, 1), 0);
103 float SpeechSynthesisUtterance::Rate() const { return mRate
; }
105 void SpeechSynthesisUtterance::SetRate(float aRate
) {
106 mRate
= std::max
<float>(std::min
<float>(aRate
, 10), 0.1f
);
109 float SpeechSynthesisUtterance::Pitch() const { return mPitch
; }
111 void SpeechSynthesisUtterance::SetPitch(float aPitch
) {
112 mPitch
= std::max
<float>(std::min
<float>(aPitch
, 2), 0);
115 void SpeechSynthesisUtterance::GetChosenVoiceURI(nsString
& aResult
) const {
116 aResult
= mChosenVoiceURI
;
119 void SpeechSynthesisUtterance::DispatchSpeechSynthesisEvent(
120 const nsAString
& aEventType
, uint32_t aCharIndex
,
121 const Nullable
<uint32_t>& aCharLength
, float aElapsedTime
,
122 const nsAString
& aName
) {
123 SpeechSynthesisEventInit init
;
124 init
.mBubbles
= false;
125 init
.mCancelable
= false;
126 init
.mUtterance
= this;
127 init
.mCharIndex
= aCharIndex
;
128 init
.mCharLength
= aCharLength
;
129 init
.mElapsedTime
= aElapsedTime
;
132 RefPtr
<SpeechSynthesisEvent
> event
=
133 SpeechSynthesisEvent::Constructor(this, aEventType
, init
);
134 DispatchTrustedEvent(event
);
137 } // namespace mozilla::dom