OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h" | 5 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 } | 57 } |
58 | 58 |
59 // static | 59 // static |
60 const char* HotwordPrivateEventService::service_name() { | 60 const char* HotwordPrivateEventService::service_name() { |
61 return "HotwordPrivateEventService"; | 61 return "HotwordPrivateEventService"; |
62 } | 62 } |
63 | 63 |
64 void HotwordPrivateEventService::OnEnabledChanged( | 64 void HotwordPrivateEventService::OnEnabledChanged( |
65 const std::string& pref_name) { | 65 const std::string& pref_name) { |
66 DCHECK(pref_name == std::string(prefs::kHotwordSearchEnabled) || | 66 DCHECK(pref_name == std::string(prefs::kHotwordSearchEnabled) || |
67 pref_name == std::string(prefs::kHotwordAlwaysOnSearchEnabled)); | 67 pref_name == std::string(prefs::kHotwordAlwaysOnSearchEnabled) || |
| 68 pref_name == std::string( |
| 69 hotword_internal::kHotwordTrainingEnabled)); |
68 SignalEvent(OnEnabledChanged::kEventName); | 70 SignalEvent(OnEnabledChanged::kEventName); |
69 } | 71 } |
70 | 72 |
71 void HotwordPrivateEventService::OnHotwordSessionRequested() { | 73 void HotwordPrivateEventService::OnHotwordSessionRequested() { |
72 SignalEvent(api::hotword_private::OnHotwordSessionRequested::kEventName); | 74 SignalEvent(api::hotword_private::OnHotwordSessionRequested::kEventName); |
73 } | 75 } |
74 | 76 |
75 void HotwordPrivateEventService::OnHotwordSessionStopped() { | 77 void HotwordPrivateEventService::OnHotwordSessionStopped() { |
76 SignalEvent(api::hotword_private::OnHotwordSessionStopped::kEventName); | 78 SignalEvent(api::hotword_private::OnHotwordSessionStopped::kEventName); |
77 } | 79 } |
78 | 80 |
| 81 void HotwordPrivateEventService::OnFinalizeSpeakerModel() { |
| 82 SignalEvent(api::hotword_private::OnFinalizeSpeakerModel::kEventName); |
| 83 } |
| 84 |
| 85 void HotwordPrivateEventService::OnHotwordTriggered() { |
| 86 SignalEvent(api::hotword_private::OnHotwordTriggered::kEventName); |
| 87 } |
| 88 |
79 void HotwordPrivateEventService::SignalEvent(const std::string& event_name) { | 89 void HotwordPrivateEventService::SignalEvent(const std::string& event_name) { |
80 EventRouter* router = EventRouter::Get(profile_); | 90 EventRouter* router = EventRouter::Get(profile_); |
81 if (!router || !router->HasEventListener(event_name)) | 91 if (!router || !router->HasEventListener(event_name)) |
82 return; | 92 return; |
83 scoped_ptr<base::ListValue> args(new base::ListValue()); | 93 scoped_ptr<base::ListValue> args(new base::ListValue()); |
84 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 94 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
85 router->BroadcastEvent(event.Pass()); | 95 router->BroadcastEvent(event.Pass()); |
86 } | 96 } |
87 | 97 |
88 bool HotwordPrivateSetEnabledFunction::RunSync() { | 98 bool HotwordPrivateSetEnabledFunction::RunSync() { |
(...skipping 27 matching lines...) Expand all Loading... |
116 PrefService* prefs = GetProfile()->GetPrefs(); | 126 PrefService* prefs = GetProfile()->GetPrefs(); |
117 prefs->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, params->state); | 127 prefs->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, params->state); |
118 return true; | 128 return true; |
119 } | 129 } |
120 | 130 |
121 bool HotwordPrivateGetStatusFunction::RunSync() { | 131 bool HotwordPrivateGetStatusFunction::RunSync() { |
122 api::hotword_private::StatusDetails result; | 132 api::hotword_private::StatusDetails result; |
123 | 133 |
124 HotwordService* hotword_service = | 134 HotwordService* hotword_service = |
125 HotwordServiceFactory::GetForProfile(GetProfile()); | 135 HotwordServiceFactory::GetForProfile(GetProfile()); |
126 if (!hotword_service) | 136 if (!hotword_service) { |
127 result.available = false; | 137 result.available = false; |
128 else | 138 } else { |
129 result.available = hotword_service->IsServiceAvailable(); | 139 result.available = hotword_service->IsServiceAvailable(); |
| 140 result.audio_logging_enabled = hotword_service->IsOptedIntoAudioLogging(); |
| 141 result.training_enabled = hotword_service->IsTraining(); |
| 142 } |
130 | 143 |
131 PrefService* prefs = GetProfile()->GetPrefs(); | 144 PrefService* prefs = GetProfile()->GetPrefs(); |
132 result.enabled_set = prefs->HasPrefPath(prefs::kHotwordSearchEnabled); | 145 result.enabled_set = prefs->HasPrefPath(prefs::kHotwordSearchEnabled); |
133 result.enabled = prefs->GetBoolean(prefs::kHotwordSearchEnabled); | 146 result.enabled = prefs->GetBoolean(prefs::kHotwordSearchEnabled); |
134 result.always_on_enabled = | 147 result.always_on_enabled = |
135 prefs->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled); | 148 prefs->GetBoolean(prefs::kHotwordAlwaysOnSearchEnabled); |
136 result.audio_logging_enabled = false; | 149 result.audio_logging_enabled = false; |
137 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 150 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
138 result.experimental_hotword_enabled = command_line->HasSwitch( | 151 result.experimental_hotword_enabled = command_line->HasSwitch( |
139 switches::kEnableExperimentalHotwording); | 152 switches::kEnableExperimentalHotwording); |
140 if (hotword_service) | |
141 result.audio_logging_enabled = hotword_service->IsOptedIntoAudioLogging(); | |
142 | 153 |
143 SetResult(result.ToValue().release()); | 154 SetResult(result.ToValue().release()); |
144 return true; | 155 return true; |
145 } | 156 } |
146 | 157 |
147 bool HotwordPrivateSetHotwordSessionStateFunction::RunSync() { | 158 bool HotwordPrivateSetHotwordSessionStateFunction::RunSync() { |
148 scoped_ptr<api::hotword_private::SetHotwordSessionState::Params> params( | 159 scoped_ptr<api::hotword_private::SetHotwordSessionState::Params> params( |
149 api::hotword_private::SetHotwordSessionState::Params::Create(*args_)); | 160 api::hotword_private::SetHotwordSessionState::Params::Create(*args_)); |
150 EXTENSION_FUNCTION_VALIDATE(params.get()); | 161 EXTENSION_FUNCTION_VALIDATE(params.get()); |
151 | 162 |
152 HotwordService* hotword_service = | 163 HotwordService* hotword_service = |
153 HotwordServiceFactory::GetForProfile(GetProfile()); | 164 HotwordServiceFactory::GetForProfile(GetProfile()); |
154 if (hotword_service && hotword_service->client()) | 165 if (hotword_service && |
| 166 hotword_service->client() && |
| 167 !hotword_service->IsTraining()) |
155 hotword_service->client()->OnHotwordStateChanged(params->started); | 168 hotword_service->client()->OnHotwordStateChanged(params->started); |
156 return true; | 169 return true; |
157 } | 170 } |
158 | 171 |
159 bool HotwordPrivateNotifyHotwordRecognitionFunction::RunSync() { | 172 bool HotwordPrivateNotifyHotwordRecognitionFunction::RunSync() { |
160 HotwordService* hotword_service = | 173 HotwordService* hotword_service = |
161 HotwordServiceFactory::GetForProfile(GetProfile()); | 174 HotwordServiceFactory::GetForProfile(GetProfile()); |
162 if (hotword_service) { | 175 if (hotword_service) { |
163 if (hotword_service->client()) { | 176 if (hotword_service->IsTraining()) { |
| 177 hotword_service->NotifyHotwordTriggered(); |
| 178 } else if (hotword_service->client()) { |
164 hotword_service->client()->OnHotwordRecognized(); | 179 hotword_service->client()->OnHotwordRecognized(); |
165 } else if (HotwordService::IsExperimentalHotwordingEnabled() && | 180 } else if (HotwordService::IsExperimentalHotwordingEnabled() && |
166 hotword_service->IsAlwaysOnEnabled()) { | 181 hotword_service->IsAlwaysOnEnabled()) { |
167 Browser* browser = GetCurrentBrowser(); | 182 Browser* browser = GetCurrentBrowser(); |
168 // If a Browser does not exist, fall back to the universally available, | 183 // If a Browser does not exist, fall back to the universally available, |
169 // but not recommended, way. | 184 // but not recommended, way. |
170 AppListService* app_list_service = AppListService::Get( | 185 AppListService* app_list_service = AppListService::Get( |
171 browser ? browser->host_desktop_type() : chrome::GetActiveDesktop()); | 186 browser ? browser->host_desktop_type() : chrome::GetActiveDesktop()); |
172 CHECK(app_list_service); | 187 CHECK(app_list_service); |
173 app_list_service->ShowForVoiceSearch(GetProfile()); | 188 app_list_service->ShowForVoiceSearch(GetProfile()); |
174 } | 189 } |
175 } | 190 } |
176 return true; | 191 return true; |
177 } | 192 } |
178 | 193 |
179 bool HotwordPrivateGetLaunchStateFunction::RunSync() { | 194 bool HotwordPrivateGetLaunchStateFunction::RunSync() { |
180 api::hotword_private::LaunchState result; | |
181 | |
182 HotwordService* hotword_service = | 195 HotwordService* hotword_service = |
183 HotwordServiceFactory::GetForProfile(GetProfile()); | 196 HotwordServiceFactory::GetForProfile(GetProfile()); |
184 if (!hotword_service) { | 197 if (!hotword_service) { |
185 error_ = hotword_private_constants::kHotwordServiceUnavailable; | 198 error_ = hotword_private_constants::kHotwordServiceUnavailable; |
186 return false; | 199 return false; |
187 } else { | |
188 result.launch_mode = | |
189 hotword_service->GetHotwordAudioVerificationLaunchMode(); | |
190 } | 200 } |
191 | 201 |
| 202 api::hotword_private::LaunchState result; |
| 203 result.launch_mode = |
| 204 hotword_service->GetHotwordAudioVerificationLaunchMode(); |
192 SetResult(result.ToValue().release()); | 205 SetResult(result.ToValue().release()); |
193 return true; | 206 return true; |
194 } | 207 } |
195 | 208 |
| 209 bool HotwordPrivateStartTrainingFunction::RunSync() { |
| 210 HotwordService* hotword_service = |
| 211 HotwordServiceFactory::GetForProfile(GetProfile()); |
| 212 if (!hotword_service) { |
| 213 error_ = hotword_private_constants::kHotwordServiceUnavailable; |
| 214 return false; |
| 215 } |
| 216 |
| 217 hotword_service->StartTraining(); |
| 218 return true; |
| 219 } |
| 220 |
| 221 bool HotwordPrivateFinalizeSpeakerModelFunction::RunSync() { |
| 222 HotwordService* hotword_service = |
| 223 HotwordServiceFactory::GetForProfile(GetProfile()); |
| 224 if (!hotword_service) { |
| 225 error_ = hotword_private_constants::kHotwordServiceUnavailable; |
| 226 return false; |
| 227 } |
| 228 |
| 229 hotword_service->FinalizeSpeakerModel(); |
| 230 return true; |
| 231 } |
| 232 |
| 233 bool HotwordPrivateStopTrainingFunction::RunSync() { |
| 234 HotwordService* hotword_service = |
| 235 HotwordServiceFactory::GetForProfile(GetProfile()); |
| 236 if (!hotword_service) { |
| 237 error_ = hotword_private_constants::kHotwordServiceUnavailable; |
| 238 return false; |
| 239 } |
| 240 |
| 241 hotword_service->StopTraining(); |
| 242 return true; |
| 243 } |
| 244 |
196 } // namespace extensions | 245 } // namespace extensions |
OLD | NEW |