nsSpeechTask.h (3308B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_nsSpeechTask_h 8 #define mozilla_dom_nsSpeechTask_h 9 10 #include "AudioChannelAgent.h" 11 #include "SpeechSynthesisUtterance.h" 12 #include "nsISpeechService.h" 13 #include "nsWeakReference.h" 14 15 namespace mozilla { 16 17 class SharedBuffer; 18 19 namespace dom { 20 21 class SpeechSynthesisUtterance; 22 class SpeechSynthesis; 23 24 class nsSpeechTask : public nsISpeechTask, 25 public nsIAudioChannelAgentCallback, 26 public nsSupportsWeakReference { 27 public: 28 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 29 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsSpeechTask, nsISpeechTask) 30 31 NS_DECL_NSISPEECHTASK 32 NS_DECL_NSIAUDIOCHANNELAGENTCALLBACK 33 34 explicit nsSpeechTask(SpeechSynthesisUtterance* aUtterance, 35 bool aShouldResistFingerprinting); 36 nsSpeechTask(float aVolume, const nsAString& aText, 37 bool aShouldResistFingerprinting); 38 39 virtual void Pause(); 40 41 virtual void Resume(); 42 43 virtual void Cancel(); 44 45 virtual void ForceEnd(); 46 47 void SetSpeechSynthesis(SpeechSynthesis* aSpeechSynthesis); 48 49 void Init(); 50 51 void SetChosenVoiceURI(const nsAString& aUri); 52 53 virtual void SetAudioOutputVolume(float aVolume); 54 55 void ForceError(float aElapsedTime, uint32_t aCharIndex); 56 57 bool IsPreCanceled() { return mPreCanceled; }; 58 59 bool IsPrePaused() { return mPrePaused; } 60 61 bool ShouldResistFingerprinting() { return mShouldResistFingerprinting; } 62 63 enum { STATE_PENDING, STATE_SPEAKING, STATE_ENDED }; 64 65 uint32_t GetState() const { return mState; } 66 67 bool IsSpeaking() const { return mState == STATE_SPEAKING; } 68 69 bool IsPending() const { return mState == STATE_PENDING; } 70 71 protected: 72 virtual ~nsSpeechTask(); 73 74 nsresult DispatchStartImpl(); 75 76 virtual nsresult DispatchStartImpl(const nsAString& aUri); 77 78 virtual nsresult DispatchEndImpl(float aElapsedTime, uint32_t aCharIndex); 79 80 virtual nsresult DispatchPauseImpl(float aElapsedTime, uint32_t aCharIndex); 81 82 virtual nsresult DispatchResumeImpl(float aElapsedTime, uint32_t aCharIndex); 83 84 virtual nsresult DispatchErrorImpl(float aElapsedTime, uint32_t aCharIndex); 85 86 virtual nsresult DispatchBoundaryImpl(const nsAString& aName, 87 float aElapsedTime, uint32_t aCharIndex, 88 uint32_t aCharLength, uint8_t argc); 89 90 virtual nsresult DispatchMarkImpl(const nsAString& aName, float aElapsedTime, 91 uint32_t aCharIndex); 92 93 RefPtr<SpeechSynthesisUtterance> mUtterance; 94 95 float mVolume; 96 97 nsString mText; 98 99 bool mInited; 100 101 bool mPrePaused; 102 103 bool mPreCanceled; 104 105 private: 106 void End(); 107 108 void CreateAudioChannelAgent(); 109 110 void DestroyAudioChannelAgent(); 111 112 nsCOMPtr<nsISpeechTaskCallback> mCallback; 113 114 RefPtr<mozilla::dom::AudioChannelAgent> mAudioChannelAgent; 115 116 RefPtr<SpeechSynthesis> mSpeechSynthesis; 117 118 nsString mChosenVoiceURI; 119 120 bool mShouldResistFingerprinting; 121 122 uint32_t mState; 123 }; 124 125 } // namespace dom 126 } // namespace mozilla 127 128 #endif