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 return delegate_->GetData(); |
24 } | 47 } |
25 | 48 |
26 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const { | 49 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const { |
27 if (!delegate_) | 50 CHECK(delegate_); |
28 return gfx::Rect(); | 51 gfx::Rect bounds = GetData().location; |
29 gfx::Rect bounds = delegate_->GetData()->location; | |
30 bounds.Offset(delegate_->GetGlobalCoordinateOffset()); | 52 bounds.Offset(delegate_->GetGlobalCoordinateOffset()); |
31 return bounds; | 53 return bounds; |
32 } | 54 } |
33 | 55 |
34 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() { | 56 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() { |
35 return delegate_ ? delegate_->GetParent() : NULL; | 57 CHECK(delegate_); |
| 58 return delegate_->GetParent(); |
36 } | 59 } |
37 | 60 |
38 int AXPlatformNodeBase::GetChildCount() { | 61 int AXPlatformNodeBase::GetChildCount() { |
39 return delegate_ ? delegate_->GetChildCount() : 0; | 62 CHECK(delegate_); |
| 63 return delegate_->GetChildCount(); |
40 } | 64 } |
41 | 65 |
42 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) { | 66 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) { |
43 return delegate_ ? delegate_->ChildAtIndex(index) : NULL; | 67 CHECK(delegate_); |
44 } | 68 return delegate_->ChildAtIndex(index); |
45 | 69 } |
46 // AXPlatformNode | 70 |
| 71 // AXPlatformNode overrides. |
47 | 72 |
48 void AXPlatformNodeBase::Destroy() { | 73 void AXPlatformNodeBase::Destroy() { |
49 delegate_ = NULL; | 74 delegate_ = nullptr; |
50 delete this; | 75 delete this; |
51 } | 76 } |
52 | 77 |
53 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() { | 78 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() { |
54 return NULL; | 79 return nullptr; |
55 } | 80 } |
56 | 81 |
| 82 AXPlatformNodeDelegate* AXPlatformNodeBase::GetDelegate() const { |
| 83 return delegate_; |
| 84 } |
| 85 |
| 86 // Helpers. |
| 87 |
| 88 AXPlatformNodeBase* AXPlatformNodeBase::GetPreviousSibling() { |
| 89 CHECK(delegate_); |
| 90 gfx::NativeViewAccessible parent_accessible = GetParent(); |
| 91 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); |
| 92 if (!parent) |
| 93 return nullptr; |
| 94 |
| 95 int previous_index = GetIndexInParent() - 1; |
| 96 if (previous_index >= 0 && |
| 97 previous_index < parent->GetChildCount()) { |
| 98 return FromNativeViewAccessible(parent->ChildAtIndex(previous_index)); |
| 99 } |
| 100 return nullptr; |
| 101 } |
| 102 |
| 103 AXPlatformNodeBase* AXPlatformNodeBase::GetNextSibling() { |
| 104 CHECK(delegate_); |
| 105 gfx::NativeViewAccessible parent_accessible = GetParent(); |
| 106 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); |
| 107 if (!parent) |
| 108 return nullptr; |
| 109 |
| 110 int next_index = GetIndexInParent() + 1; |
| 111 if (next_index >= 0 && next_index < parent->GetChildCount()) |
| 112 return FromNativeViewAccessible(parent->ChildAtIndex(next_index)); |
| 113 return nullptr; |
| 114 } |
| 115 |
| 116 bool AXPlatformNodeBase::IsDescendant(AXPlatformNodeBase* node) { |
| 117 CHECK(delegate_); |
| 118 if (!node) |
| 119 return false; |
| 120 if (node == this) |
| 121 return true; |
| 122 AXPlatformNodeBase* parent = FromNativeViewAccessible(node->GetParent()); |
| 123 return IsDescendant(parent); |
| 124 } |
| 125 |
| 126 bool AXPlatformNodeBase::HasBoolAttribute( |
| 127 ui::AXBoolAttribute attribute) const { |
| 128 CHECK(delegate_); |
| 129 const ui::AXNodeData& data = GetData(); |
| 130 auto iter = FindInVectorOfPairs(attribute, data.bool_attributes); |
| 131 return iter != data.bool_attributes.end(); |
| 132 } |
| 133 |
| 134 bool AXPlatformNodeBase::GetBoolAttribute( |
| 135 ui::AXBoolAttribute attribute) const { |
| 136 CHECK(delegate_); |
| 137 bool result; |
| 138 if (GetBoolAttribute(attribute, &result)) |
| 139 return result; |
| 140 return false; |
| 141 } |
| 142 |
| 143 bool AXPlatformNodeBase::GetBoolAttribute( |
| 144 ui::AXBoolAttribute attribute, bool* value) const { |
| 145 CHECK(delegate_); |
| 146 const ui::AXNodeData& data = GetData(); |
| 147 auto iter = FindInVectorOfPairs(attribute, data.bool_attributes); |
| 148 if (iter != data.bool_attributes.end()) { |
| 149 *value = iter->second; |
| 150 return true; |
| 151 } |
| 152 |
| 153 return false; |
| 154 } |
| 155 |
| 156 bool AXPlatformNodeBase::HasFloatAttribute( |
| 157 ui::AXFloatAttribute attribute) const { |
| 158 CHECK(delegate_); |
| 159 const ui::AXNodeData& data = GetData(); |
| 160 auto iter = FindInVectorOfPairs(attribute, data.float_attributes); |
| 161 return iter != data.float_attributes.end(); |
| 162 } |
| 163 |
| 164 float AXPlatformNodeBase::GetFloatAttribute( |
| 165 ui::AXFloatAttribute attribute) const { |
| 166 CHECK(delegate_); |
| 167 float result; |
| 168 if (GetFloatAttribute(attribute, &result)) |
| 169 return result; |
| 170 return 0.0; |
| 171 } |
| 172 |
| 173 bool AXPlatformNodeBase::GetFloatAttribute( |
| 174 ui::AXFloatAttribute attribute, float* value) const { |
| 175 CHECK(delegate_); |
| 176 const ui::AXNodeData& data = GetData(); |
| 177 auto iter = FindInVectorOfPairs(attribute, data.float_attributes); |
| 178 if (iter != data.float_attributes.end()) { |
| 179 *value = iter->second; |
| 180 return true; |
| 181 } |
| 182 |
| 183 return false; |
| 184 } |
| 185 |
| 186 bool AXPlatformNodeBase::HasIntAttribute( |
| 187 ui::AXIntAttribute attribute) const { |
| 188 CHECK(delegate_); |
| 189 const ui::AXNodeData& data = GetData(); |
| 190 auto iter = FindInVectorOfPairs(attribute, data.int_attributes); |
| 191 return iter != data.int_attributes.end(); |
| 192 } |
| 193 |
| 194 int AXPlatformNodeBase::GetIntAttribute( |
| 195 ui::AXIntAttribute attribute) const { |
| 196 CHECK(delegate_); |
| 197 int result; |
| 198 if (GetIntAttribute(attribute, &result)) |
| 199 return result; |
| 200 return 0; |
| 201 } |
| 202 |
| 203 bool AXPlatformNodeBase::GetIntAttribute( |
| 204 ui::AXIntAttribute attribute, int* value) const { |
| 205 CHECK(delegate_); |
| 206 const ui::AXNodeData& data = GetData(); |
| 207 auto iter = FindInVectorOfPairs(attribute, data.int_attributes); |
| 208 if (iter != data.int_attributes.end()) { |
| 209 *value = iter->second; |
| 210 return true; |
| 211 } |
| 212 |
| 213 return false; |
| 214 } |
| 215 |
| 216 bool AXPlatformNodeBase::HasStringAttribute( |
| 217 ui::AXStringAttribute attribute) const { |
| 218 CHECK(delegate_); |
| 219 const ui::AXNodeData& data = GetData(); |
| 220 auto iter = FindInVectorOfPairs(attribute, data.string_attributes); |
| 221 return iter != data.string_attributes.end(); |
| 222 } |
| 223 |
| 224 const std::string& AXPlatformNodeBase::GetStringAttribute( |
| 225 ui::AXStringAttribute attribute) const { |
| 226 CHECK(delegate_); |
| 227 const ui::AXNodeData& data = GetData(); |
| 228 CR_DEFINE_STATIC_LOCAL(std::string, empty_string, ()); |
| 229 auto iter = FindInVectorOfPairs(attribute, data.string_attributes); |
| 230 return iter != data.string_attributes.end() ? iter->second : empty_string; |
| 231 } |
| 232 |
| 233 bool AXPlatformNodeBase::GetStringAttribute( |
| 234 ui::AXStringAttribute attribute, std::string* value) const { |
| 235 CHECK(delegate_); |
| 236 const ui::AXNodeData& data = GetData(); |
| 237 auto iter = FindInVectorOfPairs(attribute, data.string_attributes); |
| 238 if (iter != data.string_attributes.end()) { |
| 239 *value = iter->second; |
| 240 return true; |
| 241 } |
| 242 |
| 243 return false; |
| 244 } |
| 245 |
| 246 base::string16 AXPlatformNodeBase::GetString16Attribute( |
| 247 ui::AXStringAttribute attribute) const { |
| 248 CHECK(delegate_); |
| 249 std::string value_utf8; |
| 250 if (!GetStringAttribute(attribute, &value_utf8)) |
| 251 return base::string16(); |
| 252 return base::UTF8ToUTF16(value_utf8); |
| 253 } |
| 254 |
| 255 bool AXPlatformNodeBase::GetString16Attribute( |
| 256 ui::AXStringAttribute attribute, |
| 257 base::string16* value) const { |
| 258 CHECK(delegate_); |
| 259 std::string value_utf8; |
| 260 if (!GetStringAttribute(attribute, &value_utf8)) |
| 261 return false; |
| 262 *value = base::UTF8ToUTF16(value_utf8); |
| 263 return true; |
| 264 } |
| 265 |
| 266 AXPlatformNodeBase::AXPlatformNodeBase() { |
| 267 } |
| 268 |
| 269 AXPlatformNodeBase::~AXPlatformNodeBase() { |
| 270 } |
| 271 |
| 272 // static |
| 273 AXPlatformNodeBase* AXPlatformNodeBase::FromNativeViewAccessible( |
| 274 gfx::NativeViewAccessible accessible) { |
| 275 return static_cast<AXPlatformNodeBase*>( |
| 276 AXPlatformNode::FromNativeViewAccessible(accessible)); |
| 277 } |
57 | 278 |
58 } // namespace ui | 279 } // namespace ui |
OLD | NEW |