Add WTF::move()
[WebKit-https.git] / Source / WebCore / Modules / speech / SpeechSynthesis.cpp
1 /*
2  * Copyright (C) 2013 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 "PlatformSpeechSynthesisVoice.h"
32 #include "PlatformSpeechSynthesizer.h"
33 #include "ScriptController.h"
34 #include "SpeechSynthesisEvent.h"
35 #include "SpeechSynthesisUtterance.h"
36 #include <wtf/CurrentTime.h>
37
38 namespace WebCore {
39     
40 PassRefPtr<SpeechSynthesis> SpeechSynthesis::create()
41 {
42     return adoptRef(new SpeechSynthesis);
43 }
44     
45 SpeechSynthesis::SpeechSynthesis()
46     : m_currentSpeechUtterance(0)
47     , m_isPaused(false)
48 #if PLATFORM(IOS)
49     , m_restrictions(RequireUserGestureForSpeechStartRestriction)
50 #endif
51 {
52 }
53     
54 void SpeechSynthesis::setPlatformSynthesizer(std::unique_ptr<PlatformSpeechSynthesizer> synthesizer)
55 {
56     m_platformSpeechSynthesizer = WTF::move(synthesizer);
57     m_voiceList.clear();
58     m_currentSpeechUtterance = 0;
59     m_utteranceQueue.clear();
60     m_isPaused = false;
61 }
62     
63 void SpeechSynthesis::voicesDidChange()
64 {
65     m_voiceList.clear();
66 }
67     
68 const Vector<RefPtr<SpeechSynthesisVoice>>& SpeechSynthesis::getVoices()
69 {
70     if (m_voiceList.size())
71         return m_voiceList;
72
73     if (!m_platformSpeechSynthesizer)
74         m_platformSpeechSynthesizer = std::make_unique<PlatformSpeechSynthesizer>(this);
75
76     // If the voiceList is empty, that's the cue to get the voices from the platform again.
77     const Vector<RefPtr<PlatformSpeechSynthesisVoice>>& platformVoices = m_platformSpeechSynthesizer->voiceList();
78     size_t voiceCount = platformVoices.size();
79     for (size_t k = 0; k < voiceCount; k++)
80         m_voiceList.append(SpeechSynthesisVoice::create(platformVoices[k]));
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     if (!utterance)
125         return;
126  
127     // Like Audio, we should require that the user interact to start a speech synthesis session.
128 #if PLATFORM(IOS)
129     if (ScriptController::processingUserGesture())
130         removeBehaviorRestriction(RequireUserGestureForSpeechStartRestriction);
131     else if (userGestureRequiredForSpeechStart())
132         return;
133 #endif
134     
135     m_utteranceQueue.append(utterance);
136     
137     // If the queue was empty, speak this immediately and add it to the queue.
138     if (m_utteranceQueue.size() == 1)
139         startSpeakingImmediately(utterance);
140 }
141
142 void SpeechSynthesis::cancel()
143 {
144     // Remove all the items from the utterance queue.
145     // Hold on to the current utterance so the platform synthesizer can have a chance to clean up.
146     RefPtr<SpeechSynthesisUtterance> current = m_currentSpeechUtterance;
147     m_utteranceQueue.clear();
148     if (m_platformSpeechSynthesizer)
149         m_platformSpeechSynthesizer->cancel();
150     current = 0;
151     
152     // The platform should have called back immediately and cleared the current utterance.
153     ASSERT(!m_currentSpeechUtterance);
154 }
155
156 void SpeechSynthesis::pause()
157 {
158     if (!m_isPaused && m_platformSpeechSynthesizer)
159         m_platformSpeechSynthesizer->pause();
160 }
161
162 void SpeechSynthesis::resume()
163 {
164     if (m_currentSpeechUtterance && m_platformSpeechSynthesizer)
165         m_platformSpeechSynthesizer->resume();
166 }
167
168 void SpeechSynthesis::fireEvent(const AtomicString& type, SpeechSynthesisUtterance* utterance, unsigned long charIndex, const String& name)
169 {
170     utterance->dispatchEvent(SpeechSynthesisEvent::create(type, charIndex, (monotonicallyIncreasingTime() - utterance->startTime()), name));
171 }
172     
173 void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance* utterance, bool errorOccurred)
174 {
175     ASSERT(utterance);
176     ASSERT(m_currentSpeechUtterance);
177     RefPtr<SpeechSynthesisUtterance> protect(utterance);
178     
179     m_currentSpeechUtterance = 0;
180
181     fireEvent(errorOccurred ? eventNames().errorEvent : eventNames().endEvent, utterance, 0, String());
182
183     if (m_utteranceQueue.size()) {
184         RefPtr<SpeechSynthesisUtterance> firstUtterance = m_utteranceQueue.first();
185         ASSERT(firstUtterance == utterance);
186         if (firstUtterance == utterance)
187             m_utteranceQueue.removeFirst();
188         
189         // Start the next job if there is one pending.
190         if (!m_utteranceQueue.isEmpty())
191             startSpeakingImmediately(m_utteranceQueue.first().get());
192     }
193 }
194     
195 void SpeechSynthesis::boundaryEventOccurred(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance, SpeechBoundary boundary, unsigned charIndex)
196 {
197     DEPRECATED_DEFINE_STATIC_LOCAL(const String, wordBoundaryString, (ASCIILiteral("word")));
198     DEPRECATED_DEFINE_STATIC_LOCAL(const String, sentenceBoundaryString, (ASCIILiteral("sentence")));
199
200     switch (boundary) {
201     case SpeechWordBoundary:
202         fireEvent(eventNames().boundaryEvent, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, wordBoundaryString);
203         break;
204     case SpeechSentenceBoundary:
205         fireEvent(eventNames().boundaryEvent, static_cast<SpeechSynthesisUtterance*>(utterance->client()), charIndex, sentenceBoundaryString);
206         break;
207     default:
208         ASSERT_NOT_REACHED();
209     }
210 }
211
212 void SpeechSynthesis::didStartSpeaking(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance)
213 {
214     if (utterance->client())
215         fireEvent(eventNames().startEvent, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
216 }
217     
218 void SpeechSynthesis::didPauseSpeaking(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance)
219 {
220     m_isPaused = true;
221     if (utterance->client())
222         fireEvent(eventNames().pauseEvent, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
223 }
224
225 void SpeechSynthesis::didResumeSpeaking(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance)
226 {
227     m_isPaused = false;
228     if (utterance->client())
229         fireEvent(eventNames().resumeEvent, static_cast<SpeechSynthesisUtterance*>(utterance->client()), 0, String());
230 }
231
232 void SpeechSynthesis::didFinishSpeaking(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance)
233 {
234     if (utterance->client())
235         handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), false);
236 }
237     
238 void SpeechSynthesis::speakingErrorOccurred(PassRefPtr<PlatformSpeechSynthesisUtterance> utterance)
239 {
240     if (utterance->client())
241         handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance*>(utterance->client()), true);
242 }
243
244 } // namespace WebCore
245
246 #endif // ENABLE(INPUT_SPEECH)