Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsILocaleService.h" |
michael@0 | 8 | #include "nsISpeechService.h" |
michael@0 | 9 | #include "nsServiceManagerUtils.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "SpeechSynthesisUtterance.h" |
michael@0 | 12 | #include "SpeechSynthesisVoice.h" |
michael@0 | 13 | #include "nsSynthVoiceRegistry.h" |
michael@0 | 14 | #include "nsSpeechTask.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "nsString.h" |
michael@0 | 17 | #include "mozilla/StaticPtr.h" |
michael@0 | 18 | #include "mozilla/dom/ContentChild.h" |
michael@0 | 19 | #include "mozilla/dom/ContentParent.h" |
michael@0 | 20 | #include "mozilla/unused.h" |
michael@0 | 21 | |
michael@0 | 22 | #include "SpeechSynthesisChild.h" |
michael@0 | 23 | #include "SpeechSynthesisParent.h" |
michael@0 | 24 | |
michael@0 | 25 | #undef LOG |
michael@0 | 26 | #ifdef PR_LOGGING |
michael@0 | 27 | extern PRLogModuleInfo* GetSpeechSynthLog(); |
michael@0 | 28 | #define LOG(type, msg) PR_LOG(GetSpeechSynthLog(), type, msg) |
michael@0 | 29 | #else |
michael@0 | 30 | #define LOG(type, msg) |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | namespace { |
michael@0 | 34 | |
michael@0 | 35 | void |
michael@0 | 36 | GetAllSpeechSynthActors(InfallibleTArray<mozilla::dom::SpeechSynthesisParent*>& aActors) |
michael@0 | 37 | { |
michael@0 | 38 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 39 | MOZ_ASSERT(aActors.IsEmpty()); |
michael@0 | 40 | |
michael@0 | 41 | nsAutoTArray<mozilla::dom::ContentParent*, 20> contentActors; |
michael@0 | 42 | mozilla::dom::ContentParent::GetAll(contentActors); |
michael@0 | 43 | |
michael@0 | 44 | for (uint32_t contentIndex = 0; |
michael@0 | 45 | contentIndex < contentActors.Length(); |
michael@0 | 46 | ++contentIndex) { |
michael@0 | 47 | MOZ_ASSERT(contentActors[contentIndex]); |
michael@0 | 48 | |
michael@0 | 49 | AutoInfallibleTArray<mozilla::dom::PSpeechSynthesisParent*, 5> speechsynthActors; |
michael@0 | 50 | contentActors[contentIndex]->ManagedPSpeechSynthesisParent(speechsynthActors); |
michael@0 | 51 | |
michael@0 | 52 | for (uint32_t speechsynthIndex = 0; |
michael@0 | 53 | speechsynthIndex < speechsynthActors.Length(); |
michael@0 | 54 | ++speechsynthIndex) { |
michael@0 | 55 | MOZ_ASSERT(speechsynthActors[speechsynthIndex]); |
michael@0 | 56 | |
michael@0 | 57 | mozilla::dom::SpeechSynthesisParent* actor = |
michael@0 | 58 | static_cast<mozilla::dom::SpeechSynthesisParent*>(speechsynthActors[speechsynthIndex]); |
michael@0 | 59 | aActors.AppendElement(actor); |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | namespace mozilla { |
michael@0 | 66 | namespace dom { |
michael@0 | 67 | |
michael@0 | 68 | // VoiceData |
michael@0 | 69 | |
michael@0 | 70 | class VoiceData MOZ_FINAL |
michael@0 | 71 | { |
michael@0 | 72 | private: |
michael@0 | 73 | // Private destructor, to discourage deletion outside of Release(): |
michael@0 | 74 | ~VoiceData() {} |
michael@0 | 75 | |
michael@0 | 76 | public: |
michael@0 | 77 | VoiceData(nsISpeechService* aService, const nsAString& aUri, |
michael@0 | 78 | const nsAString& aName, const nsAString& aLang, bool aIsLocal) |
michael@0 | 79 | : mService(aService) |
michael@0 | 80 | , mUri(aUri) |
michael@0 | 81 | , mName(aName) |
michael@0 | 82 | , mLang(aLang) |
michael@0 | 83 | , mIsLocal(aIsLocal) {} |
michael@0 | 84 | |
michael@0 | 85 | NS_INLINE_DECL_REFCOUNTING(VoiceData) |
michael@0 | 86 | |
michael@0 | 87 | nsCOMPtr<nsISpeechService> mService; |
michael@0 | 88 | |
michael@0 | 89 | nsString mUri; |
michael@0 | 90 | |
michael@0 | 91 | nsString mName; |
michael@0 | 92 | |
michael@0 | 93 | nsString mLang; |
michael@0 | 94 | |
michael@0 | 95 | bool mIsLocal; |
michael@0 | 96 | }; |
michael@0 | 97 | |
michael@0 | 98 | // nsSynthVoiceRegistry |
michael@0 | 99 | |
michael@0 | 100 | static StaticRefPtr<nsSynthVoiceRegistry> gSynthVoiceRegistry; |
michael@0 | 101 | |
michael@0 | 102 | NS_IMPL_ISUPPORTS(nsSynthVoiceRegistry, nsISynthVoiceRegistry) |
michael@0 | 103 | |
michael@0 | 104 | nsSynthVoiceRegistry::nsSynthVoiceRegistry() |
michael@0 | 105 | : mSpeechSynthChild(nullptr) |
michael@0 | 106 | { |
michael@0 | 107 | if (XRE_GetProcessType() == GeckoProcessType_Content) { |
michael@0 | 108 | |
michael@0 | 109 | mSpeechSynthChild = new SpeechSynthesisChild(); |
michael@0 | 110 | ContentChild::GetSingleton()->SendPSpeechSynthesisConstructor(mSpeechSynthChild); |
michael@0 | 111 | |
michael@0 | 112 | InfallibleTArray<RemoteVoice> voices; |
michael@0 | 113 | InfallibleTArray<nsString> defaults; |
michael@0 | 114 | |
michael@0 | 115 | mSpeechSynthChild->SendReadVoiceList(&voices, &defaults); |
michael@0 | 116 | |
michael@0 | 117 | for (uint32_t i = 0; i < voices.Length(); ++i) { |
michael@0 | 118 | RemoteVoice voice = voices[i]; |
michael@0 | 119 | AddVoiceImpl(nullptr, voice.voiceURI(), |
michael@0 | 120 | voice.name(), voice.lang(), |
michael@0 | 121 | voice.localService()); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | for (uint32_t i = 0; i < defaults.Length(); ++i) { |
michael@0 | 125 | SetDefaultVoice(defaults[i], true); |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | nsSynthVoiceRegistry::~nsSynthVoiceRegistry() |
michael@0 | 131 | { |
michael@0 | 132 | LOG(PR_LOG_DEBUG, ("~nsSynthVoiceRegistry")); |
michael@0 | 133 | |
michael@0 | 134 | // mSpeechSynthChild's lifecycle is managed by the Content protocol. |
michael@0 | 135 | mSpeechSynthChild = nullptr; |
michael@0 | 136 | |
michael@0 | 137 | mUriVoiceMap.Clear(); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | nsSynthVoiceRegistry* |
michael@0 | 141 | nsSynthVoiceRegistry::GetInstance() |
michael@0 | 142 | { |
michael@0 | 143 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 144 | |
michael@0 | 145 | if (!gSynthVoiceRegistry) { |
michael@0 | 146 | gSynthVoiceRegistry = new nsSynthVoiceRegistry(); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | return gSynthVoiceRegistry; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | already_AddRefed<nsSynthVoiceRegistry> |
michael@0 | 153 | nsSynthVoiceRegistry::GetInstanceForService() |
michael@0 | 154 | { |
michael@0 | 155 | nsRefPtr<nsSynthVoiceRegistry> registry = GetInstance(); |
michael@0 | 156 | |
michael@0 | 157 | return registry.forget(); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | void |
michael@0 | 161 | nsSynthVoiceRegistry::Shutdown() |
michael@0 | 162 | { |
michael@0 | 163 | LOG(PR_LOG_DEBUG, ("[%s] nsSynthVoiceRegistry::Shutdown()", |
michael@0 | 164 | (XRE_GetProcessType() == GeckoProcessType_Content) ? "Content" : "Default")); |
michael@0 | 165 | gSynthVoiceRegistry = nullptr; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | void |
michael@0 | 169 | nsSynthVoiceRegistry::SendVoices(InfallibleTArray<RemoteVoice>* aVoices, |
michael@0 | 170 | InfallibleTArray<nsString>* aDefaults) |
michael@0 | 171 | { |
michael@0 | 172 | for (uint32_t i=0; i < mVoices.Length(); ++i) { |
michael@0 | 173 | nsRefPtr<VoiceData> voice = mVoices[i]; |
michael@0 | 174 | |
michael@0 | 175 | aVoices->AppendElement(RemoteVoice(voice->mUri, voice->mName, voice->mLang, |
michael@0 | 176 | voice->mIsLocal)); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | for (uint32_t i=0; i < mDefaultVoices.Length(); ++i) { |
michael@0 | 180 | aDefaults->AppendElement(mDefaultVoices[i]->mUri); |
michael@0 | 181 | } |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | void |
michael@0 | 185 | nsSynthVoiceRegistry::RecvRemoveVoice(const nsAString& aUri) |
michael@0 | 186 | { |
michael@0 | 187 | // If we dont have a local instance of the registry yet, we will recieve current |
michael@0 | 188 | // voices at contruction time. |
michael@0 | 189 | if(!gSynthVoiceRegistry) { |
michael@0 | 190 | return; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | gSynthVoiceRegistry->RemoveVoice(nullptr, aUri); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | void |
michael@0 | 197 | nsSynthVoiceRegistry::RecvAddVoice(const RemoteVoice& aVoice) |
michael@0 | 198 | { |
michael@0 | 199 | // If we dont have a local instance of the registry yet, we will recieve current |
michael@0 | 200 | // voices at contruction time. |
michael@0 | 201 | if(!gSynthVoiceRegistry) { |
michael@0 | 202 | return; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | gSynthVoiceRegistry->AddVoiceImpl(nullptr, aVoice.voiceURI(), |
michael@0 | 206 | aVoice.name(), aVoice.lang(), |
michael@0 | 207 | aVoice.localService()); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | void |
michael@0 | 211 | nsSynthVoiceRegistry::RecvSetDefaultVoice(const nsAString& aUri, bool aIsDefault) |
michael@0 | 212 | { |
michael@0 | 213 | // If we dont have a local instance of the registry yet, we will recieve current |
michael@0 | 214 | // voices at contruction time. |
michael@0 | 215 | if(!gSynthVoiceRegistry) { |
michael@0 | 216 | return; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | gSynthVoiceRegistry->SetDefaultVoice(aUri, aIsDefault); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | NS_IMETHODIMP |
michael@0 | 223 | nsSynthVoiceRegistry::AddVoice(nsISpeechService* aService, |
michael@0 | 224 | const nsAString& aUri, |
michael@0 | 225 | const nsAString& aName, |
michael@0 | 226 | const nsAString& aLang, |
michael@0 | 227 | bool aLocalService) |
michael@0 | 228 | { |
michael@0 | 229 | LOG(PR_LOG_DEBUG, |
michael@0 | 230 | ("nsSynthVoiceRegistry::AddVoice uri='%s' name='%s' lang='%s' local=%s", |
michael@0 | 231 | NS_ConvertUTF16toUTF8(aUri).get(), NS_ConvertUTF16toUTF8(aName).get(), |
michael@0 | 232 | NS_ConvertUTF16toUTF8(aLang).get(), |
michael@0 | 233 | aLocalService ? "true" : "false")); |
michael@0 | 234 | |
michael@0 | 235 | NS_ENSURE_FALSE(XRE_GetProcessType() == GeckoProcessType_Content, |
michael@0 | 236 | NS_ERROR_NOT_AVAILABLE); |
michael@0 | 237 | |
michael@0 | 238 | return AddVoiceImpl(aService, aUri, aName, aLang, |
michael@0 | 239 | aLocalService); |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | NS_IMETHODIMP |
michael@0 | 243 | nsSynthVoiceRegistry::RemoveVoice(nsISpeechService* aService, |
michael@0 | 244 | const nsAString& aUri) |
michael@0 | 245 | { |
michael@0 | 246 | LOG(PR_LOG_DEBUG, |
michael@0 | 247 | ("nsSynthVoiceRegistry::RemoveVoice uri='%s' (%s)", |
michael@0 | 248 | NS_ConvertUTF16toUTF8(aUri).get(), |
michael@0 | 249 | (XRE_GetProcessType() == GeckoProcessType_Content) ? "child" : "parent")); |
michael@0 | 250 | |
michael@0 | 251 | bool found = false; |
michael@0 | 252 | VoiceData* retval = mUriVoiceMap.GetWeak(aUri, &found); |
michael@0 | 253 | |
michael@0 | 254 | NS_ENSURE_TRUE(found, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 255 | NS_ENSURE_TRUE(aService == retval->mService, NS_ERROR_INVALID_ARG); |
michael@0 | 256 | |
michael@0 | 257 | mVoices.RemoveElement(retval); |
michael@0 | 258 | mDefaultVoices.RemoveElement(retval); |
michael@0 | 259 | mUriVoiceMap.Remove(aUri); |
michael@0 | 260 | |
michael@0 | 261 | nsTArray<SpeechSynthesisParent*> ssplist; |
michael@0 | 262 | GetAllSpeechSynthActors(ssplist); |
michael@0 | 263 | |
michael@0 | 264 | for (uint32_t i = 0; i < ssplist.Length(); ++i) |
michael@0 | 265 | unused << ssplist[i]->SendVoiceRemoved(nsString(aUri)); |
michael@0 | 266 | |
michael@0 | 267 | return NS_OK; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | NS_IMETHODIMP |
michael@0 | 271 | nsSynthVoiceRegistry::SetDefaultVoice(const nsAString& aUri, |
michael@0 | 272 | bool aIsDefault) |
michael@0 | 273 | { |
michael@0 | 274 | bool found = false; |
michael@0 | 275 | VoiceData* retval = mUriVoiceMap.GetWeak(aUri, &found); |
michael@0 | 276 | NS_ENSURE_TRUE(found, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 277 | |
michael@0 | 278 | mDefaultVoices.RemoveElement(retval); |
michael@0 | 279 | |
michael@0 | 280 | LOG(PR_LOG_DEBUG, ("nsSynthVoiceRegistry::SetDefaultVoice %s %s", |
michael@0 | 281 | NS_ConvertUTF16toUTF8(aUri).get(), |
michael@0 | 282 | aIsDefault ? "true" : "false")); |
michael@0 | 283 | |
michael@0 | 284 | if (aIsDefault) { |
michael@0 | 285 | mDefaultVoices.AppendElement(retval); |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | if (XRE_GetProcessType() == GeckoProcessType_Default) { |
michael@0 | 289 | nsTArray<SpeechSynthesisParent*> ssplist; |
michael@0 | 290 | GetAllSpeechSynthActors(ssplist); |
michael@0 | 291 | |
michael@0 | 292 | for (uint32_t i = 0; i < ssplist.Length(); ++i) { |
michael@0 | 293 | unused << ssplist[i]->SendSetDefaultVoice(nsString(aUri), aIsDefault); |
michael@0 | 294 | } |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | return NS_OK; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | NS_IMETHODIMP |
michael@0 | 301 | nsSynthVoiceRegistry::GetVoiceCount(uint32_t* aRetval) |
michael@0 | 302 | { |
michael@0 | 303 | *aRetval = mVoices.Length(); |
michael@0 | 304 | |
michael@0 | 305 | return NS_OK; |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | NS_IMETHODIMP |
michael@0 | 309 | nsSynthVoiceRegistry::GetVoice(uint32_t aIndex, nsAString& aRetval) |
michael@0 | 310 | { |
michael@0 | 311 | NS_ENSURE_TRUE(aIndex < mVoices.Length(), NS_ERROR_INVALID_ARG); |
michael@0 | 312 | |
michael@0 | 313 | aRetval = mVoices[aIndex]->mUri; |
michael@0 | 314 | |
michael@0 | 315 | return NS_OK; |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | NS_IMETHODIMP |
michael@0 | 319 | nsSynthVoiceRegistry::IsDefaultVoice(const nsAString& aUri, bool* aRetval) |
michael@0 | 320 | { |
michael@0 | 321 | bool found; |
michael@0 | 322 | VoiceData* voice = mUriVoiceMap.GetWeak(aUri, &found); |
michael@0 | 323 | NS_ENSURE_TRUE(found, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 324 | |
michael@0 | 325 | for (int32_t i = mDefaultVoices.Length(); i > 0; ) { |
michael@0 | 326 | VoiceData* defaultVoice = mDefaultVoices[--i]; |
michael@0 | 327 | |
michael@0 | 328 | if (voice->mLang.Equals(defaultVoice->mLang)) { |
michael@0 | 329 | *aRetval = voice == defaultVoice; |
michael@0 | 330 | return NS_OK; |
michael@0 | 331 | } |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | *aRetval = false; |
michael@0 | 335 | return NS_OK; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | NS_IMETHODIMP |
michael@0 | 339 | nsSynthVoiceRegistry::IsLocalVoice(const nsAString& aUri, bool* aRetval) |
michael@0 | 340 | { |
michael@0 | 341 | bool found; |
michael@0 | 342 | VoiceData* voice = mUriVoiceMap.GetWeak(aUri, &found); |
michael@0 | 343 | NS_ENSURE_TRUE(found, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 344 | |
michael@0 | 345 | *aRetval = voice->mIsLocal; |
michael@0 | 346 | return NS_OK; |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | NS_IMETHODIMP |
michael@0 | 350 | nsSynthVoiceRegistry::GetVoiceLang(const nsAString& aUri, nsAString& aRetval) |
michael@0 | 351 | { |
michael@0 | 352 | bool found; |
michael@0 | 353 | VoiceData* voice = mUriVoiceMap.GetWeak(aUri, &found); |
michael@0 | 354 | NS_ENSURE_TRUE(found, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 355 | |
michael@0 | 356 | aRetval = voice->mLang; |
michael@0 | 357 | return NS_OK; |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | NS_IMETHODIMP |
michael@0 | 361 | nsSynthVoiceRegistry::GetVoiceName(const nsAString& aUri, nsAString& aRetval) |
michael@0 | 362 | { |
michael@0 | 363 | bool found; |
michael@0 | 364 | VoiceData* voice = mUriVoiceMap.GetWeak(aUri, &found); |
michael@0 | 365 | NS_ENSURE_TRUE(found, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 366 | |
michael@0 | 367 | aRetval = voice->mName; |
michael@0 | 368 | return NS_OK; |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | nsresult |
michael@0 | 372 | nsSynthVoiceRegistry::AddVoiceImpl(nsISpeechService* aService, |
michael@0 | 373 | const nsAString& aUri, |
michael@0 | 374 | const nsAString& aName, |
michael@0 | 375 | const nsAString& aLang, |
michael@0 | 376 | bool aLocalService) |
michael@0 | 377 | { |
michael@0 | 378 | bool found = false; |
michael@0 | 379 | mUriVoiceMap.GetWeak(aUri, &found); |
michael@0 | 380 | NS_ENSURE_FALSE(found, NS_ERROR_INVALID_ARG); |
michael@0 | 381 | |
michael@0 | 382 | nsRefPtr<VoiceData> voice = new VoiceData(aService, aUri, aName, aLang, |
michael@0 | 383 | aLocalService); |
michael@0 | 384 | |
michael@0 | 385 | mVoices.AppendElement(voice); |
michael@0 | 386 | mUriVoiceMap.Put(aUri, voice); |
michael@0 | 387 | |
michael@0 | 388 | nsTArray<SpeechSynthesisParent*> ssplist; |
michael@0 | 389 | GetAllSpeechSynthActors(ssplist); |
michael@0 | 390 | |
michael@0 | 391 | if (!ssplist.IsEmpty()) { |
michael@0 | 392 | mozilla::dom::RemoteVoice ssvoice(nsString(aUri), |
michael@0 | 393 | nsString(aName), |
michael@0 | 394 | nsString(aLang), |
michael@0 | 395 | aLocalService); |
michael@0 | 396 | |
michael@0 | 397 | for (uint32_t i = 0; i < ssplist.Length(); ++i) { |
michael@0 | 398 | unused << ssplist[i]->SendVoiceAdded(ssvoice); |
michael@0 | 399 | } |
michael@0 | 400 | } |
michael@0 | 401 | |
michael@0 | 402 | return NS_OK; |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | bool |
michael@0 | 406 | nsSynthVoiceRegistry::FindVoiceByLang(const nsAString& aLang, |
michael@0 | 407 | VoiceData** aRetval) |
michael@0 | 408 | { |
michael@0 | 409 | nsAString::const_iterator dashPos, start, end; |
michael@0 | 410 | aLang.BeginReading(start); |
michael@0 | 411 | aLang.EndReading(end); |
michael@0 | 412 | |
michael@0 | 413 | while (true) { |
michael@0 | 414 | nsAutoString langPrefix(Substring(start, end)); |
michael@0 | 415 | |
michael@0 | 416 | for (int32_t i = mDefaultVoices.Length(); i > 0; ) { |
michael@0 | 417 | VoiceData* voice = mDefaultVoices[--i]; |
michael@0 | 418 | |
michael@0 | 419 | if (StringBeginsWith(voice->mLang, langPrefix)) { |
michael@0 | 420 | *aRetval = voice; |
michael@0 | 421 | return true; |
michael@0 | 422 | } |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | for (int32_t i = mVoices.Length(); i > 0; ) { |
michael@0 | 426 | VoiceData* voice = mVoices[--i]; |
michael@0 | 427 | |
michael@0 | 428 | if (StringBeginsWith(voice->mLang, langPrefix)) { |
michael@0 | 429 | *aRetval = voice; |
michael@0 | 430 | return true; |
michael@0 | 431 | } |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | dashPos = end; |
michael@0 | 435 | end = start; |
michael@0 | 436 | |
michael@0 | 437 | if (!RFindInReadable(NS_LITERAL_STRING("-"), end, dashPos)) { |
michael@0 | 438 | break; |
michael@0 | 439 | } |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | return false; |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | VoiceData* |
michael@0 | 446 | nsSynthVoiceRegistry::FindBestMatch(const nsAString& aUri, |
michael@0 | 447 | const nsAString& aLang) |
michael@0 | 448 | { |
michael@0 | 449 | if (mVoices.IsEmpty()) { |
michael@0 | 450 | return nullptr; |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | bool found = false; |
michael@0 | 454 | VoiceData* retval = mUriVoiceMap.GetWeak(aUri, &found); |
michael@0 | 455 | |
michael@0 | 456 | if (found) { |
michael@0 | 457 | LOG(PR_LOG_DEBUG, ("nsSynthVoiceRegistry::FindBestMatch - Matched URI")); |
michael@0 | 458 | return retval; |
michael@0 | 459 | } |
michael@0 | 460 | |
michael@0 | 461 | // Try finding a match for given voice. |
michael@0 | 462 | if (!aLang.IsVoid() && !aLang.IsEmpty()) { |
michael@0 | 463 | if (FindVoiceByLang(aLang, &retval)) { |
michael@0 | 464 | LOG(PR_LOG_DEBUG, |
michael@0 | 465 | ("nsSynthVoiceRegistry::FindBestMatch - Matched language (%s ~= %s)", |
michael@0 | 466 | NS_ConvertUTF16toUTF8(aLang).get(), |
michael@0 | 467 | NS_ConvertUTF16toUTF8(retval->mLang).get())); |
michael@0 | 468 | |
michael@0 | 469 | return retval; |
michael@0 | 470 | } |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | // Try UI language. |
michael@0 | 474 | nsresult rv; |
michael@0 | 475 | nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv); |
michael@0 | 476 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 477 | |
michael@0 | 478 | nsAutoString uiLang; |
michael@0 | 479 | rv = localeService->GetLocaleComponentForUserAgent(uiLang); |
michael@0 | 480 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 481 | |
michael@0 | 482 | if (FindVoiceByLang(uiLang, &retval)) { |
michael@0 | 483 | LOG(PR_LOG_DEBUG, |
michael@0 | 484 | ("nsSynthVoiceRegistry::FindBestMatch - Matched UI language (%s ~= %s)", |
michael@0 | 485 | NS_ConvertUTF16toUTF8(uiLang).get(), |
michael@0 | 486 | NS_ConvertUTF16toUTF8(retval->mLang).get())); |
michael@0 | 487 | |
michael@0 | 488 | return retval; |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | // Try en-US, the language of locale "C" |
michael@0 | 492 | if (FindVoiceByLang(NS_LITERAL_STRING("en-US"), &retval)) { |
michael@0 | 493 | LOG(PR_LOG_DEBUG, |
michael@0 | 494 | ("nsSynthVoiceRegistry::FindBestMatch - Matched C locale language (en-US ~= %s)", |
michael@0 | 495 | NS_ConvertUTF16toUTF8(retval->mLang).get())); |
michael@0 | 496 | |
michael@0 | 497 | return retval; |
michael@0 | 498 | } |
michael@0 | 499 | |
michael@0 | 500 | // The top default voice is better than nothing... |
michael@0 | 501 | if (!mDefaultVoices.IsEmpty()) { |
michael@0 | 502 | return mDefaultVoices.LastElement(); |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | return nullptr; |
michael@0 | 506 | } |
michael@0 | 507 | |
michael@0 | 508 | already_AddRefed<nsSpeechTask> |
michael@0 | 509 | nsSynthVoiceRegistry::SpeakUtterance(SpeechSynthesisUtterance& aUtterance, |
michael@0 | 510 | const nsAString& aDocLang) |
michael@0 | 511 | { |
michael@0 | 512 | nsString lang = nsString(aUtterance.mLang.IsEmpty() ? aDocLang : aUtterance.mLang); |
michael@0 | 513 | nsAutoString uri; |
michael@0 | 514 | |
michael@0 | 515 | if (aUtterance.mVoice) { |
michael@0 | 516 | aUtterance.mVoice->GetVoiceURI(uri); |
michael@0 | 517 | } |
michael@0 | 518 | |
michael@0 | 519 | nsRefPtr<nsSpeechTask> task; |
michael@0 | 520 | if (XRE_GetProcessType() == GeckoProcessType_Content) { |
michael@0 | 521 | task = new SpeechTaskChild(&aUtterance); |
michael@0 | 522 | SpeechSynthesisRequestChild* actor = |
michael@0 | 523 | new SpeechSynthesisRequestChild(static_cast<SpeechTaskChild*>(task.get())); |
michael@0 | 524 | mSpeechSynthChild->SendPSpeechSynthesisRequestConstructor(actor, |
michael@0 | 525 | aUtterance.mText, |
michael@0 | 526 | lang, |
michael@0 | 527 | uri, |
michael@0 | 528 | aUtterance.Volume(), |
michael@0 | 529 | aUtterance.Rate(), |
michael@0 | 530 | aUtterance.Pitch()); |
michael@0 | 531 | } else { |
michael@0 | 532 | task = new nsSpeechTask(&aUtterance); |
michael@0 | 533 | Speak(aUtterance.mText, lang, uri, |
michael@0 | 534 | aUtterance.Rate(), aUtterance.Pitch(), task); |
michael@0 | 535 | } |
michael@0 | 536 | |
michael@0 | 537 | return task.forget(); |
michael@0 | 538 | } |
michael@0 | 539 | |
michael@0 | 540 | void |
michael@0 | 541 | nsSynthVoiceRegistry::Speak(const nsAString& aText, |
michael@0 | 542 | const nsAString& aLang, |
michael@0 | 543 | const nsAString& aUri, |
michael@0 | 544 | const float& aRate, |
michael@0 | 545 | const float& aPitch, |
michael@0 | 546 | nsSpeechTask* aTask) |
michael@0 | 547 | { |
michael@0 | 548 | LOG(PR_LOG_DEBUG, |
michael@0 | 549 | ("nsSynthVoiceRegistry::Speak text='%s' lang='%s' uri='%s' rate=%f pitch=%f", |
michael@0 | 550 | NS_ConvertUTF16toUTF8(aText).get(), NS_ConvertUTF16toUTF8(aLang).get(), |
michael@0 | 551 | NS_ConvertUTF16toUTF8(aUri).get(), aRate, aPitch)); |
michael@0 | 552 | |
michael@0 | 553 | VoiceData* voice = FindBestMatch(aUri, aLang); |
michael@0 | 554 | |
michael@0 | 555 | if (!voice) { |
michael@0 | 556 | NS_WARNING("No voices found."); |
michael@0 | 557 | aTask->DispatchError(0, 0); |
michael@0 | 558 | return; |
michael@0 | 559 | } |
michael@0 | 560 | |
michael@0 | 561 | LOG(PR_LOG_DEBUG, ("nsSynthVoiceRegistry::Speak - Using voice URI: %s", |
michael@0 | 562 | NS_ConvertUTF16toUTF8(voice->mUri).get())); |
michael@0 | 563 | |
michael@0 | 564 | SpeechServiceType serviceType; |
michael@0 | 565 | |
michael@0 | 566 | DebugOnly<nsresult> rv = voice->mService->GetServiceType(&serviceType); |
michael@0 | 567 | NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to get speech service type"); |
michael@0 | 568 | |
michael@0 | 569 | if (serviceType == nsISpeechService::SERVICETYPE_INDIRECT_AUDIO) { |
michael@0 | 570 | aTask->SetIndirectAudio(true); |
michael@0 | 571 | } |
michael@0 | 572 | |
michael@0 | 573 | voice->mService->Speak(aText, voice->mUri, aRate, aPitch, aTask); |
michael@0 | 574 | } |
michael@0 | 575 | |
michael@0 | 576 | } // namespace dom |
michael@0 | 577 | } // namespace mozilla |