Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: chrome/browser/speech/extension_api/tts_extension_api.cc

Issue 12589005: Implement web speech synthesis. (Closed) Base URL: http://git.chromium.org/chromium/src.git@webtts
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 std::string TtsEventTypeToString(TtsEventType event_type) {
tommi (sloooow) - chröme 2013/03/07 13:04:46 nit: is there a reason for returning a string obje
dmazzoni 2013/03/19 17:30:22 Changed to const char *, that way no string copyin
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 std::string();
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 DictionaryValue* details = new DictionaryValue();
hans 2013/03/09 14:19:52 maybe make this a scoped_ptr?
dmazzoni 2013/03/19 17:30:22 Done.
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);
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;
tommi (sloooow) - chröme 2013/03/07 13:04:46 thinking out loud here - is there some way for us
dmazzoni 2013/03/19 17:30:22 Since TTS spans processes, we use a simple convent
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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 ListValue* result_voices = new ListValue();
267 for (size_t i = 0; i < voices.size(); i++) {
tommi (sloooow) - chröme 2013/03/07 13:04:46 ++i
dmazzoni 2013/03/19 17:30:22 Done throughout
268 const VoiceData& voice = voices[i];
269 DictionaryValue* result_voice = new DictionaryValue();
hans 2013/03/09 14:19:52 scoped_ptr?
dmazzoni 2013/03/19 17:30:22 Same - this gets owned by the ListValue.
270 result_voice->SetString(constants::kVoiceNameKey, voice.name);
271 if (!voice.lang.empty())
tommi (sloooow) - chröme 2013/03/07 13:04:46 I'm guessing that if all of these are empty, then
dmazzoni 2013/03/19 17:30:22 Well, it still has a name... Currently support fo
tommi (sloooow) - chröme 2013/03/21 14:34:02 OK
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();
hans 2013/03/09 14:19:52 scoped_ptr?
279 for (size_t j = 0; j < voice.events.size(); j++)
tommi (sloooow) - chröme 2013/03/07 13:04:46 ++j
280 event_types->Append(Value::CreateStringValue(voice.events[j]));
281 result_voice->Set(constants::kEventTypesKey, event_types);
hans 2013/03/09 14:19:52 naive question because I don't have the whole pict
dmazzoni 2013/03/19 17:30:22 It's done this way because it's a JavaScript API.
282
283 result_voices->Append(result_voice);
284 }
285
286 SetResult(result_voices);
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698