blob: 3234372bc301f38506ad9d6b0f178acb3bf762a8 [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]b05cd542011-06-08 01:38:0246 // Detach all descendants of this subtree and push all of the node pointers,
47 // including this node, onto the end of |nodes|.
48 virtual void DetachTree(std::vector<BrowserAccessibility*>* nodes);
49
[email protected]f27e81c2010-10-07 05:20:2350 // Perform platform specific initialization. This can be called multiple times
51 // during the lifetime of this instance after the members of this base object
52 // have been reset with new values from the renderer process.
[email protected]8368e3c2011-03-08 19:26:2453 virtual void Initialize();
[email protected]f27e81c2010-10-07 05:20:2354
[email protected]8368e3c2011-03-08 19:26:2455 // Initialize this object, reading attributes from |src|. Does not
56 // recurse into children of |src| and build the whole subtree.
[email protected]f27e81c2010-10-07 05:20:2357 void Initialize(BrowserAccessibilityManager* manager,
58 BrowserAccessibility* parent,
59 int32 child_id,
60 int32 index_in_parent,
61 const WebAccessibility& src);
62
63 // Add a child of this object.
64 void AddChild(BrowserAccessibility* child);
65
[email protected]8368e3c2011-03-08 19:26:2466 // 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]80d647532011-06-17 22:53:49144 const std::vector<int32>& line_breaks() const {
145 return line_breaks_;
146 }
[email protected]0aed2f52011-03-23 18:06:36147 gfx::Rect location() const { return location_; }
[email protected]0d7dad62010-10-19 21:18:50148 BrowserAccessibilityManager* manager() const { return manager_; }
149 const string16& name() const { return name_; }
150 int32 renderer_id() const { return renderer_id_; }
151 int32 role() const { return role_; }
152 const string16& role_name() const { return role_name_; }
153 int32 state() const { return state_; }
154 const string16& value() const { return value_; }
[email protected]8368e3c2011-03-08 19:26:24155 bool instance_active() const { return instance_active_; }
156 int32 ref_count() const { return ref_count_; }
[email protected]f27e81c2010-10-07 05:20:23157
[email protected]0d7dad62010-10-19 21:18:50158#if defined(OS_MACOSX) && __OBJC__
159 BrowserAccessibilityCocoa* toBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10160#elif defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:23161 BrowserAccessibilityWin* toBrowserAccessibilityWin();
162#endif
163
[email protected]aa50cea82010-11-05 23:02:38164 // BrowserAccessibilityCocoa needs access to these methods.
[email protected]02747d4e2010-11-03 19:10:00165 // Return true if this attribute is in the attributes map.
166 bool HasAttribute(WebAccessibility::Attribute attribute);
167
168 // Retrieve the string value of an attribute from the attribute map and
169 // returns true if found.
170 bool GetAttribute(WebAccessibility::Attribute attribute, string16* value);
171
172 // Retrieve the value of an attribute from the attribute map and
173 // if found and nonempty, try to convert it to an integer.
174 // Returns true only if both the attribute was found and it was successfully
175 // converted to an integer.
176 bool GetAttributeAsInt(
177 WebAccessibility::Attribute attribute, int* value_int);
178
[email protected]aa50cea82010-11-05 23:02:38179 protected:
180 BrowserAccessibility();
181
[email protected]f27e81c2010-10-07 05:20:23182 // The manager of this tree of accessibility objects; needed for
183 // global operations like focus tracking.
184 BrowserAccessibilityManager* manager_;
185
186 // The parent of this object, may be NULL if we're the root object.
187 BrowserAccessibility* parent_;
188
189 // The ID of this object; globally unique within the browser process.
190 int32 child_id_;
191
192 // The index of this within its parent object.
193 int32 index_in_parent_;
194
195 // The ID of this object in the renderer process.
196 int32 renderer_id_;
197
198 // The children of this object.
199 std::vector<BrowserAccessibility*> children_;
200
[email protected]8368e3c2011-03-08 19:26:24201 // The number of internal references to this object.
202 int32 ref_count_;
203
[email protected]f27e81c2010-10-07 05:20:23204 // Accessibility metadata from the renderer
205 string16 name_;
206 string16 value_;
207 std::map<int32, string16> attributes_;
208 std::vector<std::pair<string16, string16> > html_attributes_;
209 int32 role_;
210 int32 state_;
211 string16 role_name_;
[email protected]0aed2f52011-03-23 18:06:36212 gfx::Rect location_;
[email protected]8368e3c2011-03-08 19:26:24213 std::vector<int32> indirect_child_ids_;
[email protected]be4eeb0b2011-06-15 22:15:06214 std::vector<int32> line_breaks_;
[email protected]8368e3c2011-03-08 19:26:24215
216 // BrowserAccessibility objects are reference-counted on some platforms.
217 // When we're done with this object and it's removed from our accessibility
218 // tree, a client may still be holding onto a pointer to this object, so
219 // we mark it as inactive so that calls to any of this object's methods
220 // immediately return failure.
221 bool instance_active_;
[email protected]f27e81c2010-10-07 05:20:23222
[email protected]c5c2a672010-10-01 23:28:04223 private:
224 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
225};
226
227#endif // CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_