| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/extensions/extension_tts_api_chromeos.h" |
| 6 |
| 7 #include <list> |
| 8 |
| 5 #include "base/bind.h" | 9 #include "base/bind.h" |
| 6 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 7 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 8 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" | 15 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
| 11 #include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" | 16 #include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" |
| 12 #include "chrome/browser/extensions/extension_tts_api_controller.h" | 17 #include "chrome/browser/extensions/extension_tts_api_controller.h" |
| 13 #include "chrome/browser/extensions/extension_tts_api_platform.h" | 18 #include "chrome/browser/extensions/extension_tts_api_platform.h" |
| 14 | 19 |
| 15 using base::DoubleToString; | 20 using base::DoubleToString; |
| 16 | 21 |
| 17 namespace { | 22 namespace { |
| 18 const int kSpeechCheckDelayIntervalMs = 100; | 23 const int kSpeechCheckDelayIntervalMs = 100; |
| 19 }; | 24 }; |
| 20 | 25 |
| 21 class ExtensionTtsPlatformImplChromeOs : public ExtensionTtsPlatformImpl { | 26 // Very simple queue of utterances that can't be spoken because audio |
| 27 // isn't initialized yet. |
| 28 struct QueuedUtterance { |
| 29 int utterance_id; |
| 30 std::string utterance; |
| 31 std::string lang; |
| 32 UtteranceContinuousParameters params; |
| 33 }; |
| 34 |
| 35 class ExtensionTtsPlatformImplChromeOs |
| 36 : public ExtensionTtsPlatformImpl { |
| 22 public: | 37 public: |
| 23 virtual bool PlatformImplAvailable() { | 38 virtual bool PlatformImplAvailable() { |
| 24 return true; | 39 return true; |
| 25 } | 40 } |
| 26 | 41 |
| 27 virtual bool Speak( | 42 virtual bool Speak( |
| 28 int utterance_id, | 43 int utterance_id, |
| 29 const std::string& utterance, | 44 const std::string& utterance, |
| 30 const std::string& lang, | 45 const std::string& lang, |
| 31 const UtteranceContinuousParameters& params); | 46 const UtteranceContinuousParameters& params); |
| 32 | 47 |
| 33 virtual bool StopSpeaking(); | 48 virtual bool StopSpeaking(); |
| 34 | 49 |
| 35 virtual bool SendsEvent(TtsEventType event_type); | 50 virtual bool SendsEvent(TtsEventType event_type); |
| 36 | 51 |
| 37 // Get the single instance of this class. | 52 // Get the single instance of this class. |
| 38 static ExtensionTtsPlatformImplChromeOs* GetInstance(); | 53 static ExtensionTtsPlatformImplChromeOs* GetInstance(); |
| 39 | 54 |
| 55 // TTS won't begin until this is called. |
| 56 void Enable(); |
| 57 |
| 40 private: | 58 private: |
| 41 ExtensionTtsPlatformImplChromeOs() | 59 ExtensionTtsPlatformImplChromeOs(); |
| 42 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} | |
| 43 virtual ~ExtensionTtsPlatformImplChromeOs() {} | 60 virtual ~ExtensionTtsPlatformImplChromeOs() {} |
| 44 | 61 |
| 45 void PollUntilSpeechFinishes(int utterance_id); | 62 void PollUntilSpeechFinishes(int utterance_id); |
| 46 void ContinuePollingIfSpeechIsNotFinished(int utterance_id, bool result); | 63 void ContinuePollingIfSpeechIsNotFinished(int utterance_id, bool result); |
| 47 | 64 |
| 48 void AppendSpeakOption(std::string key, | 65 void AppendSpeakOption(std::string key, |
| 49 std::string value, | 66 std::string value, |
| 50 std::string* options); | 67 std::string* options); |
| 51 | 68 |
| 52 int utterance_id_; | 69 int utterance_id_; |
| 53 int utterance_length_; | 70 int utterance_length_; |
| 71 std::list<QueuedUtterance*> queued_utterances_; |
| 72 bool enabled_; |
| 54 base::WeakPtrFactory<ExtensionTtsPlatformImplChromeOs> weak_ptr_factory_; | 73 base::WeakPtrFactory<ExtensionTtsPlatformImplChromeOs> weak_ptr_factory_; |
| 55 | 74 |
| 56 friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplChromeOs>; | 75 friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplChromeOs>; |
| 57 | 76 |
| 58 DISALLOW_COPY_AND_ASSIGN(ExtensionTtsPlatformImplChromeOs); | 77 DISALLOW_COPY_AND_ASSIGN(ExtensionTtsPlatformImplChromeOs); |
| 59 }; | 78 }; |
| 60 | 79 |
| 61 // static | 80 // static |
| 62 ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() { | 81 ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() { |
| 63 return ExtensionTtsPlatformImplChromeOs::GetInstance(); | 82 return ExtensionTtsPlatformImplChromeOs::GetInstance(); |
| 64 } | 83 } |
| 65 | 84 |
| 85 ExtensionTtsPlatformImplChromeOs::ExtensionTtsPlatformImplChromeOs() |
| 86 : enabled_(false), |
| 87 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 88 } |
| 89 |
| 66 bool ExtensionTtsPlatformImplChromeOs::Speak( | 90 bool ExtensionTtsPlatformImplChromeOs::Speak( |
| 67 int utterance_id, | 91 int utterance_id, |
| 68 const std::string& utterance, | 92 const std::string& utterance, |
| 69 const std::string& lang, | 93 const std::string& lang, |
| 70 const UtteranceContinuousParameters& params) { | 94 const UtteranceContinuousParameters& params) { |
| 95 if (!enabled_) { |
| 96 QueuedUtterance *queued = new QueuedUtterance(); |
| 97 queued->utterance_id = utterance_id; |
| 98 queued->utterance = utterance; |
| 99 queued->lang = lang; |
| 100 queued->params = params; |
| 101 queued_utterances_.push_back(queued); |
| 102 return true; |
| 103 } |
| 104 |
| 71 utterance_id_ = utterance_id; | 105 utterance_id_ = utterance_id; |
| 72 utterance_length_ = utterance.size(); | 106 utterance_length_ = utterance.size(); |
| 73 | 107 |
| 74 std::string options; | 108 std::string options; |
| 75 | 109 |
| 76 if (!lang.empty()) { | 110 if (!lang.empty()) { |
| 77 AppendSpeakOption( | 111 AppendSpeakOption( |
| 78 chromeos::SpeechSynthesizerClient::kSpeechPropertyLocale, | 112 chromeos::SpeechSynthesizerClient::kSpeechPropertyLocale, |
| 79 lang, | 113 lang, |
| 80 &options); | 114 &options); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 104 &options); | 138 &options); |
| 105 } | 139 } |
| 106 | 140 |
| 107 chromeos::SpeechSynthesizerClient* speech_synthesizer_client = | 141 chromeos::SpeechSynthesizerClient* speech_synthesizer_client = |
| 108 chromeos::DBusThreadManager::Get()->GetSpeechSynthesizerClient(); | 142 chromeos::DBusThreadManager::Get()->GetSpeechSynthesizerClient(); |
| 109 | 143 |
| 110 if (!options.empty()) | 144 if (!options.empty()) |
| 111 speech_synthesizer_client->SetSpeakProperties(options); | 145 speech_synthesizer_client->SetSpeakProperties(options); |
| 112 | 146 |
| 113 speech_synthesizer_client->Speak(utterance); | 147 speech_synthesizer_client->Speak(utterance); |
| 114 ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); | 148 if (utterance_id_ >= 0) { |
| 115 controller->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0, std::string()); | 149 ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); |
| 116 PollUntilSpeechFinishes(utterance_id_); | 150 controller->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0, std::string()); |
| 151 PollUntilSpeechFinishes(utterance_id_); |
| 152 } |
| 117 | 153 |
| 118 return true; | 154 return true; |
| 119 } | 155 } |
| 120 | 156 |
| 121 bool ExtensionTtsPlatformImplChromeOs::StopSpeaking() { | 157 bool ExtensionTtsPlatformImplChromeOs::StopSpeaking() { |
| 158 // If we haven't been enabled yet, clear the internal queue. |
| 159 if (!enabled_) { |
| 160 STLDeleteElements(&queued_utterances_); |
| 161 return true; |
| 162 } |
| 163 |
| 122 chromeos::DBusThreadManager::Get()->GetSpeechSynthesizerClient()-> | 164 chromeos::DBusThreadManager::Get()->GetSpeechSynthesizerClient()-> |
| 123 StopSpeaking(); | 165 StopSpeaking(); |
| 124 return true; | 166 return true; |
| 125 } | 167 } |
| 126 | 168 |
| 127 bool ExtensionTtsPlatformImplChromeOs::SendsEvent(TtsEventType event_type) { | 169 bool ExtensionTtsPlatformImplChromeOs::SendsEvent(TtsEventType event_type) { |
| 128 return (event_type == TTS_EVENT_START || | 170 return (event_type == TTS_EVENT_START || |
| 129 event_type == TTS_EVENT_END || | 171 event_type == TTS_EVENT_END || |
| 130 event_type == TTS_EVENT_ERROR); | 172 event_type == TTS_EVENT_ERROR); |
| 131 } | 173 } |
| 132 | 174 |
| 175 void ExtensionTtsPlatformImplChromeOs::Enable() { |
| 176 enabled_ = true; |
| 177 while (!queued_utterances_.empty()) { |
| 178 QueuedUtterance* queued = queued_utterances_.front(); |
| 179 Speak(queued->utterance_id, |
| 180 queued->utterance, |
| 181 queued->lang, |
| 182 queued->params); |
| 183 delete queued; |
| 184 queued_utterances_.pop_front(); |
| 185 } |
| 186 } |
| 187 |
| 133 void ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes( | 188 void ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes( |
| 134 int utterance_id) { | 189 int utterance_id) { |
| 135 if (utterance_id != utterance_id_) { | 190 if (utterance_id != utterance_id_) { |
| 136 // This utterance must have been interrupted or cancelled. | 191 // This utterance must have been interrupted or cancelled. |
| 137 return; | 192 return; |
| 138 } | 193 } |
| 139 chromeos::SpeechSynthesizerClient* speech_synthesizer_client = | 194 chromeos::SpeechSynthesizerClient* speech_synthesizer_client = |
| 140 chromeos::DBusThreadManager::Get()->GetSpeechSynthesizerClient(); | 195 chromeos::DBusThreadManager::Get()->GetSpeechSynthesizerClient(); |
| 141 speech_synthesizer_client->IsSpeaking(base::Bind( | 196 speech_synthesizer_client->IsSpeaking(base::Bind( |
| 142 &ExtensionTtsPlatformImplChromeOs::ContinuePollingIfSpeechIsNotFinished, | 197 &ExtensionTtsPlatformImplChromeOs::ContinuePollingIfSpeechIsNotFinished, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 173 chromeos::SpeechSynthesizerClient::kSpeechPropertyEquals + | 228 chromeos::SpeechSynthesizerClient::kSpeechPropertyEquals + |
| 174 value + | 229 value + |
| 175 chromeos::SpeechSynthesizerClient::kSpeechPropertyDelimiter; | 230 chromeos::SpeechSynthesizerClient::kSpeechPropertyDelimiter; |
| 176 } | 231 } |
| 177 | 232 |
| 178 // static | 233 // static |
| 179 ExtensionTtsPlatformImplChromeOs* | 234 ExtensionTtsPlatformImplChromeOs* |
| 180 ExtensionTtsPlatformImplChromeOs::GetInstance() { | 235 ExtensionTtsPlatformImplChromeOs::GetInstance() { |
| 181 return Singleton<ExtensionTtsPlatformImplChromeOs>::get(); | 236 return Singleton<ExtensionTtsPlatformImplChromeOs>::get(); |
| 182 } | 237 } |
| 238 |
| 239 // global |
| 240 void EnableChromeOsTts() { |
| 241 ExtensionTtsPlatformImplChromeOs::GetInstance()->Enable(); |
| 242 } |
| OLD | NEW |