Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / speech / extension_api / tts_engine_extension_api.cc
blobbc050e17c84965f2e3315e854f37de1b5edbd2c3
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
3 // found in the LICENSE file.
5 #include "chrome/browser/speech/extension_api/tts_engine_extension_api.h"
7 #include <string>
9 #include "base/json/json_writer.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/component_loader.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/speech/extension_api/tts_engine_extension_observer.h"
15 #include "chrome/browser/speech/extension_api/tts_extension_api.h"
16 #include "chrome/browser/speech/extension_api/tts_extension_api_constants.h"
17 #include "chrome/browser/speech/tts_controller.h"
18 #include "chrome/common/extensions/api/speech/tts_engine_manifest_handler.h"
19 #include "chrome/common/extensions/extension_constants.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/common/console_message_level.h"
23 #include "extensions/browser/event_router.h"
24 #include "extensions/browser/extension_host.h"
25 #include "extensions/browser/extension_registry.h"
26 #include "extensions/browser/extension_system.h"
27 #include "extensions/browser/process_manager.h"
28 #include "extensions/common/extension.h"
29 #include "extensions/common/extension_messages.h"
30 #include "extensions/common/extension_set.h"
31 #include "net/base/network_change_notifier.h"
33 using extensions::EventRouter;
34 using extensions::Extension;
35 using extensions::ExtensionSystem;
37 namespace constants = tts_extension_api_constants;
39 namespace tts_engine_events {
40 const char kOnSpeak[] = "ttsEngine.onSpeak";
41 const char kOnStop[] = "ttsEngine.onStop";
42 const char kOnPause[] = "ttsEngine.onPause";
43 const char kOnResume[] = "ttsEngine.onResume";
44 }; // namespace tts_engine_events
46 namespace {
48 void WarnIfMissingPauseOrResumeListener(
49 Profile* profile, EventRouter* event_router, std::string extension_id) {
50 bool has_onpause = event_router->ExtensionHasEventListener(
51 extension_id, tts_engine_events::kOnPause);
52 bool has_onresume = event_router->ExtensionHasEventListener(
53 extension_id, tts_engine_events::kOnResume);
54 if (has_onpause == has_onresume)
55 return;
57 extensions::ExtensionHost* host =
58 extensions::ProcessManager::Get(profile)
59 ->GetBackgroundHostForExtension(extension_id);
60 host->render_process_host()->Send(new ExtensionMsg_AddMessageToConsole(
61 host->render_view_host()->GetRoutingID(),
62 content::CONSOLE_MESSAGE_LEVEL_WARNING,
63 constants::kErrorMissingPauseOrResume));
66 } // namespace
68 TtsExtensionEngine* TtsExtensionEngine::GetInstance() {
69 return Singleton<TtsExtensionEngine>::get();
72 void TtsExtensionEngine::GetVoices(content::BrowserContext* browser_context,
73 std::vector<VoiceData>* out_voices) {
74 Profile* profile = Profile::FromBrowserContext(browser_context);
75 EventRouter* event_router = EventRouter::Get(profile);
76 DCHECK(event_router);
78 bool is_offline = (net::NetworkChangeNotifier::GetConnectionType() ==
79 net::NetworkChangeNotifier::CONNECTION_NONE);
81 const extensions::ExtensionSet& extensions =
82 extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
83 extensions::ExtensionSet::const_iterator iter;
84 for (iter = extensions.begin(); iter != extensions.end(); ++iter) {
85 const Extension* extension = iter->get();
87 if (!event_router->ExtensionHasEventListener(
88 extension->id(), tts_engine_events::kOnSpeak) ||
89 !event_router->ExtensionHasEventListener(
90 extension->id(), tts_engine_events::kOnStop)) {
91 continue;
94 const std::vector<extensions::TtsVoice>* tts_voices =
95 extensions::TtsVoice::GetTtsVoices(extension);
96 if (!tts_voices)
97 continue;
99 for (size_t i = 0; i < tts_voices->size(); ++i) {
100 const extensions::TtsVoice& voice = tts_voices->at(i);
102 // Don't return remote voices when the system is offline.
103 if (voice.remote && is_offline)
104 continue;
106 out_voices->push_back(VoiceData());
107 VoiceData& result_voice = out_voices->back();
109 result_voice.native = false;
110 result_voice.name = voice.voice_name;
111 result_voice.lang = voice.lang;
112 result_voice.remote = voice.remote;
113 result_voice.extension_id = extension->id();
114 if (voice.gender == constants::kGenderMale)
115 result_voice.gender = TTS_GENDER_MALE;
116 else if (voice.gender == constants::kGenderFemale)
117 result_voice.gender = TTS_GENDER_FEMALE;
118 else
119 result_voice.gender = TTS_GENDER_NONE;
121 for (std::set<std::string>::const_iterator iter =
122 voice.event_types.begin();
123 iter != voice.event_types.end();
124 ++iter) {
125 result_voice.events.insert(TtsEventTypeFromString(*iter));
128 // If the extension sends end events, the controller will handle
129 // queueing and send interrupted and cancelled events.
130 if (voice.event_types.find(constants::kEventTypeEnd) !=
131 voice.event_types.end()) {
132 result_voice.events.insert(TTS_EVENT_CANCELLED);
133 result_voice.events.insert(TTS_EVENT_INTERRUPTED);
139 void TtsExtensionEngine::Speak(Utterance* utterance,
140 const VoiceData& voice) {
141 // See if the engine supports the "end" event; if so, we can keep the
142 // utterance around and track it. If not, we're finished with this
143 // utterance now.
144 bool sends_end_event = voice.events.find(TTS_EVENT_END) != voice.events.end();
146 scoped_ptr<base::ListValue> args(new base::ListValue());
147 args->AppendString(utterance->text());
149 // Pass through most options to the speech engine, but remove some
150 // that are handled internally.
151 scoped_ptr<base::DictionaryValue> options(static_cast<base::DictionaryValue*>(
152 utterance->options()->DeepCopy()));
153 if (options->HasKey(constants::kRequiredEventTypesKey))
154 options->Remove(constants::kRequiredEventTypesKey, NULL);
155 if (options->HasKey(constants::kDesiredEventTypesKey))
156 options->Remove(constants::kDesiredEventTypesKey, NULL);
157 if (sends_end_event && options->HasKey(constants::kEnqueueKey))
158 options->Remove(constants::kEnqueueKey, NULL);
159 if (options->HasKey(constants::kSrcIdKey))
160 options->Remove(constants::kSrcIdKey, NULL);
161 if (options->HasKey(constants::kIsFinalEventKey))
162 options->Remove(constants::kIsFinalEventKey, NULL);
163 if (options->HasKey(constants::kOnEventKey))
164 options->Remove(constants::kOnEventKey, NULL);
166 // Get the volume, pitch, and rate, but only if they weren't already in
167 // the options. TODO(dmazzoni): these shouldn't be redundant.
168 // http://crbug.com/463264
169 if (!options->HasKey(constants::kRateKey)) {
170 options->SetDouble(constants::kRateKey,
171 utterance->continuous_parameters().rate);
173 if (!options->HasKey(constants::kPitchKey)) {
174 options->SetDouble(constants::kPitchKey,
175 utterance->continuous_parameters().pitch);
177 if (!options->HasKey(constants::kVolumeKey)) {
178 options->SetDouble(constants::kVolumeKey,
179 utterance->continuous_parameters().volume);
182 // Add the voice name and language to the options if they're not
183 // already there, since they might have been picked by the TTS controller
184 // rather than directly by the client that requested the speech.
185 if (!options->HasKey(constants::kVoiceNameKey))
186 options->SetString(constants::kVoiceNameKey, voice.name);
187 if (!options->HasKey(constants::kLangKey))
188 options->SetString(constants::kLangKey, voice.lang);
190 args->Append(options.release());
191 args->AppendInteger(utterance->id());
193 std::string json;
194 base::JSONWriter::Write(args.get(), &json);
196 scoped_ptr<extensions::Event> event(new extensions::Event(
197 tts_engine_events::kOnSpeak, args.Pass()));
198 Profile* profile = Profile::FromBrowserContext(utterance->browser_context());
199 event->restrict_to_browser_context = profile;
200 EventRouter::Get(profile)
201 ->DispatchEventToExtension(utterance->extension_id(), event.Pass());
204 void TtsExtensionEngine::Stop(Utterance* utterance) {
205 scoped_ptr<base::ListValue> args(new base::ListValue());
206 scoped_ptr<extensions::Event> event(new extensions::Event(
207 tts_engine_events::kOnStop, args.Pass()));
208 Profile* profile = Profile::FromBrowserContext(utterance->browser_context());
209 event->restrict_to_browser_context = profile;
210 EventRouter::Get(profile)
211 ->DispatchEventToExtension(utterance->extension_id(), event.Pass());
214 void TtsExtensionEngine::Pause(Utterance* utterance) {
215 scoped_ptr<base::ListValue> args(new base::ListValue());
216 scoped_ptr<extensions::Event> event(new extensions::Event(
217 tts_engine_events::kOnPause, args.Pass()));
218 Profile* profile = Profile::FromBrowserContext(utterance->browser_context());
219 event->restrict_to_browser_context = profile;
220 EventRouter* event_router = EventRouter::Get(profile);
221 std::string id = utterance->extension_id();
222 event_router->DispatchEventToExtension(id, event.Pass());
223 WarnIfMissingPauseOrResumeListener(profile, event_router, id);
226 void TtsExtensionEngine::Resume(Utterance* utterance) {
227 scoped_ptr<base::ListValue> args(new base::ListValue());
228 scoped_ptr<extensions::Event> event(new extensions::Event(
229 tts_engine_events::kOnResume, args.Pass()));
230 Profile* profile = Profile::FromBrowserContext(utterance->browser_context());
231 event->restrict_to_browser_context = profile;
232 EventRouter* event_router = EventRouter::Get(profile);
233 std::string id = utterance->extension_id();
234 event_router->DispatchEventToExtension(id, event.Pass());
235 WarnIfMissingPauseOrResumeListener(profile, event_router, id);
238 bool TtsExtensionEngine::LoadBuiltInTtsExtension(
239 content::BrowserContext* browser_context) {
240 #if defined(OS_CHROMEOS)
241 Profile* profile = Profile::FromBrowserContext(browser_context);
242 // Check to see if the engine was previously loaded.
243 if (TtsEngineExtensionObserver::GetInstance(profile)->SawExtensionLoad(
244 extension_misc::kSpeechSynthesisExtensionId, true)) {
245 return false;
248 // Load the component extension into this profile.
249 ExtensionService* extension_service =
250 extensions::ExtensionSystem::Get(profile)->extension_service();
251 DCHECK(extension_service);
252 extension_service->component_loader()
253 ->AddChromeOsSpeechSynthesisExtension();
254 return true;
255 #else
256 return false;
257 #endif
260 bool ExtensionTtsEngineSendTtsEventFunction::RunSync() {
261 int utterance_id;
262 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &utterance_id));
264 base::DictionaryValue* event;
265 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &event));
267 std::string event_type;
268 EXTENSION_FUNCTION_VALIDATE(
269 event->GetString(constants::kEventTypeKey, &event_type));
271 int char_index = 0;
272 if (event->HasKey(constants::kCharIndexKey)) {
273 EXTENSION_FUNCTION_VALIDATE(
274 event->GetInteger(constants::kCharIndexKey, &char_index));
277 // Make sure the extension has included this event type in its manifest.
278 bool event_type_allowed = false;
279 const std::vector<extensions::TtsVoice>* tts_voices =
280 extensions::TtsVoice::GetTtsVoices(extension());
281 if (!tts_voices) {
282 error_ = constants::kErrorUndeclaredEventType;
283 return false;
286 for (size_t i = 0; i < tts_voices->size(); i++) {
287 const extensions::TtsVoice& voice = tts_voices->at(i);
288 if (voice.event_types.find(event_type) != voice.event_types.end()) {
289 event_type_allowed = true;
290 break;
293 if (!event_type_allowed) {
294 error_ = constants::kErrorUndeclaredEventType;
295 return false;
298 TtsController* controller = TtsController::GetInstance();
299 if (event_type == constants::kEventTypeStart) {
300 controller->OnTtsEvent(
301 utterance_id, TTS_EVENT_START, char_index, std::string());
302 } else if (event_type == constants::kEventTypeEnd) {
303 controller->OnTtsEvent(
304 utterance_id, TTS_EVENT_END, char_index, std::string());
305 } else if (event_type == constants::kEventTypeWord) {
306 controller->OnTtsEvent(
307 utterance_id, TTS_EVENT_WORD, char_index, std::string());
308 } else if (event_type == constants::kEventTypeSentence) {
309 controller->OnTtsEvent(
310 utterance_id, TTS_EVENT_SENTENCE, char_index, std::string());
311 } else if (event_type == constants::kEventTypeMarker) {
312 controller->OnTtsEvent(
313 utterance_id, TTS_EVENT_MARKER, char_index, std::string());
314 } else if (event_type == constants::kEventTypeError) {
315 std::string error_message;
316 event->GetString(constants::kErrorMessageKey, &error_message);
317 controller->OnTtsEvent(
318 utterance_id, TTS_EVENT_ERROR, char_index, error_message);
319 } else if (event_type == constants::kEventTypePause) {
320 controller->OnTtsEvent(
321 utterance_id, TTS_EVENT_PAUSE, char_index, std::string());
322 } else if (event_type == constants::kEventTypeResume) {
323 controller->OnTtsEvent(
324 utterance_id, TTS_EVENT_RESUME, char_index, std::string());
325 } else {
326 EXTENSION_FUNCTION_VALIDATE(false);
329 return true;