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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 224 } |
224 | 225 |
225 void SpeechInputExtensionManager::ResetToIdleState() { | 226 void SpeechInputExtensionManager::ResetToIdleState() { |
226 VLOG(1) << "State changed to idle. Deassociating any extensions."; | 227 VLOG(1) << "State changed to idle. Deassociating any extensions."; |
227 state_ = kIdle; | 228 state_ = kIdle; |
228 extension_id_in_use_.clear(); | 229 extension_id_in_use_.clear(); |
229 } | 230 } |
230 | 231 |
231 void SpeechInputExtensionManager::SetRecognitionResult( | 232 void SpeechInputExtensionManager::SetRecognitionResult( |
232 int caller_id, | 233 int caller_id, |
233 const content::SpeechInputResult& result) { | 234 const content::SpeechRecognitionResult& result) { |
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
235 DCHECK_EQ(caller_id, kSpeechCallerId); | 236 DCHECK_EQ(caller_id, kSpeechCallerId); |
236 | 237 |
237 // Stopping will start the disassociation with the extension. | 238 // Stopping will start the disassociation with the extension. |
238 // Make a copy to report the results to the proper one. | 239 // Make a copy to report the results to the proper one. |
239 std::string extension_id = extension_id_in_use_; | 240 std::string extension_id = extension_id_in_use_; |
240 ForceStopOnIOThread(); | 241 ForceStopOnIOThread(); |
241 | 242 |
242 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 243 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
243 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultOnUIThread, | 244 base::Bind(&SpeechInputExtensionManager::SetRecognitionResultOnUIThread, |
244 this, result, extension_id)); | 245 this, result, extension_id)); |
245 } | 246 } |
246 | 247 |
247 void SpeechInputExtensionManager::SetRecognitionResultOnUIThread( | 248 void SpeechInputExtensionManager::SetRecognitionResultOnUIThread( |
248 const content::SpeechInputResult& result, | 249 const content::SpeechRecognitionResult& result, |
249 const std::string& extension_id) { | 250 const std::string& extension_id) { |
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
251 | 252 |
252 ListValue args; | 253 ListValue args; |
253 DictionaryValue* js_event = new DictionaryValue(); | 254 DictionaryValue* js_event = new DictionaryValue(); |
254 args.Append(js_event); | 255 args.Append(js_event); |
255 | 256 |
256 ListValue* js_hypothesis_array = new ListValue(); | 257 ListValue* js_hypothesis_array = new ListValue(); |
257 js_event->Set(kHypothesesKey, js_hypothesis_array); | 258 js_event->Set(kHypothesesKey, js_hypothesis_array); |
258 | 259 |
259 for (size_t i = 0; i < result.hypotheses.size(); ++i) { | 260 for (size_t i = 0; i < result.hypotheses.size(); ++i) { |
260 const content::SpeechInputHypothesis& hypothesis = result.hypotheses[i]; | 261 const SpeechRecognitionHypothesis& hypothesis = result.hypotheses[i]; |
261 | 262 |
262 DictionaryValue* js_hypothesis_object = new DictionaryValue(); | 263 DictionaryValue* js_hypothesis_object = new DictionaryValue(); |
263 js_hypothesis_array->Append(js_hypothesis_object); | 264 js_hypothesis_array->Append(js_hypothesis_object); |
264 | 265 |
265 js_hypothesis_object->SetString(kUtteranceKey, | 266 js_hypothesis_object->SetString(kUtteranceKey, |
266 UTF16ToUTF8(hypothesis.utterance)); | 267 UTF16ToUTF8(hypothesis.utterance)); |
267 js_hypothesis_object->SetDouble(kConfidenceKey, | 268 js_hypothesis_object->SetDouble(kConfidenceKey, |
268 hypothesis.confidence); | 269 hypothesis.confidence); |
269 } | 270 } |
270 | 271 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 } | 321 } |
321 | 322 |
322 VLOG(1) << "Sending start notification"; | 323 VLOG(1) << "Sending start notification"; |
323 content::NotificationService::current()->Notify( | 324 content::NotificationService::current()->Notify( |
324 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, | 325 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, |
325 content::Source<Profile>(profile_), | 326 content::Source<Profile>(profile_), |
326 content::Details<std::string>(&extension_id_in_use_)); | 327 content::Details<std::string>(&extension_id_in_use_)); |
327 } | 328 } |
328 | 329 |
329 void SpeechInputExtensionManager::OnRecognizerError( | 330 void SpeechInputExtensionManager::OnRecognizerError( |
330 int caller_id, content::SpeechInputError error) { | 331 int caller_id, content::SpeechRecognitionErrorCode error) { |
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 332 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
332 DCHECK_EQ(caller_id, kSpeechCallerId); | 333 DCHECK_EQ(caller_id, kSpeechCallerId); |
333 VLOG(1) << "OnRecognizerError: " << error; | 334 VLOG(1) << "OnRecognizerError: " << error; |
334 | 335 |
335 base::AutoLock auto_lock(state_lock_); | 336 base::AutoLock auto_lock(state_lock_); |
336 if (state_ == kShutdown) | 337 if (state_ == kShutdown) |
337 return; | 338 return; |
338 | 339 |
339 // Release the recognizer object. | 340 // Release the recognizer object. |
340 GetSpeechInputExtensionInterface()->StopRecording(true); | 341 GetSpeechInputExtensionInterface()->StopRecording(true); |
341 | 342 |
342 std::string event_error_code; | 343 std::string event_error_code; |
343 bool report_to_event = true; | 344 bool report_to_event = true; |
344 | 345 |
345 switch (error) { | 346 switch (error) { |
346 case content::SPEECH_INPUT_ERROR_NONE: | 347 case content::SPEECH_RECOGNITION_ERROR_NONE: |
347 break; | 348 break; |
348 | 349 |
349 case content::SPEECH_INPUT_ERROR_AUDIO: | 350 case content::SPEECH_RECOGNITION_ERROR_AUDIO: |
350 if (state_ == kStarting) { | 351 if (state_ == kStarting) { |
351 event_error_code = kErrorUnableToStart; | 352 event_error_code = kErrorUnableToStart; |
352 report_to_event = false; | 353 report_to_event = false; |
353 } else { | 354 } else { |
354 event_error_code = kErrorCaptureError; | 355 event_error_code = kErrorCaptureError; |
355 } | 356 } |
356 break; | 357 break; |
357 | 358 |
358 case content::SPEECH_INPUT_ERROR_NETWORK: | 359 case content::SPEECH_RECOGNITION_ERROR_NETWORK: |
359 event_error_code = kErrorNetworkError; | 360 event_error_code = kErrorNetworkError; |
360 break; | 361 break; |
361 | 362 |
362 case content::SPEECH_INPUT_ERROR_BAD_GRAMMAR: | 363 case content::SPEECH_RECOGNITION_ERROR_BAD_GRAMMAR: |
363 // No error is returned on invalid language, for example. | 364 // No error is returned on invalid language, for example. |
364 // To avoid confusion about when this is would be fired, the invalid | 365 // To avoid confusion about when this is would be fired, the invalid |
365 // params error is not being exposed to the onError event. | 366 // params error is not being exposed to the onError event. |
366 event_error_code = kErrorUnableToStart; | 367 event_error_code = kErrorUnableToStart; |
367 break; | 368 break; |
368 | 369 |
369 case content::SPEECH_INPUT_ERROR_NO_SPEECH: | 370 case content::SPEECH_RECOGNITION_ERROR_NO_SPEECH: |
370 event_error_code = kErrorNoSpeechHeard; | 371 event_error_code = kErrorNoSpeechHeard; |
371 break; | 372 break; |
372 | 373 |
373 case content::SPEECH_INPUT_ERROR_NO_MATCH: | 374 case content::SPEECH_RECOGNITION_ERROR_NO_MATCH: |
374 event_error_code = kErrorNoResults; | 375 event_error_code = kErrorNoResults; |
375 break; | 376 break; |
376 | 377 |
377 // The remaining kErrorAborted case should never be returned by the server. | 378 // The remaining kErrorAborted case should never be returned by the server. |
378 default: | 379 default: |
379 NOTREACHED(); | 380 NOTREACHED(); |
380 } | 381 } |
381 | 382 |
382 if (!event_error_code.empty()) { | 383 if (!event_error_code.empty()) { |
383 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 384 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 return; | 537 return; |
537 | 538 |
538 if (!GetSpeechInputExtensionInterface()->HasAudioInputDevices()) { | 539 if (!GetSpeechInputExtensionInterface()->HasAudioInputDevices()) { |
539 BrowserThread::PostTask( | 540 BrowserThread::PostTask( |
540 BrowserThread::UI, FROM_HERE, | 541 BrowserThread::UI, FROM_HERE, |
541 base::Bind(&SpeechInputExtensionManager::DispatchError, this, | 542 base::Bind(&SpeechInputExtensionManager::DispatchError, this, |
542 std::string(kErrorNoRecordingDeviceFound), false)); | 543 std::string(kErrorNoRecordingDeviceFound), false)); |
543 return; | 544 return; |
544 } | 545 } |
545 | 546 |
546 if (GetSpeechInputExtensionInterface()->IsRecordingInProcess()) { | 547 if (GetSpeechInputExtensionInterface()->IsCapturingAudio()) { |
547 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 548 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
548 base::Bind(&SpeechInputExtensionManager::DispatchError, this, | 549 base::Bind(&SpeechInputExtensionManager::DispatchError, this, |
549 std::string(kErrorRecordingDeviceInUse), false)); | 550 std::string(kErrorRecordingDeviceInUse), false)); |
550 return; | 551 return; |
551 } | 552 } |
552 | 553 |
553 GetSpeechInputExtensionInterface()->StartRecording( | 554 GetSpeechInputExtensionInterface()->StartRecording( |
554 this, context_getter, kSpeechCallerId, language, grammar, | 555 this, context_getter, kSpeechCallerId, language, grammar, |
555 filter_profanities); | 556 filter_profanities); |
556 } | 557 } |
557 | 558 |
558 bool SpeechInputExtensionManager::HasAudioInputDevices() { | 559 bool SpeechInputExtensionManager::HasAudioInputDevices() { |
559 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 560 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
560 return SpeechInputManager::GetInstance()->HasAudioInputDevices(); | 561 return SpeechRecognitionManager::GetInstance()->HasAudioInputDevices(); |
561 } | 562 } |
562 | 563 |
563 bool SpeechInputExtensionManager::IsRecordingInProcess() { | 564 bool SpeechInputExtensionManager::IsCapturingAudio() { |
564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 565 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
565 return SpeechInputManager::GetInstance()->IsRecordingInProcess(); | 566 return SpeechRecognitionManager::GetInstance()->IsCapturingAudio(); |
566 } | 567 } |
567 | 568 |
568 void SpeechInputExtensionManager::IsRecording( | 569 void SpeechInputExtensionManager::IsRecording( |
569 const IsRecordingCallback& callback) { | 570 const IsRecordingCallback& callback) { |
570 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 571 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
571 BrowserThread::PostTask( | 572 BrowserThread::PostTask( |
572 BrowserThread::IO, FROM_HERE, | 573 BrowserThread::IO, FROM_HERE, |
573 base::Bind(&SpeechInputExtensionManager::IsRecordingOnIOThread, | 574 base::Bind(&SpeechInputExtensionManager::IsRecordingOnIOThread, |
574 this, callback)); | 575 this, callback)); |
575 } | 576 } |
576 | 577 |
577 void SpeechInputExtensionManager::IsRecordingOnIOThread( | 578 void SpeechInputExtensionManager::IsRecordingOnIOThread( |
578 const IsRecordingCallback& callback) { | 579 const IsRecordingCallback& callback) { |
579 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 580 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
580 | 581 |
581 bool result = GetSpeechInputExtensionInterface()->IsRecordingInProcess(); | 582 bool result = GetSpeechInputExtensionInterface()->IsCapturingAudio(); |
582 BrowserThread::PostTask( | 583 BrowserThread::PostTask( |
583 BrowserThread::UI, FROM_HERE, | 584 BrowserThread::UI, FROM_HERE, |
584 base::Bind(&SpeechInputExtensionManager::IsRecordingOnUIThread, | 585 base::Bind(&SpeechInputExtensionManager::IsRecordingOnUIThread, |
585 this, callback, result)); | 586 this, callback, result)); |
586 } | 587 } |
587 | 588 |
588 void SpeechInputExtensionManager::IsRecordingOnUIThread( | 589 void SpeechInputExtensionManager::IsRecordingOnUIThread( |
589 const IsRecordingCallback& callback, | 590 const IsRecordingCallback& callback, |
590 bool result) { | 591 bool result) { |
591 BrowserThread::CurrentlyOn(BrowserThread::UI); | 592 BrowserThread::CurrentlyOn(BrowserThread::UI); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, | 709 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, |
709 this, volume)); | 710 this, volume)); |
710 } | 711 } |
711 | 712 |
712 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( | 713 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( |
713 float volume) { | 714 float volume) { |
714 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 715 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
715 DCHECK(notification_.get()); | 716 DCHECK(notification_.get()); |
716 notification_->SetVUMeterVolume(volume); | 717 notification_->SetVUMeterVolume(volume); |
717 } | 718 } |
OLD | NEW |