summaryrefslogtreecommitdiff
path: root/chromium/extensions/renderer/api/automation/automation_ax_tree_wrapper.h
blob: 487d3d4d6661ba465816f49fa3bb8f6c341e71f4 (plain)
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
// Copyright 2017 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 EXTENSIONS_RENDERER_API_AUTOMATION_AUTOMATION_AX_TREE_WRAPPER_H_
#define EXTENSIONS_RENDERER_API_AUTOMATION_AUTOMATION_AX_TREE_WRAPPER_H_

#include "extensions/common/api/automation.h"
#include "ui/accessibility/ax_event_generator.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_tree.h"
#include "ui/accessibility/ax_tree_manager.h"

struct ExtensionMsg_AccessibilityEventBundleParams;

namespace extensions {

class AutomationInternalCustomBindings;

// A class that wraps one AXTree and all of the additional state
// and helper methods needed to use it for the automation API.
class AutomationAXTreeWrapper : public ui::AXTreeObserver,
                                public ui::AXTreeManager {
 public:
  AutomationAXTreeWrapper(ui::AXTreeID tree_id,
                          AutomationInternalCustomBindings* owner);
  ~AutomationAXTreeWrapper() override;

  // Returns the AutomationAXTreeWrapper that lists |tree_id| as one of its
  // child trees, if any.
  static AutomationAXTreeWrapper* GetParentOfTreeId(ui::AXTreeID tree_id);

  static std::map<ui::AXTreeID, AutomationAXTreeWrapper*>&
  GetChildTreeIDReverseMap();

  // Multiroot tree lookup.
  static ui::AXNode* GetParentTreeNodeForAppID(
      const std::string& app_id,
      const AutomationInternalCustomBindings* owner);
  static AutomationAXTreeWrapper* GetParentTreeWrapperForAppID(
      const std::string& app_id,
      const AutomationInternalCustomBindings* owner);
  static ui::AXNode* GetChildTreeNodeForAppID(
      const std::string& app_id,
      const AutomationInternalCustomBindings* owner);

  ui::AXTree* tree() { return &tree_; }
  AutomationInternalCustomBindings* owner() { return owner_; }

  // Called by AutomationInternalCustomBindings::OnAccessibilityEvents on
  // the AutomationAXTreeWrapper instance for the correct tree corresponding
  // to this event. Unserializes the tree update and calls back to
  // AutomationInternalCustomBindings to fire any automation events needed.
  bool OnAccessibilityEvents(
      const ExtensionMsg_AccessibilityEventBundleParams& events,
      bool is_active_profile);

  // Returns true if this is the desktop tree.
  bool IsDesktopTree() const;

  // Returns whether this tree is scaled by a device scale factor.
  bool HasDeviceScaleFactor() const;

  // Returns whether |node_id| is the focused node in this tree. Accounts for
  // cases where this tree itself is not focused. Behaves similarly to
  // document.activeElement (within the DOM).
  bool IsInFocusChain(int32_t node_id);

  ui::AXTree::Selection GetUnignoredSelection();

  // Returns an AXNode from the underlying tree if it both exists and is not
  // ignored.
  ui::AXNode* GetUnignoredNodeFromId(int32_t id);

  // Accessibility focus is globally set via automation API from js.
  void SetAccessibilityFocus(int32_t node_id);
  ui::AXNode* GetAccessibilityFocusedNode();

  int32_t accessibility_focused_id() { return accessibility_focused_id_; }

  // Gets the parent tree wrapper.
  AutomationAXTreeWrapper* GetParentTree();

  // Gets the first tree wrapper with an unignored root. This can be |this| tree
  // wrapper or an ancestor. A root is ignored if the tree has valid nodes with
  // a valid ax::mojom::StringAttribute::kChildTreeNodeAppId making the tree
  // have multiple roots.
  AutomationAXTreeWrapper* GetTreeWrapperWithUnignoredRoot();

  // Gets a parent tree wrapper by following the first valid parent tree node
  // app id. Useful if this tree contains app ids that always reference the same
  // parent tree.
  AutomationAXTreeWrapper* GetParentTreeFromAnyAppID();

  // Updates or gets this wrapper with the latest state of listeners in js.
  void EventListenerAdded(api::automation::EventType event_type,
                          ui::AXNode* node);
  void EventListenerRemoved(api::automation::EventType event_type,
                            ui::AXNode* node);
  bool HasEventListener(api::automation::EventType event_type,
                        ui::AXNode* node);

  // AXTreeManager overrides.
  ui::AXNode* GetNodeFromTree(const ui::AXTreeID tree_id,
                              const ui::AXNodeID node_id) const override;
  ui::AXNode* GetNodeFromTree(const ui::AXNodeID node_id) const override;
  ui::AXTreeID GetTreeID() const override;
  ui::AXTreeID GetParentTreeID() const override;
  ui::AXNode* GetRootAsAXNode() const override;
  ui::AXNode* GetParentNodeFromParentTreeAsAXNode() const override;
  std::string ToString() const override;

 private:
  // AXTreeObserver overrides.
  void OnNodeDataChanged(ui::AXTree* tree,
                         const ui::AXNodeData& old_node_data,
                         const ui::AXNodeData& new_node_data) override;
  void OnStringAttributeChanged(ui::AXTree* tree,
                                ui::AXNode* node,
                                ax::mojom::StringAttribute attr,
                                const std::string& old_value,
                                const std::string& new_value) override;
  void OnNodeWillBeDeleted(ui::AXTree* tree, ui::AXNode* node) override;
  void OnNodeCreated(ui::AXTree* tree, ui::AXNode* node) override;
  void OnAtomicUpdateFinished(ui::AXTree* tree,
                              bool root_changed,
                              const std::vector<Change>& changes) override;

  ui::AXTreeID tree_id_;
  ui::AXTree tree_;
  AutomationInternalCustomBindings* owner_;
  std::vector<int> deleted_node_ids_;
  std::vector<int> text_changed_node_ids_;
  ui::AXEventGenerator event_generator_;

  int32_t accessibility_focused_id_ = ui::kInvalidAXNodeID;

  // Tracks whether a tree change event was sent during unserialization. Tree
  // changes outside of unserialization do not get reflected here. The value is
  // reset after unserialization.
  bool did_send_tree_change_during_unserialization_ = false;

  // Maps a node to a set containing events for which the node has listeners.
  std::map<int32_t, std::set<api::automation::EventType>> node_id_to_events_;

  // A collection of all app ids in this tree nodes'
  // ax::mojom::StringAttribute::kParentTreeNodeAppId.
  std::set<std::string> all_parent_tree_node_app_ids_;

  DISALLOW_COPY_AND_ASSIGN(AutomationAXTreeWrapper);
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_API_AUTOMATION_AUTOMATION_AX_TREE_WRAPPER_H_