3131#include " EventNames.h"
3232#include " PlatformSpeechSynthesisVoice.h"
3333#include " PlatformSpeechSynthesizer.h"
34- #include " SpeechSynthesisErrorEvent .h"
34+ #include " SpeechSynthesisEvent .h"
3535#include " SpeechSynthesisUtterance.h"
3636#include " UserGestureIndicator.h"
37- #include " Logging.h"
3837#include < wtf/NeverDestroyed.h>
3938
4039namespace WebCore {
@@ -73,7 +72,7 @@ const Vector<Ref<SpeechSynthesisVoice>>& SpeechSynthesis::getVoices()
7372 return m_voiceList;
7473
7574 if (!m_platformSpeechSynthesizer)
76- m_platformSpeechSynthesizer = PlatformSpeechSynthesizer::create (this );
75+ m_platformSpeechSynthesizer = std::make_unique<PlatformSpeechSynthesizer> (this );
7776
7877 // If the voiceList is empty, that's the cue to get the voices from the platform again.
7978 for (auto & voice : m_platformSpeechSynthesizer->voiceList ())
@@ -110,12 +109,12 @@ void SpeechSynthesis::startSpeakingImmediately(SpeechSynthesisUtterance& utteran
110109
111110 // Zero lengthed strings should immediately notify that the event is complete.
112111 if (utterance.text ().isEmpty ()) {
113- handleSpeakingCompleted (utterance, SpeechErrorNone );
112+ handleSpeakingCompleted (utterance, false );
114113 return ;
115114 }
116115
117116 if (!m_platformSpeechSynthesizer)
118- m_platformSpeechSynthesizer = PlatformSpeechSynthesizer::create (this );
117+ m_platformSpeechSynthesizer = std::make_unique<PlatformSpeechSynthesizer> (this );
119118 m_platformSpeechSynthesizer->speak (utterance.platformUtterance ());
120119}
121120
@@ -141,19 +140,11 @@ void SpeechSynthesis::cancel()
141140 // Remove all the items from the utterance queue.
142141 // Hold on to the current utterance so the platform synthesizer can have a chance to clean up.
143142 RefPtr<SpeechSynthesisUtterance> current = m_currentSpeechUtterance;
144- Deque<Ref<SpeechSynthesisUtterance>> utteranceQueue = WTFMove ( m_utteranceQueue);
143+ m_utteranceQueue. clear ( );
145144 if (m_platformSpeechSynthesizer)
146145 m_platformSpeechSynthesizer->cancel ();
147146 current = nullptr ;
148147
149- // Trigger canceled events for queued utterances
150- while (utteranceQueue.size () > 0 ) {
151- Ref<SpeechSynthesisUtterance> utterance = utteranceQueue.takeFirst ();
152- if (m_currentSpeechUtterance != utterance.ptr ())
153- fireErrorEvent (utterance, SpeechErrorCanceled);
154- }
155- utteranceQueue.clear ();
156-
157148 // The platform should have called back immediately and cleared the current utterance.
158149 ASSERT (!m_currentSpeechUtterance);
159150}
@@ -170,46 +161,19 @@ void SpeechSynthesis::resume()
170161 m_platformSpeechSynthesizer->resume ();
171162}
172163
173- SpeechSynthesisErrorEvent::Code toSpeechSynthesisErrorEventCode (SpeechError error) {
174- switch (error) {
175- case SpeechErrorCanceled: return SpeechSynthesisErrorEvent::Code::Canceled;
176- case SpeechErrorInterrupted: return SpeechSynthesisErrorEvent::Code::Interrupted;
177- case SpeechErrorAudioBusy: return SpeechSynthesisErrorEvent::Code::AudioBusy;
178- case SpeechErrorAudioHardware: return SpeechSynthesisErrorEvent::Code::AudioHardware;
179- case SpeechErrorNetwork: return SpeechSynthesisErrorEvent::Code::Network;
180- case SpeechErrorSynthesisUnavailable: return SpeechSynthesisErrorEvent::Code::SynthesisUnavailable;
181- case SpeechErrorSynthesisFailed: return SpeechSynthesisErrorEvent::Code::SynthesisFailed;
182- case SpeechErrorLanguageUnavailable: return SpeechSynthesisErrorEvent::Code::LanguageUnavailable;
183- case SpeechErrorVoiceUnavailable: return SpeechSynthesisErrorEvent::Code::VoiceUnavailable;
184- case SpeechErrorTextTooLong: return SpeechSynthesisErrorEvent::Code::TextTooLong;
185- case SpeechErrorInvalidArgument: return SpeechSynthesisErrorEvent::Code::InvalidArgument;
186- case SpeechErrorNotAllowed: return SpeechSynthesisErrorEvent::Code::NotAllowed;
187- default : return SpeechSynthesisErrorEvent::Code::Interrupted;
188- }
189- }
190-
191164void SpeechSynthesis::fireEvent (const AtomicString& type, SpeechSynthesisUtterance& utterance, unsigned long charIndex, const String& name)
192165{
193166 utterance.dispatchEvent (SpeechSynthesisEvent::create (type, charIndex, (MonotonicTime::now () - utterance.startTime ()).seconds (), name));
194167}
195168
196- void SpeechSynthesis::fireErrorEvent (SpeechSynthesisUtterance& utterance, SpeechError error)
197- {
198- utterance.dispatchEvent (SpeechSynthesisErrorEvent::create (toSpeechSynthesisErrorEventCode (error)));
199- }
200-
201- void SpeechSynthesis::handleSpeakingCompleted (SpeechSynthesisUtterance& utterance, SpeechError error)
169+ void SpeechSynthesis::handleSpeakingCompleted (SpeechSynthesisUtterance& utterance, bool errorOccurred)
202170{
203171 ASSERT (m_currentSpeechUtterance);
204172 Ref<SpeechSynthesisUtterance> protect (utterance);
205173
206- if (m_currentSpeechUtterance == &utterance)
207- m_currentSpeechUtterance = nullptr ;
174+ m_currentSpeechUtterance = nullptr ;
208175
209- if (error == SpeechErrorNone)
210- fireEvent (eventNames ().endEvent , utterance, 0 , String ());
211- else
212- fireErrorEvent (utterance, error);
176+ fireEvent (errorOccurred ? eventNames ().errorEvent : eventNames ().endEvent , utterance, 0 , String ());
213177
214178 if (m_utteranceQueue.size ()) {
215179 Ref<SpeechSynthesisUtterance> firstUtterance = m_utteranceQueue.takeFirst ();
@@ -240,20 +204,6 @@ void SpeechSynthesis::boundaryEventOccurred(PlatformSpeechSynthesisUtterance& ut
240204 }
241205}
242206
243- double SpeechSynthesis::getPageMediaVolume ()
244- {
245- if (m_currentSpeechUtterance)
246- return m_currentSpeechUtterance->getPageMediaVolume ();
247-
248- return 0.0 ;
249- }
250-
251- void SpeechSynthesis::setPageMediaVolume (double volume)
252- {
253- if (m_currentSpeechUtterance)
254- m_currentSpeechUtterance->setPageMediaVolume (volume);
255- }
256-
257207void SpeechSynthesis::didStartSpeaking (PlatformSpeechSynthesisUtterance& utterance)
258208{
259209 if (utterance.client ())
@@ -277,13 +227,13 @@ void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance& uttera
277227void SpeechSynthesis::didFinishSpeaking (PlatformSpeechSynthesisUtterance& utterance)
278228{
279229 if (utterance.client ())
280- handleSpeakingCompleted (static_cast <SpeechSynthesisUtterance&>(*utterance.client ()), SpeechErrorNone );
230+ handleSpeakingCompleted (static_cast <SpeechSynthesisUtterance&>(*utterance.client ()), false );
281231}
282232
283- void SpeechSynthesis::speakingErrorOccurred (PlatformSpeechSynthesisUtterance& utterance, SpeechError error )
233+ void SpeechSynthesis::speakingErrorOccurred (PlatformSpeechSynthesisUtterance& utterance)
284234{
285235 if (utterance.client ())
286- handleSpeakingCompleted (static_cast <SpeechSynthesisUtterance&>(*utterance.client ()), error );
236+ handleSpeakingCompleted (static_cast <SpeechSynthesisUtterance&>(*utterance.client ()), true );
287237}
288238
289239} // namespace WebCore
0 commit comments