OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/accessibility/platform/ax_platform_node_base.h" | 5 #include "ui/accessibility/platform/ax_platform_node_base.h" |
6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
7 #include "ui/accessibility/ax_node_data.h" | 8 #include "ui/accessibility/ax_node_data.h" |
8 #include "ui/accessibility/platform/ax_platform_node_delegate.h" | 9 #include "ui/accessibility/platform/ax_platform_node_delegate.h" |
9 | 10 |
10 namespace ui { | 11 namespace ui { |
11 | 12 |
12 AXPlatformNodeBase::AXPlatformNodeBase() { | 13 namespace { |
13 } | 14 |
14 | 15 // Predicate that returns true if the first value of a pair is |first|. |
15 AXPlatformNodeBase::~AXPlatformNodeBase() { | 16 template<typename FirstType, typename SecondType> |
16 } | 17 struct FirstIs { |
| 18 FirstIs(FirstType first) |
| 19 : first_(first) {} |
| 20 bool operator()(std::pair<FirstType, SecondType> const& p) { |
| 21 return p.first == first_; |
| 22 } |
| 23 FirstType first_; |
| 24 }; |
| 25 |
| 26 // Helper function that finds in a vector of pairs by matching on the |
| 27 // first value, and returns an iterator. |
| 28 template<typename FirstType, typename SecondType> |
| 29 typename std::vector<std::pair<FirstType, SecondType>>::const_iterator |
| 30 FindInVectorOfPairs( |
| 31 FirstType first, |
| 32 const std::vector<std::pair<FirstType, SecondType>>& vector) { |
| 33 return std::find_if(vector.begin(), |
| 34 vector.end(), |
| 35 FirstIs<FirstType, SecondType>(first)); |
| 36 } |
| 37 |
| 38 } // namespace |
17 | 39 |
18 void AXPlatformNodeBase::Init(AXPlatformNodeDelegate* delegate) { | 40 void AXPlatformNodeBase::Init(AXPlatformNodeDelegate* delegate) { |
19 delegate_ = delegate; | 41 delegate_ = delegate; |
20 } | 42 } |
21 | 43 |
22 AXRole AXPlatformNodeBase::GetRole() const { | 44 const AXNodeData& AXPlatformNodeBase::GetData() const { |
23 return delegate_ ? delegate_->GetData()->role : AX_ROLE_UNKNOWN; | 45 CHECK(delegate_); |
| 46 CR_DEFINE_STATIC_LOCAL(ui::AXNodeData, empty_data, ()); |
| 47 const AXNodeData* data = delegate_->GetData(); |
| 48 return data ? *data : empty_data; |
24 } | 49 } |
25 | 50 |
26 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const { | 51 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const { |
27 if (!delegate_) | 52 CHECK(delegate_); |
28 return gfx::Rect(); | |
29 gfx::Rect bounds = delegate_->GetData()->location; | 53 gfx::Rect bounds = delegate_->GetData()->location; |
30 bounds.Offset(delegate_->GetGlobalCoordinateOffset()); | 54 bounds.Offset(delegate_->GetGlobalCoordinateOffset()); |
31 return bounds; | 55 return bounds; |
32 } | 56 } |
33 | 57 |
34 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() { | 58 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() { |
35 return delegate_ ? delegate_->GetParent() : NULL; | 59 CHECK(delegate_); |
| 60 return delegate_->GetParent(); |
36 } | 61 } |
37 | 62 |
38 int AXPlatformNodeBase::GetChildCount() { | 63 int AXPlatformNodeBase::GetChildCount() { |
39 return delegate_ ? delegate_->GetChildCount() : 0; | 64 CHECK(delegate_); |
| 65 return delegate_->GetChildCount(); |
40 } | 66 } |
41 | 67 |
42 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) { | 68 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) { |
43 return delegate_ ? delegate_->ChildAtIndex(index) : NULL; | 69 CHECK(delegate_); |
44 } | 70 return delegate_->ChildAtIndex(index); |
45 | 71 } |
46 // AXPlatformNode | 72 |
| 73 int AXPlatformNodeBase::GetIndexInParent() { |
| 74 CHECK(delegate_); |
| 75 return delegate_->GetIndexInParent(); |
| 76 } |
| 77 |
| 78 // AXPlatformNode overrides. |
47 | 79 |
48 void AXPlatformNodeBase::Destroy() { | 80 void AXPlatformNodeBase::Destroy() { |
49 delegate_ = NULL; | 81 delegate_ = nullptr; |
50 delete this; | 82 delete this; |
51 } | 83 } |
52 | 84 |
53 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() { | 85 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() { |
54 return NULL; | 86 return nullptr; |
55 } | 87 } |
56 | 88 |
| 89 // Helpers. |
| 90 |
| 91 AXPlatformNodeBase* AXPlatformNodeBase::GetPreviousSibling() { |
| 92 CHECK(delegate_); |
| 93 gfx::NativeViewAccessible parent_accessible = GetParent(); |
| 94 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); |
| 95 if (!parent) |
| 96 return nullptr; |
| 97 |
| 98 int previous_index = GetIndexInParent() - 1; |
| 99 if (previous_index >= 0 && |
| 100 previous_index < parent->GetChildCount()) { |
| 101 return FromNativeViewAccessible(parent->ChildAtIndex(previous_index)); |
| 102 } |
| 103 return nullptr; |
| 104 } |
| 105 |
| 106 AXPlatformNodeBase* AXPlatformNodeBase::GetNextSibling() { |
| 107 CHECK(delegate_); |
| 108 gfx::NativeViewAccessible parent_accessible = GetParent(); |
| 109 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); |
| 110 if (!parent) |
| 111 return nullptr; |
| 112 |
| 113 int next_index = GetIndexInParent() + 1; |
| 114 if (next_index >= 0 && next_index < parent->GetChildCount()) |
| 115 return FromNativeViewAccessible(parent->ChildAtIndex(next_index)); |
| 116 return nullptr; |
| 117 } |
| 118 |
| 119 bool AXPlatformNodeBase::IsDescendant(AXPlatformNodeBase* node) { |
| 120 CHECK(delegate_); |
| 121 if (!node) |
| 122 return false; |
| 123 if (node == this) |
| 124 return true; |
| 125 AXPlatformNodeBase* parent = FromNativeViewAccessible(node->GetParent()); |
| 126 return IsDescendant(parent); |
| 127 } |
| 128 |
| 129 bool AXPlatformNodeBase::HasBoolAttribute( |
| 130 ui::AXBoolAttribute attribute) const { |
| 131 CHECK(delegate_); |
| 132 const ui::AXNodeData& data = GetData(); |
| 133 auto iter = FindInVectorOfPairs(attribute, data.bool_attributes); |
| 134 return iter != data.bool_attributes.end(); |
| 135 } |
| 136 |
| 137 bool AXPlatformNodeBase::GetBoolAttribute( |
| 138 ui::AXBoolAttribute attribute) const { |
| 139 CHECK(delegate_); |
| 140 bool result; |
| 141 if (GetBoolAttribute(attribute, &result)) |
| 142 return result; |
| 143 return false; |
| 144 } |
| 145 |
| 146 bool AXPlatformNodeBase::GetBoolAttribute( |
| 147 ui::AXBoolAttribute attribute, bool* value) const { |
| 148 CHECK(delegate_); |
| 149 const ui::AXNodeData& data = GetData(); |
| 150 auto iter = FindInVectorOfPairs(attribute, data.bool_attributes); |
| 151 if (iter != data.bool_attributes.end()) { |
| 152 *value = iter->second; |
| 153 return true; |
| 154 } |
| 155 |
| 156 return false; |
| 157 } |
| 158 |
| 159 bool AXPlatformNodeBase::HasFloatAttribute( |
| 160 ui::AXFloatAttribute attribute) const { |
| 161 CHECK(delegate_); |
| 162 const ui::AXNodeData& data = GetData(); |
| 163 auto iter = FindInVectorOfPairs(attribute, data.float_attributes); |
| 164 return iter != data.float_attributes.end(); |
| 165 } |
| 166 |
| 167 float AXPlatformNodeBase::GetFloatAttribute( |
| 168 ui::AXFloatAttribute attribute) const { |
| 169 CHECK(delegate_); |
| 170 float result; |
| 171 if (GetFloatAttribute(attribute, &result)) |
| 172 return result; |
| 173 return 0.0; |
| 174 } |
| 175 |
| 176 bool AXPlatformNodeBase::GetFloatAttribute( |
| 177 ui::AXFloatAttribute attribute, float* value) const { |
| 178 CHECK(delegate_); |
| 179 const ui::AXNodeData& data = GetData(); |
| 180 auto iter = FindInVectorOfPairs(attribute, data.float_attributes); |
| 181 if (iter != data.float_attributes.end()) { |
| 182 *value = iter->second; |
| 183 return true; |
| 184 } |
| 185 |
| 186 return false; |
| 187 } |
| 188 |
| 189 bool AXPlatformNodeBase::HasIntAttribute( |
| 190 ui::AXIntAttribute attribute) const { |
| 191 CHECK(delegate_); |
| 192 const ui::AXNodeData& data = GetData(); |
| 193 auto iter = FindInVectorOfPairs(attribute, data.int_attributes); |
| 194 return iter != data.int_attributes.end(); |
| 195 } |
| 196 |
| 197 int AXPlatformNodeBase::GetIntAttribute( |
| 198 ui::AXIntAttribute attribute) const { |
| 199 CHECK(delegate_); |
| 200 int result; |
| 201 if (GetIntAttribute(attribute, &result)) |
| 202 return result; |
| 203 return 0; |
| 204 } |
| 205 |
| 206 bool AXPlatformNodeBase::GetIntAttribute( |
| 207 ui::AXIntAttribute attribute, int* value) const { |
| 208 CHECK(delegate_); |
| 209 const ui::AXNodeData& data = GetData(); |
| 210 auto iter = FindInVectorOfPairs(attribute, data.int_attributes); |
| 211 if (iter != data.int_attributes.end()) { |
| 212 *value = iter->second; |
| 213 return true; |
| 214 } |
| 215 |
| 216 return false; |
| 217 } |
| 218 |
| 219 bool AXPlatformNodeBase::HasStringAttribute( |
| 220 ui::AXStringAttribute attribute) const { |
| 221 CHECK(delegate_); |
| 222 const ui::AXNodeData& data = GetData(); |
| 223 auto iter = FindInVectorOfPairs(attribute, data.string_attributes); |
| 224 return iter != data.string_attributes.end(); |
| 225 } |
| 226 |
| 227 const std::string& AXPlatformNodeBase::GetStringAttribute( |
| 228 ui::AXStringAttribute attribute) const { |
| 229 CHECK(delegate_); |
| 230 const ui::AXNodeData& data = GetData(); |
| 231 CR_DEFINE_STATIC_LOCAL(std::string, empty_string, ()); |
| 232 auto iter = FindInVectorOfPairs(attribute, data.string_attributes); |
| 233 return iter != data.string_attributes.end() ? iter->second : empty_string; |
| 234 } |
| 235 |
| 236 bool AXPlatformNodeBase::GetStringAttribute( |
| 237 ui::AXStringAttribute attribute, std::string* value) const { |
| 238 CHECK(delegate_); |
| 239 const ui::AXNodeData& data = GetData(); |
| 240 auto iter = FindInVectorOfPairs(attribute, data.string_attributes); |
| 241 if (iter != data.string_attributes.end()) { |
| 242 *value = iter->second; |
| 243 return true; |
| 244 } |
| 245 |
| 246 return false; |
| 247 } |
| 248 |
| 249 base::string16 AXPlatformNodeBase::GetString16Attribute( |
| 250 ui::AXStringAttribute attribute) const { |
| 251 CHECK(delegate_); |
| 252 std::string value_utf8; |
| 253 if (!GetStringAttribute(attribute, &value_utf8)) |
| 254 return base::string16(); |
| 255 return base::UTF8ToUTF16(value_utf8); |
| 256 } |
| 257 |
| 258 bool AXPlatformNodeBase::GetString16Attribute( |
| 259 ui::AXStringAttribute attribute, |
| 260 base::string16* value) const { |
| 261 CHECK(delegate_); |
| 262 std::string value_utf8; |
| 263 if (!GetStringAttribute(attribute, &value_utf8)) |
| 264 return false; |
| 265 *value = base::UTF8ToUTF16(value_utf8); |
| 266 return true; |
| 267 } |
| 268 |
| 269 AXPlatformNodeBase::AXPlatformNodeBase() { |
| 270 } |
| 271 |
| 272 AXPlatformNodeBase::~AXPlatformNodeBase() { |
| 273 } |
| 274 |
| 275 // static |
| 276 AXPlatformNodeBase* AXPlatformNodeBase::FromNativeViewAccessible( |
| 277 gfx::NativeViewAccessible accessible) { |
| 278 return static_cast<AXPlatformNodeBase*>( |
| 279 AXPlatformNode::FromNativeViewAccessible(accessible)); |
| 280 } |
57 | 281 |
58 } // namespace ui | 282 } // namespace ui |
OLD | NEW |