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
|
// 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 "content/renderer/accessibility/annotations/ax_main_node_annotator.h"
#include <utility>
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
namespace content {
using blink::WebAXObject;
using blink::WebDocument;
namespace {
const char kHistogramsName[] =
"Accessibility.MainNodeAnnotations.AnnotationResult";
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// LINT.IfChange(MainNodeAnnotationResult)
enum class MainNodeAnnotationResult {
kSuccess = 0,
kInvalid = 1,
kDuplicate = 2,
kMaxValue = kDuplicate,
};
// LINT.ThenChange(/tools/metrics/histograms/metadata/accessibility/enums.xml:MainNodeAnnotationResult)
} // namespace
AXMainNodeAnnotator::AXMainNodeAnnotator(
RenderAccessibilityImpl* const render_accessibility)
: render_accessibility_(render_accessibility) {
CHECK(render_accessibility_);
}
AXMainNodeAnnotator::~AXMainNodeAnnotator() = default;
void AXMainNodeAnnotator::EnableAnnotations() {
annotator_enabled_ = true;
}
void AXMainNodeAnnotator::CancelAnnotations() {
if (render_accessibility_->GetAccessibilityMode().has_mode(
GetAXModeToEnableAnnotations())) {
return;
}
annotator_enabled_ = false;
annotator_remote_.reset();
}
uint32_t AXMainNodeAnnotator::GetAXModeToEnableAnnotations() {
return ui::AXMode::kAnnotateMainNode;
}
bool AXMainNodeAnnotator::HasAXActionToEnableAnnotations() {
return false;
}
ax::mojom::Action AXMainNodeAnnotator::GetAXActionToEnableAnnotations() {
NOTREACHED();
}
void AXMainNodeAnnotator::Annotate(const WebDocument& document,
ui::AXTreeUpdate* update,
bool load_complete) {
// Annotate is called every time RenderAccessibilityImpl sends a serialized
// tree, in the form of an AXTreeUpdate, to the browser. Before sending to
// the browser, we annotate the main node of the AXTreeUpdate here.
if (main_node_id_ != ui::kInvalidAXNodeID) {
// TODO: Replace with binary search as nodes should be in order by id.
for (ui::AXNodeData& node : update->nodes) {
if (node.id != main_node_id_) {
continue;
}
// TODO: Replace this with a role specifically for annotations.
node.role = ax::mojom::Role::kMain;
return;
}
// If the main node was set, even if if is not found in the tree anymore,
// do nothing.
// TODO: Consider whether we should call Screen2x again.
return;
}
// Do nothing if this is not a load complete event.
if (!load_complete) {
return;
}
// Check whether the author has already labeled a main node in this tree.
ComputeAuthorStatus(update);
if (author_status_ == AXMainNodeAnnotatorAuthorStatus::kAuthorProvidedMain) {
return;
}
CHECK_EQ(author_status_,
AXMainNodeAnnotatorAuthorStatus::kAuthorDidNotProvideMain);
// TODO(crbug.com/327248295): Promote the feature if the user has not enabled
// it and is on a page without a main node annotation.
if (!annotator_enabled_) {
return;
}
if (!annotator_remote_.is_bound() || !annotator_remote_.is_connected()) {
if (!render_accessibility_->render_frame()) {
return;
}
mojo::PendingRemote<screen_ai::mojom::Screen2xMainContentExtractor>
annotator;
render_accessibility_->render_frame()
->GetBrowserInterfaceBroker()
.GetInterface(annotator.InitWithNewPipeAndPassReceiver());
annotator_remote_.Bind(std::move(annotator));
annotator_remote_.reset_on_disconnect();
annotator_remote_->SetClientType(
screen_ai::mojom::MceClientType::kMainNode);
}
// Identify the main node using Screen2x.
annotator_remote_->ExtractMainNode(
*update, base::BindOnce(&AXMainNodeAnnotator::ProcessScreen2xResult,
weak_ptr_factory_.GetWeakPtr(), document));
}
void AXMainNodeAnnotator::ProcessScreen2xResult(const WebDocument& document,
ui::AXNodeID main_node_id) {
// If Screen2x returned an invalid main node id, do nothing.
if (main_node_id == ui::kInvalidAXNodeID) {
base::UmaHistogramEnumeration(kHistogramsName,
MainNodeAnnotationResult::kInvalid);
return;
}
// If the main node id was already set, do nothing.
if (main_node_id_ != ui::kInvalidAXNodeID) {
base::UmaHistogramEnumeration(kHistogramsName,
MainNodeAnnotationResult::kDuplicate);
return;
}
WebAXObject object = WebAXObject::FromWebDocumentByID(document, main_node_id);
// If the tree has changed, do nothing.
if (!object.IsIncludedInTree()) {
base::UmaHistogramEnumeration(kHistogramsName,
MainNodeAnnotationResult::kInvalid);
return;
}
main_node_id_ = main_node_id;
render_accessibility_->MarkWebAXObjectDirty(object);
base::UmaHistogramEnumeration(kHistogramsName,
MainNodeAnnotationResult::kSuccess);
}
void AXMainNodeAnnotator::ComputeAuthorStatus(ui::AXTreeUpdate* update) {
if (author_status_ != AXMainNodeAnnotatorAuthorStatus::kUnconfirmed) {
return;
}
for (ui::AXNodeData& node : update->nodes) {
if (node.role == ax::mojom::Role::kMain) {
author_status_ = AXMainNodeAnnotatorAuthorStatus::kAuthorProvidedMain;
return;
}
}
author_status_ = AXMainNodeAnnotatorAuthorStatus::kAuthorDidNotProvideMain;
}
void AXMainNodeAnnotator::BindAnnotatorForTesting(
mojo::PendingRemote<screen_ai::mojom::Screen2xMainContentExtractor>
annotator) {
annotator_remote_.Bind(std::move(annotator));
annotator_enabled_ = true;
}
} // namespace content
|