blob: d0317f37d75df6bbcc4b73ffc1f2e4d99627e71c [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#pragma once
8
[email protected]f27e81c2010-10-07 05:20:239#include <map>
10#include <utility>
11#include <vector>
12
[email protected]c5c2a672010-10-01 23:28:0413#include "base/basictypes.h"
[email protected]f27e81c2010-10-07 05:20:2314#include "build/build_config.h"
[email protected]f3112a52011-09-30 23:47:4915#include "content/common/content_export.h"
[email protected]f27e81c2010-10-07 05:20:2316#include "webkit/glue/webaccessibility.h"
17
18class BrowserAccessibilityManager;
[email protected]0d7dad62010-10-19 21:18:5019#if defined(OS_MACOSX) && __OBJC__
20@class BrowserAccessibilityCocoa;
[email protected]1dbadbd2010-10-13 18:50:1021#elif defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:2322class BrowserAccessibilityWin;
23#endif
24
25using webkit_glue::WebAccessibility;
[email protected]ee845122011-09-01 08:44:1626typedef std::map<WebAccessibility::BoolAttribute, bool> BoolAttrMap;
27typedef std::map<WebAccessibility::FloatAttribute, float> FloatAttrMap;
28typedef std::map<WebAccessibility::IntAttribute, int> IntAttrMap;
29typedef std::map<WebAccessibility::StringAttribute, string16> StringAttrMap;
[email protected]c5c2a672010-10-01 23:28:0430
31////////////////////////////////////////////////////////////////////////////////
32//
33// BrowserAccessibility
34//
35// Class implementing the cross platform interface for the Browser-Renderer
36// communication of accessibility information, providing accessibility
37// to be used by screen readers and other assistive technology (AT).
38//
39// An implementation for each platform handles platform specific accessibility
40// APIs.
41//
42////////////////////////////////////////////////////////////////////////////////
[email protected]f3112a52011-09-30 23:47:4943class CONTENT_EXPORT BrowserAccessibility {
[email protected]c5c2a672010-10-01 23:28:0444 public:
[email protected]f27e81c2010-10-07 05:20:2345 // Creates a platform specific BrowserAccessibility. Ownership passes to the
[email protected]c5c2a672010-10-01 23:28:0446 // caller.
[email protected]f27e81c2010-10-07 05:20:2347 static BrowserAccessibility* Create();
48
[email protected]c5c2a672010-10-01 23:28:0449 virtual ~BrowserAccessibility();
50
[email protected]b05cd542011-06-08 01:38:0251 // Detach all descendants of this subtree and push all of the node pointers,
52 // including this node, onto the end of |nodes|.
53 virtual void DetachTree(std::vector<BrowserAccessibility*>* nodes);
54
[email protected]f27e81c2010-10-07 05:20:2355 // Perform platform specific initialization. This can be called multiple times
56 // during the lifetime of this instance after the members of this base object
57 // have been reset with new values from the renderer process.
[email protected]a52aa412011-12-14 19:03:3058 // Child dependent initialization can be done here.
59 virtual void PostInitialize() {}
[email protected]ee845122011-09-01 08:44:1660
[email protected]8368e3c2011-03-08 19:26:2461 // Initialize this object, reading attributes from |src|. Does not
62 // recurse into children of |src| and build the whole subtree.
[email protected]a52aa412011-12-14 19:03:3063 void PreInitialize(BrowserAccessibilityManager* manager,
64 BrowserAccessibility* parent,
65 int32 child_id,
66 int32 index_in_parent,
67 const WebAccessibility& src);
[email protected]f27e81c2010-10-07 05:20:2368
69 // Add a child of this object.
70 void AddChild(BrowserAccessibility* child);
71
[email protected]8368e3c2011-03-08 19:26:2472 // Update the parent and index in parent if this node has been moved.
73 void UpdateParent(BrowserAccessibility* parent, int index_in_parent);
74
[email protected]f27e81c2010-10-07 05:20:2375 // Return true if this object is equal to or a descendant of |ancestor|.
76 bool IsDescendantOf(BrowserAccessibility* ancestor);
77
78 // Returns the parent of this object, or NULL if it's the root.
[email protected]9c1a75a2011-03-10 02:38:1279 BrowserAccessibility* parent() { return parent_; }
[email protected]f27e81c2010-10-07 05:20:2380
81 // Returns the number of children of this object.
[email protected]9c1a75a2011-03-10 02:38:1282 uint32 child_count() const { return children_.size(); }
[email protected]f27e81c2010-10-07 05:20:2383
84 // Return a pointer to the child with the given index.
85 BrowserAccessibility* GetChild(uint32 child_index);
86
87 // Return the previous sibling of this object, or NULL if it's the first
88 // child of its parent.
89 BrowserAccessibility* GetPreviousSibling();
90
91 // Return the next sibling of this object, or NULL if it's the last child
92 // of its parent.
93 BrowserAccessibility* GetNextSibling();
94
[email protected]9052cca2011-11-30 23:59:3195 // Returns the bounds of this object in coordinates relative to the
96 // top-left corner of the overall web area.
97 gfx::Rect GetLocalBoundsRect();
[email protected]02747d4e2010-11-03 19:10:0098
[email protected]9052cca2011-11-30 23:59:3199 // Returns the bounds of this object in screen coordinates.
100 gfx::Rect GetGlobalBoundsRect();
101
102 // Returns the deepest descendant that contains the specified point
103 // (in global screen coordinates).
[email protected]02747d4e2010-11-03 19:10:00104 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point);
105
[email protected]8368e3c2011-03-08 19:26:24106 //
107 // Reference counting
108 //
109 // Each object has an internal reference count and many platform
110 // implementations may also use native reference counting.
111 //
112 // The internal reference counting is used because sometimes
113 // multiple references to the same object exist temporarily during
114 // an update. When the internal reference count reaches zero,
115 // NativeReleaseReference is called.
116 //
117 // Native reference counting is used on some platforms because the
118 // operating system may hold onto a reference to a BrowserAccessibility
119 // object even after we're through with it. On these platforms, when
120 // the internal reference count reaches zero, instance_active is set
121 // to zero, and all queries on this object should return failure.
122 // The object isn't actually deleted until the operating system releases
123 // all of its references.
124 //
125
126 // Increment this node's internal reference count.
127 virtual void InternalAddReference();
128
129 // Decrement this node's internal reference count. If the reference count
130 // reaches zero, call NativeReleaseReference().
131 virtual void InternalReleaseReference(bool recursive);
132
133 // Subclasses should override this to support platform reference counting.
134 virtual void NativeAddReference() { }
135
136 // Subclasses should override this to support platform reference counting.
[email protected]402da9f2011-03-08 19:45:41137 virtual void NativeReleaseReference();
[email protected]8368e3c2011-03-08 19:26:24138
139 //
[email protected]f27e81c2010-10-07 05:20:23140 // Accessors
[email protected]8368e3c2011-03-08 19:26:24141 //
142
[email protected]ee845122011-09-01 08:44:16143 const BoolAttrMap& bool_attributes() const {
144 return bool_attributes_;
[email protected]1b12b232011-07-20 21:16:34145 }
146
[email protected]ee845122011-09-01 08:44:16147 const FloatAttrMap& float_attributes() const {
148 return float_attributes_;
149 }
150
151 const IntAttrMap& int_attributes() const {
[email protected]1b12b232011-07-20 21:16:34152 return int_attributes_;
153 }
154
[email protected]ee845122011-09-01 08:44:16155 const StringAttrMap& string_attributes() const {
156 return string_attributes_;
157 }
158
[email protected]f27e81c2010-10-07 05:20:23159 int32 child_id() const { return child_id_; }
160 const std::vector<BrowserAccessibility*>& children() const {
161 return children_;
162 }
[email protected]0d7dad62010-10-19 21:18:50163 const std::vector<std::pair<string16, string16> >& html_attributes() const {
164 return html_attributes_;
165 }
[email protected]f27e81c2010-10-07 05:20:23166 int32 index_in_parent() const { return index_in_parent_; }
[email protected]457af8d2011-05-14 01:02:47167 const std::vector<int32>& indirect_child_ids() const {
168 return indirect_child_ids_;
169 }
[email protected]80d647532011-06-17 22:53:49170 const std::vector<int32>& line_breaks() const {
171 return line_breaks_;
172 }
[email protected]1b12b232011-07-20 21:16:34173 const std::vector<int32>& cell_ids() const {
174 return cell_ids_;
175 }
[email protected]ee845122011-09-01 08:44:16176 const std::vector<int32>& unique_cell_ids() const {
177 return unique_cell_ids_;
178 }
[email protected]0aed2f52011-03-23 18:06:36179 gfx::Rect location() const { return location_; }
[email protected]0d7dad62010-10-19 21:18:50180 BrowserAccessibilityManager* manager() const { return manager_; }
181 const string16& name() const { return name_; }
182 int32 renderer_id() const { return renderer_id_; }
183 int32 role() const { return role_; }
184 const string16& role_name() const { return role_name_; }
185 int32 state() const { return state_; }
186 const string16& value() const { return value_; }
[email protected]8368e3c2011-03-08 19:26:24187 bool instance_active() const { return instance_active_; }
188 int32 ref_count() const { return ref_count_; }
[email protected]f27e81c2010-10-07 05:20:23189
[email protected]0d7dad62010-10-19 21:18:50190#if defined(OS_MACOSX) && __OBJC__
191 BrowserAccessibilityCocoa* toBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10192#elif defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:23193 BrowserAccessibilityWin* toBrowserAccessibilityWin();
194#endif
195
[email protected]f506b602011-12-21 22:01:18196 // A string representation of this node.
197 // TODO(dtseng): Move to test only library.
198 std::string ToString();
199
[email protected]ee845122011-09-01 08:44:16200 // Retrieve the value of a bool attribute from the bool attribute
201 // map and returns true if found.
202 bool GetBoolAttribute(WebAccessibility::BoolAttribute attr, bool* value)
203 const;
204
205 // Retrieve the value of a float attribute from the float attribute
206 // map and returns true if found.
207 bool GetFloatAttribute(WebAccessibility::FloatAttribute attr, float* value)
208 const;
[email protected]02747d4e2010-11-03 19:10:00209
[email protected]1b12b232011-07-20 21:16:34210 // Retrieve the value of an integer attribute from the integer attribute
211 // map and returns true if found.
[email protected]ee845122011-09-01 08:44:16212 bool GetIntAttribute(WebAccessibility::IntAttribute attribute, int* value)
213 const;
214
215 // Retrieve the value of a string attribute from the attribute map and
216 // returns true if found.
217 bool GetStringAttribute(WebAccessibility::StringAttribute attribute,
218 string16* value) const;
219
220 // Retrieve the value of a html attribute from the attribute map and
221 // returns true if found.
222 bool GetHtmlAttribute(const char* attr, string16* value) const;
223
[email protected]c43b0dc2011-12-03 04:31:13224 // Returns true if the bit corresponding to the given state enum is 1.
225 bool HasState(WebAccessibility::State state_enum) const;
226
[email protected]ee845122011-09-01 08:44:16227 // Returns true if this node is an editable text field of any kind.
228 bool IsEditableText() const;
[email protected]02747d4e2010-11-03 19:10:00229
[email protected]d1029c52011-10-21 21:59:51230 // Append the text from this node and its children.
231 string16 GetTextRecursive() const;
232
[email protected]aa50cea82010-11-05 23:02:38233 protected:
[email protected]a52aa412011-12-14 19:03:30234 // Perform platform specific initialization. This can be called multiple times
235 // during the lifetime of this instance after the members of this base object
236 // have been reset with new values from the renderer process.
237 // Perform child independent initialization in this method.
238 virtual void PreInitialize();
239
[email protected]aa50cea82010-11-05 23:02:38240 BrowserAccessibility();
241
[email protected]f27e81c2010-10-07 05:20:23242 // The manager of this tree of accessibility objects; needed for
243 // global operations like focus tracking.
244 BrowserAccessibilityManager* manager_;
245
246 // The parent of this object, may be NULL if we're the root object.
247 BrowserAccessibility* parent_;
248
249 // The ID of this object; globally unique within the browser process.
250 int32 child_id_;
251
252 // The index of this within its parent object.
253 int32 index_in_parent_;
254
255 // The ID of this object in the renderer process.
256 int32 renderer_id_;
257
258 // The children of this object.
259 std::vector<BrowserAccessibility*> children_;
260
[email protected]8368e3c2011-03-08 19:26:24261 // The number of internal references to this object.
262 int32 ref_count_;
263
[email protected]f27e81c2010-10-07 05:20:23264 // Accessibility metadata from the renderer
265 string16 name_;
266 string16 value_;
[email protected]ee845122011-09-01 08:44:16267 BoolAttrMap bool_attributes_;
268 IntAttrMap int_attributes_;
269 FloatAttrMap float_attributes_;
270 StringAttrMap string_attributes_;
[email protected]f27e81c2010-10-07 05:20:23271 std::vector<std::pair<string16, string16> > html_attributes_;
272 int32 role_;
273 int32 state_;
274 string16 role_name_;
[email protected]0aed2f52011-03-23 18:06:36275 gfx::Rect location_;
[email protected]8368e3c2011-03-08 19:26:24276 std::vector<int32> indirect_child_ids_;
[email protected]be4eeb0b2011-06-15 22:15:06277 std::vector<int32> line_breaks_;
[email protected]1b12b232011-07-20 21:16:34278 std::vector<int32> cell_ids_;
[email protected]ee845122011-09-01 08:44:16279 std::vector<int32> unique_cell_ids_;
[email protected]8368e3c2011-03-08 19:26:24280
281 // BrowserAccessibility objects are reference-counted on some platforms.
282 // When we're done with this object and it's removed from our accessibility
283 // tree, a client may still be holding onto a pointer to this object, so
284 // we mark it as inactive so that calls to any of this object's methods
285 // immediately return failure.
286 bool instance_active_;
[email protected]f27e81c2010-10-07 05:20:23287
[email protected]c5c2a672010-10-01 23:28:04288 private:
289 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
290};
291
[email protected]ba1fa652011-06-25 05:16:22292#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_