Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search / hotword_service_factory.cc
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
16 using content::BrowserContext;
17 using content::BrowserThread;
18
19 // static
20 HotwordService* HotwordServiceFactory::GetForProfile(BrowserContext* context) {
21   return static_cast<HotwordService*>(
22       GetInstance()->GetServiceForBrowserContext(context, true));
23 }
24
25 // static
26 HotwordServiceFactory* HotwordServiceFactory::GetInstance() {
27   return Singleton<HotwordServiceFactory>::get();
28 }
29
30 // static
31 bool HotwordServiceFactory::IsServiceAvailable(BrowserContext* context) {
32   HotwordService* hotword_service = GetForProfile(context);
33   return hotword_service && hotword_service->IsServiceAvailable();
34 }
35
36 // static
37 bool HotwordServiceFactory::IsHotwordAllowed(BrowserContext* context) {
38   HotwordService* hotword_service = GetForProfile(context);
39   return hotword_service && hotword_service->IsHotwordAllowed();
40 }
41
42 // static
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.
47   return true;
48 }
49
50 // static
51 int HotwordServiceFactory::GetCurrentError(BrowserContext* context) {
52   HotwordService* hotword_service = GetForProfile(context);
53   if (!hotword_service)
54     return 0;
55   return hotword_service->error_message();
56 }
57
58 // static
59 bool HotwordServiceFactory::IsMicrophoneAvailable() {
60   return GetInstance()->microphone_available();
61 }
62
63 // static
64 bool HotwordServiceFactory::IsAudioDeviceStateUpdated() {
65   return GetInstance()->audio_device_state_updated();
66 }
67
68 HotwordServiceFactory::HotwordServiceFactory()
69     : BrowserContextKeyedServiceFactory(
70         "HotwordService",
71         BrowserContextDependencyManager::GetInstance()),
72       microphone_available_(false),
73       audio_device_state_updated_(false) {
74   // No dependencies.
75
76   // Register with the device observer list to update the microphone
77   // availability.
78   BrowserThread::PostTask(
79       BrowserThread::UI, FROM_HERE,
80       base::Bind(&HotwordServiceFactory::InitializeMicrophoneObserver,
81                  base::Unretained(this)));
82 }
83
84 HotwordServiceFactory::~HotwordServiceFactory() {
85 }
86
87 void HotwordServiceFactory::InitializeMicrophoneObserver() {
88   MediaCaptureDevicesDispatcher::GetInstance()->AddObserver(this);
89 }
90
91 void HotwordServiceFactory::OnUpdateAudioDevices(
92     const content::MediaStreamDevices& devices) {
93   microphone_available_ = !devices.empty();
94   audio_device_state_updated_ = true;
95 }
96
97 void HotwordServiceFactory::UpdateMicrophoneState() {
98   // In order to trigger the monitor, just call getAudioCaptureDevices.
99   content::MediaStreamDevices devices =
100     MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices();
101 }
102
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,
109                              true,
110                              user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
111   prefs->RegisterStringPref(prefs::kHotwordPreviousLanguage,
112                             std::string(),
113                             user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
114   prefs->RegisterBooleanPref(prefs::kHotwordAudioHistoryEnabled,
115                              false,
116                              user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
117   // Per-device settings (do not sync).
118   prefs->RegisterBooleanPref(prefs::kHotwordSearchEnabled,
119                              false,
120                              user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
121   prefs->RegisterBooleanPref(prefs::kHotwordAlwaysOnSearchEnabled,
122                              false,
123                              user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
124 }
125
126 KeyedService* HotwordServiceFactory::BuildServiceInstanceFor(
127     BrowserContext* context) const {
128   return new HotwordService(Profile::FromBrowserContext(context));
129 }