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