summaryrefslogtreecommitdiffstats
path: root/dom/media/webspeech/synth/SpeechSynthesis.cpp
blob: f26e36a3f4a770118052468c5b70a21d358eeaf4 (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsISupportsPrimitives.h"
#include "nsSpeechTask.h"
#include "mozilla/Logging.h"

#include "mozilla/dom/Element.h"

#include "mozilla/dom/SpeechSynthesisBinding.h"
#include "mozilla/dom/WindowGlobalChild.h"
#include "SpeechSynthesis.h"
#include "nsContentUtils.h"
#include "nsSynthVoiceRegistry.h"
#include "mozilla/dom/Document.h"
#include "nsIDocShell.h"

#undef LOG
mozilla::LogModule* GetSpeechSynthLog() {
  static mozilla::LazyLogModule sLog("SpeechSynthesis");

  return sLog;
}
#define LOG(type, msg) MOZ_LOG(GetSpeechSynthLog(), type, msg)

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_CLASS(SpeechSynthesis)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SpeechSynthesis,
                                                DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mCurrentTask)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mSpeechQueue)
  tmp->mVoiceCache.Clear();
  NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SpeechSynthesis,
                                                  DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCurrentTask)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSpeechQueue)
  for (SpeechSynthesisVoice* voice : tmp->mVoiceCache.Values()) {
    cb.NoteXPCOMChild(voice);
  }
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechSynthesis)
  NS_INTERFACE_MAP_ENTRY(nsIObserver)
  NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

NS_IMPL_ADDREF_INHERITED(SpeechSynthesis, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(SpeechSynthesis, DOMEventTargetHelper)

SpeechSynthesis::SpeechSynthesis(nsPIDOMWindowInner* aParent)
    : DOMEventTargetHelper(aParent),
      mHoldQueue(false),
      mInnerID(aParent->WindowID()) {
  MOZ_ASSERT(NS_IsMainThread());

  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
  if (obs) {
    obs->AddObserver(this, "inner-window-destroyed", true);
    obs->AddObserver(this, "synth-voices-changed", true);
    obs->AddObserver(this, "synth-voices-error", true);
  }
}

SpeechSynthesis::~SpeechSynthesis() = default;

JSObject* SpeechSynthesis::WrapObject(JSContext* aCx,
                                      JS::Handle<JSObject*> aGivenProto) {
  return SpeechSynthesis_Binding::Wrap(aCx, this, aGivenProto);
}

bool SpeechSynthesis::Pending() const {
  // If we don't have any task, nothing is pending. If we have only one task,
  // check if that task is currently pending. If we have more than one task,
  // then the tasks after the first one are definitely pending.
  return mSpeechQueue.Length() > 1 ||
         (mSpeechQueue.Length() == 1 &&
          (!mCurrentTask || mCurrentTask->IsPending()));
}

bool SpeechSynthesis::Speaking() const {
  // Check global speaking state if there is no active speaking task.
  return (!mSpeechQueue.IsEmpty() && HasSpeakingTask()) ||
         nsSynthVoiceRegistry::GetInstance()->IsSpeaking();
}

bool SpeechSynthesis::Paused() const {
  return mHoldQueue || (mCurrentTask && mCurrentTask->IsPrePaused()) ||
         (!mSpeechQueue.IsEmpty() && mSpeechQueue.ElementAt(0)->IsPaused());
}

bool SpeechSynthesis::HasEmptyQueue() const {
  return mSpeechQueue.Length() == 0;
}

bool SpeechSynthesis::HasVoices() const {
  uint32_t voiceCount = mVoiceCache.Count();
  if (voiceCount == 0) {
    nsresult rv =
        nsSynthVoiceRegistry::GetInstance()->GetVoiceCount(&voiceCount);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return false;
    }
  }

  return voiceCount != 0;
}

void SpeechSynthesis::Speak(SpeechSynthesisUtterance& aUtterance) {
  if (!mInnerID) {
    return;
  }

  mSpeechQueue.AppendElement(&aUtterance);

  if (mSpeechQueue.Length() == 1) {
    RefPtr<WindowGlobalChild> wgc =
        WindowGlobalChild::GetByInnerWindowId(mInnerID);
    if (wgc) {
      wgc->BlockBFCacheFor(BFCacheStatus::HAS_ACTIVE_SPEECH_SYNTHESIS);
    }

    // If we only have one item in the queue, we aren't pre-paused, and
    // we have voices available, speak it.
    if (!mCurrentTask && !mHoldQueue && HasVoices()) {
      AdvanceQueue();
    }
  }
}

void SpeechSynthesis::AdvanceQueue() {
  LOG(LogLevel::Debug,
      ("SpeechSynthesis::AdvanceQueue length=%zu", mSpeechQueue.Length()));

  if (mSpeechQueue.IsEmpty()) {
    return;
  }

  RefPtr<SpeechSynthesisUtterance> utterance = mSpeechQueue.ElementAt(0);

  nsAutoString docLang;
  nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
  if (Document* doc = window ? window->GetExtantDoc() : nullptr) {
    if (Element* elm = doc->GetHtmlElement()) {
      elm->GetLang(docLang);
    }
  }

  mCurrentTask =
      nsSynthVoiceRegistry::GetInstance()->SpeakUtterance(*utterance, docLang);

  if (mCurrentTask) {
    mCurrentTask->SetSpeechSynthesis(this);
  }
}

void SpeechSynthesis::Cancel() {
  if (!mSpeechQueue.IsEmpty() && HasSpeakingTask()) {
    // Remove all queued utterances except for current one, we will remove it
    // in OnEnd
    mSpeechQueue.RemoveLastElements(mSpeechQueue.Length() - 1);
  } else {
    mSpeechQueue.Clear();
  }

  if (mCurrentTask) {
    mCurrentTask->Cancel();
  }
}

void SpeechSynthesis::Pause() {
  if (Paused()) {
    return;
  }

  if (!mSpeechQueue.IsEmpty() && HasSpeakingTask()) {
    mCurrentTask->Pause();
  } else {
    mHoldQueue = true;
  }
}

void SpeechSynthesis::Resume() {
  if (!Paused()) {
    return;
  }

  mHoldQueue = false;

  if (mCurrentTask) {
    mCurrentTask->Resume();
  } else {
    AdvanceQueue();
  }
}

void SpeechSynthesis::OnEnd(const nsSpeechTask* aTask) {
  MOZ_ASSERT(mCurrentTask == aTask);

  if (!mSpeechQueue.IsEmpty()) {
    mSpeechQueue.RemoveElementAt(0);
    if (mSpeechQueue.IsEmpty()) {
      RefPtr<WindowGlobalChild> wgc =
          WindowGlobalChild::GetByInnerWindowId(mInnerID);
      if (wgc) {
        wgc->UnblockBFCacheFor(BFCacheStatus::HAS_ACTIVE_SPEECH_SYNTHESIS);
      }
    }
  }

  mCurrentTask = nullptr;
  AdvanceQueue();
}

void SpeechSynthesis::GetVoices(
    nsTArray<RefPtr<SpeechSynthesisVoice> >& aResult) {
  aResult.Clear();
  uint32_t voiceCount = 0;
  nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
  nsCOMPtr<nsIDocShell> docShell = window ? window->GetDocShell() : nullptr;

  if (nsContentUtils::ShouldResistFingerprinting(docShell,
                                                 RFPTarget::SpeechSynthesis)) {
    return;
  }

  nsresult rv = nsSynthVoiceRegistry::GetInstance()->GetVoiceCount(&voiceCount);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return;
  }

  nsISupports* voiceParent = NS_ISUPPORTS_CAST(nsIObserver*, this);

  for (uint32_t i = 0; i < voiceCount; i++) {
    nsAutoString uri;
    rv = nsSynthVoiceRegistry::GetInstance()->GetVoice(i, uri);

    if (NS_FAILED(rv)) {
      NS_WARNING("Failed to retrieve voice from registry");
      continue;
    }

    SpeechSynthesisVoice* voice = mVoiceCache.GetWeak(uri);

    if (!voice) {
      voice = new SpeechSynthesisVoice(voiceParent, uri);
    }

    aResult.AppendElement(voice);
  }

  mVoiceCache.Clear();

  for (uint32_t i = 0; i < aResult.Length(); i++) {
    SpeechSynthesisVoice* voice = aResult[i];
    mVoiceCache.InsertOrUpdate(voice->mUri, RefPtr{voice});
  }
}

// For testing purposes, allows us to cancel the current task that is
// misbehaving, and flush the queue.
void SpeechSynthesis::ForceEnd() {
  if (mCurrentTask) {
    mCurrentTask->ForceEnd();
  }
}

NS_IMETHODIMP
SpeechSynthesis::Observe(nsISupports* aSubject, const char* aTopic,
                         const char16_t* aData) {
  MOZ_ASSERT(NS_IsMainThread());

  if (strcmp(aTopic, "inner-window-destroyed") == 0) {
    nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
    NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);

    uint64_t innerID;
    nsresult rv = wrapper->GetData(&innerID);
    NS_ENSURE_SUCCESS(rv, rv);

    if (innerID == mInnerID) {
      mInnerID = 0;
      Cancel();

      nsCOMPtr<nsIObserverService> obs =
          mozilla::services::GetObserverService();
      if (obs) {
        obs->RemoveObserver(this, "inner-window-destroyed");
      }
    }
  } else if (strcmp(aTopic, "synth-voices-changed") == 0) {
    LOG(LogLevel::Debug, ("SpeechSynthesis::onvoiceschanged"));
    nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
    nsCOMPtr<nsIDocShell> docShell = window ? window->GetDocShell() : nullptr;

    if (!nsContentUtils::ShouldResistFingerprinting(
            docShell, RFPTarget::SpeechSynthesis)) {
      DispatchTrustedEvent(u"voiceschanged"_ns);
      // If we have a pending item, and voices become available, speak it.
      if (!mCurrentTask && !mHoldQueue && HasVoices()) {
        AdvanceQueue();
      }
    }
  } else if (strcmp(aTopic, "synth-voices-error") == 0) {
    NS_WARNING("SpeechSynthesis::Observe: synth-voices-error");
    LOG(LogLevel::Debug, ("SpeechSynthesis::onvoiceserror"));
    nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();

    nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
    if (obs) {
      obs->NotifyObservers(window, "chrome-synth-voices-error", aData);
    }

    if (!mSpeechQueue.IsEmpty()) {
      for (RefPtr<SpeechSynthesisUtterance>& utterance : mSpeechQueue) {
        utterance->DispatchSpeechSynthesisEvent(u"error"_ns, 0, nullptr, 0,
                                                u""_ns);
      }
      mSpeechQueue.Clear();
    }
  }

  return NS_OK;
}

}  // namespace mozilla::dom