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. |
| 155 ClearUtteranceQueue(false); // Don't sent events. |
154 } | 156 } |
155 | 157 |
156 void ExtensionTtsController::SpeakOrEnqueue(Utterance* utterance) { | 158 void ExtensionTtsController::SpeakOrEnqueue(Utterance* utterance) { |
157 if (IsSpeaking() && utterance->can_enqueue()) { | 159 if (IsSpeaking() && utterance->can_enqueue()) { |
158 utterance_queue_.push(utterance); | 160 utterance_queue_.push(utterance); |
159 } else { | 161 } else { |
160 Stop(); | 162 Stop(); |
161 SpeakNow(utterance); | 163 SpeakNow(utterance); |
162 } | 164 } |
163 } | 165 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 if (current_utterance_ && !current_utterance_->extension_id().empty()) { | 204 if (current_utterance_ && !current_utterance_->extension_id().empty()) { |
203 ExtensionTtsEngineStop(current_utterance_); | 205 ExtensionTtsEngineStop(current_utterance_); |
204 } else { | 206 } else { |
205 GetPlatformImpl()->clear_error(); | 207 GetPlatformImpl()->clear_error(); |
206 GetPlatformImpl()->StopSpeaking(); | 208 GetPlatformImpl()->StopSpeaking(); |
207 } | 209 } |
208 | 210 |
209 if (current_utterance_) | 211 if (current_utterance_) |
210 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, -1, std::string()); | 212 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, -1, std::string()); |
211 FinishCurrentUtterance(); | 213 FinishCurrentUtterance(); |
212 ClearUtteranceQueue(); | 214 ClearUtteranceQueue(true); // Send events. |
213 } | 215 } |
214 | 216 |
215 void ExtensionTtsController::OnTtsEvent(int utterance_id, | 217 void ExtensionTtsController::OnTtsEvent(int utterance_id, |
216 TtsEventType event_type, | 218 TtsEventType event_type, |
217 int char_index, | 219 int char_index, |
218 const std::string& error_message) { | 220 const std::string& error_message) { |
219 // We may sometimes receive completion callbacks "late", after we've | 221 // We may sometimes receive completion callbacks "late", after we've |
220 // already finished the utterance (for example because another utterance | 222 // already finished the utterance (for example because another utterance |
221 // interrupted or we got a call to Stop). This is normal and we can | 223 // interrupted or we got a call to Stop). This is normal and we can |
222 // safely just ignore these events. | 224 // safely just ignore these events. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 void ExtensionTtsController::SpeakNextUtterance() { | 290 void ExtensionTtsController::SpeakNextUtterance() { |
289 // Start speaking the next utterance in the queue. Keep trying in case | 291 // 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. | 292 // one fails but there are still more in the queue to try. |
291 while (!utterance_queue_.empty() && !current_utterance_) { | 293 while (!utterance_queue_.empty() && !current_utterance_) { |
292 Utterance* utterance = utterance_queue_.front(); | 294 Utterance* utterance = utterance_queue_.front(); |
293 utterance_queue_.pop(); | 295 utterance_queue_.pop(); |
294 SpeakNow(utterance); | 296 SpeakNow(utterance); |
295 } | 297 } |
296 } | 298 } |
297 | 299 |
298 void ExtensionTtsController::ClearUtteranceQueue() { | 300 void ExtensionTtsController::ClearUtteranceQueue(bool send_events) { |
299 while (!utterance_queue_.empty()) { | 301 while (!utterance_queue_.empty()) { |
300 Utterance* utterance = utterance_queue_.front(); | 302 Utterance* utterance = utterance_queue_.front(); |
301 utterance_queue_.pop(); | 303 utterance_queue_.pop(); |
302 utterance->OnTtsEvent(TTS_EVENT_CANCELLED, -1, std::string()); | 304 if (send_events) |
| 305 utterance->OnTtsEvent(TTS_EVENT_CANCELLED, -1, std::string()); |
| 306 else |
| 307 utterance->Finish(); |
303 delete utterance; | 308 delete utterance; |
304 } | 309 } |
305 } | 310 } |
306 | 311 |
307 void ExtensionTtsController::SetPlatformImpl( | 312 void ExtensionTtsController::SetPlatformImpl( |
308 ExtensionTtsPlatformImpl* platform_impl) { | 313 ExtensionTtsPlatformImpl* platform_impl) { |
309 platform_impl_ = platform_impl; | 314 platform_impl_ = platform_impl; |
310 } | 315 } |
311 | 316 |
312 int ExtensionTtsController::QueueSize() { | 317 int ExtensionTtsController::QueueSize() { |
313 return static_cast<int>(utterance_queue_.size()); | 318 return static_cast<int>(utterance_queue_.size()); |
314 } | 319 } |
315 | 320 |
316 ExtensionTtsPlatformImpl* ExtensionTtsController::GetPlatformImpl() { | 321 ExtensionTtsPlatformImpl* ExtensionTtsController::GetPlatformImpl() { |
317 if (!platform_impl_) | 322 if (!platform_impl_) |
318 platform_impl_ = ExtensionTtsPlatformImpl::GetInstance(); | 323 platform_impl_ = ExtensionTtsPlatformImpl::GetInstance(); |
319 return platform_impl_; | 324 return platform_impl_; |
320 } | 325 } |
OLD | NEW |