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