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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 case TTS_EVENT_SENTENCE: | 33 case TTS_EVENT_SENTENCE: |
34 return constants::kEventTypeSentence; | 34 return constants::kEventTypeSentence; |
35 case TTS_EVENT_MARKER: | 35 case TTS_EVENT_MARKER: |
36 return constants::kEventTypeMarker; | 36 return constants::kEventTypeMarker; |
37 case TTS_EVENT_INTERRUPTED: | 37 case TTS_EVENT_INTERRUPTED: |
38 return constants::kEventTypeInterrupted; | 38 return constants::kEventTypeInterrupted; |
39 case TTS_EVENT_CANCELLED: | 39 case TTS_EVENT_CANCELLED: |
40 return constants::kEventTypeCancelled; | 40 return constants::kEventTypeCancelled; |
41 case TTS_EVENT_ERROR: | 41 case TTS_EVENT_ERROR: |
42 return constants::kEventTypeError; | 42 return constants::kEventTypeError; |
| 43 case TTS_EVENT_PAUSE: |
| 44 return constants::kEventTypePause; |
| 45 case TTS_EVENT_RESUME: |
| 46 return constants::kEventTypeResume; |
43 default: | 47 default: |
44 NOTREACHED(); | 48 NOTREACHED(); |
45 return constants::kEventTypeError; | 49 return constants::kEventTypeError; |
46 } | 50 } |
47 } | 51 } |
48 | 52 |
49 TtsEventType TtsEventTypeFromString(const std::string& str) { | 53 TtsEventType TtsEventTypeFromString(const std::string& str) { |
50 if (str == constants::kEventTypeStart) | 54 if (str == constants::kEventTypeStart) |
51 return TTS_EVENT_START; | 55 return TTS_EVENT_START; |
52 if (str == constants::kEventTypeEnd) | 56 if (str == constants::kEventTypeEnd) |
53 return TTS_EVENT_END; | 57 return TTS_EVENT_END; |
54 if (str == constants::kEventTypeWord) | 58 if (str == constants::kEventTypeWord) |
55 return TTS_EVENT_WORD; | 59 return TTS_EVENT_WORD; |
56 if (str == constants::kEventTypeSentence) | 60 if (str == constants::kEventTypeSentence) |
57 return TTS_EVENT_SENTENCE; | 61 return TTS_EVENT_SENTENCE; |
58 if (str == constants::kEventTypeMarker) | 62 if (str == constants::kEventTypeMarker) |
59 return TTS_EVENT_MARKER; | 63 return TTS_EVENT_MARKER; |
60 if (str == constants::kEventTypeInterrupted) | 64 if (str == constants::kEventTypeInterrupted) |
61 return TTS_EVENT_INTERRUPTED; | 65 return TTS_EVENT_INTERRUPTED; |
62 if (str == constants::kEventTypeCancelled) | 66 if (str == constants::kEventTypeCancelled) |
63 return TTS_EVENT_CANCELLED; | 67 return TTS_EVENT_CANCELLED; |
64 if (str == constants::kEventTypeError) | 68 if (str == constants::kEventTypeError) |
65 return TTS_EVENT_ERROR; | 69 return TTS_EVENT_ERROR; |
| 70 if (str == constants::kEventTypePause) |
| 71 return TTS_EVENT_PAUSE; |
| 72 if (str == constants::kEventTypeResume) |
| 73 return TTS_EVENT_RESUME; |
66 | 74 |
67 NOTREACHED(); | 75 NOTREACHED(); |
68 return TTS_EVENT_ERROR; | 76 return TTS_EVENT_ERROR; |
69 } | 77 } |
70 | 78 |
71 namespace extensions { | 79 namespace extensions { |
72 | 80 |
73 // One of these is constructed for each utterance, and deleted | 81 // One of these is constructed for each utterance, and deleted |
74 // when the utterance gets any final event. | 82 // when the utterance gets any final event. |
75 class TtsExtensionEventHandler : public UtteranceEventDelegate { | 83 class TtsExtensionEventHandler : public UtteranceEventDelegate { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 TtsController* controller = TtsController::GetInstance(); | 275 TtsController* controller = TtsController::GetInstance(); |
268 controller->SpeakOrEnqueue(utterance); | 276 controller->SpeakOrEnqueue(utterance); |
269 return true; | 277 return true; |
270 } | 278 } |
271 | 279 |
272 bool TtsStopSpeakingFunction::RunImpl() { | 280 bool TtsStopSpeakingFunction::RunImpl() { |
273 TtsController::GetInstance()->Stop(); | 281 TtsController::GetInstance()->Stop(); |
274 return true; | 282 return true; |
275 } | 283 } |
276 | 284 |
| 285 bool TtsPauseFunction::RunImpl() { |
| 286 TtsController::GetInstance()->Pause(); |
| 287 return true; |
| 288 } |
| 289 |
| 290 bool TtsResumeFunction::RunImpl() { |
| 291 TtsController::GetInstance()->Resume(); |
| 292 return true; |
| 293 } |
| 294 |
277 bool TtsIsSpeakingFunction::RunImpl() { | 295 bool TtsIsSpeakingFunction::RunImpl() { |
278 SetResult(Value::CreateBooleanValue( | 296 SetResult(Value::CreateBooleanValue( |
279 TtsController::GetInstance()->IsSpeaking())); | 297 TtsController::GetInstance()->IsSpeaking())); |
280 return true; | 298 return true; |
281 } | 299 } |
282 | 300 |
283 bool TtsGetVoicesFunction::RunImpl() { | 301 bool TtsGetVoicesFunction::RunImpl() { |
284 std::vector<VoiceData> voices; | 302 std::vector<VoiceData> voices; |
285 TtsController::GetInstance()->GetVoices(profile(), &voices); | 303 TtsController::GetInstance()->GetVoices(profile(), &voices); |
286 | 304 |
287 scoped_ptr<ListValue> result_voices(new ListValue()); | 305 scoped_ptr<ListValue> result_voices(new ListValue()); |
288 for (size_t i = 0; i < voices.size(); ++i) { | 306 for (size_t i = 0; i < voices.size(); ++i) { |
289 const VoiceData& voice = voices[i]; | 307 const VoiceData& voice = voices[i]; |
290 DictionaryValue* result_voice = new DictionaryValue(); | 308 DictionaryValue* result_voice = new DictionaryValue(); |
291 result_voice->SetString(constants::kVoiceNameKey, voice.name); | 309 result_voice->SetString(constants::kVoiceNameKey, voice.name); |
292 if (!voice.lang.empty()) | 310 if (!voice.lang.empty()) |
293 result_voice->SetString(constants::kLangKey, voice.lang); | 311 result_voice->SetString(constants::kLangKey, voice.lang); |
294 if (voice.gender == TTS_GENDER_MALE) | 312 if (voice.gender == TTS_GENDER_MALE) |
295 result_voice->SetString(constants::kGenderKey, constants::kGenderMale); | 313 result_voice->SetString(constants::kGenderKey, constants::kGenderMale); |
296 else if (voice.gender == TTS_GENDER_FEMALE) | 314 else if (voice.gender == TTS_GENDER_FEMALE) |
297 result_voice->SetString(constants::kGenderKey, constants::kGenderFemale); | 315 result_voice->SetString(constants::kGenderKey, constants::kGenderFemale); |
298 if (!voice.extension_id.empty()) | 316 if (!voice.extension_id.empty()) |
299 result_voice->SetString(constants::kExtensionIdKey, voice.extension_id); | 317 result_voice->SetString(constants::kExtensionIdKey, voice.extension_id); |
300 | 318 |
301 ListValue* event_types = new ListValue(); | 319 ListValue* event_types = new ListValue(); |
302 for (std::set<TtsEventType>::iterator iter = voice.events.begin(); | 320 for (std::set<TtsEventType>::iterator iter = voice.events.begin(); |
303 iter != voice.events.end(); ++iter) { | 321 iter != voice.events.end(); ++iter) { |
304 const char* event_name_constant = NULL; | 322 const char* event_name_constant = TtsEventTypeToString(*iter); |
305 switch (*iter) { | 323 event_types->Append(Value::CreateStringValue(event_name_constant)); |
306 case TTS_EVENT_START: | |
307 event_name_constant = constants::kEventTypeStart; | |
308 break; | |
309 case TTS_EVENT_END: | |
310 event_name_constant = constants::kEventTypeEnd; | |
311 break; | |
312 case TTS_EVENT_WORD: | |
313 event_name_constant = constants::kEventTypeWord; | |
314 break; | |
315 case TTS_EVENT_SENTENCE: | |
316 event_name_constant = constants::kEventTypeSentence; | |
317 break; | |
318 case TTS_EVENT_MARKER: | |
319 event_name_constant = constants::kEventTypeMarker; | |
320 break; | |
321 case TTS_EVENT_INTERRUPTED: | |
322 event_name_constant = constants::kEventTypeInterrupted; | |
323 break; | |
324 case TTS_EVENT_CANCELLED: | |
325 event_name_constant = constants::kEventTypeCancelled; | |
326 break; | |
327 case TTS_EVENT_ERROR: | |
328 event_name_constant = constants::kEventTypeError; | |
329 break; | |
330 } | |
331 if (event_name_constant) | |
332 event_types->Append(Value::CreateStringValue(event_name_constant)); | |
333 } | 324 } |
334 result_voice->Set(constants::kEventTypesKey, event_types); | 325 result_voice->Set(constants::kEventTypesKey, event_types); |
335 | 326 |
336 result_voices->Append(result_voice); | 327 result_voices->Append(result_voice); |
337 } | 328 } |
338 | 329 |
339 SetResult(result_voices.release()); | 330 SetResult(result_voices.release()); |
340 return true; | 331 return true; |
341 } | 332 } |
342 | 333 |
343 // static | 334 // static |
344 TtsAPI* TtsAPI::Get(Profile* profile) { | 335 TtsAPI* TtsAPI::Get(Profile* profile) { |
345 return ProfileKeyedAPIFactory<TtsAPI>::GetForProfile(profile); | 336 return ProfileKeyedAPIFactory<TtsAPI>::GetForProfile(profile); |
346 } | 337 } |
347 | 338 |
348 TtsAPI::TtsAPI(Profile* profile) { | 339 TtsAPI::TtsAPI(Profile* profile) { |
349 ExtensionFunctionRegistry* registry = | 340 ExtensionFunctionRegistry* registry = |
350 ExtensionFunctionRegistry::GetInstance(); | 341 ExtensionFunctionRegistry::GetInstance(); |
351 registry->RegisterFunction<ExtensionTtsEngineSendTtsEventFunction>(); | 342 registry->RegisterFunction<ExtensionTtsEngineSendTtsEventFunction>(); |
352 registry->RegisterFunction<TtsGetVoicesFunction>(); | 343 registry->RegisterFunction<TtsGetVoicesFunction>(); |
353 registry->RegisterFunction<TtsIsSpeakingFunction>(); | 344 registry->RegisterFunction<TtsIsSpeakingFunction>(); |
354 registry->RegisterFunction<TtsSpeakFunction>(); | 345 registry->RegisterFunction<TtsSpeakFunction>(); |
355 registry->RegisterFunction<TtsStopSpeakingFunction>(); | 346 registry->RegisterFunction<TtsStopSpeakingFunction>(); |
| 347 registry->RegisterFunction<TtsPauseFunction>(); |
| 348 registry->RegisterFunction<TtsResumeFunction>(); |
356 } | 349 } |
357 | 350 |
358 TtsAPI::~TtsAPI() { | 351 TtsAPI::~TtsAPI() { |
359 } | 352 } |
360 | 353 |
361 static base::LazyInstance<ProfileKeyedAPIFactory<TtsAPI> > | 354 static base::LazyInstance<ProfileKeyedAPIFactory<TtsAPI> > |
362 g_factory = LAZY_INSTANCE_INITIALIZER; | 355 g_factory = LAZY_INSTANCE_INITIALIZER; |
363 | 356 |
364 ProfileKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { | 357 ProfileKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { |
365 return &g_factory.Get(); | 358 return &g_factory.Get(); |
366 } | 359 } |
367 | 360 |
368 } // namespace extensions | 361 } // namespace extensions |
OLD | NEW |