OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/speech/tts_controller_impl.h" | 5 #include "chrome/browser/speech/tts_controller_impl.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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 int char_index, | 84 int char_index, |
85 const std::string& error_message) { | 85 const std::string& error_message) { |
86 if (char_index >= 0) | 86 if (char_index >= 0) |
87 char_index_ = char_index; | 87 char_index_ = char_index; |
88 if (IsFinalTtsEventType(event_type)) | 88 if (IsFinalTtsEventType(event_type)) |
89 finished_ = true; | 89 finished_ = true; |
90 | 90 |
91 if (event_delegate_) | 91 if (event_delegate_) |
92 event_delegate_->OnTtsEvent(this, event_type, char_index, error_message); | 92 event_delegate_->OnTtsEvent(this, event_type, char_index, error_message); |
93 if (finished_) | 93 if (finished_) |
94 event_delegate_.reset(); | 94 event_delegate_ = NULL; |
95 } | 95 } |
96 | 96 |
97 void Utterance::Finish() { | 97 void Utterance::Finish() { |
98 finished_ = true; | 98 finished_ = true; |
99 } | 99 } |
100 | 100 |
101 void Utterance::set_options(const base::Value* options) { | 101 void Utterance::set_options(const base::Value* options) { |
102 options_.reset(options->DeepCopy()); | 102 options_.reset(options->DeepCopy()); |
103 } | 103 } |
104 | 104 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 GetPlatformImpl()->error()); | 237 GetPlatformImpl()->error()); |
238 delete utterance; | 238 delete utterance; |
239 return; | 239 return; |
240 } | 240 } |
241 } | 241 } |
242 } | 242 } |
243 | 243 |
244 void TtsControllerImpl::Stop() { | 244 void TtsControllerImpl::Stop() { |
245 paused_ = false; | 245 paused_ = false; |
246 if (current_utterance_ && !current_utterance_->extension_id().empty()) { | 246 if (current_utterance_ && !current_utterance_->extension_id().empty()) { |
247 #if !defined(OS_ANDROID) | |
248 if (tts_engine_delegate_) | 247 if (tts_engine_delegate_) |
249 tts_engine_delegate_->Stop(current_utterance_); | 248 tts_engine_delegate_->Stop(current_utterance_); |
250 #endif | |
251 } else { | 249 } else { |
252 GetPlatformImpl()->clear_error(); | 250 GetPlatformImpl()->clear_error(); |
253 GetPlatformImpl()->StopSpeaking(); | 251 GetPlatformImpl()->StopSpeaking(); |
254 } | 252 } |
255 | 253 |
256 if (current_utterance_) | 254 if (current_utterance_) |
257 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex, | 255 current_utterance_->OnTtsEvent(TTS_EVENT_INTERRUPTED, kInvalidCharIndex, |
258 std::string()); | 256 std::string()); |
259 FinishCurrentUtterance(); | 257 FinishCurrentUtterance(); |
260 ClearUtteranceQueue(true); // Send events. | 258 ClearUtteranceQueue(true); // Send events. |
261 } | 259 } |
262 | 260 |
263 void TtsControllerImpl::Pause() { | 261 void TtsControllerImpl::Pause() { |
264 paused_ = true; | 262 paused_ = true; |
265 if (current_utterance_ && !current_utterance_->extension_id().empty()) { | 263 if (current_utterance_ && !current_utterance_->extension_id().empty()) { |
266 #if !defined(OS_ANDROID) | |
267 if (tts_engine_delegate_) | 264 if (tts_engine_delegate_) |
268 tts_engine_delegate_->Pause(current_utterance_); | 265 tts_engine_delegate_->Pause(current_utterance_); |
269 #endif | |
270 } else if (current_utterance_) { | 266 } else if (current_utterance_) { |
271 GetPlatformImpl()->clear_error(); | 267 GetPlatformImpl()->clear_error(); |
272 GetPlatformImpl()->Pause(); | 268 GetPlatformImpl()->Pause(); |
273 } | 269 } |
274 } | 270 } |
275 | 271 |
276 void TtsControllerImpl::Resume() { | 272 void TtsControllerImpl::Resume() { |
277 paused_ = false; | 273 paused_ = false; |
278 if (current_utterance_ && !current_utterance_->extension_id().empty()) { | 274 if (current_utterance_ && !current_utterance_->extension_id().empty()) { |
279 #if !defined(OS_ANDROID) | |
280 if (tts_engine_delegate_) | 275 if (tts_engine_delegate_) |
281 tts_engine_delegate_->Resume(current_utterance_); | 276 tts_engine_delegate_->Resume(current_utterance_); |
282 #endif | |
283 } else if (current_utterance_) { | 277 } else if (current_utterance_) { |
284 GetPlatformImpl()->clear_error(); | 278 GetPlatformImpl()->clear_error(); |
285 GetPlatformImpl()->Resume(); | 279 GetPlatformImpl()->Resume(); |
286 } else { | 280 } else { |
287 SpeakNextUtterance(); | 281 SpeakNextUtterance(); |
288 } | 282 } |
289 } | 283 } |
290 | 284 |
291 void TtsControllerImpl::OnTtsEvent(int utterance_id, | 285 void TtsControllerImpl::OnTtsEvent(int utterance_id, |
292 TtsEventType event_type, | 286 TtsEventType event_type, |
293 int char_index, | 287 int char_index, |
294 const std::string& error_message) { | 288 const std::string& error_message) { |
295 // We may sometimes receive completion callbacks "late", after we've | 289 // We may sometimes receive completion callbacks "late", after we've |
296 // already finished the utterance (for example because another utterance | 290 // already finished the utterance (for example because another utterance |
297 // interrupted or we got a call to Stop). This is normal and we can | 291 // interrupted or we got a call to Stop). This is normal and we can |
298 // safely just ignore these events. | 292 // safely just ignore these events. |
299 if (!current_utterance_ || utterance_id != current_utterance_->id()) { | 293 if (!current_utterance_ || utterance_id != current_utterance_->id()) { |
300 return; | 294 return; |
301 } | 295 } |
302 current_utterance_->OnTtsEvent(event_type, char_index, error_message); | 296 current_utterance_->OnTtsEvent(event_type, char_index, error_message); |
303 if (current_utterance_->finished()) { | 297 if (current_utterance_->finished()) { |
304 FinishCurrentUtterance(); | 298 FinishCurrentUtterance(); |
305 SpeakNextUtterance(); | 299 SpeakNextUtterance(); |
306 } | 300 } |
307 } | 301 } |
308 | 302 |
309 void TtsControllerImpl::GetVoices(content::BrowserContext* browser_context, | 303 void TtsControllerImpl::GetVoices(content::BrowserContext* browser_context, |
310 std::vector<VoiceData>* out_voices) { | 304 std::vector<VoiceData>* out_voices) { |
311 #if !defined(OS_ANDROID) | |
312 if (browser_context && tts_engine_delegate_) | 305 if (browser_context && tts_engine_delegate_) |
313 tts_engine_delegate_->GetVoices(browser_context, out_voices); | 306 tts_engine_delegate_->GetVoices(browser_context, out_voices); |
314 #endif | |
315 | 307 |
316 TtsPlatformImpl* platform_impl = GetPlatformImpl(); | 308 TtsPlatformImpl* platform_impl = GetPlatformImpl(); |
317 if (platform_impl) { | 309 if (platform_impl) { |
318 // Ensure we have all built-in voices loaded. This is a no-op if already | 310 // Ensure we have all built-in voices loaded. This is a no-op if already |
319 // loaded. | 311 // loaded. |
320 platform_impl->LoadBuiltInTtsExtension(browser_context); | 312 platform_impl->LoadBuiltInTtsExtension(browser_context); |
321 if (platform_impl->PlatformImplAvailable()) | 313 if (platform_impl->PlatformImplAvailable()) |
322 platform_impl->GetVoices(out_voices); | 314 platform_impl->GetVoices(out_voices); |
323 } | 315 } |
324 } | 316 } |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 void TtsControllerImpl::AddVoicesChangedDelegate( | 444 void TtsControllerImpl::AddVoicesChangedDelegate( |
453 VoicesChangedDelegate* delegate) { | 445 VoicesChangedDelegate* delegate) { |
454 voices_changed_delegates_.insert(delegate); | 446 voices_changed_delegates_.insert(delegate); |
455 } | 447 } |
456 | 448 |
457 void TtsControllerImpl::RemoveVoicesChangedDelegate( | 449 void TtsControllerImpl::RemoveVoicesChangedDelegate( |
458 VoicesChangedDelegate* delegate) { | 450 VoicesChangedDelegate* delegate) { |
459 voices_changed_delegates_.erase(delegate); | 451 voices_changed_delegates_.erase(delegate); |
460 } | 452 } |
461 | 453 |
| 454 void TtsControllerImpl::RemoveUtteranceEventDelegate( |
| 455 UtteranceEventDelegate* delegate) { |
| 456 // First clear any pending utterances with this delegate. |
| 457 std::queue<Utterance*> old_queue = utterance_queue_; |
| 458 utterance_queue_ = std::queue<Utterance*>(); |
| 459 while (!old_queue.empty()) { |
| 460 Utterance* utterance = old_queue.front(); |
| 461 old_queue.pop(); |
| 462 if (utterance->event_delegate() != delegate) |
| 463 utterance_queue_.push(utterance); |
| 464 else |
| 465 delete utterance; |
| 466 } |
| 467 |
| 468 if (current_utterance_ && current_utterance_->event_delegate() == delegate) { |
| 469 current_utterance_->set_event_delegate(NULL); |
| 470 if (!current_utterance_->extension_id().empty()) { |
| 471 if (tts_engine_delegate_) |
| 472 tts_engine_delegate_->Stop(current_utterance_); |
| 473 } else { |
| 474 GetPlatformImpl()->clear_error(); |
| 475 GetPlatformImpl()->StopSpeaking(); |
| 476 } |
| 477 |
| 478 FinishCurrentUtterance(); |
| 479 if (!paused_) |
| 480 SpeakNextUtterance(); |
| 481 } |
| 482 } |
| 483 |
462 void TtsControllerImpl::SetTtsEngineDelegate( | 484 void TtsControllerImpl::SetTtsEngineDelegate( |
463 TtsEngineDelegate* delegate) { | 485 TtsEngineDelegate* delegate) { |
464 tts_engine_delegate_ = delegate; | 486 tts_engine_delegate_ = delegate; |
465 } | 487 } |
466 | 488 |
467 TtsEngineDelegate* TtsControllerImpl::GetTtsEngineDelegate() { | 489 TtsEngineDelegate* TtsControllerImpl::GetTtsEngineDelegate() { |
468 return tts_engine_delegate_; | 490 return tts_engine_delegate_; |
469 } | 491 } |
OLD | NEW |