OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/search/hotword_service.h" | 5 #include "chrome/browser/search/hotword_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 }; | 316 }; |
317 #endif | 317 #endif |
318 | 318 |
319 void HotwordService::HotwordWebstoreInstaller::Shutdown() { | 319 void HotwordService::HotwordWebstoreInstaller::Shutdown() { |
320 AbortInstall(); | 320 AbortInstall(); |
321 } | 321 } |
322 | 322 |
323 HotwordService::HotwordService(Profile* profile) | 323 HotwordService::HotwordService(Profile* profile) |
324 : profile_(profile), | 324 : profile_(profile), |
325 extension_registry_observer_(this), | 325 extension_registry_observer_(this), |
| 326 microphone_available_(false), |
| 327 audio_device_state_updated_(false), |
326 client_(NULL), | 328 client_(NULL), |
327 error_message_(0), | 329 error_message_(0), |
328 reinstall_pending_(false), | 330 reinstall_pending_(false), |
329 training_(false), | 331 training_(false), |
330 weak_factory_(this) { | 332 weak_factory_(this) { |
331 extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile)); | 333 extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile)); |
332 | 334 |
333 // Disable the old extension so it doesn't interfere with the new stuff. | 335 // Disable the old extension so it doesn't interfere with the new stuff. |
334 ExtensionService* extension_service = GetExtensionService(profile_); | 336 ExtensionService* extension_service = GetExtensionService(profile_); |
335 if (extension_service) { | 337 if (extension_service) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 } | 381 } |
380 } | 382 } |
381 | 383 |
382 #if defined(OS_CHROMEOS) | 384 #if defined(OS_CHROMEOS) |
383 if (user_manager::UserManager::IsInitialized()) { | 385 if (user_manager::UserManager::IsInitialized()) { |
384 session_observer_.reset(new HotwordUserSessionStateObserver(this)); | 386 session_observer_.reset(new HotwordUserSessionStateObserver(this)); |
385 user_manager::UserManager::Get()->AddSessionStateObserver( | 387 user_manager::UserManager::Get()->AddSessionStateObserver( |
386 session_observer_.get()); | 388 session_observer_.get()); |
387 } | 389 } |
388 #endif | 390 #endif |
| 391 |
| 392 // Register with the device observer list to update the microphone |
| 393 // availability. |
| 394 content::BrowserThread::PostTask( |
| 395 content::BrowserThread::UI, FROM_HERE, |
| 396 base::Bind(&HotwordService::InitializeMicrophoneObserver, |
| 397 base::Unretained(this))); |
389 } | 398 } |
390 | 399 |
391 HotwordService::~HotwordService() { | 400 HotwordService::~HotwordService() { |
392 #if defined(OS_CHROMEOS) | 401 #if defined(OS_CHROMEOS) |
393 if (user_manager::UserManager::IsInitialized() && session_observer_) { | 402 if (user_manager::UserManager::IsInitialized() && session_observer_) { |
394 user_manager::UserManager::Get()->RemoveSessionStateObserver( | 403 user_manager::UserManager::Get()->RemoveSessionStateObserver( |
395 session_observer_.get()); | 404 session_observer_.get()); |
396 } | 405 } |
397 #endif | 406 #endif |
398 } | 407 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 return; | 458 return; |
450 | 459 |
451 InstallHotwordExtensionFromWebstore(kMaxInstallRetries); | 460 InstallHotwordExtensionFromWebstore(kMaxInstallRetries); |
452 SetPreviousLanguagePref(); | 461 SetPreviousLanguagePref(); |
453 } | 462 } |
454 | 463 |
455 std::string HotwordService::ReinstalledExtensionId() { | 464 std::string HotwordService::ReinstalledExtensionId() { |
456 return extension_misc::kHotwordSharedModuleId; | 465 return extension_misc::kHotwordSharedModuleId; |
457 } | 466 } |
458 | 467 |
| 468 void HotwordService::InitializeMicrophoneObserver() { |
| 469 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this); |
| 470 } |
| 471 |
459 void HotwordService::InstalledFromWebstoreCallback( | 472 void HotwordService::InstalledFromWebstoreCallback( |
460 int num_tries, | 473 int num_tries, |
461 bool success, | 474 bool success, |
462 const std::string& error, | 475 const std::string& error, |
463 extensions::webstore_install::Result result) { | 476 extensions::webstore_install::Result result) { |
464 if (result != extensions::webstore_install::SUCCESS && num_tries) { | 477 if (result != extensions::webstore_install::SUCCESS && num_tries) { |
465 // Try again on failure. | 478 // Try again on failure. |
466 content::BrowserThread::PostDelayedTask( | 479 content::BrowserThread::PostDelayedTask( |
467 content::BrowserThread::UI, | 480 content::BrowserThread::UI, |
468 FROM_HERE, | 481 FROM_HERE, |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 content::WebPluginInfo info; | 611 content::WebPluginInfo info; |
599 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); | 612 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); |
600 if (content::PluginService::GetInstance()->GetPluginInfoByPath(path, &info)) | 613 if (content::PluginService::GetInstance()->GetPluginInfoByPath(path, &info)) |
601 nacl_enabled = plugin_prefs->IsPluginEnabled(info); | 614 nacl_enabled = plugin_prefs->IsPluginEnabled(info); |
602 } | 615 } |
603 if (!nacl_enabled) | 616 if (!nacl_enabled) |
604 error_message_ = IDS_HOTWORD_NACL_DISABLED_ERROR_MESSAGE; | 617 error_message_ = IDS_HOTWORD_NACL_DISABLED_ERROR_MESSAGE; |
605 | 618 |
606 RecordErrorMetrics(error_message_); | 619 RecordErrorMetrics(error_message_); |
607 | 620 |
608 // Determine if the proper audio capabilities exist. | 621 // Determine if the proper audio capabilities exist. The first time this is |
609 // The first time this is called, it probably won't return in time, but that's | 622 // called, it probably won't return in time, but that's why it won't be |
610 // why it won't be included in the error calculation (i.e., the call to | 623 // included in the error calculation. However, this use case is rare and |
611 // IsAudioDeviceStateUpdated()). However, this use case is rare and typically | 624 // typically the devices will be initialized by the time a user goes to |
612 // the devices will be initialized by the time a user goes to settings. | 625 // settings. |
613 bool audio_device_state_updated = | |
614 HotwordServiceFactory::IsAudioDeviceStateUpdated(); | |
615 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); | 626 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); |
616 if (audio_device_state_updated) { | 627 if (audio_device_state_updated_) { |
617 bool audio_capture_allowed = | 628 bool audio_capture_allowed = |
618 profile_->GetPrefs()->GetBoolean(prefs::kAudioCaptureAllowed); | 629 profile_->GetPrefs()->GetBoolean(prefs::kAudioCaptureAllowed); |
619 if (!audio_capture_allowed || | 630 if (!audio_capture_allowed || !microphone_available_) |
620 !HotwordServiceFactory::IsMicrophoneAvailable()) | |
621 error_message_ = IDS_HOTWORD_MICROPHONE_ERROR_MESSAGE; | 631 error_message_ = IDS_HOTWORD_MICROPHONE_ERROR_MESSAGE; |
622 } | 632 } |
623 | 633 |
624 return (error_message_ == 0) && IsHotwordAllowed(); | 634 return (error_message_ == 0) && IsHotwordAllowed(); |
625 } | 635 } |
626 | 636 |
627 bool HotwordService::IsHotwordAllowed() { | 637 bool HotwordService::IsHotwordAllowed() { |
628 std::string group = base::FieldTrialList::FindFullName( | 638 std::string group = base::FieldTrialList::FindFullName( |
629 hotword_internal::kHotwordFieldTrialName); | 639 hotword_internal::kHotwordFieldTrialName); |
630 // Allow hotwording by default, and only disable if the field trial has been | 640 // Allow hotwording by default, and only disable if the field trial has been |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 void HotwordService::DisableHotwordPreferences() { | 775 void HotwordService::DisableHotwordPreferences() { |
766 if (IsSometimesOnEnabled()) { | 776 if (IsSometimesOnEnabled()) { |
767 profile_->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, false); | 777 profile_->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, false); |
768 } | 778 } |
769 if (IsAlwaysOnEnabled()) { | 779 if (IsAlwaysOnEnabled()) { |
770 profile_->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, | 780 profile_->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, |
771 false); | 781 false); |
772 } | 782 } |
773 } | 783 } |
774 | 784 |
| 785 void HotwordService::OnUpdateAudioDevices( |
| 786 const content::MediaStreamDevices& devices) { |
| 787 bool microphone_was_available = microphone_available_; |
| 788 microphone_available_ = !devices.empty(); |
| 789 audio_device_state_updated_ = true; |
| 790 HotwordPrivateEventService* event_service = |
| 791 BrowserContextKeyedAPIFactory<HotwordPrivateEventService>::Get(profile_); |
| 792 if (event_service && microphone_was_available != microphone_available_) |
| 793 event_service->OnMicrophoneStateChanged(microphone_available_); |
| 794 } |
| 795 |
775 void HotwordService::OnHotwordAlwaysOnSearchEnabledChanged( | 796 void HotwordService::OnHotwordAlwaysOnSearchEnabledChanged( |
776 const std::string& pref_name) { | 797 const std::string& pref_name) { |
777 // If the pref for always on has been changed in some way, that means that | 798 // If the pref for always on has been changed in some way, that means that |
778 // the user is aware of always on (either from settings or another source) | 799 // the user is aware of always on (either from settings or another source) |
779 // so they don't need to be shown the notification. | 800 // so they don't need to be shown the notification. |
780 profile_->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnNotificationSeen, | 801 profile_->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnNotificationSeen, |
781 true); | 802 true); |
782 pref_registrar_.Remove(prefs::kHotwordAlwaysOnSearchEnabled); | 803 pref_registrar_.Remove(prefs::kHotwordAlwaysOnSearchEnabled); |
783 } | 804 } |
784 | 805 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 // Only support multiple profiles and profile switching in ChromeOS. | 868 // Only support multiple profiles and profile switching in ChromeOS. |
848 if (user_manager::UserManager::IsInitialized()) { | 869 if (user_manager::UserManager::IsInitialized()) { |
849 user_manager::User* user = | 870 user_manager::User* user = |
850 user_manager::UserManager::Get()->GetActiveUser(); | 871 user_manager::UserManager::Get()->GetActiveUser(); |
851 if (user && user->is_profile_created()) | 872 if (user && user->is_profile_created()) |
852 return profile_ == ProfileManager::GetActiveUserProfile(); | 873 return profile_ == ProfileManager::GetActiveUserProfile(); |
853 } | 874 } |
854 #endif | 875 #endif |
855 return true; | 876 return true; |
856 } | 877 } |
OLD | NEW |