[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 | |
[email protected] | ba1fa65 | 2011-06-25 05:16:22 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ |
| 6 | #define CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 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" |
[email protected] | f3112a5 | 2011-09-30 23:47:49 | [diff] [blame^] | 15 | #include "content/common/content_export.h" |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 16 | #include "webkit/glue/webaccessibility.h" |
| 17 | |
| 18 | class BrowserAccessibilityManager; |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 19 | #if defined(OS_MACOSX) && __OBJC__ |
| 20 | @class BrowserAccessibilityCocoa; |
[email protected] | 1dbadbd | 2010-10-13 18:50:10 | [diff] [blame] | 21 | #elif defined(OS_WIN) |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 22 | class BrowserAccessibilityWin; |
| 23 | #endif |
| 24 | |
| 25 | using webkit_glue::WebAccessibility; |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 26 | typedef std::map<WebAccessibility::BoolAttribute, bool> BoolAttrMap; |
| 27 | typedef std::map<WebAccessibility::FloatAttribute, float> FloatAttrMap; |
| 28 | typedef std::map<WebAccessibility::IntAttribute, int> IntAttrMap; |
| 29 | typedef std::map<WebAccessibility::StringAttribute, string16> StringAttrMap; |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 30 | |
| 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] | f3112a5 | 2011-09-30 23:47:49 | [diff] [blame^] | 43 | class CONTENT_EXPORT BrowserAccessibility { |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 44 | public: |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 45 | // Creates a platform specific BrowserAccessibility. Ownership passes to the |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 46 | // caller. |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 47 | static BrowserAccessibility* Create(); |
| 48 | |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 49 | virtual ~BrowserAccessibility(); |
| 50 | |
[email protected] | b05cd54 | 2011-06-08 01:38:02 | [diff] [blame] | 51 | // 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] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 55 | // 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] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 58 | virtual void Initialize(); |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 59 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 60 | // Optionally send events triggered simply by the fact that this node |
| 61 | // has been created or modified (and has been attached to the tree). |
| 62 | // This can include "show" events, "text changed" events in live regions, |
| 63 | // or "alert" events. |
| 64 | virtual void SendNodeUpdateEvents() {} |
| 65 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 66 | // Initialize this object, reading attributes from |src|. Does not |
| 67 | // recurse into children of |src| and build the whole subtree. |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 68 | void Initialize(BrowserAccessibilityManager* manager, |
| 69 | BrowserAccessibility* parent, |
| 70 | int32 child_id, |
| 71 | int32 index_in_parent, |
| 72 | const WebAccessibility& src); |
| 73 | |
| 74 | // Add a child of this object. |
| 75 | void AddChild(BrowserAccessibility* child); |
| 76 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 77 | // Update the parent and index in parent if this node has been moved. |
| 78 | void UpdateParent(BrowserAccessibility* parent, int index_in_parent); |
| 79 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 80 | // Return true if this object is equal to or a descendant of |ancestor|. |
| 81 | bool IsDescendantOf(BrowserAccessibility* ancestor); |
| 82 | |
| 83 | // Returns the parent of this object, or NULL if it's the root. |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 84 | BrowserAccessibility* parent() { return parent_; } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 85 | |
| 86 | // Returns the number of children of this object. |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 87 | uint32 child_count() const { return children_.size(); } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 88 | |
| 89 | // Return a pointer to the child with the given index. |
| 90 | BrowserAccessibility* GetChild(uint32 child_index); |
| 91 | |
| 92 | // Return the previous sibling of this object, or NULL if it's the first |
| 93 | // child of its parent. |
| 94 | BrowserAccessibility* GetPreviousSibling(); |
| 95 | |
| 96 | // Return the next sibling of this object, or NULL if it's the last child |
| 97 | // of its parent. |
| 98 | BrowserAccessibility* GetNextSibling(); |
| 99 | |
[email protected] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 100 | // Returns the bounds of this object in screen coordinates. |
| 101 | gfx::Rect GetBoundsRect(); |
| 102 | |
| 103 | // Returns the deepest descendant that contains the specified point. |
| 104 | BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point); |
| 105 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 106 | // |
| 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] | 402da9f | 2011-03-08 19:45:41 | [diff] [blame] | 137 | virtual void NativeReleaseReference(); |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 138 | |
| 139 | // |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 140 | // Accessors |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 141 | // |
| 142 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 143 | const BoolAttrMap& bool_attributes() const { |
| 144 | return bool_attributes_; |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 147 | const FloatAttrMap& float_attributes() const { |
| 148 | return float_attributes_; |
| 149 | } |
| 150 | |
| 151 | const IntAttrMap& int_attributes() const { |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 152 | return int_attributes_; |
| 153 | } |
| 154 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 155 | const StringAttrMap& string_attributes() const { |
| 156 | return string_attributes_; |
| 157 | } |
| 158 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 159 | int32 child_id() const { return child_id_; } |
| 160 | const std::vector<BrowserAccessibility*>& children() const { |
| 161 | return children_; |
| 162 | } |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 163 | const std::vector<std::pair<string16, string16> >& html_attributes() const { |
| 164 | return html_attributes_; |
| 165 | } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 166 | int32 index_in_parent() const { return index_in_parent_; } |
[email protected] | 457af8d | 2011-05-14 01:02:47 | [diff] [blame] | 167 | const std::vector<int32>& indirect_child_ids() const { |
| 168 | return indirect_child_ids_; |
| 169 | } |
[email protected] | 80d64753 | 2011-06-17 22:53:49 | [diff] [blame] | 170 | const std::vector<int32>& line_breaks() const { |
| 171 | return line_breaks_; |
| 172 | } |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 173 | const std::vector<int32>& cell_ids() const { |
| 174 | return cell_ids_; |
| 175 | } |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 176 | const std::vector<int32>& unique_cell_ids() const { |
| 177 | return unique_cell_ids_; |
| 178 | } |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 179 | gfx::Rect location() const { return location_; } |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 180 | 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] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 187 | bool instance_active() const { return instance_active_; } |
| 188 | int32 ref_count() const { return ref_count_; } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 189 | |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 190 | #if defined(OS_MACOSX) && __OBJC__ |
| 191 | BrowserAccessibilityCocoa* toBrowserAccessibilityCocoa(); |
[email protected] | 1dbadbd | 2010-10-13 18:50:10 | [diff] [blame] | 192 | #elif defined(OS_WIN) |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 193 | BrowserAccessibilityWin* toBrowserAccessibilityWin(); |
| 194 | #endif |
| 195 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 196 | // Retrieve the value of a bool attribute from the bool attribute |
| 197 | // map and returns true if found. |
| 198 | bool GetBoolAttribute(WebAccessibility::BoolAttribute attr, bool* value) |
| 199 | const; |
| 200 | |
| 201 | // Retrieve the value of a float attribute from the float attribute |
| 202 | // map and returns true if found. |
| 203 | bool GetFloatAttribute(WebAccessibility::FloatAttribute attr, float* value) |
| 204 | const; |
[email protected] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 205 | |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 206 | // Retrieve the value of an integer attribute from the integer attribute |
| 207 | // map and returns true if found. |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 208 | bool GetIntAttribute(WebAccessibility::IntAttribute attribute, int* value) |
| 209 | const; |
| 210 | |
| 211 | // Retrieve the value of a string attribute from the attribute map and |
| 212 | // returns true if found. |
| 213 | bool GetStringAttribute(WebAccessibility::StringAttribute attribute, |
| 214 | string16* value) const; |
| 215 | |
| 216 | // Retrieve the value of a html attribute from the attribute map and |
| 217 | // returns true if found. |
| 218 | bool GetHtmlAttribute(const char* attr, string16* value) const; |
| 219 | |
| 220 | // Returns true if this node is an editable text field of any kind. |
| 221 | bool IsEditableText() const; |
[email protected] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 222 | |
[email protected] | aa50cea8 | 2010-11-05 23:02:38 | [diff] [blame] | 223 | protected: |
| 224 | BrowserAccessibility(); |
| 225 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 226 | // The manager of this tree of accessibility objects; needed for |
| 227 | // global operations like focus tracking. |
| 228 | BrowserAccessibilityManager* manager_; |
| 229 | |
| 230 | // The parent of this object, may be NULL if we're the root object. |
| 231 | BrowserAccessibility* parent_; |
| 232 | |
| 233 | // The ID of this object; globally unique within the browser process. |
| 234 | int32 child_id_; |
| 235 | |
| 236 | // The index of this within its parent object. |
| 237 | int32 index_in_parent_; |
| 238 | |
| 239 | // The ID of this object in the renderer process. |
| 240 | int32 renderer_id_; |
| 241 | |
| 242 | // The children of this object. |
| 243 | std::vector<BrowserAccessibility*> children_; |
| 244 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 245 | // The number of internal references to this object. |
| 246 | int32 ref_count_; |
| 247 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 248 | // Accessibility metadata from the renderer |
| 249 | string16 name_; |
| 250 | string16 value_; |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 251 | BoolAttrMap bool_attributes_; |
| 252 | IntAttrMap int_attributes_; |
| 253 | FloatAttrMap float_attributes_; |
| 254 | StringAttrMap string_attributes_; |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 255 | std::vector<std::pair<string16, string16> > html_attributes_; |
| 256 | int32 role_; |
| 257 | int32 state_; |
| 258 | string16 role_name_; |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 259 | gfx::Rect location_; |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 260 | std::vector<int32> indirect_child_ids_; |
[email protected] | be4eeb0b | 2011-06-15 22:15:06 | [diff] [blame] | 261 | std::vector<int32> line_breaks_; |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 262 | std::vector<int32> cell_ids_; |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 263 | std::vector<int32> unique_cell_ids_; |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 264 | |
| 265 | // BrowserAccessibility objects are reference-counted on some platforms. |
| 266 | // When we're done with this object and it's removed from our accessibility |
| 267 | // tree, a client may still be holding onto a pointer to this object, so |
| 268 | // we mark it as inactive so that calls to any of this object's methods |
| 269 | // immediately return failure. |
| 270 | bool instance_active_; |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 271 | |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 272 | private: |
| 273 | DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility); |
| 274 | }; |
| 275 | |
[email protected] | ba1fa65 | 2011-06-25 05:16:22 | [diff] [blame] | 276 | #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ |