Bug 1943761 - Add class alignment to the mozsearch analysis file. r=asuth
[gecko.git] / dom / media / webspeech / synth / SpeechSynthesisUtterance.cpp
blob4c812eeb66da237f0321c9ba3e7fa31971e12413
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/. */
7 #include "nsCOMPtr.h"
8 #include "nsCycleCollectionParticipant.h"
9 #include "nsGkAtoms.h"
11 #include "mozilla/dom/SpeechSynthesisEvent.h"
12 #include "mozilla/dom/SpeechSynthesisUtteranceBinding.h"
13 #include "SpeechSynthesisUtterance.h"
14 #include "SpeechSynthesisVoice.h"
16 #include <stdlib.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),
32 mText(text),
33 mVolume(1),
34 mRate(1),
35 mPitch(1),
36 mPaused(false),
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,
60 ErrorResult& aRv) {
61 nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
63 if (!win) {
64 aRv.Throw(NS_ERROR_FAILURE);
65 return nullptr;
68 RefPtr<SpeechSynthesisUtterance> object =
69 new SpeechSynthesisUtterance(win, aText);
70 return object.forget();
73 void SpeechSynthesisUtterance::GetText(nsString& aResult) const {
74 aResult = mText;
77 void SpeechSynthesisUtterance::SetText(const nsAString& aText) {
78 mText = aText;
81 void SpeechSynthesisUtterance::GetLang(nsString& aResult) const {
82 aResult = mLang;
85 void SpeechSynthesisUtterance::SetLang(const nsAString& aLang) {
86 mLang = aLang;
89 SpeechSynthesisVoice* SpeechSynthesisUtterance::GetVoice() const {
90 return mVoice;
93 void SpeechSynthesisUtterance::SetVoice(SpeechSynthesisVoice* aVoice) {
94 mVoice = 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;
130 init.mName = aName;
132 RefPtr<SpeechSynthesisEvent> event =
133 SpeechSynthesisEvent::Constructor(this, aEventType, init);
134 DispatchTrustedEvent(event);
137 } // namespace mozilla::dom