Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "chrome/browser/speech/tts_platform.h" | 6 #include "chrome/browser/speech/tts_platform.h" |
| 7 #include "components/arc/arc_bridge_service.h" | |
| 8 #include "components/arc/common/tts.mojom.h" | |
| 9 #include "mojo/public/cpp/bindings/binding.h" | |
| 7 | 10 |
| 8 // Chrome OS doesn't have native TTS, instead it includes a built-in | 11 namespace { |
| 9 // component extension that provides speech synthesis. This class includes | |
| 10 // an implementation of LoadBuiltInTtsExtension and dummy implementations of | |
| 11 // everything else. | |
| 12 | 12 |
| 13 class TtsPlatformImplChromeOs : public TtsPlatformImpl { | 13 // Helper returning an Arc tts instance. |
| 14 arc::mojom::TtsInstance* GetArcTts() { | |
| 15 return arc::ArcBridgeService::Get() | |
| 16 ? arc::ArcBridgeService::Get()->tts()->instance() | |
| 17 : nullptr; | |
| 18 } | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 22 // This class includes extension-based tts through LoadBuiltInTtsExtension and | |
| 23 // native tts through ARC. | |
| 24 class TtsPlatformImplChromeOs : public TtsPlatformImpl, | |
| 25 public arc::mojom::TtsHost { | |
| 14 public: | 26 public: |
| 15 // TtsPlatformImpl overrides: | 27 // TtsPlatformImpl overrides: |
| 16 bool PlatformImplAvailable() override { return false; } | 28 bool PlatformImplAvailable() override { return GetArcTts() != nullptr; } |
| 17 | 29 |
| 18 bool LoadBuiltInTtsExtension( | 30 bool LoadBuiltInTtsExtension( |
| 19 content::BrowserContext* browser_context) override { | 31 content::BrowserContext* browser_context) override { |
| 20 TtsEngineDelegate* tts_engine_delegate = | 32 TtsEngineDelegate* tts_engine_delegate = |
| 21 TtsController::GetInstance()->GetTtsEngineDelegate(); | 33 TtsController::GetInstance()->GetTtsEngineDelegate(); |
| 22 if (tts_engine_delegate) | 34 if (tts_engine_delegate) |
| 23 return tts_engine_delegate->LoadBuiltInTtsExtension(browser_context); | 35 return tts_engine_delegate->LoadBuiltInTtsExtension(browser_context); |
| 24 return false; | 36 return false; |
| 25 } | 37 } |
| 26 | 38 |
| 27 bool Speak(int utterance_id, | 39 bool Speak(int utterance_id, |
| 28 const std::string& utterance, | 40 const std::string& utterance, |
| 29 const std::string& lang, | 41 const std::string& lang, |
| 30 const VoiceData& voice, | 42 const VoiceData& voice, |
| 31 const UtteranceContinuousParameters& params) override { | 43 const UtteranceContinuousParameters& params) override { |
| 32 return false; | 44 InstallTtsHostBindingsIfNeeded(); |
| 45 arc::mojom::TtsInstance* tts = GetArcTts(); | |
| 46 if (!tts) | |
| 47 return false; | |
| 48 | |
| 49 arc::mojom::TtsUtterancePtr arc_utterance = arc::mojom::TtsUtterance::New(); | |
| 50 arc_utterance->utteranceId = utterance_id; | |
| 51 arc_utterance->text = utterance; | |
| 52 arc_utterance->rate = params.rate; | |
| 53 arc_utterance->pitch = params.pitch; | |
| 54 tts->Speak(std::move(arc_utterance)); | |
| 55 return true; | |
| 33 } | 56 } |
| 34 | 57 |
| 35 bool StopSpeaking() override { return false; } | 58 bool StopSpeaking() override { |
| 59 arc::mojom::TtsInstance* tts = GetArcTts(); | |
| 60 if (!tts) | |
| 61 return false; | |
| 36 | 62 |
| 63 tts->Stop(); | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 void GetVoices(std::vector<VoiceData>* out_voices) override { | |
| 68 out_voices->push_back(VoiceData()); | |
| 69 VoiceData& voice = out_voices->back(); | |
| 70 voice.native = true; | |
| 71 voice.name = "Android"; | |
| 72 voice.events.insert(TTS_EVENT_START); | |
| 73 voice.events.insert(TTS_EVENT_END); | |
| 74 } | |
| 75 | |
| 76 // Unimplemented. | |
| 37 void Pause() override {} | 77 void Pause() override {} |
| 38 | |
| 39 void Resume() override {} | 78 void Resume() override {} |
| 40 | |
| 41 bool IsSpeaking() override { return false; } | 79 bool IsSpeaking() override { return false; } |
| 42 | 80 |
| 43 void GetVoices(std::vector<VoiceData>* out_voices) override {} | 81 // arc::mojom::TtsHost overrides: |
| 82 void OnTtsEvent(uint32_t id, | |
| 83 arc::mojom::TtsEventType event_type, | |
| 84 uint32_t char_index, | |
| 85 const mojo::String& error_msg) override { | |
| 86 if (!TtsController::GetInstance()) | |
| 87 return; | |
| 88 | |
| 89 TtsEventType chrome_event_type; | |
| 90 switch (event_type) { | |
| 91 case arc::mojom::TtsEventType::START: | |
| 92 chrome_event_type = TTS_EVENT_START; | |
| 93 break; | |
| 94 case arc::mojom::TtsEventType::END: | |
| 95 chrome_event_type = TTS_EVENT_END; | |
| 96 break; | |
| 97 case arc::mojom::TtsEventType::INTERRUPTED: | |
| 98 chrome_event_type = TTS_EVENT_INTERRUPTED; | |
| 99 break; | |
| 100 case arc::mojom::TtsEventType::ERROR: | |
| 101 chrome_event_type = TTS_EVENT_ERROR; | |
| 102 break; | |
| 103 } | |
| 104 TtsController::GetInstance()->OnTtsEvent(id, chrome_event_type, char_index, | |
| 105 error_msg); | |
| 106 } | |
| 44 | 107 |
| 45 // Get the single instance of this class. | 108 // Get the single instance of this class. |
| 46 static TtsPlatformImplChromeOs* GetInstance(); | 109 static TtsPlatformImplChromeOs* GetInstance(); |
| 47 | 110 |
| 48 private: | 111 private: |
| 49 TtsPlatformImplChromeOs() {} | 112 TtsPlatformImplChromeOs() : last_tts_instance_(nullptr), binding_(this) {} |
| 50 ~TtsPlatformImplChromeOs() override {} | 113 ~TtsPlatformImplChromeOs() override {} |
| 51 | 114 |
| 115 // Binds this object as a host tts to the latest tts instance. This causes the | |
| 116 // instance side to receive an init call. | |
| 117 void InstallTtsHostBindingsIfNeeded() { | |
| 118 arc::mojom::TtsInstance* tts_instance = GetArcTts(); | |
| 119 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
| |
| 120 tts_instance->Init(binding_.CreateInterfacePtrAndBind()); | |
| 121 last_tts_instance_ = tts_instance; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 // Weak. Never used directly. | |
| 126 arc::mojom::TtsInstance* last_tts_instance_; | |
| 127 | |
| 128 mojo::Binding<arc::mojom::TtsHost> binding_; | |
| 129 | |
| 52 friend struct base::DefaultSingletonTraits<TtsPlatformImplChromeOs>; | 130 friend struct base::DefaultSingletonTraits<TtsPlatformImplChromeOs>; |
| 53 | 131 |
| 54 DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplChromeOs); | 132 DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplChromeOs); |
| 55 }; | 133 }; |
| 56 | 134 |
| 57 // static | 135 // static |
| 58 TtsPlatformImpl* TtsPlatformImpl::GetInstance() { | 136 TtsPlatformImpl* TtsPlatformImpl::GetInstance() { |
| 59 return TtsPlatformImplChromeOs::GetInstance(); | 137 return TtsPlatformImplChromeOs::GetInstance(); |
| 60 } | 138 } |
| 61 | 139 |
| 62 // static | 140 // static |
| 63 TtsPlatformImplChromeOs* | 141 TtsPlatformImplChromeOs* |
| 64 TtsPlatformImplChromeOs::GetInstance() { | 142 TtsPlatformImplChromeOs::GetInstance() { |
| 65 return base::Singleton<TtsPlatformImplChromeOs>::get(); | 143 return base::Singleton<TtsPlatformImplChromeOs>::get(); |
| 66 } | 144 } |
| OLD | NEW |