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()); |
tommi (sloooow) - chröme
2013/03/07 13:04:46
nit: if you can assume that |out_voices| is always
dmazzoni
2013/03/19 17:30:22
Actually we can't assume that - there are also pla
tommi (sloooow) - chröme
2013/03/21 14:34:02
OK as is
| |
68 VoiceData* result_voice = &out_voices->back(); | |
hans
2013/03/09 14:19:52
i would have made result_voice a reference instead
dmazzoni
2013/03/19 17:30:22
Done.
| |
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 = |
tommi (sloooow) - chröme
2013/03/07 13:04:46
std::copy now that both variables are stl?
dmazzoni
2013/03/19 17:30:22
I tried this and it crashes - any idea what I'm do
tommi (sloooow) - chröme
2013/03/21 14:34:02
hmm... maybe you need to do something like:
resul
| |
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 } |
82 // If the extension sends end events, the controller will handle | 81 // If the extension sends end events, the controller will handle |
83 // queueing and send interrupted and cancelled events. | 82 // queueing and send interrupted and cancelled events. |
84 if (voice.event_types.find(constants::kEventTypeEnd) != | 83 if (voice.event_types.find(constants::kEventTypeEnd) != |
85 voice.event_types.end()) { | 84 voice.event_types.end()) { |
86 event_types->Append( | 85 result_voice->events.push_back(constants::kEventTypeCancelled); |
87 Value::CreateStringValue(constants::kEventTypeCancelled)); | 86 result_voice->events.push_back(constants::kEventTypeInterrupted); |
88 event_types->Append(Value::CreateStringValue( | |
89 constants::kEventTypeInterrupted)); | |
90 } | 87 } |
91 | |
92 result_voice->Set(constants::kEventTypesKey, event_types); | |
93 result_voices->Append(result_voice); | |
94 } | 88 } |
95 } | 89 } |
96 } | 90 } |
97 | 91 |
98 bool GetMatchingExtensionVoice( | 92 bool GetMatchingExtensionVoice( |
99 Utterance* utterance, | 93 Utterance* utterance, |
100 const Extension** matching_extension, | 94 const Extension** matching_extension, |
101 size_t* voice_index) { | 95 size_t* voice_index) { |
102 // This will only happen during unit testing. Otherwise, an utterance | 96 // This will only happen during unit testing. Otherwise, an utterance |
103 // will always have an associated profile. | 97 // will always have an associated profile. |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 std::string error_message; | 299 std::string error_message; |
306 event->GetString(constants::kErrorMessageKey, &error_message); | 300 event->GetString(constants::kErrorMessageKey, &error_message); |
307 controller->OnTtsEvent( | 301 controller->OnTtsEvent( |
308 utterance_id, TTS_EVENT_ERROR, char_index, error_message); | 302 utterance_id, TTS_EVENT_ERROR, char_index, error_message); |
309 } else { | 303 } else { |
310 EXTENSION_FUNCTION_VALIDATE(false); | 304 EXTENSION_FUNCTION_VALIDATE(false); |
311 } | 305 } |
312 | 306 |
313 return true; | 307 return true; |
314 } | 308 } |
OLD | NEW |