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