Chromium Code Reviews| Index: chrome/browser/speech/tts_chromeos.cc |
| diff --git a/chrome/browser/speech/tts_chromeos.cc b/chrome/browser/speech/tts_chromeos.cc |
| index 7d218e826bf9c086d65e241e5536dd997b740218..795444ff5df3a72e702eb36fab807e37e0669f38 100644 |
| --- a/chrome/browser/speech/tts_chromeos.cc |
| +++ b/chrome/browser/speech/tts_chromeos.cc |
| @@ -4,16 +4,28 @@ |
| #include "base/macros.h" |
| #include "chrome/browser/speech/tts_platform.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "components/arc/common/tts.mojom.h" |
| +#include "mojo/public/cpp/bindings/binding.h" |
| -// Chrome OS doesn't have native TTS, instead it includes a built-in |
| -// component extension that provides speech synthesis. This class includes |
| -// an implementation of LoadBuiltInTtsExtension and dummy implementations of |
| -// everything else. |
| +namespace { |
| -class TtsPlatformImplChromeOs : public TtsPlatformImpl { |
| +// Helper returning an Arc tts instance. |
| +arc::mojom::TtsInstance* GetArcTts() { |
| + return arc::ArcBridgeService::Get() |
| + ? arc::ArcBridgeService::Get()->tts()->instance() |
| + : nullptr; |
| +} |
| + |
| +} // namespace |
| + |
| +// This class includes extension-based tts through LoadBuiltInTtsExtension and |
| +// native tts through ARC. |
| +class TtsPlatformImplChromeOs : public TtsPlatformImpl, |
| + public arc::mojom::TtsHost { |
| public: |
| // TtsPlatformImpl overrides: |
| - bool PlatformImplAvailable() override { return false; } |
| + bool PlatformImplAvailable() override { return GetArcTts() != nullptr; } |
| bool LoadBuiltInTtsExtension( |
| content::BrowserContext* browser_context) override { |
| @@ -29,26 +41,92 @@ class TtsPlatformImplChromeOs : public TtsPlatformImpl { |
| const std::string& lang, |
| const VoiceData& voice, |
| const UtteranceContinuousParameters& params) override { |
| - return false; |
| + InstallTtsHostBindingsIfNeeded(); |
| + arc::mojom::TtsInstance* tts = GetArcTts(); |
| + if (!tts) |
| + return false; |
| + |
| + arc::mojom::TtsUtterancePtr arc_utterance = arc::mojom::TtsUtterance::New(); |
| + arc_utterance->utteranceId = utterance_id; |
| + arc_utterance->text = utterance; |
| + arc_utterance->rate = params.rate; |
| + arc_utterance->pitch = params.pitch; |
| + tts->Speak(std::move(arc_utterance)); |
| + return true; |
| } |
| - bool StopSpeaking() override { return false; } |
| + bool StopSpeaking() override { |
| + arc::mojom::TtsInstance* tts = GetArcTts(); |
| + if (!tts) |
| + return false; |
| - void Pause() override {} |
| + tts->Stop(); |
| + return true; |
| + } |
| - void Resume() override {} |
| + void GetVoices(std::vector<VoiceData>* out_voices) override { |
| + out_voices->push_back(VoiceData()); |
| + VoiceData& voice = out_voices->back(); |
| + voice.native = true; |
| + voice.name = "Android"; |
| + voice.events.insert(TTS_EVENT_START); |
| + voice.events.insert(TTS_EVENT_END); |
| + } |
| + // Unimplemented. |
| + void Pause() override {} |
| + void Resume() override {} |
| bool IsSpeaking() override { return false; } |
| - void GetVoices(std::vector<VoiceData>* out_voices) override {} |
| + // arc::mojom::TtsHost overrides: |
| + void OnTtsEvent(uint32_t id, |
| + arc::mojom::TtsEventType event_type, |
| + uint32_t char_index, |
| + const mojo::String& error_msg) override { |
| + if (!TtsController::GetInstance()) |
| + return; |
| + |
| + TtsEventType chrome_event_type; |
| + switch (event_type) { |
| + case arc::mojom::TtsEventType::START: |
| + chrome_event_type = TTS_EVENT_START; |
| + break; |
| + case arc::mojom::TtsEventType::END: |
| + chrome_event_type = TTS_EVENT_END; |
| + break; |
| + case arc::mojom::TtsEventType::INTERRUPTED: |
| + chrome_event_type = TTS_EVENT_INTERRUPTED; |
| + break; |
| + case arc::mojom::TtsEventType::ERROR: |
| + chrome_event_type = TTS_EVENT_ERROR; |
| + break; |
| + } |
| + TtsController::GetInstance()->OnTtsEvent(id, chrome_event_type, char_index, |
| + error_msg); |
| + } |
| // Get the single instance of this class. |
| static TtsPlatformImplChromeOs* GetInstance(); |
| private: |
| - TtsPlatformImplChromeOs() {} |
| + TtsPlatformImplChromeOs() : last_tts_instance_(nullptr), binding_(this) {} |
| ~TtsPlatformImplChromeOs() override {} |
| + // Binds this object as a host tts to the latest tts instance. This causes the |
| + // instance side to receive an init call. |
| + void InstallTtsHostBindingsIfNeeded() { |
| + arc::mojom::TtsInstance* tts_instance = GetArcTts(); |
| + if (tts_instance && tts_instance != last_tts_instance_) { |
|
hidehiko
2016/07/29 09:25:53
This does not work.
E.g.; tts_instance is destruct
David Tseng
2016/08/01 20:21:41
This was all fallout of trying to keep the service
|
| + tts_instance->Init(binding_.CreateInterfacePtrAndBind()); |
| + last_tts_instance_ = tts_instance; |
| + } |
| + } |
| + |
| + // Weak. Never used directly. |
| + arc::mojom::TtsInstance* last_tts_instance_; |
| + |
| + mojo::Binding<arc::mojom::TtsHost> binding_; |
| + |
| friend struct base::DefaultSingletonTraits<TtsPlatformImplChromeOs>; |
| DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplChromeOs); |