File: caption_controller_base.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (219 lines) | stat: -rw-r--r-- 7,376 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/live_caption/caption_controller_base.h"

#include <memory>
#include <string>
#include <utility>

#include "base/check.h"
#include "components/live_caption/caption_bubble_controller.h"
#include "components/live_caption/caption_util.h"
#include "components/live_caption/pref_names.h"
#include "components/live_caption/views/translation_view_wrapper.h"
#include "components/live_caption/views/translation_view_wrapper_base.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/native_theme_observer.h"

namespace captions {
namespace {

const char* const kCaptionStylePrefsToObserve[] = {
    prefs::kAccessibilityCaptionsTextSize,
    prefs::kAccessibilityCaptionsTextFont,
    prefs::kAccessibilityCaptionsTextColor,
    prefs::kAccessibilityCaptionsTextOpacity,
    prefs::kAccessibilityCaptionsBackgroundColor,
    prefs::kAccessibilityCaptionsTextShadow,
    prefs::kAccessibilityCaptionsBackgroundOpacity};

class CaptionControllerDelgateImpl : public CaptionControllerBase::Delegate {
 public:
  CaptionControllerDelgateImpl() = default;
  ~CaptionControllerDelgateImpl() override = default;

  std::unique_ptr<CaptionBubbleController> CreateCaptionBubbleController(
      CaptionBubbleSettings* caption_bubble_settings,
      const std::string& application_locale,
      std::unique_ptr<TranslationViewWrapperBase> translation_view_wrapper)
      override {
    return CaptionBubbleController::Create(caption_bubble_settings,
                                           application_locale,
                                           std::move(translation_view_wrapper));
  }

  void AddCaptionStyleObserver(ui::NativeThemeObserver* observer) override {
    ui::NativeTheme::GetInstanceForWeb()->AddObserver(observer);
  }

  void RemoveCaptionStyleObserver(ui::NativeThemeObserver* observer) override {
    ui::NativeTheme::GetInstanceForWeb()->RemoveObserver(observer);
  }
};

}  // namespace

CaptionControllerBase::~CaptionControllerBase() {
  // The caption bubble controller, if we have one, will be cleaned up as part
  // of destruction.  Don't leave the raw ptr alias to it dangling.
  caption_bubble_controller_ = nullptr;
}

CaptionControllerBase::CaptionControllerBase(
    PrefService* profile_prefs,
    const std::string& application_locale,
    std::unique_ptr<Delegate> delegate)
    : profile_prefs_(profile_prefs),
      application_locale_(application_locale),
      delegate_(delegate ? std::move(delegate)
                         : std::make_unique<CaptionControllerDelgateImpl>()),
      pref_change_registrar_(std::make_unique<PrefChangeRegistrar>()) {
  pref_change_registrar_->Init(profile_prefs_);

  // Turn off headless captioning when we first start, so that it does not get
  // stuck on.
  if (profile_prefs_->FindPreference(prefs::kHeadlessCaptionEnabled)) {
    profile_prefs_->SetBoolean(prefs::kHeadlessCaptionEnabled, false);
  }
}

void CaptionControllerBase::CreateUI() {
  if (is_ui_constructed_) {
    return;
  }

  is_ui_constructed_ = true;

  auto controller = delegate_->CreateCaptionBubbleController(
      caption_bubble_settings(), application_locale_,
      CreateTranslationViewWrapper());
  caption_bubble_controller_ = controller.get();
  AddListener(std::move(controller));

  // Observe native theme changes for caption style updates.
  delegate_->AddCaptionStyleObserver(this);

  // Observe caption style prefs.
  for (const char* const pref_name : kCaptionStylePrefsToObserve) {
    CHECK(!pref_change_registrar_->IsObserved(pref_name));
    pref_change_registrar_->Add(
        pref_name,
        base::BindRepeating(&CaptionControllerBase::OnCaptionStyleUpdated,
                            base::Unretained(this)));
  }
  OnCaptionStyleUpdated();
}

void CaptionControllerBase::DestroyUI() {
  if (!is_ui_constructed_) {
    return;
  }
  is_ui_constructed_ = false;

  RemoveListener(caption_bubble_controller_);
  CHECK(!caption_bubble_controller_);

  // Remove native theme observer.
  delegate_->RemoveCaptionStyleObserver(this);

  // Remove prefs to observe.
  for (const char* const pref_name : kCaptionStylePrefsToObserve) {
    CHECK(pref_change_registrar_->IsObserved(pref_name));
    pref_change_registrar_->Remove(pref_name);
  }
}

PrefService* CaptionControllerBase::profile_prefs() const {
  return profile_prefs_;
}

const std::string& CaptionControllerBase::application_locale() const {
  return application_locale_;
}

PrefChangeRegistrar* CaptionControllerBase::pref_change_registrar() const {
  return pref_change_registrar_.get();
}

CaptionBubbleController* CaptionControllerBase::caption_bubble_controller()
    const {
  return caption_bubble_controller_.get();
}

std::unique_ptr<TranslationViewWrapperBase>
CaptionControllerBase::CreateTranslationViewWrapper() {
  return std::make_unique<TranslationViewWrapper>(caption_bubble_settings());
}

void CaptionControllerBase::OnCaptionStyleUpdated() {
  if (!caption_bubble_controller_) {
    return;
  }
  // Metrics are recorded when passing the caption prefs to the browser, so do
  // not duplicate them here.
  std::optional<ui::CaptionStyle> caption_style =
      GetCaptionStyleFromUserSettings(profile_prefs_,
                                      /*record_metrics=*/false);
  caption_bubble_controller_->UpdateCaptionStyle(caption_style);
}

void CaptionControllerBase::AddListener(std::unique_ptr<Listener> listener) {
  listeners_.push_back(std::move(listener));
}

void CaptionControllerBase::RemoveListener(Listener* listener) {
  if (caption_bubble_controller_ == listener) {
    caption_bubble_controller_ = nullptr;
  }
  // `std::find` doesn't like comparing unique_ptrs to raw ptrs.
  for (auto iter = listeners_.begin(); iter != listeners_.end(); iter++) {
    if (iter->get() != listener) {
      continue;
    }

    listeners_.erase(iter);
    return;
  }
  NOTREACHED();
}

bool CaptionControllerBase::DispatchTranscription(
    content::WebContents* web_contents,
    CaptionBubbleContext* caption_bubble_context,
    const media::SpeechRecognitionResult& result) {
  bool success = false;

  // Consider deleting the listener if it returns false.  It's unclear if
  // `caption_bubble_controller_` would allow this, but maybe.
  for (auto& listener : listeners_) {
    success |=
        listener->OnTranscription(web_contents, caption_bubble_context, result);
  }

  return success;
}

void CaptionControllerBase::OnAudioStreamEnd(
    content::WebContents* web_contents,
    CaptionBubbleContext* caption_bubble_context) {
  for (auto& listener : listeners_) {
    listener->OnAudioStreamEnd(web_contents, caption_bubble_context);
  }
}

void CaptionControllerBase::OnLanguageIdentificationEvent(
    content::WebContents* web_contents,
    CaptionBubbleContext* caption_bubble_context,
    const media::mojom::LanguageIdentificationEventPtr& event) {
  // TODO(crbug.com/40167928): Implement the UI for language identification.
  for (auto& listener : listeners_) {
    listener->OnLanguageIdentificationEvent(web_contents,
                                            caption_bubble_context, event);
  }
}

}  // namespace captions