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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/platform/fuchsia/semantic_provider_impl.h"
#include <fidl/fuchsia.ui.gfx/cpp/fidl.h>
#include <lib/async/default.h>
#include <lib/sys/cpp/component_context.h>
#include "base/check.h"
#include "base/check_op.h"
#include "base/fuchsia/fuchsia_component_connect.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "ui/gfx/geometry/transform.h"
namespace ui {
namespace {
using fuchsia_accessibility_semantics::Node;
constexpr size_t kMaxOperationsPerBatch = 16;
SemanticTreeEventHandler::SemanticTreeEventHandler(
base::OnceCallback<void(fidl::UnbindInfo)> on_fidl_error_callback)
: on_fidl_error_callback_(std::move(on_fidl_error_callback)) {}
SemanticTreeEventHandler::~SemanticTreeEventHandler() = default;
void SemanticTreeEventHandler::on_fidl_error(fidl::UnbindInfo error) {
std::move(on_fidl_error_callback_).Run(error);
}
} // namespace
AXFuchsiaSemanticProviderImpl::Batch::Batch(Type type) : type_(type) {}
AXFuchsiaSemanticProviderImpl::Batch::Batch(Batch&& other) = default;
AXFuchsiaSemanticProviderImpl::Batch::~Batch() = default;
bool AXFuchsiaSemanticProviderImpl::Batch::IsFull() const {
return (
(type_ == Type::kUpdate && updates_.size() >= kMaxOperationsPerBatch) ||
(type_ == Type::kDelete &&
delete_node_ids_.size() >= kMaxOperationsPerBatch));
}
void AXFuchsiaSemanticProviderImpl::Batch::Append(
fuchsia_accessibility_semantics::Node node) {
DCHECK_EQ(type_, Type::kUpdate);
DCHECK(!IsFull());
updates_.push_back(std::move(node));
}
void AXFuchsiaSemanticProviderImpl::Batch::AppendDeletion(
uint32_t delete_node_id) {
DCHECK_EQ(type_, Type::kDelete);
DCHECK(!IsFull());
delete_node_ids_.push_back(delete_node_id);
}
void AXFuchsiaSemanticProviderImpl::Batch::Apply(
fidl::Client<fuchsia_accessibility_semantics::SemanticTree>*
semantic_tree) {
if (type_ == Type::kUpdate && !updates_.empty()) {
auto result = (*semantic_tree)->UpdateSemanticNodes(std::move(updates_));
LOG_IF(ERROR, result.is_error())
<< base::FidlMethodResultErrorMessage(result, "UpdateSemanticNodes");
} else if (type_ == Type::kDelete && !delete_node_ids_.empty()) {
auto result =
(*semantic_tree)->DeleteSemanticNodes(std::move(delete_node_ids_));
LOG_IF(ERROR, result.is_error())
<< base::FidlMethodResultErrorMessage(result, "DeleteSemanticNodes");
}
}
AXFuchsiaSemanticProviderImpl::NodeInfo ::NodeInfo() = default;
AXFuchsiaSemanticProviderImpl::NodeInfo ::~NodeInfo() = default;
AXFuchsiaSemanticProviderImpl::Delegate::Delegate() = default;
AXFuchsiaSemanticProviderImpl::Delegate::~Delegate() = default;
AXFuchsiaSemanticProviderImpl::AXFuchsiaSemanticProviderImpl(
fuchsia_ui_views::ViewRef view_ref,
Delegate* delegate)
: delegate_(delegate) {
DCHECK(delegate_);
auto semantics_manager_client_end = base::fuchsia_component::Connect<
fuchsia_accessibility_semantics::SemanticsManager>();
// TODO(crbug.com/40263576): Create a path for gracefully failing to connect
// to SemanticsManager instead of CHECKing.
CHECK(semantics_manager_client_end.is_ok())
<< base::FidlConnectionErrorMessage(semantics_manager_client_end);
fidl::Client semantics_manager(
std::move(semantics_manager_client_end.value()),
async_get_default_dispatcher());
auto semantic_listener_endpoints = fidl::CreateEndpoints<
fuchsia_accessibility_semantics::SemanticListener>();
ZX_CHECK(semantic_listener_endpoints.is_ok(),
semantic_listener_endpoints.status_value());
semantic_listener_binding_.emplace(
async_get_default_dispatcher(),
std::move(semantic_listener_endpoints->server), this,
base::FidlBindingClosureWarningLogger(
"fuchsia.accessibility.semantics.SemanticListener"));
semantic_tree_event_handler_.emplace(base::BindOnce(
[](AXFuchsiaSemanticProviderImpl* semantic_provider,
fidl::UnbindInfo info) {
ZX_LOG(ERROR, info.status()) << "SemanticListener disconnected";
semantic_provider->delegate_->OnSemanticsManagerConnectionClosed(
info.status());
semantic_provider->semantic_updates_enabled_ = false;
},
this));
auto semantic_tree_endpoints =
fidl::CreateEndpoints<fuchsia_accessibility_semantics::SemanticTree>();
ZX_CHECK(semantic_tree_endpoints.is_ok(),
semantic_tree_endpoints.status_value());
semantic_tree_.Bind(std::move(semantic_tree_endpoints->client),
async_get_default_dispatcher(),
&semantic_tree_event_handler_.value());
auto result = semantics_manager->RegisterViewForSemantics({{
.view_ref = std::move(view_ref),
.listener = std::move(semantic_listener_endpoints->client),
.semantic_tree_request = std::move(semantic_tree_endpoints->server),
}});
if (result.is_error()) {
ZX_LOG(ERROR, result.error_value().status())
<< "Error calling RegisterViewForSemantics()";
}
}
AXFuchsiaSemanticProviderImpl::~AXFuchsiaSemanticProviderImpl() = default;
bool AXFuchsiaSemanticProviderImpl::Update(
fuchsia_accessibility_semantics::Node node) {
if (!semantic_updates_enabled())
return false;
DCHECK(node.node_id().has_value());
// If the updated node is the root, we need to account for the pixel scale in
// its transform.
//
// Otherwise, we need to update our connectivity book-keeping.
if (node.node_id() == kFuchsiaRootNodeId) {
gfx::Transform transform;
transform.PostScale(1 / pixel_scale_, 1 / pixel_scale_);
// Convert to fuchsia's transform type.
std::array<float, 16> mat = {};
transform.GetColMajorF(mat.data());
fuchsia_ui_gfx::Mat4 mat4{std::move(mat)};
// The root node will never have an offset container, so its transform will
// always be the identity matrix. Thus, we can safely overwrite it here.
node.node_to_container_transform(std::move(mat4));
} else {
auto found_not_reachable = not_reachable_.find(node.node_id().value());
const bool is_not_reachable = found_not_reachable != not_reachable_.end();
const std::optional<uint32_t> parent_node_id =
GetParentForNode(node.node_id().value());
if (is_not_reachable && parent_node_id) {
// Connection parent -> |node| exists now.
not_reachable_.erase(found_not_reachable);
nodes_[node.node_id().value()].parents.insert(*parent_node_id);
} else if (!parent_node_id) {
// No node or multiple nodes points to this one, so it is not reachable.
if (!is_not_reachable)
not_reachable_[node.node_id().value()] = {};
}
}
// If the node is not present in the map, the list of children will be empty
// so this is a no-op in the call below.
std::vector<uint32_t>& children = nodes_[node.node_id().value()].children;
// Before updating the node, update the list of children to be not reachable,
// in case the new list of children change.
MarkChildrenAsNotReachable(children, node.node_id().value());
children = node.child_ids().value_or(std::vector<uint32_t>());
MarkChildrenAsReachable(children, node.node_id().value());
Batch& batch = GetCurrentUnfilledBatch(Batch::Type::kUpdate);
batch.Append(std::move(node));
TryToCommit();
return true;
}
void AXFuchsiaSemanticProviderImpl::TryToCommit() {
// Don't send out updates while the tree is mid-mutation.
if (commit_inflight_ || batches_.empty())
return;
// If a tree has nodes but no root, wait until the root is present or all
// nodes are deleted.
if (!nodes_.empty() && nodes_.find(kFuchsiaRootNodeId) == nodes_.end())
return;
if (!not_reachable_.empty())
return;
for (auto& batch : batches_) {
batch.Apply(&semantic_tree_);
}
batches_.clear();
semantic_tree_->CommitUpdates().Then(
fit::bind_member(this, &AXFuchsiaSemanticProviderImpl::OnCommitComplete));
commit_inflight_ = true;
}
bool AXFuchsiaSemanticProviderImpl::Delete(uint32_t node_id) {
if (!semantic_updates_enabled())
return false;
auto it = nodes_.find(node_id);
if (it == nodes_.end())
return false;
if (it->second.parents.empty()) {
// No node points to this one, so it is safe to remove it from the tree.
not_reachable_.erase(node_id);
} else {
not_reachable_[node_id] =
it->second
.parents; // Zero or more parents can be pointing to this node.
}
MarkChildrenAsNotReachable(it->second.children, node_id);
nodes_.erase(it);
Batch& batch = GetCurrentUnfilledBatch(Batch::Type::kDelete);
batch.AppendDeletion(node_id);
TryToCommit();
return true;
}
void AXFuchsiaSemanticProviderImpl::SendEvent(
fuchsia_accessibility_semantics::SemanticEvent event) {
semantic_tree_->SendSemanticEvent({{.semantic_event = std::move(event)}})
.Then(
[](fidl::Result<
fuchsia_accessibility_semantics::SemanticTree::SendSemanticEvent>&
result) {
ZX_LOG_IF(ERROR, result.is_error(), result.error_value().status());
});
}
bool AXFuchsiaSemanticProviderImpl::HasPendingUpdates() const {
return commit_inflight_ || !batches_.empty();
}
bool AXFuchsiaSemanticProviderImpl::Clear() {
if (!semantic_updates_enabled())
return false;
batches_.clear();
not_reachable_.clear();
nodes_.clear();
Batch& batch = GetCurrentUnfilledBatch(Batch::Type::kDelete);
batch.AppendDeletion(kFuchsiaRootNodeId);
TryToCommit();
return true;
}
void AXFuchsiaSemanticProviderImpl::OnAccessibilityActionRequested(
AXFuchsiaSemanticProviderImpl::OnAccessibilityActionRequestedRequest&
request,
AXFuchsiaSemanticProviderImpl::OnAccessibilityActionRequestedCompleter::
Sync& completer) {
if (delegate_->OnAccessibilityAction(request.node_id(), request.action())) {
completer.Reply(true);
return;
}
// The action was not handled.
completer.Reply(false);
}
void AXFuchsiaSemanticProviderImpl::HitTest(
AXFuchsiaSemanticProviderImpl::HitTestRequest& request,
AXFuchsiaSemanticProviderImpl::HitTestCompleter::Sync& completer) {
delegate_->OnHitTest(
{{
.x = request.local_point().x() * pixel_scale_,
.y = request.local_point().y() * pixel_scale_,
}},
base::BindOnce(
[](HitTestCompleter::Async async_completer,
const fidl::Response<
fuchsia_accessibility_semantics::SemanticListener::HitTest>&
result) { async_completer.Reply(result); },
completer.ToAsync()));
return;
}
void AXFuchsiaSemanticProviderImpl::OnSemanticsModeChanged(
AXFuchsiaSemanticProviderImpl::OnSemanticsModeChangedRequest& request,
AXFuchsiaSemanticProviderImpl::OnSemanticsModeChangedCompleter::Sync&
completer) {
if (semantic_updates_enabled_ != request.updates_enabled()) {
delegate_->OnSemanticsEnabled(request.updates_enabled());
}
semantic_updates_enabled_ = request.updates_enabled();
completer.Reply();
}
void AXFuchsiaSemanticProviderImpl::MarkChildrenAsNotReachable(
const std::vector<uint32_t>& child_ids,
uint32_t parent_id) {
for (const uint32_t child_id : child_ids) {
const auto it = nodes_.find(child_id);
if (it != nodes_.end()) {
it->second.parents.erase(parent_id);
if (it->second.parents.empty())
not_reachable_[child_id] = {};
else
not_reachable_.erase(child_id);
} else {
auto not_reachable_it = not_reachable_.find(child_id);
// Child id is no longer in the regular map, deletes it also from
// not_reachable_ if no parent points to it anymore.
if (not_reachable_it != not_reachable_.end()) {
not_reachable_it->second.erase(parent_id);
if (not_reachable_it->second.empty())
not_reachable_.erase(not_reachable_it);
}
}
}
}
void AXFuchsiaSemanticProviderImpl::MarkChildrenAsReachable(
const std::vector<uint32_t>& child_ids,
uint32_t parent_id) {
for (const uint32_t child_id : child_ids) {
auto it = nodes_.find(child_id);
if (it == nodes_.end())
not_reachable_[child_id].insert(parent_id);
else {
it->second.parents.insert(parent_id);
if (it->second.parents.size() == 1)
not_reachable_.erase(child_id);
else
not_reachable_[child_id].insert(parent_id);
}
}
}
std::optional<uint32_t> AXFuchsiaSemanticProviderImpl::GetParentForNode(
const uint32_t node_id) {
const auto it = nodes_.find(node_id);
if (it != nodes_.end()) {
if (it->second.parents.size() == 1)
return *it->second.parents.begin();
else
return std::nullopt;
}
const auto not_reachable_it = not_reachable_.find(node_id);
if (not_reachable_it != not_reachable_.end()) {
if (not_reachable_it->second.size() == 1)
return *not_reachable_it->second.begin();
else
return std::nullopt;
}
return std::nullopt;
}
AXFuchsiaSemanticProviderImpl::Batch&
AXFuchsiaSemanticProviderImpl::GetCurrentUnfilledBatch(Batch::Type type) {
if (batches_.empty() || batches_.back().type() != type ||
batches_.back().IsFull())
batches_.emplace_back(type);
return batches_.back();
}
void AXFuchsiaSemanticProviderImpl::OnCommitComplete(
fidl::Result<fuchsia_accessibility_semantics::SemanticTree::CommitUpdates>&
result) {
ZX_LOG_IF(ERROR, result.is_error(), result.error_value().status());
commit_inflight_ = false;
TryToCommit();
}
float AXFuchsiaSemanticProviderImpl::GetPixelScale() const {
return pixel_scale_;
}
void AXFuchsiaSemanticProviderImpl::SetPixelScale(float pixel_scale) {
pixel_scale_ = pixel_scale;
// If the root node exists, then we need to update its transform to reflect
// the new pixel scale.
if (nodes_.find(kFuchsiaRootNodeId) == nodes_.end())
return;
// We need to fill the `child_ids` field to prevent Update() from trampling
// our connectivity bookkeeping. Update() will handle setting the
// `node_to_container_transform` field.
Update({{
.node_id = kFuchsiaRootNodeId,
.child_ids = nodes_[kFuchsiaRootNodeId].children,
}});
}
} // namespace ui
|