File: speech_recognition_client_browser_interface.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 5,352 bytes parent folder | download | duplicates (3)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/speech/speech_recognition_client_browser_interface.h"

#include <memory>

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/notimplemented.h"
#include "chrome/browser/profiles/profile.h"
#include "components/live_caption/caption_util.h"
#include "components/live_caption/pref_names.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/soda/constants.h"
#include "components/soda/soda_installer.h"
#include "media/base/media_switches.h"

class PrefChangeRegistrar;

namespace speech {

SpeechRecognitionClientBrowserInterface::
    SpeechRecognitionClientBrowserInterface(content::BrowserContext* context) {
  Profile* profile = Profile::FromBrowserContext(context);
  profile_prefs_ = profile->GetPrefs();

  pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
  pref_change_registrar_->Init(profile_prefs_);

  // Unretained is safe because |this| owns the pref_change_registrar_.
  pref_change_registrar_->Add(
      prefs::kLiveCaptionEnabled,
      base::BindRepeating(&SpeechRecognitionClientBrowserInterface::
                              OnLiveCaptionAvailabilityChanged,
                          base::Unretained(this)));
  // Reuse the same callback, since it does the same thing regardless of which
  // pref changed.  Ignore the pref if the feature is off, though.
  if (captions::IsHeadlessCaptionFeatureSupported()) {
    pref_change_registrar_->Add(
        prefs::kHeadlessCaptionEnabled,
        base::BindRepeating(&SpeechRecognitionClientBrowserInterface::
                                OnLiveCaptionAvailabilityChanged,
                            base::Unretained(this)));
  }
  pref_change_registrar_->Add(
      prefs::kLiveCaptionLanguageCode,
      base::BindRepeating(&SpeechRecognitionClientBrowserInterface::
                              OnLiveCaptionLanguageChanged,
                          base::Unretained(this)));
  pref_change_registrar_->Add(
      prefs::kLiveCaptionMaskOffensiveWords,
      base::BindRepeating(&SpeechRecognitionClientBrowserInterface::
                              OnSpeechRecognitionMaskOffensiveWordsChanged,
                          base::Unretained(this)));
  speech::SodaInstaller::GetInstance()->AddObserver(this);
}

SpeechRecognitionClientBrowserInterface::
    ~SpeechRecognitionClientBrowserInterface() {
  speech::SodaInstaller::GetInstance()->RemoveObserver(this);
}

void SpeechRecognitionClientBrowserInterface::BindReceiver(
    mojo::PendingReceiver<media::mojom::SpeechRecognitionClientBrowserInterface>
        receiver) {
  speech_recognition_client_browser_interface_.Add(this, std::move(receiver));
}

void SpeechRecognitionClientBrowserInterface::
    BindSpeechRecognitionBrowserObserver(
        mojo::PendingRemote<media::mojom::SpeechRecognitionBrowserObserver>
            pending_remote) {
  live_caption_availibility_observers_.Add(std::move(pending_remote));
  OnLiveCaptionAvailabilityChanged();
}

void SpeechRecognitionClientBrowserInterface::REMOVED_1() {
  NOTIMPLEMENTED();
}

#if BUILDFLAG(IS_CHROMEOS)
void SpeechRecognitionClientBrowserInterface::REMOVED_2(
    mojo::PendingRemote<media::mojom::SpeechRecognitionBrowserObserver>
        pending_remote) {
  NOTIMPLEMENTED();
}
#endif

void SpeechRecognitionClientBrowserInterface::OnSodaInstalled(
    speech::LanguageCode language_code) {
  NotifyLiveCaptionObserversIfNeeded();
}

void SpeechRecognitionClientBrowserInterface::
    OnLiveCaptionAvailabilityChanged() {
  NotifyLiveCaptionObserversIfNeeded();
}

void SpeechRecognitionClientBrowserInterface::OnLiveCaptionLanguageChanged() {
  const std::string language =
      prefs::GetLiveCaptionLanguageCode(profile_prefs_);
  for (auto& observer : live_caption_availibility_observers_) {
    observer->SpeechRecognitionLanguageChanged(language);
  }
}

void SpeechRecognitionClientBrowserInterface::
    OnSpeechRecognitionMaskOffensiveWordsChanged() {
  bool mask_offensive_words =
      profile_prefs_->GetBoolean(prefs::kLiveCaptionMaskOffensiveWords);
  for (auto& observer : live_caption_availibility_observers_) {
    observer->SpeechRecognitionMaskOffensiveWordsChanged(mask_offensive_words);
  }
}

void SpeechRecognitionClientBrowserInterface::
    NotifyLiveCaptionObserversIfNeeded() {
  if (live_caption_availibility_observers_.empty()) {
    return;
  }

  bool is_language_installed =
      speech::SodaInstaller::GetInstance()->IsSodaInstalled(
          speech::GetLanguageCode(
              prefs::GetLiveCaptionLanguageCode(profile_prefs_)));
  if (!is_language_installed) {
    // Don't notify until the language pack is installed, regardless of whether
    // recognition is enabled or not.
    return;
  }

  // Captioning is enabled if either Live Caption or Headless Caption.
  bool enabled = profile_prefs_->GetBoolean(prefs::kLiveCaptionEnabled);
  if (captions::IsHeadlessCaptionFeatureSupported()) {
    enabled |= profile_prefs_->GetBoolean(prefs::kHeadlessCaptionEnabled);
  }

  for (auto& observer : live_caption_availibility_observers_) {
    observer->SpeechRecognitionAvailabilityChanged(enabled);
  }
}

}  // namespace speech