1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_SPEECH_TTS_CONTROLLER_IMPL_H_ 6 #define CHROME_BROWSER_SPEECH_TTS_CONTROLLER_IMPL_H_ 7 8 #include <queue> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/singleton.h" 15 #include "base/memory/weak_ptr.h" 16 #include "chrome/browser/speech/tts_controller.h" 17 #include "url/gurl.h" 18 19 namespace content { 20 class BrowserContext; 21 } 22 23 // Singleton class that manages text-to-speech for the TTS and TTS engine 24 // extension APIs, maintaining a queue of pending utterances and keeping 25 // track of all state. 26 class TtsControllerImpl : public TtsController { 27 public: 28 // Get the single instance of this class. 29 static TtsControllerImpl* GetInstance(); 30 31 // TtsController methods 32 virtual bool IsSpeaking() OVERRIDE; 33 virtual void SpeakOrEnqueue(Utterance* utterance) OVERRIDE; 34 virtual void Stop() OVERRIDE; 35 virtual void Pause() OVERRIDE; 36 virtual void Resume() OVERRIDE; 37 virtual void OnTtsEvent(int utterance_id, 38 TtsEventType event_type, 39 int char_index, 40 const std::string& error_message) OVERRIDE; 41 virtual void GetVoices(content::BrowserContext* browser_context, 42 std::vector<VoiceData>* out_voices) OVERRIDE; 43 virtual void VoicesChanged() OVERRIDE; 44 virtual void AddVoicesChangedDelegate( 45 VoicesChangedDelegate* delegate) OVERRIDE; 46 virtual void RemoveVoicesChangedDelegate( 47 VoicesChangedDelegate* delegate) OVERRIDE; 48 virtual void SetTtsEngineDelegate(TtsEngineDelegate* delegate) OVERRIDE; 49 virtual TtsEngineDelegate* GetTtsEngineDelegate() OVERRIDE; 50 virtual void SetPlatformImpl(TtsPlatformImpl* platform_impl) OVERRIDE; 51 virtual int QueueSize() OVERRIDE; 52 53 protected: 54 TtsControllerImpl(); 55 virtual ~TtsControllerImpl(); 56 57 private: 58 // Get the platform TTS implementation (or injected mock). 59 TtsPlatformImpl* GetPlatformImpl(); 60 61 // Start speaking the given utterance. Will either take ownership of 62 // |utterance| or delete it if there's an error. Returns true on success. 63 void SpeakNow(Utterance* utterance); 64 65 // Clear the utterance queue. If send_events is true, will send 66 // TTS_EVENT_CANCELLED events on each one. 67 void ClearUtteranceQueue(bool send_events); 68 69 // Finalize and delete the current utterance. 70 void FinishCurrentUtterance(); 71 72 // Start speaking the next utterance in the queue. 73 void SpeakNextUtterance(); 74 75 // Given an utterance and a vector of voices, return the 76 // index of the voice that best matches the utterance. 77 int GetMatchingVoice(const Utterance* utterance, 78 std::vector<VoiceData>& voices); 79 80 friend struct DefaultSingletonTraits<TtsControllerImpl>; 81 82 // The current utterance being spoken. 83 Utterance* current_utterance_; 84 85 // Whether the queue is paused or not. 86 bool paused_; 87 88 // A queue of utterances to speak after the current one finishes. 89 std::queue<Utterance*> utterance_queue_; 90 91 // A set of delegates that want to be notified when the voices change. 92 std::set<VoicesChangedDelegate*> voices_changed_delegates_; 93 94 // A pointer to the platform implementation of text-to-speech, for 95 // dependency injection. 96 TtsPlatformImpl* platform_impl_; 97 98 // The delegate that processes TTS requests with user-installed extensions. 99 TtsEngineDelegate* tts_engine_delegate_; 100 101 DISALLOW_COPY_AND_ASSIGN(TtsControllerImpl); 102 }; 103 104 #endif // CHROME_BROWSER_SPEECH_TTS_CONTROLLER_IMPL_H_ 105