[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 2 | // 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] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 13 | #include "base/basictypes.h" |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 14 | #include "build/build_config.h" |
| 15 | #include "webkit/glue/webaccessibility.h" |
| 16 | |
| 17 | class BrowserAccessibilityManager; |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 18 | #if defined(OS_MACOSX) && __OBJC__ |
| 19 | @class BrowserAccessibilityCocoa; |
[email protected] | 1dbadbd | 2010-10-13 18:50:10 | [diff] [blame] | 20 | #elif defined(OS_WIN) |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 21 | class BrowserAccessibilityWin; |
| 22 | #endif |
| 23 | |
| 24 | using webkit_glue::WebAccessibility; |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 25 | |
| 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 | //////////////////////////////////////////////////////////////////////////////// |
| 38 | class BrowserAccessibility { |
| 39 | public: |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 40 | // Creates a platform specific BrowserAccessibility. Ownership passes to the |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 41 | // caller. |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 42 | static BrowserAccessibility* Create(); |
| 43 | |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 44 | virtual ~BrowserAccessibility(); |
| 45 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 46 | // 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] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 49 | virtual void Initialize(); |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 50 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 51 | // Initialize this object, reading attributes from |src|. Does not |
| 52 | // recurse into children of |src| and build the whole subtree. |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 53 | 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] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 62 | // 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] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 69 | // 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] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 73 | BrowserAccessibility* parent() { return parent_; } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 74 | |
| 75 | // Returns the number of children of this object. |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 76 | uint32 child_count() const { return children_.size(); } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 77 | |
| 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] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 89 | // 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] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 95 | // |
| 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] | 402da9f | 2011-03-08 19:45:41 | [diff] [blame] | 126 | virtual void NativeReleaseReference(); |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 127 | |
| 128 | // |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 129 | // Accessors |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 130 | // |
| 131 | |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 132 | const std::map<int32, string16>& attributes() const { return attributes_; } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 133 | int32 child_id() const { return child_id_; } |
| 134 | const std::vector<BrowserAccessibility*>& children() const { |
| 135 | return children_; |
| 136 | } |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 137 | const std::vector<std::pair<string16, string16> >& html_attributes() const { |
| 138 | return html_attributes_; |
| 139 | } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 140 | int32 index_in_parent() const { return index_in_parent_; } |
[email protected] | 457af8d | 2011-05-14 01:02:47 | [diff] [blame^] | 141 | const std::vector<int32>& indirect_child_ids() const { |
| 142 | return indirect_child_ids_; |
| 143 | } |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 144 | gfx::Rect location() const { return location_; } |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 145 | 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] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 152 | bool instance_active() const { return instance_active_; } |
| 153 | int32 ref_count() const { return ref_count_; } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 154 | |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 155 | #if defined(OS_MACOSX) && __OBJC__ |
| 156 | BrowserAccessibilityCocoa* toBrowserAccessibilityCocoa(); |
[email protected] | 1dbadbd | 2010-10-13 18:50:10 | [diff] [blame] | 157 | #elif defined(OS_WIN) |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 158 | BrowserAccessibilityWin* toBrowserAccessibilityWin(); |
| 159 | #endif |
| 160 | |
[email protected] | aa50cea8 | 2010-11-05 23:02:38 | [diff] [blame] | 161 | // BrowserAccessibilityCocoa needs access to these methods. |
[email protected] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 162 | // 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] | aa50cea8 | 2010-11-05 23:02:38 | [diff] [blame] | 176 | protected: |
| 177 | BrowserAccessibility(); |
| 178 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 179 | // 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] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 198 | // The number of internal references to this object. |
| 199 | int32 ref_count_; |
| 200 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 201 | // 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] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 209 | gfx::Rect location_; |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 210 | 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] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 218 | |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 219 | private: |
| 220 | DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility); |
| 221 | }; |
| 222 | |
| 223 | #endif // CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ |