tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SpeechSynthesisUtterance.cpp (4223B)


      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/. */
      6 
      7 #include "SpeechSynthesisUtterance.h"
      8 
      9 #include "SpeechSynthesisVoice.h"
     10 #include "mozilla/dom/SpeechSynthesisEvent.h"
     11 #include "mozilla/dom/SpeechSynthesisUtteranceBinding.h"
     12 #include "nsCOMPtr.h"
     13 #include "nsCycleCollectionParticipant.h"
     14 #include "nsGkAtoms.h"
     15 
     16 namespace mozilla::dom {
     17 
     18 NS_IMPL_CYCLE_COLLECTION_INHERITED(SpeechSynthesisUtterance,
     19                                   DOMEventTargetHelper, mVoice);
     20 
     21 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SpeechSynthesisUtterance)
     22 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
     23 
     24 NS_IMPL_ADDREF_INHERITED(SpeechSynthesisUtterance, DOMEventTargetHelper)
     25 NS_IMPL_RELEASE_INHERITED(SpeechSynthesisUtterance, DOMEventTargetHelper)
     26 
     27 SpeechSynthesisUtterance::SpeechSynthesisUtterance(
     28    nsPIDOMWindowInner* aOwnerWindow, const nsAString& text)
     29    : DOMEventTargetHelper(aOwnerWindow),
     30      mText(text),
     31      mVolume(1),
     32      mRate(1),
     33      mPitch(1),
     34      mPaused(false),
     35      mShouldResistFingerprinting(
     36          aOwnerWindow->AsGlobal()->ShouldResistFingerprinting(
     37              RFPTarget::SpeechSynthesis)) {}
     38 
     39 SpeechSynthesisUtterance::~SpeechSynthesisUtterance() = default;
     40 
     41 JSObject* SpeechSynthesisUtterance::WrapObject(
     42    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
     43  return SpeechSynthesisUtterance_Binding::Wrap(aCx, this, aGivenProto);
     44 }
     45 
     46 nsISupports* SpeechSynthesisUtterance::GetParentObject() const {
     47  return GetOwnerGlobal();
     48 }
     49 
     50 already_AddRefed<SpeechSynthesisUtterance>
     51 SpeechSynthesisUtterance::Constructor(GlobalObject& aGlobal, ErrorResult& aRv) {
     52  return Constructor(aGlobal, u""_ns, aRv);
     53 }
     54 
     55 already_AddRefed<SpeechSynthesisUtterance>
     56 SpeechSynthesisUtterance::Constructor(GlobalObject& aGlobal,
     57                                      const nsAString& aText,
     58                                      ErrorResult& aRv) {
     59  nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aGlobal.GetAsSupports());
     60 
     61  if (!win) {
     62    aRv.Throw(NS_ERROR_FAILURE);
     63    return nullptr;
     64  }
     65 
     66  RefPtr<SpeechSynthesisUtterance> object =
     67      new SpeechSynthesisUtterance(win, aText);
     68  return object.forget();
     69 }
     70 
     71 void SpeechSynthesisUtterance::GetText(nsString& aResult) const {
     72  aResult = mText;
     73 }
     74 
     75 void SpeechSynthesisUtterance::SetText(const nsAString& aText) {
     76  mText = aText;
     77 }
     78 
     79 void SpeechSynthesisUtterance::GetLang(nsString& aResult) const {
     80  aResult = mLang;
     81 }
     82 
     83 void SpeechSynthesisUtterance::SetLang(const nsAString& aLang) {
     84  mLang = aLang;
     85 }
     86 
     87 SpeechSynthesisVoice* SpeechSynthesisUtterance::GetVoice() const {
     88  return mVoice;
     89 }
     90 
     91 void SpeechSynthesisUtterance::SetVoice(SpeechSynthesisVoice* aVoice) {
     92  mVoice = aVoice;
     93 }
     94 
     95 float SpeechSynthesisUtterance::Volume() const { return mVolume; }
     96 
     97 void SpeechSynthesisUtterance::SetVolume(float aVolume) {
     98  mVolume = std::max<float>(std::min<float>(aVolume, 1), 0);
     99 }
    100 
    101 float SpeechSynthesisUtterance::Rate() const { return mRate; }
    102 
    103 void SpeechSynthesisUtterance::SetRate(float aRate) {
    104  mRate = std::max<float>(std::min<float>(aRate, 10), 0.1f);
    105 }
    106 
    107 float SpeechSynthesisUtterance::Pitch() const { return mPitch; }
    108 
    109 void SpeechSynthesisUtterance::SetPitch(float aPitch) {
    110  mPitch = std::max<float>(std::min<float>(aPitch, 2), 0);
    111 }
    112 
    113 void SpeechSynthesisUtterance::GetChosenVoiceURI(nsString& aResult) const {
    114  aResult = mChosenVoiceURI;
    115 }
    116 
    117 void SpeechSynthesisUtterance::DispatchSpeechSynthesisEvent(
    118    const nsAString& aEventType, uint32_t aCharIndex,
    119    const Nullable<uint32_t>& aCharLength, float aElapsedTime,
    120    const nsAString& aName) {
    121  SpeechSynthesisEventInit init;
    122  init.mBubbles = false;
    123  init.mCancelable = false;
    124  init.mUtterance = this;
    125  init.mCharIndex = aCharIndex;
    126  init.mCharLength = aCharLength;
    127  init.mElapsedTime = aElapsedTime;
    128  init.mName = aName;
    129 
    130  RefPtr<SpeechSynthesisEvent> event =
    131      SpeechSynthesisEvent::Constructor(this, aEventType, init);
    132  DispatchTrustedEvent(event);
    133 }
    134 
    135 }  // namespace mozilla::dom