blob: eb4d5d6b273e96483aa982edba4573c90970d6b2 [file] [log] [blame]
[email protected]4b02bbca2013-11-22 08:59:031// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UI_ACCESSIBILITY_AX_TREE_H_
6#define UI_ACCESSIBILITY_AX_TREE_H_
7
[email protected]d4e273462013-12-04 04:37:588#include <set>
[email protected]4b02bbca2013-11-22 08:59:039
[email protected]d4e273462013-12-04 04:37:5810#include "base/containers/hash_tables.h"
[email protected]4b02bbca2013-11-22 08:59:0311#include "ui/accessibility/ax_export.h"
12#include "ui/accessibility/ax_tree.h"
13#include "ui/accessibility/ax_tree_update.h"
14
15namespace ui {
16
17class AXNode;
[email protected]e736e81b2014-02-24 07:15:5818struct AXTreeUpdateState;
19
20// Used when you want to be notified when changes happen to the tree.
21class AX_EXPORT AXTreeDelegate {
22 public:
23 AXTreeDelegate();
24 virtual ~AXTreeDelegate();
25
26 // Called just before a node is deleted. Its id and data will be valid,
27 // but its links to parents and children are invalid. This is called
28 // in the middle of an update, the tree may be in an invalid state!
29 virtual void OnNodeWillBeDeleted(AXNode* node) = 0;
30
31 // Called after a new node is created. It's guaranteed to be called
32 // after it's been fully initialized, so you can rely on its data and
33 // links to parents and children being valid. This will be called on
34 // parents before it's called on their children.
35 virtual void OnNodeCreated(AXNode* node) = 0;
36
37 // Called when a node changes its data or children.
38 virtual void OnNodeChanged(AXNode* node) = 0;
39
40 // Called when the root node changes.
41 virtual void OnRootChanged(AXNode* new_root) = 0;
42};
[email protected]4b02bbca2013-11-22 08:59:0343
44// AXTree is a live, managed tree of AXNode objects that can receive
45// updates from another AXTreeSource via AXTreeUpdates, and it can be
46// used as a source for sending updates to another client tree.
47// It's designed to be subclassed to implement support for native
48// accessibility APIs on a specific platform.
49class AX_EXPORT AXTree {
50 public:
51 AXTree();
52 explicit AXTree(const AXTreeUpdate& initial_state);
53 virtual ~AXTree();
54
[email protected]e736e81b2014-02-24 07:15:5855 virtual void SetDelegate(AXTreeDelegate* delegate);
56
[email protected]4b02bbca2013-11-22 08:59:0357 virtual AXNode* GetRoot() const;
58 virtual AXNode* GetFromId(int32 id) const;
59
[email protected]d4e273462013-12-04 04:37:5860 // Returns true on success. If it returns false, it's a fatal error
61 // and this tree should be destroyed, and the source of the tree update
62 // should not be trusted any longer.
[email protected]4b02bbca2013-11-22 08:59:0363 virtual bool Unserialize(const AXTreeUpdate& update);
64
[email protected]5eec2f52014-01-06 22:30:5465 // Return a multi-line indented string representation, for logging.
66 std::string ToString() const;
67
[email protected]d4e273462013-12-04 04:37:5868 // A string describing the error from an unsuccessful Unserialize,
69 // for testing and debugging.
70 const std::string& error() { return error_; }
71
[email protected]e736e81b2014-02-24 07:15:5872 private:
73 AXNode* CreateNode(AXNode* parent, int32 id, int32 index_in_parent);
[email protected]4b02bbca2013-11-22 08:59:0374
75 // This is called from within Unserialize(), it returns true on success.
[email protected]e736e81b2014-02-24 07:15:5876 bool UpdateNode(const AXNodeData& src, AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:0377
[email protected]e736e81b2014-02-24 07:15:5878 void OnRootChanged();
[email protected]4b02bbca2013-11-22 08:59:0379
[email protected]4b02bbca2013-11-22 08:59:0380 // Convenience function to create a node and call Initialize on it.
81 AXNode* CreateAndInitializeNode(
82 AXNode* parent, int32 id, int32 index_in_parent);
83
84 // Call Destroy() on |node|, and delete it from the id map, and then
85 // call recursively on all nodes in its subtree.
86 void DestroyNodeAndSubtree(AXNode* node);
87
88 // Iterate over the children of |node| and for each child, destroy the
89 // child and its subtree if its id is not in |new_child_ids|. Returns
90 // true on success, false on fatal error.
91 bool DeleteOldChildren(AXNode* node,
92 const std::vector<int32> new_child_ids);
93
94 // Iterate over |new_child_ids| and populate |new_children| with
95 // pointers to child nodes, reusing existing nodes already in the tree
96 // if they exist, and creating otherwise. Reparenting is disallowed, so
97 // if the id already exists as the child of another node, that's an
[email protected]e736e81b2014-02-24 07:15:5898 // error. Returns true on success, false on fatal error.
[email protected]4b02bbca2013-11-22 08:59:0399 bool CreateNewChildVector(AXNode* node,
100 const std::vector<int32> new_child_ids,
[email protected]d4e273462013-12-04 04:37:58101 std::vector<AXNode*>* new_children,
[email protected]e736e81b2014-02-24 07:15:58102 AXTreeUpdateState* update_state);
[email protected]4b02bbca2013-11-22 08:59:03103
[email protected]e736e81b2014-02-24 07:15:58104 AXTreeDelegate* delegate_;
[email protected]4b02bbca2013-11-22 08:59:03105 AXNode* root_;
106 base::hash_map<int32, AXNode*> id_map_;
[email protected]d4e273462013-12-04 04:37:58107 std::string error_;
[email protected]4b02bbca2013-11-22 08:59:03108};
109
110} // namespace ui
111
112#endif // UI_ACCESSIBILITY_AX_TREE_H_