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