content/media/webspeech/synth/nsSynthVoiceRegistry.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial