Line data Source code
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/. */
6 :
7 : #include "nsCOMPtr.h"
8 : #include "nsCycleCollectionParticipant.h"
9 : #include "nsGkAtoms.h"
10 :
11 : #include "mozilla/dom/SpeechSynthesisEvent.h"
12 : #include "mozilla/dom/SpeechSynthesisUtteranceBinding.h"
13 : #include "SpeechSynthesisUtterance.h"
14 : #include "SpeechSynthesisVoice.h"
15 :
16 : #include <stdlib.h>
17 :
18 : namespace mozilla {
19 : namespace dom {
20 :
21 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(SpeechSynthesisUtterance,
22 : DOMEventTargetHelper, mVoice);
23 :
24 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(SpeechSynthesisUtterance)
25 0 : NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
26 :
27 0 : NS_IMPL_ADDREF_INHERITED(SpeechSynthesisUtterance, DOMEventTargetHelper)
28 0 : NS_IMPL_RELEASE_INHERITED(SpeechSynthesisUtterance, DOMEventTargetHelper)
29 :
30 0 : SpeechSynthesisUtterance::SpeechSynthesisUtterance(nsPIDOMWindowInner* aOwnerWindow,
31 0 : const nsAString& text)
32 : : DOMEventTargetHelper(aOwnerWindow)
33 : , mText(text)
34 : , mVolume(1)
35 : , mRate(1)
36 : , mPitch(1)
37 : , mState(STATE_NONE)
38 0 : , mPaused(false)
39 : {
40 0 : }
41 :
42 0 : SpeechSynthesisUtterance::~SpeechSynthesisUtterance() {}
43 :
44 : JSObject*
45 0 : SpeechSynthesisUtterance::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
46 : {
47 0 : return SpeechSynthesisUtteranceBinding::Wrap(aCx, this, aGivenProto);
48 : }
49 :
50 : nsISupports*
51 0 : SpeechSynthesisUtterance::GetParentObject() const
52 : {
53 0 : return GetOwner();
54 : }
55 :
56 : already_AddRefed<SpeechSynthesisUtterance>
57 0 : SpeechSynthesisUtterance::Constructor(GlobalObject& aGlobal,
58 : ErrorResult& aRv)
59 : {
60 0 : return Constructor(aGlobal, EmptyString(), aRv);
61 : }
62 :
63 : already_AddRefed<SpeechSynthesisUtterance>
64 0 : SpeechSynthesisUtterance::Constructor(GlobalObject& aGlobal,
65 : const nsAString& aText,
66 : ErrorResult& aRv)
67 : {
68 0 : nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
69 :
70 0 : if (!win) {
71 0 : aRv.Throw(NS_ERROR_FAILURE);
72 0 : return nullptr;
73 : }
74 :
75 0 : MOZ_ASSERT(win->IsInnerWindow());
76 : RefPtr<SpeechSynthesisUtterance> object =
77 0 : new SpeechSynthesisUtterance(win, aText);
78 0 : return object.forget();
79 : }
80 :
81 : void
82 0 : SpeechSynthesisUtterance::GetText(nsString& aResult) const
83 : {
84 0 : aResult = mText;
85 0 : }
86 :
87 : void
88 0 : SpeechSynthesisUtterance::SetText(const nsAString& aText)
89 : {
90 0 : mText = aText;
91 0 : }
92 :
93 : void
94 0 : SpeechSynthesisUtterance::GetLang(nsString& aResult) const
95 : {
96 0 : aResult = mLang;
97 0 : }
98 :
99 : void
100 0 : SpeechSynthesisUtterance::SetLang(const nsAString& aLang)
101 : {
102 0 : mLang = aLang;
103 0 : }
104 :
105 : SpeechSynthesisVoice*
106 0 : SpeechSynthesisUtterance::GetVoice() const
107 : {
108 0 : return mVoice;
109 : }
110 :
111 : void
112 0 : SpeechSynthesisUtterance::SetVoice(SpeechSynthesisVoice* aVoice)
113 : {
114 0 : mVoice = aVoice;
115 0 : }
116 :
117 : float
118 0 : SpeechSynthesisUtterance::Volume() const
119 : {
120 0 : return mVolume;
121 : }
122 :
123 : void
124 0 : SpeechSynthesisUtterance::SetVolume(float aVolume)
125 : {
126 0 : mVolume = std::max<float>(std::min<float>(aVolume, 1), 0);
127 0 : }
128 :
129 : float
130 0 : SpeechSynthesisUtterance::Rate() const
131 : {
132 0 : return mRate;
133 : }
134 :
135 : void
136 0 : SpeechSynthesisUtterance::SetRate(float aRate)
137 : {
138 0 : mRate = std::max<float>(std::min<float>(aRate, 10), 0.1f);
139 0 : }
140 :
141 : float
142 0 : SpeechSynthesisUtterance::Pitch() const
143 : {
144 0 : return mPitch;
145 : }
146 :
147 : void
148 0 : SpeechSynthesisUtterance::SetPitch(float aPitch)
149 : {
150 0 : mPitch = std::max<float>(std::min<float>(aPitch, 2), 0);
151 0 : }
152 :
153 : void
154 0 : SpeechSynthesisUtterance::GetChosenVoiceURI(nsString& aResult) const
155 : {
156 0 : aResult = mChosenVoiceURI;
157 0 : }
158 :
159 : void
160 0 : SpeechSynthesisUtterance::DispatchSpeechSynthesisEvent(const nsAString& aEventType,
161 : uint32_t aCharIndex,
162 : const Nullable<uint32_t>& aCharLength,
163 : float aElapsedTime,
164 : const nsAString& aName)
165 : {
166 0 : SpeechSynthesisEventInit init;
167 0 : init.mBubbles = false;
168 0 : init.mCancelable = false;
169 0 : init.mUtterance = this;
170 0 : init.mCharIndex = aCharIndex;
171 0 : init.mCharLength = aCharLength;
172 0 : init.mElapsedTime = aElapsedTime;
173 0 : init.mName = aName;
174 :
175 : RefPtr<SpeechSynthesisEvent> event =
176 0 : SpeechSynthesisEvent::Constructor(this, aEventType, init);
177 0 : DispatchTrustedEvent(event);
178 0 : }
179 :
180 : } // namespace dom
181 : } // namespace mozilla
|