blob: e0b18bd31f5af2d32dd3bf9401efbf230b562193 [file] [log] [blame]
[email protected]eaf8a3422012-01-24 23:35:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c5c2a672010-10-01 23:28:042// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ba1fa652011-06-25 05:16:225#ifndef CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_
6#define CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_
[email protected]c5c2a672010-10-01 23:28:047
[email protected]f27e81c2010-10-07 05:20:238#include <map>
9#include <utility>
10#include <vector>
11
[email protected]c5c2a672010-10-01 23:28:0412#include "base/basictypes.h"
[email protected]eecf89f02013-08-20 23:41:5113#include "base/strings/string16.h"
mohan.reddy0079fba2014-09-25 07:25:2314#include "base/strings/string_split.h"
[email protected]f27e81c2010-10-07 05:20:2315#include "build/build_config.h"
[email protected]f3112a52011-09-30 23:47:4916#include "content/common/content_export.h"
[email protected]10760e4a2013-09-04 23:32:2017#include "third_party/WebKit/public/web/WebAXEnums.h"
[email protected]d96f3842014-04-21 18:07:2918#include "ui/accessibility/ax_node.h"
[email protected]5eec2f52014-01-06 22:30:5419#include "ui/accessibility/ax_node_data.h"
nektar6baff46f2015-02-25 06:22:3520#include "ui/accessibility/ax_text_utils.h"
[email protected]f27e81c2010-10-07 05:20:2321
[email protected]95b3f5442012-05-06 17:10:0722#if defined(OS_MACOSX) && __OBJC__
23@class BrowserAccessibilityCocoa;
[email protected]e6b34872012-10-24 20:51:3224#endif
25
26namespace content {
27class BrowserAccessibilityManager;
28#if defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:2329class BrowserAccessibilityWin;
30#endif
31
[email protected]c5c2a672010-10-01 23:28:0432////////////////////////////////////////////////////////////////////////////////
33//
34// BrowserAccessibility
35//
dmazzoni0b5d2482014-09-10 19:45:5736// A BrowserAccessibility object represents one node in the accessibility
37// tree on the browser side. It exactly corresponds to one WebAXObject from
38// Blink. It's owned by a BrowserAccessibilityManager.
[email protected]c5c2a672010-10-01 23:28:0439//
dmazzoni0b5d2482014-09-10 19:45:5740// There are subclasses of BrowserAccessibility for each platform where
41// we implement native accessibility APIs. This base class is used occasionally
42// for tests.
[email protected]c5c2a672010-10-01 23:28:0443//
44////////////////////////////////////////////////////////////////////////////////
[email protected]f3112a52011-09-30 23:47:4945class CONTENT_EXPORT BrowserAccessibility {
[email protected]c5c2a672010-10-01 23:28:0446 public:
[email protected]f27e81c2010-10-07 05:20:2347 // Creates a platform specific BrowserAccessibility. Ownership passes to the
[email protected]c5c2a672010-10-01 23:28:0448 // caller.
[email protected]f27e81c2010-10-07 05:20:2349 static BrowserAccessibility* Create();
50
[email protected]c5c2a672010-10-01 23:28:0451 virtual ~BrowserAccessibility();
52
[email protected]d96f3842014-04-21 18:07:2953 // Called only once, immediately after construction. The constructor doesn't
54 // take any arguments because in the Windows subclass we use a special
55 // function to construct a COM object.
56 virtual void Init(BrowserAccessibilityManager* manager, ui::AXNode* node);
[email protected]b05cd542011-06-08 01:38:0257
[email protected]d96f3842014-04-21 18:07:2958 // Called after the object is first initialized and again every time
59 // its data changes.
dmazzoni9b381f32015-01-27 00:34:0860 virtual void OnDataChanged() {}
[email protected]d96f3842014-04-21 18:07:2961
dmazzoni9b381f32015-01-27 00:34:0862 virtual void OnSubtreeWillBeDeleted() {}
63
[email protected]d96f3842014-04-21 18:07:2964 // Called when the location changed.
dmazzoni487bac02014-08-27 20:54:1165 virtual void OnLocationChanged() {}
[email protected]a10a5f82013-05-06 05:24:5666
[email protected]f27e81c2010-10-07 05:20:2367 // Return true if this object is equal to or a descendant of |ancestor|.
68 bool IsDescendantOf(BrowserAccessibility* ancestor);
69
[email protected]933af8c2013-11-01 17:59:4070 // Returns true if this is a leaf node on this platform, meaning any
71 // children should not be exposed to this platform's native accessibility
72 // layer. Each platform subclass should implement this itself.
73 // The definition of a leaf may vary depending on the platform,
74 // but a leaf node should never have children that are focusable or
75 // that might send notifications.
76 virtual bool PlatformIsLeaf() const;
77
78 // Returns the number of children of this object, or 0 if PlatformIsLeaf()
79 // returns true.
80 uint32 PlatformChildCount() const;
81
82 // Return a pointer to the child at the given index, or NULL for an
83 // invalid index. Returns NULL if PlatformIsLeaf() returns true.
84 BrowserAccessibility* PlatformGetChild(uint32 child_index) const;
[email protected]f27e81c2010-10-07 05:20:2385
dmazzoni9b381f32015-01-27 00:34:0886 // Returns true if an ancestor of this node (not including itself) is a
87 // leaf node, meaning that this node is not actually exposed to the
88 // platform.
89 bool PlatformIsChildOfLeaf() const;
90
[email protected]f27e81c2010-10-07 05:20:2391 // Return the previous sibling of this object, or NULL if it's the first
92 // child of its parent.
93 BrowserAccessibility* GetPreviousSibling();
94
95 // Return the next sibling of this object, or NULL if it's the last child
96 // of its parent.
97 BrowserAccessibility* GetNextSibling();
98
[email protected]9052cca2011-11-30 23:59:3199 // Returns the bounds of this object in coordinates relative to the
100 // top-left corner of the overall web area.
[email protected]7f2d9f6b2013-06-13 22:57:01101 gfx::Rect GetLocalBoundsRect() const;
[email protected]02747d4e2010-11-03 19:10:00102
[email protected]9052cca2011-11-30 23:59:31103 // Returns the bounds of this object in screen coordinates.
[email protected]7f2d9f6b2013-06-13 22:57:01104 gfx::Rect GetGlobalBoundsRect() const;
[email protected]9052cca2011-11-30 23:59:31105
[email protected]516c16a2013-11-04 21:32:20106 // Returns the bounds of the given range in coordinates relative to the
107 // top-left corner of the overall web area. Only valid when the
108 // role is WebAXRoleStaticText.
109 gfx::Rect GetLocalBoundsForRange(int start, int len) const;
110
111 // Same as GetLocalBoundsForRange, in screen coordinates. Only valid when
112 // the role is WebAXRoleStaticText.
113 gfx::Rect GetGlobalBoundsForRange(int start, int len) const;
114
nektar6baff46f2015-02-25 06:22:35115 // Searches in the given text and from the given offset until the start of
116 // the next or previous word is found and returns its position.
117 int GetWordStartBoundary(
118 int start, ui::TextBoundaryDirection direction) const;
119
[email protected]9052cca2011-11-30 23:59:31120 // Returns the deepest descendant that contains the specified point
121 // (in global screen coordinates).
[email protected]02747d4e2010-11-03 19:10:00122 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point);
123
[email protected]c477540962013-03-07 00:43:10124 // Marks this object for deletion, releases our reference to it, and
[email protected]d96f3842014-04-21 18:07:29125 // nulls out the pointer to the underlying AXNode. May not delete
126 // the object immediately due to reference counting.
[email protected]8368e3c2011-03-08 19:26:24127 //
[email protected]c477540962013-03-07 00:43:10128 // Reference counting is used on some platforms because the
[email protected]8368e3c2011-03-08 19:26:24129 // operating system may hold onto a reference to a BrowserAccessibility
[email protected]c477540962013-03-07 00:43:10130 // object even after we're through with it. When a BrowserAccessibility
131 // has had Destroy() called but its reference count is not yet zero,
[email protected]d96f3842014-04-21 18:07:29132 // instance_active() returns false and queries on this object return failure.
[email protected]c477540962013-03-07 00:43:10133 virtual void Destroy();
[email protected]8368e3c2011-03-08 19:26:24134
135 // Subclasses should override this to support platform reference counting.
136 virtual void NativeAddReference() { }
137
138 // Subclasses should override this to support platform reference counting.
[email protected]402da9f2011-03-08 19:45:41139 virtual void NativeReleaseReference();
[email protected]8368e3c2011-03-08 19:26:24140
141 //
[email protected]f27e81c2010-10-07 05:20:23142 // Accessors
[email protected]8368e3c2011-03-08 19:26:24143 //
144
[email protected]0d7dad62010-10-19 21:18:50145 BrowserAccessibilityManager* manager() const { return manager_; }
[email protected]d96f3842014-04-21 18:07:29146 bool instance_active() const { return node_ != NULL; }
147 ui::AXNode* node() const { return node_; }
[email protected]99d408f2013-12-04 07:19:43148
[email protected]c1f9be742014-04-10 13:15:46149 // These access the internal accessibility tree, which doesn't necessarily
150 // reflect the accessibility tree that should be exposed on each platform.
151 // Use PlatformChildCount and PlatformGetChild to implement platform
152 // accessibility APIs.
[email protected]d96f3842014-04-21 18:07:29153 uint32 InternalChildCount() const;
154 BrowserAccessibility* InternalGetChild(uint32 child_index) const;
[email protected]c1f9be742014-04-10 13:15:46155
[email protected]d96f3842014-04-21 18:07:29156 BrowserAccessibility* GetParent() const;
157 int32 GetIndexInParent() const;
[email protected]c1f9be742014-04-10 13:15:46158
[email protected]d96f3842014-04-21 18:07:29159 int32 GetId() const;
160 const ui::AXNodeData& GetData() const;
161 gfx::Rect GetLocation() const;
162 int32 GetRole() const;
163 int32 GetState() const;
164
mohan.reddy0079fba2014-09-25 07:25:23165 typedef base::StringPairs HtmlAttributes;
[email protected]d96f3842014-04-21 18:07:29166 const HtmlAttributes& GetHtmlAttributes() const;
[email protected]c1f9be742014-04-10 13:15:46167
dmazzoni9b381f32015-01-27 00:34:08168
169 // Returns true if this is a native platform-specific object, vs a
170 // cross-platform generic object. Don't call ToBrowserAccessibilityXXX if
171 // IsNative returns false.
172 virtual bool IsNative() const;
173
[email protected]95b3f5442012-05-06 17:10:07174#if defined(OS_MACOSX) && __OBJC__
[email protected]cd3e2d902012-05-09 07:05:20175 BrowserAccessibilityCocoa* ToBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10176#elif defined(OS_WIN)
[email protected]cd3e2d902012-05-09 07:05:20177 BrowserAccessibilityWin* ToBrowserAccessibilityWin();
[email protected]f27e81c2010-10-07 05:20:23178#endif
179
[email protected]eecf89f02013-08-20 23:41:51180 // Accessing accessibility attributes:
181 //
182 // There are dozens of possible attributes for an accessibility node,
183 // but only a few tend to apply to any one object, so we store them
184 // in sparse arrays of <attribute id, attribute value> pairs, organized
185 // by type (bool, int, float, string, int list).
186 //
187 // There are three accessors for each type of attribute: one that returns
188 // true if the attribute is present and false if not, one that takes a
189 // pointer argument and returns true if the attribute is present (if you
190 // need to distinguish between the default value and a missing attribute),
191 // and another that returns the default value for that type if the
192 // attribute is not present. In addition, strings can be returned as
[email protected]fcf75d42013-12-03 20:11:26193 // either std::string or base::string16, for convenience.
[email protected]ee845122011-09-01 08:44:16194
[email protected]5eec2f52014-01-06 22:30:54195 bool HasBoolAttribute(ui::AXBoolAttribute attr) const;
196 bool GetBoolAttribute(ui::AXBoolAttribute attr) const;
197 bool GetBoolAttribute(ui::AXBoolAttribute attr, bool* value) const;
[email protected]eecf89f02013-08-20 23:41:51198
[email protected]5eec2f52014-01-06 22:30:54199 bool HasFloatAttribute(ui::AXFloatAttribute attr) const;
200 float GetFloatAttribute(ui::AXFloatAttribute attr) const;
201 bool GetFloatAttribute(ui::AXFloatAttribute attr, float* value) const;
[email protected]02747d4e2010-11-03 19:10:00202
[email protected]5eec2f52014-01-06 22:30:54203 bool HasIntAttribute(ui::AXIntAttribute attribute) const;
204 int GetIntAttribute(ui::AXIntAttribute attribute) const;
205 bool GetIntAttribute(ui::AXIntAttribute attribute, int* value) const;
[email protected]ee845122011-09-01 08:44:16206
[email protected]eecf89f02013-08-20 23:41:51207 bool HasStringAttribute(
[email protected]5eec2f52014-01-06 22:30:54208 ui::AXStringAttribute attribute) const;
209 const std::string& GetStringAttribute(ui::AXStringAttribute attribute) const;
210 bool GetStringAttribute(ui::AXStringAttribute attribute,
[email protected]eecf89f02013-08-20 23:41:51211 std::string* value) const;
212
[email protected]5eec2f52014-01-06 22:30:54213 bool GetString16Attribute(ui::AXStringAttribute attribute,
[email protected]fcf75d42013-12-03 20:11:26214 base::string16* value) const;
215 base::string16 GetString16Attribute(
[email protected]5eec2f52014-01-06 22:30:54216 ui::AXStringAttribute attribute) const;
[email protected]eecf89f02013-08-20 23:41:51217
[email protected]5eec2f52014-01-06 22:30:54218 bool HasIntListAttribute(ui::AXIntListAttribute attribute) const;
[email protected]eecf89f02013-08-20 23:41:51219 const std::vector<int32>& GetIntListAttribute(
[email protected]5eec2f52014-01-06 22:30:54220 ui::AXIntListAttribute attribute) const;
221 bool GetIntListAttribute(ui::AXIntListAttribute attribute,
[email protected]eecf89f02013-08-20 23:41:51222 std::vector<int32>* value) const;
223
[email protected]ee845122011-09-01 08:44:16224 // Retrieve the value of a html attribute from the attribute map and
225 // returns true if found.
[email protected]fcf75d42013-12-03 20:11:26226 bool GetHtmlAttribute(const char* attr, base::string16* value) const;
[email protected]eecf89f02013-08-20 23:41:51227 bool GetHtmlAttribute(const char* attr, std::string* value) const;
[email protected]ee845122011-09-01 08:44:16228
[email protected]38f1e3b052012-02-10 21:46:07229 // Utility method to handle special cases for ARIA booleans, tristates and
230 // booleans which have a "mixed" state.
231 //
232 // Warning: the term "Tristate" is used loosely by the spec and here,
233 // as some attributes support a 4th state.
234 //
235 // The following attributes are appropriate to use with this method:
236 // aria-selected (selectable)
237 // aria-grabbed (grabbable)
238 // aria-expanded (expandable)
239 // aria-pressed (toggleable/pressable) -- supports 4th "mixed" state
240 // aria-checked (checkable) -- supports 4th "mixed state"
241 bool GetAriaTristate(const char* attr_name,
242 bool* is_defined,
243 bool* is_mixed) const;
244
[email protected]c43b0dc2011-12-03 04:31:13245 // Returns true if the bit corresponding to the given state enum is 1.
[email protected]5eec2f52014-01-06 22:30:54246 bool HasState(ui::AXState state_enum) const;
[email protected]c43b0dc2011-12-03 04:31:13247
je_julie.kimff9c8d32015-01-12 11:14:46248 // Returns true if this node is an cell or an table header.
249 bool IsCellOrTableHeaderRole() const;
250
[email protected]ee845122011-09-01 08:44:16251 // Returns true if this node is an editable text field of any kind.
252 bool IsEditableText() const;
[email protected]02747d4e2010-11-03 19:10:00253
dmazzoni8b43c602014-12-17 21:23:35254 // True if this is a web area, and its grandparent is a presentational iframe.
255 bool IsWebAreaForPresentationalIframe() const;
256
[email protected]aa50cea82010-11-05 23:02:38257 protected:
258 BrowserAccessibility();
259
[email protected]d96f3842014-04-21 18:07:29260 // The manager of this tree of accessibility objects.
[email protected]f27e81c2010-10-07 05:20:23261 BrowserAccessibilityManager* manager_;
262
[email protected]d96f3842014-04-21 18:07:29263 // The underlying node.
264 ui::AXNode* node_;
[email protected]f27e81c2010-10-07 05:20:23265
[email protected]99d408f2013-12-04 07:19:43266 private:
[email protected]3dba3ed2013-12-17 21:24:01267 // Return the sum of the lengths of all static text descendants,
268 // including this object if it's static text.
269 int GetStaticTextLenRecursive() const;
270
dmazzonie80918022015-01-16 00:33:37271 // Similar to GetParent(), but includes nodes that are the host of a
272 // subtree rather than skipping over them - because they contain important
273 // bounds offsets.
274 BrowserAccessibility* GetParentForBoundsCalculation() const;
275
dmazzoni9df5f8fd2015-02-04 07:36:06276 // Convert the bounding rectangle of an element (which is relative to
277 // its nearest scrollable ancestor) to local bounds (which are relative
278 // to the top of the web accessibility tree).
279 gfx::Rect ElementBoundsToLocalBounds(gfx::Rect bounds) const;
280
[email protected]c5c2a672010-10-01 23:28:04281 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
282};
283
[email protected]e6b34872012-10-24 20:51:32284} // namespace content
285
[email protected]ba1fa652011-06-25 05:16:22286#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_