| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/resource_context.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/common/speech_input_result.h" | 25 #include "content/public/common/speech_input_result.h" |
| 25 | 26 |
| 26 using content::BrowserThread; | 27 using content::BrowserThread; |
| 27 using namespace speech_input; | 28 using speech_input::SpeechRecognizer; |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 const char kErrorNoRecordingDeviceFound[] = "noRecordingDeviceFound"; | 32 const char kErrorNoRecordingDeviceFound[] = "noRecordingDeviceFound"; |
| 32 const char kErrorRecordingDeviceInUse[] = "recordingDeviceInUse"; | 33 const char kErrorRecordingDeviceInUse[] = "recordingDeviceInUse"; |
| 33 const char kErrorUnableToStart[] = "unableToStart"; | 34 const char kErrorUnableToStart[] = "unableToStart"; |
| 34 const char kErrorRequestDenied[] = "requestDenied"; | 35 const char kErrorRequestDenied[] = "requestDenied"; |
| 35 const char kErrorRequestInProgress[] = "requestInProgress"; | 36 const char kErrorRequestInProgress[] = "requestInProgress"; |
| 36 const char kErrorInvalidOperation[] = "invalidOperation"; | 37 const char kErrorInvalidOperation[] = "invalidOperation"; |
| 37 | 38 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 67 SpeechInputExtensionManager* manager() const { return manager_.get(); } | 68 SpeechInputExtensionManager* manager() const { return manager_.get(); } |
| 68 | 69 |
| 69 private: | 70 private: |
| 70 // Methods from ProfileKeyedService. | 71 // Methods from ProfileKeyedService. |
| 71 virtual void Shutdown() OVERRIDE { | 72 virtual void Shutdown() OVERRIDE { |
| 72 manager()->ShutdownOnUIThread(); | 73 manager()->ShutdownOnUIThread(); |
| 73 } | 74 } |
| 74 | 75 |
| 75 scoped_refptr<SpeechInputExtensionManager> manager_; | 76 scoped_refptr<SpeechInputExtensionManager> manager_; |
| 76 }; | 77 }; |
| 77 | |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Factory for SpeechInputExtensionManagers as profile keyed services. | 80 // Factory for SpeechInputExtensionManagers as profile keyed services. |
| 81 class SpeechInputExtensionManager::Factory : public ProfileKeyedServiceFactory { | 81 class SpeechInputExtensionManager::Factory : public ProfileKeyedServiceFactory { |
| 82 public: | 82 public: |
| 83 static void Initialize(); | 83 static void Initialize(); |
| 84 static Factory* GetInstance(); | 84 static Factory* GetInstance(); |
| 85 | 85 |
| 86 SpeechInputExtensionManagerWrapper* GetForProfile(Profile* profile); | 86 SpeechInputExtensionManagerWrapper* GetForProfile(Profile* profile); |
| 87 | 87 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 speech_interface_(NULL) { | 144 speech_interface_(NULL) { |
| 145 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 145 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 146 content::Source<Profile>(profile_)); | 146 content::Source<Profile>(profile_)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 SpeechInputExtensionManager::~SpeechInputExtensionManager() { | 149 SpeechInputExtensionManager::~SpeechInputExtensionManager() { |
| 150 } | 150 } |
| 151 | 151 |
| 152 SpeechInputExtensionManager* SpeechInputExtensionManager::GetForProfile( | 152 SpeechInputExtensionManager* SpeechInputExtensionManager::GetForProfile( |
| 153 Profile* profile) { | 153 Profile* profile) { |
| 154 SpeechInputExtensionManagerWrapper *wrapper = | 154 SpeechInputExtensionManagerWrapper* wrapper = |
| 155 Factory::GetInstance()->GetForProfile(profile); | 155 Factory::GetInstance()->GetForProfile(profile); |
| 156 if (!wrapper) | 156 if (!wrapper) |
| 157 return NULL; | 157 return NULL; |
| 158 return wrapper->manager(); | 158 return wrapper->manager(); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void SpeechInputExtensionManager::InitializeFactory() { | 161 void SpeechInputExtensionManager::InitializeFactory() { |
| 162 Factory::Initialize(); | 162 Factory::Initialize(); |
| 163 } | 163 } |
| 164 | 164 |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 ExtensionError details(extension_id, error); | 455 ExtensionError details(extension_id, error); |
| 456 content::NotificationService::current()->Notify( | 456 content::NotificationService::current()->Notify( |
| 457 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED, | 457 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED, |
| 458 content::Source<Profile>(profile_), | 458 content::Source<Profile>(profile_), |
| 459 content::Details<ExtensionError>(&details)); | 459 content::Details<ExtensionError>(&details)); |
| 460 } | 460 } |
| 461 | 461 |
| 462 // Used for errors that are also reported via the onError event. | 462 // Used for errors that are also reported via the onError event. |
| 463 if (dispatch_event) { | 463 if (dispatch_event) { |
| 464 ListValue args; | 464 ListValue args; |
| 465 DictionaryValue *js_error = new DictionaryValue(); | 465 DictionaryValue* js_error = new DictionaryValue(); |
| 466 args.Append(js_error); | 466 args.Append(js_error); |
| 467 js_error->SetString(kErrorCodeKey, error); | 467 js_error->SetString(kErrorCodeKey, error); |
| 468 std::string json_args; | 468 std::string json_args; |
| 469 base::JSONWriter::Write(&args, false, &json_args); | 469 base::JSONWriter::Write(&args, false, &json_args); |
| 470 DispatchEventToExtension(extension_id, | 470 DispatchEventToExtension(extension_id, |
| 471 kOnErrorEvent, json_args); | 471 kOnErrorEvent, json_args); |
| 472 } | 472 } |
| 473 } | 473 } |
| 474 | 474 |
| 475 bool SpeechInputExtensionManager::Start(const std::string& extension_id, | 475 bool SpeechInputExtensionManager::Start(const std::string& extension_id, |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 base::Bind(&SpeechInputExtensionManager::DispatchError, this, | 539 base::Bind(&SpeechInputExtensionManager::DispatchError, this, |
| 540 std::string(kErrorRecordingDeviceInUse), false)); | 540 std::string(kErrorRecordingDeviceInUse), false)); |
| 541 return; | 541 return; |
| 542 } | 542 } |
| 543 | 543 |
| 544 GetSpeechInputExtensionInterface()->StartRecording(this, context_getter, | 544 GetSpeechInputExtensionInterface()->StartRecording(this, context_getter, |
| 545 kSpeechCallerId, language, grammar, filter_profanities); | 545 kSpeechCallerId, language, grammar, filter_profanities); |
| 546 } | 546 } |
| 547 | 547 |
| 548 bool SpeechInputExtensionManager::HasAudioInputDevices() { | 548 bool SpeechInputExtensionManager::HasAudioInputDevices() { |
| 549 return AudioManager::GetAudioManager()->HasAudioInputDevices(); | 549 return profile_->GetResourceContext().audio_manager()->HasAudioInputDevices(); |
| 550 } | 550 } |
| 551 | 551 |
| 552 bool SpeechInputExtensionManager::IsRecordingInProcess() { | 552 bool SpeechInputExtensionManager::IsRecordingInProcess() { |
| 553 // Thread-safe query. | 553 // Thread-safe query. |
| 554 return AudioManager::GetAudioManager()->IsRecordingInProcess(); | 554 return profile_->GetResourceContext().audio_manager()->IsRecordingInProcess(); |
| 555 } | 555 } |
| 556 | 556 |
| 557 bool SpeechInputExtensionManager::IsRecording() { | 557 bool SpeechInputExtensionManager::IsRecording() { |
| 558 return GetSpeechInputExtensionInterface()->IsRecordingInProcess(); | 558 return GetSpeechInputExtensionInterface()->IsRecordingInProcess(); |
| 559 } | 559 } |
| 560 | 560 |
| 561 void SpeechInputExtensionManager::StartRecording( | 561 void SpeechInputExtensionManager::StartRecording( |
| 562 speech_input::SpeechRecognizerDelegate* delegate, | 562 speech_input::SpeechRecognizerDelegate* delegate, |
| 563 net::URLRequestContextGetter* context_getter, int caller_id, | 563 net::URLRequestContextGetter* context_getter, int caller_id, |
| 564 const std::string& language, const std::string& grammar, | 564 const std::string& language, const std::string& grammar, |
| 565 bool filter_profanities) { | 565 bool filter_profanities) { |
| 566 DCHECK(!recognizer_); | 566 DCHECK(!recognizer_); |
| 567 recognizer_ = new SpeechRecognizer(delegate, caller_id, language, grammar, | 567 recognizer_ = new SpeechRecognizer(delegate, caller_id, language, grammar, |
| 568 context_getter, filter_profanities, "", ""); | 568 context_getter, profile_->GetResourceContext().audio_manager(), |
| 569 filter_profanities, "", ""); |
| 569 recognizer_->StartRecording(); | 570 recognizer_->StartRecording(); |
| 570 } | 571 } |
| 571 | 572 |
| 572 bool SpeechInputExtensionManager::HasValidRecognizer() { | 573 bool SpeechInputExtensionManager::HasValidRecognizer() { |
| 573 // Conditional expression used to avoid a performance warning on windows. | 574 // Conditional expression used to avoid a performance warning on windows. |
| 574 return recognizer_ ? true : false; | 575 return recognizer_ ? true : false; |
| 575 } | 576 } |
| 576 | 577 |
| 577 bool SpeechInputExtensionManager::Stop(const std::string& extension_id, | 578 bool SpeechInputExtensionManager::Stop(const std::string& extension_id, |
| 578 std::string* error) { | 579 std::string* error) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, | 672 base::Bind(&SpeechInputExtensionManager::SetInputVolumeOnUIThread, |
| 672 this, volume)); | 673 this, volume)); |
| 673 } | 674 } |
| 674 | 675 |
| 675 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( | 676 void SpeechInputExtensionManager::SetInputVolumeOnUIThread( |
| 676 float volume) { | 677 float volume) { |
| 677 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 678 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 678 DCHECK(notification_.get()); | 679 DCHECK(notification_.get()); |
| 679 notification_->SetVUMeterVolume(volume); | 680 notification_->SetVUMeterVolume(volume); |
| 680 } | 681 } |
| OLD | NEW |