tts_controller_impl.h 3.4 KB

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