content/media/webspeech/synth/SpeechSynthesis.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 "nsSpeechTask.h"
michael@0 8 #include "prlog.h"
michael@0 9
michael@0 10 #include "mozilla/dom/ContentChild.h"
michael@0 11 #include "mozilla/dom/Element.h"
michael@0 12
michael@0 13 #include "mozilla/dom/SpeechSynthesisBinding.h"
michael@0 14 #include "SpeechSynthesis.h"
michael@0 15 #include "nsSynthVoiceRegistry.h"
michael@0 16 #include "nsIDocument.h"
michael@0 17
michael@0 18 #undef LOG
michael@0 19 #ifdef PR_LOGGING
michael@0 20 PRLogModuleInfo*
michael@0 21 GetSpeechSynthLog()
michael@0 22 {
michael@0 23 static PRLogModuleInfo* sLog = nullptr;
michael@0 24
michael@0 25 if (!sLog) {
michael@0 26 sLog = PR_NewLogModule("SpeechSynthesis");
michael@0 27 }
michael@0 28
michael@0 29 return sLog;
michael@0 30 }
michael@0 31 #define LOG(type, msg) PR_LOG(GetSpeechSynthLog(), type, msg)
michael@0 32 #else
michael@0 33 #define LOG(type, msg)
michael@0 34 #endif
michael@0 35
michael@0 36 namespace mozilla {
michael@0 37 namespace dom {
michael@0 38
michael@0 39 static PLDHashOperator
michael@0 40 TraverseCachedVoices(const nsAString& aKey, SpeechSynthesisVoice* aEntry, void* aData)
michael@0 41 {
michael@0 42 nsCycleCollectionTraversalCallback* cb = static_cast<nsCycleCollectionTraversalCallback*>(aData);
michael@0 43 cb->NoteXPCOMChild(aEntry);
michael@0 44 return PL_DHASH_NEXT;
michael@0 45 }
michael@0 46
michael@0 47 NS_IMPL_CYCLE_COLLECTION_CLASS(SpeechSynthesis)
michael@0 48
michael@0 49 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(SpeechSynthesis)
michael@0 50 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
michael@0 51 NS_IMPL_CYCLE_COLLECTION_UNLINK(mCurrentTask)
michael@0 52 NS_IMPL_CYCLE_COLLECTION_UNLINK(mSpeechQueue)
michael@0 53 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
michael@0 54 tmp->mVoiceCache.Clear();
michael@0 55 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
michael@0 56
michael@0 57 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(SpeechSynthesis)
michael@0 58 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
michael@0 59 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCurrentTask)
michael@0 60 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSpeechQueue)
michael@0 61 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
michael@0 62 tmp->mVoiceCache.EnumerateRead(TraverseCachedVoices, &cb);
michael@0 63 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 64
michael@0 65 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(SpeechSynthesis)
michael@0 66 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
michael@0 67 NS_IMPL_CYCLE_COLLECTION_TRACE_END
michael@0 68
michael@0 69 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechSynthesis)
michael@0 70 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
michael@0 71 NS_INTERFACE_MAP_ENTRY(nsISupports)
michael@0 72 NS_INTERFACE_MAP_END
michael@0 73
michael@0 74 NS_IMPL_CYCLE_COLLECTING_ADDREF(SpeechSynthesis)
michael@0 75 NS_IMPL_CYCLE_COLLECTING_RELEASE(SpeechSynthesis)
michael@0 76
michael@0 77 SpeechSynthesis::SpeechSynthesis(nsPIDOMWindow* aParent)
michael@0 78 : mParent(aParent)
michael@0 79 {
michael@0 80 SetIsDOMBinding();
michael@0 81 }
michael@0 82
michael@0 83 SpeechSynthesis::~SpeechSynthesis()
michael@0 84 {
michael@0 85 }
michael@0 86
michael@0 87 JSObject*
michael@0 88 SpeechSynthesis::WrapObject(JSContext* aCx)
michael@0 89 {
michael@0 90 return SpeechSynthesisBinding::Wrap(aCx, this);
michael@0 91 }
michael@0 92
michael@0 93 nsIDOMWindow*
michael@0 94 SpeechSynthesis::GetParentObject() const
michael@0 95 {
michael@0 96 return mParent;
michael@0 97 }
michael@0 98
michael@0 99 bool
michael@0 100 SpeechSynthesis::Pending() const
michael@0 101 {
michael@0 102 switch (mSpeechQueue.Length()) {
michael@0 103 case 0:
michael@0 104 return false;
michael@0 105
michael@0 106 case 1:
michael@0 107 return mSpeechQueue.ElementAt(0)->GetState() == SpeechSynthesisUtterance::STATE_PENDING;
michael@0 108
michael@0 109 default:
michael@0 110 return true;
michael@0 111 }
michael@0 112 }
michael@0 113
michael@0 114 bool
michael@0 115 SpeechSynthesis::Speaking() const
michael@0 116 {
michael@0 117 if (mSpeechQueue.IsEmpty()) {
michael@0 118 return false;
michael@0 119 }
michael@0 120
michael@0 121 return mSpeechQueue.ElementAt(0)->GetState() == SpeechSynthesisUtterance::STATE_SPEAKING;
michael@0 122 }
michael@0 123
michael@0 124 bool
michael@0 125 SpeechSynthesis::Paused() const
michael@0 126 {
michael@0 127 if (mSpeechQueue.IsEmpty()) {
michael@0 128 return false;
michael@0 129 }
michael@0 130
michael@0 131 return mSpeechQueue.ElementAt(0)->IsPaused();
michael@0 132 }
michael@0 133
michael@0 134 void
michael@0 135 SpeechSynthesis::Speak(SpeechSynthesisUtterance& aUtterance)
michael@0 136 {
michael@0 137 if (aUtterance.mState != SpeechSynthesisUtterance::STATE_NONE) {
michael@0 138 // XXX: Should probably raise an error
michael@0 139 return;
michael@0 140 }
michael@0 141
michael@0 142 mSpeechQueue.AppendElement(&aUtterance);
michael@0 143 aUtterance.mState = SpeechSynthesisUtterance::STATE_PENDING;
michael@0 144
michael@0 145 if (mSpeechQueue.Length() == 1) {
michael@0 146 AdvanceQueue();
michael@0 147 }
michael@0 148 }
michael@0 149
michael@0 150 void
michael@0 151 SpeechSynthesis::AdvanceQueue()
michael@0 152 {
michael@0 153 LOG(PR_LOG_DEBUG,
michael@0 154 ("SpeechSynthesis::AdvanceQueue length=%d", mSpeechQueue.Length()));
michael@0 155
michael@0 156 if (mSpeechQueue.IsEmpty()) {
michael@0 157 return;
michael@0 158 }
michael@0 159
michael@0 160 nsRefPtr<SpeechSynthesisUtterance> utterance = mSpeechQueue.ElementAt(0);
michael@0 161
michael@0 162 nsAutoString docLang;
michael@0 163 nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(mParent);
michael@0 164 nsIDocument* doc = win->GetExtantDoc();
michael@0 165
michael@0 166 if (doc) {
michael@0 167 Element* elm = doc->GetHtmlElement();
michael@0 168
michael@0 169 if (elm) {
michael@0 170 elm->GetLang(docLang);
michael@0 171 }
michael@0 172 }
michael@0 173
michael@0 174 mCurrentTask =
michael@0 175 nsSynthVoiceRegistry::GetInstance()->SpeakUtterance(*utterance, docLang);
michael@0 176
michael@0 177 if (mCurrentTask) {
michael@0 178 mCurrentTask->SetSpeechSynthesis(this);
michael@0 179 }
michael@0 180
michael@0 181 return;
michael@0 182 }
michael@0 183
michael@0 184 void
michael@0 185 SpeechSynthesis::Cancel()
michael@0 186 {
michael@0 187 mSpeechQueue.Clear();
michael@0 188
michael@0 189 if (mCurrentTask) {
michael@0 190 mCurrentTask->Cancel();
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 void
michael@0 195 SpeechSynthesis::Pause()
michael@0 196 {
michael@0 197 if (mCurrentTask) {
michael@0 198 mCurrentTask->Pause();
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 void
michael@0 203 SpeechSynthesis::Resume()
michael@0 204 {
michael@0 205 if (mCurrentTask) {
michael@0 206 mCurrentTask->Resume();
michael@0 207 }
michael@0 208 }
michael@0 209
michael@0 210 void
michael@0 211 SpeechSynthesis::OnEnd(const nsSpeechTask* aTask)
michael@0 212 {
michael@0 213 MOZ_ASSERT(mCurrentTask == aTask);
michael@0 214
michael@0 215 if (!mSpeechQueue.IsEmpty()) {
michael@0 216 mSpeechQueue.RemoveElementAt(0);
michael@0 217 }
michael@0 218
michael@0 219 mCurrentTask = nullptr;
michael@0 220 AdvanceQueue();
michael@0 221 }
michael@0 222
michael@0 223 void
michael@0 224 SpeechSynthesis::GetVoices(nsTArray< nsRefPtr<SpeechSynthesisVoice> >& aResult)
michael@0 225 {
michael@0 226 aResult.Clear();
michael@0 227 uint32_t voiceCount = 0;
michael@0 228
michael@0 229 nsresult rv = nsSynthVoiceRegistry::GetInstance()->GetVoiceCount(&voiceCount);
michael@0 230 NS_ENSURE_SUCCESS_VOID(rv);
michael@0 231
michael@0 232 for (uint32_t i = 0; i < voiceCount; i++) {
michael@0 233 nsAutoString uri;
michael@0 234 rv = nsSynthVoiceRegistry::GetInstance()->GetVoice(i, uri);
michael@0 235
michael@0 236 if (NS_FAILED(rv)) {
michael@0 237 NS_WARNING("Failed to retrieve voice from registry");
michael@0 238 continue;
michael@0 239 }
michael@0 240
michael@0 241 SpeechSynthesisVoice* voice = mVoiceCache.GetWeak(uri);
michael@0 242
michael@0 243 if (!voice) {
michael@0 244 voice = new SpeechSynthesisVoice(this, uri);
michael@0 245 }
michael@0 246
michael@0 247 aResult.AppendElement(voice);
michael@0 248 }
michael@0 249
michael@0 250 mVoiceCache.Clear();
michael@0 251
michael@0 252 for (uint32_t i = 0; i < aResult.Length(); i++) {
michael@0 253 SpeechSynthesisVoice* voice = aResult[i];
michael@0 254 mVoiceCache.Put(voice->mUri, voice);
michael@0 255 }
michael@0 256 }
michael@0 257
michael@0 258 } // namespace dom
michael@0 259 } // namespace mozilla

mercurial