OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/speech_input_extension_manager.h" | 5 #include "chrome/browser/speech/speech_input_extension_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 SpeechInputExtensionManager::GetSpeechInputExtensionInterface() { | 230 SpeechInputExtensionManager::GetSpeechInputExtensionInterface() { |
231 return speech_interface_ ? speech_interface_ : this; | 231 return speech_interface_ ? speech_interface_ : this; |
232 } | 232 } |
233 | 233 |
234 void SpeechInputExtensionManager::ResetToIdleState() { | 234 void SpeechInputExtensionManager::ResetToIdleState() { |
235 VLOG(1) << "State changed to idle. Deassociating any extensions."; | 235 VLOG(1) << "State changed to idle. Deassociating any extensions."; |
236 state_ = kIdle; | 236 state_ = kIdle; |
237 extension_id_in_use_.clear(); | 237 extension_id_in_use_.clear(); |
238 } | 238 } |
239 | 239 |
240 void SpeechInputExtensionManager::SetRecognitionResult( | 240 void SpeechInputExtensionManager::OnRecognitionResult( |
241 int caller_id, | 241 int caller_id, |
242 const content::SpeechRecognitionResult& result) { | 242 const content::SpeechRecognitionResult& result) { |
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
244 DCHECK_EQ(caller_id, kSpeechCallerId); | 244 DCHECK_EQ(caller_id, kSpeechCallerId); |
245 | 245 |
246 // Stopping will start the disassociation with the extension. | 246 // Stopping will start the disassociation with the extension. |
247 // Make a copy to report the results to the proper one. | 247 // Make a copy to report the results to the proper one. |
248 std::string extension_id = extension_id_in_use_; | 248 std::string extension_id = extension_id_in_use_; |
249 ForceStopOnIOThread(); | 249 ForceStopOnIOThread(); |
250 | 250 |
(...skipping 25 matching lines...) Expand all Loading... |
276 js_hypothesis_object->SetDouble(kConfidenceKey, | 276 js_hypothesis_object->SetDouble(kConfidenceKey, |
277 hypothesis.confidence); | 277 hypothesis.confidence); |
278 } | 278 } |
279 | 279 |
280 std::string json_args; | 280 std::string json_args; |
281 base::JSONWriter::Write(&args, false, &json_args); | 281 base::JSONWriter::Write(&args, false, &json_args); |
282 VLOG(1) << "Results: " << json_args; | 282 VLOG(1) << "Results: " << json_args; |
283 DispatchEventToExtension(extension_id, kOnResultEvent, json_args); | 283 DispatchEventToExtension(extension_id, kOnResultEvent, json_args); |
284 } | 284 } |
285 | 285 |
286 void SpeechInputExtensionManager::DidStartReceivingAudio(int caller_id) { | 286 void SpeechInputExtensionManager::OnRecognitionStart(int caller_id) { |
287 VLOG(1) << "DidStartReceivingAudio"; | 287 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 288 } |
| 289 |
| 290 void SpeechInputExtensionManager::OnAudioStart(int caller_id) { |
| 291 VLOG(1) << "OnAudioStart"; |
288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 292 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
289 DCHECK_EQ(caller_id, kSpeechCallerId); | 293 DCHECK_EQ(caller_id, kSpeechCallerId); |
290 | 294 |
291 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 295 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
292 base::Bind(&SpeechInputExtensionManager::DidStartReceivingAudioOnUIThread, | 296 base::Bind(&SpeechInputExtensionManager::DidStartReceivingAudioOnUIThread, |
293 this)); | 297 this)); |
294 } | 298 } |
295 | 299 |
296 void SpeechInputExtensionManager::DidCompleteRecording(int caller_id) { | 300 void SpeechInputExtensionManager::OnAudioEnd(int caller_id) { |
297 DCHECK_EQ(caller_id, kSpeechCallerId); | 301 DCHECK_EQ(caller_id, kSpeechCallerId); |
298 } | 302 } |
299 | 303 |
300 void SpeechInputExtensionManager::DidCompleteRecognition(int caller_id) { | 304 void SpeechInputExtensionManager::OnRecognitionEnd(int caller_id) { |
301 DCHECK_EQ(caller_id, kSpeechCallerId); | 305 DCHECK_EQ(caller_id, kSpeechCallerId); |
302 } | 306 } |
303 | 307 |
304 void SpeechInputExtensionManager::DidStartReceivingAudioOnUIThread() { | 308 void SpeechInputExtensionManager::DidStartReceivingAudioOnUIThread() { |
305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
306 | 310 |
307 base::AutoLock auto_lock(state_lock_); | 311 base::AutoLock auto_lock(state_lock_); |
308 if (state_ == kShutdown) | 312 if (state_ == kShutdown) |
309 return; | 313 return; |
310 | 314 |
(...skipping 17 matching lines...) Expand all Loading... |
328 prefs::kSpeechInputTrayNotificationShown, true); | 332 prefs::kSpeechInputTrayNotificationShown, true); |
329 } | 333 } |
330 | 334 |
331 VLOG(1) << "Sending start notification"; | 335 VLOG(1) << "Sending start notification"; |
332 content::NotificationService::current()->Notify( | 336 content::NotificationService::current()->Notify( |
333 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, | 337 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, |
334 content::Source<Profile>(profile_), | 338 content::Source<Profile>(profile_), |
335 content::Details<std::string>(&extension_id_in_use_)); | 339 content::Details<std::string>(&extension_id_in_use_)); |
336 } | 340 } |
337 | 341 |
338 void SpeechInputExtensionManager::OnRecognizerError( | 342 void SpeechInputExtensionManager::OnRecognitionError( |
339 int caller_id, content::SpeechRecognitionErrorCode error) { | 343 int caller_id, const content::SpeechRecognitionErrorCode& error) { |
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
341 DCHECK_EQ(caller_id, kSpeechCallerId); | 345 DCHECK_EQ(caller_id, kSpeechCallerId); |
342 VLOG(1) << "OnRecognizerError: " << error; | 346 VLOG(1) << "OnRecognitionError: " << error; |
343 | 347 |
344 base::AutoLock auto_lock(state_lock_); | 348 base::AutoLock auto_lock(state_lock_); |
345 if (state_ == kShutdown) | 349 if (state_ == kShutdown) |
346 return; | 350 return; |
347 | 351 |
348 // Release the recognizer object. | 352 // Release the recognizer object. |
349 GetSpeechInputExtensionInterface()->StopRecording(true); | 353 GetSpeechInputExtensionInterface()->StopRecording(true); |
350 | 354 |
351 std::string event_error_code; | 355 std::string event_error_code; |
352 bool report_to_event = true; | 356 bool report_to_event = true; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 NOTREACHED(); | 392 NOTREACHED(); |
389 } | 393 } |
390 | 394 |
391 if (!event_error_code.empty()) { | 395 if (!event_error_code.empty()) { |
392 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 396 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
393 base::Bind(&SpeechInputExtensionManager::DispatchError, | 397 base::Bind(&SpeechInputExtensionManager::DispatchError, |
394 this, event_error_code, report_to_event)); | 398 this, event_error_code, report_to_event)); |
395 } | 399 } |
396 } | 400 } |
397 | 401 |
398 void SpeechInputExtensionManager::DidCompleteEnvironmentEstimation( | 402 void SpeechInputExtensionManager::OnEnvironmentEstimationComplete( |
399 int caller_id) { | 403 int caller_id) { |
400 DCHECK_EQ(caller_id, kSpeechCallerId); | 404 DCHECK_EQ(caller_id, kSpeechCallerId); |
401 } | 405 } |
402 | 406 |
403 void SpeechInputExtensionManager::DidStartReceivingSpeech(int caller_id) { | 407 void SpeechInputExtensionManager::OnSoundStart(int caller_id) { |
404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 408 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
405 DCHECK_EQ(caller_id, kSpeechCallerId); | 409 DCHECK_EQ(caller_id, kSpeechCallerId); |
406 VLOG(1) << "DidStartReceivingSpeech"; | 410 VLOG(1) << "OnSoundStart"; |
407 | 411 |
408 std::string json_args; | 412 std::string json_args; |
409 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 413 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
410 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, | 414 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, |
411 this, extension_id_in_use_, std::string(kOnSoundStartEvent), | 415 this, extension_id_in_use_, std::string(kOnSoundStartEvent), |
412 json_args)); | 416 json_args)); |
413 } | 417 } |
414 | 418 |
415 void SpeechInputExtensionManager::DidStopReceivingSpeech(int caller_id) { | 419 void SpeechInputExtensionManager::OnSoundEnd(int caller_id) { |
416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
417 DCHECK_EQ(caller_id, kSpeechCallerId); | 421 DCHECK_EQ(caller_id, kSpeechCallerId); |
418 VLOG(1) << "DidStopReceivingSpeech"; | 422 VLOG(1) << "OnSoundEnd"; |
419 | 423 |
420 std::string json_args; | 424 std::string json_args; |
421 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 425 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
422 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, | 426 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, |
423 this, extension_id_in_use_, std::string(kOnSoundEndEvent), | 427 this, extension_id_in_use_, std::string(kOnSoundEndEvent), |
424 json_args)); | 428 json_args)); |
425 } | 429 } |
426 | 430 |
427 void SpeechInputExtensionManager::DispatchEventToExtension( | 431 void SpeechInputExtensionManager::DispatchEventToExtension( |
428 const std::string& extension_id, const std::string& event, | 432 const std::string& extension_id, const std::string& event, |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 } | 599 } |
596 | 600 |
597 void SpeechInputExtensionManager::IsRecordingOnUIThread( | 601 void SpeechInputExtensionManager::IsRecordingOnUIThread( |
598 const IsRecordingCallback& callback, | 602 const IsRecordingCallback& callback, |
599 bool result) { | 603 bool result) { |
600 BrowserThread::CurrentlyOn(BrowserThread::UI); | 604 BrowserThread::CurrentlyOn(BrowserThread::UI); |
601 callback.Run(result); | 605 callback.Run(result); |
602 } | 606 } |
603 | 607 |
604 void SpeechInputExtensionManager::StartRecording( | 608 void SpeechInputExtensionManager::StartRecording( |
605 content::SpeechRecognizerDelegate* delegate, | 609 content::SpeechRecognitionEventListener* listener, |
606 net::URLRequestContextGetter* context_getter, | 610 net::URLRequestContextGetter* context_getter, |
607 int caller_id, | 611 int caller_id, |
608 const std::string& language, | 612 const std::string& language, |
609 const std::string& grammar, | 613 const std::string& grammar, |
610 bool filter_profanities) { | 614 bool filter_profanities) { |
611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 615 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
612 DCHECK(!recognizer_); | 616 DCHECK(!recognizer_); |
613 recognizer_ = content::SpeechRecognizer::Create( | 617 recognizer_ = content::SpeechRecognizer::Create( |
614 delegate, caller_id, language, grammar, context_getter, | 618 listener, caller_id, language, grammar, context_getter, |
615 filter_profanities, "", ""); | 619 filter_profanities, "", ""); |
616 recognizer_->StartRecording(); | 620 recognizer_->StartRecognition(); |
617 } | 621 } |
618 | 622 |
619 bool SpeechInputExtensionManager::HasValidRecognizer() { | 623 bool SpeechInputExtensionManager::HasValidRecognizer() { |
620 return !!recognizer_; | 624 return !!recognizer_; |
621 } | 625 } |
622 | 626 |
623 bool SpeechInputExtensionManager::Stop(const std::string& extension_id, | 627 bool SpeechInputExtensionManager::Stop(const std::string& extension_id, |
624 std::string* error) { | 628 std::string* error) { |
625 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 629 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
626 DCHECK(error); | 630 DCHECK(error); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 | 679 |
676 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 680 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
677 base::Bind(&SpeechInputExtensionManager::StopSucceededOnUIThread, this)); | 681 base::Bind(&SpeechInputExtensionManager::StopSucceededOnUIThread, this)); |
678 } | 682 } |
679 | 683 |
680 void SpeechInputExtensionManager::StopRecording(bool recognition_failed) { | 684 void SpeechInputExtensionManager::StopRecording(bool recognition_failed) { |
681 if (recognizer_) { | 685 if (recognizer_) { |
682 // Recognition is already cancelled in case of failure. | 686 // Recognition is already cancelled in case of failure. |
683 // Double-cancelling leads to assertion failures. | 687 // Double-cancelling leads to assertion failures. |
684 if (!recognition_failed) | 688 if (!recognition_failed) |
685 recognizer_->CancelRecognition(); | 689 recognizer_->AbortRecognition(); |
686 recognizer_.release(); | 690 recognizer_.release(); |
687 } | 691 } |
688 } | 692 } |
689 | 693 |
690 void SpeechInputExtensionManager::StopSucceededOnUIThread() { | 694 void SpeechInputExtensionManager::StopSucceededOnUIThread() { |
691 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 695 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
692 VLOG(1) << "Stop succeeded (UI thread)"; | 696 VLOG(1) << "Stop succeeded (UI thread)"; |
693 | 697 |
694 base::AutoLock auto_lock(state_lock_); | 698 base::AutoLock auto_lock(state_lock_); |
695 if (state_ == kShutdown) | 699 if (state_ == kShutdown) |
696 return; | 700 return; |
697 | 701 |
698 std::string extension_id = extension_id_in_use_; | 702 std::string extension_id = extension_id_in_use_; |
699 ResetToIdleState(); | 703 ResetToIdleState(); |
700 | 704 |
701 DCHECK(notification_.get()); | 705 DCHECK(notification_.get()); |
702 notification_->Hide(); | 706 notification_->Hide(); |
703 | 707 |
704 content::NotificationService::current()->Notify( | 708 content::NotificationService::current()->Notify( |
705 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, | 709 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, |
706 // Guarded by the state_ == kShutdown check. | 710 // Guarded by the state_ == kShutdown check. |
707 content::Source<Profile>(profile_), | 711 content::Source<Profile>(profile_), |
708 content::Details<std::string>(&extension_id)); | 712 content::Details<std::string>(&extension_id)); |
709 } | 713 } |
710 | 714 |
711 void SpeechInputExtensionManager::SetInputVolume(int caller_id, | 715 void SpeechInputExtensionManager::OnAudioLevelsChange(int caller_id, |
712 float volume, | 716 float volume, |
713 float noise_volume) { | 717 float noise_volume) { |
714 DCHECK_EQ(caller_id, kSpeechCallerId); | 718 DCHECK_EQ(caller_id, kSpeechCallerId); |
715 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 719 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
716 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 720 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
717 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, | 721 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, |
718 this, volume)); | 722 this, volume)); |
719 } | 723 } |
720 | 724 |
721 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( | 725 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( |
722 float volume) { | 726 float volume) { |
723 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 727 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
724 DCHECK(notification_.get()); | 728 DCHECK(notification_.get()); |
725 notification_->SetVUMeterVolume(volume); | 729 notification_->SetVUMeterVolume(volume); |
726 } | 730 } |
OLD | NEW |