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
|
// Copyright 2024 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/ax_main_node_annotator_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/screen_ai/screen_ai_service_router.h"
#include "chrome/browser/screen_ai/screen_ai_service_router_factory.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/scoped_accessibility_mode.h"
#include "ui/accessibility/platform/ax_platform.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/accessibility/view_accessibility.h"
namespace {
// Invoke screen reader alert to notify the user of the state.
void AnnounceToScreenReader(const int message_id) {
const Browser* browser = BrowserList::GetInstance()->GetLastActive();
if (!browser) {
VLOG(2) << "Browser is not ready to announce";
return;
}
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
if (!browser_view) {
VLOG(2) << "Browser is not ready to announce";
return;
}
browser_view->GetViewAccessibility().AnnounceText(
l10n_util::GetStringUTF16(message_id));
}
} // namespace
namespace screen_ai {
AXMainNodeAnnotatorController::AXMainNodeAnnotatorController(Profile* profile)
: profile_(profile) {
// Initialize an observer for changes of AX Main Node Annotation pref.
DCHECK(profile_);
VLOG(2) << "Init AXMainNodeAnnotatorController";
pref_change_registrar_.Init(profile_->GetPrefs());
pref_change_registrar_.Add(
prefs::kAccessibilityMainNodeAnnotationsEnabled,
base::BindRepeating(
&AXMainNodeAnnotatorController::OnAXMainNodeAnnotationsEnabledChanged,
weak_ptr_factory_.GetWeakPtr()));
// Register for changes to screenreader/spoken feedback.
ax_mode_observation_.Observe(&ui::AXPlatform::GetInstance());
activated_ = ui::AXPlatform::GetInstance().IsScreenReaderActive();
OnActivationChanged();
}
AXMainNodeAnnotatorController::~AXMainNodeAnnotatorController() = default;
bool AXMainNodeAnnotatorController::IsEnabled() const {
return scoped_accessibility_mode_ != nullptr;
}
void AXMainNodeAnnotatorController::OnAXMainNodeAnnotationsEnabledChanged() {
const auto& pref_value = profile_->GetPrefs()->GetValue(
prefs::kAccessibilityMainNodeAnnotationsEnabled);
VLOG(2) << "AXMainNode annotations enabled changed: " << pref_value.GetBool();
OnActivationChanged();
}
void AXMainNodeAnnotatorController::OnActivationChanged() {
const bool is_activated =
activated_ && profile_->GetPrefs()->GetBoolean(
prefs::kAccessibilityMainNodeAnnotationsEnabled);
if (is_activated == IsEnabled()) {
return; // No change in activation.
}
if (is_activated) {
if (!service_ready_) {
// Avoid repeated requests.
if (waiting_for_service_initialization_) {
return;
}
waiting_for_service_initialization_ = true;
if (ScreenAIInstallState::GetInstance()->get_state() !=
ScreenAIInstallState::State::kDownloaded &&
!component_ready_observer_.IsObserving()) {
// Start observing ScreenAIInstallState to report it to user.
component_ready_observer_.Observe(ScreenAIInstallState::GetInstance());
}
screen_ai::ScreenAIServiceRouterFactory::GetForBrowserContext(profile_)
->GetServiceStateAsync(
ScreenAIServiceRouter::Service::kMainContentExtraction,
base::BindOnce(
&AXMainNodeAnnotatorController::
MainNodeExtractionServiceInitializationCallback,
weak_ptr_factory_.GetWeakPtr()));
return;
}
// This will send the `kAnnotateMainNode` flag to all WebContents.
scoped_accessibility_mode_ =
content::BrowserAccessibilityState::GetInstance()
->CreateScopedModeForBrowserContext(profile_,
ui::AXMode::kAnnotateMainNode);
} else {
scoped_accessibility_mode_.reset();
}
}
void AXMainNodeAnnotatorController::
MainNodeExtractionServiceInitializationCallback(bool successful) {
waiting_for_service_initialization_ = false;
service_ready_ = successful;
if (successful) {
OnActivationChanged();
} else {
// Call `StateChanged` to announce the state to user.
StateChanged(ScreenAIInstallState::State::kDownloadFailed);
}
// No more need for observing Screen AI state changes.
component_ready_observer_.Reset();
}
void AXMainNodeAnnotatorController::StateChanged(
ScreenAIInstallState::State state) {
switch (state) {
case ScreenAIInstallState::State::kNotDownloaded:
break;
case ScreenAIInstallState::State::kDownloading:
AnnounceToScreenReader(IDS_SETTINGS_MAIN_NODE_ANNOTATIONS_DOWNLOADING);
break;
case ScreenAIInstallState::State::kDownloadFailed:
AnnounceToScreenReader(IDS_SETTINGS_MAIN_NODE_ANNOTATIONS_DOWNLOAD_ERROR);
// Update the main node annotations pref to be false to toggle off the
// button.
profile_->GetPrefs()->SetBoolean(
prefs::kAccessibilityMainNodeAnnotationsEnabled, false);
break;
case ScreenAIInstallState::State::kDownloaded:
AnnounceToScreenReader(
IDS_SETTINGS_MAIN_NODE_ANNOTATIONS_DOWNLOAD_COMPLETE);
break;
}
}
void AXMainNodeAnnotatorController::Activate() {
activated_ = true;
OnActivationChanged();
}
void AXMainNodeAnnotatorController::OnAssistiveTechChanged(
ui::AssistiveTech assistive_tech) {
activated_ = ui::IsScreenReader(assistive_tech);
OnActivationChanged();
}
} // namespace screen_ai
|