Remove unused parameter.
[chromium-blink-merge.git] / ui / accessibility / platform / ax_platform_node_base.cc
blob62cfe5aec10ec627902eb34742e80828aed7a64d
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"
11 namespace ui {
13 namespace {
15 // Predicate that returns true if the first value of a pair is |first|.
16 template<typename FirstType, typename SecondType>
17 struct FirstIs {
18 FirstIs(FirstType first)
19 : first_(first) {}
20 bool operator()(std::pair<FirstType, SecondType> const& p) {
21 return p.first == first_;
23 FirstType 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
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));
38 } // namespace
40 void AXPlatformNodeBase::Init(AXPlatformNodeDelegate* delegate) {
41 delegate_ = delegate;
44 const AXNodeData& AXPlatformNodeBase::GetData() const {
45 CHECK(delegate_);
46 return delegate_->GetData();
49 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const {
50 CHECK(delegate_);
51 gfx::Rect bounds = GetData().location;
52 bounds.Offset(delegate_->GetGlobalCoordinateOffset());
53 return bounds;
56 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() {
57 CHECK(delegate_);
58 return delegate_->GetParent();
61 int AXPlatformNodeBase::GetChildCount() {
62 CHECK(delegate_);
63 return delegate_->GetChildCount();
66 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) {
67 CHECK(delegate_);
68 return delegate_->ChildAtIndex(index);
71 // AXPlatformNode overrides.
73 void AXPlatformNodeBase::Destroy() {
74 delegate_ = nullptr;
75 delete this;
78 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() {
79 return nullptr;
82 AXPlatformNodeDelegate* AXPlatformNodeBase::GetDelegate() const {
83 return delegate_;
86 // Helpers.
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;
95 int previous_index = GetIndexInParent() - 1;
96 if (previous_index >= 0 &&
97 previous_index < parent->GetChildCount()) {
98 return FromNativeViewAccessible(parent->ChildAtIndex(previous_index));
100 return nullptr;
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;
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;
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);
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();
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;
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;
153 return false;
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();
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;
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;
183 return false;
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();
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;
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;
213 return false;
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();
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;
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;
243 return false;
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);
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;
266 AXPlatformNodeBase::AXPlatformNodeBase() {
269 AXPlatformNodeBase::~AXPlatformNodeBase() {
272 // static
273 AXPlatformNodeBase* AXPlatformNodeBase::FromNativeViewAccessible(
274 gfx::NativeViewAccessible accessible) {
275 return static_cast<AXPlatformNodeBase*>(
276 AXPlatformNode::FromNativeViewAccessible(accessible));
279 } // namespace ui