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 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 }; | 318 }; |
319 #endif | 319 #endif |
320 | 320 |
321 void HotwordService::HotwordWebstoreInstaller::Shutdown() { | 321 void HotwordService::HotwordWebstoreInstaller::Shutdown() { |
322 AbortInstall(); | 322 AbortInstall(); |
323 } | 323 } |
324 | 324 |
325 HotwordService::HotwordService(Profile* profile) | 325 HotwordService::HotwordService(Profile* profile) |
326 : profile_(profile), | 326 : profile_(profile), |
327 extension_registry_observer_(this), | 327 extension_registry_observer_(this), |
| 328 microphone_available_(false), |
| 329 audio_device_state_updated_(false), |
328 client_(NULL), | 330 client_(NULL), |
329 error_message_(0), | 331 error_message_(0), |
330 reinstall_pending_(false), | 332 reinstall_pending_(false), |
331 training_(false), | 333 training_(false), |
332 weak_factory_(this) { | 334 weak_factory_(this) { |
333 extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile)); | 335 extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile)); |
334 | 336 |
335 // Disable the old extension so it doesn't interfere with the new stuff. | 337 // Disable the old extension so it doesn't interfere with the new stuff. |
336 ExtensionService* extension_service = GetExtensionService(profile_); | 338 ExtensionService* extension_service = GetExtensionService(profile_); |
337 if (extension_service) { | 339 if (extension_service) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 } | 390 } |
389 } | 391 } |
390 | 392 |
391 #if defined(OS_CHROMEOS) | 393 #if defined(OS_CHROMEOS) |
392 if (user_manager::UserManager::IsInitialized()) { | 394 if (user_manager::UserManager::IsInitialized()) { |
393 session_observer_.reset(new HotwordUserSessionStateObserver(this)); | 395 session_observer_.reset(new HotwordUserSessionStateObserver(this)); |
394 user_manager::UserManager::Get()->AddSessionStateObserver( | 396 user_manager::UserManager::Get()->AddSessionStateObserver( |
395 session_observer_.get()); | 397 session_observer_.get()); |
396 } | 398 } |
397 #endif | 399 #endif |
| 400 |
| 401 // Register with the device observer list to update the microphone |
| 402 // availability. |
| 403 content::BrowserThread::PostTask( |
| 404 content::BrowserThread::UI, FROM_HERE, |
| 405 base::Bind(&HotwordService::InitializeMicrophoneObserver, |
| 406 base::Unretained(this))); |
398 } | 407 } |
399 | 408 |
400 HotwordService::~HotwordService() { | 409 HotwordService::~HotwordService() { |
401 #if defined(OS_CHROMEOS) | 410 #if defined(OS_CHROMEOS) |
402 if (user_manager::UserManager::IsInitialized() && session_observer_) { | 411 if (user_manager::UserManager::IsInitialized() && session_observer_) { |
403 user_manager::UserManager::Get()->RemoveSessionStateObserver( | 412 user_manager::UserManager::Get()->RemoveSessionStateObserver( |
404 session_observer_.get()); | 413 session_observer_.get()); |
405 } | 414 } |
406 #endif | 415 #endif |
407 } | 416 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 return; | 467 return; |
459 | 468 |
460 InstallHotwordExtensionFromWebstore(kMaxInstallRetries); | 469 InstallHotwordExtensionFromWebstore(kMaxInstallRetries); |
461 SetPreviousLanguagePref(); | 470 SetPreviousLanguagePref(); |
462 } | 471 } |
463 | 472 |
464 std::string HotwordService::ReinstalledExtensionId() { | 473 std::string HotwordService::ReinstalledExtensionId() { |
465 return extension_misc::kHotwordSharedModuleId; | 474 return extension_misc::kHotwordSharedModuleId; |
466 } | 475 } |
467 | 476 |
| 477 void HotwordService::InitializeMicrophoneObserver() { |
| 478 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this); |
| 479 } |
| 480 |
468 void HotwordService::InstalledFromWebstoreCallback( | 481 void HotwordService::InstalledFromWebstoreCallback( |
469 int num_tries, | 482 int num_tries, |
470 bool success, | 483 bool success, |
471 const std::string& error, | 484 const std::string& error, |
472 extensions::webstore_install::Result result) { | 485 extensions::webstore_install::Result result) { |
473 if (result != extensions::webstore_install::SUCCESS && num_tries) { | 486 if (result != extensions::webstore_install::SUCCESS && num_tries) { |
474 // Try again on failure. | 487 // Try again on failure. |
475 content::BrowserThread::PostDelayedTask( | 488 content::BrowserThread::PostDelayedTask( |
476 content::BrowserThread::UI, | 489 content::BrowserThread::UI, |
477 FROM_HERE, | 490 FROM_HERE, |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 content::WebPluginInfo info; | 620 content::WebPluginInfo info; |
608 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); | 621 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); |
609 if (content::PluginService::GetInstance()->GetPluginInfoByPath(path, &info)) | 622 if (content::PluginService::GetInstance()->GetPluginInfoByPath(path, &info)) |
610 nacl_enabled = plugin_prefs->IsPluginEnabled(info); | 623 nacl_enabled = plugin_prefs->IsPluginEnabled(info); |
611 } | 624 } |
612 if (!nacl_enabled) | 625 if (!nacl_enabled) |
613 error_message_ = IDS_HOTWORD_NACL_DISABLED_ERROR_MESSAGE; | 626 error_message_ = IDS_HOTWORD_NACL_DISABLED_ERROR_MESSAGE; |
614 | 627 |
615 RecordErrorMetrics(error_message_); | 628 RecordErrorMetrics(error_message_); |
616 | 629 |
617 // Determine if the proper audio capabilities exist. | 630 // Determine if the proper audio capabilities exist. The first time this is |
618 // The first time this is called, it probably won't return in time, but that's | 631 // called, it probably won't return in time, but that's why it won't be |
619 // why it won't be included in the error calculation (i.e., the call to | 632 // included in the error calculation. However, this use case is rare and |
620 // IsAudioDeviceStateUpdated()). However, this use case is rare and typically | 633 // typically the devices will be initialized by the time a user goes to |
621 // the devices will be initialized by the time a user goes to settings. | 634 // settings. |
622 bool audio_device_state_updated = | |
623 HotwordServiceFactory::IsAudioDeviceStateUpdated(); | |
624 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); | 635 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); |
625 if (audio_device_state_updated) { | 636 if (audio_device_state_updated_) { |
626 bool audio_capture_allowed = | 637 bool audio_capture_allowed = |
627 profile_->GetPrefs()->GetBoolean(prefs::kAudioCaptureAllowed); | 638 profile_->GetPrefs()->GetBoolean(prefs::kAudioCaptureAllowed); |
628 if (!audio_capture_allowed || | 639 if (!audio_capture_allowed || !microphone_available_) |
629 !HotwordServiceFactory::IsMicrophoneAvailable()) | |
630 error_message_ = IDS_HOTWORD_MICROPHONE_ERROR_MESSAGE; | 640 error_message_ = IDS_HOTWORD_MICROPHONE_ERROR_MESSAGE; |
631 } | 641 } |
632 | 642 |
633 return (error_message_ == 0) && IsHotwordAllowed(); | 643 return (error_message_ == 0) && IsHotwordAllowed(); |
634 } | 644 } |
635 | 645 |
636 bool HotwordService::IsHotwordAllowed() { | 646 bool HotwordService::IsHotwordAllowed() { |
637 std::string group = base::FieldTrialList::FindFullName( | 647 std::string group = base::FieldTrialList::FindFullName( |
638 hotword_internal::kHotwordFieldTrialName); | 648 hotword_internal::kHotwordFieldTrialName); |
639 // Allow hotwording by default, and only disable if the field trial has been | 649 // 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... |
774 void HotwordService::DisableHotwordPreferences() { | 784 void HotwordService::DisableHotwordPreferences() { |
775 if (IsSometimesOnEnabled()) { | 785 if (IsSometimesOnEnabled()) { |
776 profile_->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, false); | 786 profile_->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, false); |
777 } | 787 } |
778 if (IsAlwaysOnEnabled()) { | 788 if (IsAlwaysOnEnabled()) { |
779 profile_->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, | 789 profile_->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, |
780 false); | 790 false); |
781 } | 791 } |
782 } | 792 } |
783 | 793 |
| 794 void HotwordService::OnUpdateAudioDevices( |
| 795 const content::MediaStreamDevices& devices) { |
| 796 bool microphone_was_available = microphone_available_; |
| 797 microphone_available_ = !devices.empty(); |
| 798 audio_device_state_updated_ = true; |
| 799 HotwordPrivateEventService* event_service = |
| 800 BrowserContextKeyedAPIFactory<HotwordPrivateEventService>::Get(profile_); |
| 801 if (event_service && microphone_was_available != microphone_available_) |
| 802 event_service->OnMicrophoneStateChanged(microphone_available_); |
| 803 } |
| 804 |
784 void HotwordService::OnHotwordAlwaysOnSearchEnabledChanged( | 805 void HotwordService::OnHotwordAlwaysOnSearchEnabledChanged( |
785 const std::string& pref_name) { | 806 const std::string& pref_name) { |
786 // If the pref for always on has been changed in some way, that means that | 807 // If the pref for always on has been changed in some way, that means that |
787 // the user is aware of always on (either from settings or another source) | 808 // the user is aware of always on (either from settings or another source) |
788 // so they don't need to be shown the notification. | 809 // so they don't need to be shown the notification. |
789 profile_->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnNotificationSeen, | 810 profile_->GetPrefs()->SetBoolean(prefs::kHotwordAlwaysOnNotificationSeen, |
790 true); | 811 true); |
791 pref_registrar_.Remove(prefs::kHotwordAlwaysOnSearchEnabled); | 812 pref_registrar_.Remove(prefs::kHotwordAlwaysOnSearchEnabled); |
792 } | 813 } |
793 | 814 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
856 // Only support multiple profiles and profile switching in ChromeOS. | 877 // Only support multiple profiles and profile switching in ChromeOS. |
857 if (user_manager::UserManager::IsInitialized()) { | 878 if (user_manager::UserManager::IsInitialized()) { |
858 user_manager::User* user = | 879 user_manager::User* user = |
859 user_manager::UserManager::Get()->GetActiveUser(); | 880 user_manager::UserManager::Get()->GetActiveUser(); |
860 if (user && user->is_profile_created()) | 881 if (user && user->is_profile_created()) |
861 return profile_ == ProfileManager::GetActiveUserProfile(); | 882 return profile_ == ProfileManager::GetActiveUserProfile(); |
862 } | 883 } |
863 #endif | 884 #endif |
864 return true; | 885 return true; |
865 } | 886 } |
OLD | NEW |