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/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search/hotword_service.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/keyed_service/content/browser_context_dependency_manager.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
18 #if defined(OS_CHROMEOS)
19 #include "chrome/common/chrome_version_info.h"
22 using content::BrowserContext
;
23 using content::BrowserThread
;
26 HotwordService
* HotwordServiceFactory::GetForProfile(BrowserContext
* context
) {
27 return static_cast<HotwordService
*>(
28 GetInstance()->GetServiceForBrowserContext(context
, true));
32 HotwordServiceFactory
* HotwordServiceFactory::GetInstance() {
33 return Singleton
<HotwordServiceFactory
>::get();
37 bool HotwordServiceFactory::IsServiceAvailable(BrowserContext
* context
) {
38 HotwordService
* hotword_service
= GetForProfile(context
);
39 return hotword_service
&& hotword_service
->IsServiceAvailable();
43 bool HotwordServiceFactory::IsHotwordAllowed(BrowserContext
* context
) {
44 HotwordService
* hotword_service
= GetForProfile(context
);
45 return hotword_service
&& hotword_service
->IsHotwordAllowed();
49 bool HotwordServiceFactory::IsAlwaysOnAvailable() {
50 #if defined(OS_CHROMEOS)
51 chrome::VersionInfo::Channel channel
= chrome::VersionInfo::GetChannel();
52 if ((channel
== chrome::VersionInfo::CHANNEL_UNKNOWN
||
53 channel
== chrome::VersionInfo::CHANNEL_CANARY
||
54 channel
== chrome::VersionInfo::CHANNEL_DEV
) &&
55 HotwordService::IsHotwordHardwareAvailable()) {
59 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
60 return command_line
->HasSwitch(switches::kEnableExperimentalHotwordHardware
);
64 int HotwordServiceFactory::GetCurrentError(BrowserContext
* context
) {
65 HotwordService
* hotword_service
= GetForProfile(context
);
68 return hotword_service
->error_message();
72 bool HotwordServiceFactory::IsMicrophoneAvailable() {
73 return GetInstance()->microphone_available();
77 bool HotwordServiceFactory::IsAudioDeviceStateUpdated() {
78 return GetInstance()->audio_device_state_updated();
81 HotwordServiceFactory::HotwordServiceFactory()
82 : BrowserContextKeyedServiceFactory(
84 BrowserContextDependencyManager::GetInstance()),
85 microphone_available_(false),
86 audio_device_state_updated_(false) {
89 // Register with the device observer list to update the microphone
91 BrowserThread::PostTask(
92 BrowserThread::UI
, FROM_HERE
,
93 base::Bind(&HotwordServiceFactory::InitializeMicrophoneObserver
,
94 base::Unretained(this)));
97 HotwordServiceFactory::~HotwordServiceFactory() {
100 void HotwordServiceFactory::InitializeMicrophoneObserver() {
101 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
104 void HotwordServiceFactory::OnUpdateAudioDevices(
105 const content::MediaStreamDevices
& devices
) {
106 microphone_available_
= !devices
.empty();
107 audio_device_state_updated_
= true;
110 void HotwordServiceFactory::UpdateMicrophoneState() {
111 // In order to trigger the monitor, just call getAudioCaptureDevices.
112 content::MediaStreamDevices devices
=
113 MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices();
116 void HotwordServiceFactory::RegisterProfilePrefs(
117 user_prefs::PrefRegistrySyncable
* prefs
) {
118 prefs
->RegisterBooleanPref(prefs::kHotwordAudioLoggingEnabled
,
120 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
121 prefs
->RegisterStringPref(prefs::kHotwordPreviousLanguage
,
123 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
124 // Per-device settings (do not sync).
125 prefs
->RegisterBooleanPref(prefs::kHotwordSearchEnabled
,
127 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
128 prefs
->RegisterBooleanPref(prefs::kHotwordAlwaysOnSearchEnabled
,
130 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
131 prefs
->RegisterBooleanPref(prefs::kHotwordAlwaysOnNotificationSeen
,
133 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
136 KeyedService
* HotwordServiceFactory::BuildServiceInstanceFor(
137 BrowserContext
* context
) const {
138 return new HotwordService(Profile::FromBrowserContext(context
));