| 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_engine_extension_api.h" | 5 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // Given a language/region code of the form 'fr-FR', returns just the basic | 30 // Given a language/region code of the form 'fr-FR', returns just the basic |
| 31 // language portion, e.g. 'fr'. | 31 // language portion, e.g. 'fr'. |
| 32 std::string TrimLanguageCode(std::string lang) { | 32 std::string TrimLanguageCode(std::string lang) { |
| 33 if (lang.size() >= 5 && lang[2] == '-') | 33 if (lang.size() >= 5 && lang[2] == '-') |
| 34 return lang.substr(0, 2); | 34 return lang.substr(0, 2); |
| 35 else | 35 else |
| 36 return lang; | 36 return lang; |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 void GetExtensionVoices(Profile* profile, ListValue* result_voices) { | 40 void GetExtensionVoices(Profile* profile, std::vector<VoiceData>* out_voices) { |
| 41 ExtensionService* service = profile->GetExtensionService(); | 41 ExtensionService* service = profile->GetExtensionService(); |
| 42 DCHECK(service); | 42 DCHECK(service); |
| 43 extensions::EventRouter* event_router = | 43 extensions::EventRouter* event_router = |
| 44 extensions::ExtensionSystem::Get(profile)->event_router(); | 44 extensions::ExtensionSystem::Get(profile)->event_router(); |
| 45 DCHECK(event_router); | 45 DCHECK(event_router); |
| 46 | 46 |
| 47 const ExtensionSet* extensions = service->extensions(); | 47 const ExtensionSet* extensions = service->extensions(); |
| 48 ExtensionSet::const_iterator iter; | 48 ExtensionSet::const_iterator iter; |
| 49 for (iter = extensions->begin(); iter != extensions->end(); ++iter) { | 49 for (iter = extensions->begin(); iter != extensions->end(); ++iter) { |
| 50 const Extension* extension = *iter; | 50 const Extension* extension = *iter; |
| 51 | 51 |
| 52 if (!event_router->ExtensionHasEventListener( | 52 if (!event_router->ExtensionHasEventListener( |
| 53 extension->id(), tts_engine_events::kOnSpeak) || | 53 extension->id(), tts_engine_events::kOnSpeak) || |
| 54 !event_router->ExtensionHasEventListener( | 54 !event_router->ExtensionHasEventListener( |
| 55 extension->id(), tts_engine_events::kOnStop)) { | 55 extension->id(), tts_engine_events::kOnStop)) { |
| 56 continue; | 56 continue; |
| 57 } | 57 } |
| 58 | 58 |
| 59 const std::vector<extensions::TtsVoice>* tts_voices = | 59 const std::vector<extensions::TtsVoice>* tts_voices = |
| 60 extensions::TtsVoice::GetTtsVoices(extension); | 60 extensions::TtsVoice::GetTtsVoices(extension); |
| 61 if (!tts_voices) | 61 if (!tts_voices) |
| 62 continue; | 62 continue; |
| 63 | 63 |
| 64 for (size_t i = 0; i < tts_voices->size(); ++i) { | 64 for (size_t i = 0; i < tts_voices->size(); ++i) { |
| 65 const extensions::TtsVoice& voice = tts_voices->at(i); | 65 const extensions::TtsVoice& voice = tts_voices->at(i); |
| 66 DictionaryValue* result_voice = new DictionaryValue(); | |
| 67 if (!voice.voice_name.empty()) | |
| 68 result_voice->SetString(constants::kVoiceNameKey, voice.voice_name); | |
| 69 if (!voice.lang.empty()) | |
| 70 result_voice->SetString(constants::kLangKey, voice.lang); | |
| 71 if (!voice.gender.empty()) | |
| 72 result_voice->SetString(constants::kGenderKey, voice.gender); | |
| 73 result_voice->SetString(constants::kExtensionIdKey, extension->id()); | |
| 74 | 66 |
| 75 ListValue* event_types = new ListValue(); | 67 out_voices->push_back(VoiceData()); |
| 68 VoiceData& result_voice = out_voices->back(); |
| 69 |
| 70 result_voice.name = voice.voice_name; |
| 71 result_voice.lang = voice.lang; |
| 72 result_voice.gender = voice.gender; |
| 73 result_voice.extension_id = extension->id(); |
| 74 |
| 76 for (std::set<std::string>::const_iterator iter = | 75 for (std::set<std::string>::const_iterator iter = |
| 77 voice.event_types.begin(); | 76 voice.event_types.begin(); |
| 78 iter != voice.event_types.end(); | 77 iter != voice.event_types.end(); |
| 79 ++iter) { | 78 ++iter) { |
| 80 event_types->Append(Value::CreateStringValue(*iter)); | 79 result_voice.events.push_back(*iter); |
| 81 } | 80 } |
| 81 |
| 82 // If the extension sends end events, the controller will handle | 82 // If the extension sends end events, the controller will handle |
| 83 // queueing and send interrupted and cancelled events. | 83 // queueing and send interrupted and cancelled events. |
| 84 if (voice.event_types.find(constants::kEventTypeEnd) != | 84 if (voice.event_types.find(constants::kEventTypeEnd) != |
| 85 voice.event_types.end()) { | 85 voice.event_types.end()) { |
| 86 event_types->Append( | 86 result_voice.events.push_back(constants::kEventTypeCancelled); |
| 87 Value::CreateStringValue(constants::kEventTypeCancelled)); | 87 result_voice.events.push_back(constants::kEventTypeInterrupted); |
| 88 event_types->Append(Value::CreateStringValue( | |
| 89 constants::kEventTypeInterrupted)); | |
| 90 } | 88 } |
| 91 | |
| 92 result_voice->Set(constants::kEventTypesKey, event_types); | |
| 93 result_voices->Append(result_voice); | |
| 94 } | 89 } |
| 95 } | 90 } |
| 96 } | 91 } |
| 97 | 92 |
| 98 bool GetMatchingExtensionVoice( | 93 bool GetMatchingExtensionVoice( |
| 99 Utterance* utterance, | 94 Utterance* utterance, |
| 100 const Extension** matching_extension, | 95 const Extension** matching_extension, |
| 101 size_t* voice_index) { | 96 size_t* voice_index) { |
| 102 // This will only happen during unit testing. Otherwise, an utterance | 97 // This will only happen during unit testing. Otherwise, an utterance |
| 103 // will always have an associated profile. | 98 // will always have an associated profile. |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 event_types = tts_voices->at(voice_index).event_types; | 199 event_types = tts_voices->at(voice_index).event_types; |
| 205 | 200 |
| 206 bool sends_end_event = | 201 bool sends_end_event = |
| 207 (event_types.find(constants::kEventTypeEnd) != event_types.end()); | 202 (event_types.find(constants::kEventTypeEnd) != event_types.end()); |
| 208 | 203 |
| 209 scoped_ptr<ListValue> args(new ListValue()); | 204 scoped_ptr<ListValue> args(new ListValue()); |
| 210 args->Set(0, Value::CreateStringValue(utterance->text())); | 205 args->Set(0, Value::CreateStringValue(utterance->text())); |
| 211 | 206 |
| 212 // Pass through most options to the speech engine, but remove some | 207 // Pass through most options to the speech engine, but remove some |
| 213 // that are handled internally. | 208 // that are handled internally. |
| 214 DictionaryValue* options = static_cast<DictionaryValue*>( | 209 scoped_ptr<DictionaryValue> options(static_cast<DictionaryValue*>( |
| 215 utterance->options()->DeepCopy()); | 210 utterance->options()->DeepCopy())); |
| 216 if (options->HasKey(constants::kRequiredEventTypesKey)) | 211 if (options->HasKey(constants::kRequiredEventTypesKey)) |
| 217 options->Remove(constants::kRequiredEventTypesKey, NULL); | 212 options->Remove(constants::kRequiredEventTypesKey, NULL); |
| 218 if (options->HasKey(constants::kDesiredEventTypesKey)) | 213 if (options->HasKey(constants::kDesiredEventTypesKey)) |
| 219 options->Remove(constants::kDesiredEventTypesKey, NULL); | 214 options->Remove(constants::kDesiredEventTypesKey, NULL); |
| 220 if (sends_end_event && options->HasKey(constants::kEnqueueKey)) | 215 if (sends_end_event && options->HasKey(constants::kEnqueueKey)) |
| 221 options->Remove(constants::kEnqueueKey, NULL); | 216 options->Remove(constants::kEnqueueKey, NULL); |
| 222 if (options->HasKey(constants::kSrcIdKey)) | 217 if (options->HasKey(constants::kSrcIdKey)) |
| 223 options->Remove(constants::kSrcIdKey, NULL); | 218 options->Remove(constants::kSrcIdKey, NULL); |
| 224 if (options->HasKey(constants::kIsFinalEventKey)) | 219 if (options->HasKey(constants::kIsFinalEventKey)) |
| 225 options->Remove(constants::kIsFinalEventKey, NULL); | 220 options->Remove(constants::kIsFinalEventKey, NULL); |
| 226 if (options->HasKey(constants::kOnEventKey)) | 221 if (options->HasKey(constants::kOnEventKey)) |
| 227 options->Remove(constants::kOnEventKey, NULL); | 222 options->Remove(constants::kOnEventKey, NULL); |
| 228 | 223 |
| 229 args->Set(1, options); | 224 args->Set(1, options.release()); |
| 230 args->Set(2, Value::CreateIntegerValue(utterance->id())); | 225 args->Set(2, Value::CreateIntegerValue(utterance->id())); |
| 231 | 226 |
| 232 scoped_ptr<extensions::Event> event(new extensions::Event( | 227 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 233 tts_engine_events::kOnSpeak, args.Pass())); | 228 tts_engine_events::kOnSpeak, args.Pass())); |
| 234 event->restrict_to_profile = utterance->profile(); | 229 event->restrict_to_profile = utterance->profile(); |
| 235 extensions::ExtensionSystem::Get(utterance->profile())->event_router()-> | 230 extensions::ExtensionSystem::Get(utterance->profile())->event_router()-> |
| 236 DispatchEventToExtension(utterance->extension_id(), event.Pass()); | 231 DispatchEventToExtension(utterance->extension_id(), event.Pass()); |
| 237 } | 232 } |
| 238 | 233 |
| 239 void ExtensionTtsEngineStop(Utterance* utterance) { | 234 void ExtensionTtsEngineStop(Utterance* utterance) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 std::string error_message; | 300 std::string error_message; |
| 306 event->GetString(constants::kErrorMessageKey, &error_message); | 301 event->GetString(constants::kErrorMessageKey, &error_message); |
| 307 controller->OnTtsEvent( | 302 controller->OnTtsEvent( |
| 308 utterance_id, TTS_EVENT_ERROR, char_index, error_message); | 303 utterance_id, TTS_EVENT_ERROR, char_index, error_message); |
| 309 } else { | 304 } else { |
| 310 EXTENSION_FUNCTION_VALIDATE(false); | 305 EXTENSION_FUNCTION_VALIDATE(false); |
| 311 } | 306 } |
| 312 | 307 |
| 313 return true; | 308 return true; |
| 314 } | 309 } |
| OLD | NEW |