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
3 // found in the LICENSE file.
5 #include "ui/accessibility/platform/ax_platform_node_base.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/accessibility/ax_node_data.h"
9 #include "ui/accessibility/platform/ax_platform_node_delegate.h"
15 // Predicate that returns true if the first value of a pair is |first|.
16 template<typename FirstType
, typename SecondType
>
18 FirstIs(FirstType first
)
20 bool operator()(std::pair
<FirstType
, SecondType
> const& p
) {
21 return p
.first
== first_
;
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
32 const std::vector
<std::pair
<FirstType
, SecondType
>>& vector
) {
33 return std::find_if(vector
.begin(),
35 FirstIs
<FirstType
, SecondType
>(first
));
40 void AXPlatformNodeBase::Init(AXPlatformNodeDelegate
* delegate
) {
44 const AXNodeData
& AXPlatformNodeBase::GetData() const {
46 return delegate_
->GetData();
49 gfx::Rect
AXPlatformNodeBase::GetBoundsInScreen() const {
51 gfx::Rect bounds
= GetData().location
;
52 bounds
.Offset(delegate_
->GetGlobalCoordinateOffset());
56 gfx::NativeViewAccessible
AXPlatformNodeBase::GetParent() {
58 return delegate_
->GetParent();
61 int AXPlatformNodeBase::GetChildCount() {
63 return delegate_
->GetChildCount();
66 gfx::NativeViewAccessible
AXPlatformNodeBase::ChildAtIndex(int index
) {
68 return delegate_
->ChildAtIndex(index
);
71 // AXPlatformNode overrides.
73 void AXPlatformNodeBase::Destroy() {
78 gfx::NativeViewAccessible
AXPlatformNodeBase::GetNativeViewAccessible() {
82 AXPlatformNodeDelegate
* AXPlatformNodeBase::GetDelegate() const {
88 AXPlatformNodeBase
* AXPlatformNodeBase::GetPreviousSibling() {
90 gfx::NativeViewAccessible parent_accessible
= GetParent();
91 AXPlatformNodeBase
* parent
= FromNativeViewAccessible(parent_accessible
);
95 int previous_index
= GetIndexInParent() - 1;
96 if (previous_index
>= 0 &&
97 previous_index
< parent
->GetChildCount()) {
98 return FromNativeViewAccessible(parent
->ChildAtIndex(previous_index
));
103 AXPlatformNodeBase
* AXPlatformNodeBase::GetNextSibling() {
105 gfx::NativeViewAccessible parent_accessible
= GetParent();
106 AXPlatformNodeBase
* parent
= FromNativeViewAccessible(parent_accessible
);
110 int next_index
= GetIndexInParent() + 1;
111 if (next_index
>= 0 && next_index
< parent
->GetChildCount())
112 return FromNativeViewAccessible(parent
->ChildAtIndex(next_index
));
116 bool AXPlatformNodeBase::IsDescendant(AXPlatformNodeBase
* node
) {
122 AXPlatformNodeBase
* parent
= FromNativeViewAccessible(node
->GetParent());
123 return IsDescendant(parent
);
126 bool AXPlatformNodeBase::HasBoolAttribute(
127 ui::AXBoolAttribute attribute
) const {
129 const ui::AXNodeData
& data
= GetData();
130 auto iter
= FindInVectorOfPairs(attribute
, data
.bool_attributes
);
131 return iter
!= data
.bool_attributes
.end();
134 bool AXPlatformNodeBase::GetBoolAttribute(
135 ui::AXBoolAttribute attribute
) const {
138 if (GetBoolAttribute(attribute
, &result
))
143 bool AXPlatformNodeBase::GetBoolAttribute(
144 ui::AXBoolAttribute attribute
, bool* value
) const {
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
;
156 bool AXPlatformNodeBase::HasFloatAttribute(
157 ui::AXFloatAttribute attribute
) const {
159 const ui::AXNodeData
& data
= GetData();
160 auto iter
= FindInVectorOfPairs(attribute
, data
.float_attributes
);
161 return iter
!= data
.float_attributes
.end();
164 float AXPlatformNodeBase::GetFloatAttribute(
165 ui::AXFloatAttribute attribute
) const {
168 if (GetFloatAttribute(attribute
, &result
))
173 bool AXPlatformNodeBase::GetFloatAttribute(
174 ui::AXFloatAttribute attribute
, float* value
) const {
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
;
186 bool AXPlatformNodeBase::HasIntAttribute(
187 ui::AXIntAttribute attribute
) const {
189 const ui::AXNodeData
& data
= GetData();
190 auto iter
= FindInVectorOfPairs(attribute
, data
.int_attributes
);
191 return iter
!= data
.int_attributes
.end();
194 int AXPlatformNodeBase::GetIntAttribute(
195 ui::AXIntAttribute attribute
) const {
198 if (GetIntAttribute(attribute
, &result
))
203 bool AXPlatformNodeBase::GetIntAttribute(
204 ui::AXIntAttribute attribute
, int* value
) const {
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
;
216 bool AXPlatformNodeBase::HasStringAttribute(
217 ui::AXStringAttribute attribute
) const {
219 const ui::AXNodeData
& data
= GetData();
220 auto iter
= FindInVectorOfPairs(attribute
, data
.string_attributes
);
221 return iter
!= data
.string_attributes
.end();
224 const std::string
& AXPlatformNodeBase::GetStringAttribute(
225 ui::AXStringAttribute attribute
) const {
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
;
233 bool AXPlatformNodeBase::GetStringAttribute(
234 ui::AXStringAttribute attribute
, std::string
* value
) const {
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
;
246 base::string16
AXPlatformNodeBase::GetString16Attribute(
247 ui::AXStringAttribute attribute
) const {
249 std::string value_utf8
;
250 if (!GetStringAttribute(attribute
, &value_utf8
))
251 return base::string16();
252 return base::UTF8ToUTF16(value_utf8
);
255 bool AXPlatformNodeBase::GetString16Attribute(
256 ui::AXStringAttribute attribute
,
257 base::string16
* value
) const {
259 std::string value_utf8
;
260 if (!GetStringAttribute(attribute
, &value_utf8
))
262 *value
= base::UTF8ToUTF16(value_utf8
);
266 AXPlatformNodeBase::AXPlatformNodeBase() {
269 AXPlatformNodeBase::~AXPlatformNodeBase() {
273 AXPlatformNodeBase
* AXPlatformNodeBase::FromNativeViewAccessible(
274 gfx::NativeViewAccessible accessible
) {
275 return static_cast<AXPlatformNodeBase
*>(
276 AXPlatformNode::FromNativeViewAccessible(accessible
));