Chromium Code Reviews| 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 AXPlatformNodeBase::AXPlatformNodeBase() { |
| 13 } | 14 } |
| 14 | 15 |
| 15 AXPlatformNodeBase::~AXPlatformNodeBase() { | 16 AXPlatformNodeBase::~AXPlatformNodeBase() { |
| 16 } | 17 } |
| 17 | 18 |
| 18 void AXPlatformNodeBase::Init(AXPlatformNodeDelegate* delegate) { | 19 void AXPlatformNodeBase::Init(AXPlatformNodeDelegate* delegate) { |
| 19 delegate_ = delegate; | 20 delegate_ = delegate; |
| 20 } | 21 } |
| 21 | 22 |
| 22 AXRole AXPlatformNodeBase::GetRole() const { | 23 const AXNodeData& AXPlatformNodeBase::GetData() const { |
| 23 return delegate_ ? delegate_->GetData()->role : AX_ROLE_UNKNOWN; | 24 CR_DEFINE_STATIC_LOCAL(ui::AXNodeData, empty_data, ()); |
| 25 if (!delegate_) | |
| 26 return empty_data; | |
| 27 const AXNodeData* data = delegate_->GetData(); | |
| 28 return data ? *data : empty_data; | |
| 24 } | 29 } |
| 25 | 30 |
| 26 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const { | 31 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const { |
| 27 if (!delegate_) | 32 if (!delegate_) |
| 28 return gfx::Rect(); | 33 return gfx::Rect(); |
| 29 gfx::Rect bounds = delegate_->GetData()->location; | 34 gfx::Rect bounds = delegate_->GetData()->location; |
| 30 bounds.Offset(delegate_->GetGlobalCoordinateOffset()); | 35 bounds.Offset(delegate_->GetGlobalCoordinateOffset()); |
| 31 return bounds; | 36 return bounds; |
| 32 } | 37 } |
| 33 | 38 |
| 34 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() { | 39 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() { |
| 35 return delegate_ ? delegate_->GetParent() : NULL; | 40 return delegate_ ? delegate_->GetParent() : NULL; |
| 36 } | 41 } |
| 37 | 42 |
| 38 int AXPlatformNodeBase::GetChildCount() { | 43 int AXPlatformNodeBase::GetChildCount() { |
| 39 return delegate_ ? delegate_->GetChildCount() : 0; | 44 return delegate_ ? delegate_->GetChildCount() : 0; |
| 40 } | 45 } |
| 41 | 46 |
| 42 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) { | 47 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) { |
| 43 return delegate_ ? delegate_->ChildAtIndex(index) : NULL; | 48 return delegate_ ? delegate_->ChildAtIndex(index) : NULL; |
| 44 } | 49 } |
| 45 | 50 |
| 46 // AXPlatformNode | 51 int AXPlatformNodeBase::GetIndexInParent() { |
| 52 return delegate_ ? delegate_->GetIndexInParent() : 0; | |
|
David Tseng
2015/02/13 20:10:10
Is this index 0-based?
nektarios
2015/02/13 22:44:29
Yes.
David Tseng
2015/02/13 23:34:25
Concern is 0 is also being used as the "error" val
dmazzoni
2015/02/17 22:48:05
Good point. Thinking about this some more, trying
| |
| 53 } | |
| 54 | |
| 55 // AXPlatformNode. | |
|
David Tseng
2015/02/13 20:10:10
?
dmazzoni
2015/02/17 22:48:05
Changed to "AXPlatformNode overrides". Some Chrome
| |
| 47 | 56 |
| 48 void AXPlatformNodeBase::Destroy() { | 57 void AXPlatformNodeBase::Destroy() { |
| 49 delegate_ = NULL; | 58 delegate_ = NULL; |
| 50 delete this; | 59 delete this; |
| 51 } | 60 } |
| 52 | 61 |
| 53 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() { | 62 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() { |
| 54 return NULL; | 63 return NULL; |
| 55 } | 64 } |
| 56 | 65 |
| 66 // Helpers. | |
| 67 | |
| 68 AXPlatformNodeBase* AXPlatformNodeBase::GetPreviousSibling() { | |
| 69 gfx::NativeViewAccessible parent_accessible = GetParent(); | |
| 70 AXPlatformNodeBase* parent = static_cast<AXPlatformNodeBase*>( | |
|
nektarios
2015/02/13 22:44:29
Are all the static_casts in this file really neces
dmazzoni
2015/02/17 22:48:04
Good point - I added an additional helper function
| |
| 71 AXPlatformNode::FromNativeViewAccessible(parent_accessible)); | |
| 72 if (!parent) | |
| 73 return NULL; | |
| 74 | |
| 75 int index_in_parent = GetIndexInParent(); | |
| 76 if (index_in_parent > 0 && | |
|
nektarios
2015/02/13 22:44:29
Use >= 1 and <= .count instead. Clearer.
dmazzoni
2015/02/17 22:48:05
Agreed about the first, but I think that the secon
| |
| 77 index_in_parent - 1 < parent->GetChildCount()) { | |
| 78 return static_cast<AXPlatformNodeBase*>( | |
| 79 AXPlatformNode::FromNativeViewAccessible( | |
| 80 parent->ChildAtIndex(index_in_parent - 1))); | |
| 81 } | |
| 82 return NULL; | |
| 83 } | |
| 84 | |
| 85 AXPlatformNodeBase* AXPlatformNodeBase::GetNextSibling() { | |
| 86 gfx::NativeViewAccessible parent_accessible = GetParent(); | |
| 87 AXPlatformNodeBase* parent = static_cast<AXPlatformNodeBase*>( | |
| 88 AXPlatformNode::FromNativeViewAccessible(parent_accessible)); | |
| 89 if (!parent) | |
| 90 return NULL; | |
| 91 | |
| 92 int index_in_parent = GetIndexInParent(); | |
|
nektarios
2015/02/13 22:44:29
Do the plus one here. Clearer.
dmazzoni
2015/02/17 22:48:05
Great idea.
| |
| 93 if (index_in_parent + 1 < parent->GetChildCount()) { | |
|
David Tseng
2015/02/13 20:10:10
No lower bound?
nektarios
2015/02/13 22:44:29
Non-user provided. Supposed to be error-free.
dmazzoni
2015/02/17 22:48:05
Done.
dmazzoni
2015/02/17 22:48:05
Done.
| |
| 94 return static_cast<AXPlatformNodeBase*>( | |
| 95 AXPlatformNode::FromNativeViewAccessible( | |
| 96 parent->ChildAtIndex(index_in_parent + 1))); | |
| 97 } | |
| 98 return NULL; | |
| 99 } | |
| 100 | |
| 101 bool AXPlatformNodeBase::IsDescendant(AXPlatformNodeBase* descendant) { | |
|
David Tseng
2015/02/13 20:10:10
I find this param name confusing. Perhaps just nod
| |
| 102 if (!descendant) | |
| 103 return false; | |
| 104 if (descendant == this) | |
| 105 return true; | |
| 106 AXPlatformNodeBase* parent = static_cast<AXPlatformNodeBase*>( | |
| 107 AXPlatformNode::FromNativeViewAccessible(descendant->GetParent())); | |
|
David Tseng
2015/02/13 20:10:10
Suggestion: refactor this static_cast... pattern i
dmazzoni
2015/02/17 22:48:05
Done.
| |
| 108 return IsDescendant(parent); | |
| 109 } | |
| 110 | |
| 111 bool AXPlatformNodeBase::HasBoolAttribute( | |
| 112 ui::AXBoolAttribute attribute) const { | |
| 113 const ui::AXNodeData& data = GetData(); | |
| 114 for (size_t i = 0; i < data.bool_attributes.size(); ++i) { | |
|
nektarios
2015/02/13 22:44:29
Instead of these for loops I prefer a function tha
dmazzoni
2015/02/17 22:48:05
OK. I created a templated helper function FindInVe
| |
| 115 if (data.bool_attributes[i].first == attribute) | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 return false; | |
| 120 } | |
| 121 | |
| 122 | |
|
David Tseng
2015/02/13 20:10:10
nit: extra line
dmazzoni
2015/02/17 22:48:05
Done.
| |
| 123 bool AXPlatformNodeBase::GetBoolAttribute( | |
| 124 ui::AXBoolAttribute attribute) const { | |
| 125 const ui::AXNodeData& data = GetData(); | |
| 126 for (size_t i = 0; i < data.bool_attributes.size(); ++i) { | |
| 127 if (data.bool_attributes[i].first == attribute) | |
| 128 return data.bool_attributes[i].second; | |
| 129 } | |
| 130 | |
| 131 return false; | |
| 132 } | |
|
David Tseng
2015/02/13 20:10:10
The below method is a generalization of this one.
dmazzoni
2015/02/17 22:48:05
Sure. Done for bool, int, and float. I kept the Ge
| |
| 133 | |
| 134 bool AXPlatformNodeBase::GetBoolAttribute( | |
| 135 ui::AXBoolAttribute attribute, bool* value) const { | |
| 136 const ui::AXNodeData& data = GetData(); | |
| 137 for (size_t i = 0; i < data.bool_attributes.size(); ++i) { | |
| 138 if (data.bool_attributes[i].first == attribute) { | |
| 139 *value = data.bool_attributes[i].second; | |
| 140 return true; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 return false; | |
| 145 } | |
| 146 | |
| 147 bool AXPlatformNodeBase::HasFloatAttribute( | |
| 148 ui::AXFloatAttribute attribute) const { | |
| 149 const ui::AXNodeData& data = GetData(); | |
| 150 for (size_t i = 0; i < data.float_attributes.size(); ++i) { | |
| 151 if (data.float_attributes[i].first == attribute) | |
| 152 return true; | |
| 153 } | |
| 154 | |
| 155 return false; | |
| 156 } | |
|
David Tseng
2015/02/13 20:10:10
Same for this one. Just use the return value of th
dmazzoni
2015/02/17 22:48:04
Done.
| |
| 157 | |
| 158 float AXPlatformNodeBase::GetFloatAttribute( | |
| 159 ui::AXFloatAttribute attribute) const { | |
| 160 const ui::AXNodeData& data = GetData(); | |
| 161 for (size_t i = 0; i < data.float_attributes.size(); ++i) { | |
| 162 if (data.float_attributes[i].first == attribute) | |
| 163 return data.float_attributes[i].second; | |
| 164 } | |
| 165 | |
| 166 return 0.0; | |
| 167 } | |
|
David Tseng
2015/02/13 20:10:10
ditto.
| |
| 168 | |
| 169 bool AXPlatformNodeBase::GetFloatAttribute( | |
| 170 ui::AXFloatAttribute attribute, float* value) const { | |
| 171 const ui::AXNodeData& data = GetData(); | |
| 172 for (size_t i = 0; i < data.float_attributes.size(); ++i) { | |
| 173 if (data.float_attributes[i].first == attribute) { | |
| 174 *value = data.float_attributes[i].second; | |
| 175 return true; | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 return false; | |
| 180 } | |
|
David Tseng
2015/02/13 20:10:10
ditto
| |
| 181 | |
| 182 bool AXPlatformNodeBase::HasIntAttribute( | |
| 183 ui::AXIntAttribute attribute) const { | |
| 184 const ui::AXNodeData& data = GetData(); | |
| 185 for (size_t i = 0; i < data.int_attributes.size(); ++i) { | |
| 186 if (data.int_attributes[i].first == attribute) | |
| 187 return true; | |
| 188 } | |
| 189 | |
| 190 return false; | |
| 191 } | |
|
David Tseng
2015/02/13 20:10:10
ditto
| |
| 192 | |
| 193 int AXPlatformNodeBase::GetIntAttribute(ui::AXIntAttribute attribute) const { | |
| 194 const ui::AXNodeData& data = GetData(); | |
| 195 for (size_t i = 0; i < data.int_attributes.size(); ++i) { | |
| 196 if (data.int_attributes[i].first == attribute) | |
| 197 return data.int_attributes[i].second; | |
| 198 } | |
| 199 | |
| 200 return 0; | |
| 201 } | |
|
David Tseng
2015/02/13 20:10:10
ditto
| |
| 202 | |
| 203 bool AXPlatformNodeBase::GetIntAttribute( | |
| 204 ui::AXIntAttribute attribute, int* value) const { | |
| 205 const ui::AXNodeData& data = GetData(); | |
| 206 for (size_t i = 0; i < data.int_attributes.size(); ++i) { | |
| 207 if (data.int_attributes[i].first == attribute) { | |
| 208 *value = data.int_attributes[i].second; | |
| 209 return true; | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 return false; | |
| 214 } | |
| 215 | |
| 216 bool AXPlatformNodeBase::HasStringAttribute( | |
| 217 ui::AXStringAttribute attribute) const { | |
| 218 const ui::AXNodeData& data = GetData(); | |
| 219 for (size_t i = 0; i < data.string_attributes.size(); ++i) { | |
| 220 if (data.string_attributes[i].first == attribute) | |
| 221 return true; | |
| 222 } | |
| 223 | |
| 224 return false; | |
| 225 } | |
|
David Tseng
2015/02/13 20:10:11
ditto
| |
| 226 | |
| 227 const std::string& AXPlatformNodeBase::GetStringAttribute( | |
| 228 ui::AXStringAttribute attribute) const { | |
| 229 const ui::AXNodeData& data = GetData(); | |
| 230 CR_DEFINE_STATIC_LOCAL(std::string, empty_string, ()); | |
| 231 for (size_t i = 0; i < data.string_attributes.size(); ++i) { | |
| 232 if (data.string_attributes[i].first == attribute) | |
| 233 return data.string_attributes[i].second; | |
| 234 } | |
| 235 | |
| 236 return empty_string; | |
| 237 } | |
|
David Tseng
2015/02/13 20:10:11
ditto
| |
| 238 | |
| 239 bool AXPlatformNodeBase::GetStringAttribute( | |
| 240 ui::AXStringAttribute attribute, std::string* value) const { | |
| 241 const ui::AXNodeData& data = GetData(); | |
| 242 for (size_t i = 0; i < data.string_attributes.size(); ++i) { | |
| 243 if (data.string_attributes[i].first == attribute) { | |
| 244 *value = data.string_attributes[i].second; | |
| 245 return true; | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 return false; | |
| 250 } | |
| 251 | |
| 252 base::string16 AXPlatformNodeBase::GetString16Attribute( | |
| 253 ui::AXStringAttribute attribute) const { | |
| 254 std::string value_utf8; | |
| 255 if (!GetStringAttribute(attribute, &value_utf8)) | |
| 256 return base::string16(); | |
| 257 return base::UTF8ToUTF16(value_utf8); | |
| 258 } | |
| 259 | |
| 260 bool AXPlatformNodeBase::GetString16Attribute( | |
| 261 ui::AXStringAttribute attribute, | |
| 262 base::string16* value) const { | |
| 263 std::string value_utf8; | |
| 264 if (!GetStringAttribute(attribute, &value_utf8)) | |
| 265 return false; | |
| 266 *value = base::UTF8ToUTF16(value_utf8); | |
| 267 return true; | |
| 268 } | |
| 57 | 269 |
| 58 } // namespace ui | 270 } // namespace ui |
| OLD | NEW |