|
41 | 41 | #include <wtf/IsoMallocInlines.h> |
42 | 42 | #include <wtf/NeverDestroyed.h> |
43 | 43 |
|
| 44 | +namespace { |
| 45 | +WebCore::SpeechSynthesisErrorCode toSpeechSynthesisErrorEventCode(WebCore::SpeechError error) { |
| 46 | + switch(error) { |
| 47 | + case WebCore::SpeechError::Canceled: return WebCore::SpeechSynthesisErrorCode::Canceled; |
| 48 | + case WebCore::SpeechError::Interrupted: return WebCore::SpeechSynthesisErrorCode::Interrupted; |
| 49 | + case WebCore::SpeechError::AudioBusy: return WebCore::SpeechSynthesisErrorCode::AudioBusy; |
| 50 | + case WebCore::SpeechError::AudioHardware: return WebCore::SpeechSynthesisErrorCode::AudioHardware; |
| 51 | + case WebCore::SpeechError::Network: return WebCore::SpeechSynthesisErrorCode::Network; |
| 52 | + case WebCore::SpeechError::SynthesisUnavailable: return WebCore::SpeechSynthesisErrorCode::SynthesisUnavailable; |
| 53 | + case WebCore::SpeechError::SynthesisFailed: return WebCore::SpeechSynthesisErrorCode::SynthesisFailed; |
| 54 | + case WebCore::SpeechError::LanguageUnavailable: return WebCore::SpeechSynthesisErrorCode::LanguageUnavailable; |
| 55 | + case WebCore::SpeechError::VoiceUnavailable: return WebCore::SpeechSynthesisErrorCode::VoiceUnavailable; |
| 56 | + case WebCore::SpeechError::TextTooLong: return WebCore::SpeechSynthesisErrorCode::TextTooLong; |
| 57 | + case WebCore::SpeechError::InvalidArgument: return WebCore::SpeechSynthesisErrorCode::InvalidArgument; |
| 58 | + case WebCore::SpeechError::NotAllowed: return WebCore::SpeechSynthesisErrorCode::NotAllowed; |
| 59 | + default: ASSERT(false, "Invalid SpeechError code"); return WebCore::SpeechSynthesisErrorCode::Interrupted; |
| 60 | + } |
| 61 | +} |
| 62 | +} // namespace |
| 63 | + |
44 | 64 | namespace WebCore { |
45 | 65 |
|
46 | 66 | WTF_MAKE_ISO_ALLOCATED_IMPL(SpeechSynthesis); |
@@ -207,18 +227,20 @@ void SpeechSynthesis::fireErrorEvent(const AtomString& type, SpeechSynthesisUtte |
207 | 227 | utterance.dispatchEvent(SpeechSynthesisErrorEvent::create(type, { { &utterance, 0, 0, static_cast<float>((MonotonicTime::now() - utterance.startTime()).seconds()), { } }, errorCode })); |
208 | 228 | } |
209 | 229 |
|
210 | | -void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance& utterance, bool errorOccurred) |
| 230 | +void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance& utterance, bool errorOccurred, SpeechError error) |
211 | 231 | { |
212 | 232 | ASSERT(m_currentSpeechUtterance); |
213 | 233 | Ref<SpeechSynthesisUtterance> protect(utterance); |
214 | 234 |
|
215 | | - m_currentSpeechUtterance = nullptr; |
| 235 | + if (m_currentSpeechUtterance == &utterance) |
| 236 | + m_currentSpeechUtterance = nullptr; |
216 | 237 |
|
217 | | - if (errorOccurred) |
218 | | - fireErrorEvent(eventNames().errorEvent, utterance, SpeechSynthesisErrorCode::Canceled); |
| 238 | + // For PlatformSpeechSynthesizer(s) that don't support ErrorEvent yet |
| 239 | + if (errorOccurred && error != SpeechError::None) |
| 240 | + fireErrorEvent(eventNames().errorEvent, utterance, toSpeechSynthesisErrorEventCode(error)); |
219 | 241 | else |
220 | 242 | fireEvent(eventNames().endEvent, utterance, 0, 0, String()); |
221 | | - |
| 243 | + |
222 | 244 | if (m_utteranceQueue.size()) { |
223 | 245 | Ref<SpeechSynthesisUtterance> firstUtterance = m_utteranceQueue.takeFirst(); |
224 | 246 | ASSERT(&utterance == firstUtterance.ptr()); |
@@ -280,7 +302,7 @@ void SpeechSynthesis::speakingErrorOccurred() |
280 | 302 | { |
281 | 303 | if (!m_currentSpeechUtterance) |
282 | 304 | return; |
283 | | - speakingErrorOccurred(*m_currentSpeechUtterance->platformUtterance()); |
| 305 | + speakingErrorOccurred(*m_currentSpeechUtterance->platformUtterance(), SpeechError::None); |
284 | 306 | } |
285 | 307 |
|
286 | 308 | void SpeechSynthesis::boundaryEventOccurred(bool wordBoundary, unsigned charIndex, unsigned charLength) |
@@ -321,10 +343,10 @@ void SpeechSynthesis::didFinishSpeaking(PlatformSpeechSynthesisUtterance& uttera |
321 | 343 | handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), false); |
322 | 344 | } |
323 | 345 |
|
324 | | -void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance& utterance) |
| 346 | +void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance& utterance, SpeechError error) |
325 | 347 | { |
326 | 348 | if (utterance.client()) |
327 | | - handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), true); |
| 349 | + handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), true, error); |
328 | 350 | } |
329 | 351 |
|
330 | 352 | } // namespace WebCore |
|
0 commit comments