| 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/extensions/speech_input/extension_speech_input_manager.
h" | 5 #include "chrome/browser/speech/extension_speech_input_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/speech_input/extension_speech_input_api_cons
tants.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_dependency_manager.h" | 15 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 15 #include "chrome/browser/profiles/profile_keyed_service.h" | 16 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 16 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | 17 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
| 18 #include "chrome/browser/speech/extension_speech_input_api_constants.h" |
| 19 #include "chrome/browser/speech/extension_speech_input_notification_ui.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
| 22 #include "chrome/common/pref_names.h" |
| 19 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 21 | 25 |
| 22 using content::BrowserThread; | 26 using content::BrowserThread; |
| 23 | 27 |
| 24 using namespace speech_input; | 28 using namespace speech_input; |
| 25 | 29 |
| 26 namespace constants = extension_speech_input_api_constants; | 30 namespace constants = extension_speech_input_api_constants; |
| 27 | 31 |
| 28 namespace { | 32 namespace { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 | 115 |
| 112 ExtensionSpeechInterface::ExtensionSpeechInterface() { | 116 ExtensionSpeechInterface::ExtensionSpeechInterface() { |
| 113 } | 117 } |
| 114 | 118 |
| 115 ExtensionSpeechInterface::~ExtensionSpeechInterface() { | 119 ExtensionSpeechInterface::~ExtensionSpeechInterface() { |
| 116 } | 120 } |
| 117 | 121 |
| 118 ExtensionSpeechInputManager::ExtensionSpeechInputManager(Profile* profile) | 122 ExtensionSpeechInputManager::ExtensionSpeechInputManager(Profile* profile) |
| 119 : profile_(profile), | 123 : profile_(profile), |
| 120 state_(kIdle), | 124 state_(kIdle), |
| 121 speech_interface_(NULL) { | 125 speech_interface_(NULL), |
| 126 notification_(profile) { |
| 122 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 127 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 123 content::Source<Profile>(profile_)); | 128 content::Source<Profile>(profile_)); |
| 124 } | 129 } |
| 125 | 130 |
| 126 ExtensionSpeechInputManager::~ExtensionSpeechInputManager() { | 131 ExtensionSpeechInputManager::~ExtensionSpeechInputManager() { |
| 127 } | 132 } |
| 128 | 133 |
| 129 ExtensionSpeechInputManager* ExtensionSpeechInputManager::GetForProfile( | 134 ExtensionSpeechInputManager* ExtensionSpeechInputManager::GetForProfile( |
| 130 Profile* profile) { | 135 Profile* profile) { |
| 131 ExtensionSpeechInputManagerWrapper *wrapper = | 136 ExtensionSpeechInputManagerWrapper *wrapper = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 150 } | 155 } |
| 151 } | 156 } |
| 152 | 157 |
| 153 void ExtensionSpeechInputManager::ShutdownOnUIThread() { | 158 void ExtensionSpeechInputManager::ShutdownOnUIThread() { |
| 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 155 VLOG(1) << "Profile shutting down."; | 160 VLOG(1) << "Profile shutting down."; |
| 156 | 161 |
| 157 base::AutoLock auto_lock(state_lock_); | 162 base::AutoLock auto_lock(state_lock_); |
| 158 DCHECK(state_ != kShutdown); | 163 DCHECK(state_ != kShutdown); |
| 159 if (state_ != kIdle) { | 164 if (state_ != kIdle) { |
| 165 notification_.Hide(); |
| 160 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 166 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 161 base::Bind(&ExtensionSpeechInputManager::ForceStopOnIOThread, this)); | 167 base::Bind(&ExtensionSpeechInputManager::ForceStopOnIOThread, this)); |
| 162 } | 168 } |
| 163 state_ = kShutdown; | 169 state_ = kShutdown; |
| 164 VLOG(1) << "Entering the shutdown sink state."; | 170 VLOG(1) << "Entering the shutdown sink state."; |
| 165 registrar_.RemoveAll(); | 171 registrar_.RemoveAll(); |
| 166 profile_ = NULL; | 172 profile_ = NULL; |
| 167 } | 173 } |
| 168 | 174 |
| 169 void ExtensionSpeechInputManager::ExtensionUnloaded( | 175 void ExtensionSpeechInputManager::ExtensionUnloaded( |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 267 | 273 |
| 268 base::AutoLock auto_lock(state_lock_); | 274 base::AutoLock auto_lock(state_lock_); |
| 269 if (state_ == kShutdown) | 275 if (state_ == kShutdown) |
| 270 return; | 276 return; |
| 271 | 277 |
| 272 DCHECK_EQ(state_, kStarting); | 278 DCHECK_EQ(state_, kStarting); |
| 273 VLOG(1) << "State changed to recording"; | 279 VLOG(1) << "State changed to recording"; |
| 274 state_ = kRecording; | 280 state_ = kRecording; |
| 275 | 281 |
| 282 const Extension* extension = profile_->GetExtensionService()-> |
| 283 GetExtensionById(extension_id_in_use_, true); |
| 284 DCHECK(extension); |
| 285 |
| 286 bool show_notification = !profile_->GetPrefs()->GetBoolean( |
| 287 prefs::kSpeechInputTrayNotificationShown); |
| 288 |
| 289 notification_.Show(extension, show_notification); |
| 290 |
| 291 if (show_notification) { |
| 292 profile_->GetPrefs()->SetBoolean( |
| 293 prefs::kSpeechInputTrayNotificationShown, true); |
| 294 } |
| 295 |
| 276 VLOG(1) << "Sending start notification"; | 296 VLOG(1) << "Sending start notification"; |
| 277 content::NotificationService::current()->Notify( | 297 content::NotificationService::current()->Notify( |
| 278 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, | 298 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED, |
| 279 content::Source<Profile>(profile_), | 299 content::Source<Profile>(profile_), |
| 280 content::Details<std::string>(&extension_id_in_use_)); | 300 content::Details<std::string>(&extension_id_in_use_)); |
| 281 } | 301 } |
| 282 | 302 |
| 283 void ExtensionSpeechInputManager::OnRecognizerError( | 303 void ExtensionSpeechInputManager::OnRecognizerError( |
| 284 int caller_id, SpeechInputError error) { | 304 int caller_id, SpeechInputError error) { |
| 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 void ExtensionSpeechInputManager::DispatchError( | 415 void ExtensionSpeechInputManager::DispatchError( |
| 396 const std::string& error, bool dispatch_event) { | 416 const std::string& error, bool dispatch_event) { |
| 397 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 398 | 418 |
| 399 std::string extension_id; | 419 std::string extension_id; |
| 400 { | 420 { |
| 401 base::AutoLock auto_lock(state_lock_); | 421 base::AutoLock auto_lock(state_lock_); |
| 402 if (state_ == kShutdown) | 422 if (state_ == kShutdown) |
| 403 return; | 423 return; |
| 404 | 424 |
| 425 if (state_ == kRecording) |
| 426 notification_.Hide(); |
| 427 |
| 405 extension_id = extension_id_in_use_; | 428 extension_id = extension_id_in_use_; |
| 406 ResetToIdleState(); | 429 ResetToIdleState(); |
| 407 | 430 |
| 408 // Will set the error property in the ongoing extension function calls. | 431 // Will set the error property in the ongoing extension function calls. |
| 409 ExtensionError details(extension_id, error); | 432 ExtensionError details(extension_id, error); |
| 410 content::NotificationService::current()->Notify( | 433 content::NotificationService::current()->Notify( |
| 411 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED, | 434 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED, |
| 412 content::Source<Profile>(profile_), | 435 content::Source<Profile>(profile_), |
| 413 content::Details<ExtensionError>(&details)); | 436 content::Details<ExtensionError>(&details)); |
| 414 } | 437 } |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 622 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 600 VLOG(1) << "Stop succeeded (UI thread)"; | 623 VLOG(1) << "Stop succeeded (UI thread)"; |
| 601 | 624 |
| 602 base::AutoLock auto_lock(state_lock_); | 625 base::AutoLock auto_lock(state_lock_); |
| 603 if (state_ == kShutdown) | 626 if (state_ == kShutdown) |
| 604 return; | 627 return; |
| 605 | 628 |
| 606 std::string extension_id = extension_id_in_use_; | 629 std::string extension_id = extension_id_in_use_; |
| 607 ResetToIdleState(); | 630 ResetToIdleState(); |
| 608 | 631 |
| 632 notification_.Hide(); |
| 633 |
| 609 content::NotificationService::current()->Notify( | 634 content::NotificationService::current()->Notify( |
| 610 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, | 635 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED, |
| 611 // Guarded by the state_ == kShutdown check. | 636 // Guarded by the state_ == kShutdown check. |
| 612 content::Source<Profile>(profile_), | 637 content::Source<Profile>(profile_), |
| 613 content::Details<std::string>(&extension_id)); | 638 content::Details<std::string>(&extension_id)); |
| 614 } | 639 } |
| 640 |
| 641 void ExtensionSpeechInputManager::SetInputVolume(int caller_id, |
| 642 float volume, |
| 643 float noise_volume) { |
| 644 DCHECK_EQ(caller_id, kSpeechCallerId); |
| 645 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 646 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 647 base::Bind(&ExtensionSpeechInputManager::SetInputVolumeOnUIThread, |
| 648 this, volume)); |
| 649 } |
| 650 |
| 651 void ExtensionSpeechInputManager::SetInputVolumeOnUIThread( |
| 652 float volume) { |
| 653 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 654 notification_.SetVUMeterVolume(volume); |
| 655 } |
| OLD | NEW |