| 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" |
| 11 #include "chrome/browser/extensions/extension_event_router.h" | 11 #include "chrome/browser/extensions/extension_event_router.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/profiles/profile_dependency_manager.h" | 15 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 16 #include "chrome/browser/profiles/profile_keyed_service.h" | 16 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 17 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | 17 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
| 18 #include "chrome/browser/speech/speech_input_extension_notification.h" | 18 #include "chrome/browser/speech/speech_input_extension_notification.h" |
| 19 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 20 #include "chrome/common/extensions/extension.h" | 20 #include "chrome/common/extensions/extension.h" |
| 21 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/notification_registrar.h" | 23 #include "content/public/browser/notification_registrar.h" |
| 24 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/browser/speech_input_manager.h" | 25 #include "content/public/browser/speech_recognition_manager.h" |
| 26 #include "content/public/browser/speech_recognizer.h" | 26 #include "content/public/browser/speech_recognizer.h" |
| 27 #include "content/public/common/speech_input_result.h" | 27 #include "content/public/common/speech_recognition_result.h" |
| 28 | 28 |
| 29 using content::BrowserThread; | 29 using content::BrowserThread; |
| 30 using content::SpeechInputManager; | 30 using content::SpeechRecognitionHypothesis; |
| 31 using content::SpeechRecognitionManager; |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 const char kErrorNoRecordingDeviceFound[] = "noRecordingDeviceFound"; | 35 const char kErrorNoRecordingDeviceFound[] = "noRecordingDeviceFound"; |
| 35 const char kErrorRecordingDeviceInUse[] = "recordingDeviceInUse"; | 36 const char kErrorRecordingDeviceInUse[] = "recordingDeviceInUse"; |
| 36 const char kErrorUnableToStart[] = "unableToStart"; | 37 const char kErrorUnableToStart[] = "unableToStart"; |
| 37 const char kErrorRequestDenied[] = "requestDenied"; | 38 const char kErrorRequestDenied[] = "requestDenied"; |
| 38 const char kErrorRequestInProgress[] = "requestInProgress"; | 39 const char kErrorRequestInProgress[] = "requestInProgress"; |
| 39 const char kErrorInvalidOperation[] = "invalidOperation"; | 40 const char kErrorInvalidOperation[] = "invalidOperation"; |
| 40 | 41 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 } | 232 } |
| 232 | 233 |
| 233 void SpeechInputExtensionManager::ResetToIdleState() { | 234 void SpeechInputExtensionManager::ResetToIdleState() { |
| 234 VLOG(1) << "State changed to idle. Deassociating any extensions."; | 235 VLOG(1) << "State changed to idle. Deassociating any extensions."; |
| 235 state_ = kIdle; | 236 state_ = kIdle; |
| 236 extension_id_in_use_.clear(); | 237 extension_id_in_use_.clear(); |
| 237 } | 238 } |
| 238 | 239 |
| 239 void SpeechInputExtensionManager::SetRecognitionResult( | 240 void SpeechInputExtensionManager::SetRecognitionResult( |
| 240 int caller_id, | 241 int caller_id, |
| 241 const content::SpeechInputResult& result) { | 242 const content::SpeechRecognitionResult& result) { |
| 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 243 DCHECK_EQ(caller_id, kSpeechCallerId); | 244 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 244 | 245 |
| 245 // Stopping will start the disassociation with the extension. | 246 // Stopping will start the disassociation with the extension. |
| 246 // Make a copy to report the results to the proper one. | 247 // Make a copy to report the results to the proper one. |
| 247 std::string extension_id = extension_id_in_use_; | 248 std::string extension_id = extension_id_in_use_; |
| 248 ForceStopOnIOThread(); | 249 ForceStopOnIOThread(); |
| 249 | 250 |
| 250 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 251 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 251 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultOnUIThread, | 252 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultOnUIThread, |
| 252 this, result, extension_id)); | 253 this, result, extension_id)); |
| 253 } | 254 } |
| 254 | 255 |
| 255 void SpeechInputExtensionManager::SetRecognitionResultOnUIThread( | 256 void SpeechInputExtensionManager::SetRecognitionResultOnUIThread( |
| 256 const content::SpeechInputResult& result, | 257 const content::SpeechRecognitionResult& result, |
| 257 const std::string& extension_id) { | 258 const std::string& extension_id) { |
| 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 259 | 260 |
| 260 ListValue args; | 261 ListValue args; |
| 261 DictionaryValue* js_event = new DictionaryValue(); | 262 DictionaryValue* js_event = new DictionaryValue(); |
| 262 args.Append(js_event); | 263 args.Append(js_event); |
| 263 | 264 |
| 264 ListValue* js_hypothesis_array = new ListValue(); | 265 ListValue* js_hypothesis_array = new ListValue(); |
| 265 js_event->Set(kHypothesesKey, js_hypothesis_array); | 266 js_event->Set(kHypothesesKey, js_hypothesis_array); |
| 266 | 267 |
| 267 for (size_t i = 0; i < result.hypotheses.size(); ++i) { | 268 for (size_t i = 0; i < result.hypotheses.size(); ++i) { |
| 268 const content::SpeechInputHypothesis& hypothesis = result.hypotheses[i]; | 269 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; |
| 269 | 270 |
| 270 DictionaryValue* js_hypothesis_object = new DictionaryValue(); | 271 DictionaryValue* js_hypothesis_object = new DictionaryValue(); |
| 271 js_hypothesis_array->Append(js_hypothesis_object); | 272 js_hypothesis_array->Append(js_hypothesis_object); |
| 272 | 273 |
| 273 js_hypothesis_object->SetString(kUtteranceKey, | 274 js_hypothesis_object->SetString(kUtteranceKey, |
| 274 UTF16ToUTF8(hypothesis.utterance)); | 275 UTF16ToUTF8(hypothesis.utterance)); |
| 275 js_hypothesis_object->SetDouble(kConfidenceKey, | 276 js_hypothesis_object->SetDouble(kConfidenceKey, |
| 276 hypothesis.confidence); | 277 hypothesis.confidence); |
| 277 } | 278 } |
| 278 | 279 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 } | 329 } |
| 329 | 330 |
| 330 VLOG(1) << "Sending start notification"; | 331 VLOG(1) << "Sending start notification"; |
| 331 content::NotificationService::current()->Notify( | 332 content::NotificationService::current()->Notify( |
| 332 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, | 333 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, |
| 333 content::Source<Profile>(profile_), | 334 content::Source<Profile>(profile_), |
| 334 content::Details<std::string>(&extension_id_in_use_)); | 335 content::Details<std::string>(&extension_id_in_use_)); |
| 335 } | 336 } |
| 336 | 337 |
| 337 void SpeechInputExtensionManager::OnRecognizerError( | 338 void SpeechInputExtensionManager::OnRecognizerError( |
| 338 int caller_id, content::SpeechInputError error) { | 339 int caller_id, content::SpeechRecognitionErrorCode error) { |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 340 DCHECK_EQ(caller_id, kSpeechCallerId); | 341 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 341 VLOG(1) << "OnRecognizerError: " << error; | 342 VLOG(1) << "OnRecognizerError: " << error; |
| 342 | 343 |
| 343 base::AutoLock auto_lock(state_lock_); | 344 base::AutoLock auto_lock(state_lock_); |
| 344 if (state_ == kShutdown) | 345 if (state_ == kShutdown) |
| 345 return; | 346 return; |
| 346 | 347 |
| 347 // Release the recognizer object. | 348 // Release the recognizer object. |
| 348 GetSpeechInputExtensionInterface()->StopRecording(true); | 349 GetSpeechInputExtensionInterface()->StopRecording(true); |
| 349 | 350 |
| 350 std::string event_error_code; | 351 std::string event_error_code; |
| 351 bool report_to_event = true; | 352 bool report_to_event = true; |
| 352 | 353 |
| 353 switch (error) { | 354 switch (error) { |
| 354 case content::SPEECH_INPUT_ERROR_NONE: | 355 case content::SPEECH_RECOGNITION_ERROR_NONE: |
| 355 break; | 356 break; |
| 356 | 357 |
| 357 case content::SPEECH_INPUT_ERROR_AUDIO: | 358 case content::SPEECH_RECOGNITION_ERROR_AUDIO: |
| 358 if (state_ == kStarting) { | 359 if (state_ == kStarting) { |
| 359 event_error_code = kErrorUnableToStart; | 360 event_error_code = kErrorUnableToStart; |
| 360 report_to_event = false; | 361 report_to_event = false; |
| 361 } else { | 362 } else { |
| 362 event_error_code = kErrorCaptureError; | 363 event_error_code = kErrorCaptureError; |
| 363 } | 364 } |
| 364 break; | 365 break; |
| 365 | 366 |
| 366 case content::SPEECH_INPUT_ERROR_NETWORK: | 367 case content::SPEECH_RECOGNITION_ERROR_NETWORK: |
| 367 event_error_code = kErrorNetworkError; | 368 event_error_code = kErrorNetworkError; |
| 368 break; | 369 break; |
| 369 | 370 |
| 370 case content::SPEECH_INPUT_ERROR_BAD_GRAMMAR: | 371 case content::SPEECH_RECOGNITION_ERROR_BAD_GRAMMAR: |
| 371 // No error is returned on invalid language, for example. | 372 // No error is returned on invalid language, for example. |
| 372 // To avoid confusion about when this is would be fired, the invalid | 373 // To avoid confusion about when this is would be fired, the invalid |
| 373 // params error is not being exposed to the onError event. | 374 // params error is not being exposed to the onError event. |
| 374 event_error_code = kErrorUnableToStart; | 375 event_error_code = kErrorUnableToStart; |
| 375 break; | 376 break; |
| 376 | 377 |
| 377 case content::SPEECH_INPUT_ERROR_NO_SPEECH: | 378 case content::SPEECH_RECOGNITION_ERROR_NO_SPEECH: |
| 378 event_error_code = kErrorNoSpeechHeard; | 379 event_error_code = kErrorNoSpeechHeard; |
| 379 break; | 380 break; |
| 380 | 381 |
| 381 case content::SPEECH_INPUT_ERROR_NO_MATCH: | 382 case content::SPEECH_RECOGNITION_ERROR_NO_MATCH: |
| 382 event_error_code = kErrorNoResults; | 383 event_error_code = kErrorNoResults; |
| 383 break; | 384 break; |
| 384 | 385 |
| 385 // The remaining kErrorAborted case should never be returned by the server. | 386 // The remaining kErrorAborted case should never be returned by the server. |
| 386 default: | 387 default: |
| 387 NOTREACHED(); | 388 NOTREACHED(); |
| 388 } | 389 } |
| 389 | 390 |
| 390 if (!event_error_code.empty()) { | 391 if (!event_error_code.empty()) { |
| 391 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 392 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 return; | 545 return; |
| 545 | 546 |
| 546 if (!GetSpeechInputExtensionInterface()->HasAudioInputDevices()) { | 547 if (!GetSpeechInputExtensionInterface()->HasAudioInputDevices()) { |
| 547 BrowserThread::PostTask( | 548 BrowserThread::PostTask( |
| 548 BrowserThread::UI, FROM_HERE, | 549 BrowserThread::UI, FROM_HERE, |
| 549 base::Bind(&SpeechInputExtensionManager::DispatchError, this, | 550 base::Bind(&SpeechInputExtensionManager::DispatchError, this, |
| 550 std::string(kErrorNoRecordingDeviceFound), false)); | 551 std::string(kErrorNoRecordingDeviceFound), false)); |
| 551 return; | 552 return; |
| 552 } | 553 } |
| 553 | 554 |
| 554 if (GetSpeechInputExtensionInterface()->IsRecordingInProcess()) { | 555 if (GetSpeechInputExtensionInterface()->IsCapturingAudio()) { |
| 555 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 556 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 556 base::Bind(&SpeechInputExtensionManager::DispatchError, this, | 557 base::Bind(&SpeechInputExtensionManager::DispatchError, this, |
| 557 std::string(kErrorRecordingDeviceInUse), false)); | 558 std::string(kErrorRecordingDeviceInUse), false)); |
| 558 return; | 559 return; |
| 559 } | 560 } |
| 560 | 561 |
| 561 GetSpeechInputExtensionInterface()->StartRecording( | 562 GetSpeechInputExtensionInterface()->StartRecording( |
| 562 this, context_getter, kSpeechCallerId, language, grammar, | 563 this, context_getter, kSpeechCallerId, language, grammar, |
| 563 filter_profanities); | 564 filter_profanities); |
| 564 } | 565 } |
| 565 | 566 |
| 566 bool SpeechInputExtensionManager::HasAudioInputDevices() { | 567 bool SpeechInputExtensionManager::HasAudioInputDevices() { |
| 567 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 568 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 568 return SpeechInputManager::GetInstance()->HasAudioInputDevices(); | 569 return SpeechRecognitionManager::GetInstance()->HasAudioInputDevices(); |
| 569 } | 570 } |
| 570 | 571 |
| 571 bool SpeechInputExtensionManager::IsRecordingInProcess() { | 572 bool SpeechInputExtensionManager::IsCapturingAudio() { |
| 572 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 573 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 573 return SpeechInputManager::GetInstance()->IsRecordingInProcess(); | 574 return SpeechRecognitionManager::GetInstance()->IsCapturingAudio(); |
| 574 } | 575 } |
| 575 | 576 |
| 576 void SpeechInputExtensionManager::IsRecording( | 577 void SpeechInputExtensionManager::IsRecording( |
| 577 const IsRecordingCallback& callback) { | 578 const IsRecordingCallback& callback) { |
| 578 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 579 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 579 BrowserThread::PostTask( | 580 BrowserThread::PostTask( |
| 580 BrowserThread::IO, FROM_HERE, | 581 BrowserThread::IO, FROM_HERE, |
| 581 base::Bind(&SpeechInputExtensionManager::IsRecordingOnIOThread, | 582 base::Bind(&SpeechInputExtensionManager::IsRecordingOnIOThread, |
| 582 this, callback)); | 583 this, callback)); |
| 583 } | 584 } |
| 584 | 585 |
| 585 void SpeechInputExtensionManager::IsRecordingOnIOThread( | 586 void SpeechInputExtensionManager::IsRecordingOnIOThread( |
| 586 const IsRecordingCallback& callback) { | 587 const IsRecordingCallback& callback) { |
| 587 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 588 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 588 | 589 |
| 589 bool result = GetSpeechInputExtensionInterface()->IsRecordingInProcess(); | 590 bool result = GetSpeechInputExtensionInterface()->IsCapturingAudio(); |
| 590 BrowserThread::PostTask( | 591 BrowserThread::PostTask( |
| 591 BrowserThread::UI, FROM_HERE, | 592 BrowserThread::UI, FROM_HERE, |
| 592 base::Bind(&SpeechInputExtensionManager::IsRecordingOnUIThread, | 593 base::Bind(&SpeechInputExtensionManager::IsRecordingOnUIThread, |
| 593 this, callback, result)); | 594 this, callback, result)); |
| 594 } | 595 } |
| 595 | 596 |
| 596 void SpeechInputExtensionManager::IsRecordingOnUIThread( | 597 void SpeechInputExtensionManager::IsRecordingOnUIThread( |
| 597 const IsRecordingCallback& callback, | 598 const IsRecordingCallback& callback, |
| 598 bool result) { | 599 bool result) { |
| 599 BrowserThread::CurrentlyOn(BrowserThread::UI); | 600 BrowserThread::CurrentlyOn(BrowserThread::UI); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, | 717 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, |
| 717 this, volume)); | 718 this, volume)); |
| 718 } | 719 } |
| 719 | 720 |
| 720 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( | 721 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( |
| 721 float volume) { | 722 float volume) { |
| 722 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 723 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 723 DCHECK(notification_.get()); | 724 DCHECK(notification_.get()); |
| 724 notification_->SetVUMeterVolume(volume); | 725 notification_->SetVUMeterVolume(volume); |
| 725 } | 726 } |
| OLD | NEW |