Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: ui/accessibility/platform/ax_platform_node_win.cc

Issue 909143003: Re-land: Implement NativeViewAccessibilityWin using AXPlatformNodeWin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unit test Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include <atlbase.h>
6 #include <atlcom.h>
7 #include <oleacc.h>
8
9 #include "base/containers/hash_tables.h"
10 #include "base/lazy_instance.h"
11 #include "base/win/scoped_comptr.h"
12 #include "third_party/iaccessible2/ia2_api_all.h"
13 #include "ui/accessibility/ax_node_data.h"
14 #include "ui/accessibility/ax_text_utils.h"
15 #include "ui/accessibility/platform/ax_platform_node_delegate.h"
16 #include "ui/accessibility/platform/ax_platform_node_win.h"
17 #include "ui/base/win/atl_module.h"
18
19 //
20 // Macros to use at the top of any AXPlatformNodeWin function that implements
21 // a COM interface. Because COM objects are reference counted and clients
22 // are completely untrusted, it's important to always first check that our
23 // object is still valid, and then check that all pointer arguments are
24 // not NULL.
25 //
26
27 #define COM_OBJECT_VALIDATE() \
28 if (!delegate_) \
29 return E_FAIL;
30 #define COM_OBJECT_VALIDATE_1_ARG(arg) \
31 if (!delegate_) return E_FAIL; \
32 if (!arg) return E_INVALIDARG
33 #define COM_OBJECT_VALIDATE_2_ARGS(arg1, arg2) \
34 if (!delegate_) return E_FAIL; \
35 if (!arg1) return E_INVALIDARG; \
36 if (!arg2) return E_INVALIDARG
37 #define COM_OBJECT_VALIDATE_3_ARGS(arg1, arg2, arg3) \
38 if (!delegate_) return E_FAIL; \
39 if (!arg1) return E_INVALIDARG; \
40 if (!arg2) return E_INVALIDARG; \
41 if (!arg3) return E_INVALIDARG
42 #define COM_OBJECT_VALIDATE_4_ARGS(arg1, arg2, arg3, arg4) \
43 if (!delegate_) return E_FAIL; \
44 if (!arg1) return E_INVALIDARG; \
45 if (!arg2) return E_INVALIDARG; \
46 if (!arg3) return E_INVALIDARG; \
47 if (!arg4) return E_INVALIDARG
48 #define COM_OBJECT_VALIDATE_VAR_ID(var_id) \
49 if (!delegate_) return E_FAIL; \
50 if (!IsValidId(var_id)) return E_INVALIDARG
51 #define COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, arg) \
52 if (!delegate_) return E_FAIL; \
53 if (!IsValidId(var_id)) return E_INVALIDARG; \
54 if (!arg) return E_INVALIDARG
55 #define COM_OBJECT_VALIDATE_VAR_ID_2_ARGS(var_id, arg1, arg2) \
56 if (!delegate_) return E_FAIL; \
57 if (!IsValidId(var_id)) return E_INVALIDARG; \
58 if (!arg1) return E_INVALIDARG; \
59 if (!arg2) return E_INVALIDARG
60 #define COM_OBJECT_VALIDATE_VAR_ID_3_ARGS(var_id, arg1, arg2, arg3) \
61 if (!delegate_) return E_FAIL; \
62 if (!IsValidId(var_id)) return E_INVALIDARG; \
63 if (!arg1) return E_INVALIDARG; \
64 if (!arg2) return E_INVALIDARG; \
65 if (!arg3) return E_INVALIDARG
66 #define COM_OBJECT_VALIDATE_VAR_ID_4_ARGS(var_id, arg1, arg2, arg3, arg4) \
67 if (!delegate_) return E_FAIL; \
68 if (!IsValidId(var_id)) return E_INVALIDARG; \
69 if (!arg1) return E_INVALIDARG; \
70 if (!arg2) return E_INVALIDARG; \
71 if (!arg3) return E_INVALIDARG; \
72 if (!arg4) return E_INVALIDARG
73
74 namespace ui {
75
76 namespace {
77
78 typedef base::hash_map<LONG, AXPlatformNodeWin*> UniqueIdWinMap;
79 // Map from each AXPlatformNodeWin's unique id to its instance.
80 base::LazyInstance<UniqueIdWinMap> g_unique_id_win_map =
81 LAZY_INSTANCE_INITIALIZER;
82
83 typedef base::hash_set<AXPlatformNodeWin*> AXPlatformNodeWinSet;
84 // Set of all AXPlatformNodeWin objects that were the target of an
85 // alert event.
86 base::LazyInstance<AXPlatformNodeWinSet> g_alert_targets =
87 LAZY_INSTANCE_INITIALIZER;
88
89 LONG GetNextNegativeUniqueIdForWinAccessibility(AXPlatformNodeWin* obj) {
90 static LONG next_unique_id = -1;
91 LONG unique_id = next_unique_id;
92 if (next_unique_id == LONG_MIN)
93 next_unique_id = -1;
94 else
95 next_unique_id--;
96
97 g_unique_id_win_map.Get().insert(std::make_pair(unique_id, obj));
98
99 return unique_id;
100 }
101
102 void UnregisterNegativeUniqueId(LONG unique_id) {
103 g_unique_id_win_map.Get().erase(unique_id);
104 }
105
106 } // namespace
107
108 // static
109 AXPlatformNode* AXPlatformNode::Create(AXPlatformNodeDelegate* delegate) {
110 // Make sure ATL is initialized in this module.
111 ui::win::CreateATLModuleIfNeeded();
112
113 CComObject<AXPlatformNodeWin>* instance = nullptr;
114 HRESULT hr = CComObject<AXPlatformNodeWin>::CreateInstance(&instance);
115 DCHECK(SUCCEEDED(hr));
116 instance->Init(delegate);
117 instance->AddRef();
118 return instance;
119 }
120
121 // static
122 AXPlatformNode* AXPlatformNode::FromNativeViewAccessible(
123 gfx::NativeViewAccessible accessible) {
124 base::win::ScopedComPtr<AXPlatformNodeWin> ax_platform_node;
125 accessible->QueryInterface(ax_platform_node.Receive());
126 return ax_platform_node.get();
127 }
128
129 AXPlatformNodeWin::AXPlatformNodeWin()
130 : unique_id_win_(GetNextNegativeUniqueIdForWinAccessibility(this)) {
131 }
132
133 AXPlatformNodeWin::~AXPlatformNodeWin() {
134 CHECK(!delegate_);
135 }
136
137 //
138 // AXPlatformNode implementation.
139 //
140
141 void AXPlatformNodeWin::Destroy() {
142 delegate_ = nullptr;
143 UnregisterNegativeUniqueId(unique_id_win_);
144 RemoveAlertTarget();
145 Release();
146 }
147
148 gfx::NativeViewAccessible AXPlatformNodeWin::GetNativeViewAccessible() {
149 return this;
150 }
151
152 void AXPlatformNodeWin::NotifyAccessibilityEvent(ui::AXEvent event_type) {
153 HWND hwnd = delegate_->GetTargetForNativeAccessibilityEvent();
154 if (!hwnd)
155 return;
156
157 int native_event = MSAAEvent(event_type);
158 if (native_event < EVENT_MIN)
159 return;
160
161 ::NotifyWinEvent(native_event, hwnd, OBJID_CLIENT, unique_id_win_);
162
163 // Keep track of objects that are a target of an alert event.
164 if (event_type == ui::AX_EVENT_ALERT)
165 AddAlertTarget();
166 }
167
168 //
169 // IAccessible implementation.
170 //
171
172 STDMETHODIMP AXPlatformNodeWin::accHitTest(
173 LONG x_left, LONG y_top, VARIANT* child) {
174 COM_OBJECT_VALIDATE_1_ARG(child);
175 gfx::NativeViewAccessible hit_child = delegate_->HitTestSync(x_left, y_top);
176 if (!hit_child) {
177 child->vt = VT_EMPTY;
178 return S_FALSE;
179 }
180
181 if (hit_child == this) {
182 // This object is the best match, so return CHILDID_SELF. It's tempting to
183 // simplify the logic and use VT_DISPATCH everywhere, but the Windows
184 // call AccessibleObjectFromPoint will keep calling accHitTest until some
185 // object returns CHILDID_SELF.
186 child->vt = VT_I4;
187 child->lVal = CHILDID_SELF;
188 return S_OK;
189 }
190
191 // Call accHitTest recursively on the result, which may be a recursive call
192 // to this function or it may be overridden, for example in the case of a
193 // WebView.
194 HRESULT result = hit_child->accHitTest(x_left, y_top, child);
195
196 // If the recursive call returned CHILDID_SELF, we have to convert that
197 // into a VT_DISPATCH for the return value to this call.
198 if (S_OK == result && child->vt == VT_I4 && child->lVal == CHILDID_SELF) {
199 child->vt = VT_DISPATCH;
200 child->pdispVal = hit_child;
201 // Always increment ref when returning a reference to a COM object.
202 child->pdispVal->AddRef();
203 }
204 return result;
205 }
206
207 HRESULT AXPlatformNodeWin::accDoDefaultAction(VARIANT var_id) {
208 COM_OBJECT_VALIDATE_VAR_ID(var_id);
209 delegate_->DoDefaultAction();
210 return S_OK;
211 }
212
213 STDMETHODIMP AXPlatformNodeWin::accLocation(
214 LONG* x_left, LONG* y_top, LONG* width, LONG* height, VARIANT var_id) {
215 COM_OBJECT_VALIDATE_VAR_ID_4_ARGS(var_id, x_left, y_top, width, height);
216 gfx::Rect bounds = GetData().location;
217 bounds += delegate_->GetGlobalCoordinateOffset();
218 *x_left = bounds.x();
219 *y_top = bounds.y();
220 *width = bounds.width();
221 *height = bounds.height();
222
223 if (bounds.IsEmpty())
224 return S_FALSE;
225
226 return S_OK;
227 }
228
229 STDMETHODIMP AXPlatformNodeWin::accNavigate(
230 LONG nav_dir, VARIANT start, VARIANT* end) {
231 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(start, end);
232 IAccessible* result = nullptr;
233
234 switch (nav_dir) {
235 case NAVDIR_DOWN:
236 case NAVDIR_UP:
237 case NAVDIR_LEFT:
238 case NAVDIR_RIGHT:
239 // These directions are not implemented, matching Mozilla and IE.
240 return E_NOTIMPL;
241
242 case NAVDIR_FIRSTCHILD:
243 if (delegate_->GetChildCount() > 0)
244 result = delegate_->ChildAtIndex(0);
245 break;
246
247 case NAVDIR_LASTCHILD:
248 if (delegate_->GetChildCount() > 0)
249 result = delegate_->ChildAtIndex(delegate_->GetChildCount() - 1);
250 break;
251
252 case NAVDIR_NEXT:
253 result = GetNextSibling()->GetNativeViewAccessible();
254 break;
255
256 case NAVDIR_PREVIOUS:
257 result = GetPreviousSibling()->GetNativeViewAccessible();
258 break;
259
260 default:
261 return E_INVALIDARG;
262 }
263
264 if (!result) {
265 end->vt = VT_EMPTY;
266 return S_FALSE;
267 }
268
269 end->vt = VT_DISPATCH;
270 end->pdispVal = result;
271 // Always increment ref when returning a reference to a COM object.
272 end->pdispVal->AddRef();
273
274 return S_OK;
275 }
276
277 STDMETHODIMP AXPlatformNodeWin::get_accChild(VARIANT var_child,
278 IDispatch** disp_child) {
279 COM_OBJECT_VALIDATE_1_ARG(disp_child);
280 LONG child_id = V_I4(&var_child);
281 if (child_id == CHILDID_SELF) {
282 *disp_child = this;
283 (*disp_child)->AddRef();
284 return S_OK;
285 }
286
287 if (child_id >= 1 && child_id <= delegate_->GetChildCount()) {
288 // Positive child ids are a 1-based child index, used by clients
289 // that want to enumerate all immediate children.
290 *disp_child = delegate_->ChildAtIndex(child_id - 1);
291 (*disp_child)->AddRef();
292 return S_OK;
293 }
294
295 if (child_id >= 0)
296 return E_FAIL;
297
298 // Negative child ids can be used to map to any descendant.
299 UniqueIdWinMap* unique_ids = g_unique_id_win_map.Pointer();
300 auto iter = unique_ids->find(child_id);
301 if (iter != unique_ids->end()) {
302 *disp_child = iter->second;
303 (*disp_child)->AddRef();
304 return S_OK;
305 }
306
307 *disp_child = nullptr;
308 return E_FAIL;
309 }
310
311 STDMETHODIMP AXPlatformNodeWin::get_accChildCount(LONG* child_count) {
312 COM_OBJECT_VALIDATE_1_ARG(child_count);
313 *child_count = delegate_->GetChildCount();
314 return S_OK;
315 }
316
317 STDMETHODIMP AXPlatformNodeWin::get_accDefaultAction(
318 VARIANT var_id, BSTR* def_action) {
319 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, def_action);
320 return GetStringAttributeAsBstr(ui::AX_ATTR_ACTION, def_action);
321 }
322
323 STDMETHODIMP AXPlatformNodeWin::get_accDescription(
324 VARIANT var_id, BSTR* desc) {
325 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, desc);
326 return GetStringAttributeAsBstr(ui::AX_ATTR_DESCRIPTION, desc);
327 }
328
329 STDMETHODIMP AXPlatformNodeWin::get_accFocus(VARIANT* focus_child) {
330 COM_OBJECT_VALIDATE_1_ARG(focus_child);
331 gfx::NativeViewAccessible focus_accessible = delegate_->GetFocus();
332 if (focus_accessible == this) {
333 focus_child->vt = VT_I4;
334 focus_child->lVal = CHILDID_SELF;
335 } else if (focus_accessible) {
336 focus_child->vt = VT_DISPATCH;
337 focus_child->pdispVal = focus_accessible;
338 focus_child->pdispVal->AddRef();
339 return S_OK;
340 } else {
341 focus_child->vt = VT_EMPTY;
342 }
343
344 return S_OK;
345 }
346
347 STDMETHODIMP AXPlatformNodeWin::get_accKeyboardShortcut(
348 VARIANT var_id, BSTR* acc_key) {
349 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, acc_key);
350 return GetStringAttributeAsBstr(ui::AX_ATTR_SHORTCUT, acc_key);
351 }
352
353 STDMETHODIMP AXPlatformNodeWin::get_accName(
354 VARIANT var_id, BSTR* name) {
355 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, name);
356 return GetStringAttributeAsBstr(ui::AX_ATTR_NAME, name);
357 }
358
359 STDMETHODIMP AXPlatformNodeWin::get_accParent(
360 IDispatch** disp_parent) {
361 COM_OBJECT_VALIDATE_1_ARG(disp_parent);
362 *disp_parent = GetParent();
363 if (*disp_parent) {
364 (*disp_parent)->AddRef();
365 return S_OK;
366 }
367
368 return S_FALSE;
369 }
370
371 STDMETHODIMP AXPlatformNodeWin::get_accRole(
372 VARIANT var_id, VARIANT* role) {
373 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, role);
374 role->vt = VT_I4;
375 role->lVal = MSAARole();
376 return S_OK;
377 }
378
379 STDMETHODIMP AXPlatformNodeWin::get_accState(
380 VARIANT var_id, VARIANT* state) {
381 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, state);
382 state->vt = VT_I4;
383 state->lVal = MSAAState();
384 return S_OK;
385 }
386
387 STDMETHODIMP AXPlatformNodeWin::get_accHelp(
388 VARIANT var_id, BSTR* help) {
389 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, help);
390 return GetStringAttributeAsBstr(ui::AX_ATTR_HELP, help);
391 }
392
393 STDMETHODIMP AXPlatformNodeWin::get_accValue(VARIANT var_id, BSTR* value) {
394 COM_OBJECT_VALIDATE_VAR_ID_1_ARG(var_id, value);
395 return GetStringAttributeAsBstr(ui::AX_ATTR_VALUE, value);
396 }
397
398 STDMETHODIMP AXPlatformNodeWin::put_accValue(VARIANT var_id,
399 BSTR new_value) {
400 COM_OBJECT_VALIDATE_VAR_ID(var_id);
401 if (delegate_->SetStringValue(new_value))
402 return S_OK;
403 return E_FAIL;
404 }
405
406 // IAccessible functions not supported.
407
408 STDMETHODIMP AXPlatformNodeWin::get_accSelection(VARIANT* selected) {
409 COM_OBJECT_VALIDATE_1_ARG(selected);
410 if (selected)
411 selected->vt = VT_EMPTY;
412 return E_NOTIMPL;
413 }
414
415 STDMETHODIMP AXPlatformNodeWin::accSelect(
416 LONG flagsSelect, VARIANT var_id) {
417 COM_OBJECT_VALIDATE_VAR_ID(var_id);
418 return E_NOTIMPL;
419 }
420
421 STDMETHODIMP AXPlatformNodeWin::get_accHelpTopic(
422 BSTR* help_file, VARIANT var_id, LONG* topic_id) {
423 COM_OBJECT_VALIDATE_VAR_ID_2_ARGS(var_id, help_file, topic_id);
424 if (help_file) {
425 *help_file = nullptr;
426 }
427 if (topic_id) {
428 *topic_id = static_cast<LONG>(-1);
429 }
430 return E_NOTIMPL;
431 }
432
433 STDMETHODIMP AXPlatformNodeWin::put_accName(
434 VARIANT var_id, BSTR put_name) {
435 COM_OBJECT_VALIDATE_VAR_ID(var_id);
436 // Deprecated.
437 return E_NOTIMPL;
438 }
439
440 //
441 // IAccessible2 implementation.
442 //
443
444 STDMETHODIMP AXPlatformNodeWin::role(LONG* role) {
445 COM_OBJECT_VALIDATE_1_ARG(role);
446 *role = MSAARole();
447 return S_OK;
448 }
449
450 STDMETHODIMP AXPlatformNodeWin::get_states(AccessibleStates* states) {
451 COM_OBJECT_VALIDATE_1_ARG(states);
452 // There are only a couple of states we need to support
453 // in IAccessible2. If any more are added, we may want to
454 // add a helper function like MSAAState.
455 *states = IA2_STATE_OPAQUE;
456 if (GetData().state & (1 << ui::AX_STATE_EDITABLE))
457 *states |= IA2_STATE_EDITABLE;
458
459 return S_OK;
460 }
461
462 STDMETHODIMP AXPlatformNodeWin::get_uniqueID(LONG* unique_id) {
463 COM_OBJECT_VALIDATE_1_ARG(unique_id);
464 *unique_id = unique_id_win_;
465 return S_OK;
466 }
467
468 STDMETHODIMP AXPlatformNodeWin::get_windowHandle(HWND* window_handle) {
469 COM_OBJECT_VALIDATE_1_ARG(window_handle);
470 *window_handle = delegate_->GetTargetForNativeAccessibilityEvent();
471 return *window_handle ? S_OK : S_FALSE;
472 }
473
474 STDMETHODIMP AXPlatformNodeWin::get_relationTargetsOfType(
475 BSTR type_bstr,
476 long max_targets,
477 IUnknown ***targets,
478 long *n_targets) {
479 COM_OBJECT_VALIDATE_2_ARGS(targets, n_targets);
480
481 *n_targets = 0;
482 *targets = nullptr;
483
484 // Only respond to requests for relations of type "alerts".
485 base::string16 type(type_bstr);
486 if (type != L"alerts")
487 return S_FALSE;
488
489 // Collect all of the objects that have had an alert fired on them that
490 // are a descendant of this object.
491 std::vector<AXPlatformNodeWin*> alert_targets;
492 for (auto iter = g_alert_targets.Get().begin();
493 iter != g_alert_targets.Get().end();
494 ++iter) {
495 AXPlatformNodeWin* target = *iter;
496 if (IsDescendant(target))
497 alert_targets.push_back(target);
498 }
499
500 long count = static_cast<long>(alert_targets.size());
501 if (count == 0)
502 return S_FALSE;
503
504 // Don't return more targets than max_targets - but note that the caller
505 // is allowed to specify max_targets=0 to mean no limit.
506 if (max_targets > 0 && count > max_targets)
507 count = max_targets;
508
509 // Return the number of targets.
510 *n_targets = count;
511
512 // Allocate COM memory for the result array and populate it.
513 *targets = static_cast<IUnknown**>(
514 CoTaskMemAlloc(count * sizeof(IUnknown*)));
515 for (long i = 0; i < count; ++i) {
516 (*targets)[i] = static_cast<IAccessible*>(alert_targets[i]);
517 (*targets)[i]->AddRef();
518 }
519 return S_OK;
520 }
521
522 STDMETHODIMP AXPlatformNodeWin::get_attributes(BSTR* attributes) {
523 COM_OBJECT_VALIDATE_1_ARG(attributes);
524 base::string16 attributes_str;
525
526 // Text fields need to report the attribute "text-model:a1" to instruct
527 // screen readers to use IAccessible2 APIs to handle text editing in this
528 // object (as opposed to treating it like a native Windows text box).
529 // The text-model:a1 attribute is documented here:
530 // http://www.linuxfoundation.org/collaborate/workgroups/accessibility/ia2/ia2 _implementation_guide
531 if (GetData().role == ui::AX_ROLE_TEXT_FIELD) {
532 attributes_str = L"text-model:a1;";
533 }
534
535 *attributes = SysAllocString(attributes_str.c_str());
536 DCHECK(*attributes);
537 return S_OK;
538 }
539
540 //
541 // IAccessibleText
542 //
543
544 STDMETHODIMP AXPlatformNodeWin::get_nCharacters(LONG* n_characters) {
545 COM_OBJECT_VALIDATE_1_ARG(n_characters);
546 base::string16 text = TextForIAccessibleText();
547 *n_characters = static_cast<LONG>(text.size());
548
549 return S_OK;
550 }
551
552 STDMETHODIMP AXPlatformNodeWin::get_caretOffset(LONG* offset) {
553 COM_OBJECT_VALIDATE_1_ARG(offset);
554 *offset = static_cast<LONG>(GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END));
555 return S_OK;
556 }
557
558 STDMETHODIMP AXPlatformNodeWin::get_nSelections(LONG* n_selections) {
559 COM_OBJECT_VALIDATE_1_ARG(n_selections);
560 int sel_start = GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START);
561 int sel_end = GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END);
562 if (sel_start != sel_end)
563 *n_selections = 1;
564 else
565 *n_selections = 0;
566 return S_OK;
567 }
568
569 STDMETHODIMP AXPlatformNodeWin::get_selection(LONG selection_index,
570 LONG* start_offset,
571 LONG* end_offset) {
572 COM_OBJECT_VALIDATE_2_ARGS(start_offset, end_offset);
573 if (selection_index != 0)
574 return E_INVALIDARG;
575
576 *start_offset = static_cast<LONG>(
577 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START));
578 *end_offset = static_cast<LONG>(
579 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END));
580 return S_OK;
581 }
582
583 STDMETHODIMP AXPlatformNodeWin::get_text(LONG start_offset,
584 LONG end_offset,
585 BSTR* text) {
586 COM_OBJECT_VALIDATE_1_ARG(text);
587 int sel_end = GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START);
588 base::string16 text_str = TextForIAccessibleText();
589 LONG len = static_cast<LONG>(text_str.size());
590
591 if (start_offset == IA2_TEXT_OFFSET_LENGTH) {
592 start_offset = len;
593 } else if (start_offset == IA2_TEXT_OFFSET_CARET) {
594 start_offset = static_cast<LONG>(sel_end);
595 }
596 if (end_offset == IA2_TEXT_OFFSET_LENGTH) {
597 end_offset = static_cast<LONG>(text_str.size());
598 } else if (end_offset == IA2_TEXT_OFFSET_CARET) {
599 end_offset = static_cast<LONG>(sel_end);
600 }
601
602 // The spec allows the arguments to be reversed.
603 if (start_offset > end_offset) {
604 LONG tmp = start_offset;
605 start_offset = end_offset;
606 end_offset = tmp;
607 }
608
609 // The spec does not allow the start or end offsets to be out or range;
610 // we must return an error if so.
611 if (start_offset < 0)
612 return E_INVALIDARG;
613 if (end_offset > len)
614 return E_INVALIDARG;
615
616 base::string16 substr =
617 text_str.substr(start_offset, end_offset - start_offset);
618 if (substr.empty())
619 return S_FALSE;
620
621 *text = SysAllocString(substr.c_str());
622 DCHECK(*text);
623 return S_OK;
624 }
625
626 STDMETHODIMP AXPlatformNodeWin::get_textAtOffset(
627 LONG offset,
628 enum IA2TextBoundaryType boundary_type,
629 LONG* start_offset, LONG* end_offset,
630 BSTR* text) {
631 COM_OBJECT_VALIDATE_3_ARGS(start_offset, end_offset, text);
632 // The IAccessible2 spec says we don't have to implement the "sentence"
633 // boundary type, we can just let the screen reader handle it.
634 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
635 *start_offset = 0;
636 *end_offset = 0;
637 *text = nullptr;
638 return S_FALSE;
639 }
640
641 const base::string16& text_str = TextForIAccessibleText();
642
643 *start_offset = FindBoundary(
644 text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
645 *end_offset = FindBoundary(
646 text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
647 return get_text(*start_offset, *end_offset, text);
648 }
649
650 STDMETHODIMP AXPlatformNodeWin::get_textBeforeOffset(
651 LONG offset,
652 enum IA2TextBoundaryType boundary_type,
653 LONG* start_offset, LONG* end_offset,
654 BSTR* text) {
655 if (!start_offset || !end_offset || !text)
656 return E_INVALIDARG;
657
658 // The IAccessible2 spec says we don't have to implement the "sentence"
659 // boundary type, we can just let the screenreader handle it.
660 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
661 *start_offset = 0;
662 *end_offset = 0;
663 *text = nullptr;
664 return S_FALSE;
665 }
666
667 const base::string16& text_str = TextForIAccessibleText();
668
669 *start_offset = FindBoundary(
670 text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
671 *end_offset = offset;
672 return get_text(*start_offset, *end_offset, text);
673 }
674
675 STDMETHODIMP AXPlatformNodeWin::get_textAfterOffset(
676 LONG offset,
677 enum IA2TextBoundaryType boundary_type,
678 LONG* start_offset, LONG* end_offset,
679 BSTR* text) {
680 if (!start_offset || !end_offset || !text)
681 return E_INVALIDARG;
682
683 // The IAccessible2 spec says we don't have to implement the "sentence"
684 // boundary type, we can just let the screenreader handle it.
685 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
686 *start_offset = 0;
687 *end_offset = 0;
688 *text = nullptr;
689 return S_FALSE;
690 }
691
692 const base::string16& text_str = TextForIAccessibleText();
693
694 *start_offset = offset;
695 *end_offset = FindBoundary(
696 text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
697 return get_text(*start_offset, *end_offset, text);
698 }
699
700 STDMETHODIMP AXPlatformNodeWin::get_offsetAtPoint(
701 LONG x, LONG y, enum IA2CoordinateType coord_type, LONG* offset) {
702 COM_OBJECT_VALIDATE_1_ARG(offset);
703 // We don't support this method, but we have to return something
704 // rather than E_NOTIMPL or screen readers will complain.
705 *offset = 0;
706 return S_OK;
707 }
708
709 //
710 // IServiceProvider implementation.
711 //
712
713 STDMETHODIMP AXPlatformNodeWin::QueryService(
714 REFGUID guidService, REFIID riid, void** object) {
715 COM_OBJECT_VALIDATE_1_ARG(object);
716 if (guidService == IID_IAccessible ||
717 guidService == IID_IAccessible2 ||
718 guidService == IID_IAccessible2_2 ||
719 guidService == IID_IAccessibleText) {
720 return QueryInterface(riid, object);
721 }
722
723 *object = nullptr;
724 return E_FAIL;
725 }
726
727 //
728 // Private member functions.
729 //
730
731 bool AXPlatformNodeWin::IsValidId(const VARIANT& child) const {
732 // Since we have a separate IAccessible COM object for each node, we only
733 // support the CHILDID_SELF id.
734 return (VT_I4 == child.vt) && (CHILDID_SELF == child.lVal);
735 }
736
737 int AXPlatformNodeWin::MSAARole() {
738 switch (GetData().role) {
739 case ui::AX_ROLE_ALERT:
740 return ROLE_SYSTEM_ALERT;
741 case ui::AX_ROLE_APPLICATION:
742 return ROLE_SYSTEM_APPLICATION;
743 case ui::AX_ROLE_BUTTON_DROP_DOWN:
744 return ROLE_SYSTEM_BUTTONDROPDOWN;
745 case ui::AX_ROLE_POP_UP_BUTTON:
746 return ROLE_SYSTEM_BUTTONMENU;
747 case ui::AX_ROLE_CHECK_BOX:
748 return ROLE_SYSTEM_CHECKBUTTON;
749 case ui::AX_ROLE_COMBO_BOX:
750 return ROLE_SYSTEM_COMBOBOX;
751 case ui::AX_ROLE_DIALOG:
752 return ROLE_SYSTEM_DIALOG;
753 case ui::AX_ROLE_GROUP:
754 return ROLE_SYSTEM_GROUPING;
755 case ui::AX_ROLE_IMAGE:
756 return ROLE_SYSTEM_GRAPHIC;
757 case ui::AX_ROLE_LINK:
758 return ROLE_SYSTEM_LINK;
759 case ui::AX_ROLE_LOCATION_BAR:
760 return ROLE_SYSTEM_GROUPING;
761 case ui::AX_ROLE_MENU_BAR:
762 return ROLE_SYSTEM_MENUBAR;
763 case ui::AX_ROLE_MENU_ITEM:
764 return ROLE_SYSTEM_MENUITEM;
765 case ui::AX_ROLE_MENU_LIST_POPUP:
766 return ROLE_SYSTEM_MENUPOPUP;
767 case ui::AX_ROLE_TREE:
768 return ROLE_SYSTEM_OUTLINE;
769 case ui::AX_ROLE_TREE_ITEM:
770 return ROLE_SYSTEM_OUTLINEITEM;
771 case ui::AX_ROLE_TAB:
772 return ROLE_SYSTEM_PAGETAB;
773 case ui::AX_ROLE_TAB_LIST:
774 return ROLE_SYSTEM_PAGETABLIST;
775 case ui::AX_ROLE_PANE:
776 return ROLE_SYSTEM_PANE;
777 case ui::AX_ROLE_PROGRESS_INDICATOR:
778 return ROLE_SYSTEM_PROGRESSBAR;
779 case ui::AX_ROLE_BUTTON:
780 return ROLE_SYSTEM_PUSHBUTTON;
781 case ui::AX_ROLE_RADIO_BUTTON:
782 return ROLE_SYSTEM_RADIOBUTTON;
783 case ui::AX_ROLE_SCROLL_BAR:
784 return ROLE_SYSTEM_SCROLLBAR;
785 case ui::AX_ROLE_SPLITTER:
786 return ROLE_SYSTEM_SEPARATOR;
787 case ui::AX_ROLE_SLIDER:
788 return ROLE_SYSTEM_SLIDER;
789 case ui::AX_ROLE_STATIC_TEXT:
790 return ROLE_SYSTEM_STATICTEXT;
791 case ui::AX_ROLE_TEXT_FIELD:
792 return ROLE_SYSTEM_TEXT;
793 case ui::AX_ROLE_TITLE_BAR:
794 return ROLE_SYSTEM_TITLEBAR;
795 case ui::AX_ROLE_TOOLBAR:
796 return ROLE_SYSTEM_TOOLBAR;
797 case ui::AX_ROLE_WEB_VIEW:
798 return ROLE_SYSTEM_GROUPING;
799 case ui::AX_ROLE_WINDOW:
800 return ROLE_SYSTEM_WINDOW;
801 case ui::AX_ROLE_CLIENT:
802 default:
803 // This is the default role for MSAA.
804 return ROLE_SYSTEM_CLIENT;
805 }
806 }
807
808 int AXPlatformNodeWin::MSAAState() {
809 uint32 state = GetData().state;
810
811 int msaa_state = 0;
812 if (state & (1 << ui::AX_STATE_CHECKED))
813 msaa_state |= STATE_SYSTEM_CHECKED;
814 if (state & (1 << ui::AX_STATE_COLLAPSED))
815 msaa_state |= STATE_SYSTEM_COLLAPSED;
816 if (state & (1 << ui::AX_STATE_DEFAULT))
817 msaa_state |= STATE_SYSTEM_DEFAULT;
818 if (state & (1 << ui::AX_STATE_EXPANDED))
819 msaa_state |= STATE_SYSTEM_EXPANDED;
820 if (state & (1 << ui::AX_STATE_FOCUSABLE))
821 msaa_state |= STATE_SYSTEM_FOCUSABLE;
822 if (state & (1 << ui::AX_STATE_FOCUSED))
823 msaa_state |= STATE_SYSTEM_FOCUSED;
824 if (state & (1 << ui::AX_STATE_HASPOPUP))
825 msaa_state |= STATE_SYSTEM_HASPOPUP;
826 if (state & (1 << ui::AX_STATE_HOVERED))
827 msaa_state |= STATE_SYSTEM_HOTTRACKED;
828 if (state & (1 << ui::AX_STATE_INVISIBLE))
829 msaa_state |= STATE_SYSTEM_INVISIBLE;
830 if (state & (1 << ui::AX_STATE_LINKED))
831 msaa_state |= STATE_SYSTEM_LINKED;
832 if (state & (1 << ui::AX_STATE_OFFSCREEN))
833 msaa_state |= STATE_SYSTEM_OFFSCREEN;
834 if (state & (1 << ui::AX_STATE_PRESSED))
835 msaa_state |= STATE_SYSTEM_PRESSED;
836 if (state & (1 << ui::AX_STATE_PROTECTED))
837 msaa_state |= STATE_SYSTEM_PROTECTED;
838 if (state & (1 << ui::AX_STATE_READ_ONLY))
839 msaa_state |= STATE_SYSTEM_READONLY;
840 if (state & (1 << ui::AX_STATE_SELECTABLE))
841 msaa_state |= STATE_SYSTEM_SELECTABLE;
842 if (state & (1 << ui::AX_STATE_SELECTED))
843 msaa_state |= STATE_SYSTEM_SELECTED;
844 if (state & (1 << ui::AX_STATE_DISABLED))
845 msaa_state |= STATE_SYSTEM_UNAVAILABLE;
846
847 return msaa_state;
848 }
849
850 int AXPlatformNodeWin::MSAAEvent(ui::AXEvent event) {
851 switch (event) {
852 case ui::AX_EVENT_ALERT:
853 return EVENT_SYSTEM_ALERT;
854 case ui::AX_EVENT_FOCUS:
855 return EVENT_OBJECT_FOCUS;
856 case ui::AX_EVENT_MENU_START:
857 return EVENT_SYSTEM_MENUSTART;
858 case ui::AX_EVENT_MENU_END:
859 return EVENT_SYSTEM_MENUEND;
860 case ui::AX_EVENT_MENU_POPUP_START:
861 return EVENT_SYSTEM_MENUPOPUPSTART;
862 case ui::AX_EVENT_MENU_POPUP_END:
863 return EVENT_SYSTEM_MENUPOPUPEND;
864 case ui::AX_EVENT_SELECTION:
865 return EVENT_OBJECT_SELECTION;
866 case ui::AX_EVENT_SELECTION_ADD:
867 return EVENT_OBJECT_SELECTIONADD;
868 case ui::AX_EVENT_SELECTION_REMOVE:
869 return EVENT_OBJECT_SELECTIONREMOVE;
870 case ui::AX_EVENT_TEXT_CHANGED:
871 return EVENT_OBJECT_NAMECHANGE;
872 case ui::AX_EVENT_TEXT_SELECTION_CHANGED:
873 return IA2_EVENT_TEXT_CARET_MOVED;
874 case ui::AX_EVENT_VALUE_CHANGED:
875 return EVENT_OBJECT_VALUECHANGE;
876 default:
877 return -1;
878 }
879 }
880
881 HRESULT AXPlatformNodeWin::GetStringAttributeAsBstr(
882 ui::AXStringAttribute attribute,
883 BSTR* value_bstr) const {
884 base::string16 str;
885
886 if (!GetString16Attribute(attribute, &str))
887 return S_FALSE;
888
889 if (str.empty())
890 return S_FALSE;
891
892 *value_bstr = SysAllocString(str.c_str());
893 DCHECK(*value_bstr);
894
895 return S_OK;
896 }
897
898 void AXPlatformNodeWin::AddAlertTarget() {
899 g_alert_targets.Get().insert(this);
900 }
901
902 void AXPlatformNodeWin::RemoveAlertTarget() {
903 if (g_alert_targets.Get().find(this) != g_alert_targets.Get().end())
904 g_alert_targets.Get().erase(this);
905 }
906
907 base::string16 AXPlatformNodeWin::TextForIAccessibleText() {
908 if (GetData().role == ui::AX_ROLE_TEXT_FIELD)
909 return GetString16Attribute(ui::AX_ATTR_VALUE);
910 else
911 return GetString16Attribute(ui::AX_ATTR_NAME);
912 }
913
914 void AXPlatformNodeWin::HandleSpecialTextOffset(
915 const base::string16& text, LONG* offset) {
916 if (*offset == IA2_TEXT_OFFSET_LENGTH) {
917 *offset = static_cast<LONG>(text.size());
918 } else if (*offset == IA2_TEXT_OFFSET_CARET) {
919 get_caretOffset(offset);
920 }
921 }
922
923 ui::TextBoundaryType AXPlatformNodeWin::IA2TextBoundaryToTextBoundary(
924 IA2TextBoundaryType ia2_boundary) {
925 switch(ia2_boundary) {
926 case IA2_TEXT_BOUNDARY_CHAR: return ui::CHAR_BOUNDARY;
927 case IA2_TEXT_BOUNDARY_WORD: return ui::WORD_BOUNDARY;
928 case IA2_TEXT_BOUNDARY_LINE: return ui::LINE_BOUNDARY;
929 case IA2_TEXT_BOUNDARY_SENTENCE: return ui::SENTENCE_BOUNDARY;
930 case IA2_TEXT_BOUNDARY_PARAGRAPH: return ui::PARAGRAPH_BOUNDARY;
931 case IA2_TEXT_BOUNDARY_ALL: return ui::ALL_BOUNDARY;
932 default:
933 NOTREACHED();
934 return ui::CHAR_BOUNDARY;
935 }
936 }
937
938 LONG AXPlatformNodeWin::FindBoundary(
939 const base::string16& text,
940 IA2TextBoundaryType ia2_boundary,
941 LONG start_offset,
942 ui::TextBoundaryDirection direction) {
943 HandleSpecialTextOffset(text, &start_offset);
944 ui::TextBoundaryType boundary = IA2TextBoundaryToTextBoundary(ia2_boundary);
945 std::vector<int32> line_breaks;
946 return ui::FindAccessibleTextBoundary(
947 text, line_breaks, boundary, start_offset, direction);
948 }
949
950 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698