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 "chrome/browser/extensions/extension_tts_api_controller.h" | 5 #include "chrome/browser/extensions/extension_tts_api_controller.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/float_util.h" | 10 #include "base/float_util.h" |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 ExtensionTtsController::ExtensionTtsController() | 143 ExtensionTtsController::ExtensionTtsController() |
| 144 : current_utterance_(NULL), | 144 : current_utterance_(NULL), |
| 145 platform_impl_(NULL) { | 145 platform_impl_(NULL) { |
| 146 } | 146 } |
| 147 | 147 |
| 148 ExtensionTtsController::~ExtensionTtsController() { | 148 ExtensionTtsController::~ExtensionTtsController() { |
| 149 if (current_utterance_) { | 149 if (current_utterance_) { |
| 150 current_utterance_->Finish(); | 150 current_utterance_->Finish(); |
| 151 delete current_utterance_; | 151 delete current_utterance_; |
| 152 } | 152 } |
| 153 ClearUtteranceQueue(); | 153 |
| 154 // Clear any queued utterances too, but don't send "cancelled" events | |
| 155 // because we're being destroyed. | |
| 156 ClearUtteranceQueue(/*send_events=*/false); | |
|
Aaron Boodman
2011/11/16 09:26:36
It's more common in Chrome code to do:
ClearUtter
dmazzoni
2011/11/16 15:50:39
Done.
| |
| 154 } | 157 } |
| 155 | 158 |
| 156 void ExtensionTtsController::SpeakOrEnqueue(Utterance* utterance) { | 159 void ExtensionTtsController::SpeakOrEnqueue(Utterance* utterance) { |
| 157 if (IsSpeaking() && utterance->can_enqueue()) { | 160 if (IsSpeaking() && utterance->can_enqueue()) { |
| 158 utterance_queue_.push(utterance); | 161 utterance_queue_.push(utterance); |
| 159 } else { | 162 } else { |
| 160 Stop(); | 163 Stop(); |
| 161 SpeakNow(utterance); | 164 SpeakNow(utterance); |
| 162 } | 165 } |
| 163 } | 166 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 if (current_utterance_ && !current_utterance_->extension_id().empty()) { | 205 if (current_utterance_ && !current_utterance_->extension_id().empty()) { |
| 203 ExtensionTtsEngineStop(current_utterance_); | 206 ExtensionTtsEngineStop(current_utterance_); |
| 204 } else { | 207 } else { |
| 205 GetPlatformImpl()->clear_error(); | 208 GetPlatformImpl()->clear_error(); |
| 206 GetPlatformImpl()->StopSpeaking(); | 209 GetPlatformImpl()->StopSpeaking(); |
| 207 } | 210 } |
| 208 | 211 |
| 209 if (current_utterance_) | 212 if (current_utterance_) |
| 210 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, -1, std::string()); | 213 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, -1, std::string()); |
| 211 FinishCurrentUtterance(); | 214 FinishCurrentUtterance(); |
| 212 ClearUtteranceQueue(); | 215 ClearUtteranceQueue(/*send_events=*/true); |
| 213 } | 216 } |
| 214 | 217 |
| 215 void ExtensionTtsController::OnTtsEvent(int utterance_id, | 218 void ExtensionTtsController::OnTtsEvent(int utterance_id, |
| 216 TtsEventType event_type, | 219 TtsEventType event_type, |
| 217 int char_index, | 220 int char_index, |
| 218 const std::string& error_message) { | 221 const std::string& error_message) { |
| 219 // We may sometimes receive completion callbacks "late", after we've | 222 // We may sometimes receive completion callbacks "late", after we've |
| 220 // already finished the utterance (for example because another utterance | 223 // already finished the utterance (for example because another utterance |
| 221 // interrupted or we got a call to Stop). This is normal and we can | 224 // interrupted or we got a call to Stop). This is normal and we can |
| 222 // safely just ignore these events. | 225 // safely just ignore these events. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 void ExtensionTtsController::SpeakNextUtterance() { | 291 void ExtensionTtsController::SpeakNextUtterance() { |
| 289 // Start speaking the next utterance in the queue. Keep trying in case | 292 // Start speaking the next utterance in the queue. Keep trying in case |
| 290 // one fails but there are still more in the queue to try. | 293 // one fails but there are still more in the queue to try. |
| 291 while (!utterance_queue_.empty() && !current_utterance_) { | 294 while (!utterance_queue_.empty() && !current_utterance_) { |
| 292 Utterance* utterance = utterance_queue_.front(); | 295 Utterance* utterance = utterance_queue_.front(); |
| 293 utterance_queue_.pop(); | 296 utterance_queue_.pop(); |
| 294 SpeakNow(utterance); | 297 SpeakNow(utterance); |
| 295 } | 298 } |
| 296 } | 299 } |
| 297 | 300 |
| 298 void ExtensionTtsController::ClearUtteranceQueue() { | 301 void ExtensionTtsController::ClearUtteranceQueue(bool send_events) { |
| 299 while (!utterance_queue_.empty()) { | 302 while (!utterance_queue_.empty()) { |
| 300 Utterance* utterance = utterance_queue_.front(); | 303 Utterance* utterance = utterance_queue_.front(); |
| 301 utterance_queue_.pop(); | 304 utterance_queue_.pop(); |
| 302 utterance->OnTtsEvent(TTS_EVENT_CANCELLED, -1, std::string()); | 305 if (send_events) |
| 306 utterance->OnTtsEvent(TTS_EVENT_CANCELLED, -1, std::string()); | |
| 307 else | |
| 308 utterance->Finish(); | |
| 303 delete utterance; | 309 delete utterance; |
| 304 } | 310 } |
| 305 } | 311 } |
| 306 | 312 |
| 307 void ExtensionTtsController::SetPlatformImpl( | 313 void ExtensionTtsController::SetPlatformImpl( |
| 308 ExtensionTtsPlatformImpl* platform_impl) { | 314 ExtensionTtsPlatformImpl* platform_impl) { |
| 309 platform_impl_ = platform_impl; | 315 platform_impl_ = platform_impl; |
| 310 } | 316 } |
| 311 | 317 |
| 312 int ExtensionTtsController::QueueSize() { | 318 int ExtensionTtsController::QueueSize() { |
| 313 return static_cast<int>(utterance_queue_.size()); | 319 return static_cast<int>(utterance_queue_.size()); |
| 314 } | 320 } |
| 315 | 321 |
| 316 ExtensionTtsPlatformImpl* ExtensionTtsController::GetPlatformImpl() { | 322 ExtensionTtsPlatformImpl* ExtensionTtsController::GetPlatformImpl() { |
| 317 if (!platform_impl_) | 323 if (!platform_impl_) |
| 318 platform_impl_ = ExtensionTtsPlatformImpl::GetInstance(); | 324 platform_impl_ = ExtensionTtsPlatformImpl::GetInstance(); |
| 319 return platform_impl_; | 325 return platform_impl_; |
| 320 } | 326 } |
| OLD | NEW |