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 bool HotwordServiceFactory::IsHotwordHardwareAvailable() {
44 // TODO(rlp, dgreid): return has_hotword_hardware()
45 // Fill in once the hardware has the correct interface implemented.
46 // In the meantime, this function can be used to get other parts moving.
51 int HotwordServiceFactory::GetCurrentError(BrowserContext* context) {
52 HotwordService* hotword_service = GetForProfile(context);
55 return hotword_service->error_message();
59 bool HotwordServiceFactory::IsMicrophoneAvailable() {
60 return GetInstance()->microphone_available();
64 bool HotwordServiceFactory::IsAudioDeviceStateUpdated() {
65 return GetInstance()->audio_device_state_updated();
68 HotwordServiceFactory::HotwordServiceFactory()
69 : BrowserContextKeyedServiceFactory(
71 BrowserContextDependencyManager::GetInstance()),
72 microphone_available_(false),
73 audio_device_state_updated_(false) {
76 // Register with the device observer list to update the microphone
78 BrowserThread::PostTask(
79 BrowserThread::UI, FROM_HERE,
80 base::Bind(&HotwordServiceFactory::InitializeMicrophoneObserver,
81 base::Unretained(this)));
84 HotwordServiceFactory::~HotwordServiceFactory() {
87 void HotwordServiceFactory::InitializeMicrophoneObserver() {
88 MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
91 void HotwordServiceFactory::OnUpdateAudioDevices(
92 const content::MediaStreamDevices& devices) {
93 microphone_available_ = !devices.empty();
94 audio_device_state_updated_ = true;
97 void HotwordServiceFactory::UpdateMicrophoneState() {
98 // In order to trigger the monitor, just call getAudioCaptureDevices.
99 content::MediaStreamDevices devices =
100 MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices();
103 void HotwordServiceFactory::RegisterProfilePrefs(
104 user_prefs::PrefRegistrySyncable* prefs) {
105 // Although this is default true, users will not send back information to
106 // Google unless they have opted-in to Hotwording at which point they must
107 // also confirm that they wish this preference to be true or opt out of it.
108 prefs->RegisterBooleanPref(prefs::kHotwordAudioLoggingEnabled,
110 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
111 prefs->RegisterStringPref(prefs::kHotwordPreviousLanguage,
113 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
114 prefs->RegisterBooleanPref(prefs::kHotwordAudioHistoryEnabled,
116 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
117 // Per-device settings (do not sync).
118 prefs->RegisterBooleanPref(prefs::kHotwordSearchEnabled,
120 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
121 prefs->RegisterBooleanPref(prefs::kHotwordAlwaysOnSearchEnabled,
123 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
126 KeyedService* HotwordServiceFactory::BuildServiceInstanceFor(
127 BrowserContext* context) const {
128 return new HotwordService(Profile::FromBrowserContext(context));