[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] | a52aa41 | 2011-12-14 19:03:30 | [diff] [blame^] | 58 | // Child dependent initialization can be done here. |
| 59 | virtual void PostInitialize() {} |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 60 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 61 | // Initialize this object, reading attributes from |src|. Does not |
| 62 | // recurse into children of |src| and build the whole subtree. |
[email protected] | a52aa41 | 2011-12-14 19:03:30 | [diff] [blame^] | 63 | void PreInitialize(BrowserAccessibilityManager* manager, |
| 64 | BrowserAccessibility* parent, |
| 65 | int32 child_id, |
| 66 | int32 index_in_parent, |
| 67 | const WebAccessibility& src); |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 68 | |
| 69 | // Add a child of this object. |
| 70 | void AddChild(BrowserAccessibility* child); |
| 71 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 72 | // 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] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 75 | // 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] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 79 | BrowserAccessibility* parent() { return parent_; } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 80 | |
| 81 | // Returns the number of children of this object. |
[email protected] | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 82 | uint32 child_count() const { return children_.size(); } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 83 | |
| 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] | 9052cca | 2011-11-30 23:59:31 | [diff] [blame] | 95 | // 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] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 98 | |
[email protected] | 9052cca | 2011-11-30 23:59:31 | [diff] [blame] | 99 | // 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] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 104 | BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point); |
| 105 | |
[email protected] | 72f3f55 | 2011-12-03 07:36:33 | [diff] [blame] | 106 | // Scrolls this element so that the |focus| rect (in local coordinates |
| 107 | // relative to this element) is scrolled to fit within the given |
| 108 | // |viewport| (which is clipped to the actual bounds of this |
| 109 | // object if you specify something larger). If the whole focus area |
| 110 | // doesn't fit, you can specify |subfocus|, which will prioritize |
| 111 | // a smaller area within |focus|. |
| 112 | // |
| 113 | // Note that "focus" doesn't necessarily mean the rectangle must correspond |
| 114 | // to a focused element on the page; assistive technology might request |
| 115 | // that any object be made visible. |
| 116 | void ScrollToMakeVisible(const gfx::Rect& subfocus, |
| 117 | const gfx::Rect& focus, |
| 118 | const gfx::Rect& viewport); |
| 119 | |
| 120 | // This is a 1-dimensional scroll offset helper function that's applied |
| 121 | // separately in the horizontal and vertical directions, because the |
| 122 | // logic is the same. The goal is to compute the best scroll offset |
| 123 | // in order to make a focused item visible within a viewport. |
| 124 | // |
| 125 | // In case the whole focused item cannot fit, you can specify a |
| 126 | // subfocus - a smaller region within the focus that should |
| 127 | // be prioritized. If the whole focused item can fit, the subfocus is |
| 128 | // ignored. |
| 129 | // |
| 130 | // Example: the viewport is scrolled to the right just enough |
| 131 | // that the focus is in view. |
| 132 | // Before: |
| 133 | // +----------Viewport---------+ |
| 134 | // +----Focus---+ |
| 135 | // +--SubFocus--+ |
| 136 | // |
| 137 | // After: |
| 138 | // +----------Viewport---------+ |
| 139 | // +----Focus---+ |
| 140 | // +--SubFocus--+ |
| 141 | // |
| 142 | // When constraints cannot be fully satisfied, the min |
| 143 | // (left/top) position takes precedence over the max (right/bottom). |
| 144 | // |
| 145 | // Note that the return value represents the ideal new scroll offset. |
| 146 | // This may be out of range - the calling function should clip this |
| 147 | // to the available range. |
| 148 | static int ComputeBestScrollOffset(int current_scroll_offset, |
| 149 | int subfocus_min, int subfocus_max, |
| 150 | int focus_min, int focus_max, |
| 151 | int viewport_min, int viewport_max); |
| 152 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 153 | // |
| 154 | // Reference counting |
| 155 | // |
| 156 | // Each object has an internal reference count and many platform |
| 157 | // implementations may also use native reference counting. |
| 158 | // |
| 159 | // The internal reference counting is used because sometimes |
| 160 | // multiple references to the same object exist temporarily during |
| 161 | // an update. When the internal reference count reaches zero, |
| 162 | // NativeReleaseReference is called. |
| 163 | // |
| 164 | // Native reference counting is used on some platforms because the |
| 165 | // operating system may hold onto a reference to a BrowserAccessibility |
| 166 | // object even after we're through with it. On these platforms, when |
| 167 | // the internal reference count reaches zero, instance_active is set |
| 168 | // to zero, and all queries on this object should return failure. |
| 169 | // The object isn't actually deleted until the operating system releases |
| 170 | // all of its references. |
| 171 | // |
| 172 | |
| 173 | // Increment this node's internal reference count. |
| 174 | virtual void InternalAddReference(); |
| 175 | |
| 176 | // Decrement this node's internal reference count. If the reference count |
| 177 | // reaches zero, call NativeReleaseReference(). |
| 178 | virtual void InternalReleaseReference(bool recursive); |
| 179 | |
| 180 | // Subclasses should override this to support platform reference counting. |
| 181 | virtual void NativeAddReference() { } |
| 182 | |
| 183 | // Subclasses should override this to support platform reference counting. |
[email protected] | 402da9f | 2011-03-08 19:45:41 | [diff] [blame] | 184 | virtual void NativeReleaseReference(); |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 185 | |
| 186 | // |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 187 | // Accessors |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 188 | // |
| 189 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 190 | const BoolAttrMap& bool_attributes() const { |
| 191 | return bool_attributes_; |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 194 | const FloatAttrMap& float_attributes() const { |
| 195 | return float_attributes_; |
| 196 | } |
| 197 | |
| 198 | const IntAttrMap& int_attributes() const { |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 199 | return int_attributes_; |
| 200 | } |
| 201 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 202 | const StringAttrMap& string_attributes() const { |
| 203 | return string_attributes_; |
| 204 | } |
| 205 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 206 | int32 child_id() const { return child_id_; } |
| 207 | const std::vector<BrowserAccessibility*>& children() const { |
| 208 | return children_; |
| 209 | } |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 210 | const std::vector<std::pair<string16, string16> >& html_attributes() const { |
| 211 | return html_attributes_; |
| 212 | } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 213 | int32 index_in_parent() const { return index_in_parent_; } |
[email protected] | 457af8d | 2011-05-14 01:02:47 | [diff] [blame] | 214 | const std::vector<int32>& indirect_child_ids() const { |
| 215 | return indirect_child_ids_; |
| 216 | } |
[email protected] | 80d64753 | 2011-06-17 22:53:49 | [diff] [blame] | 217 | const std::vector<int32>& line_breaks() const { |
| 218 | return line_breaks_; |
| 219 | } |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 220 | const std::vector<int32>& cell_ids() const { |
| 221 | return cell_ids_; |
| 222 | } |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 223 | const std::vector<int32>& unique_cell_ids() const { |
| 224 | return unique_cell_ids_; |
| 225 | } |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 226 | gfx::Rect location() const { return location_; } |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 227 | BrowserAccessibilityManager* manager() const { return manager_; } |
| 228 | const string16& name() const { return name_; } |
| 229 | int32 renderer_id() const { return renderer_id_; } |
| 230 | int32 role() const { return role_; } |
| 231 | const string16& role_name() const { return role_name_; } |
| 232 | int32 state() const { return state_; } |
| 233 | const string16& value() const { return value_; } |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 234 | bool instance_active() const { return instance_active_; } |
| 235 | int32 ref_count() const { return ref_count_; } |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 236 | |
[email protected] | 0d7dad6 | 2010-10-19 21:18:50 | [diff] [blame] | 237 | #if defined(OS_MACOSX) && __OBJC__ |
| 238 | BrowserAccessibilityCocoa* toBrowserAccessibilityCocoa(); |
[email protected] | 1dbadbd | 2010-10-13 18:50:10 | [diff] [blame] | 239 | #elif defined(OS_WIN) |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 240 | BrowserAccessibilityWin* toBrowserAccessibilityWin(); |
| 241 | #endif |
| 242 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 243 | // Retrieve the value of a bool attribute from the bool attribute |
| 244 | // map and returns true if found. |
| 245 | bool GetBoolAttribute(WebAccessibility::BoolAttribute attr, bool* value) |
| 246 | const; |
| 247 | |
| 248 | // Retrieve the value of a float attribute from the float attribute |
| 249 | // map and returns true if found. |
| 250 | bool GetFloatAttribute(WebAccessibility::FloatAttribute attr, float* value) |
| 251 | const; |
[email protected] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 252 | |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 253 | // Retrieve the value of an integer attribute from the integer attribute |
| 254 | // map and returns true if found. |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 255 | bool GetIntAttribute(WebAccessibility::IntAttribute attribute, int* value) |
| 256 | const; |
| 257 | |
| 258 | // Retrieve the value of a string attribute from the attribute map and |
| 259 | // returns true if found. |
| 260 | bool GetStringAttribute(WebAccessibility::StringAttribute attribute, |
| 261 | string16* value) const; |
| 262 | |
| 263 | // Retrieve the value of a html attribute from the attribute map and |
| 264 | // returns true if found. |
| 265 | bool GetHtmlAttribute(const char* attr, string16* value) const; |
| 266 | |
[email protected] | c43b0dc | 2011-12-03 04:31:13 | [diff] [blame] | 267 | // Returns true if the bit corresponding to the given state enum is 1. |
| 268 | bool HasState(WebAccessibility::State state_enum) const; |
| 269 | |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 270 | // Returns true if this node is an editable text field of any kind. |
| 271 | bool IsEditableText() const; |
[email protected] | 02747d4e | 2010-11-03 19:10:00 | [diff] [blame] | 272 | |
[email protected] | d1029c5 | 2011-10-21 21:59:51 | [diff] [blame] | 273 | // Append the text from this node and its children. |
| 274 | string16 GetTextRecursive() const; |
| 275 | |
[email protected] | aa50cea8 | 2010-11-05 23:02:38 | [diff] [blame] | 276 | protected: |
[email protected] | a52aa41 | 2011-12-14 19:03:30 | [diff] [blame^] | 277 | // Perform platform specific initialization. This can be called multiple times |
| 278 | // during the lifetime of this instance after the members of this base object |
| 279 | // have been reset with new values from the renderer process. |
| 280 | // Perform child independent initialization in this method. |
| 281 | virtual void PreInitialize(); |
| 282 | |
[email protected] | aa50cea8 | 2010-11-05 23:02:38 | [diff] [blame] | 283 | BrowserAccessibility(); |
| 284 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 285 | // The manager of this tree of accessibility objects; needed for |
| 286 | // global operations like focus tracking. |
| 287 | BrowserAccessibilityManager* manager_; |
| 288 | |
| 289 | // The parent of this object, may be NULL if we're the root object. |
| 290 | BrowserAccessibility* parent_; |
| 291 | |
| 292 | // The ID of this object; globally unique within the browser process. |
| 293 | int32 child_id_; |
| 294 | |
| 295 | // The index of this within its parent object. |
| 296 | int32 index_in_parent_; |
| 297 | |
| 298 | // The ID of this object in the renderer process. |
| 299 | int32 renderer_id_; |
| 300 | |
| 301 | // The children of this object. |
| 302 | std::vector<BrowserAccessibility*> children_; |
| 303 | |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 304 | // The number of internal references to this object. |
| 305 | int32 ref_count_; |
| 306 | |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 307 | // Accessibility metadata from the renderer |
| 308 | string16 name_; |
| 309 | string16 value_; |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 310 | BoolAttrMap bool_attributes_; |
| 311 | IntAttrMap int_attributes_; |
| 312 | FloatAttrMap float_attributes_; |
| 313 | StringAttrMap string_attributes_; |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 314 | std::vector<std::pair<string16, string16> > html_attributes_; |
| 315 | int32 role_; |
| 316 | int32 state_; |
| 317 | string16 role_name_; |
[email protected] | 0aed2f5 | 2011-03-23 18:06:36 | [diff] [blame] | 318 | gfx::Rect location_; |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 319 | std::vector<int32> indirect_child_ids_; |
[email protected] | be4eeb0b | 2011-06-15 22:15:06 | [diff] [blame] | 320 | std::vector<int32> line_breaks_; |
[email protected] | 1b12b23 | 2011-07-20 21:16:34 | [diff] [blame] | 321 | std::vector<int32> cell_ids_; |
[email protected] | ee84512 | 2011-09-01 08:44:16 | [diff] [blame] | 322 | std::vector<int32> unique_cell_ids_; |
[email protected] | 8368e3c | 2011-03-08 19:26:24 | [diff] [blame] | 323 | |
| 324 | // BrowserAccessibility objects are reference-counted on some platforms. |
| 325 | // When we're done with this object and it's removed from our accessibility |
| 326 | // tree, a client may still be holding onto a pointer to this object, so |
| 327 | // we mark it as inactive so that calls to any of this object's methods |
| 328 | // immediately return failure. |
| 329 | bool instance_active_; |
[email protected] | f27e81c | 2010-10-07 05:20:23 | [diff] [blame] | 330 | |
[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame] | 331 | private: |
| 332 | DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility); |
| 333 | }; |
| 334 | |
[email protected] | ba1fa65 | 2011-06-25 05:16:22 | [diff] [blame] | 335 | #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ |