1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ 6 #define CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ 7 8 #include <map> 9 #include <utility> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "build/build_config.h" 14 #include "content/common/accessibility_node_data.h" 15 #include "content/common/content_export.h" 16 17 #if defined(OS_MACOSX) && __OBJC__ 18 @class BrowserAccessibilityCocoa; 19 #endif 20 21 namespace content { 22 class BrowserAccessibilityManager; 23 #if defined(OS_WIN) 24 class BrowserAccessibilityWin; 25 #elif defined(TOOLKIT_GTK) 26 class BrowserAccessibilityGtk; 27 #endif 28 29 typedef std::map<AccessibilityNodeData::BoolAttribute, bool> BoolAttrMap; 30 typedef std::map<AccessibilityNodeData::FloatAttribute, float> FloatAttrMap; 31 typedef std::map<AccessibilityNodeData::IntAttribute, int> IntAttrMap; 32 typedef std::map<AccessibilityNodeData::StringAttribute, string16> 33 StringAttrMap; 34 35 //////////////////////////////////////////////////////////////////////////////// 36 // 37 // BrowserAccessibility 38 // 39 // Class implementing the cross platform interface for the Browser-Renderer 40 // communication of accessibility information, providing accessibility 41 // to be used by screen readers and other assistive technology (AT). 42 // 43 // An implementation for each platform handles platform specific accessibility 44 // APIs. 45 // 46 //////////////////////////////////////////////////////////////////////////////// 47 class CONTENT_EXPORT BrowserAccessibility { 48 public: 49 // Creates a platform specific BrowserAccessibility. Ownership passes to the 50 // caller. 51 static BrowserAccessibility* Create(); 52 53 virtual ~BrowserAccessibility(); 54 55 // Detach all descendants of this subtree and push all of the node pointers, 56 // including this node, onto the end of |nodes|. 57 virtual void DetachTree(std::vector<BrowserAccessibility*>* nodes); 58 59 // Perform platform-specific initialization. This can be called multiple times 60 // during the lifetime of this instance after the members of this base object 61 // have been reset with new values from the renderer process. 62 // Child dependent initialization can be done here. 63 virtual void PostInitialize() {} 64 65 // Returns true if this is a native platform-specific object, vs a 66 // cross-platform generic object. 67 virtual bool IsNative() const; 68 69 // Initialize the tree structure of this object. 70 void InitializeTreeStructure( 71 BrowserAccessibilityManager* manager, 72 BrowserAccessibility* parent, 73 int32 renderer_id, 74 int32 index_in_parent); 75 76 // Initialize this object's data. 77 void InitializeData(const AccessibilityNodeData& src); 78 79 virtual void SwapChildren(std::vector<BrowserAccessibility*>& children); 80 81 // Update the parent and index in parent if this node has been moved. 82 void UpdateParent(BrowserAccessibility* parent, int index_in_parent); 83 84 // Update this node's location, leaving everything else the same. 85 virtual void SetLocation(const gfx::Rect& new_location); 86 87 // Return true if this object is equal to or a descendant of |ancestor|. 88 bool IsDescendantOf(BrowserAccessibility* ancestor); 89 90 // Returns the parent of this object, or NULL if it's the root. 91 BrowserAccessibility* parent() const { return parent_; } 92 93 // Returns the number of children of this object. 94 uint32 child_count() const { return children_.size(); } 95 96 // Return a pointer to the child with the given index. 97 BrowserAccessibility* GetChild(uint32 child_index) const; 98 99 // Return the previous sibling of this object, or NULL if it's the first 100 // child of its parent. 101 BrowserAccessibility* GetPreviousSibling(); 102 103 // Return the next sibling of this object, or NULL if it's the last child 104 // of its parent. 105 BrowserAccessibility* GetNextSibling(); 106 107 // Returns the bounds of this object in coordinates relative to the 108 // top-left corner of the overall web area. 109 gfx::Rect GetLocalBoundsRect() const; 110 111 // Returns the bounds of this object in screen coordinates. 112 gfx::Rect GetGlobalBoundsRect() const; 113 114 // Returns the deepest descendant that contains the specified point 115 // (in global screen coordinates). 116 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point); 117 118 // Marks this object for deletion, releases our reference to it, and 119 // recursively calls Destroy() on its children. May not delete 120 // immediately due to reference counting. 121 // 122 // Reference counting is used on some platforms because the 123 // operating system may hold onto a reference to a BrowserAccessibility 124 // object even after we're through with it. When a BrowserAccessibility 125 // has had Destroy() called but its reference count is not yet zero, 126 // queries on this object return failure 127 virtual void Destroy(); 128 129 // Subclasses should override this to support platform reference counting. 130 virtual void NativeAddReference() { } 131 132 // Subclasses should override this to support platform reference counting. 133 virtual void NativeReleaseReference(); 134 135 // 136 // Accessors 137 // 138 139 const BoolAttrMap& bool_attributes() const { 140 return bool_attributes_; 141 } 142 143 const FloatAttrMap& float_attributes() const { 144 return float_attributes_; 145 } 146 147 const IntAttrMap& int_attributes() const { 148 return int_attributes_; 149 } 150 151 const StringAttrMap& string_attributes() const { 152 return string_attributes_; 153 } 154 155 const std::vector<BrowserAccessibility*>& children() const { 156 return children_; 157 } 158 const std::vector<std::pair<string16, string16> >& html_attributes() const { 159 return html_attributes_; 160 } 161 int32 index_in_parent() const { return index_in_parent_; } 162 const std::vector<int32>& indirect_child_ids() const { 163 return indirect_child_ids_; 164 } 165 const std::vector<int32>& line_breaks() const { 166 return line_breaks_; 167 } 168 const std::vector<int32>& cell_ids() const { 169 return cell_ids_; 170 } 171 const std::vector<int32>& unique_cell_ids() const { 172 return unique_cell_ids_; 173 } 174 gfx::Rect location() const { return location_; } 175 BrowserAccessibilityManager* manager() const { return manager_; } 176 const string16& name() const { return name_; } 177 int32 renderer_id() const { return renderer_id_; } 178 int32 role() const { return role_; } 179 const string16& role_name() const { return role_name_; } 180 int32 state() const { return state_; } 181 const string16& value() const { return value_; } 182 bool instance_active() const { return instance_active_; } 183 184 #if defined(OS_MACOSX) && __OBJC__ 185 BrowserAccessibilityCocoa* ToBrowserAccessibilityCocoa(); 186 #elif defined(OS_WIN) 187 BrowserAccessibilityWin* ToBrowserAccessibilityWin(); 188 #elif defined(TOOLKIT_GTK) 189 BrowserAccessibilityGtk* ToBrowserAccessibilityGtk(); 190 #endif 191 192 // Retrieve the value of a bool attribute from the bool attribute 193 // map and returns true if found. 194 bool GetBoolAttribute( 195 AccessibilityNodeData::BoolAttribute attr, bool* value) const; 196 197 // Retrieve the value of a float attribute from the float attribute 198 // map and returns true if found. 199 bool GetFloatAttribute(AccessibilityNodeData::FloatAttribute attr, 200 float* value) const; 201 202 // Retrieve the value of an integer attribute from the integer attribute 203 // map and returns true if found. 204 bool GetIntAttribute(AccessibilityNodeData::IntAttribute attribute, 205 int* value) const; 206 207 // Retrieve the value of a string attribute from the attribute map and 208 // returns true if found. 209 bool GetStringAttribute( 210 AccessibilityNodeData::StringAttribute attribute, string16* value) const; 211 212 // Retrieve the value of a html attribute from the attribute map and 213 // returns true if found. 214 bool GetHtmlAttribute(const char* attr, string16* value) const; 215 216 // Utility method to handle special cases for ARIA booleans, tristates and 217 // booleans which have a "mixed" state. 218 // 219 // Warning: the term "Tristate" is used loosely by the spec and here, 220 // as some attributes support a 4th state. 221 // 222 // The following attributes are appropriate to use with this method: 223 // aria-selected (selectable) 224 // aria-grabbed (grabbable) 225 // aria-expanded (expandable) 226 // aria-pressed (toggleable/pressable) -- supports 4th "mixed" state 227 // aria-checked (checkable) -- supports 4th "mixed state" 228 bool GetAriaTristate(const char* attr_name, 229 bool* is_defined, 230 bool* is_mixed) const; 231 232 // Returns true if the bit corresponding to the given state enum is 1. 233 bool HasState(AccessibilityNodeData::State state_enum) const; 234 235 // Returns true if this node is an editable text field of any kind. 236 bool IsEditableText() const; 237 238 // Append the text from this node and its children. 239 string16 GetTextRecursive() const; 240 241 protected: 242 // Perform platform specific initialization. This can be called multiple times 243 // during the lifetime of this instance after the members of this base object 244 // have been reset with new values from the renderer process. 245 // Perform child independent initialization in this method. 246 virtual void PreInitialize() {} 247 248 BrowserAccessibility(); 249 250 // The manager of this tree of accessibility objects; needed for 251 // global operations like focus tracking. 252 BrowserAccessibilityManager* manager_; 253 254 // The parent of this object, may be NULL if we're the root object. 255 BrowserAccessibility* parent_; 256 257 // The index of this within its parent object. 258 int32 index_in_parent_; 259 260 // The ID of this object in the renderer process. 261 int32 renderer_id_; 262 263 // The children of this object. 264 std::vector<BrowserAccessibility*> children_; 265 266 // Accessibility metadata from the renderer 267 string16 name_; 268 string16 value_; 269 BoolAttrMap bool_attributes_; 270 IntAttrMap int_attributes_; 271 FloatAttrMap float_attributes_; 272 StringAttrMap string_attributes_; 273 std::vector<std::pair<string16, string16> > html_attributes_; 274 int32 role_; 275 int32 state_; 276 string16 role_name_; 277 gfx::Rect location_; 278 std::vector<int32> indirect_child_ids_; 279 std::vector<int32> line_breaks_; 280 std::vector<int32> cell_ids_; 281 std::vector<int32> unique_cell_ids_; 282 283 // BrowserAccessibility objects are reference-counted on some platforms. 284 // When we're done with this object and it's removed from our accessibility 285 // tree, a client may still be holding onto a pointer to this object, so 286 // we mark it as inactive so that calls to any of this object's methods 287 // immediately return failure. 288 bool instance_active_; 289 290 private: 291 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility); 292 }; 293 294 } // namespace content 295 296 #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_ 297