tor-browser

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

FakeSpeechRecognitionService.cpp (3954B)


      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 "FakeSpeechRecognitionService.h"
      8 
      9 #include "SpeechRecognition.h"
     10 #include "SpeechRecognitionAlternative.h"
     11 #include "SpeechRecognitionResult.h"
     12 #include "SpeechRecognitionResultList.h"
     13 #include "mozilla/Services.h"
     14 #include "mozilla/StaticPrefs_media.h"
     15 #include "nsIObserverService.h"
     16 #include "nsThreadUtils.h"
     17 
     18 namespace mozilla {
     19 
     20 using namespace dom;
     21 
     22 NS_IMPL_ISUPPORTS(FakeSpeechRecognitionService, nsISpeechRecognitionService,
     23                  nsIObserver)
     24 
     25 FakeSpeechRecognitionService::FakeSpeechRecognitionService() = default;
     26 
     27 FakeSpeechRecognitionService::~FakeSpeechRecognitionService() = default;
     28 
     29 NS_IMETHODIMP
     30 FakeSpeechRecognitionService::Initialize(
     31    WeakPtr<SpeechRecognition> aSpeechRecognition) {
     32  MOZ_ASSERT(NS_IsMainThread());
     33  mRecognition = aSpeechRecognition;
     34  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     35  obs->AddObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC, false);
     36  obs->AddObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC, false);
     37  return NS_OK;
     38 }
     39 
     40 NS_IMETHODIMP
     41 FakeSpeechRecognitionService::ProcessAudioSegment(AudioSegment* aAudioSegment,
     42                                                  int32_t aSampleRate) {
     43  MOZ_ASSERT(!NS_IsMainThread());
     44  return NS_OK;
     45 }
     46 
     47 NS_IMETHODIMP
     48 FakeSpeechRecognitionService::SoundEnd() {
     49  MOZ_ASSERT(NS_IsMainThread());
     50  return NS_OK;
     51 }
     52 
     53 NS_IMETHODIMP
     54 FakeSpeechRecognitionService::ValidateAndSetGrammarList(
     55    mozilla::dom::SpeechGrammar*, nsISpeechGrammarCompilationCallback*) {
     56  return NS_OK;
     57 }
     58 
     59 NS_IMETHODIMP
     60 FakeSpeechRecognitionService::Abort() {
     61  MOZ_ASSERT(NS_IsMainThread());
     62  return NS_OK;
     63 }
     64 
     65 NS_IMETHODIMP
     66 FakeSpeechRecognitionService::Observe(nsISupports* aSubject, const char* aTopic,
     67                                      const char16_t* aData) {
     68  MOZ_ASSERT(StaticPrefs::media_webspeech_test_fake_recognition_service(),
     69             "Got request to fake recognition service event, but "
     70             "media.webspeech.test.fake_recognition_service is not set");
     71 
     72  if (!strcmp(aTopic, SPEECH_RECOGNITION_TEST_END_TOPIC)) {
     73    nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     74    obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_EVENT_REQUEST_TOPIC);
     75    obs->RemoveObserver(this, SPEECH_RECOGNITION_TEST_END_TOPIC);
     76 
     77    return NS_OK;
     78  }
     79 
     80  const nsDependentString eventName = nsDependentString(aData);
     81 
     82  if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_ERROR")) {
     83    mRecognition->DispatchError(
     84        SpeechRecognition::EVENT_RECOGNITIONSERVICE_ERROR,
     85        SpeechRecognitionErrorCode::Network,  // TODO different codes?
     86        "RECOGNITIONSERVICE_ERROR test event");
     87 
     88  } else if (eventName.EqualsLiteral("EVENT_RECOGNITIONSERVICE_FINAL_RESULT")) {
     89    RefPtr<SpeechEvent> event = new SpeechEvent(
     90        mRecognition, SpeechRecognition::EVENT_RECOGNITIONSERVICE_FINAL_RESULT);
     91 
     92    event->mRecognitionResultList = BuildMockResultList();
     93    NS_DispatchToMainThread(event);
     94  }
     95  return NS_OK;
     96 }
     97 
     98 SpeechRecognitionResultList*
     99 FakeSpeechRecognitionService::BuildMockResultList() {
    100  SpeechRecognitionResultList* resultList =
    101      new SpeechRecognitionResultList(mRecognition);
    102  SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition);
    103  if (0 < mRecognition->MaxAlternatives()) {
    104    SpeechRecognitionAlternative* alternative =
    105        new SpeechRecognitionAlternative(mRecognition);
    106 
    107    alternative->mTranscript = u"Mock final result"_ns;
    108    alternative->mConfidence = 0.0f;
    109 
    110    result->mItems.AppendElement(alternative);
    111  }
    112  resultList->mItems.AppendElement(result);
    113 
    114  return resultList;
    115 }
    116 
    117 }  // namespace mozilla