| 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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(SPEECH_SYNTHESIS) |
| 29 | |
| 30 | #include "PlatformSpeechSynthesisUtterance.h" |
| 31 | #include "PlatformSpeechSynthesizer.h" |
| 32 | #include "SpeechSynthesisClient.h" |
| 33 | #include "SpeechSynthesisUtterance.h" |
| 34 | #include "SpeechSynthesisVoice.h" |
| 35 | #include <wtf/Deque.h> |
| 36 | #include <wtf/RefCounted.h> |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | class PlatformSpeechSynthesizerClient; |
| 41 | class SpeechSynthesisVoice; |
| 42 | |
| 43 | class SpeechSynthesis : public PlatformSpeechSynthesizerClient, public SpeechSynthesisClientObserver, public RefCounted<SpeechSynthesis> { |
| 44 | public: |
| 45 | static Ref<SpeechSynthesis> create(WeakPtr<SpeechSynthesisClient>); |
| 46 | |
| 47 | bool pending() const; |
| 48 | bool speaking() const; |
| 49 | bool paused() const; |
| 50 | |
| 51 | void speak(SpeechSynthesisUtterance&); |
| 52 | void cancel(); |
| 53 | void pause(); |
| 54 | void resume(); |
| 55 | |
| 56 | const Vector<Ref<SpeechSynthesisVoice>>& getVoices(); |
| 57 | |
| 58 | // Used in testing to use a mock platform synthesizer |
| 59 | WEBCORE_EXPORT void setPlatformSynthesizer(std::unique_ptr<PlatformSpeechSynthesizer>); |
| 60 | |
| 61 | private: |
| 62 | SpeechSynthesis(WeakPtr<SpeechSynthesisClient>); |
| 63 | |
| 64 | // PlatformSpeechSynthesizerClient override methods. |
| 65 | void voicesDidChange() override; |
| 66 | void didStartSpeaking(PlatformSpeechSynthesisUtterance&) override; |
| 67 | void didPauseSpeaking(PlatformSpeechSynthesisUtterance&) override; |
| 68 | void didResumeSpeaking(PlatformSpeechSynthesisUtterance&) override; |
| 69 | void didFinishSpeaking(PlatformSpeechSynthesisUtterance&) override; |
| 70 | void speakingErrorOccurred(PlatformSpeechSynthesisUtterance&) override; |
| 71 | void boundaryEventOccurred(PlatformSpeechSynthesisUtterance&, SpeechBoundary, unsigned charIndex) override; |
| 72 | |
| 73 | // SpeechSynthesisClient override methods |
| 74 | void didStartSpeaking() override; |
| 75 | void didFinishSpeaking() override; |
| 76 | void didPauseSpeaking() override; |
| 77 | void didResumeSpeaking() override; |
| 78 | void speakingErrorOccurred() override; |
| 79 | void boundaryEventOccurred(bool wordBoundary, unsigned charIndex) override; |
| 80 | void voicesChanged() override; |
| 81 | |
| 82 | void startSpeakingImmediately(SpeechSynthesisUtterance&); |
| 83 | void handleSpeakingCompleted(SpeechSynthesisUtterance&, bool errorOccurred); |
| 84 | void fireEvent(const AtomicString& type, SpeechSynthesisUtterance&, unsigned long charIndex, const String& name); |
| 85 | |
| 86 | #if PLATFORM(IOS_FAMILY) |
| 87 | // Restrictions to change default behaviors. |
| 88 | enum BehaviorRestrictionFlags { |
| 89 | NoRestrictions = 0, |
| 90 | RequireUserGestureForSpeechStartRestriction = 1 << 0, |
| 91 | }; |
| 92 | typedef unsigned BehaviorRestrictions; |
| 93 | |
| 94 | bool userGestureRequiredForSpeechStart() const { return m_restrictions & RequireUserGestureForSpeechStartRestriction; } |
| 95 | void removeBehaviorRestriction(BehaviorRestrictions restriction) { m_restrictions &= ~restriction; } |
| 96 | #endif |
| 97 | PlatformSpeechSynthesizer& ensurePlatformSpeechSynthesizer(); |
| 98 | |
| 99 | std::unique_ptr<PlatformSpeechSynthesizer> m_platformSpeechSynthesizer; |
| 100 | Vector<Ref<SpeechSynthesisVoice>> m_voiceList; |
| 101 | SpeechSynthesisUtterance* m_currentSpeechUtterance; |
| 102 | Deque<Ref<SpeechSynthesisUtterance>> m_utteranceQueue; |
| 103 | bool m_isPaused; |
| 104 | #if PLATFORM(IOS_FAMILY) |
| 105 | BehaviorRestrictions m_restrictions; |
| 106 | #endif |
| 107 | WeakPtr<SpeechSynthesisClient> m_speechSynthesisClient; |
| 108 | }; |
| 109 | |
| 110 | } // namespace WebCore |
| 111 | |
| 112 | #endif // ENABLE(SPEECH_SYNTHESIS) |
| 113 | |