Chromium Code Reviews| 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, |
|
Satish
2012/03/13 15:30:15
change 'caller_id' to 'session_id' throughout to m
Primiano Tucci (use gerrit)
2012/03/14 09:40:35
See previous reply
| |
| 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 |
| 251 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 251 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| (...skipping 24 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, |
| 305 bool success) { | |
| 301 DCHECK_EQ(caller_id, kSpeechCallerId); | 306 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 302 } | 307 } |
| 303 | 308 |
| 304 void SpeechInputExtensionManager::DidStartReceivingAudioOnUIThread() { | 309 void SpeechInputExtensionManager::DidStartReceivingAudioOnUIThread() { |
| 305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 306 | 311 |
| 307 base::AutoLock auto_lock(state_lock_); | 312 base::AutoLock auto_lock(state_lock_); |
| 308 if (state_ == kShutdown) | 313 if (state_ == kShutdown) |
| 309 return; | 314 return; |
| 310 | 315 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 328 prefs::kSpeechInputTrayNotificationShown, true); | 333 prefs::kSpeechInputTrayNotificationShown, true); |
| 329 } | 334 } |
| 330 | 335 |
| 331 VLOG(1) << "Sending start notification"; | 336 VLOG(1) << "Sending start notification"; |
| 332 content::NotificationService::current()->Notify( | 337 content::NotificationService::current()->Notify( |
| 333 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, | 338 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, |
| 334 content::Source<Profile>(profile_), | 339 content::Source<Profile>(profile_), |
| 335 content::Details<std::string>(&extension_id_in_use_)); | 340 content::Details<std::string>(&extension_id_in_use_)); |
| 336 } | 341 } |
| 337 | 342 |
| 338 void SpeechInputExtensionManager::OnRecognizerError( | 343 void SpeechInputExtensionManager::OnRecognitionError( |
| 339 int caller_id, content::SpeechRecognitionErrorCode error) { | 344 int caller_id, const content::SpeechRecognitionErrorCode& error) { |
| 340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 341 DCHECK_EQ(caller_id, kSpeechCallerId); | 346 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 342 VLOG(1) << "OnRecognizerError: " << error; | 347 VLOG(1) << "OnRecognitionError: " << error; |
| 343 | 348 |
| 344 base::AutoLock auto_lock(state_lock_); | 349 base::AutoLock auto_lock(state_lock_); |
| 345 if (state_ == kShutdown) | 350 if (state_ == kShutdown) |
| 346 return; | 351 return; |
| 347 | 352 |
| 348 // Release the recognizer object. | 353 // Release the recognizer object. |
| 349 GetSpeechInputExtensionInterface()->StopRecording(true); | 354 GetSpeechInputExtensionInterface()->StopRecording(true); |
| 350 | 355 |
| 351 std::string event_error_code; | 356 std::string event_error_code; |
| 352 bool report_to_event = true; | 357 bool report_to_event = true; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 NOTREACHED(); | 393 NOTREACHED(); |
| 389 } | 394 } |
| 390 | 395 |
| 391 if (!event_error_code.empty()) { | 396 if (!event_error_code.empty()) { |
| 392 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 397 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 393 base::Bind(&SpeechInputExtensionManager::DispatchError, | 398 base::Bind(&SpeechInputExtensionManager::DispatchError, |
| 394 this, event_error_code, report_to_event)); | 399 this, event_error_code, report_to_event)); |
| 395 } | 400 } |
| 396 } | 401 } |
| 397 | 402 |
| 398 void SpeechInputExtensionManager::DidCompleteEnvironmentEstimation( | 403 void SpeechInputExtensionManager::OnEnvironmentEstimationComplete( |
| 399 int caller_id) { | 404 int caller_id) { |
| 400 DCHECK_EQ(caller_id, kSpeechCallerId); | 405 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 401 } | 406 } |
| 402 | 407 |
| 403 void SpeechInputExtensionManager::DidStartReceivingSpeech(int caller_id) { | 408 void SpeechInputExtensionManager::OnSoundStart(int caller_id) { |
| 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 405 DCHECK_EQ(caller_id, kSpeechCallerId); | 410 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 406 VLOG(1) << "DidStartReceivingSpeech"; | 411 VLOG(1) << "OnSoundStart"; |
| 407 | 412 |
| 408 std::string json_args; | 413 std::string json_args; |
| 409 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 414 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 410 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, | 415 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, |
| 411 this, extension_id_in_use_, std::string(kOnSoundStartEvent), | 416 this, extension_id_in_use_, std::string(kOnSoundStartEvent), |
| 412 json_args)); | 417 json_args)); |
| 413 } | 418 } |
| 414 | 419 |
| 415 void SpeechInputExtensionManager::DidStopReceivingSpeech(int caller_id) { | 420 void SpeechInputExtensionManager::OnSoundEnd(int caller_id) { |
| 416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 417 DCHECK_EQ(caller_id, kSpeechCallerId); | 422 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 418 VLOG(1) << "DidStopReceivingSpeech"; | 423 VLOG(1) << "OnSoundEnd"; |
| 419 | 424 |
| 420 std::string json_args; | 425 std::string json_args; |
| 421 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 426 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 422 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, | 427 base::Bind(&SpeechInputExtensionManager::DispatchEventToExtension, |
| 423 this, extension_id_in_use_, std::string(kOnSoundEndEvent), | 428 this, extension_id_in_use_, std::string(kOnSoundEndEvent), |
| 424 json_args)); | 429 json_args)); |
| 425 } | 430 } |
| 426 | 431 |
| 427 void SpeechInputExtensionManager::DispatchEventToExtension( | 432 void SpeechInputExtensionManager::DispatchEventToExtension( |
| 428 const std::string& extension_id, const std::string& event, | 433 const std::string& extension_id, const std::string& event, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 595 } | 600 } |
| 596 | 601 |
| 597 void SpeechInputExtensionManager::IsRecordingOnUIThread( | 602 void SpeechInputExtensionManager::IsRecordingOnUIThread( |
| 598 const IsRecordingCallback& callback, | 603 const IsRecordingCallback& callback, |
| 599 bool result) { | 604 bool result) { |
| 600 BrowserThread::CurrentlyOn(BrowserThread::UI); | 605 BrowserThread::CurrentlyOn(BrowserThread::UI); |
| 601 callback.Run(result); | 606 callback.Run(result); |
| 602 } | 607 } |
| 603 | 608 |
| 604 void SpeechInputExtensionManager::StartRecording( | 609 void SpeechInputExtensionManager::StartRecording( |
| 605 content::SpeechRecognizerDelegate* delegate, | 610 content::SpeechRecognitionEventListener* delegate, |
|
hans
2012/03/13 14:32:46
event_listener?
Primiano Tucci (use gerrit)
2012/03/14 09:40:35
Right.
Renaming to just "listener", as agreed offl
| |
| 606 net::URLRequestContextGetter* context_getter, | 611 net::URLRequestContextGetter* context_getter, |
| 607 int caller_id, | 612 int caller_id, |
| 608 const std::string& language, | 613 const std::string& language, |
| 609 const std::string& grammar, | 614 const std::string& grammar, |
| 610 bool filter_profanities) { | 615 bool filter_profanities) { |
| 611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 616 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 612 DCHECK(!recognizer_); | 617 DCHECK(!recognizer_); |
| 613 recognizer_ = content::SpeechRecognizer::Create( | 618 recognizer_ = content::SpeechRecognizer::Create( |
| 614 delegate, caller_id, language, grammar, context_getter, | 619 delegate, caller_id, language, grammar, context_getter, |
| 615 filter_profanities, "", ""); | 620 filter_profanities, "", ""); |
| 616 recognizer_->StartRecording(); | 621 recognizer_->StartRecognition(); |
| 617 } | 622 } |
| 618 | 623 |
| 619 bool SpeechInputExtensionManager::HasValidRecognizer() { | 624 bool SpeechInputExtensionManager::HasValidRecognizer() { |
| 620 return !!recognizer_; | 625 return !!recognizer_; |
| 621 } | 626 } |
| 622 | 627 |
| 623 bool SpeechInputExtensionManager::Stop(const std::string& extension_id, | 628 bool SpeechInputExtensionManager::Stop(const std::string& extension_id, |
| 624 std::string* error) { | 629 std::string* error) { |
| 625 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 630 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 626 DCHECK(error); | 631 DCHECK(error); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 675 | 680 |
| 676 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 681 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 677 base::Bind(&SpeechInputExtensionManager::StopSucceededOnUIThread, this)); | 682 base::Bind(&SpeechInputExtensionManager::StopSucceededOnUIThread, this)); |
| 678 } | 683 } |
| 679 | 684 |
| 680 void SpeechInputExtensionManager::StopRecording(bool recognition_failed) { | 685 void SpeechInputExtensionManager::StopRecording(bool recognition_failed) { |
| 681 if (recognizer_) { | 686 if (recognizer_) { |
| 682 // Recognition is already cancelled in case of failure. | 687 // Recognition is already cancelled in case of failure. |
| 683 // Double-cancelling leads to assertion failures. | 688 // Double-cancelling leads to assertion failures. |
| 684 if (!recognition_failed) | 689 if (!recognition_failed) |
| 685 recognizer_->CancelRecognition(); | 690 recognizer_->AbortRecognition(); |
| 686 recognizer_.release(); | 691 recognizer_.release(); |
| 687 } | 692 } |
| 688 } | 693 } |
| 689 | 694 |
| 690 void SpeechInputExtensionManager::StopSucceededOnUIThread() { | 695 void SpeechInputExtensionManager::StopSucceededOnUIThread() { |
| 691 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 696 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 692 VLOG(1) << "Stop succeeded (UI thread)"; | 697 VLOG(1) << "Stop succeeded (UI thread)"; |
| 693 | 698 |
| 694 base::AutoLock auto_lock(state_lock_); | 699 base::AutoLock auto_lock(state_lock_); |
| 695 if (state_ == kShutdown) | 700 if (state_ == kShutdown) |
| 696 return; | 701 return; |
| 697 | 702 |
| 698 std::string extension_id = extension_id_in_use_; | 703 std::string extension_id = extension_id_in_use_; |
| 699 ResetToIdleState(); | 704 ResetToIdleState(); |
| 700 | 705 |
| 701 DCHECK(notification_.get()); | 706 DCHECK(notification_.get()); |
| 702 notification_->Hide(); | 707 notification_->Hide(); |
| 703 | 708 |
| 704 content::NotificationService::current()->Notify( | 709 content::NotificationService::current()->Notify( |
| 705 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, | 710 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, |
| 706 // Guarded by the state_ == kShutdown check. | 711 // Guarded by the state_ == kShutdown check. |
| 707 content::Source<Profile>(profile_), | 712 content::Source<Profile>(profile_), |
| 708 content::Details<std::string>(&extension_id)); | 713 content::Details<std::string>(&extension_id)); |
| 709 } | 714 } |
| 710 | 715 |
| 711 void SpeechInputExtensionManager::SetInputVolume(int caller_id, | 716 void SpeechInputExtensionManager::OnAudioLevelsChanged(int caller_id, |
| 712 float volume, | 717 float volume, |
| 713 float noise_volume) { | 718 float noise_volume) { |
| 714 DCHECK_EQ(caller_id, kSpeechCallerId); | 719 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 715 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 720 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 716 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 721 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 717 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, | 722 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, |
| 718 this, volume)); | 723 this, volume)); |
| 719 } | 724 } |
| 720 | 725 |
| 721 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( | 726 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( |
| 722 float volume) { | 727 float volume) { |
| 723 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 728 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 724 DCHECK(notification_.get()); | 729 DCHECK(notification_.get()); |
| 725 notification_->SetVUMeterVolume(volume); | 730 notification_->SetVUMeterVolume(volume); |
| 726 } | 731 } |
| OLD | NEW |