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 3// found in the LICENSE file. 4 5#include "chrome/browser/search/hotword_service_factory.h" 6 7#include "base/prefs/pref_service.h" 8#include "chrome/browser/profiles/profile.h" 9#include "chrome/browser/search/hotword_service.h" 10#include "chrome/common/pref_names.h" 11#include "components/keyed_service/content/browser_context_dependency_manager.h" 12#include "components/pref_registry/pref_registry_syncable.h" 13#include "content/public/browser/browser_context.h" 14#include "content/public/browser/browser_thread.h" 15 16using content::BrowserContext; 17using content::BrowserThread; 18 19// static 20HotwordService* HotwordServiceFactory::GetForProfile(BrowserContext* context) { 21 return static_cast<HotwordService*>( 22 GetInstance()->GetServiceForBrowserContext(context, true)); 23} 24 25// static 26HotwordServiceFactory* HotwordServiceFactory::GetInstance() { 27 return Singleton<HotwordServiceFactory>::get(); 28} 29 30// static 31bool HotwordServiceFactory::IsServiceAvailable(BrowserContext* context) { 32 HotwordService* hotword_service = GetForProfile(context); 33 return hotword_service && hotword_service->IsServiceAvailable(); 34} 35 36// static 37bool HotwordServiceFactory::IsHotwordAllowed(BrowserContext* context) { 38 HotwordService* hotword_service = GetForProfile(context); 39 return hotword_service && hotword_service->IsHotwordAllowed(); 40} 41 42// static 43int HotwordServiceFactory::GetCurrentError(BrowserContext* context) { 44 HotwordService* hotword_service = GetForProfile(context); 45 if (!hotword_service) 46 return 0; 47 return hotword_service->error_message(); 48} 49 50// static 51bool HotwordServiceFactory::IsMicrophoneAvailable() { 52 return GetInstance()->microphone_available(); 53} 54 55HotwordServiceFactory::HotwordServiceFactory() 56 : BrowserContextKeyedServiceFactory( 57 "HotwordService", 58 BrowserContextDependencyManager::GetInstance()), 59 microphone_available_(false) { 60 // No dependencies. 61 62 // Register with the device observer list to update the microphone 63 // availability. 64 BrowserThread::PostTask( 65 BrowserThread::UI, FROM_HERE, 66 base::Bind(&HotwordServiceFactory::InitializeMicrophoneObserver, 67 base::Unretained(this))); 68} 69 70HotwordServiceFactory::~HotwordServiceFactory() { 71} 72 73void HotwordServiceFactory::InitializeMicrophoneObserver() { 74 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this); 75} 76 77void HotwordServiceFactory::OnUpdateAudioDevices( 78 const content::MediaStreamDevices& devices) { 79 microphone_available_ = !devices.empty(); 80} 81 82void HotwordServiceFactory::UpdateMicrophoneState() { 83 // In order to trigger the monitor, just call getAudioCaptureDevices. 84 content::MediaStreamDevices devices = 85 MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices(); 86 87 // If the monitor had not previously been started, there may be 0 devices 88 // even if that is not accurate. However, we can update the microphone 89 // availability state now. Either the number of devices will be correct or 90 // we know that the call above will start the monitor and the microphone 91 // state will be updated very soon and call OnUpdateAudioDevices. 92 OnUpdateAudioDevices(devices); 93} 94 95void HotwordServiceFactory::RegisterProfilePrefs( 96 user_prefs::PrefRegistrySyncable* prefs) { 97 prefs->RegisterBooleanPref(prefs::kHotwordSearchEnabled, 98 false, 99 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 100 // Although this is default true, users will not send back information to 101 // Google unless they have opted-in to Hotwording at which point they must 102 // also confirm that they wish this preference to be true or opt out of it. 103 prefs->RegisterBooleanPref(prefs::kHotwordAudioLoggingEnabled, 104 true, 105 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 106 prefs->RegisterStringPref(prefs::kHotwordPreviousLanguage, 107 std::string(), 108 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 109 prefs->RegisterBooleanPref(prefs::kHotwordAlwaysOnSearchEnabled, 110 false, 111 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 112} 113 114KeyedService* HotwordServiceFactory::BuildServiceInstanceFor( 115 BrowserContext* context) const { 116 return new HotwordService(Profile::FromBrowserContext(context)); 117} 118