Skip to content

Commit

Permalink
Creates an observer interface that can be used to observe node additi…
Browse files Browse the repository at this point in the history
…ons, removals and updates on a tree source

During offline discussions it was decided that modifying the AXTreeSource interface in order to add methods
that are specific only to an AXTreeSourceAnnotator component, such as AXImageAnnotator,
would be undesirable because it would polude the interface with methods that would only be implemented by a handful of tree sources.

This patch takes another approach: It adds an AXTreeSourceObserver which can be
used by the annotator to get notified when nodes are added, removed, or updated in the tree source.

R=dtseng@chromium.org, mschillaci@google.com

AX-Relnotes: n/a.
Bug: 1248380
Change-Id: I0bab9c3eb054bca718cbba5c795d512157948d18
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3164937
Commit-Queue: Nektarios Paisios <nektar@chromium.org>
Reviewed-by: Mark Schillaci <mschillaci@google.com>
Cr-Commit-Position: refs/heads/main@{#923428}
  • Loading branch information
Nektarios Paisios authored and Chromium LUCI CQ committed Sep 21, 2021
1 parent 1962045 commit 700dd6f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions ui/accessibility/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ component("accessibility") {
"ax_tree_source.h",
"ax_tree_source_annotator.h",
"ax_tree_source_checker.h",
"ax_tree_source_observer.h",
"null_ax_action_target.cc",
"null_ax_action_target.h",
]
Expand Down
22 changes: 20 additions & 2 deletions ui/accessibility/ax_tree_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

#include <stdint.h>

#include <string>
#include <vector>

#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_tree_data.h"
#include "ui/accessibility/ax_tree_source_observer.h"

namespace ui {

Expand All @@ -25,7 +28,7 @@ namespace ui {
template <typename AXNodeSource>
class AXTreeSource {
public:
virtual ~AXTreeSource() {}
virtual ~AXTreeSource() = default;

// Get the tree data and returns true if there is any data to copy.
virtual bool GetTreeData(AXTreeData* data) const = 0;
Expand All @@ -37,7 +40,9 @@ class AXTreeSource {
// null node, i.e. one that will return false if you call IsValid on it.
virtual AXNodeSource GetFromId(AXNodeID id) const = 0;

// Return the id of a node. All ids must be positive integers.
// Return the id of a node. All ids must be positive integers; 0 is not a
// valid ID. IDs are unique only across the current tree source, not across
// tree sources.
virtual AXNodeID GetId(AXNodeSource node) const = 0;

// Append all children of |node| to |out_children|.
Expand Down Expand Up @@ -77,6 +82,19 @@ class AXTreeSource {
// indexed by node ID and delete nodes when they're no longer needed.
virtual void SerializerClearedNode(AXNodeID node_id) {}

// The following methods should be overridden in order to add or remove an
// `AXTreeSourceObserver`, which is notified when nodes are added, removed or
// updated in this tree source.

virtual void AddObserver(ui::AXTreeSourceObserver<AXNodeSource>* observer) {
NOTIMPLEMENTED();
}

virtual void RemoveObserver(
ui::AXTreeSourceObserver<AXNodeSource>* observer) {
NOTIMPLEMENTED();
}

protected:
AXTreeSource() {}
};
Expand Down
14 changes: 3 additions & 11 deletions ui/accessibility/ax_tree_source_annotator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_export.h"
#include "ui/accessibility/ax_tree_source.h"
#include "ui/accessibility/ax_tree_source_observer.h"

namespace ui {

Expand All @@ -18,7 +19,8 @@ namespace ui {
// generated alt text for unlabeled images. A specific annotator may be able to
// work on multiple `AXNodeSource`s, e.g. `AXNode*` and `WebAXObject`.
template <typename AXNodeSource>
class AX_EXPORT AXTreeSourceAnnotator {
class AX_EXPORT AXTreeSourceAnnotator
: public AXTreeSourceObserver<AXNodeSource> {
public:
virtual ~AXTreeSourceAnnotator() = default;

Expand Down Expand Up @@ -50,16 +52,6 @@ class AX_EXPORT AXTreeSourceAnnotator {
virtual bool HasNodeInCache(const AXTreeSource<AXNodeSource>& tree_source,
const AXNodeSource& node_source) const = 0;

// Methods for observing an `AXTreeSource` in order to find out whether a node
// that needs automatically generated annotations has been added, removed or
// updated.
virtual void OnNodeAdded(const AXTreeSource<AXNodeSource>& tree_source,
const AXNodeSource& node_source) = 0;
virtual void OnNodeUpdated(const AXTreeSource<AXNodeSource>& tree_source,
const AXNodeSource& node_source) = 0;
virtual void OnNodeRemoved(const AXTreeSource<AXNodeSource>& tree_source,
const AXNodeSource& node_source) = 0;

// Returns true if the existing accessible name for a node consists of mostly
// stopwords, such as "the" and "of". This would be a strong indication that
// the accessible name is not informative and should be replaced by an
Expand Down
32 changes: 32 additions & 0 deletions ui/accessibility/ax_tree_source_observer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_ACCESSIBILITY_AX_TREE_SOURCE_OBSERVER_H_
#define UI_ACCESSIBILITY_AX_TREE_SOURCE_OBSERVER_H_

#include "base/observer_list_types.h"
#include "ui/accessibility/ax_export.h"

namespace ui {

template <typename AXNodeSource>
class AXTreeSource;

// This is an interface for a class that observes changes to an `AXTreeSource`.
template <typename AXNodeSource>
class AX_EXPORT AXTreeSourceObserver : public base::CheckedObserver {
public:
~AXTreeSourceObserver() override = default;

virtual void OnNodeAdded(const AXTreeSource<AXNodeSource>& tree_source,
const AXNodeSource& node_source) = 0;
virtual void OnNodeUpdated(const AXTreeSource<AXNodeSource>& tree_source,
const AXNodeSource& node_source) = 0;
virtual void OnNodeRemoved(const AXTreeSource<AXNodeSource>& tree_source,
const AXNodeSource& node_source) = 0;
};

} // namespace ui

#endif // UI_ACCESSIBILITY_AX_TREE_SOURCE_OBSERVER_H_

0 comments on commit 700dd6f

Please sign in to comment.