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.
5 #ifndef CHROME_BROWSER_SPEECH_TTS_CONTROLLER_IMPL_H_
6 #define CHROME_BROWSER_SPEECH_TTS_CONTROLLER_IMPL_H_
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/singleton.h"
15 #include "chrome/browser/speech/tts_controller.h"
22 // Singleton class that manages text-to-speech for the TTS and TTS engine
23 // extension APIs, maintaining a queue of pending utterances and keeping
24 // track of all state.
25 class TtsControllerImpl
: public TtsController
{
27 // Get the single instance of this class.
28 static TtsControllerImpl
* GetInstance();
30 // TtsController methods
31 bool IsSpeaking() override
;
32 void SpeakOrEnqueue(Utterance
* utterance
) override
;
34 void Pause() override
;
35 void Resume() override
;
36 void OnTtsEvent(int utterance_id
,
37 TtsEventType event_type
,
39 const std::string
& error_message
) override
;
40 void GetVoices(content::BrowserContext
* browser_context
,
41 std::vector
<VoiceData
>* out_voices
) override
;
42 void VoicesChanged() override
;
43 void AddVoicesChangedDelegate(VoicesChangedDelegate
* delegate
) override
;
44 void RemoveVoicesChangedDelegate(VoicesChangedDelegate
* delegate
) override
;
45 void RemoveUtteranceEventDelegate(UtteranceEventDelegate
* delegate
) override
;
46 void SetTtsEngineDelegate(TtsEngineDelegate
* delegate
) override
;
47 TtsEngineDelegate
* GetTtsEngineDelegate() override
;
48 void SetPlatformImpl(TtsPlatformImpl
* platform_impl
) override
;
49 int QueueSize() override
;
53 ~TtsControllerImpl() override
;
56 // Get the platform TTS implementation (or injected mock).
57 TtsPlatformImpl
* GetPlatformImpl();
59 // Start speaking the given utterance. Will either take ownership of
60 // |utterance| or delete it if there's an error. Returns true on success.
61 void SpeakNow(Utterance
* utterance
);
63 // Clear the utterance queue. If send_events is true, will send
64 // TTS_EVENT_CANCELLED events on each one.
65 void ClearUtteranceQueue(bool send_events
);
67 // Finalize and delete the current utterance.
68 void FinishCurrentUtterance();
70 // Start speaking the next utterance in the queue.
71 void SpeakNextUtterance();
73 // Given an utterance and a vector of voices, return the
74 // index of the voice that best matches the utterance.
75 int GetMatchingVoice(const Utterance
* utterance
,
76 std::vector
<VoiceData
>& voices
);
78 friend struct DefaultSingletonTraits
<TtsControllerImpl
>;
80 // The current utterance being spoken.
81 Utterance
* current_utterance_
;
83 // Whether the queue is paused or not.
86 // A queue of utterances to speak after the current one finishes.
87 std::queue
<Utterance
*> utterance_queue_
;
89 // A set of delegates that want to be notified when the voices change.
90 std::set
<VoicesChangedDelegate
*> voices_changed_delegates_
;
92 // A pointer to the platform implementation of text-to-speech, for
93 // dependency injection.
94 TtsPlatformImpl
* platform_impl_
;
96 // The delegate that processes TTS requests with user-installed extensions.
97 TtsEngineDelegate
* tts_engine_delegate_
;
99 DISALLOW_COPY_AND_ASSIGN(TtsControllerImpl
);
102 #endif // CHROME_BROWSER_SPEECH_TTS_CONTROLLER_IMPL_H_