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
|
// Copyright 2022 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/accessibility/live_caption/live_caption_unavailability_notifier.h"
#include <memory>
#include <utility>
#include "base/containers/contains.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/accessibility/caption_bubble_context_browser.h"
#include "chrome/browser/accessibility/live_caption/live_caption_controller_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "components/live_caption/live_caption_controller.h"
#include "components/live_caption/pref_names.h"
#include "components/live_caption/views/caption_bubble.h"
#include "components/live_caption/views/caption_bubble_model.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "media/mojo/mojom/renderer_extensions.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "url/origin.h"
namespace captions {
// static
void LiveCaptionUnavailabilityNotifier::Create(
content::RenderFrameHost* frame_host,
mojo::PendingReceiver<media::mojom::MediaFoundationRendererNotifier>
receiver) {
CHECK(frame_host);
// The object is bound to the lifetime of |frame_host| and the mojo
// connection. See DocumentService for details.
new LiveCaptionUnavailabilityNotifier(*frame_host, std::move(receiver));
}
LiveCaptionUnavailabilityNotifier::LiveCaptionUnavailabilityNotifier(
content::RenderFrameHost& frame_host,
mojo::PendingReceiver<media::mojom::MediaFoundationRendererNotifier>
receiver)
: DocumentService<media::mojom::MediaFoundationRendererNotifier>(
frame_host,
std::move(receiver)) {
content::WebContents* web_contents = GetWebContents();
if (!web_contents)
return;
context_ = CaptionBubbleContextBrowser::Create(web_contents);
Profile* profile =
Profile::FromBrowserContext(render_frame_host().GetBrowserContext());
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(&LiveCaptionUnavailabilityNotifier::
OnSpeechRecognitionAvailabilityChanged,
base::Unretained(this)));
}
LiveCaptionUnavailabilityNotifier::~LiveCaptionUnavailabilityNotifier() {
LiveCaptionController* live_caption_controller = GetLiveCaptionController();
if (live_caption_controller)
live_caption_controller->OnAudioStreamEnd(GetWebContents(), context_.get());
}
void LiveCaptionUnavailabilityNotifier::MediaFoundationRendererCreated(
mojo::PendingReceiver<media::mojom::MediaFoundationRendererObserver>
observer) {
observers_.Add(this, std::move(observer));
if (ShouldDisplayMediaFoundationRendererError()) {
DisplayMediaFoundationRendererError();
}
}
content::WebContents* LiveCaptionUnavailabilityNotifier::GetWebContents() {
return content::WebContents::FromRenderFrameHost(&render_frame_host());
}
LiveCaptionController*
LiveCaptionUnavailabilityNotifier::GetLiveCaptionController() {
Profile* profile =
Profile::FromBrowserContext(render_frame_host().GetBrowserContext());
if (!profile)
return nullptr;
return LiveCaptionControllerFactory::GetForProfile(profile);
}
bool LiveCaptionUnavailabilityNotifier::
ShouldDisplayMediaFoundationRendererError() {
if (!profile_prefs_->GetBoolean(prefs::kLiveCaptionEnabled))
return false;
if (observers_.empty())
return false;
return !ErrorSilencedForOrigin();
}
bool LiveCaptionUnavailabilityNotifier::ErrorSilencedForOrigin() {
return base::Contains(
profile_prefs_->GetList(
prefs::kLiveCaptionMediaFoundationRendererErrorSilenced),
render_frame_host().GetLastCommittedOrigin().Serialize());
}
void LiveCaptionUnavailabilityNotifier::DisplayMediaFoundationRendererError() {
LiveCaptionController* live_caption_controller = GetLiveCaptionController();
if (!live_caption_controller)
return;
// This will trigger the caption bubble to display a message informing the
// user that Live Caption is unavailable and link them to the settings page
// where they can disable the media foundation renderer to enable Live
// Caption. The error message may be overwritten if recognition events are
// received from another audio stream.
live_caption_controller->OnError(
context_.get(),
CaptionBubbleErrorType::kMediaFoundationRendererUnsupported,
base::BindRepeating(&LiveCaptionUnavailabilityNotifier::
OnMediaFoundationRendererErrorClicked,
weak_factory_.GetWeakPtr()),
base::BindRepeating(
&LiveCaptionUnavailabilityNotifier::
OnMediaFoundationRendererErrorDoNotShowAgainCheckboxClicked,
weak_factory_.GetWeakPtr()));
}
void LiveCaptionUnavailabilityNotifier::
OnMediaFoundationRendererErrorDoNotShowAgainCheckboxClicked(
CaptionBubbleErrorType error_type,
bool checked) {
PrefService* prefs =
Profile::FromBrowserContext(render_frame_host().GetBrowserContext())
->GetPrefs();
ScopedListPrefUpdate update(
prefs, prefs::kLiveCaptionMediaFoundationRendererErrorSilenced);
if (checked) {
update->Append(render_frame_host().GetLastCommittedOrigin().Serialize());
} else {
update->EraseIf([&](const base::Value& value) {
return value.GetString() ==
render_frame_host().GetLastCommittedOrigin().Serialize();
});
}
}
void LiveCaptionUnavailabilityNotifier::
OnMediaFoundationRendererErrorClicked() {
Profile* profile =
Profile::FromBrowserContext(render_frame_host().GetBrowserContext());
chrome::ShowSiteSettings(
profile, render_frame_host().GetLastCommittedOrigin().GetURL());
}
void LiveCaptionUnavailabilityNotifier::
OnSpeechRecognitionAvailabilityChanged() {
if (ShouldDisplayMediaFoundationRendererError()) {
DisplayMediaFoundationRendererError();
}
}
} // namespace captions
|