| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/speech/extension_api/tts_extension_api.h" | 5 #include "chrome/browser/speech/extension_api/tts_extension_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 utterance->set_desired_event_types(desired_event_types); | 288 utterance->set_desired_event_types(desired_event_types); |
| 289 utterance->set_extension_id(voice_extension_id); | 289 utterance->set_extension_id(voice_extension_id); |
| 290 utterance->set_options(options.get()); | 290 utterance->set_options(options.get()); |
| 291 utterance->set_event_delegate(new TtsExtensionEventHandler(extension_id())); | 291 utterance->set_event_delegate(new TtsExtensionEventHandler(extension_id())); |
| 292 | 292 |
| 293 TtsController* controller = TtsController::GetInstance(); | 293 TtsController* controller = TtsController::GetInstance(); |
| 294 controller->SpeakOrEnqueue(utterance); | 294 controller->SpeakOrEnqueue(utterance); |
| 295 return true; | 295 return true; |
| 296 } | 296 } |
| 297 | 297 |
| 298 bool TtsStopSpeakingFunction::RunSync() { | 298 ExtensionFunction::ResponseAction TtsStopSpeakingFunction::Run() { |
| 299 TtsController::GetInstance()->Stop(); | 299 TtsController::GetInstance()->Stop(); |
| 300 return true; | 300 return RespondNow(NoArguments()); |
| 301 } | 301 } |
| 302 | 302 |
| 303 bool TtsPauseFunction::RunSync() { | 303 ExtensionFunction::ResponseAction TtsPauseFunction::Run() { |
| 304 TtsController::GetInstance()->Pause(); | 304 TtsController::GetInstance()->Pause(); |
| 305 return true; | 305 return RespondNow(NoArguments()); |
| 306 } | 306 } |
| 307 | 307 |
| 308 bool TtsResumeFunction::RunSync() { | 308 ExtensionFunction::ResponseAction TtsResumeFunction::Run() { |
| 309 TtsController::GetInstance()->Resume(); | 309 TtsController::GetInstance()->Resume(); |
| 310 return true; | 310 return RespondNow(NoArguments()); |
| 311 } | 311 } |
| 312 | 312 |
| 313 bool TtsIsSpeakingFunction::RunSync() { | 313 ExtensionFunction::ResponseAction TtsIsSpeakingFunction::Run() { |
| 314 SetResult(base::MakeUnique<base::FundamentalValue>( | 314 return RespondNow(OneArgument(base::MakeUnique<base::FundamentalValue>( |
| 315 TtsController::GetInstance()->IsSpeaking())); | 315 TtsController::GetInstance()->IsSpeaking()))); |
| 316 return true; | |
| 317 } | 316 } |
| 318 | 317 |
| 319 bool TtsGetVoicesFunction::RunSync() { | 318 ExtensionFunction::ResponseAction TtsGetVoicesFunction::Run() { |
| 320 std::vector<VoiceData> voices; | 319 std::vector<VoiceData> voices; |
| 321 TtsController::GetInstance()->GetVoices(GetProfile(), &voices); | 320 TtsController::GetInstance()->GetVoices(browser_context(), &voices); |
| 322 | 321 |
| 323 std::unique_ptr<base::ListValue> result_voices(new base::ListValue()); | 322 std::unique_ptr<base::ListValue> result_voices(new base::ListValue()); |
| 324 for (size_t i = 0; i < voices.size(); ++i) { | 323 for (size_t i = 0; i < voices.size(); ++i) { |
| 325 const VoiceData& voice = voices[i]; | 324 const VoiceData& voice = voices[i]; |
| 326 std::unique_ptr<base::DictionaryValue> result_voice( | 325 std::unique_ptr<base::DictionaryValue> result_voice( |
| 327 new base::DictionaryValue()); | 326 new base::DictionaryValue()); |
| 328 result_voice->SetString(constants::kVoiceNameKey, voice.name); | 327 result_voice->SetString(constants::kVoiceNameKey, voice.name); |
| 329 result_voice->SetBoolean(constants::kRemoteKey, voice.remote); | 328 result_voice->SetBoolean(constants::kRemoteKey, voice.remote); |
| 330 if (!voice.lang.empty()) | 329 if (!voice.lang.empty()) |
| 331 result_voice->SetString(constants::kLangKey, voice.lang); | 330 result_voice->SetString(constants::kLangKey, voice.lang); |
| 332 if (voice.gender == TTS_GENDER_MALE) | 331 if (voice.gender == TTS_GENDER_MALE) |
| 333 result_voice->SetString(constants::kGenderKey, constants::kGenderMale); | 332 result_voice->SetString(constants::kGenderKey, constants::kGenderMale); |
| 334 else if (voice.gender == TTS_GENDER_FEMALE) | 333 else if (voice.gender == TTS_GENDER_FEMALE) |
| 335 result_voice->SetString(constants::kGenderKey, constants::kGenderFemale); | 334 result_voice->SetString(constants::kGenderKey, constants::kGenderFemale); |
| 336 if (!voice.extension_id.empty()) | 335 if (!voice.extension_id.empty()) |
| 337 result_voice->SetString(constants::kExtensionIdKey, voice.extension_id); | 336 result_voice->SetString(constants::kExtensionIdKey, voice.extension_id); |
| 338 | 337 |
| 339 base::ListValue* event_types = new base::ListValue(); | 338 base::ListValue* event_types = new base::ListValue(); |
| 340 for (std::set<TtsEventType>::iterator iter = voice.events.begin(); | 339 for (std::set<TtsEventType>::iterator iter = voice.events.begin(); |
| 341 iter != voice.events.end(); ++iter) { | 340 iter != voice.events.end(); ++iter) { |
| 342 const char* event_name_constant = TtsEventTypeToString(*iter); | 341 const char* event_name_constant = TtsEventTypeToString(*iter); |
| 343 event_types->AppendString(event_name_constant); | 342 event_types->AppendString(event_name_constant); |
| 344 } | 343 } |
| 345 result_voice->Set(constants::kEventTypesKey, event_types); | 344 result_voice->Set(constants::kEventTypesKey, event_types); |
| 346 | 345 |
| 347 result_voices->Append(std::move(result_voice)); | 346 result_voices->Append(std::move(result_voice)); |
| 348 } | 347 } |
| 349 | 348 |
| 350 SetResult(std::move(result_voices)); | 349 return RespondNow(OneArgument(std::move(result_voices))); |
| 351 return true; | |
| 352 } | 350 } |
| 353 | 351 |
| 354 TtsAPI::TtsAPI(content::BrowserContext* context) { | 352 TtsAPI::TtsAPI(content::BrowserContext* context) { |
| 355 ExtensionFunctionRegistry* registry = | 353 ExtensionFunctionRegistry* registry = |
| 356 ExtensionFunctionRegistry::GetInstance(); | 354 ExtensionFunctionRegistry::GetInstance(); |
| 357 registry->RegisterFunction<ExtensionTtsEngineSendTtsEventFunction>(); | 355 registry->RegisterFunction<ExtensionTtsEngineSendTtsEventFunction>(); |
| 358 registry->RegisterFunction<TtsGetVoicesFunction>(); | 356 registry->RegisterFunction<TtsGetVoicesFunction>(); |
| 359 registry->RegisterFunction<TtsIsSpeakingFunction>(); | 357 registry->RegisterFunction<TtsIsSpeakingFunction>(); |
| 360 registry->RegisterFunction<TtsSpeakFunction>(); | 358 registry->RegisterFunction<TtsSpeakFunction>(); |
| 361 registry->RegisterFunction<TtsStopSpeakingFunction>(); | 359 registry->RegisterFunction<TtsStopSpeakingFunction>(); |
| 362 registry->RegisterFunction<TtsPauseFunction>(); | 360 registry->RegisterFunction<TtsPauseFunction>(); |
| 363 registry->RegisterFunction<TtsResumeFunction>(); | 361 registry->RegisterFunction<TtsResumeFunction>(); |
| 364 | 362 |
| 365 // Ensure we're observing newly added engines for the given context. | 363 // Ensure we're observing newly added engines for the given context. |
| 366 TtsEngineExtensionObserver::GetInstance(Profile::FromBrowserContext(context)); | 364 TtsEngineExtensionObserver::GetInstance(Profile::FromBrowserContext(context)); |
| 367 } | 365 } |
| 368 | 366 |
| 369 TtsAPI::~TtsAPI() { | 367 TtsAPI::~TtsAPI() { |
| 370 } | 368 } |
| 371 | 369 |
| 372 static base::LazyInstance<BrowserContextKeyedAPIFactory<TtsAPI> > g_factory = | 370 static base::LazyInstance<BrowserContextKeyedAPIFactory<TtsAPI> > g_factory = |
| 373 LAZY_INSTANCE_INITIALIZER; | 371 LAZY_INSTANCE_INITIALIZER; |
| 374 | 372 |
| 375 BrowserContextKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { | 373 BrowserContextKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { |
| 376 return g_factory.Pointer(); | 374 return g_factory.Pointer(); |
| 377 } | 375 } |
| 378 | 376 |
| 379 } // namespace extensions | 377 } // namespace extensions |
| OLD | NEW |