Chromium Code Reviews| Index: chrome/browser/extensions/extension_tts_api_chromeos.cc |
| =================================================================== |
| --- chrome/browser/extensions/extension_tts_api_chromeos.cc (revision 109026) |
| +++ chrome/browser/extensions/extension_tts_api_chromeos.cc (working copy) |
| @@ -2,6 +2,8 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include <queue> |
| + |
| #include "base/bind.h" |
| #include "base/memory/singleton.h" |
| #include "base/memory/weak_ptr.h" |
| @@ -9,6 +11,7 @@ |
| #include "base/string_number_conversions.h" |
| #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" |
| #include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" |
| +#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.
|
| #include "chrome/browser/extensions/extension_tts_api_controller.h" |
| #include "chrome/browser/extensions/extension_tts_api_platform.h" |
| @@ -18,7 +21,17 @@ |
| const int kSpeechCheckDelayIntervalMs = 100; |
| }; |
| -class ExtensionTtsPlatformImplChromeOs : public ExtensionTtsPlatformImpl { |
| +// Very simple queue of utterances that can't be spoken because audio |
| +// isn't initialized yet. |
| +struct QueuedUtterance { |
| + int utterance_id; |
| + std::string utterance; |
| + std::string lang; |
| + UtteranceContinuousParameters params; |
| +}; |
| + |
| +class ExtensionTtsPlatformImplChromeOs |
| + : public ExtensionTtsPlatformImpl { |
| public: |
| virtual bool PlatformImplAvailable() { |
| return true; |
| @@ -37,9 +50,11 @@ |
| // Get the single instance of this class. |
| static ExtensionTtsPlatformImplChromeOs* GetInstance(); |
| + // TTS won't begin until this is called. |
| + void Enable(); |
| + |
| private: |
| - ExtensionTtsPlatformImplChromeOs() |
| - : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} |
| + ExtensionTtsPlatformImplChromeOs(); |
| virtual ~ExtensionTtsPlatformImplChromeOs() {} |
| void PollUntilSpeechFinishes(int utterance_id); |
| @@ -51,6 +66,8 @@ |
| int utterance_id_; |
| int utterance_length_; |
| + std::queue<QueuedUtterance*> queued_utterances_; |
| + bool enabled_; |
| base::WeakPtrFactory<ExtensionTtsPlatformImplChromeOs> weak_ptr_factory_; |
| friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplChromeOs>; |
| @@ -63,11 +80,26 @@ |
| return ExtensionTtsPlatformImplChromeOs::GetInstance(); |
| } |
| +ExtensionTtsPlatformImplChromeOs::ExtensionTtsPlatformImplChromeOs() |
| + : enabled_(false), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| +} |
| + |
| bool ExtensionTtsPlatformImplChromeOs::Speak( |
| int utterance_id, |
| const std::string& utterance, |
| const std::string& lang, |
| const UtteranceContinuousParameters& params) { |
| + if (!enabled_) { |
| + QueuedUtterance *queued = new QueuedUtterance(); |
| + queued->utterance_id = utterance_id; |
| + queued->utterance = utterance; |
| + queued->lang = lang; |
| + queued->params = params; |
| + queued_utterances_.push(queued); |
| + return true; |
| + } |
| + |
| utterance_id_ = utterance_id; |
| utterance_length_ = utterance.size(); |
| @@ -111,14 +143,25 @@ |
| speech_synthesizer_client->SetSpeakProperties(options); |
| speech_synthesizer_client->Speak(utterance); |
| - ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); |
| - controller->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0, std::string()); |
| - PollUntilSpeechFinishes(utterance_id_); |
| + if (utterance_id_ >= 0) { |
| + ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); |
| + controller->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0, std::string()); |
| + PollUntilSpeechFinishes(utterance_id_); |
| + } |
| return true; |
| } |
| bool ExtensionTtsPlatformImplChromeOs::StopSpeaking() { |
| + // If we haven't been enabled yet, clear the internal queue. |
| + if (!enabled_) { |
| + while (!queued_utterances_.empty()) { |
|
oshima
2011/11/09 23:47:32
STLDeleteContainerPointers?
dmazzoni
2011/11/10 07:19:57
Good idea, I used STLDeleteElements.
|
| + delete queued_utterances_.front(); |
| + queued_utterances_.pop(); |
| + } |
| + return true; |
| + } |
| + |
| chromeos::DBusThreadManager::Get()->GetSpeechSynthesizerClient()-> |
| StopSpeaking(); |
| return true; |
| @@ -130,6 +173,19 @@ |
| event_type == TTS_EVENT_ERROR); |
| } |
| +void ExtensionTtsPlatformImplChromeOs::Enable() { |
| + enabled_ = true; |
| + while (!queued_utterances_.empty()) { |
| + QueuedUtterance* queued = queued_utterances_.front(); |
| + Speak(queued->utterance_id, |
| + queued->utterance, |
| + queued->lang, |
| + queued->params); |
| + delete queued; |
| + queued_utterances_.pop(); |
| + } |
| +} |
| + |
| void ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes( |
| int utterance_id) { |
| if (utterance_id != utterance_id_) { |
| @@ -180,3 +236,8 @@ |
| ExtensionTtsPlatformImplChromeOs::GetInstance() { |
| return Singleton<ExtensionTtsPlatformImplChromeOs>::get(); |
| } |
| + |
| +// global |
| +void EnableChromeOsTts() { |
| + ExtensionTtsPlatformImplChromeOs::GetInstance()->Enable(); |
| +} |