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" |
| 11 #include "chrome/browser/extensions/event_router.h" |
11 #include "chrome/browser/extensions/extension_function_registry.h" | 12 #include "chrome/browser/extensions/extension_function_registry.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h" | 14 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h" |
14 #include "chrome/browser/speech/extension_api/tts_extension_api_constants.h" | 15 #include "chrome/browser/speech/extension_api/tts_extension_api_constants.h" |
15 #include "chrome/browser/speech/tts_controller.h" | 16 #include "chrome/browser/speech/tts_controller.h" |
16 #include "chrome/common/extensions/api/speech/tts_engine_manifest_handler.h" | 17 #include "chrome/common/extensions/api/speech/tts_engine_manifest_handler.h" |
17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
18 | 19 |
19 namespace constants = tts_extension_api_constants; | 20 namespace constants = tts_extension_api_constants; |
20 | 21 |
| 22 namespace events { |
| 23 const char kOnEvent[] = "tts.onEvent"; |
| 24 }; // namespace events |
| 25 |
| 26 namespace { |
| 27 |
| 28 const char *TtsEventTypeToString(TtsEventType event_type) { |
| 29 switch (event_type) { |
| 30 case TTS_EVENT_START: |
| 31 return constants::kEventTypeStart; |
| 32 case TTS_EVENT_END: |
| 33 return constants::kEventTypeEnd; |
| 34 case TTS_EVENT_WORD: |
| 35 return constants::kEventTypeWord; |
| 36 case TTS_EVENT_SENTENCE: |
| 37 return constants::kEventTypeSentence; |
| 38 case TTS_EVENT_MARKER: |
| 39 return constants::kEventTypeMarker; |
| 40 case TTS_EVENT_INTERRUPTED: |
| 41 return constants::kEventTypeInterrupted; |
| 42 case TTS_EVENT_CANCELLED: |
| 43 return constants::kEventTypeCancelled; |
| 44 case TTS_EVENT_ERROR: |
| 45 return constants::kEventTypeError; |
| 46 default: |
| 47 NOTREACHED(); |
| 48 return ""; |
| 49 } |
| 50 } |
| 51 |
| 52 } // anonymous namespace |
| 53 |
21 namespace extensions { | 54 namespace extensions { |
22 | 55 |
| 56 // One of these is constructed for each utterance, and deleted |
| 57 // when the utterance gets any final event. |
| 58 class TtsExtensionEventHandler : public UtteranceEventDelegate { |
| 59 public: |
| 60 virtual void OnTtsEvent(Utterance* utterance, |
| 61 TtsEventType event_type, |
| 62 int char_index, |
| 63 const std::string& error_message) OVERRIDE; |
| 64 }; |
| 65 |
| 66 void TtsExtensionEventHandler::OnTtsEvent(Utterance* utterance, |
| 67 TtsEventType event_type, |
| 68 int char_index, |
| 69 const std::string& error_message) { |
| 70 if (utterance->src_id() < 0) |
| 71 return; |
| 72 |
| 73 std::string event_type_string = TtsEventTypeToString(event_type); |
| 74 const std::set<std::string>& desired_event_types = |
| 75 utterance->desired_event_types(); |
| 76 if (desired_event_types.size() > 0 && |
| 77 desired_event_types.find(event_type_string) == |
| 78 desired_event_types.end()) { |
| 79 return; |
| 80 } |
| 81 |
| 82 scoped_ptr<DictionaryValue> details(new DictionaryValue()); |
| 83 if (char_index >= 0) |
| 84 details->SetInteger(constants::kCharIndexKey, char_index); |
| 85 details->SetString(constants::kEventTypeKey, event_type_string); |
| 86 if (event_type == TTS_EVENT_ERROR) { |
| 87 details->SetString(constants::kErrorMessageKey, error_message); |
| 88 } |
| 89 details->SetInteger(constants::kSrcIdKey, utterance->src_id()); |
| 90 details->SetBoolean(constants::kIsFinalEventKey, utterance->finished()); |
| 91 |
| 92 scoped_ptr<ListValue> arguments(new ListValue()); |
| 93 arguments->Set(0, details.release()); |
| 94 |
| 95 scoped_ptr<extensions::Event> event( |
| 96 new extensions::Event(events::kOnEvent, arguments.Pass())); |
| 97 event->restrict_to_profile = utterance->profile(); |
| 98 event->event_url = utterance->src_url(); |
| 99 extensions::ExtensionSystem::Get(utterance->profile())->event_router()-> |
| 100 DispatchEventToExtension(utterance->src_extension_id(), event.Pass()); |
| 101 |
| 102 if (utterance->finished()) |
| 103 delete this; |
| 104 } |
| 105 |
| 106 |
23 bool TtsSpeakFunction::RunImpl() { | 107 bool TtsSpeakFunction::RunImpl() { |
24 std::string text; | 108 std::string text; |
25 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text)); | 109 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text)); |
26 if (text.size() > 32768) { | 110 if (text.size() > 32768) { |
27 error_ = constants::kErrorUtteranceTooLong; | 111 error_ = constants::kErrorUtteranceTooLong; |
28 return false; | 112 return false; |
29 } | 113 } |
30 | 114 |
31 scoped_ptr<DictionaryValue> options(new DictionaryValue()); | 115 scoped_ptr<DictionaryValue> options(new DictionaryValue()); |
32 if (args_->GetSize() >= 2) { | 116 if (args_->GetSize() >= 2) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 if (options->HasKey(constants::kEnqueueKey)) { | 178 if (options->HasKey(constants::kEnqueueKey)) { |
95 EXTENSION_FUNCTION_VALIDATE( | 179 EXTENSION_FUNCTION_VALIDATE( |
96 options->GetBoolean(constants::kEnqueueKey, &can_enqueue)); | 180 options->GetBoolean(constants::kEnqueueKey, &can_enqueue)); |
97 } | 181 } |
98 | 182 |
99 std::set<std::string> required_event_types; | 183 std::set<std::string> required_event_types; |
100 if (options->HasKey(constants::kRequiredEventTypesKey)) { | 184 if (options->HasKey(constants::kRequiredEventTypesKey)) { |
101 ListValue* list; | 185 ListValue* list; |
102 EXTENSION_FUNCTION_VALIDATE( | 186 EXTENSION_FUNCTION_VALIDATE( |
103 options->GetList(constants::kRequiredEventTypesKey, &list)); | 187 options->GetList(constants::kRequiredEventTypesKey, &list)); |
104 for (size_t i = 0; i < list->GetSize(); i++) { | 188 for (size_t i = 0; i < list->GetSize(); ++i) { |
105 std::string event_type; | 189 std::string event_type; |
106 if (!list->GetString(i, &event_type)) | 190 if (!list->GetString(i, &event_type)) |
107 required_event_types.insert(event_type); | 191 required_event_types.insert(event_type); |
108 } | 192 } |
109 } | 193 } |
110 | 194 |
111 std::set<std::string> desired_event_types; | 195 std::set<std::string> desired_event_types; |
112 if (options->HasKey(constants::kDesiredEventTypesKey)) { | 196 if (options->HasKey(constants::kDesiredEventTypesKey)) { |
113 ListValue* list; | 197 ListValue* list; |
114 EXTENSION_FUNCTION_VALIDATE( | 198 EXTENSION_FUNCTION_VALIDATE( |
115 options->GetList(constants::kDesiredEventTypesKey, &list)); | 199 options->GetList(constants::kDesiredEventTypesKey, &list)); |
116 for (size_t i = 0; i < list->GetSize(); i++) { | 200 for (size_t i = 0; i < list->GetSize(); ++i) { |
117 std::string event_type; | 201 std::string event_type; |
118 if (!list->GetString(i, &event_type)) | 202 if (!list->GetString(i, &event_type)) |
119 desired_event_types.insert(event_type); | 203 desired_event_types.insert(event_type); |
120 } | 204 } |
121 } | 205 } |
122 | 206 |
123 std::string voice_extension_id; | 207 std::string voice_extension_id; |
124 if (options->HasKey(constants::kExtensionIdKey)) { | 208 if (options->HasKey(constants::kExtensionIdKey)) { |
125 EXTENSION_FUNCTION_VALIDATE( | 209 EXTENSION_FUNCTION_VALIDATE( |
126 options->GetString(constants::kExtensionIdKey, &voice_extension_id)); | 210 options->GetString(constants::kExtensionIdKey, &voice_extension_id)); |
(...skipping 23 matching lines...) Expand all Loading... |
150 utterance->set_src_id(src_id); | 234 utterance->set_src_id(src_id); |
151 utterance->set_src_url(source_url()); | 235 utterance->set_src_url(source_url()); |
152 utterance->set_lang(lang); | 236 utterance->set_lang(lang); |
153 utterance->set_gender(gender); | 237 utterance->set_gender(gender); |
154 utterance->set_continuous_parameters(continuous_params); | 238 utterance->set_continuous_parameters(continuous_params); |
155 utterance->set_can_enqueue(can_enqueue); | 239 utterance->set_can_enqueue(can_enqueue); |
156 utterance->set_required_event_types(required_event_types); | 240 utterance->set_required_event_types(required_event_types); |
157 utterance->set_desired_event_types(desired_event_types); | 241 utterance->set_desired_event_types(desired_event_types); |
158 utterance->set_extension_id(voice_extension_id); | 242 utterance->set_extension_id(voice_extension_id); |
159 utterance->set_options(options.get()); | 243 utterance->set_options(options.get()); |
| 244 utterance->set_event_delegate(new TtsExtensionEventHandler()); |
160 | 245 |
161 TtsController* controller = TtsController::GetInstance(); | 246 TtsController* controller = TtsController::GetInstance(); |
162 controller->SpeakOrEnqueue(utterance); | 247 controller->SpeakOrEnqueue(utterance); |
163 return true; | 248 return true; |
164 } | 249 } |
165 | 250 |
166 bool TtsStopSpeakingFunction::RunImpl() { | 251 bool TtsStopSpeakingFunction::RunImpl() { |
167 TtsController::GetInstance()->Stop(); | 252 TtsController::GetInstance()->Stop(); |
168 return true; | 253 return true; |
169 } | 254 } |
170 | 255 |
171 bool TtsIsSpeakingFunction::RunImpl() { | 256 bool TtsIsSpeakingFunction::RunImpl() { |
172 SetResult(Value::CreateBooleanValue( | 257 SetResult(Value::CreateBooleanValue( |
173 TtsController::GetInstance()->IsSpeaking())); | 258 TtsController::GetInstance()->IsSpeaking())); |
174 return true; | 259 return true; |
175 } | 260 } |
176 | 261 |
177 bool TtsGetVoicesFunction::RunImpl() { | 262 bool TtsGetVoicesFunction::RunImpl() { |
178 SetResult(TtsController::GetInstance()->GetVoices(profile())); | 263 std::vector<VoiceData> voices; |
| 264 TtsController::GetInstance()->GetVoices(profile(), &voices); |
| 265 |
| 266 scoped_ptr<ListValue> result_voices(new ListValue()); |
| 267 for (size_t i = 0; i < voices.size(); ++i) { |
| 268 const VoiceData& voice = voices[i]; |
| 269 DictionaryValue* result_voice = new DictionaryValue(); |
| 270 result_voice->SetString(constants::kVoiceNameKey, voice.name); |
| 271 if (!voice.lang.empty()) |
| 272 result_voice->SetString(constants::kLangKey, voice.lang); |
| 273 if (!voice.gender.empty()) |
| 274 result_voice->SetString(constants::kGenderKey, voice.gender); |
| 275 if (!voice.extension_id.empty()) |
| 276 result_voice->SetString(constants::kExtensionIdKey, voice.extension_id); |
| 277 |
| 278 ListValue* event_types = new ListValue(); |
| 279 for (size_t j = 0; j < voice.events.size(); ++j) |
| 280 event_types->Append(Value::CreateStringValue(voice.events[j])); |
| 281 result_voice->Set(constants::kEventTypesKey, event_types); |
| 282 |
| 283 result_voices->Append(result_voice); |
| 284 } |
| 285 |
| 286 SetResult(result_voices.release()); |
179 return true; | 287 return true; |
180 } | 288 } |
181 | 289 |
182 // static | 290 // static |
183 TtsAPI* TtsAPI::Get(Profile* profile) { | 291 TtsAPI* TtsAPI::Get(Profile* profile) { |
184 return ProfileKeyedAPIFactory<TtsAPI>::GetForProfile(profile); | 292 return ProfileKeyedAPIFactory<TtsAPI>::GetForProfile(profile); |
185 } | 293 } |
186 | 294 |
187 TtsAPI::TtsAPI(Profile* profile) { | 295 TtsAPI::TtsAPI(Profile* profile) { |
188 (new TtsEngineManifestHandler)->Register(); | 296 (new TtsEngineManifestHandler)->Register(); |
(...skipping 10 matching lines...) Expand all Loading... |
199 } | 307 } |
200 | 308 |
201 static base::LazyInstance<ProfileKeyedAPIFactory<TtsAPI> > | 309 static base::LazyInstance<ProfileKeyedAPIFactory<TtsAPI> > |
202 g_factory = LAZY_INSTANCE_INITIALIZER; | 310 g_factory = LAZY_INSTANCE_INITIALIZER; |
203 | 311 |
204 ProfileKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { | 312 ProfileKeyedAPIFactory<TtsAPI>* TtsAPI::GetFactoryInstance() { |
205 return &g_factory.Get(); | 313 return &g_factory.Get(); |
206 } | 314 } |
207 | 315 |
208 } // namespace extensions | 316 } // namespace extensions |
OLD | NEW |