NeverDestroyed<String>(ASCIILiteral(...)) is not thread safe.
[WebKit-https.git] / Source / WebCore / Modules / speech / SpeechSynthesis.cpp
1 /*
2  * Copyright (C) 2013-2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "SpeechSynthesis.h"
28
29 #if ENABLE(SPEECH_SYNTHESIS)
30
31 #include "EventNames.h"
32 #include "PlatformSpeechSynthesisVoice.h"
33 #include "PlatformSpeechSynthesizer.h"
34 #include "ScriptController.h"
35 #include "SpeechSynthesisEvent.h"
36 #include "SpeechSynthesisUtterance.h"
37 #include <wtf/CurrentTime.h>
38 #include <wtf/NeverDestroyed.h>
39
40 namespace WebCore {
41
42 Ref<SpeechSynthesis> SpeechSynthesis::create()
43 {
44     return adoptRef(*new SpeechSynthesis);
45 }
46
47 SpeechSynthesis::SpeechSynthesis()
48     : m_currentSpeechUtterance(nullptr)
49     , m_isPaused(false)
50 #if PLATFORM(IOS)
51     , m_restrictions(RequireUserGestureForSpeechStartRestriction)
52 #endif
53 {
54 }
55
56 void SpeechSynthesis::setPlatformSynthesizer(std::unique_ptr<PlatformSpeechSynthesizer> synthesizer)
57 {
58     m_platformSpeechSynthesizer = WTFMove(synthesizer);
59     m_voiceList.clear();
60     m_currentSpeechUtterance = nullptr;
61     m_utteranceQueue.clear();
62     m_isPaused = false;
63 }
64
65 void SpeechSynthesis::voicesDidChange()
66 {
67     m_voiceList.clear();
68 }
69
70 const Vector<Ref<SpeechSynthesisVoice>>& SpeechSynthesis::getVoices()
71 {
72     if (m_voiceList.size())
73         return m_voiceList;
74
75     if (!m_platformSpeechSynthesizer)
76         m_platformSpeechSynthesizer = std::make_unique<PlatformSpeechSynthesizer>(this);
77
78     // If the voiceList is empty, that's the cue to get the voices from the platform again.
79     for (auto& voice : m_platformSpeechSynthesizer->voiceList())
80         m_voiceList.append(SpeechSynthesisVoice::create(*voice));
81
82     return m_voiceList;
83 }
84
85 bool SpeechSynthesis::speaking() const
86 {
87     // If we have a current speech utterance, then that means we're assumed to be in a speaking state.
88     // This state is independent of whether the utterance happens to be paused.
89     return m_currentSpeechUtterance;
90 }
91
92 bool SpeechSynthesis::pending() const
93 {
94     // This is true if there are any utterances that have not started.
95     // That means there will be more than one in the queue.
96     return m_utteranceQueue.size() > 1;
97 }
98
99 bool SpeechSynthesis::paused() const
100 {
101     return m_isPaused;
102 }
103
104 void SpeechSynthesis::startSpeakingImmediately(SpeechSynthesisUtterance& utterance)
105 {
106     ASSERT(!m_currentSpeechUtterance);
107     utterance.setStartTime(monotonicallyIncreasingTime());
108     m_currentSpeechUtterance = &utterance;
109     m_isPaused = false;
110
111     // Zero lengthed strings should immediately notify that the event is complete.
112     if (utterance.text().isEmpty()) {
113         handleSpeakingCompleted(utterance, false);
114         return;
115     }
116
117     if (!m_platformSpeechSynthesizer)
118         m_platformSpeechSynthesizer = std::make_unique<PlatformSpeechSynthesizer>(this);
119     m_platformSpeechSynthesizer->speak(utterance.platformUtterance());
120 }
121
122 void SpeechSynthesis::speak(SpeechSynthesisUtterance& utterance)
123 {
124     // Like Audio, we should require that the user interact to start a speech synthesis session.
125 #if PLATFORM(IOS)
126     if (ScriptController::processingUserGesture())
127         removeBehaviorRestriction(RequireUserGestureForSpeechStartRestriction);
128     else if (userGestureRequiredForSpeechStart())
129         return;
130 #endif
131
132     m_utteranceQueue.append(utterance);
133
134     // If the queue was empty, speak this immediately and add it to the queue.
135     if (m_utteranceQueue.size() == 1)
136         startSpeakingImmediately(m_utteranceQueue.first());
137 }
138
139 void SpeechSynthesis::cancel()
140 {
141     // Remove all the items from the utterance queue.
142     // Hold on to the current utterance so the platform synthesizer can have a chance to clean up.
143     RefPtr<SpeechSynthesisUtterance> current = m_currentSpeechUtterance;
144     m_utteranceQueue.clear();
145     if (m_platformSpeechSynthesizer)
146         m_platformSpeechSynthesizer->cancel();
147     current = nullptr;
148
149     // The platform should have called back immediately and cleared the current utterance.
150     ASSERT(!m_currentSpeechUtterance);
151 }
152
153 void SpeechSynthesis::pause()
154 {
155     if (!m_isPaused && m_platformSpeechSynthesizer)
156         m_platformSpeechSynthesizer->pause();
157 }
158
159 void SpeechSynthesis::resume()
160 {
161     if (m_currentSpeechUtterance && m_platformSpeechSynthesizer)
162         m_platformSpeechSynthesizer->resume();
163 }
164
165 void SpeechSynthesis::fireEvent(const AtomicString& type, SpeechSynthesisUtterance& utterance, unsigned long charIndex, const String& name)
166 {
167     utterance.dispatchEvent(SpeechSynthesisEvent::create(type, charIndex, (monotonicallyIncreasingTime() - utterance.startTime()), name));
168 }
169
170 void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance& utterance, bool errorOccurred)
171 {
172     ASSERT(m_currentSpeechUtterance);
173     Ref<SpeechSynthesisUtterance> protect(utterance);
174
175     m_currentSpeechUtterance = nullptr;
176
177     fireEvent(errorOccurred ? eventNames().errorEvent : eventNames().endEvent, utterance, 0, String());
178
179     if (m_utteranceQueue.size()) {
180         Ref<SpeechSynthesisUtterance> firstUtterance = m_utteranceQueue.takeFirst();
181         ASSERT(&utterance == firstUtterance.ptr());
182
183         // Start the next job if there is one pending.
184         if (!m_utteranceQueue.isEmpty())
185             startSpeakingImmediately(m_utteranceQueue.first());
186     }
187 }
188
189 void SpeechSynthesis::boundaryEventOccurred(PlatformSpeechSynthesisUtterance& utterance, SpeechBoundary boundary, unsigned charIndex)
190 {
191     static NeverDestroyed<const String> wordBoundaryString(MAKE_STATIC_STRING_IMPL("word"));
192     static NeverDestroyed<const String> sentenceBoundaryString(MAKE_STATIC_STRING_IMPL("sentence"));
193
194     ASSERT(utterance.client());
195
196     switch (boundary) {
197     case SpeechWordBoundary:
198         fireEvent(eventNames().boundaryEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), charIndex, wordBoundaryString);
199         break;
200     case SpeechSentenceBoundary:
201         fireEvent(eventNames().boundaryEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), charIndex, sentenceBoundaryString);
202         break;
203     default:
204         ASSERT_NOT_REACHED();
205     }
206 }
207
208 void SpeechSynthesis::didStartSpeaking(PlatformSpeechSynthesisUtterance& utterance)
209 {
210     if (utterance.client())
211         fireEvent(eventNames().startEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), 0, String());
212 }
213
214 void SpeechSynthesis::didPauseSpeaking(PlatformSpeechSynthesisUtterance& utterance)
215 {
216     m_isPaused = true;
217     if (utterance.client())
218         fireEvent(eventNames().pauseEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), 0, String());
219 }
220
221 void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance& utterance)
222 {
223     m_isPaused = false;
224     if (utterance.client())
225         fireEvent(eventNames().resumeEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), 0, String());
226 }
227
228 void SpeechSynthesis::didFinishSpeaking(PlatformSpeechSynthesisUtterance& utterance)
229 {
230     if (utterance.client())
231         handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), false);
232 }
233
234 void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance& utterance)
235 {
236     if (utterance.client())
237         handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), true);
238 }
239
240 } // namespace WebCore
241
242 #endif // ENABLE(SPEECH_SYNTHESIS)