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 <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/search/hotword_audio_history_handler.h" | 16 #include "chrome/browser/search/hotword_audio_history_handler.h" |
16 #include "chrome/browser/search/hotword_client.h" | 17 #include "chrome/browser/search/hotword_client.h" |
17 #include "chrome/browser/search/hotword_service.h" | 18 #include "chrome/browser/search/hotword_service.h" |
18 #include "chrome/browser/search/hotword_service_factory.h" | 19 #include "chrome/browser/search/hotword_service_factory.h" |
19 #include "chrome/browser/ui/app_list/app_list_service.h" | 20 #include "chrome/browser/ui/app_list/app_list_service.h" |
20 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 void HotwordPrivateEventService::OnMicrophoneStateChanged(bool enabled) { | 126 void HotwordPrivateEventService::OnMicrophoneStateChanged(bool enabled) { |
126 SignalEvent(events::HOTWORD_PRIVATE_ON_MICROPHONE_STATE_CHANGED, | 127 SignalEvent(events::HOTWORD_PRIVATE_ON_MICROPHONE_STATE_CHANGED, |
127 api::hotword_private::OnMicrophoneStateChanged::kEventName, | 128 api::hotword_private::OnMicrophoneStateChanged::kEventName, |
128 api::hotword_private::OnMicrophoneStateChanged::Create(enabled)); | 129 api::hotword_private::OnMicrophoneStateChanged::Create(enabled)); |
129 } | 130 } |
130 | 131 |
131 void HotwordPrivateEventService::SignalEvent( | 132 void HotwordPrivateEventService::SignalEvent( |
132 events::HistogramValue histogram_value, | 133 events::HistogramValue histogram_value, |
133 const std::string& event_name) { | 134 const std::string& event_name) { |
134 SignalEvent(histogram_value, event_name, | 135 SignalEvent(histogram_value, event_name, |
135 make_scoped_ptr(new base::ListValue())); | 136 base::WrapUnique(new base::ListValue())); |
136 } | 137 } |
137 | 138 |
138 void HotwordPrivateEventService::SignalEvent( | 139 void HotwordPrivateEventService::SignalEvent( |
139 events::HistogramValue histogram_value, | 140 events::HistogramValue histogram_value, |
140 const std::string& event_name, | 141 const std::string& event_name, |
141 scoped_ptr<base::ListValue> args) { | 142 std::unique_ptr<base::ListValue> args) { |
142 EventRouter* router = EventRouter::Get(profile_); | 143 EventRouter* router = EventRouter::Get(profile_); |
143 if (!router || !router->HasEventListener(event_name)) | 144 if (!router || !router->HasEventListener(event_name)) |
144 return; | 145 return; |
145 | 146 |
146 scoped_ptr<Event> event( | 147 std::unique_ptr<Event> event( |
147 new Event(histogram_value, event_name, std::move(args))); | 148 new Event(histogram_value, event_name, std::move(args))); |
148 router->BroadcastEvent(std::move(event)); | 149 router->BroadcastEvent(std::move(event)); |
149 } | 150 } |
150 | 151 |
151 bool HotwordPrivateSetEnabledFunction::RunSync() { | 152 bool HotwordPrivateSetEnabledFunction::RunSync() { |
152 scoped_ptr<api::hotword_private::SetEnabled::Params> params( | 153 std::unique_ptr<api::hotword_private::SetEnabled::Params> params( |
153 api::hotword_private::SetEnabled::Params::Create(*args_)); | 154 api::hotword_private::SetEnabled::Params::Create(*args_)); |
154 EXTENSION_FUNCTION_VALIDATE(params.get()); | 155 EXTENSION_FUNCTION_VALIDATE(params.get()); |
155 | 156 |
156 PrefService* prefs = GetProfile()->GetPrefs(); | 157 PrefService* prefs = GetProfile()->GetPrefs(); |
157 prefs->SetBoolean(prefs::kHotwordSearchEnabled, params->state); | 158 prefs->SetBoolean(prefs::kHotwordSearchEnabled, params->state); |
158 return true; | 159 return true; |
159 } | 160 } |
160 | 161 |
161 bool HotwordPrivateSetAudioLoggingEnabledFunction::RunSync() { | 162 bool HotwordPrivateSetAudioLoggingEnabledFunction::RunSync() { |
162 scoped_ptr<api::hotword_private::SetAudioLoggingEnabled::Params> params( | 163 std::unique_ptr<api::hotword_private::SetAudioLoggingEnabled::Params> params( |
163 api::hotword_private::SetAudioLoggingEnabled::Params::Create(*args_)); | 164 api::hotword_private::SetAudioLoggingEnabled::Params::Create(*args_)); |
164 EXTENSION_FUNCTION_VALIDATE(params.get()); | 165 EXTENSION_FUNCTION_VALIDATE(params.get()); |
165 | 166 |
166 // TODO(kcarattini): Sync the chrome pref with the account-level | 167 // TODO(kcarattini): Sync the chrome pref with the account-level |
167 // Audio History setting. | 168 // Audio History setting. |
168 PrefService* prefs = GetProfile()->GetPrefs(); | 169 PrefService* prefs = GetProfile()->GetPrefs(); |
169 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, params->state); | 170 prefs->SetBoolean(prefs::kHotwordAudioLoggingEnabled, params->state); |
170 return true; | 171 return true; |
171 } | 172 } |
172 | 173 |
173 bool HotwordPrivateSetHotwordAlwaysOnSearchEnabledFunction::RunSync() { | 174 bool HotwordPrivateSetHotwordAlwaysOnSearchEnabledFunction::RunSync() { |
174 scoped_ptr<api::hotword_private::SetHotwordAlwaysOnSearchEnabled::Params> | 175 std::unique_ptr<api::hotword_private::SetHotwordAlwaysOnSearchEnabled::Params> |
175 params(api::hotword_private::SetHotwordAlwaysOnSearchEnabled::Params:: | 176 params( |
176 Create(*args_)); | 177 api::hotword_private::SetHotwordAlwaysOnSearchEnabled::Params::Create( |
| 178 *args_)); |
177 EXTENSION_FUNCTION_VALIDATE(params.get()); | 179 EXTENSION_FUNCTION_VALIDATE(params.get()); |
178 | 180 |
179 PrefService* prefs = GetProfile()->GetPrefs(); | 181 PrefService* prefs = GetProfile()->GetPrefs(); |
180 prefs->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, params->state); | 182 prefs->SetBoolean(prefs::kHotwordAlwaysOnSearchEnabled, params->state); |
181 return true; | 183 return true; |
182 } | 184 } |
183 | 185 |
184 bool HotwordPrivateGetStatusFunction::RunSync() { | 186 bool HotwordPrivateGetStatusFunction::RunSync() { |
185 scoped_ptr<api::hotword_private::GetStatus::Params> params( | 187 std::unique_ptr<api::hotword_private::GetStatus::Params> params( |
186 api::hotword_private::GetStatus::Params::Create(*args_)); | 188 api::hotword_private::GetStatus::Params::Create(*args_)); |
187 EXTENSION_FUNCTION_VALIDATE(params.get()); | 189 EXTENSION_FUNCTION_VALIDATE(params.get()); |
188 | 190 |
189 api::hotword_private::StatusDetails result; | 191 api::hotword_private::StatusDetails result; |
190 | 192 |
191 HotwordService* hotword_service = | 193 HotwordService* hotword_service = |
192 HotwordServiceFactory::GetForProfile(GetProfile()); | 194 HotwordServiceFactory::GetForProfile(GetProfile()); |
193 if (!hotword_service) { | 195 if (!hotword_service) { |
194 result.available = false; | 196 result.available = false; |
195 result.always_on_available = false; | 197 result.always_on_available = false; |
(...skipping 20 matching lines...) Expand all Loading... |
216 } | 218 } |
217 | 219 |
218 PrefService* prefs = GetProfile()->GetPrefs(); | 220 PrefService* prefs = GetProfile()->GetPrefs(); |
219 result.enabled_set = prefs->HasPrefPath(prefs::kHotwordSearchEnabled); | 221 result.enabled_set = prefs->HasPrefPath(prefs::kHotwordSearchEnabled); |
220 | 222 |
221 SetResult(result.ToValue().release()); | 223 SetResult(result.ToValue().release()); |
222 return true; | 224 return true; |
223 } | 225 } |
224 | 226 |
225 bool HotwordPrivateSetHotwordSessionStateFunction::RunSync() { | 227 bool HotwordPrivateSetHotwordSessionStateFunction::RunSync() { |
226 scoped_ptr<api::hotword_private::SetHotwordSessionState::Params> params( | 228 std::unique_ptr<api::hotword_private::SetHotwordSessionState::Params> params( |
227 api::hotword_private::SetHotwordSessionState::Params::Create(*args_)); | 229 api::hotword_private::SetHotwordSessionState::Params::Create(*args_)); |
228 EXTENSION_FUNCTION_VALIDATE(params.get()); | 230 EXTENSION_FUNCTION_VALIDATE(params.get()); |
229 | 231 |
230 HotwordService* hotword_service = | 232 HotwordService* hotword_service = |
231 HotwordServiceFactory::GetForProfile(GetProfile()); | 233 HotwordServiceFactory::GetForProfile(GetProfile()); |
232 if (hotword_service && | 234 if (hotword_service && |
233 hotword_service->client() && | 235 hotword_service->client() && |
234 !hotword_service->IsTraining()) | 236 !hotword_service->IsTraining()) |
235 hotword_service->client()->OnHotwordStateChanged(params->started); | 237 hotword_service->client()->OnHotwordStateChanged(params->started); |
236 return true; | 238 return true; |
237 } | 239 } |
238 | 240 |
239 bool HotwordPrivateNotifyHotwordRecognitionFunction::RunSync() { | 241 bool HotwordPrivateNotifyHotwordRecognitionFunction::RunSync() { |
240 scoped_ptr<api::hotword_private::NotifyHotwordRecognition::Params> params( | 242 std::unique_ptr<api::hotword_private::NotifyHotwordRecognition::Params> |
241 api::hotword_private::NotifyHotwordRecognition::Params::Create(*args_)); | 243 params(api::hotword_private::NotifyHotwordRecognition::Params::Create( |
| 244 *args_)); |
242 EXTENSION_FUNCTION_VALIDATE(params.get()); | 245 EXTENSION_FUNCTION_VALIDATE(params.get()); |
243 | 246 |
244 scoped_refptr<content::SpeechRecognitionSessionPreamble> preamble; | 247 scoped_refptr<content::SpeechRecognitionSessionPreamble> preamble; |
245 if (params->log.get() && | 248 if (params->log.get() && |
246 !params->log->buffer.empty() && | 249 !params->log->buffer.empty() && |
247 params->log->channels == 1) { | 250 params->log->channels == 1) { |
248 // TODO(amistry): Convert multi-channel preamble log into mono. | 251 // TODO(amistry): Convert multi-channel preamble log into mono. |
249 preamble = new content::SpeechRecognitionSessionPreamble(); | 252 preamble = new content::SpeechRecognitionSessionPreamble(); |
250 preamble->sample_rate = params->log->sample_rate; | 253 preamble->sample_rate = params->log->sample_rate; |
251 preamble->sample_depth = params->log->bytes_per_sample; | 254 preamble->sample_depth = params->log->bytes_per_sample; |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 l10n_util::GetStringUTF16(IDS_HOTWORD_OPT_IN_FINISHED_WAIT)); | 451 l10n_util::GetStringUTF16(IDS_HOTWORD_OPT_IN_FINISHED_WAIT)); |
449 | 452 |
450 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | 453 const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
451 webui::SetLoadTimeDataDefaults(app_locale, localized_strings); | 454 webui::SetLoadTimeDataDefaults(app_locale, localized_strings); |
452 | 455 |
453 SetResult(localized_strings); | 456 SetResult(localized_strings); |
454 return true; | 457 return true; |
455 } | 458 } |
456 | 459 |
457 bool HotwordPrivateSetAudioHistoryEnabledFunction::RunAsync() { | 460 bool HotwordPrivateSetAudioHistoryEnabledFunction::RunAsync() { |
458 scoped_ptr<api::hotword_private::SetAudioHistoryEnabled::Params> params( | 461 std::unique_ptr<api::hotword_private::SetAudioHistoryEnabled::Params> params( |
459 api::hotword_private::SetAudioHistoryEnabled::Params::Create(*args_)); | 462 api::hotword_private::SetAudioHistoryEnabled::Params::Create(*args_)); |
460 EXTENSION_FUNCTION_VALIDATE(params.get()); | 463 EXTENSION_FUNCTION_VALIDATE(params.get()); |
461 | 464 |
462 HotwordService* hotword_service = | 465 HotwordService* hotword_service = |
463 HotwordServiceFactory::GetForProfile(GetProfile()); | 466 HotwordServiceFactory::GetForProfile(GetProfile()); |
464 if (!hotword_service || !hotword_service->GetAudioHistoryHandler()) { | 467 if (!hotword_service || !hotword_service->GetAudioHistoryHandler()) { |
465 error_ = hotword_private_constants::kHotwordServiceUnavailable; | 468 error_ = hotword_private_constants::kHotwordServiceUnavailable; |
466 return false; | 469 return false; |
467 } | 470 } |
468 | 471 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 void HotwordPrivateGetAudioHistoryEnabledFunction::SetResultAndSendResponse( | 504 void HotwordPrivateGetAudioHistoryEnabledFunction::SetResultAndSendResponse( |
502 bool success, bool new_enabled_value) { | 505 bool success, bool new_enabled_value) { |
503 api::hotword_private::AudioHistoryState result; | 506 api::hotword_private::AudioHistoryState result; |
504 result.success = success; | 507 result.success = success; |
505 result.enabled = new_enabled_value; | 508 result.enabled = new_enabled_value; |
506 SetResult(result.ToValue().release()); | 509 SetResult(result.ToValue().release()); |
507 SendResponse(true); | 510 SendResponse(true); |
508 } | 511 } |
509 | 512 |
510 bool HotwordPrivateSpeakerModelExistsResultFunction::RunSync() { | 513 bool HotwordPrivateSpeakerModelExistsResultFunction::RunSync() { |
511 scoped_ptr<api::hotword_private::SpeakerModelExistsResult::Params> params( | 514 std::unique_ptr<api::hotword_private::SpeakerModelExistsResult::Params> |
512 api::hotword_private::SpeakerModelExistsResult::Params::Create(*args_)); | 515 params(api::hotword_private::SpeakerModelExistsResult::Params::Create( |
| 516 *args_)); |
513 EXTENSION_FUNCTION_VALIDATE(params.get()); | 517 EXTENSION_FUNCTION_VALIDATE(params.get()); |
514 | 518 |
515 HotwordService* hotword_service = | 519 HotwordService* hotword_service = |
516 HotwordServiceFactory::GetForProfile(GetProfile()); | 520 HotwordServiceFactory::GetForProfile(GetProfile()); |
517 if (!hotword_service) | 521 if (!hotword_service) |
518 return false; | 522 return false; |
519 | 523 |
520 hotword_service->SpeakerModelExistsComplete(params->exists); | 524 hotword_service->SpeakerModelExistsComplete(params->exists); |
521 return true; | 525 return true; |
522 } | 526 } |
523 | 527 |
524 } // namespace extensions | 528 } // namespace extensions |
OLD | NEW |