blob: 9788b4e326cf8916fe8dec3d6d886961edeef01c [file] [log] [blame]
[email protected]9c1a75a2011-03-10 02:38:121// Copyright (c) 2011 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
5#ifndef CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_
6#define CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_
7#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"
15#include "webkit/glue/webaccessibility.h"
16
17class BrowserAccessibilityManager;
[email protected]0d7dad62010-10-19 21:18:5018#if defined(OS_MACOSX) && __OBJC__
19@class BrowserAccessibilityCocoa;
[email protected]1dbadbd2010-10-13 18:50:1020#elif defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:2321class BrowserAccessibilityWin;
22#endif
23
24using webkit_glue::WebAccessibility;
[email protected]c5c2a672010-10-01 23:28:0425
26////////////////////////////////////////////////////////////////////////////////
27//
28// BrowserAccessibility
29//
30// Class implementing the cross platform interface for the Browser-Renderer
31// communication of accessibility information, providing accessibility
32// to be used by screen readers and other assistive technology (AT).
33//
34// An implementation for each platform handles platform specific accessibility
35// APIs.
36//
37////////////////////////////////////////////////////////////////////////////////
38class BrowserAccessibility {
39 public:
[email protected]f27e81c2010-10-07 05:20:2340 // Creates a platform specific BrowserAccessibility. Ownership passes to the
[email protected]c5c2a672010-10-01 23:28:0441 // caller.
[email protected]f27e81c2010-10-07 05:20:2342 static BrowserAccessibility* Create();
43
[email protected]c5c2a672010-10-01 23:28:0444 virtual ~BrowserAccessibility();
45
[email protected]f27e81c2010-10-07 05:20:2346 // Perform platform specific initialization. This can be called multiple times
47 // during the lifetime of this instance after the members of this base object
48 // have been reset with new values from the renderer process.
[email protected]8368e3c2011-03-08 19:26:2449 virtual void Initialize();
[email protected]f27e81c2010-10-07 05:20:2350
[email protected]8368e3c2011-03-08 19:26:2451 // Initialize this object, reading attributes from |src|. Does not
52 // recurse into children of |src| and build the whole subtree.
[email protected]f27e81c2010-10-07 05:20:2353 void Initialize(BrowserAccessibilityManager* manager,
54 BrowserAccessibility* parent,
55 int32 child_id,
56 int32 index_in_parent,
57 const WebAccessibility& src);
58
59 // Add a child of this object.
60 void AddChild(BrowserAccessibility* child);
61
[email protected]8368e3c2011-03-08 19:26:2462 // Detach all descendants of this subtree and push all of the node pointers,
63 // including this node, onto the end of |nodes|.
64 void DetachTree(std::vector<BrowserAccessibility*>* nodes);
65
66 // Update the parent and index in parent if this node has been moved.
67 void UpdateParent(BrowserAccessibility* parent, int index_in_parent);
68
[email protected]f27e81c2010-10-07 05:20:2369 // Return true if this object is equal to or a descendant of |ancestor|.
70 bool IsDescendantOf(BrowserAccessibility* ancestor);
71
72 // Returns the parent of this object, or NULL if it's the root.
[email protected]9c1a75a2011-03-10 02:38:1273 BrowserAccessibility* parent() { return parent_; }
[email protected]f27e81c2010-10-07 05:20:2374
75 // Returns the number of children of this object.
[email protected]9c1a75a2011-03-10 02:38:1276 uint32 child_count() const { return children_.size(); }
[email protected]f27e81c2010-10-07 05:20:2377
78 // Return a pointer to the child with the given index.
79 BrowserAccessibility* GetChild(uint32 child_index);
80
81 // Return the previous sibling of this object, or NULL if it's the first
82 // child of its parent.
83 BrowserAccessibility* GetPreviousSibling();
84
85 // Return the next sibling of this object, or NULL if it's the last child
86 // of its parent.
87 BrowserAccessibility* GetNextSibling();
88
[email protected]02747d4e2010-11-03 19:10:0089 // Returns the bounds of this object in screen coordinates.
90 gfx::Rect GetBoundsRect();
91
92 // Returns the deepest descendant that contains the specified point.
93 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point);
94
[email protected]8368e3c2011-03-08 19:26:2495 //
96 // Reference counting
97 //
98 // Each object has an internal reference count and many platform
99 // implementations may also use native reference counting.
100 //
101 // The internal reference counting is used because sometimes
102 // multiple references to the same object exist temporarily during
103 // an update. When the internal reference count reaches zero,
104 // NativeReleaseReference is called.
105 //
106 // Native reference counting is used on some platforms because the
107 // operating system may hold onto a reference to a BrowserAccessibility
108 // object even after we're through with it. On these platforms, when
109 // the internal reference count reaches zero, instance_active is set
110 // to zero, and all queries on this object should return failure.
111 // The object isn't actually deleted until the operating system releases
112 // all of its references.
113 //
114
115 // Increment this node's internal reference count.
116 virtual void InternalAddReference();
117
118 // Decrement this node's internal reference count. If the reference count
119 // reaches zero, call NativeReleaseReference().
120 virtual void InternalReleaseReference(bool recursive);
121
122 // Subclasses should override this to support platform reference counting.
123 virtual void NativeAddReference() { }
124
125 // Subclasses should override this to support platform reference counting.
[email protected]402da9f2011-03-08 19:45:41126 virtual void NativeReleaseReference();
[email protected]8368e3c2011-03-08 19:26:24127
128 //
[email protected]f27e81c2010-10-07 05:20:23129 // Accessors
[email protected]8368e3c2011-03-08 19:26:24130 //
131
[email protected]0d7dad62010-10-19 21:18:50132 const std::map<int32, string16>& attributes() const { return attributes_; }
[email protected]f27e81c2010-10-07 05:20:23133 int32 child_id() const { return child_id_; }
134 const std::vector<BrowserAccessibility*>& children() const {
135 return children_;
136 }
[email protected]0d7dad62010-10-19 21:18:50137 const std::vector<std::pair<string16, string16> >& html_attributes() const {
138 return html_attributes_;
139 }
[email protected]f27e81c2010-10-07 05:20:23140 int32 index_in_parent() const { return index_in_parent_; }
[email protected]457af8d2011-05-14 01:02:47141 const std::vector<int32>& indirect_child_ids() const {
142 return indirect_child_ids_;
143 }
[email protected]0aed2f52011-03-23 18:06:36144 gfx::Rect location() const { return location_; }
[email protected]0d7dad62010-10-19 21:18:50145 BrowserAccessibilityManager* manager() const { return manager_; }
146 const string16& name() const { return name_; }
147 int32 renderer_id() const { return renderer_id_; }
148 int32 role() const { return role_; }
149 const string16& role_name() const { return role_name_; }
150 int32 state() const { return state_; }
151 const string16& value() const { return value_; }
[email protected]8368e3c2011-03-08 19:26:24152 bool instance_active() const { return instance_active_; }
153 int32 ref_count() const { return ref_count_; }
[email protected]f27e81c2010-10-07 05:20:23154
[email protected]0d7dad62010-10-19 21:18:50155#if defined(OS_MACOSX) && __OBJC__
156 BrowserAccessibilityCocoa* toBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10157#elif defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:23158 BrowserAccessibilityWin* toBrowserAccessibilityWin();
159#endif
160
[email protected]aa50cea82010-11-05 23:02:38161 // BrowserAccessibilityCocoa needs access to these methods.
[email protected]02747d4e2010-11-03 19:10:00162 // Return true if this attribute is in the attributes map.
163 bool HasAttribute(WebAccessibility::Attribute attribute);
164
165 // Retrieve the string value of an attribute from the attribute map and
166 // returns true if found.
167 bool GetAttribute(WebAccessibility::Attribute attribute, string16* value);
168
169 // Retrieve the value of an attribute from the attribute map and
170 // if found and nonempty, try to convert it to an integer.
171 // Returns true only if both the attribute was found and it was successfully
172 // converted to an integer.
173 bool GetAttributeAsInt(
174 WebAccessibility::Attribute attribute, int* value_int);
175
[email protected]aa50cea82010-11-05 23:02:38176 protected:
177 BrowserAccessibility();
178
[email protected]f27e81c2010-10-07 05:20:23179 // The manager of this tree of accessibility objects; needed for
180 // global operations like focus tracking.
181 BrowserAccessibilityManager* manager_;
182
183 // The parent of this object, may be NULL if we're the root object.
184 BrowserAccessibility* parent_;
185
186 // The ID of this object; globally unique within the browser process.
187 int32 child_id_;
188
189 // The index of this within its parent object.
190 int32 index_in_parent_;
191
192 // The ID of this object in the renderer process.
193 int32 renderer_id_;
194
195 // The children of this object.
196 std::vector<BrowserAccessibility*> children_;
197
[email protected]8368e3c2011-03-08 19:26:24198 // The number of internal references to this object.
199 int32 ref_count_;
200
[email protected]f27e81c2010-10-07 05:20:23201 // Accessibility metadata from the renderer
202 string16 name_;
203 string16 value_;
204 std::map<int32, string16> attributes_;
205 std::vector<std::pair<string16, string16> > html_attributes_;
206 int32 role_;
207 int32 state_;
208 string16 role_name_;
[email protected]0aed2f52011-03-23 18:06:36209 gfx::Rect location_;
[email protected]8368e3c2011-03-08 19:26:24210 std::vector<int32> indirect_child_ids_;
211
212 // BrowserAccessibility objects are reference-counted on some platforms.
213 // When we're done with this object and it's removed from our accessibility
214 // tree, a client may still be holding onto a pointer to this object, so
215 // we mark it as inactive so that calls to any of this object's methods
216 // immediately return failure.
217 bool instance_active_;
[email protected]f27e81c2010-10-07 05:20:23218
[email protected]c5c2a672010-10-01 23:28:04219 private:
220 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
221};
222
223#endif // CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_