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 "base/strings/utf_string_conversions.h" |
8 #include "ui/accessibility/ax_action_data.h" | 8 #include "ui/accessibility/ax_action_data.h" |
9 #include "ui/accessibility/ax_node_data.h" | 9 #include "ui/accessibility/ax_node_data.h" |
10 #include "ui/accessibility/ax_role_properties.h" | 10 #include "ui/accessibility/ax_role_properties.h" |
11 #include "ui/accessibility/platform/ax_platform_node_delegate.h" | 11 #include "ui/accessibility/platform/ax_platform_node_delegate.h" |
12 #include "ui/gfx/geometry/rect_conversions.h" | 12 #include "ui/gfx/geometry/rect_conversions.h" |
13 | 13 |
14 namespace ui { | 14 namespace ui { |
15 | 15 |
16 void AXPlatformNodeBase::Init(AXPlatformNodeDelegate* delegate) { | 16 void AXPlatformNodeBase::Init(AXPlatformNodeDelegate* delegate) { |
17 delegate_ = delegate; | 17 delegate_ = delegate; |
18 } | 18 } |
19 | 19 |
20 const AXNodeData& AXPlatformNodeBase::GetData() const { | 20 const AXNodeData& AXPlatformNodeBase::GetData() const { |
21 CHECK(delegate_); | 21 CR_DEFINE_STATIC_LOCAL(ui::AXNodeData, empty_data, ()); |
22 return delegate_->GetData(); | 22 if (delegate_) |
23 return delegate_->GetData(); | |
24 return empty_data; | |
23 } | 25 } |
24 | 26 |
25 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const { | 27 gfx::Rect AXPlatformNodeBase::GetBoundsInScreen() const { |
26 CHECK(delegate_); | 28 if (delegate_) |
27 return delegate_->GetScreenBoundsRect(); | 29 return delegate_->GetScreenBoundsRect(); |
30 return gfx::Rect(); | |
28 } | 31 } |
29 | 32 |
30 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() { | 33 gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() { |
31 CHECK(delegate_); | 34 if (delegate_) |
32 return delegate_->GetParent(); | 35 return delegate_->GetParent(); |
36 return nullptr; | |
33 } | 37 } |
34 | 38 |
35 int AXPlatformNodeBase::GetChildCount() { | 39 int AXPlatformNodeBase::GetChildCount() { |
36 CHECK(delegate_); | 40 if (delegate_) |
37 return delegate_->GetChildCount(); | 41 return delegate_->GetChildCount(); |
42 return 0; | |
38 } | 43 } |
39 | 44 |
40 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) { | 45 gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(int index) { |
41 CHECK(delegate_); | 46 if (delegate_) |
42 return delegate_->ChildAtIndex(index); | 47 return delegate_->ChildAtIndex(index); |
48 return nullptr; | |
43 } | 49 } |
44 | 50 |
45 // AXPlatformNode overrides. | 51 // AXPlatformNode overrides. |
46 | 52 |
47 void AXPlatformNodeBase::Destroy() { | 53 void AXPlatformNodeBase::Destroy() { |
48 AXPlatformNode::Destroy(); | 54 AXPlatformNode::Destroy(); |
49 delegate_ = nullptr; | 55 delegate_ = nullptr; |
50 Dispose(); | 56 Dispose(); |
51 } | 57 } |
52 | 58 |
53 void AXPlatformNodeBase::Dispose() { | 59 void AXPlatformNodeBase::Dispose() { |
54 delete this; | 60 delete this; |
55 } | 61 } |
56 | 62 |
57 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() { | 63 gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() { |
58 return nullptr; | 64 return nullptr; |
59 } | 65 } |
60 | 66 |
61 AXPlatformNodeDelegate* AXPlatformNodeBase::GetDelegate() const { | 67 AXPlatformNodeDelegate* AXPlatformNodeBase::GetDelegate() const { |
62 return delegate_; | 68 return delegate_; |
63 } | 69 } |
64 | 70 |
65 // Helpers. | 71 // Helpers. |
66 | 72 |
67 AXPlatformNodeBase* AXPlatformNodeBase::GetPreviousSibling() { | 73 AXPlatformNodeBase* AXPlatformNodeBase::GetPreviousSibling() { |
68 CHECK(delegate_); | 74 if (!delegate_) |
75 return nullptr; | |
76 | |
69 gfx::NativeViewAccessible parent_accessible = GetParent(); | 77 gfx::NativeViewAccessible parent_accessible = GetParent(); |
70 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); | 78 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); |
71 if (!parent) | 79 if (!parent) |
72 return nullptr; | 80 return nullptr; |
73 | 81 |
74 int previous_index = GetIndexInParent() - 1; | 82 int previous_index = GetIndexInParent() - 1; |
75 if (previous_index >= 0 && | 83 if (previous_index >= 0 && |
76 previous_index < parent->GetChildCount()) { | 84 previous_index < parent->GetChildCount()) { |
77 return FromNativeViewAccessible(parent->ChildAtIndex(previous_index)); | 85 return FromNativeViewAccessible(parent->ChildAtIndex(previous_index)); |
78 } | 86 } |
79 return nullptr; | 87 return nullptr; |
80 } | 88 } |
81 | 89 |
82 AXPlatformNodeBase* AXPlatformNodeBase::GetNextSibling() { | 90 AXPlatformNodeBase* AXPlatformNodeBase::GetNextSibling() { |
83 CHECK(delegate_); | 91 if (!delegate_) |
92 return nullptr; | |
93 | |
84 gfx::NativeViewAccessible parent_accessible = GetParent(); | 94 gfx::NativeViewAccessible parent_accessible = GetParent(); |
85 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); | 95 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); |
86 if (!parent) | 96 if (!parent) |
87 return nullptr; | 97 return nullptr; |
88 | 98 |
89 int next_index = GetIndexInParent() + 1; | 99 int next_index = GetIndexInParent() + 1; |
90 if (next_index >= 0 && next_index < parent->GetChildCount()) | 100 if (next_index >= 0 && next_index < parent->GetChildCount()) |
91 return FromNativeViewAccessible(parent->ChildAtIndex(next_index)); | 101 return FromNativeViewAccessible(parent->ChildAtIndex(next_index)); |
92 return nullptr; | 102 return nullptr; |
93 } | 103 } |
94 | 104 |
95 bool AXPlatformNodeBase::IsDescendant(AXPlatformNodeBase* node) { | 105 bool AXPlatformNodeBase::IsDescendant(AXPlatformNodeBase* node) { |
96 CHECK(delegate_); | 106 if (!delegate_) |
107 return false; | |
108 | |
97 if (!node) | 109 if (!node) |
98 return false; | 110 return false; |
99 if (node == this) | 111 if (node == this) |
100 return true; | 112 return true; |
101 gfx::NativeViewAccessible native_parent = node->GetParent(); | 113 gfx::NativeViewAccessible native_parent = node->GetParent(); |
102 if (!native_parent) | 114 if (!native_parent) |
103 return false; | 115 return false; |
104 AXPlatformNodeBase* parent = FromNativeViewAccessible(native_parent); | 116 AXPlatformNodeBase* parent = FromNativeViewAccessible(native_parent); |
105 return IsDescendant(parent); | 117 return IsDescendant(parent); |
106 } | 118 } |
107 | 119 |
108 bool AXPlatformNodeBase::HasBoolAttribute( | 120 bool AXPlatformNodeBase::HasBoolAttribute( |
109 ui::AXBoolAttribute attribute) const { | 121 ui::AXBoolAttribute attribute) const { |
110 CHECK(delegate_); | 122 if (!delegate_) |
123 return false; | |
111 return GetData().HasBoolAttribute(attribute); | 124 return GetData().HasBoolAttribute(attribute); |
112 } | 125 } |
113 | 126 |
114 bool AXPlatformNodeBase::GetBoolAttribute( | 127 bool AXPlatformNodeBase::GetBoolAttribute( |
115 ui::AXBoolAttribute attribute) const { | 128 ui::AXBoolAttribute attribute) const { |
116 CHECK(delegate_); | 129 if (!delegate_) |
130 return false; | |
117 return GetData().GetBoolAttribute(attribute); | 131 return GetData().GetBoolAttribute(attribute); |
118 } | 132 } |
119 | 133 |
120 bool AXPlatformNodeBase::GetBoolAttribute( | 134 bool AXPlatformNodeBase::GetBoolAttribute( |
121 ui::AXBoolAttribute attribute, bool* value) const { | 135 ui::AXBoolAttribute attribute, bool* value) const { |
122 CHECK(delegate_); | 136 if (!delegate_) |
137 return false; | |
123 return GetData().GetBoolAttribute(attribute, value); | 138 return GetData().GetBoolAttribute(attribute, value); |
124 } | 139 } |
125 | 140 |
126 bool AXPlatformNodeBase::HasFloatAttribute( | 141 bool AXPlatformNodeBase::HasFloatAttribute( |
127 ui::AXFloatAttribute attribute) const { | 142 ui::AXFloatAttribute attribute) const { |
128 CHECK(delegate_); | 143 if (!delegate_) |
144 return false; | |
129 return GetData().HasFloatAttribute(attribute); | 145 return GetData().HasFloatAttribute(attribute); |
130 } | 146 } |
131 | 147 |
132 float AXPlatformNodeBase::GetFloatAttribute( | 148 float AXPlatformNodeBase::GetFloatAttribute( |
133 ui::AXFloatAttribute attribute) const { | 149 ui::AXFloatAttribute attribute) const { |
134 CHECK(delegate_); | 150 if (!delegate_) |
151 return false; | |
135 return GetData().GetFloatAttribute(attribute); | 152 return GetData().GetFloatAttribute(attribute); |
136 } | 153 } |
137 | 154 |
138 bool AXPlatformNodeBase::GetFloatAttribute( | 155 bool AXPlatformNodeBase::GetFloatAttribute( |
139 ui::AXFloatAttribute attribute, float* value) const { | 156 ui::AXFloatAttribute attribute, float* value) const { |
140 CHECK(delegate_); | 157 if (!delegate_) |
158 return false; | |
141 return GetData().GetFloatAttribute(attribute, value); | 159 return GetData().GetFloatAttribute(attribute, value); |
142 } | 160 } |
143 | 161 |
144 bool AXPlatformNodeBase::HasIntAttribute( | 162 bool AXPlatformNodeBase::HasIntAttribute( |
145 ui::AXIntAttribute attribute) const { | 163 ui::AXIntAttribute attribute) const { |
146 CHECK(delegate_); | 164 if (!delegate_) |
165 return false; | |
147 return GetData().HasIntAttribute(attribute); | 166 return GetData().HasIntAttribute(attribute); |
148 } | 167 } |
149 | 168 |
150 int AXPlatformNodeBase::GetIntAttribute( | 169 int AXPlatformNodeBase::GetIntAttribute( |
151 ui::AXIntAttribute attribute) const { | 170 ui::AXIntAttribute attribute) const { |
152 CHECK(delegate_); | 171 if (!delegate_) |
172 return false; | |
153 return GetData().GetIntAttribute(attribute); | 173 return GetData().GetIntAttribute(attribute); |
154 } | 174 } |
155 | 175 |
156 bool AXPlatformNodeBase::GetIntAttribute( | 176 bool AXPlatformNodeBase::GetIntAttribute( |
157 ui::AXIntAttribute attribute, int* value) const { | 177 ui::AXIntAttribute attribute, int* value) const { |
158 CHECK(delegate_); | 178 if (!delegate_) |
179 return false; | |
159 return GetData().GetIntAttribute(attribute, value); | 180 return GetData().GetIntAttribute(attribute, value); |
160 } | 181 } |
161 | 182 |
162 bool AXPlatformNodeBase::HasStringAttribute( | 183 bool AXPlatformNodeBase::HasStringAttribute( |
163 ui::AXStringAttribute attribute) const { | 184 ui::AXStringAttribute attribute) const { |
164 CHECK(delegate_); | 185 if (!delegate_) |
186 return false; | |
165 return GetData().HasStringAttribute(attribute); | 187 return GetData().HasStringAttribute(attribute); |
166 } | 188 } |
167 | 189 |
168 const std::string& AXPlatformNodeBase::GetStringAttribute( | 190 const std::string& AXPlatformNodeBase::GetStringAttribute( |
169 ui::AXStringAttribute attribute) const { | 191 ui::AXStringAttribute attribute) const { |
170 CHECK(delegate_); | 192 CR_DEFINE_STATIC_LOCAL(std::string, empty_data, ()); |
193 if (!delegate_) | |
194 return empty_data; | |
171 return GetData().GetStringAttribute(attribute); | 195 return GetData().GetStringAttribute(attribute); |
172 } | 196 } |
173 | 197 |
174 bool AXPlatformNodeBase::GetStringAttribute( | 198 bool AXPlatformNodeBase::GetStringAttribute( |
175 ui::AXStringAttribute attribute, std::string* value) const { | 199 ui::AXStringAttribute attribute, std::string* value) const { |
176 CHECK(delegate_); | 200 if (!delegate_) |
201 return false; | |
177 return GetData().GetStringAttribute(attribute, value); | 202 return GetData().GetStringAttribute(attribute, value); |
178 } | 203 } |
179 | 204 |
180 base::string16 AXPlatformNodeBase::GetString16Attribute( | 205 base::string16 AXPlatformNodeBase::GetString16Attribute( |
181 ui::AXStringAttribute attribute) const { | 206 ui::AXStringAttribute attribute) const { |
182 CHECK(delegate_); | 207 if (!delegate_) |
208 return false; | |
183 return GetData().GetString16Attribute(attribute); | 209 return GetData().GetString16Attribute(attribute); |
184 } | 210 } |
185 | 211 |
186 bool AXPlatformNodeBase::GetString16Attribute( | 212 bool AXPlatformNodeBase::GetString16Attribute( |
187 ui::AXStringAttribute attribute, | 213 ui::AXStringAttribute attribute, |
188 base::string16* value) const { | 214 base::string16* value) const { |
189 CHECK(delegate_); | 215 if (!delegate_) |
216 return false; | |
190 return GetData().GetString16Attribute(attribute, value); | 217 return GetData().GetString16Attribute(attribute, value); |
191 } | 218 } |
192 | 219 |
193 bool AXPlatformNodeBase::HasIntListAttribute( | 220 bool AXPlatformNodeBase::HasIntListAttribute( |
194 ui::AXIntListAttribute attribute) const { | 221 ui::AXIntListAttribute attribute) const { |
222 if (!delegate_) | |
223 return false; | |
195 return GetData().HasIntListAttribute(attribute); | 224 return GetData().HasIntListAttribute(attribute); |
196 } | 225 } |
197 | 226 |
198 const std::vector<int32_t>& AXPlatformNodeBase::GetIntListAttribute( | 227 const std::vector<int32_t>& AXPlatformNodeBase::GetIntListAttribute( |
199 ui::AXIntListAttribute attribute) const { | 228 ui::AXIntListAttribute attribute) const { |
229 CR_DEFINE_STATIC_LOCAL(std::vector<int32_t>, empty_data, ()); | |
230 if (!delegate_) | |
231 return empty_data; | |
200 return GetData().GetIntListAttribute(attribute); | 232 return GetData().GetIntListAttribute(attribute); |
201 } | 233 } |
202 | 234 |
203 bool AXPlatformNodeBase::GetIntListAttribute( | 235 bool AXPlatformNodeBase::GetIntListAttribute( |
204 ui::AXIntListAttribute attribute, | 236 ui::AXIntListAttribute attribute, |
205 std::vector<int32_t>* value) const { | 237 std::vector<int32_t>* value) const { |
238 if (!delegate_) | |
239 return false; | |
206 return GetData().GetIntListAttribute(attribute, value); | 240 return GetData().GetIntListAttribute(attribute, value); |
207 } | 241 } |
208 | 242 |
209 AXPlatformNodeBase::AXPlatformNodeBase() { | 243 AXPlatformNodeBase::AXPlatformNodeBase() { |
210 } | 244 } |
211 | 245 |
212 AXPlatformNodeBase::~AXPlatformNodeBase() { | 246 AXPlatformNodeBase::~AXPlatformNodeBase() { |
213 CHECK(!delegate_); | |
214 } | 247 } |
215 | 248 |
216 // static | 249 // static |
217 AXPlatformNodeBase* AXPlatformNodeBase::FromNativeViewAccessible( | 250 AXPlatformNodeBase* AXPlatformNodeBase::FromNativeViewAccessible( |
218 gfx::NativeViewAccessible accessible) { | 251 gfx::NativeViewAccessible accessible) { |
219 return static_cast<AXPlatformNodeBase*>( | 252 return static_cast<AXPlatformNodeBase*>( |
220 AXPlatformNode::FromNativeViewAccessible(accessible)); | 253 AXPlatformNode::FromNativeViewAccessible(accessible)); |
221 } | 254 } |
222 | 255 |
223 bool AXPlatformNodeBase::SetTextSelection(int start_offset, int end_offset) { | 256 bool AXPlatformNodeBase::SetTextSelection(int start_offset, int end_offset) { |
224 ui::AXActionData action_data; | 257 ui::AXActionData action_data; |
225 action_data.action = ui::AX_ACTION_SET_SELECTION; | 258 action_data.action = ui::AX_ACTION_SET_SELECTION; |
226 action_data.anchor_node_id = action_data.focus_node_id = GetData().id; | 259 action_data.anchor_node_id = action_data.focus_node_id = GetData().id; |
227 action_data.anchor_offset = start_offset; | 260 action_data.anchor_offset = start_offset; |
228 action_data.focus_offset = end_offset; | 261 action_data.focus_offset = end_offset; |
229 DCHECK(delegate_); | 262 if (!delegate_) |
263 return false; | |
264 | |
230 return delegate_->AccessibilityPerformAction(action_data); | 265 return delegate_->AccessibilityPerformAction(action_data); |
231 } | 266 } |
232 | 267 |
233 bool AXPlatformNodeBase::IsTextOnlyObject() const { | 268 bool AXPlatformNodeBase::IsTextOnlyObject() const { |
234 return GetData().role == ui::AX_ROLE_STATIC_TEXT || | 269 return GetData().role == ui::AX_ROLE_STATIC_TEXT || |
235 GetData().role == ui::AX_ROLE_LINE_BREAK || | 270 GetData().role == ui::AX_ROLE_LINE_BREAK || |
236 GetData().role == ui::AX_ROLE_INLINE_TEXT_BOX; | 271 GetData().role == ui::AX_ROLE_INLINE_TEXT_BOX; |
237 } | 272 } |
238 | 273 |
239 bool AXPlatformNodeBase::IsNativeTextControl() const { | 274 bool AXPlatformNodeBase::IsNativeTextControl() const { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 case ui::AX_ROLE_SCROLL_BAR: | 334 case ui::AX_ROLE_SCROLL_BAR: |
300 return true; | 335 return true; |
301 case ui::AX_ROLE_SPLITTER: | 336 case ui::AX_ROLE_SPLITTER: |
302 return GetData().HasState(ui::AX_STATE_FOCUSABLE); | 337 return GetData().HasState(ui::AX_STATE_FOCUSABLE); |
303 default: | 338 default: |
304 return false; | 339 return false; |
305 } | 340 } |
306 } | 341 } |
307 | 342 |
308 AXPlatformNodeBase* AXPlatformNodeBase::GetTable() const { | 343 AXPlatformNodeBase* AXPlatformNodeBase::GetTable() const { |
344 if (!delegate_) | |
345 return nullptr; | |
346 | |
309 AXPlatformNodeBase* table = const_cast<AXPlatformNodeBase*>(this); | 347 AXPlatformNodeBase* table = const_cast<AXPlatformNodeBase*>(this); |
310 while (table && !ui::IsTableLikeRole(table->GetData().role)) { | 348 while (table && !ui::IsTableLikeRole(table->GetData().role)) { |
311 gfx::NativeViewAccessible parent_accessible = table->GetParent(); | 349 gfx::NativeViewAccessible parent_accessible = table->GetParent(); |
312 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); | 350 AXPlatformNodeBase* parent = FromNativeViewAccessible(parent_accessible); |
313 | 351 |
314 table = parent; | 352 table = parent; |
315 } | 353 } |
316 return table; | 354 return table; |
317 } | 355 } |
318 | 356 |
319 AXPlatformNodeBase* AXPlatformNodeBase::GetTableCell(int index) const { | 357 AXPlatformNodeBase* AXPlatformNodeBase::GetTableCell(int index) const { |
320 DCHECK(delegate_); | 358 if (!delegate_) |
David Tseng
2017/07/12 22:48:45
Optional: This is such a common pattern I am tempt
| |
359 return nullptr; | |
321 | 360 |
322 if (!ui::IsTableLikeRole(GetData().role) && | 361 if (!ui::IsTableLikeRole(GetData().role) && |
323 !ui::IsCellOrTableHeaderRole(GetData().role)) | 362 !ui::IsCellOrTableHeaderRole(GetData().role)) |
324 return nullptr; | 363 return nullptr; |
325 | 364 |
326 AXPlatformNodeBase* table = GetTable(); | 365 AXPlatformNodeBase* table = GetTable(); |
327 if (!table) | 366 if (!table) |
328 return nullptr; | 367 return nullptr; |
329 const std::vector<int32_t>& unique_cell_ids = | 368 const std::vector<int32_t>& unique_cell_ids = |
330 table->GetIntListAttribute(ui::AX_ATTR_UNIQUE_CELL_IDS); | 369 table->GetIntListAttribute(ui::AX_ATTR_UNIQUE_CELL_IDS); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 if (!ui::IsCellOrTableHeaderRole(GetData().role)) | 459 if (!ui::IsCellOrTableHeaderRole(GetData().role)) |
421 return 0; | 460 return 0; |
422 | 461 |
423 int row_span; | 462 int row_span; |
424 if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &row_span)) | 463 if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &row_span)) |
425 return row_span; | 464 return row_span; |
426 return 1; | 465 return 1; |
427 } | 466 } |
428 | 467 |
429 } // namespace ui | 468 } // namespace ui |
OLD | NEW |