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.
5 #include "chrome/browser/search/hotword_service_factory.h"
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"
16 using content::BrowserContext
;
17 using content::BrowserThread
;
20 HotwordService
* HotwordServiceFactory::GetForProfile(BrowserContext
* context
) {
21 return static_cast<HotwordService
*>(
22 GetInstance()->GetServiceForBrowserContext(context
, true));
26 HotwordServiceFactory
* HotwordServiceFactory::GetInstance() {
27 return Singleton
<HotwordServiceFactory
>::get();
31 bool HotwordServiceFactory::IsServiceAvailable(BrowserContext
* context
) {
32 HotwordService
* hotword_service
= GetForProfile(context
);
33 return hotword_service
&& hotword_service
->IsServiceAvailable();
37 bool HotwordServiceFactory::IsHotwordAllowed(BrowserContext
* context
) {
38 HotwordService
* hotword_service
= GetForProfile(context
);
39 return hotword_service
&& hotword_service
->IsHotwordAllowed();
43 int HotwordServiceFactory::GetCurrentError(BrowserContext
* context
) {
44 HotwordService
* hotword_service
= GetForProfile(context
);
47 return hotword_service
->error_message();
51 bool HotwordServiceFactory::IsMicrophoneAvailable() {
52 return GetInstance()->microphone_available();
56 bool HotwordServiceFactory::IsAudioDeviceStateUpdated() {
57 return GetInstance()->audio_device_state_updated();
60 HotwordServiceFactory::HotwordServiceFactory()
61 : BrowserContextKeyedServiceFactory(
63 BrowserContextDependencyManager::GetInstance()),
64 microphone_available_(false),
65 audio_device_state_updated_(false) {
68 // Register with the device observer list to update the microphone
70 BrowserThread::PostTask(
71 BrowserThread::UI
, FROM_HERE
,
72 base::Bind(&HotwordServiceFactory::InitializeMicrophoneObserver
,
73 base::Unretained(this)));
76 HotwordServiceFactory::~HotwordServiceFactory() {
79 void HotwordServiceFactory::InitializeMicrophoneObserver() {
80 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
83 void HotwordServiceFactory::OnUpdateAudioDevices(
84 const content::MediaStreamDevices
& devices
) {
85 microphone_available_
= !devices
.empty();
86 audio_device_state_updated_
= true;
89 void HotwordServiceFactory::UpdateMicrophoneState() {
90 // In order to trigger the monitor, just call getAudioCaptureDevices.
91 content::MediaStreamDevices devices
=
92 MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices();
95 void HotwordServiceFactory::RegisterProfilePrefs(
96 user_prefs::PrefRegistrySyncable
* prefs
) {
97 prefs
->RegisterBooleanPref(prefs::kHotwordSearchEnabled
,
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
,
105 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
106 prefs
->RegisterStringPref(prefs::kHotwordPreviousLanguage
,
108 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
109 prefs
->RegisterBooleanPref(prefs::kHotwordAlwaysOnSearchEnabled
,
111 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
114 KeyedService
* HotwordServiceFactory::BuildServiceInstanceFor(
115 BrowserContext
* context
) const {
116 return new HotwordService(Profile::FromBrowserContext(context
));