[WTF] Add makeUnique<T>, which ensures T is fast-allocated, makeUnique / makeUniqueWi...
[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 "SpeechSynthesisEvent.h"
35 #include "SpeechSynthesisUtterance.h"
36 #include "UserGestureIndicator.h"
37 #include <wtf/NeverDestroyed.h>
38
39 namespace WebCore {
40
41 Ref<SpeechSynthesis> SpeechSynthesis::create(WeakPtr<SpeechSynthesisClient> client)
42 {
43     return adoptRef(*new SpeechSynthesis(client));
44 }
45
46 SpeechSynthesis::SpeechSynthesis(WeakPtr<SpeechSynthesisClient> client)
47     : m_currentSpeechUtterance(nullptr)
48     , m_isPaused(false)
49 #if PLATFORM(IOS_FAMILY)
50     , m_restrictions(RequireUserGestureForSpeechStartRestriction)
51 #endif
52     , m_speechSynthesisClient(client)
53 {
54     if (m_speechSynthesisClient)
55         m_speechSynthesisClient->setObserver(makeWeakPtr(this));
56 }
57
58 void SpeechSynthesis::setPlatformSynthesizer(std::unique_ptr<PlatformSpeechSynthesizer> synthesizer)
59 {
60     m_platformSpeechSynthesizer = WTFMove(synthesizer);
61     m_voiceList.clear();
62     m_currentSpeechUtterance = nullptr;
63     m_utteranceQueue.clear();
64     m_isPaused = false;
65     m_speechSynthesisClient = nullptr;
66 }
67
68 void SpeechSynthesis::voicesDidChange()
69 {
70     m_voiceList.clear();
71 }
72
73 PlatformSpeechSynthesizer& SpeechSynthesis::ensurePlatformSpeechSynthesizer()
74 {
75     if (!m_platformSpeechSynthesizer)
76         m_platformSpeechSynthesizer = makeUnique<PlatformSpeechSynthesizer>(this);
77     return *m_platformSpeechSynthesizer;
78 }
79
80 const Vector<Ref<SpeechSynthesisVoice>>& SpeechSynthesis::getVoices()
81 {
82     if (!m_voiceList.isEmpty())
83         return m_voiceList;
84
85     // If the voiceList is empty, that's the cue to get the voices from the platform again.
86     for (auto& voice : m_speechSynthesisClient ? m_speechSynthesisClient->voiceList() : ensurePlatformSpeechSynthesizer().voiceList())
87         m_voiceList.append(SpeechSynthesisVoice::create(*voice));
88
89     return m_voiceList;
90 }
91
92 bool SpeechSynthesis::speaking() const
93 {
94     // If we have a current speech utterance, then that means we're assumed to be in a speaking state.
95     // This state is independent of whether the utterance happens to be paused.
96     return m_currentSpeechUtterance;
97 }
98
99 bool SpeechSynthesis::pending() const
100 {
101     // This is true if there are any utterances that have not started.
102     // That means there will be more than one in the queue.
103     return m_utteranceQueue.size() > 1;
104 }
105
106 bool SpeechSynthesis::paused() const
107 {
108     return m_isPaused;
109 }
110
111 void SpeechSynthesis::startSpeakingImmediately(SpeechSynthesisUtterance& utterance)
112 {
113     utterance.setStartTime(MonotonicTime::now());
114     m_currentSpeechUtterance = &utterance;
115     m_isPaused = false;
116
117     // Zero lengthed strings should immediately notify that the event is complete.
118     if (utterance.text().isEmpty()) {
119         handleSpeakingCompleted(utterance, false);
120         return;
121     }
122
123     if (m_speechSynthesisClient)
124         m_speechSynthesisClient->speak(utterance.platformUtterance());
125     else
126         ensurePlatformSpeechSynthesizer().speak(utterance.platformUtterance());
127 }
128
129 void SpeechSynthesis::speak(SpeechSynthesisUtterance& utterance)
130 {
131     // Like Audio, we should require that the user interact to start a speech synthesis session.
132 #if PLATFORM(IOS_FAMILY)
133     if (UserGestureIndicator::processingUserGesture())
134         removeBehaviorRestriction(RequireUserGestureForSpeechStartRestriction);
135     else if (userGestureRequiredForSpeechStart())
136         return;
137 #endif
138
139     m_utteranceQueue.append(utterance);
140
141     // If the queue was empty, speak this immediately and add it to the queue.
142     if (m_utteranceQueue.size() == 1)
143         startSpeakingImmediately(m_utteranceQueue.first());
144 }
145
146 void SpeechSynthesis::cancel()
147 {
148     // Remove all the items from the utterance queue.
149     // Hold on to the current utterance so the platform synthesizer can have a chance to clean up.
150     RefPtr<SpeechSynthesisUtterance> current = m_currentSpeechUtterance;
151     m_utteranceQueue.clear();
152     if (m_speechSynthesisClient)
153         m_speechSynthesisClient->cancel();
154     else if (m_platformSpeechSynthesizer) {
155         m_platformSpeechSynthesizer->cancel();
156         // The platform should have called back immediately and cleared the current utterance.
157         ASSERT(!m_currentSpeechUtterance);
158     }
159     current = nullptr;
160 }
161
162 void SpeechSynthesis::pause()
163 {
164     if (!m_isPaused) {
165         if (m_speechSynthesisClient)
166             m_speechSynthesisClient->pause();
167         else if (m_platformSpeechSynthesizer)
168             m_platformSpeechSynthesizer->pause();
169     }
170 }
171
172 void SpeechSynthesis::resume()
173 {
174     if (m_currentSpeechUtterance) {
175         if (m_speechSynthesisClient)
176             m_speechSynthesisClient->resume();
177         else if (m_platformSpeechSynthesizer)
178             m_platformSpeechSynthesizer->resume();
179     }
180 }
181
182 void SpeechSynthesis::fireEvent(const AtomString& type, SpeechSynthesisUtterance& utterance, unsigned long charIndex, const String& name)
183 {
184     utterance.dispatchEvent(SpeechSynthesisEvent::create(type, charIndex, (MonotonicTime::now() - utterance.startTime()).seconds(), name));
185 }
186
187 void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance& utterance, bool errorOccurred)
188 {
189     ASSERT(m_currentSpeechUtterance);
190     Ref<SpeechSynthesisUtterance> protect(utterance);
191
192     m_currentSpeechUtterance = nullptr;
193
194     fireEvent(errorOccurred ? eventNames().errorEvent : eventNames().endEvent, utterance, 0, String());
195
196     if (m_utteranceQueue.size()) {
197         Ref<SpeechSynthesisUtterance> firstUtterance = m_utteranceQueue.takeFirst();
198         ASSERT(&utterance == firstUtterance.ptr());
199
200         // Start the next job if there is one pending.
201         if (!m_utteranceQueue.isEmpty())
202             startSpeakingImmediately(m_utteranceQueue.first());
203     }
204 }
205
206 void SpeechSynthesis::boundaryEventOccurred(PlatformSpeechSynthesisUtterance& utterance, SpeechBoundary boundary, unsigned charIndex)
207 {
208     static NeverDestroyed<const String> wordBoundaryString(MAKE_STATIC_STRING_IMPL("word"));
209     static NeverDestroyed<const String> sentenceBoundaryString(MAKE_STATIC_STRING_IMPL("sentence"));
210
211     ASSERT(utterance.client());
212
213     switch (boundary) {
214     case SpeechBoundary::SpeechWordBoundary:
215         fireEvent(eventNames().boundaryEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), charIndex, wordBoundaryString);
216         break;
217     case SpeechBoundary::SpeechSentenceBoundary:
218         fireEvent(eventNames().boundaryEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), charIndex, sentenceBoundaryString);
219         break;
220     default:
221         ASSERT_NOT_REACHED();
222     }
223 }
224
225 void SpeechSynthesis::didStartSpeaking()
226 {
227     if (!m_currentSpeechUtterance)
228         return;
229     didStartSpeaking(*m_currentSpeechUtterance->platformUtterance());
230 }
231
232 void SpeechSynthesis::didFinishSpeaking()
233 {
234     if (!m_currentSpeechUtterance)
235         return;
236     didFinishSpeaking(*m_currentSpeechUtterance->platformUtterance());
237 }
238
239 void SpeechSynthesis::didPauseSpeaking()
240 {
241     if (!m_currentSpeechUtterance)
242         return;
243     didPauseSpeaking(*m_currentSpeechUtterance->platformUtterance());
244 }
245
246 void SpeechSynthesis::didResumeSpeaking()
247 {
248     if (!m_currentSpeechUtterance)
249         return;
250     didResumeSpeaking(*m_currentSpeechUtterance->platformUtterance());
251 }
252
253 void SpeechSynthesis::speakingErrorOccurred()
254 {
255     if (!m_currentSpeechUtterance)
256         return;
257     speakingErrorOccurred(*m_currentSpeechUtterance->platformUtterance());
258 }
259
260 void SpeechSynthesis::boundaryEventOccurred(bool wordBoundary, unsigned charIndex)
261 {
262     if (!m_currentSpeechUtterance)
263         return;
264     boundaryEventOccurred(*m_currentSpeechUtterance->platformUtterance(), wordBoundary ? SpeechBoundary::SpeechWordBoundary : SpeechBoundary::SpeechSentenceBoundary, charIndex);
265 }
266
267 void SpeechSynthesis::voicesChanged()
268 {
269     voicesDidChange();
270 }
271
272 void SpeechSynthesis::didStartSpeaking(PlatformSpeechSynthesisUtterance& utterance)
273 {
274     if (utterance.client())
275         fireEvent(eventNames().startEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), 0, String());
276 }
277
278 void SpeechSynthesis::didPauseSpeaking(PlatformSpeechSynthesisUtterance& utterance)
279 {
280     m_isPaused = true;
281     if (utterance.client())
282         fireEvent(eventNames().pauseEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), 0, String());
283 }
284
285 void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance& utterance)
286 {
287     m_isPaused = false;
288     if (utterance.client())
289         fireEvent(eventNames().resumeEvent, static_cast<SpeechSynthesisUtterance&>(*utterance.client()), 0, String());
290 }
291
292 void SpeechSynthesis::didFinishSpeaking(PlatformSpeechSynthesisUtterance& utterance)
293 {
294     if (utterance.client())
295         handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), false);
296 }
297
298 void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance& utterance)
299 {
300     if (utterance.client())
301         handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), true);
302 }
303
304 } // namespace WebCore
305
306 #endif // ENABLE(SPEECH_SYNTHESIS)