Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_win.cc
blobb61f2f9c2be917dbd38371de7e6714c4dd2fa1cc
1 // Copyright (c) 2012 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 "content/browser/accessibility/browser_accessibility_win.h"
7 #include <UIAutomationClient.h>
8 #include <UIAutomationCoreApi.h>
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/enum_variant.h"
15 #include "base/win/scoped_comptr.h"
16 #include "base/win/windows_version.h"
17 #include "content/browser/accessibility/browser_accessibility_manager_win.h"
18 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
19 #include "content/common/accessibility_messages.h"
20 #include "content/public/common/content_client.h"
21 #include "ui/accessibility/ax_text_utils.h"
22 #include "ui/base/win/accessibility_ids_win.h"
23 #include "ui/base/win/accessibility_misc_utils.h"
24 #include "ui/base/win/atl_module.h"
26 namespace content {
28 // These nonstandard GUIDs are taken directly from the Mozilla sources
29 // (accessible/src/msaa/nsAccessNodeWrap.cpp); some documentation is here:
30 // http://developer.mozilla.org/en/Accessibility/AT-APIs/ImplementationFeatures/MSAA
31 const GUID GUID_ISimpleDOM = {
32 0x0c539790, 0x12e4, 0x11cf,
33 0xb6, 0x61, 0x00, 0xaa, 0x00, 0x4c, 0xd6, 0xd8};
34 const GUID GUID_IAccessibleContentDocument = {
35 0xa5d8e1f3, 0x3571, 0x4d8f,
36 0x95, 0x21, 0x07, 0xed, 0x28, 0xfb, 0x07, 0x2e};
38 const base::char16 BrowserAccessibilityWin::kEmbeddedCharacter = L'\xfffc';
40 // static
41 LONG BrowserAccessibilityWin::next_unique_id_win_ =
42 base::win::kFirstBrowserAccessibilityManagerAccessibilityId;
45 // BrowserAccessibilityRelation
47 // A simple implementation of IAccessibleRelation, used to represent
48 // a relationship between two accessible nodes in the tree.
51 class BrowserAccessibilityRelation
52 : public CComObjectRootEx<CComMultiThreadModel>,
53 public IAccessibleRelation {
54 BEGIN_COM_MAP(BrowserAccessibilityRelation)
55 COM_INTERFACE_ENTRY(IAccessibleRelation)
56 END_COM_MAP()
58 CONTENT_EXPORT BrowserAccessibilityRelation() {}
59 CONTENT_EXPORT virtual ~BrowserAccessibilityRelation() {}
61 CONTENT_EXPORT void Initialize(BrowserAccessibilityWin* owner,
62 const base::string16& type);
63 CONTENT_EXPORT void AddTarget(int target_id);
65 // IAccessibleRelation methods.
66 CONTENT_EXPORT STDMETHODIMP get_relationType(BSTR* relation_type);
67 CONTENT_EXPORT STDMETHODIMP get_nTargets(long* n_targets);
68 CONTENT_EXPORT STDMETHODIMP get_target(long target_index, IUnknown** target);
69 CONTENT_EXPORT STDMETHODIMP get_targets(long max_targets,
70 IUnknown** targets,
71 long* n_targets);
73 // IAccessibleRelation methods not implemented.
74 CONTENT_EXPORT STDMETHODIMP get_localizedRelationType(BSTR* relation_type) {
75 return E_NOTIMPL;
78 private:
79 base::string16 type_;
80 base::win::ScopedComPtr<BrowserAccessibilityWin> owner_;
81 std::vector<int> target_ids_;
84 void BrowserAccessibilityRelation::Initialize(BrowserAccessibilityWin* owner,
85 const base::string16& type) {
86 owner_ = owner;
87 type_ = type;
90 void BrowserAccessibilityRelation::AddTarget(int target_id) {
91 target_ids_.push_back(target_id);
94 STDMETHODIMP BrowserAccessibilityRelation::get_relationType(
95 BSTR* relation_type) {
96 if (!relation_type)
97 return E_INVALIDARG;
99 if (!owner_->instance_active())
100 return E_FAIL;
102 *relation_type = SysAllocString(type_.c_str());
103 DCHECK(*relation_type);
104 return S_OK;
107 STDMETHODIMP BrowserAccessibilityRelation::get_nTargets(long* n_targets) {
108 if (!n_targets)
109 return E_INVALIDARG;
111 if (!owner_->instance_active())
112 return E_FAIL;
114 *n_targets = static_cast<long>(target_ids_.size());
116 BrowserAccessibilityManager* manager = owner_->manager();
117 for (long i = *n_targets - 1; i >= 0; --i) {
118 BrowserAccessibility* result = manager->GetFromID(target_ids_[i]);
119 if (!result || !result->instance_active()) {
120 *n_targets = 0;
121 break;
124 return S_OK;
127 STDMETHODIMP BrowserAccessibilityRelation::get_target(long target_index,
128 IUnknown** target) {
129 if (!target)
130 return E_INVALIDARG;
132 if (!owner_->instance_active())
133 return E_FAIL;
135 if (target_index < 0 ||
136 target_index >= static_cast<long>(target_ids_.size())) {
137 return E_INVALIDARG;
140 BrowserAccessibilityManager* manager = owner_->manager();
141 BrowserAccessibility* result =
142 manager->GetFromID(target_ids_[target_index]);
143 if (!result || !result->instance_active())
144 return E_FAIL;
146 *target = static_cast<IAccessible*>(
147 result->ToBrowserAccessibilityWin()->NewReference());
148 return S_OK;
151 STDMETHODIMP BrowserAccessibilityRelation::get_targets(long max_targets,
152 IUnknown** targets,
153 long* n_targets) {
154 if (!targets || !n_targets)
155 return E_INVALIDARG;
157 if (!owner_->instance_active())
158 return E_FAIL;
160 long count = static_cast<long>(target_ids_.size());
161 if (count > max_targets)
162 count = max_targets;
164 *n_targets = count;
165 if (count == 0)
166 return S_FALSE;
168 for (long i = 0; i < count; ++i) {
169 HRESULT result = get_target(i, &targets[i]);
170 if (result != S_OK)
171 return result;
174 return S_OK;
178 // BrowserAccessibilityWin::WinAttributes
181 BrowserAccessibilityWin::WinAttributes::WinAttributes()
182 : ia_role(0),
183 ia_state(0),
184 ia2_role(0),
185 ia2_state(0) {
189 // BrowserAccessibilityWin
192 // static
193 BrowserAccessibility* BrowserAccessibility::Create() {
194 ui::win::CreateATLModuleIfNeeded();
195 CComObject<BrowserAccessibilityWin>* instance;
196 HRESULT hr = CComObject<BrowserAccessibilityWin>::CreateInstance(&instance);
197 DCHECK(SUCCEEDED(hr));
198 return instance->NewReference();
201 BrowserAccessibilityWin* BrowserAccessibility::ToBrowserAccessibilityWin() {
202 return static_cast<BrowserAccessibilityWin*>(this);
205 BrowserAccessibilityWin::BrowserAccessibilityWin()
206 : win_attributes_(new WinAttributes()),
207 previous_scroll_x_(0),
208 previous_scroll_y_(0) {
209 // Start unique IDs at -1 and decrement each time, because get_accChild
210 // uses positive IDs to enumerate children, so we use negative IDs to
211 // clearly distinguish between indices and unique IDs.
212 unique_id_win_ = next_unique_id_win_;
213 if (next_unique_id_win_ ==
214 base::win::kLastBrowserAccessibilityManagerAccessibilityId) {
215 next_unique_id_win_ =
216 base::win::kFirstBrowserAccessibilityManagerAccessibilityId;
218 next_unique_id_win_--;
221 BrowserAccessibilityWin::~BrowserAccessibilityWin() {
222 for (size_t i = 0; i < relations_.size(); ++i)
223 relations_[i]->Release();
227 // IAccessible methods.
229 // Conventions:
230 // * Always test for instance_active() first and return E_FAIL if it's false.
231 // * Always check for invalid arguments first, even if they're unused.
232 // * Return S_FALSE if the only output is a string argument and it's empty.
235 HRESULT BrowserAccessibilityWin::accDoDefaultAction(VARIANT var_id) {
236 if (!instance_active())
237 return E_FAIL;
239 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
240 if (!target)
241 return E_INVALIDARG;
243 manager()->DoDefaultAction(*target);
244 return S_OK;
247 STDMETHODIMP BrowserAccessibilityWin::accHitTest(LONG x_left,
248 LONG y_top,
249 VARIANT* child) {
250 if (!instance_active())
251 return E_FAIL;
253 if (!child)
254 return E_INVALIDARG;
256 gfx::Point point(x_left, y_top);
257 if (!GetGlobalBoundsRect().Contains(point)) {
258 // Return S_FALSE and VT_EMPTY when the outside the object's boundaries.
259 child->vt = VT_EMPTY;
260 return S_FALSE;
263 BrowserAccessibility* result = BrowserAccessibilityForPoint(point);
264 if (result == this) {
265 // Point is within this object.
266 child->vt = VT_I4;
267 child->lVal = CHILDID_SELF;
268 } else {
269 child->vt = VT_DISPATCH;
270 child->pdispVal = result->ToBrowserAccessibilityWin()->NewReference();
272 return S_OK;
275 STDMETHODIMP BrowserAccessibilityWin::accLocation(LONG* x_left,
276 LONG* y_top,
277 LONG* width,
278 LONG* height,
279 VARIANT var_id) {
280 if (!instance_active())
281 return E_FAIL;
283 if (!x_left || !y_top || !width || !height)
284 return E_INVALIDARG;
286 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
287 if (!target)
288 return E_INVALIDARG;
290 gfx::Rect bounds = target->GetGlobalBoundsRect();
291 *x_left = bounds.x();
292 *y_top = bounds.y();
293 *width = bounds.width();
294 *height = bounds.height();
296 return S_OK;
299 STDMETHODIMP BrowserAccessibilityWin::accNavigate(LONG nav_dir,
300 VARIANT start,
301 VARIANT* end) {
302 BrowserAccessibilityWin* target = GetTargetFromChildID(start);
303 if (!target)
304 return E_INVALIDARG;
306 if ((nav_dir == NAVDIR_LASTCHILD || nav_dir == NAVDIR_FIRSTCHILD) &&
307 start.lVal != CHILDID_SELF) {
308 // MSAA states that navigating to first/last child can only be from self.
309 return E_INVALIDARG;
312 uint32 child_count = target->PlatformChildCount();
314 BrowserAccessibility* result = NULL;
315 switch (nav_dir) {
316 case NAVDIR_DOWN:
317 case NAVDIR_UP:
318 case NAVDIR_LEFT:
319 case NAVDIR_RIGHT:
320 // These directions are not implemented, matching Mozilla and IE.
321 return E_NOTIMPL;
322 case NAVDIR_FIRSTCHILD:
323 if (child_count > 0)
324 result = target->PlatformGetChild(0);
325 break;
326 case NAVDIR_LASTCHILD:
327 if (child_count > 0)
328 result = target->PlatformGetChild(child_count - 1);
329 break;
330 case NAVDIR_NEXT:
331 result = target->GetNextSibling();
332 break;
333 case NAVDIR_PREVIOUS:
334 result = target->GetPreviousSibling();
335 break;
338 if (!result) {
339 end->vt = VT_EMPTY;
340 return S_FALSE;
343 end->vt = VT_DISPATCH;
344 end->pdispVal = result->ToBrowserAccessibilityWin()->NewReference();
345 return S_OK;
348 STDMETHODIMP BrowserAccessibilityWin::get_accChild(VARIANT var_child,
349 IDispatch** disp_child) {
350 if (!instance_active())
351 return E_FAIL;
353 if (!disp_child)
354 return E_INVALIDARG;
356 *disp_child = NULL;
358 BrowserAccessibilityWin* target = GetTargetFromChildID(var_child);
359 if (!target)
360 return E_INVALIDARG;
362 (*disp_child) = target->NewReference();
363 return S_OK;
366 STDMETHODIMP BrowserAccessibilityWin::get_accChildCount(LONG* child_count) {
367 if (!instance_active())
368 return E_FAIL;
370 if (!child_count)
371 return E_INVALIDARG;
373 *child_count = PlatformChildCount();
375 return S_OK;
378 STDMETHODIMP BrowserAccessibilityWin::get_accDefaultAction(VARIANT var_id,
379 BSTR* def_action) {
380 if (!instance_active())
381 return E_FAIL;
383 if (!def_action)
384 return E_INVALIDARG;
386 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
387 if (!target)
388 return E_INVALIDARG;
390 return target->GetStringAttributeAsBstr(
391 ui::AX_ATTR_ACTION, def_action);
394 STDMETHODIMP BrowserAccessibilityWin::get_accDescription(VARIANT var_id,
395 BSTR* desc) {
396 if (!instance_active())
397 return E_FAIL;
399 if (!desc)
400 return E_INVALIDARG;
402 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
403 if (!target)
404 return E_INVALIDARG;
406 base::string16 description_str = target->description();
407 if (description_str.empty())
408 return S_FALSE;
410 *desc = SysAllocString(description_str.c_str());
412 DCHECK(*desc);
413 return S_OK;
416 STDMETHODIMP BrowserAccessibilityWin::get_accFocus(VARIANT* focus_child) {
417 if (!instance_active())
418 return E_FAIL;
420 if (!focus_child)
421 return E_INVALIDARG;
423 BrowserAccessibilityWin* focus = static_cast<BrowserAccessibilityWin*>(
424 manager()->GetFocus(this));
425 if (focus == this) {
426 focus_child->vt = VT_I4;
427 focus_child->lVal = CHILDID_SELF;
428 } else if (focus == NULL) {
429 focus_child->vt = VT_EMPTY;
430 } else {
431 focus_child->vt = VT_DISPATCH;
432 focus_child->pdispVal = focus->NewReference();
435 return S_OK;
438 STDMETHODIMP BrowserAccessibilityWin::get_accHelp(VARIANT var_id, BSTR* help) {
439 if (!instance_active())
440 return E_FAIL;
442 if (!help)
443 return E_INVALIDARG;
445 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
446 if (!target)
447 return E_INVALIDARG;
449 base::string16 help_str = target->help();
450 if (help_str.empty())
451 return S_FALSE;
453 *help = SysAllocString(help_str.c_str());
455 DCHECK(*help);
456 return S_OK;
459 STDMETHODIMP BrowserAccessibilityWin::get_accKeyboardShortcut(VARIANT var_id,
460 BSTR* acc_key) {
461 if (!instance_active())
462 return E_FAIL;
464 if (!acc_key)
465 return E_INVALIDARG;
467 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
468 if (!target)
469 return E_INVALIDARG;
471 return target->GetStringAttributeAsBstr(
472 ui::AX_ATTR_SHORTCUT, acc_key);
475 STDMETHODIMP BrowserAccessibilityWin::get_accName(VARIANT var_id, BSTR* name) {
476 if (!instance_active())
477 return E_FAIL;
479 if (!name)
480 return E_INVALIDARG;
482 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
483 if (!target)
484 return E_INVALIDARG;
486 base::string16 name_str = target->name();
488 // If the name is empty, see if it's labeled by another element.
489 if (name_str.empty()) {
490 int title_elem_id;
491 if (target->GetIntAttribute(ui::AX_ATTR_TITLE_UI_ELEMENT,
492 &title_elem_id)) {
493 BrowserAccessibilityWin* title_elem =
494 manager()->GetFromID(title_elem_id)->ToBrowserAccessibilityWin();
495 if (title_elem)
496 name_str = title_elem->GetNameRecursive();
500 if (name_str.empty())
501 return S_FALSE;
503 *name = SysAllocString(name_str.c_str());
505 DCHECK(*name);
506 return S_OK;
509 STDMETHODIMP BrowserAccessibilityWin::get_accParent(IDispatch** disp_parent) {
510 if (!instance_active())
511 return E_FAIL;
513 if (!disp_parent)
514 return E_INVALIDARG;
516 IAccessible* parent_obj = GetParent()->ToBrowserAccessibilityWin();
517 if (parent_obj == NULL) {
518 // This happens if we're the root of the tree;
519 // return the IAccessible for the window.
520 parent_obj =
521 manager()->ToBrowserAccessibilityManagerWin()->GetParentIAccessible();
522 // |parent| can only be NULL if the manager was created before the parent
523 // IAccessible was known and it wasn't subsequently set before a client
524 // requested it. This has been fixed. |parent| may also be NULL during
525 // destruction. Possible cases where this could occur include tabs being
526 // dragged to a new window, etc.
527 if (!parent_obj) {
528 DVLOG(1) << "In Function: "
529 << __FUNCTION__
530 << ". Parent IAccessible interface is NULL. Returning failure";
531 return E_FAIL;
534 parent_obj->AddRef();
535 *disp_parent = parent_obj;
536 return S_OK;
539 STDMETHODIMP BrowserAccessibilityWin::get_accRole(VARIANT var_id,
540 VARIANT* role) {
541 if (!instance_active())
542 return E_FAIL;
544 if (!role)
545 return E_INVALIDARG;
547 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
548 if (!target)
549 return E_INVALIDARG;
551 if (!target->role_name().empty()) {
552 role->vt = VT_BSTR;
553 role->bstrVal = SysAllocString(target->role_name().c_str());
554 } else {
555 role->vt = VT_I4;
556 role->lVal = target->ia_role();
558 return S_OK;
561 STDMETHODIMP BrowserAccessibilityWin::get_accState(VARIANT var_id,
562 VARIANT* state) {
563 if (!instance_active())
564 return E_FAIL;
566 if (!state)
567 return E_INVALIDARG;
569 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
570 if (!target)
571 return E_INVALIDARG;
573 state->vt = VT_I4;
574 state->lVal = target->ia_state();
575 if (manager()->GetFocus(NULL) == this)
576 state->lVal |= STATE_SYSTEM_FOCUSED;
578 return S_OK;
581 STDMETHODIMP BrowserAccessibilityWin::get_accValue(VARIANT var_id,
582 BSTR* value) {
583 if (!instance_active())
584 return E_FAIL;
586 if (!value)
587 return E_INVALIDARG;
589 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
590 if (!target)
591 return E_INVALIDARG;
593 if (target->ia_role() == ROLE_SYSTEM_PROGRESSBAR ||
594 target->ia_role() == ROLE_SYSTEM_SCROLLBAR ||
595 target->ia_role() == ROLE_SYSTEM_SLIDER) {
596 base::string16 value_text = target->GetValueText();
597 *value = SysAllocString(value_text.c_str());
598 DCHECK(*value);
599 return S_OK;
602 // Expose color well value.
603 if (target->ia2_role() == IA2_ROLE_COLOR_CHOOSER) {
604 int r = target->GetIntAttribute(
605 ui::AX_ATTR_COLOR_VALUE_RED);
606 int g = target->GetIntAttribute(
607 ui::AX_ATTR_COLOR_VALUE_GREEN);
608 int b = target->GetIntAttribute(
609 ui::AX_ATTR_COLOR_VALUE_BLUE);
610 base::string16 value_text;
611 value_text = base::IntToString16((r * 100) / 255) + L"% red " +
612 base::IntToString16((g * 100) / 255) + L"% green " +
613 base::IntToString16((b * 100) / 255) + L"% blue";
614 *value = SysAllocString(value_text.c_str());
615 DCHECK(*value);
616 return S_OK;
619 *value = SysAllocString(target->value().c_str());
620 DCHECK(*value);
621 return S_OK;
624 STDMETHODIMP BrowserAccessibilityWin::get_accHelpTopic(BSTR* help_file,
625 VARIANT var_id,
626 LONG* topic_id) {
627 return E_NOTIMPL;
630 STDMETHODIMP BrowserAccessibilityWin::get_accSelection(VARIANT* selected) {
631 if (!instance_active())
632 return E_FAIL;
634 if (GetRole() != ui::AX_ROLE_LIST_BOX)
635 return E_NOTIMPL;
637 unsigned long selected_count = 0;
638 for (size_t i = 0; i < InternalChildCount(); ++i) {
639 if (InternalGetChild(i)->HasState(ui::AX_STATE_SELECTED))
640 ++selected_count;
643 if (selected_count == 0) {
644 selected->vt = VT_EMPTY;
645 return S_OK;
648 if (selected_count == 1) {
649 for (size_t i = 0; i < InternalChildCount(); ++i) {
650 if (InternalGetChild(i)->HasState(ui::AX_STATE_SELECTED)) {
651 selected->vt = VT_DISPATCH;
652 selected->pdispVal =
653 InternalGetChild(i)->ToBrowserAccessibilityWin()->NewReference();
654 return S_OK;
659 // Multiple items are selected.
660 base::win::EnumVariant* enum_variant =
661 new base::win::EnumVariant(selected_count);
662 enum_variant->AddRef();
663 unsigned long index = 0;
664 for (size_t i = 0; i < InternalChildCount(); ++i) {
665 if (InternalGetChild(i)->HasState(ui::AX_STATE_SELECTED)) {
666 enum_variant->ItemAt(index)->vt = VT_DISPATCH;
667 enum_variant->ItemAt(index)->pdispVal =
668 InternalGetChild(i)->ToBrowserAccessibilityWin()->NewReference();
669 ++index;
672 selected->vt = VT_UNKNOWN;
673 selected->punkVal = static_cast<IUnknown*>(
674 static_cast<base::win::IUnknownImpl*>(enum_variant));
675 return S_OK;
678 STDMETHODIMP BrowserAccessibilityWin::accSelect(
679 LONG flags_sel, VARIANT var_id) {
680 if (!instance_active())
681 return E_FAIL;
683 if (flags_sel & SELFLAG_TAKEFOCUS) {
684 manager()->SetFocus(this, true);
685 return S_OK;
688 return S_FALSE;
692 // IAccessible2 methods.
695 STDMETHODIMP BrowserAccessibilityWin::role(LONG* role) {
696 if (!instance_active())
697 return E_FAIL;
699 if (!role)
700 return E_INVALIDARG;
702 *role = ia2_role();
704 return S_OK;
707 STDMETHODIMP BrowserAccessibilityWin::get_attributes(BSTR* attributes) {
708 if (!instance_active())
709 return E_FAIL;
711 if (!attributes)
712 return E_INVALIDARG;
714 // The iaccessible2 attributes are a set of key-value pairs
715 // separated by semicolons, with a colon between the key and the value.
716 base::string16 str;
717 const std::vector<base::string16>& attributes_list = ia2_attributes();
718 for (unsigned int i = 0; i < attributes_list.size(); ++i) {
719 str += attributes_list[i] + L';';
722 if (str.empty())
723 return S_FALSE;
725 *attributes = SysAllocString(str.c_str());
726 DCHECK(*attributes);
727 return S_OK;
730 STDMETHODIMP BrowserAccessibilityWin::get_states(AccessibleStates* states) {
731 if (!instance_active())
732 return E_FAIL;
734 if (!states)
735 return E_INVALIDARG;
737 *states = ia2_state();
739 return S_OK;
742 STDMETHODIMP BrowserAccessibilityWin::get_uniqueID(LONG* unique_id) {
743 if (!instance_active())
744 return E_FAIL;
746 if (!unique_id)
747 return E_INVALIDARG;
749 *unique_id = unique_id_win_;
750 return S_OK;
753 STDMETHODIMP BrowserAccessibilityWin::get_windowHandle(HWND* window_handle) {
754 if (!instance_active())
755 return E_FAIL;
757 if (!window_handle)
758 return E_INVALIDARG;
760 *window_handle =
761 manager()->ToBrowserAccessibilityManagerWin()->GetParentHWND();
762 if (!*window_handle)
763 return E_FAIL;
765 return S_OK;
768 STDMETHODIMP BrowserAccessibilityWin::get_indexInParent(LONG* index_in_parent) {
769 if (!instance_active())
770 return E_FAIL;
772 if (!index_in_parent)
773 return E_INVALIDARG;
775 *index_in_parent = this->GetIndexInParent();
776 return S_OK;
779 STDMETHODIMP BrowserAccessibilityWin::get_nRelations(LONG* n_relations) {
780 if (!instance_active())
781 return E_FAIL;
783 if (!n_relations)
784 return E_INVALIDARG;
786 *n_relations = relations_.size();
787 return S_OK;
790 STDMETHODIMP BrowserAccessibilityWin::get_relation(
791 LONG relation_index,
792 IAccessibleRelation** relation) {
793 if (!instance_active())
794 return E_FAIL;
796 if (relation_index < 0 ||
797 relation_index >= static_cast<long>(relations_.size())) {
798 return E_INVALIDARG;
801 if (!relation)
802 return E_INVALIDARG;
804 relations_[relation_index]->AddRef();
805 *relation = relations_[relation_index];
806 return S_OK;
809 STDMETHODIMP BrowserAccessibilityWin::get_relations(
810 LONG max_relations,
811 IAccessibleRelation** relations,
812 LONG* n_relations) {
813 if (!instance_active())
814 return E_FAIL;
816 if (!relations || !n_relations)
817 return E_INVALIDARG;
819 long count = static_cast<long>(relations_.size());
820 *n_relations = count;
821 if (count == 0)
822 return S_FALSE;
824 for (long i = 0; i < count; ++i) {
825 relations_[i]->AddRef();
826 relations[i] = relations_[i];
829 return S_OK;
832 STDMETHODIMP BrowserAccessibilityWin::scrollTo(enum IA2ScrollType scroll_type) {
833 if (!instance_active())
834 return E_FAIL;
836 gfx::Rect r = GetLocation();
837 switch(scroll_type) {
838 case IA2_SCROLL_TYPE_TOP_LEFT:
839 manager()->ScrollToMakeVisible(*this, gfx::Rect(r.x(), r.y(), 0, 0));
840 break;
841 case IA2_SCROLL_TYPE_BOTTOM_RIGHT:
842 manager()->ScrollToMakeVisible(
843 *this, gfx::Rect(r.right(), r.bottom(), 0, 0));
844 break;
845 case IA2_SCROLL_TYPE_TOP_EDGE:
846 manager()->ScrollToMakeVisible(
847 *this, gfx::Rect(r.x(), r.y(), r.width(), 0));
848 break;
849 case IA2_SCROLL_TYPE_BOTTOM_EDGE:
850 manager()->ScrollToMakeVisible(
851 *this, gfx::Rect(r.x(), r.bottom(), r.width(), 0));
852 break;
853 case IA2_SCROLL_TYPE_LEFT_EDGE:
854 manager()->ScrollToMakeVisible(
855 *this, gfx::Rect(r.x(), r.y(), 0, r.height()));
856 break;
857 case IA2_SCROLL_TYPE_RIGHT_EDGE:
858 manager()->ScrollToMakeVisible(
859 *this, gfx::Rect(r.right(), r.y(), 0, r.height()));
860 break;
861 case IA2_SCROLL_TYPE_ANYWHERE:
862 default:
863 manager()->ScrollToMakeVisible(*this, r);
864 break;
867 manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
869 return S_OK;
872 STDMETHODIMP BrowserAccessibilityWin::scrollToPoint(
873 enum IA2CoordinateType coordinate_type,
874 LONG x,
875 LONG y) {
876 if (!instance_active())
877 return E_FAIL;
879 gfx::Point scroll_to(x, y);
881 if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
882 scroll_to -= manager()->GetViewBounds().OffsetFromOrigin();
883 } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
884 if (GetParent())
885 scroll_to += GetParent()->GetLocation().OffsetFromOrigin();
886 } else {
887 return E_INVALIDARG;
890 manager()->ScrollToPoint(*this, scroll_to);
891 manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
893 return S_OK;
896 STDMETHODIMP BrowserAccessibilityWin::get_groupPosition(
897 LONG* group_level,
898 LONG* similar_items_in_group,
899 LONG* position_in_group) {
900 if (!instance_active())
901 return E_FAIL;
903 if (!group_level || !similar_items_in_group || !position_in_group)
904 return E_INVALIDARG;
906 *group_level = 0;
907 *similar_items_in_group = GetIntAttribute(ui::AX_ATTR_SET_SIZE);
908 *position_in_group = GetIntAttribute(ui::AX_ATTR_POS_IN_SET);
909 return S_OK;
913 // IAccessibleApplication methods.
916 STDMETHODIMP BrowserAccessibilityWin::get_appName(BSTR* app_name) {
917 // No need to check |instance_active()| because this interface is
918 // global, and doesn't depend on any local state.
920 if (!app_name)
921 return E_INVALIDARG;
923 // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out
924 // the part before the "/".
925 std::vector<std::string> product_components;
926 base::SplitString(GetContentClient()->GetProduct(), '/', &product_components);
927 DCHECK_EQ(2U, product_components.size());
928 if (product_components.size() != 2)
929 return E_FAIL;
930 *app_name = SysAllocString(base::UTF8ToUTF16(product_components[0]).c_str());
931 DCHECK(*app_name);
932 return *app_name ? S_OK : E_FAIL;
935 STDMETHODIMP BrowserAccessibilityWin::get_appVersion(BSTR* app_version) {
936 // No need to check |instance_active()| because this interface is
937 // global, and doesn't depend on any local state.
939 if (!app_version)
940 return E_INVALIDARG;
942 // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out
943 // the part after the "/".
944 std::vector<std::string> product_components;
945 base::SplitString(GetContentClient()->GetProduct(), '/', &product_components);
946 DCHECK_EQ(2U, product_components.size());
947 if (product_components.size() != 2)
948 return E_FAIL;
949 *app_version =
950 SysAllocString(base::UTF8ToUTF16(product_components[1]).c_str());
951 DCHECK(*app_version);
952 return *app_version ? S_OK : E_FAIL;
955 STDMETHODIMP BrowserAccessibilityWin::get_toolkitName(BSTR* toolkit_name) {
956 // No need to check |instance_active()| because this interface is
957 // global, and doesn't depend on any local state.
959 if (!toolkit_name)
960 return E_INVALIDARG;
962 // This is hard-coded; all products based on the Chromium engine
963 // will have the same toolkit name, so that assistive technology can
964 // detect any Chrome-based product.
965 *toolkit_name = SysAllocString(L"Chrome");
966 DCHECK(*toolkit_name);
967 return *toolkit_name ? S_OK : E_FAIL;
970 STDMETHODIMP BrowserAccessibilityWin::get_toolkitVersion(
971 BSTR* toolkit_version) {
972 // No need to check |instance_active()| because this interface is
973 // global, and doesn't depend on any local state.
975 if (!toolkit_version)
976 return E_INVALIDARG;
978 std::string user_agent = GetContentClient()->GetUserAgent();
979 *toolkit_version = SysAllocString(base::UTF8ToUTF16(user_agent).c_str());
980 DCHECK(*toolkit_version);
981 return *toolkit_version ? S_OK : E_FAIL;
985 // IAccessibleImage methods.
988 STDMETHODIMP BrowserAccessibilityWin::get_description(BSTR* desc) {
989 if (!instance_active())
990 return E_FAIL;
992 if (!desc)
993 return E_INVALIDARG;
995 if (description().empty())
996 return S_FALSE;
998 *desc = SysAllocString(description().c_str());
1000 DCHECK(*desc);
1001 return S_OK;
1004 STDMETHODIMP BrowserAccessibilityWin::get_imagePosition(
1005 enum IA2CoordinateType coordinate_type,
1006 LONG* x,
1007 LONG* y) {
1008 if (!instance_active())
1009 return E_FAIL;
1011 if (!x || !y)
1012 return E_INVALIDARG;
1014 if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
1015 HWND parent_hwnd =
1016 manager()->ToBrowserAccessibilityManagerWin()->GetParentHWND();
1017 if (!parent_hwnd)
1018 return E_FAIL;
1019 POINT top_left = {0, 0};
1020 ::ClientToScreen(parent_hwnd, &top_left);
1021 *x = GetLocation().x() + top_left.x;
1022 *y = GetLocation().y() + top_left.y;
1023 } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
1024 *x = GetLocation().x();
1025 *y = GetLocation().y();
1026 if (GetParent()) {
1027 *x -= GetParent()->GetLocation().x();
1028 *y -= GetParent()->GetLocation().y();
1030 } else {
1031 return E_INVALIDARG;
1034 return S_OK;
1037 STDMETHODIMP BrowserAccessibilityWin::get_imageSize(LONG* height, LONG* width) {
1038 if (!instance_active())
1039 return E_FAIL;
1041 if (!height || !width)
1042 return E_INVALIDARG;
1044 *height = GetLocation().height();
1045 *width = GetLocation().width();
1046 return S_OK;
1050 // IAccessibleTable methods.
1053 STDMETHODIMP BrowserAccessibilityWin::get_accessibleAt(
1054 long row,
1055 long column,
1056 IUnknown** accessible) {
1057 if (!instance_active())
1058 return E_FAIL;
1060 if (!accessible)
1061 return E_INVALIDARG;
1063 int columns;
1064 int rows;
1065 if (!GetIntAttribute(
1066 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1067 !GetIntAttribute(
1068 ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1069 columns <= 0 ||
1070 rows <= 0) {
1071 return S_FALSE;
1074 if (row < 0 || row >= rows || column < 0 || column >= columns)
1075 return E_INVALIDARG;
1077 const std::vector<int32>& cell_ids = GetIntListAttribute(
1078 ui::AX_ATTR_CELL_IDS);
1079 DCHECK_EQ(columns * rows, static_cast<int>(cell_ids.size()));
1081 int cell_id = cell_ids[row * columns + column];
1082 BrowserAccessibilityWin* cell = GetFromID(cell_id);
1083 if (cell) {
1084 *accessible = static_cast<IAccessible*>(cell->NewReference());
1085 return S_OK;
1088 *accessible = NULL;
1089 return E_INVALIDARG;
1092 STDMETHODIMP BrowserAccessibilityWin::get_caption(IUnknown** accessible) {
1093 if (!instance_active())
1094 return E_FAIL;
1096 if (!accessible)
1097 return E_INVALIDARG;
1099 // TODO(dmazzoni): implement
1100 return S_FALSE;
1103 STDMETHODIMP BrowserAccessibilityWin::get_childIndex(long row,
1104 long column,
1105 long* cell_index) {
1106 if (!instance_active())
1107 return E_FAIL;
1109 if (!cell_index)
1110 return E_INVALIDARG;
1112 int columns;
1113 int rows;
1114 if (!GetIntAttribute(
1115 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1116 !GetIntAttribute(
1117 ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1118 columns <= 0 ||
1119 rows <= 0) {
1120 return S_FALSE;
1123 if (row < 0 || row >= rows || column < 0 || column >= columns)
1124 return E_INVALIDARG;
1126 const std::vector<int32>& cell_ids = GetIntListAttribute(
1127 ui::AX_ATTR_CELL_IDS);
1128 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1129 ui::AX_ATTR_UNIQUE_CELL_IDS);
1130 DCHECK_EQ(columns * rows, static_cast<int>(cell_ids.size()));
1131 int cell_id = cell_ids[row * columns + column];
1132 for (size_t i = 0; i < unique_cell_ids.size(); ++i) {
1133 if (unique_cell_ids[i] == cell_id) {
1134 *cell_index = (long)i;
1135 return S_OK;
1139 return S_FALSE;
1142 STDMETHODIMP BrowserAccessibilityWin::get_columnDescription(long column,
1143 BSTR* description) {
1144 if (!instance_active())
1145 return E_FAIL;
1147 if (!description)
1148 return E_INVALIDARG;
1150 int columns;
1151 int rows;
1152 if (!GetIntAttribute(
1153 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1154 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1155 columns <= 0 ||
1156 rows <= 0) {
1157 return S_FALSE;
1160 if (column < 0 || column >= columns)
1161 return E_INVALIDARG;
1163 const std::vector<int32>& cell_ids = GetIntListAttribute(
1164 ui::AX_ATTR_CELL_IDS);
1165 for (int i = 0; i < rows; ++i) {
1166 int cell_id = cell_ids[i * columns + column];
1167 BrowserAccessibilityWin* cell = static_cast<BrowserAccessibilityWin*>(
1168 manager()->GetFromID(cell_id));
1169 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER) {
1170 base::string16 cell_name = cell->GetString16Attribute(
1171 ui::AX_ATTR_NAME);
1172 if (cell_name.size() > 0) {
1173 *description = SysAllocString(cell_name.c_str());
1174 return S_OK;
1177 if (cell->description().size() > 0) {
1178 *description = SysAllocString(cell->description().c_str());
1179 return S_OK;
1184 return S_FALSE;
1187 STDMETHODIMP BrowserAccessibilityWin::get_columnExtentAt(
1188 long row,
1189 long column,
1190 long* n_columns_spanned) {
1191 if (!instance_active())
1192 return E_FAIL;
1194 if (!n_columns_spanned)
1195 return E_INVALIDARG;
1197 int columns;
1198 int rows;
1199 if (!GetIntAttribute(
1200 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1201 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1202 columns <= 0 ||
1203 rows <= 0) {
1204 return S_FALSE;
1207 if (row < 0 || row >= rows || column < 0 || column >= columns)
1208 return E_INVALIDARG;
1210 const std::vector<int32>& cell_ids = GetIntListAttribute(
1211 ui::AX_ATTR_CELL_IDS);
1212 int cell_id = cell_ids[row * columns + column];
1213 BrowserAccessibilityWin* cell = static_cast<BrowserAccessibilityWin*>(
1214 manager()->GetFromID(cell_id));
1215 int colspan;
1216 if (cell &&
1217 cell->GetIntAttribute(
1218 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1219 colspan >= 1) {
1220 *n_columns_spanned = colspan;
1221 return S_OK;
1224 return S_FALSE;
1227 STDMETHODIMP BrowserAccessibilityWin::get_columnHeader(
1228 IAccessibleTable** accessible_table,
1229 long* starting_row_index) {
1230 // TODO(dmazzoni): implement
1231 return E_NOTIMPL;
1234 STDMETHODIMP BrowserAccessibilityWin::get_columnIndex(long cell_index,
1235 long* column_index) {
1236 if (!instance_active())
1237 return E_FAIL;
1239 if (!column_index)
1240 return E_INVALIDARG;
1242 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1243 ui::AX_ATTR_UNIQUE_CELL_IDS);
1244 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1245 if (cell_index < 0)
1246 return E_INVALIDARG;
1247 if (cell_index >= cell_id_count)
1248 return S_FALSE;
1250 int cell_id = unique_cell_ids[cell_index];
1251 BrowserAccessibilityWin* cell =
1252 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1253 int col_index;
1254 if (cell &&
1255 cell->GetIntAttribute(
1256 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &col_index)) {
1257 *column_index = col_index;
1258 return S_OK;
1261 return S_FALSE;
1264 STDMETHODIMP BrowserAccessibilityWin::get_nColumns(long* column_count) {
1265 if (!instance_active())
1266 return E_FAIL;
1268 if (!column_count)
1269 return E_INVALIDARG;
1271 int columns;
1272 if (GetIntAttribute(
1273 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns)) {
1274 *column_count = columns;
1275 return S_OK;
1278 return S_FALSE;
1281 STDMETHODIMP BrowserAccessibilityWin::get_nRows(long* row_count) {
1282 if (!instance_active())
1283 return E_FAIL;
1285 if (!row_count)
1286 return E_INVALIDARG;
1288 int rows;
1289 if (GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1290 *row_count = rows;
1291 return S_OK;
1294 return S_FALSE;
1297 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedChildren(long* cell_count) {
1298 if (!instance_active())
1299 return E_FAIL;
1301 if (!cell_count)
1302 return E_INVALIDARG;
1304 // TODO(dmazzoni): add support for selected cells/rows/columns in tables.
1305 *cell_count = 0;
1306 return S_OK;
1309 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedColumns(long* column_count) {
1310 if (!instance_active())
1311 return E_FAIL;
1313 if (!column_count)
1314 return E_INVALIDARG;
1316 *column_count = 0;
1317 return S_OK;
1320 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedRows(long* row_count) {
1321 if (!instance_active())
1322 return E_FAIL;
1324 if (!row_count)
1325 return E_INVALIDARG;
1327 *row_count = 0;
1328 return S_OK;
1331 STDMETHODIMP BrowserAccessibilityWin::get_rowDescription(long row,
1332 BSTR* description) {
1333 if (!instance_active())
1334 return E_FAIL;
1336 if (!description)
1337 return E_INVALIDARG;
1339 int columns;
1340 int rows;
1341 if (!GetIntAttribute(
1342 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1343 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1344 columns <= 0 ||
1345 rows <= 0) {
1346 return S_FALSE;
1349 if (row < 0 || row >= rows)
1350 return E_INVALIDARG;
1352 const std::vector<int32>& cell_ids = GetIntListAttribute(
1353 ui::AX_ATTR_CELL_IDS);
1354 for (int i = 0; i < columns; ++i) {
1355 int cell_id = cell_ids[row * columns + i];
1356 BrowserAccessibilityWin* cell =
1357 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1358 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER) {
1359 base::string16 cell_name = cell->GetString16Attribute(
1360 ui::AX_ATTR_NAME);
1361 if (cell_name.size() > 0) {
1362 *description = SysAllocString(cell_name.c_str());
1363 return S_OK;
1366 if (cell->description().size() > 0) {
1367 *description = SysAllocString(cell->description().c_str());
1368 return S_OK;
1373 return S_FALSE;
1376 STDMETHODIMP BrowserAccessibilityWin::get_rowExtentAt(long row,
1377 long column,
1378 long* n_rows_spanned) {
1379 if (!instance_active())
1380 return E_FAIL;
1382 if (!n_rows_spanned)
1383 return E_INVALIDARG;
1385 int columns;
1386 int rows;
1387 if (!GetIntAttribute(
1388 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1389 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1390 columns <= 0 ||
1391 rows <= 0) {
1392 return S_FALSE;
1395 if (row < 0 || row >= rows || column < 0 || column >= columns)
1396 return E_INVALIDARG;
1398 const std::vector<int32>& cell_ids = GetIntListAttribute(
1399 ui::AX_ATTR_CELL_IDS);
1400 int cell_id = cell_ids[row * columns + column];
1401 BrowserAccessibilityWin* cell =
1402 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1403 int rowspan;
1404 if (cell &&
1405 cell->GetIntAttribute(
1406 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1407 rowspan >= 1) {
1408 *n_rows_spanned = rowspan;
1409 return S_OK;
1412 return S_FALSE;
1415 STDMETHODIMP BrowserAccessibilityWin::get_rowHeader(
1416 IAccessibleTable** accessible_table,
1417 long* starting_column_index) {
1418 // TODO(dmazzoni): implement
1419 return E_NOTIMPL;
1422 STDMETHODIMP BrowserAccessibilityWin::get_rowIndex(long cell_index,
1423 long* row_index) {
1424 if (!instance_active())
1425 return E_FAIL;
1427 if (!row_index)
1428 return E_INVALIDARG;
1430 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1431 ui::AX_ATTR_UNIQUE_CELL_IDS);
1432 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1433 if (cell_index < 0)
1434 return E_INVALIDARG;
1435 if (cell_index >= cell_id_count)
1436 return S_FALSE;
1438 int cell_id = unique_cell_ids[cell_index];
1439 BrowserAccessibilityWin* cell =
1440 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1441 int cell_row_index;
1442 if (cell &&
1443 cell->GetIntAttribute(
1444 ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &cell_row_index)) {
1445 *row_index = cell_row_index;
1446 return S_OK;
1449 return S_FALSE;
1452 STDMETHODIMP BrowserAccessibilityWin::get_selectedChildren(long max_children,
1453 long** children,
1454 long* n_children) {
1455 if (!instance_active())
1456 return E_FAIL;
1458 if (!children || !n_children)
1459 return E_INVALIDARG;
1461 // TODO(dmazzoni): Implement this.
1462 *n_children = 0;
1463 return S_OK;
1466 STDMETHODIMP BrowserAccessibilityWin::get_selectedColumns(long max_columns,
1467 long** columns,
1468 long* n_columns) {
1469 if (!instance_active())
1470 return E_FAIL;
1472 if (!columns || !n_columns)
1473 return E_INVALIDARG;
1475 // TODO(dmazzoni): Implement this.
1476 *n_columns = 0;
1477 return S_OK;
1480 STDMETHODIMP BrowserAccessibilityWin::get_selectedRows(long max_rows,
1481 long** rows,
1482 long* n_rows) {
1483 if (!instance_active())
1484 return E_FAIL;
1486 if (!rows || !n_rows)
1487 return E_INVALIDARG;
1489 // TODO(dmazzoni): Implement this.
1490 *n_rows = 0;
1491 return S_OK;
1494 STDMETHODIMP BrowserAccessibilityWin::get_summary(IUnknown** accessible) {
1495 if (!instance_active())
1496 return E_FAIL;
1498 if (!accessible)
1499 return E_INVALIDARG;
1501 // TODO(dmazzoni): implement
1502 return S_FALSE;
1505 STDMETHODIMP BrowserAccessibilityWin::get_isColumnSelected(
1506 long column,
1507 boolean* is_selected) {
1508 if (!instance_active())
1509 return E_FAIL;
1511 if (!is_selected)
1512 return E_INVALIDARG;
1514 // TODO(dmazzoni): Implement this.
1515 *is_selected = false;
1516 return S_OK;
1519 STDMETHODIMP BrowserAccessibilityWin::get_isRowSelected(long row,
1520 boolean* is_selected) {
1521 if (!instance_active())
1522 return E_FAIL;
1524 if (!is_selected)
1525 return E_INVALIDARG;
1527 // TODO(dmazzoni): Implement this.
1528 *is_selected = false;
1529 return S_OK;
1532 STDMETHODIMP BrowserAccessibilityWin::get_isSelected(long row,
1533 long column,
1534 boolean* is_selected) {
1535 if (!instance_active())
1536 return E_FAIL;
1538 if (!is_selected)
1539 return E_INVALIDARG;
1541 // TODO(dmazzoni): Implement this.
1542 *is_selected = false;
1543 return S_OK;
1546 STDMETHODIMP BrowserAccessibilityWin::get_rowColumnExtentsAtIndex(
1547 long index,
1548 long* row,
1549 long* column,
1550 long* row_extents,
1551 long* column_extents,
1552 boolean* is_selected) {
1553 if (!instance_active())
1554 return E_FAIL;
1556 if (!row || !column || !row_extents || !column_extents || !is_selected)
1557 return E_INVALIDARG;
1559 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1560 ui::AX_ATTR_UNIQUE_CELL_IDS);
1561 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1562 if (index < 0)
1563 return E_INVALIDARG;
1564 if (index >= cell_id_count)
1565 return S_FALSE;
1567 int cell_id = unique_cell_ids[index];
1568 BrowserAccessibilityWin* cell =
1569 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1570 int rowspan;
1571 int colspan;
1572 if (cell &&
1573 cell->GetIntAttribute(
1574 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1575 cell->GetIntAttribute(
1576 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1577 rowspan >= 1 &&
1578 colspan >= 1) {
1579 *row_extents = rowspan;
1580 *column_extents = colspan;
1581 return S_OK;
1584 return S_FALSE;
1588 // IAccessibleTable2 methods.
1591 STDMETHODIMP BrowserAccessibilityWin::get_cellAt(long row,
1592 long column,
1593 IUnknown** cell) {
1594 return get_accessibleAt(row, column, cell);
1597 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedCells(long* cell_count) {
1598 return get_nSelectedChildren(cell_count);
1601 STDMETHODIMP BrowserAccessibilityWin::get_selectedCells(
1602 IUnknown*** cells,
1603 long* n_selected_cells) {
1604 if (!instance_active())
1605 return E_FAIL;
1607 if (!cells || !n_selected_cells)
1608 return E_INVALIDARG;
1610 // TODO(dmazzoni): Implement this.
1611 *n_selected_cells = 0;
1612 return S_OK;
1615 STDMETHODIMP BrowserAccessibilityWin::get_selectedColumns(long** columns,
1616 long* n_columns) {
1617 if (!instance_active())
1618 return E_FAIL;
1620 if (!columns || !n_columns)
1621 return E_INVALIDARG;
1623 // TODO(dmazzoni): Implement this.
1624 *n_columns = 0;
1625 return S_OK;
1628 STDMETHODIMP BrowserAccessibilityWin::get_selectedRows(long** rows,
1629 long* n_rows) {
1630 if (!instance_active())
1631 return E_FAIL;
1633 if (!rows || !n_rows)
1634 return E_INVALIDARG;
1636 // TODO(dmazzoni): Implement this.
1637 *n_rows = 0;
1638 return S_OK;
1643 // IAccessibleTableCell methods.
1646 STDMETHODIMP BrowserAccessibilityWin::get_columnExtent(
1647 long* n_columns_spanned) {
1648 if (!instance_active())
1649 return E_FAIL;
1651 if (!n_columns_spanned)
1652 return E_INVALIDARG;
1654 int colspan;
1655 if (GetIntAttribute(
1656 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1657 colspan >= 1) {
1658 *n_columns_spanned = colspan;
1659 return S_OK;
1662 return S_FALSE;
1665 STDMETHODIMP BrowserAccessibilityWin::get_columnHeaderCells(
1666 IUnknown*** cell_accessibles,
1667 long* n_column_header_cells) {
1668 if (!instance_active())
1669 return E_FAIL;
1671 if (!cell_accessibles || !n_column_header_cells)
1672 return E_INVALIDARG;
1674 *n_column_header_cells = 0;
1676 int column;
1677 if (!GetIntAttribute(
1678 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column)) {
1679 return S_FALSE;
1682 BrowserAccessibility* table = GetParent();
1683 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
1684 table = table->GetParent();
1685 if (!table) {
1686 NOTREACHED();
1687 return S_FALSE;
1690 int columns;
1691 int rows;
1692 if (!table->GetIntAttribute(
1693 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1694 !table->GetIntAttribute(
1695 ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1696 return S_FALSE;
1698 if (columns <= 0 || rows <= 0 || column < 0 || column >= columns)
1699 return S_FALSE;
1701 const std::vector<int32>& cell_ids = table->GetIntListAttribute(
1702 ui::AX_ATTR_CELL_IDS);
1704 for (int i = 0; i < rows; ++i) {
1705 int cell_id = cell_ids[i * columns + column];
1706 BrowserAccessibilityWin* cell =
1707 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1708 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER)
1709 (*n_column_header_cells)++;
1712 *cell_accessibles = static_cast<IUnknown**>(CoTaskMemAlloc(
1713 (*n_column_header_cells) * sizeof(cell_accessibles[0])));
1714 int index = 0;
1715 for (int i = 0; i < rows; ++i) {
1716 int cell_id = cell_ids[i * columns + column];
1717 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1718 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER) {
1719 (*cell_accessibles)[index] = static_cast<IAccessible*>(
1720 cell->ToBrowserAccessibilityWin()->NewReference());
1721 ++index;
1725 return S_OK;
1728 STDMETHODIMP BrowserAccessibilityWin::get_columnIndex(long* column_index) {
1729 if (!instance_active())
1730 return E_FAIL;
1732 if (!column_index)
1733 return E_INVALIDARG;
1735 int column;
1736 if (GetIntAttribute(
1737 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column)) {
1738 *column_index = column;
1739 return S_OK;
1742 return S_FALSE;
1745 STDMETHODIMP BrowserAccessibilityWin::get_rowExtent(long* n_rows_spanned) {
1746 if (!instance_active())
1747 return E_FAIL;
1749 if (!n_rows_spanned)
1750 return E_INVALIDARG;
1752 int rowspan;
1753 if (GetIntAttribute(
1754 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1755 rowspan >= 1) {
1756 *n_rows_spanned = rowspan;
1757 return S_OK;
1760 return S_FALSE;
1763 STDMETHODIMP BrowserAccessibilityWin::get_rowHeaderCells(
1764 IUnknown*** cell_accessibles,
1765 long* n_row_header_cells) {
1766 if (!instance_active())
1767 return E_FAIL;
1769 if (!cell_accessibles || !n_row_header_cells)
1770 return E_INVALIDARG;
1772 *n_row_header_cells = 0;
1774 int row;
1775 if (!GetIntAttribute(
1776 ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row)) {
1777 return S_FALSE;
1780 BrowserAccessibility* table = GetParent();
1781 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
1782 table = table->GetParent();
1783 if (!table) {
1784 NOTREACHED();
1785 return S_FALSE;
1788 int columns;
1789 int rows;
1790 if (!table->GetIntAttribute(
1791 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1792 !table->GetIntAttribute(
1793 ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1794 return S_FALSE;
1796 if (columns <= 0 || rows <= 0 || row < 0 || row >= rows)
1797 return S_FALSE;
1799 const std::vector<int32>& cell_ids = table->GetIntListAttribute(
1800 ui::AX_ATTR_CELL_IDS);
1802 for (int i = 0; i < columns; ++i) {
1803 int cell_id = cell_ids[row * columns + i];
1804 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1805 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER)
1806 (*n_row_header_cells)++;
1809 *cell_accessibles = static_cast<IUnknown**>(CoTaskMemAlloc(
1810 (*n_row_header_cells) * sizeof(cell_accessibles[0])));
1811 int index = 0;
1812 for (int i = 0; i < columns; ++i) {
1813 int cell_id = cell_ids[row * columns + i];
1814 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1815 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER) {
1816 (*cell_accessibles)[index] = static_cast<IAccessible*>(
1817 cell->ToBrowserAccessibilityWin()->NewReference());
1818 ++index;
1822 return S_OK;
1825 STDMETHODIMP BrowserAccessibilityWin::get_rowIndex(long* row_index) {
1826 if (!instance_active())
1827 return E_FAIL;
1829 if (!row_index)
1830 return E_INVALIDARG;
1832 int row;
1833 if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row)) {
1834 *row_index = row;
1835 return S_OK;
1837 return S_FALSE;
1840 STDMETHODIMP BrowserAccessibilityWin::get_isSelected(boolean* is_selected) {
1841 if (!instance_active())
1842 return E_FAIL;
1844 if (!is_selected)
1845 return E_INVALIDARG;
1847 *is_selected = false;
1848 return S_OK;
1851 STDMETHODIMP BrowserAccessibilityWin::get_rowColumnExtents(
1852 long* row_index,
1853 long* column_index,
1854 long* row_extents,
1855 long* column_extents,
1856 boolean* is_selected) {
1857 if (!instance_active())
1858 return E_FAIL;
1860 if (!row_index ||
1861 !column_index ||
1862 !row_extents ||
1863 !column_extents ||
1864 !is_selected) {
1865 return E_INVALIDARG;
1868 int row;
1869 int column;
1870 int rowspan;
1871 int colspan;
1872 if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row) &&
1873 GetIntAttribute(
1874 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column) &&
1875 GetIntAttribute(
1876 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1877 GetIntAttribute(
1878 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan)) {
1879 *row_index = row;
1880 *column_index = column;
1881 *row_extents = rowspan;
1882 *column_extents = colspan;
1883 *is_selected = false;
1884 return S_OK;
1887 return S_FALSE;
1890 STDMETHODIMP BrowserAccessibilityWin::get_table(IUnknown** table) {
1891 if (!instance_active())
1892 return E_FAIL;
1894 if (!table)
1895 return E_INVALIDARG;
1898 int row;
1899 int column;
1900 GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row);
1901 GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column);
1903 BrowserAccessibility* find_table = GetParent();
1904 while (find_table && find_table->GetRole() != ui::AX_ROLE_TABLE)
1905 find_table = find_table->GetParent();
1906 if (!find_table) {
1907 NOTREACHED();
1908 return S_FALSE;
1911 *table = static_cast<IAccessibleTable*>(
1912 find_table->ToBrowserAccessibilityWin()->NewReference());
1914 return S_OK;
1918 // IAccessibleText methods.
1921 STDMETHODIMP BrowserAccessibilityWin::get_nCharacters(LONG* n_characters) {
1922 if (!instance_active())
1923 return E_FAIL;
1925 if (!n_characters)
1926 return E_INVALIDARG;
1928 *n_characters = TextForIAccessibleText().length();
1929 return S_OK;
1932 STDMETHODIMP BrowserAccessibilityWin::get_caretOffset(LONG* offset) {
1933 if (!instance_active())
1934 return E_FAIL;
1936 if (!offset)
1937 return E_INVALIDARG;
1939 // IA2 spec says that caret offset should be -1 if the object is not focused.
1940 if (manager()->GetFocus(this) != this) {
1941 *offset = -1;
1942 return S_FALSE;
1945 *offset = 0;
1946 if (IsEditableText()) {
1947 int sel_start = 0;
1948 if (GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START,
1949 &sel_start))
1950 *offset = sel_start;
1953 return S_OK;
1956 STDMETHODIMP BrowserAccessibilityWin::get_characterExtents(
1957 LONG offset,
1958 enum IA2CoordinateType coordinate_type,
1959 LONG* out_x,
1960 LONG* out_y,
1961 LONG* out_width,
1962 LONG* out_height) {
1963 if (!instance_active())
1964 return E_FAIL;
1966 if (!out_x || !out_y || !out_width || !out_height)
1967 return E_INVALIDARG;
1969 const base::string16& text_str = TextForIAccessibleText();
1970 HandleSpecialTextOffset(text_str, &offset);
1972 if (offset < 0 || offset > static_cast<LONG>(text_str.size()))
1973 return E_INVALIDARG;
1975 gfx::Rect character_bounds;
1976 if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
1977 character_bounds = GetGlobalBoundsForRange(offset, 1);
1978 } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
1979 character_bounds = GetLocalBoundsForRange(offset, 1);
1980 character_bounds -= GetLocation().OffsetFromOrigin();
1981 } else {
1982 return E_INVALIDARG;
1985 *out_x = character_bounds.x();
1986 *out_y = character_bounds.y();
1987 *out_width = character_bounds.width();
1988 *out_height = character_bounds.height();
1990 return S_OK;
1993 STDMETHODIMP BrowserAccessibilityWin::get_nSelections(LONG* n_selections) {
1994 if (!instance_active())
1995 return E_FAIL;
1997 if (!n_selections)
1998 return E_INVALIDARG;
2000 *n_selections = 0;
2001 if (IsEditableText()) {
2002 int sel_start = 0;
2003 int sel_end = 0;
2004 if (GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START,
2005 &sel_start) &&
2006 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end) &&
2007 sel_start != sel_end)
2008 *n_selections = 1;
2011 return S_OK;
2014 STDMETHODIMP BrowserAccessibilityWin::get_selection(LONG selection_index,
2015 LONG* start_offset,
2016 LONG* end_offset) {
2017 if (!instance_active())
2018 return E_FAIL;
2020 if (!start_offset || !end_offset || selection_index != 0)
2021 return E_INVALIDARG;
2023 LONG n_selections = 0;
2024 if (FAILED(get_nSelections(&n_selections)) || n_selections < 1)
2025 return E_INVALIDARG;
2027 *start_offset = 0;
2028 *end_offset = 0;
2029 if (IsEditableText()) {
2030 int sel_start = 0;
2031 int sel_end = 0;
2032 if (GetIntAttribute(
2033 ui::AX_ATTR_TEXT_SEL_START, &sel_start) &&
2034 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end)) {
2035 *start_offset = sel_start;
2036 *end_offset = sel_end;
2040 return S_OK;
2043 STDMETHODIMP BrowserAccessibilityWin::get_text(LONG start_offset,
2044 LONG end_offset,
2045 BSTR* text) {
2046 if (!instance_active())
2047 return E_FAIL;
2049 if (!text)
2050 return E_INVALIDARG;
2052 const base::string16& text_str = TextForIAccessibleText();
2054 // Handle special text offsets.
2055 HandleSpecialTextOffset(text_str, &start_offset);
2056 HandleSpecialTextOffset(text_str, &end_offset);
2058 // The spec allows the arguments to be reversed.
2059 if (start_offset > end_offset) {
2060 LONG tmp = start_offset;
2061 start_offset = end_offset;
2062 end_offset = tmp;
2065 // The spec does not allow the start or end offsets to be out or range;
2066 // we must return an error if so.
2067 LONG len = text_str.length();
2068 if (start_offset < 0)
2069 return E_INVALIDARG;
2070 if (end_offset > len)
2071 return E_INVALIDARG;
2073 base::string16 substr = text_str.substr(start_offset,
2074 end_offset - start_offset);
2075 if (substr.empty())
2076 return S_FALSE;
2078 *text = SysAllocString(substr.c_str());
2079 DCHECK(*text);
2080 return S_OK;
2083 STDMETHODIMP BrowserAccessibilityWin::get_textAtOffset(
2084 LONG offset,
2085 enum IA2TextBoundaryType boundary_type,
2086 LONG* start_offset,
2087 LONG* end_offset,
2088 BSTR* text) {
2089 if (!instance_active())
2090 return E_FAIL;
2092 if (!start_offset || !end_offset || !text)
2093 return E_INVALIDARG;
2095 const base::string16& text_str = TextForIAccessibleText();
2096 HandleSpecialTextOffset(text_str, &offset);
2097 if (offset < 0)
2098 return E_INVALIDARG;
2100 LONG text_len = text_str.length();
2101 if (offset > text_len)
2102 return E_INVALIDARG;
2104 // The IAccessible2 spec says we don't have to implement the "sentence"
2105 // boundary type, we can just let the screenreader handle it.
2106 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2107 *start_offset = 0;
2108 *end_offset = 0;
2109 *text = NULL;
2110 return S_FALSE;
2113 // According to the IA2 Spec, only line boundaries should succeed when
2114 // the offset is one past the end of the text.
2115 if (offset == text_len && boundary_type != IA2_TEXT_BOUNDARY_LINE) {
2116 *start_offset = 0;
2117 *end_offset = 0;
2118 *text = nullptr;
2119 return S_FALSE;
2122 *start_offset = FindBoundary(
2123 text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
2124 *end_offset = FindBoundary(
2125 text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
2126 return get_text(*start_offset, *end_offset, text);
2129 STDMETHODIMP BrowserAccessibilityWin::get_textBeforeOffset(
2130 LONG offset,
2131 enum IA2TextBoundaryType boundary_type,
2132 LONG* start_offset,
2133 LONG* end_offset,
2134 BSTR* text) {
2135 if (!instance_active())
2136 return E_FAIL;
2138 if (!start_offset || !end_offset || !text)
2139 return E_INVALIDARG;
2141 // The IAccessible2 spec says we don't have to implement the "sentence"
2142 // boundary type, we can just let the screenreader handle it.
2143 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2144 *start_offset = 0;
2145 *end_offset = 0;
2146 *text = NULL;
2147 return S_FALSE;
2150 const base::string16& text_str = TextForIAccessibleText();
2152 *start_offset = FindBoundary(
2153 text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
2154 *end_offset = offset;
2155 return get_text(*start_offset, *end_offset, text);
2158 STDMETHODIMP BrowserAccessibilityWin::get_textAfterOffset(
2159 LONG offset,
2160 enum IA2TextBoundaryType boundary_type,
2161 LONG* start_offset,
2162 LONG* end_offset,
2163 BSTR* text) {
2164 if (!instance_active())
2165 return E_FAIL;
2167 if (!start_offset || !end_offset || !text)
2168 return E_INVALIDARG;
2170 // The IAccessible2 spec says we don't have to implement the "sentence"
2171 // boundary type, we can just let the screenreader handle it.
2172 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2173 *start_offset = 0;
2174 *end_offset = 0;
2175 *text = NULL;
2176 return S_FALSE;
2179 const base::string16& text_str = TextForIAccessibleText();
2181 *start_offset = offset;
2182 *end_offset = FindBoundary(
2183 text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
2184 return get_text(*start_offset, *end_offset, text);
2187 STDMETHODIMP BrowserAccessibilityWin::get_newText(IA2TextSegment* new_text) {
2188 if (!instance_active())
2189 return E_FAIL;
2191 if (!new_text)
2192 return E_INVALIDARG;
2194 if (!old_win_attributes_)
2195 return E_FAIL;
2197 int start, old_len, new_len;
2198 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
2199 if (new_len == 0)
2200 return E_FAIL;
2202 base::string16 substr = hypertext().substr(start, new_len);
2203 new_text->text = SysAllocString(substr.c_str());
2204 new_text->start = static_cast<long>(start);
2205 new_text->end = static_cast<long>(start + new_len);
2206 return S_OK;
2209 STDMETHODIMP BrowserAccessibilityWin::get_oldText(IA2TextSegment* old_text) {
2210 if (!instance_active())
2211 return E_FAIL;
2213 if (!old_text)
2214 return E_INVALIDARG;
2216 if (!old_win_attributes_)
2217 return E_FAIL;
2219 int start, old_len, new_len;
2220 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
2221 if (old_len == 0)
2222 return E_FAIL;
2224 base::string16 old_hypertext = old_win_attributes_->hypertext;
2225 base::string16 substr = old_hypertext.substr(start, old_len);
2226 old_text->text = SysAllocString(substr.c_str());
2227 old_text->start = static_cast<long>(start);
2228 old_text->end = static_cast<long>(start + old_len);
2229 return S_OK;
2232 STDMETHODIMP BrowserAccessibilityWin::get_offsetAtPoint(
2233 LONG x,
2234 LONG y,
2235 enum IA2CoordinateType coord_type,
2236 LONG* offset) {
2237 if (!instance_active())
2238 return E_FAIL;
2240 if (!offset)
2241 return E_INVALIDARG;
2243 // TODO(dmazzoni): implement this. We're returning S_OK for now so that
2244 // screen readers still return partially accurate results rather than
2245 // completely failing.
2246 *offset = 0;
2247 return S_OK;
2250 STDMETHODIMP BrowserAccessibilityWin::scrollSubstringTo(
2251 LONG start_index,
2252 LONG end_index,
2253 enum IA2ScrollType scroll_type) {
2254 // TODO(dmazzoni): adjust this for the start and end index, too.
2255 return scrollTo(scroll_type);
2258 STDMETHODIMP BrowserAccessibilityWin::scrollSubstringToPoint(
2259 LONG start_index,
2260 LONG end_index,
2261 enum IA2CoordinateType coordinate_type,
2262 LONG x, LONG y) {
2263 // TODO(dmazzoni): adjust this for the start and end index, too.
2264 return scrollToPoint(coordinate_type, x, y);
2267 STDMETHODIMP BrowserAccessibilityWin::addSelection(LONG start_offset,
2268 LONG end_offset) {
2269 if (!instance_active())
2270 return E_FAIL;
2272 const base::string16& text_str = TextForIAccessibleText();
2273 HandleSpecialTextOffset(text_str, &start_offset);
2274 HandleSpecialTextOffset(text_str, &end_offset);
2276 manager()->SetTextSelection(*this, start_offset, end_offset);
2277 return S_OK;
2280 STDMETHODIMP BrowserAccessibilityWin::removeSelection(LONG selection_index) {
2281 if (!instance_active())
2282 return E_FAIL;
2284 if (selection_index != 0)
2285 return E_INVALIDARG;
2287 manager()->SetTextSelection(*this, 0, 0);
2288 return S_OK;
2291 STDMETHODIMP BrowserAccessibilityWin::setCaretOffset(LONG offset) {
2292 if (!instance_active())
2293 return E_FAIL;
2295 const base::string16& text_str = TextForIAccessibleText();
2296 HandleSpecialTextOffset(text_str, &offset);
2297 manager()->SetTextSelection(*this, offset, offset);
2298 return S_OK;
2301 STDMETHODIMP BrowserAccessibilityWin::setSelection(LONG selection_index,
2302 LONG start_offset,
2303 LONG end_offset) {
2304 if (!instance_active())
2305 return E_FAIL;
2307 if (selection_index != 0)
2308 return E_INVALIDARG;
2310 const base::string16& text_str = TextForIAccessibleText();
2311 HandleSpecialTextOffset(text_str, &start_offset);
2312 HandleSpecialTextOffset(text_str, &end_offset);
2314 manager()->SetTextSelection(*this, start_offset, end_offset);
2315 return S_OK;
2319 // IAccessibleHypertext methods.
2322 STDMETHODIMP BrowserAccessibilityWin::get_nHyperlinks(long* hyperlink_count) {
2323 if (!instance_active())
2324 return E_FAIL;
2326 if (!hyperlink_count)
2327 return E_INVALIDARG;
2329 *hyperlink_count = hyperlink_offset_to_index().size();
2330 return S_OK;
2333 STDMETHODIMP BrowserAccessibilityWin::get_hyperlink(
2334 long index,
2335 IAccessibleHyperlink** hyperlink) {
2336 if (!instance_active())
2337 return E_FAIL;
2339 if (!hyperlink ||
2340 index < 0 ||
2341 index >= static_cast<long>(hyperlinks().size())) {
2342 return E_INVALIDARG;
2345 int32 id = hyperlinks()[index];
2346 BrowserAccessibilityWin* child =
2347 manager()->GetFromID(id)->ToBrowserAccessibilityWin();
2348 if (child) {
2349 *hyperlink = static_cast<IAccessibleHyperlink*>(child->NewReference());
2350 return S_OK;
2353 return E_FAIL;
2356 STDMETHODIMP BrowserAccessibilityWin::get_hyperlinkIndex(
2357 long char_index,
2358 long* hyperlink_index) {
2359 if (!instance_active())
2360 return E_FAIL;
2362 if (!hyperlink_index)
2363 return E_INVALIDARG;
2365 *hyperlink_index = -1;
2367 if (char_index < 0 ||
2368 char_index >= static_cast<long>(hypertext().size())) {
2369 return E_INVALIDARG;
2372 std::map<int32, int32>::iterator it =
2373 hyperlink_offset_to_index().find(char_index);
2374 if (it == hyperlink_offset_to_index().end())
2375 return E_FAIL;
2377 *hyperlink_index = it->second;
2378 return S_OK;
2382 // IAccessibleValue methods.
2385 STDMETHODIMP BrowserAccessibilityWin::get_currentValue(VARIANT* value) {
2386 if (!instance_active())
2387 return E_FAIL;
2389 if (!value)
2390 return E_INVALIDARG;
2392 float float_val;
2393 if (GetFloatAttribute(
2394 ui::AX_ATTR_VALUE_FOR_RANGE, &float_val)) {
2395 value->vt = VT_R8;
2396 value->dblVal = float_val;
2397 return S_OK;
2400 value->vt = VT_EMPTY;
2401 return S_FALSE;
2404 STDMETHODIMP BrowserAccessibilityWin::get_minimumValue(VARIANT* value) {
2405 if (!instance_active())
2406 return E_FAIL;
2408 if (!value)
2409 return E_INVALIDARG;
2411 float float_val;
2412 if (GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE,
2413 &float_val)) {
2414 value->vt = VT_R8;
2415 value->dblVal = float_val;
2416 return S_OK;
2419 value->vt = VT_EMPTY;
2420 return S_FALSE;
2423 STDMETHODIMP BrowserAccessibilityWin::get_maximumValue(VARIANT* value) {
2424 if (!instance_active())
2425 return E_FAIL;
2427 if (!value)
2428 return E_INVALIDARG;
2430 float float_val;
2431 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE,
2432 &float_val)) {
2433 value->vt = VT_R8;
2434 value->dblVal = float_val;
2435 return S_OK;
2438 value->vt = VT_EMPTY;
2439 return S_FALSE;
2442 STDMETHODIMP BrowserAccessibilityWin::setCurrentValue(VARIANT new_value) {
2443 // TODO(dmazzoni): Implement this.
2444 return E_NOTIMPL;
2448 // ISimpleDOMDocument methods.
2451 STDMETHODIMP BrowserAccessibilityWin::get_URL(BSTR* url) {
2452 if (!instance_active())
2453 return E_FAIL;
2455 if (!url)
2456 return E_INVALIDARG;
2458 return GetStringAttributeAsBstr(ui::AX_ATTR_DOC_URL, url);
2461 STDMETHODIMP BrowserAccessibilityWin::get_title(BSTR* title) {
2462 if (!instance_active())
2463 return E_FAIL;
2465 if (!title)
2466 return E_INVALIDARG;
2468 return GetStringAttributeAsBstr(ui::AX_ATTR_DOC_TITLE, title);
2471 STDMETHODIMP BrowserAccessibilityWin::get_mimeType(BSTR* mime_type) {
2472 if (!instance_active())
2473 return E_FAIL;
2475 if (!mime_type)
2476 return E_INVALIDARG;
2478 return GetStringAttributeAsBstr(
2479 ui::AX_ATTR_DOC_MIMETYPE, mime_type);
2482 STDMETHODIMP BrowserAccessibilityWin::get_docType(BSTR* doc_type) {
2483 if (!instance_active())
2484 return E_FAIL;
2486 if (!doc_type)
2487 return E_INVALIDARG;
2489 return GetStringAttributeAsBstr(
2490 ui::AX_ATTR_DOC_DOCTYPE, doc_type);
2494 // ISimpleDOMNode methods.
2497 STDMETHODIMP BrowserAccessibilityWin::get_nodeInfo(
2498 BSTR* node_name,
2499 short* name_space_id,
2500 BSTR* node_value,
2501 unsigned int* num_children,
2502 unsigned int* unique_id,
2503 unsigned short* node_type) {
2504 if (!instance_active())
2505 return E_FAIL;
2507 if (!node_name || !name_space_id || !node_value || !num_children ||
2508 !unique_id || !node_type) {
2509 return E_INVALIDARG;
2512 base::string16 tag;
2513 if (GetString16Attribute(ui::AX_ATTR_HTML_TAG, &tag))
2514 *node_name = SysAllocString(tag.c_str());
2515 else
2516 *node_name = NULL;
2518 *name_space_id = 0;
2519 *node_value = SysAllocString(value().c_str());
2520 *num_children = PlatformChildCount();
2521 *unique_id = unique_id_win_;
2523 if (ia_role() == ROLE_SYSTEM_DOCUMENT) {
2524 *node_type = NODETYPE_DOCUMENT;
2525 } else if (ia_role() == ROLE_SYSTEM_TEXT &&
2526 ((ia2_state() & IA2_STATE_EDITABLE) == 0)) {
2527 *node_type = NODETYPE_TEXT;
2528 } else {
2529 *node_type = NODETYPE_ELEMENT;
2532 return S_OK;
2535 STDMETHODIMP BrowserAccessibilityWin::get_attributes(
2536 unsigned short max_attribs,
2537 BSTR* attrib_names,
2538 short* name_space_id,
2539 BSTR* attrib_values,
2540 unsigned short* num_attribs) {
2541 if (!instance_active())
2542 return E_FAIL;
2544 if (!attrib_names || !name_space_id || !attrib_values || !num_attribs)
2545 return E_INVALIDARG;
2547 *num_attribs = max_attribs;
2548 if (*num_attribs > GetHtmlAttributes().size())
2549 *num_attribs = GetHtmlAttributes().size();
2551 for (unsigned short i = 0; i < *num_attribs; ++i) {
2552 attrib_names[i] = SysAllocString(
2553 base::UTF8ToUTF16(GetHtmlAttributes()[i].first).c_str());
2554 name_space_id[i] = 0;
2555 attrib_values[i] = SysAllocString(
2556 base::UTF8ToUTF16(GetHtmlAttributes()[i].second).c_str());
2558 return S_OK;
2561 STDMETHODIMP BrowserAccessibilityWin::get_attributesForNames(
2562 unsigned short num_attribs,
2563 BSTR* attrib_names,
2564 short* name_space_id,
2565 BSTR* attrib_values) {
2566 if (!instance_active())
2567 return E_FAIL;
2569 if (!attrib_names || !name_space_id || !attrib_values)
2570 return E_INVALIDARG;
2572 for (unsigned short i = 0; i < num_attribs; ++i) {
2573 name_space_id[i] = 0;
2574 bool found = false;
2575 std::string name = base::UTF16ToUTF8((LPCWSTR)attrib_names[i]);
2576 for (unsigned int j = 0; j < GetHtmlAttributes().size(); ++j) {
2577 if (GetHtmlAttributes()[j].first == name) {
2578 attrib_values[i] = SysAllocString(
2579 base::UTF8ToUTF16(GetHtmlAttributes()[j].second).c_str());
2580 found = true;
2581 break;
2584 if (!found) {
2585 attrib_values[i] = NULL;
2588 return S_OK;
2591 STDMETHODIMP BrowserAccessibilityWin::get_computedStyle(
2592 unsigned short max_style_properties,
2593 boolean use_alternate_view,
2594 BSTR* style_properties,
2595 BSTR* style_values,
2596 unsigned short *num_style_properties) {
2597 if (!instance_active())
2598 return E_FAIL;
2600 if (!style_properties || !style_values)
2601 return E_INVALIDARG;
2603 // We only cache a single style property for now: DISPLAY
2605 base::string16 display;
2606 if (max_style_properties == 0 ||
2607 !GetString16Attribute(ui::AX_ATTR_DISPLAY, &display)) {
2608 *num_style_properties = 0;
2609 return S_OK;
2612 *num_style_properties = 1;
2613 style_properties[0] = SysAllocString(L"display");
2614 style_values[0] = SysAllocString(display.c_str());
2616 return S_OK;
2619 STDMETHODIMP BrowserAccessibilityWin::get_computedStyleForProperties(
2620 unsigned short num_style_properties,
2621 boolean use_alternate_view,
2622 BSTR* style_properties,
2623 BSTR* style_values) {
2624 if (!instance_active())
2625 return E_FAIL;
2627 if (!style_properties || !style_values)
2628 return E_INVALIDARG;
2630 // We only cache a single style property for now: DISPLAY
2632 for (unsigned short i = 0; i < num_style_properties; ++i) {
2633 base::string16 name = (LPCWSTR)style_properties[i];
2634 base::StringToLowerASCII(&name);
2635 if (name == L"display") {
2636 base::string16 display = GetString16Attribute(
2637 ui::AX_ATTR_DISPLAY);
2638 style_values[i] = SysAllocString(display.c_str());
2639 } else {
2640 style_values[i] = NULL;
2644 return S_OK;
2647 STDMETHODIMP BrowserAccessibilityWin::scrollTo(boolean placeTopLeft) {
2648 return scrollTo(placeTopLeft ?
2649 IA2_SCROLL_TYPE_TOP_LEFT : IA2_SCROLL_TYPE_ANYWHERE);
2652 STDMETHODIMP BrowserAccessibilityWin::get_parentNode(ISimpleDOMNode** node) {
2653 if (!instance_active())
2654 return E_FAIL;
2656 if (!node)
2657 return E_INVALIDARG;
2659 *node = GetParent()->ToBrowserAccessibilityWin()->NewReference();
2660 return S_OK;
2663 STDMETHODIMP BrowserAccessibilityWin::get_firstChild(ISimpleDOMNode** node) {
2664 if (!instance_active())
2665 return E_FAIL;
2667 if (!node)
2668 return E_INVALIDARG;
2670 if (PlatformChildCount() == 0) {
2671 *node = NULL;
2672 return S_FALSE;
2675 *node = PlatformGetChild(0)->ToBrowserAccessibilityWin()->NewReference();
2676 return S_OK;
2679 STDMETHODIMP BrowserAccessibilityWin::get_lastChild(ISimpleDOMNode** node) {
2680 if (!instance_active())
2681 return E_FAIL;
2683 if (!node)
2684 return E_INVALIDARG;
2686 if (PlatformChildCount() == 0) {
2687 *node = NULL;
2688 return S_FALSE;
2691 *node = PlatformGetChild(PlatformChildCount() - 1)
2692 ->ToBrowserAccessibilityWin()->NewReference();
2693 return S_OK;
2696 STDMETHODIMP BrowserAccessibilityWin::get_previousSibling(
2697 ISimpleDOMNode** node) {
2698 if (!instance_active())
2699 return E_FAIL;
2701 if (!node)
2702 return E_INVALIDARG;
2704 if (!GetParent() || GetIndexInParent() <= 0) {
2705 *node = NULL;
2706 return S_FALSE;
2709 *node = GetParent()->InternalGetChild(GetIndexInParent() - 1)->
2710 ToBrowserAccessibilityWin()->NewReference();
2711 return S_OK;
2714 STDMETHODIMP BrowserAccessibilityWin::get_nextSibling(ISimpleDOMNode** node) {
2715 if (!instance_active())
2716 return E_FAIL;
2718 if (!node)
2719 return E_INVALIDARG;
2721 if (!GetParent() ||
2722 GetIndexInParent() < 0 ||
2723 GetIndexInParent() >= static_cast<int>(
2724 GetParent()->InternalChildCount()) - 1) {
2725 *node = NULL;
2726 return S_FALSE;
2729 *node = GetParent()->InternalGetChild(GetIndexInParent() + 1)->
2730 ToBrowserAccessibilityWin()->NewReference();
2731 return S_OK;
2734 STDMETHODIMP BrowserAccessibilityWin::get_childAt(
2735 unsigned int child_index,
2736 ISimpleDOMNode** node) {
2737 if (!instance_active())
2738 return E_FAIL;
2740 if (!node)
2741 return E_INVALIDARG;
2743 if (child_index >= PlatformChildCount())
2744 return E_INVALIDARG;
2746 BrowserAccessibility* child = PlatformGetChild(child_index);
2747 if (!child) {
2748 *node = NULL;
2749 return S_FALSE;
2752 *node = child->ToBrowserAccessibilityWin()->NewReference();
2753 return S_OK;
2757 // ISimpleDOMText methods.
2760 STDMETHODIMP BrowserAccessibilityWin::get_domText(BSTR* dom_text) {
2761 if (!instance_active())
2762 return E_FAIL;
2764 if (!dom_text)
2765 return E_INVALIDARG;
2767 return GetStringAttributeAsBstr(
2768 ui::AX_ATTR_NAME, dom_text);
2771 STDMETHODIMP BrowserAccessibilityWin::get_clippedSubstringBounds(
2772 unsigned int start_index,
2773 unsigned int end_index,
2774 int* out_x,
2775 int* out_y,
2776 int* out_width,
2777 int* out_height) {
2778 // TODO(dmazzoni): fully support this API by intersecting the
2779 // rect with the container's rect.
2780 return get_unclippedSubstringBounds(
2781 start_index, end_index, out_x, out_y, out_width, out_height);
2784 STDMETHODIMP BrowserAccessibilityWin::get_unclippedSubstringBounds(
2785 unsigned int start_index,
2786 unsigned int end_index,
2787 int* out_x,
2788 int* out_y,
2789 int* out_width,
2790 int* out_height) {
2791 if (!instance_active())
2792 return E_FAIL;
2794 if (!out_x || !out_y || !out_width || !out_height)
2795 return E_INVALIDARG;
2797 const base::string16& text_str = TextForIAccessibleText();
2798 if (start_index > text_str.size() ||
2799 end_index > text_str.size() ||
2800 start_index > end_index) {
2801 return E_INVALIDARG;
2804 gfx::Rect bounds = GetGlobalBoundsForRange(
2805 start_index, end_index - start_index);
2806 *out_x = bounds.x();
2807 *out_y = bounds.y();
2808 *out_width = bounds.width();
2809 *out_height = bounds.height();
2810 return S_OK;
2813 STDMETHODIMP BrowserAccessibilityWin::scrollToSubstring(
2814 unsigned int start_index,
2815 unsigned int end_index) {
2816 if (!instance_active())
2817 return E_FAIL;
2819 const base::string16& text_str = TextForIAccessibleText();
2820 if (start_index > text_str.size() ||
2821 end_index > text_str.size() ||
2822 start_index > end_index) {
2823 return E_INVALIDARG;
2826 manager()->ScrollToMakeVisible(*this, GetLocalBoundsForRange(
2827 start_index, end_index - start_index));
2828 manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
2830 return S_OK;
2834 // IServiceProvider methods.
2837 STDMETHODIMP BrowserAccessibilityWin::QueryService(REFGUID guidService,
2838 REFIID riid,
2839 void** object) {
2840 if (!instance_active())
2841 return E_FAIL;
2843 // The system uses IAccessible APIs for many purposes, but only
2844 // assistive technology like screen readers uses IAccessible2.
2845 // Enable full accessibility support when IAccessible2 APIs are queried.
2846 if (riid == IID_IAccessible2)
2847 BrowserAccessibilityStateImpl::GetInstance()->EnableAccessibility();
2849 if (guidService == GUID_IAccessibleContentDocument) {
2850 // Special Mozilla extension: return the accessible for the root document.
2851 // Screen readers use this to distinguish between a document loaded event
2852 // on the root document vs on an iframe.
2853 return manager()->GetRoot()->ToBrowserAccessibilityWin()->QueryInterface(
2854 IID_IAccessible2, object);
2857 if (guidService == IID_IAccessible ||
2858 guidService == IID_IAccessible2 ||
2859 guidService == IID_IAccessibleAction ||
2860 guidService == IID_IAccessibleApplication ||
2861 guidService == IID_IAccessibleHyperlink ||
2862 guidService == IID_IAccessibleHypertext ||
2863 guidService == IID_IAccessibleImage ||
2864 guidService == IID_IAccessibleTable ||
2865 guidService == IID_IAccessibleTable2 ||
2866 guidService == IID_IAccessibleTableCell ||
2867 guidService == IID_IAccessibleText ||
2868 guidService == IID_IAccessibleValue ||
2869 guidService == IID_ISimpleDOMDocument ||
2870 guidService == IID_ISimpleDOMNode ||
2871 guidService == IID_ISimpleDOMText ||
2872 guidService == GUID_ISimpleDOM) {
2873 return QueryInterface(riid, object);
2876 // We only support the IAccessibleEx interface on Windows 8 and above. This
2877 // is needed for the on-screen Keyboard to show up in metro mode, when the
2878 // user taps an editable portion on the page.
2879 // All methods in the IAccessibleEx interface are unimplemented.
2880 if (riid == IID_IAccessibleEx &&
2881 base::win::GetVersion() >= base::win::VERSION_WIN8) {
2882 return QueryInterface(riid, object);
2885 *object = NULL;
2886 return E_FAIL;
2889 STDMETHODIMP BrowserAccessibilityWin::GetPatternProvider(PATTERNID id,
2890 IUnknown** provider) {
2891 DVLOG(1) << "In Function: "
2892 << __FUNCTION__
2893 << " for pattern id: "
2894 << id;
2895 if (id == UIA_ValuePatternId || id == UIA_TextPatternId) {
2896 if (IsEditableText()) {
2897 DVLOG(1) << "Returning UIA text provider";
2898 base::win::UIATextProvider::CreateTextProvider(
2899 GetValueText(), true, provider);
2900 return S_OK;
2903 return E_NOTIMPL;
2906 STDMETHODIMP BrowserAccessibilityWin::GetPropertyValue(PROPERTYID id,
2907 VARIANT* ret) {
2908 DVLOG(1) << "In Function: "
2909 << __FUNCTION__
2910 << " for property id: "
2911 << id;
2912 V_VT(ret) = VT_EMPTY;
2913 if (id == UIA_ControlTypePropertyId) {
2914 if (IsEditableText()) {
2915 V_VT(ret) = VT_I4;
2916 ret->lVal = UIA_EditControlTypeId;
2917 DVLOG(1) << "Returning Edit control type";
2918 } else {
2919 DVLOG(1) << "Returning empty control type";
2922 return S_OK;
2926 // CComObjectRootEx methods.
2929 // static
2930 HRESULT WINAPI BrowserAccessibilityWin::InternalQueryInterface(
2931 void* this_ptr,
2932 const _ATL_INTMAP_ENTRY* entries,
2933 REFIID iid,
2934 void** object) {
2935 BrowserAccessibilityWin* accessibility =
2936 reinterpret_cast<BrowserAccessibilityWin*>(this_ptr);
2937 int32 ia_role = accessibility->ia_role();
2938 if (iid == IID_IAccessibleImage) {
2939 if (ia_role != ROLE_SYSTEM_GRAPHIC) {
2940 *object = NULL;
2941 return E_NOINTERFACE;
2943 } else if (iid == IID_IAccessibleTable || iid == IID_IAccessibleTable2) {
2944 if (ia_role != ROLE_SYSTEM_TABLE) {
2945 *object = NULL;
2946 return E_NOINTERFACE;
2948 } else if (iid == IID_IAccessibleTableCell) {
2949 if (!accessibility->IsCellOrTableHeaderRole()) {
2950 *object = NULL;
2951 return E_NOINTERFACE;
2953 } else if (iid == IID_IAccessibleValue) {
2954 if (ia_role != ROLE_SYSTEM_PROGRESSBAR &&
2955 ia_role != ROLE_SYSTEM_SCROLLBAR &&
2956 ia_role != ROLE_SYSTEM_SLIDER) {
2957 *object = NULL;
2958 return E_NOINTERFACE;
2960 } else if (iid == IID_ISimpleDOMDocument) {
2961 if (ia_role != ROLE_SYSTEM_DOCUMENT) {
2962 *object = NULL;
2963 return E_NOINTERFACE;
2967 return CComObjectRootBase::InternalQueryInterface(
2968 this_ptr, entries, iid, object);
2972 // Private methods.
2975 void BrowserAccessibilityWin::UpdateStep1ComputeWinAttributes() {
2976 // Swap win_attributes_ to old_win_attributes_, allowing us to see
2977 // exactly what changed and fire appropriate events. Note that
2978 // old_win_attributes_ is cleared at the end of UpdateStep3FireEvents.
2979 old_win_attributes_.swap(win_attributes_);
2980 win_attributes_.reset(new WinAttributes());
2982 InitRoleAndState();
2984 win_attributes_->ia2_attributes.clear();
2986 // Expose autocomplete attribute for combobox and textbox.
2987 StringAttributeToIA2(ui::AX_ATTR_AUTO_COMPLETE, "autocomplete");
2989 // Expose the "display" and "tag" attributes.
2990 StringAttributeToIA2(ui::AX_ATTR_DISPLAY, "display");
2991 StringAttributeToIA2(ui::AX_ATTR_DROPEFFECT, "dropeffect");
2992 StringAttributeToIA2(ui::AX_ATTR_TEXT_INPUT_TYPE, "text-input-type");
2993 StringAttributeToIA2(ui::AX_ATTR_HTML_TAG, "tag");
2994 StringAttributeToIA2(ui::AX_ATTR_ROLE, "xml-roles");
2996 // Expose "level" attribute for headings, trees, etc.
2997 IntAttributeToIA2(ui::AX_ATTR_HIERARCHICAL_LEVEL, "level");
2999 // Expose the set size and position in set.
3000 IntAttributeToIA2(ui::AX_ATTR_SET_SIZE, "setsize");
3001 IntAttributeToIA2(ui::AX_ATTR_POS_IN_SET, "posinset");
3003 if (ia_role() == ROLE_SYSTEM_CHECKBUTTON ||
3004 ia_role() == ROLE_SYSTEM_RADIOBUTTON ||
3005 ia2_role() == IA2_ROLE_CHECK_MENU_ITEM ||
3006 ia2_role() == IA2_ROLE_RADIO_MENU_ITEM ||
3007 ia2_role() == IA2_ROLE_TOGGLE_BUTTON) {
3008 win_attributes_->ia2_attributes.push_back(L"checkable:true");
3011 // Expose live region attributes.
3012 StringAttributeToIA2(ui::AX_ATTR_LIVE_STATUS, "live");
3013 StringAttributeToIA2(ui::AX_ATTR_LIVE_RELEVANT, "relevant");
3014 BoolAttributeToIA2(ui::AX_ATTR_LIVE_ATOMIC, "atomic");
3015 BoolAttributeToIA2(ui::AX_ATTR_LIVE_BUSY, "busy");
3017 // Expose aria-grabbed attributes.
3018 BoolAttributeToIA2(ui::AX_ATTR_GRABBED, "grabbed");
3020 // Expose container live region attributes.
3021 StringAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_STATUS,
3022 "container-live");
3023 StringAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_RELEVANT,
3024 "container-relevant");
3025 BoolAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_ATOMIC,
3026 "container-atomic");
3027 BoolAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_BUSY,
3028 "container-busy");
3030 // Expose table cell index.
3031 if (IsCellOrTableHeaderRole()) {
3032 BrowserAccessibility* table = GetParent();
3033 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
3034 table = table->GetParent();
3035 if (table) {
3036 const std::vector<int32>& unique_cell_ids = table->GetIntListAttribute(
3037 ui::AX_ATTR_UNIQUE_CELL_IDS);
3038 for (size_t i = 0; i < unique_cell_ids.size(); ++i) {
3039 if (unique_cell_ids[i] == GetId()) {
3040 win_attributes_->ia2_attributes.push_back(
3041 base::string16(L"table-cell-index:") + base::IntToString16(i));
3047 // Expose invalid state for form controls and elements with aria-invalid.
3048 int invalid_state;
3049 if (GetIntAttribute(ui::AX_ATTR_INVALID_STATE, &invalid_state)) {
3050 // TODO(nektar): Handle the possibility of having multiple aria-invalid
3051 // attributes defined, e.g., "invalid:spelling,grammar".
3052 switch (invalid_state) {
3053 case ui::AX_INVALID_STATE_FALSE:
3054 win_attributes_->ia2_attributes.push_back(L"invalid:false");
3055 break;
3056 case ui::AX_INVALID_STATE_TRUE:
3057 win_attributes_->ia2_attributes.push_back(L"invalid:true");
3058 break;
3059 case ui::AX_INVALID_STATE_SPELLING:
3060 win_attributes_->ia2_attributes.push_back(L"invalid:spelling");
3061 break;
3062 case ui::AX_INVALID_STATE_GRAMMAR:
3063 win_attributes_->ia2_attributes.push_back(L"invalid:grammar");
3064 break;
3065 case ui::AX_INVALID_STATE_OTHER:
3067 base::string16 aria_invalid_value;
3068 if (GetString16Attribute(ui::AX_ATTR_ARIA_INVALID_VALUE,
3069 &aria_invalid_value)) {
3070 win_attributes_->ia2_attributes.push_back(
3071 L"invalid:" + aria_invalid_value);
3072 } else {
3073 // Set the attribute to L"true", since we cannot be more specific.
3074 win_attributes_->ia2_attributes.push_back(L"invalid:true");
3077 break;
3078 default:
3079 NOTREACHED();
3083 // Expose row or column header sort direction.
3084 int32 sort_direction;
3085 if ((ia_role() == ROLE_SYSTEM_COLUMNHEADER ||
3086 ia_role() == ROLE_SYSTEM_ROWHEADER) &&
3087 GetIntAttribute(ui::AX_ATTR_SORT_DIRECTION, &sort_direction)) {
3088 switch (sort_direction) {
3089 case ui::AX_SORT_DIRECTION_UNSORTED:
3090 win_attributes_->ia2_attributes.push_back(L"sort:none");
3091 break;
3092 case ui::AX_SORT_DIRECTION_ASCENDING:
3093 win_attributes_->ia2_attributes.push_back(L"sort:ascending");
3094 break;
3095 case ui::AX_SORT_DIRECTION_DESCENDING:
3096 win_attributes_->ia2_attributes.push_back(L"sort:descending");
3097 break;
3098 case ui::AX_SORT_DIRECTION_OTHER:
3099 win_attributes_->ia2_attributes.push_back(L"sort:other");
3100 break;
3101 default:
3102 NOTREACHED();
3106 // The calculation of the accessible name of an element has been
3107 // standardized in the HTML to Platform Accessibility APIs Implementation
3108 // Guide (http://www.w3.org/TR/html-aapi/). In order to return the
3109 // appropriate accessible name on Windows, we need to apply some logic
3110 // to the fields we get from WebKit.
3112 // TODO(dmazzoni): move most of this logic into WebKit.
3114 // WebKit gives us:
3116 // name: the default name, e.g. inner text
3117 // title ui element: a reference to a <label> element on the same
3118 // page that labels this node.
3119 // description: accessible labels that override the default name:
3120 // aria-label or aria-labelledby or aria-describedby
3121 // help: the value of the "title" attribute
3123 // On Windows, the logic we apply lets some fields take precedence and
3124 // always returns the primary name in "name" and the secondary name,
3125 // if any, in "description".
3127 int title_elem_id = GetIntAttribute(ui::AX_ATTR_TITLE_UI_ELEMENT);
3128 base::string16 name = GetString16Attribute(ui::AX_ATTR_NAME);
3129 base::string16 description = GetString16Attribute(ui::AX_ATTR_DESCRIPTION);
3130 base::string16 help = GetString16Attribute(ui::AX_ATTR_HELP);
3131 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
3133 // WebKit annoyingly puts the title in the description if there's no other
3134 // description, which just confuses the rest of the logic. Put it back.
3135 // Now "help" is always the value of the "title" attribute, if present.
3136 base::string16 title_attr;
3137 if (GetHtmlAttribute("title", &title_attr) &&
3138 description == title_attr &&
3139 help.empty()) {
3140 help = description;
3141 description.clear();
3144 // Now implement the main logic: the descripion should become the name if
3145 // it's nonempty, and the help should become the description if
3146 // there's no description - or the name if there's no name or description.
3147 if (!description.empty()) {
3148 name = description;
3149 description.clear();
3151 if (!help.empty() && description.empty()) {
3152 description = help;
3153 help.clear();
3155 if (!description.empty() && name.empty() && !title_elem_id) {
3156 name = description;
3157 description.clear();
3160 // If it's a text field, also consider the placeholder.
3161 base::string16 placeholder;
3162 if (GetRole() == ui::AX_ROLE_TEXT_FIELD &&
3163 HasState(ui::AX_STATE_FOCUSABLE) &&
3164 GetHtmlAttribute("placeholder", &placeholder)) {
3165 if (name.empty() && !title_elem_id) {
3166 name = placeholder;
3167 } else if (description.empty()) {
3168 description = placeholder;
3172 // On Windows, the value of a document should be its url.
3173 if (GetRole() == ui::AX_ROLE_ROOT_WEB_AREA ||
3174 GetRole() == ui::AX_ROLE_WEB_AREA) {
3175 value = GetString16Attribute(ui::AX_ATTR_DOC_URL);
3178 // For certain roles (listbox option, static text, and list marker)
3179 // WebKit stores the main accessible text in the "value" - swap it so
3180 // that it's the "name".
3181 if (name.empty() &&
3182 (GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
3183 GetRole() == ui::AX_ROLE_STATIC_TEXT ||
3184 GetRole() == ui::AX_ROLE_LIST_MARKER)) {
3185 base::string16 tmp = value;
3186 value = name;
3187 name = tmp;
3190 // If this doesn't have a value and is linked then set its value to the url
3191 // attribute. This allows screen readers to read an empty link's destination.
3192 if (value.empty() && (ia_state() & STATE_SYSTEM_LINKED))
3193 value = GetString16Attribute(ui::AX_ATTR_URL);
3195 win_attributes_->name = name;
3196 win_attributes_->description = description;
3197 win_attributes_->help = help;
3198 win_attributes_->value = value;
3200 // Clear any old relationships between this node and other nodes.
3201 for (size_t i = 0; i < relations_.size(); ++i)
3202 relations_[i]->Release();
3203 relations_.clear();
3205 // Handle title UI element.
3206 if (title_elem_id) {
3207 // Add a labelled by relationship.
3208 CComObject<BrowserAccessibilityRelation>* relation;
3209 HRESULT hr = CComObject<BrowserAccessibilityRelation>::CreateInstance(
3210 &relation);
3211 DCHECK(SUCCEEDED(hr));
3212 relation->AddRef();
3213 relation->Initialize(this, IA2_RELATION_LABELLED_BY);
3214 relation->AddTarget(title_elem_id);
3215 relations_.push_back(relation);
3218 // Expose slider value.
3219 if (ia_role() == ROLE_SYSTEM_PROGRESSBAR ||
3220 ia_role() == ROLE_SYSTEM_SCROLLBAR ||
3221 ia_role() == ROLE_SYSTEM_SLIDER) {
3222 win_attributes_->ia2_attributes.push_back(L"valuetext:" + GetValueText());
3225 // If this is a web area for a presentational iframe, give it a role of
3226 // something other than DOCUMENT so that the fact that it's a separate doc
3227 // is not exposed to AT.
3228 if (IsWebAreaForPresentationalIframe()) {
3229 win_attributes_->ia_role = ROLE_SYSTEM_GROUPING;
3230 win_attributes_->ia2_role = ROLE_SYSTEM_GROUPING;
3234 void BrowserAccessibilityWin::UpdateStep2ComputeHypertext() {
3235 // Construct the hypertext for this node, which contains the concatenation
3236 // of all of the static text of this node's children and an embedded object
3237 // character for all non-static-text children. Build up a map from the
3238 // character index of each embedded object character to the id of the
3239 // child object it points to.
3240 for (unsigned int i = 0; i < PlatformChildCount(); ++i) {
3241 BrowserAccessibilityWin* child =
3242 PlatformGetChild(i)->ToBrowserAccessibilityWin();
3243 if (child->GetRole() == ui::AX_ROLE_STATIC_TEXT) {
3244 win_attributes_->hypertext += child->name();
3245 } else {
3246 int32 char_offset = hypertext().size();
3247 int32 child_id = child->GetId();
3248 int32 index = hyperlinks().size();
3249 win_attributes_->hyperlink_offset_to_index[char_offset] = index;
3250 win_attributes_->hyperlinks.push_back(child_id);
3251 win_attributes_->hypertext += kEmbeddedCharacter;
3256 void BrowserAccessibilityWin::UpdateStep3FireEvents(bool is_subtree_creation) {
3257 BrowserAccessibilityManagerWin* manager =
3258 this->manager()->ToBrowserAccessibilityManagerWin();
3260 // Fire an event when an alert first appears.
3261 if (ia_role() == ROLE_SYSTEM_ALERT &&
3262 old_win_attributes_->ia_role != ROLE_SYSTEM_ALERT) {
3263 manager->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
3266 // Fire an event when a new subtree is created.
3267 if (is_subtree_creation)
3268 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SHOW, this);
3270 // The rest of the events only fire on changes, not on new objects.
3271 if (old_win_attributes_->ia_role != 0 ||
3272 !old_win_attributes_->role_name.empty()) {
3273 // Fire an event if the name, description, help, or value changes.
3274 if (name() != old_win_attributes_->name)
3275 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, this);
3276 if (description() != old_win_attributes_->description)
3277 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_DESCRIPTIONCHANGE, this);
3278 if (help() != old_win_attributes_->help)
3279 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_HELPCHANGE, this);
3280 if (value() != old_win_attributes_->value)
3281 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_VALUECHANGE, this);
3282 if (ia_state() != old_win_attributes_->ia_state)
3283 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_STATECHANGE, this);
3285 // Normally focus events are handled elsewhere, however
3286 // focus for managed descendants is platform-specific.
3287 // Fire a focus event if the focused descendant in a multi-select
3288 // list box changes.
3289 if (GetRole() == ui::AX_ROLE_LIST_BOX_OPTION &&
3290 (ia_state() & STATE_SYSTEM_FOCUSABLE) &&
3291 (ia_state() & STATE_SYSTEM_SELECTABLE) &&
3292 (ia_state() & STATE_SYSTEM_FOCUSED) &&
3293 !(old_win_attributes_->ia_state & STATE_SYSTEM_FOCUSED)) {
3294 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_FOCUS, this);
3297 // Handle selection being added or removed.
3298 bool is_selected_now = (ia_state() & STATE_SYSTEM_SELECTED) != 0;
3299 bool was_selected_before =
3300 (old_win_attributes_->ia_state & STATE_SYSTEM_SELECTED) != 0;
3301 if (is_selected_now && !was_selected_before) {
3302 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTIONADD, this);
3303 } else if (!is_selected_now && was_selected_before) {
3304 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTIONREMOVE, this);
3307 // Fire an event if this container object has scrolled.
3308 int sx = 0;
3309 int sy = 0;
3310 if (GetIntAttribute(ui::AX_ATTR_SCROLL_X, &sx) &&
3311 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &sy)) {
3312 if (sx != previous_scroll_x_ || sy != previous_scroll_y_)
3313 manager->MaybeCallNotifyWinEvent(EVENT_SYSTEM_SCROLLINGEND, this);
3314 previous_scroll_x_ = sx;
3315 previous_scroll_y_ = sy;
3318 // Changing a static text node can affect the IAccessibleText hypertext
3319 // of the parent node, so force an update on the parent.
3320 BrowserAccessibilityWin* parent = GetParent()->ToBrowserAccessibilityWin();
3321 if (parent &&
3322 GetRole() == ui::AX_ROLE_STATIC_TEXT &&
3323 name() != old_win_attributes_->name) {
3324 parent->UpdateStep1ComputeWinAttributes();
3325 parent->UpdateStep2ComputeHypertext();
3326 parent->UpdateStep3FireEvents(false);
3329 // Fire hypertext-related events.
3330 int start, old_len, new_len;
3331 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
3332 if (old_len > 0) {
3333 // In-process screen readers may call IAccessibleText::get_oldText
3334 // in reaction to this event to retrieve the text that was removed.
3335 manager->MaybeCallNotifyWinEvent(IA2_EVENT_TEXT_REMOVED, this);
3337 if (new_len > 0) {
3338 // In-process screen readers may call IAccessibleText::get_newText
3339 // in reaction to this event to retrieve the text that was inserted.
3340 manager->MaybeCallNotifyWinEvent(IA2_EVENT_TEXT_INSERTED, this);
3344 old_win_attributes_.reset(nullptr);
3347 void BrowserAccessibilityWin::OnSubtreeWillBeDeleted() {
3348 manager()->ToBrowserAccessibilityManagerWin()->MaybeCallNotifyWinEvent(
3349 EVENT_OBJECT_HIDE, this);
3352 void BrowserAccessibilityWin::NativeAddReference() {
3353 AddRef();
3356 void BrowserAccessibilityWin::NativeReleaseReference() {
3357 Release();
3360 bool BrowserAccessibilityWin::IsNative() const {
3361 return true;
3364 void BrowserAccessibilityWin::OnLocationChanged() {
3365 manager()->ToBrowserAccessibilityManagerWin()->MaybeCallNotifyWinEvent(
3366 EVENT_OBJECT_LOCATIONCHANGE, this);
3369 BrowserAccessibilityWin* BrowserAccessibilityWin::NewReference() {
3370 AddRef();
3371 return this;
3374 BrowserAccessibilityWin* BrowserAccessibilityWin::GetTargetFromChildID(
3375 const VARIANT& var_id) {
3376 if (var_id.vt != VT_I4)
3377 return NULL;
3379 LONG child_id = var_id.lVal;
3380 if (child_id == CHILDID_SELF)
3381 return this;
3383 if (child_id >= 1 && child_id <= static_cast<LONG>(PlatformChildCount()))
3384 return PlatformGetChild(child_id - 1)->ToBrowserAccessibilityWin();
3386 return manager()->ToBrowserAccessibilityManagerWin()->
3387 GetFromUniqueIdWin(child_id);
3390 HRESULT BrowserAccessibilityWin::GetStringAttributeAsBstr(
3391 ui::AXStringAttribute attribute,
3392 BSTR* value_bstr) {
3393 base::string16 str;
3395 if (!GetString16Attribute(attribute, &str))
3396 return S_FALSE;
3398 if (str.empty())
3399 return S_FALSE;
3401 *value_bstr = SysAllocString(str.c_str());
3402 DCHECK(*value_bstr);
3404 return S_OK;
3407 void BrowserAccessibilityWin::StringAttributeToIA2(
3408 ui::AXStringAttribute attribute,
3409 const char* ia2_attr) {
3410 base::string16 value;
3411 if (GetString16Attribute(attribute, &value)) {
3412 win_attributes_->ia2_attributes.push_back(
3413 base::ASCIIToUTF16(ia2_attr) + L":" + value);
3417 void BrowserAccessibilityWin::BoolAttributeToIA2(
3418 ui::AXBoolAttribute attribute,
3419 const char* ia2_attr) {
3420 bool value;
3421 if (GetBoolAttribute(attribute, &value)) {
3422 win_attributes_->ia2_attributes.push_back(
3423 (base::ASCIIToUTF16(ia2_attr) + L":") +
3424 (value ? L"true" : L"false"));
3428 void BrowserAccessibilityWin::IntAttributeToIA2(
3429 ui::AXIntAttribute attribute,
3430 const char* ia2_attr) {
3431 int value;
3432 if (GetIntAttribute(attribute, &value)) {
3433 win_attributes_->ia2_attributes.push_back(
3434 base::ASCIIToUTF16(ia2_attr) + L":" +
3435 base::IntToString16(value));
3439 base::string16 BrowserAccessibilityWin::GetNameRecursive() const {
3440 if (!name().empty()) {
3441 return name();
3444 base::string16 result;
3445 for (uint32 i = 0; i < PlatformChildCount(); ++i) {
3446 result += PlatformGetChild(i)->ToBrowserAccessibilityWin()->
3447 GetNameRecursive();
3449 return result;
3452 base::string16 BrowserAccessibilityWin::GetValueText() {
3453 float fval;
3454 base::string16 value = this->value();
3456 if (value.empty() &&
3457 GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE, &fval)) {
3458 value = base::UTF8ToUTF16(base::DoubleToString(fval));
3460 return value;
3463 base::string16 BrowserAccessibilityWin::TextForIAccessibleText() {
3464 if (IsEditableText())
3465 return value();
3466 return (GetRole() == ui::AX_ROLE_STATIC_TEXT) ? name() : hypertext();
3469 bool BrowserAccessibilityWin::IsSameHypertextCharacter(size_t old_char_index,
3470 size_t new_char_index) {
3471 CHECK(old_win_attributes_);
3473 // For anything other than the "embedded character", we just compare the
3474 // characters directly.
3475 base::char16 old_ch = old_win_attributes_->hypertext[old_char_index];
3476 base::char16 new_ch = win_attributes_->hypertext[new_char_index];
3477 if (old_ch != new_ch)
3478 return false;
3479 if (old_ch == new_ch && new_ch != kEmbeddedCharacter)
3480 return true;
3482 // If it's an embedded character, they're only identical if the child id
3483 // the hyperlink points to is the same.
3484 std::map<int32, int32>& old_offset_to_index =
3485 old_win_attributes_->hyperlink_offset_to_index;
3486 std::vector<int32>& old_hyperlinks = old_win_attributes_->hyperlinks;
3487 int32 old_hyperlinks_count = static_cast<int32>(old_hyperlinks.size());
3488 std::map<int32, int32>::iterator iter;
3489 iter = old_offset_to_index.find(old_char_index);
3490 int old_index = (iter != old_offset_to_index.end()) ? iter->second : -1;
3491 int old_child_id = (old_index >= 0 && old_index < old_hyperlinks_count) ?
3492 old_hyperlinks[old_index] : -1;
3494 std::map<int32, int32>& new_offset_to_index =
3495 win_attributes_->hyperlink_offset_to_index;
3496 std::vector<int32>& new_hyperlinks = win_attributes_->hyperlinks;
3497 int32 new_hyperlinks_count = static_cast<int32>(new_hyperlinks.size());
3498 iter = new_offset_to_index.find(new_char_index);
3499 int new_index = (iter != new_offset_to_index.end()) ? iter->second : -1;
3500 int new_child_id = (new_index >= 0 && new_index < new_hyperlinks_count) ?
3501 new_hyperlinks[new_index] : -1;
3503 return old_child_id == new_child_id;
3506 void BrowserAccessibilityWin::ComputeHypertextRemovedAndInserted(
3507 int* start, int* old_len, int* new_len) {
3508 CHECK(old_win_attributes_);
3510 *start = 0;
3511 *old_len = 0;
3512 *new_len = 0;
3514 const base::string16& old_text = old_win_attributes_->hypertext;
3515 const base::string16& new_text = hypertext();
3517 size_t common_prefix = 0;
3518 while (common_prefix < old_text.size() &&
3519 common_prefix < new_text.size() &&
3520 IsSameHypertextCharacter(common_prefix, common_prefix)) {
3521 ++common_prefix;
3524 size_t common_suffix = 0;
3525 while (common_prefix + common_suffix < old_text.size() &&
3526 common_prefix + common_suffix < new_text.size() &&
3527 IsSameHypertextCharacter(
3528 old_text.size() - common_suffix - 1,
3529 new_text.size() - common_suffix - 1)) {
3530 ++common_suffix;
3533 *start = common_prefix;
3534 *old_len = old_text.size() - common_prefix - common_suffix;
3535 *new_len = new_text.size() - common_prefix - common_suffix;
3538 void BrowserAccessibilityWin::HandleSpecialTextOffset(
3539 const base::string16& text,
3540 LONG* offset) {
3541 if (*offset == IA2_TEXT_OFFSET_LENGTH)
3542 *offset = static_cast<LONG>(text.size());
3543 else if (*offset == IA2_TEXT_OFFSET_CARET)
3544 get_caretOffset(offset);
3547 ui::TextBoundaryType BrowserAccessibilityWin::IA2TextBoundaryToTextBoundary(
3548 IA2TextBoundaryType ia2_boundary) {
3549 switch(ia2_boundary) {
3550 case IA2_TEXT_BOUNDARY_CHAR:
3551 return ui::CHAR_BOUNDARY;
3552 case IA2_TEXT_BOUNDARY_WORD:
3553 return ui::WORD_BOUNDARY;
3554 case IA2_TEXT_BOUNDARY_LINE:
3555 return ui::LINE_BOUNDARY;
3556 case IA2_TEXT_BOUNDARY_SENTENCE:
3557 return ui::SENTENCE_BOUNDARY;
3558 case IA2_TEXT_BOUNDARY_PARAGRAPH:
3559 return ui::PARAGRAPH_BOUNDARY;
3560 case IA2_TEXT_BOUNDARY_ALL:
3561 return ui::ALL_BOUNDARY;
3562 default:
3563 NOTREACHED();
3565 return ui::CHAR_BOUNDARY;
3568 LONG BrowserAccessibilityWin::FindBoundary(
3569 const base::string16& text,
3570 IA2TextBoundaryType ia2_boundary,
3571 LONG start_offset,
3572 ui::TextBoundaryDirection direction) {
3573 HandleSpecialTextOffset(text, &start_offset);
3574 if (ia2_boundary == IA2_TEXT_BOUNDARY_WORD &&
3575 GetRole() == ui::AX_ROLE_TEXT_FIELD) {
3576 return GetWordStartBoundary(static_cast<int>(start_offset), direction);
3579 ui::TextBoundaryType boundary = IA2TextBoundaryToTextBoundary(ia2_boundary);
3580 const std::vector<int32>& line_breaks = GetIntListAttribute(
3581 ui::AX_ATTR_LINE_BREAKS);
3582 return ui::FindAccessibleTextBoundary(
3583 text, line_breaks, boundary, start_offset, direction);
3586 BrowserAccessibilityWin* BrowserAccessibilityWin::GetFromID(int32 id) {
3587 return manager()->GetFromID(id)->ToBrowserAccessibilityWin();
3590 void BrowserAccessibilityWin::InitRoleAndState() {
3591 int32 ia_role = 0;
3592 int32 ia_state = 0;
3593 base::string16 role_name;
3594 int32 ia2_role = 0;
3595 int32 ia2_state = IA2_STATE_OPAQUE;
3597 if (HasState(ui::AX_STATE_BUSY))
3598 ia_state |= STATE_SYSTEM_BUSY;
3599 if (HasState(ui::AX_STATE_CHECKED))
3600 ia_state |= STATE_SYSTEM_CHECKED;
3601 if (HasState(ui::AX_STATE_COLLAPSED))
3602 ia_state |= STATE_SYSTEM_COLLAPSED;
3603 if (HasState(ui::AX_STATE_EXPANDED))
3604 ia_state |= STATE_SYSTEM_EXPANDED;
3605 if (HasState(ui::AX_STATE_FOCUSABLE))
3606 ia_state |= STATE_SYSTEM_FOCUSABLE;
3607 if (HasState(ui::AX_STATE_HASPOPUP))
3608 ia_state |= STATE_SYSTEM_HASPOPUP;
3609 if (HasState(ui::AX_STATE_INDETERMINATE))
3610 ia_state |= STATE_SYSTEM_INDETERMINATE;
3611 if (HasIntAttribute(ui::AX_ATTR_INVALID_STATE) &&
3612 GetIntAttribute(ui::AX_ATTR_INVALID_STATE) != ui::AX_INVALID_STATE_FALSE)
3613 ia2_state |= IA2_STATE_INVALID_ENTRY;
3614 if (HasState(ui::AX_STATE_INVISIBLE))
3615 ia_state |= STATE_SYSTEM_INVISIBLE;
3616 if (HasState(ui::AX_STATE_LINKED))
3617 ia_state |= STATE_SYSTEM_LINKED;
3618 if (HasState(ui::AX_STATE_MULTISELECTABLE)) {
3619 ia_state |= STATE_SYSTEM_EXTSELECTABLE;
3620 ia_state |= STATE_SYSTEM_MULTISELECTABLE;
3622 // TODO(ctguil): Support STATE_SYSTEM_EXTSELECTABLE/accSelect.
3623 if (HasState(ui::AX_STATE_OFFSCREEN))
3624 ia_state |= STATE_SYSTEM_OFFSCREEN;
3625 if (HasState(ui::AX_STATE_PRESSED))
3626 ia_state |= STATE_SYSTEM_PRESSED;
3627 if (HasState(ui::AX_STATE_PROTECTED))
3628 ia_state |= STATE_SYSTEM_PROTECTED;
3629 if (HasState(ui::AX_STATE_REQUIRED))
3630 ia2_state |= IA2_STATE_REQUIRED;
3631 if (HasState(ui::AX_STATE_SELECTABLE))
3632 ia_state |= STATE_SYSTEM_SELECTABLE;
3633 if (HasState(ui::AX_STATE_SELECTED))
3634 ia_state |= STATE_SYSTEM_SELECTED;
3635 if (HasState(ui::AX_STATE_VISITED))
3636 ia_state |= STATE_SYSTEM_TRAVERSED;
3637 if (!HasState(ui::AX_STATE_ENABLED))
3638 ia_state |= STATE_SYSTEM_UNAVAILABLE;
3639 if (HasState(ui::AX_STATE_VERTICAL))
3640 ia2_state |= IA2_STATE_VERTICAL;
3641 if (HasState(ui::AX_STATE_HORIZONTAL))
3642 ia2_state |= IA2_STATE_HORIZONTAL;
3643 if (HasState(ui::AX_STATE_VISITED))
3644 ia_state |= STATE_SYSTEM_TRAVERSED;
3646 // Expose whether or not the mouse is over an element, but suppress
3647 // this for tests because it can make the test results flaky depending
3648 // on the position of the mouse.
3649 BrowserAccessibilityStateImpl* accessibility_state =
3650 BrowserAccessibilityStateImpl::GetInstance();
3651 if (!accessibility_state->disable_hot_tracking_for_testing()) {
3652 if (HasState(ui::AX_STATE_HOVERED))
3653 ia_state |= STATE_SYSTEM_HOTTRACKED;
3656 // WebKit marks everything as readonly unless it's editable text, so if it's
3657 // not readonly, mark it as editable now. The final computation of the
3658 // READONLY state for MSAA is below, after the switch.
3659 if (!HasState(ui::AX_STATE_READ_ONLY))
3660 ia2_state |= IA2_STATE_EDITABLE;
3662 if (GetBoolAttribute(ui::AX_ATTR_BUTTON_MIXED))
3663 ia_state |= STATE_SYSTEM_MIXED;
3665 if (GetBoolAttribute(ui::AX_ATTR_CAN_SET_VALUE))
3666 ia2_state |= IA2_STATE_EDITABLE;
3668 if (!GetStringAttribute(ui::AX_ATTR_AUTO_COMPLETE).empty())
3669 ia2_state |= IA2_STATE_SUPPORTS_AUTOCOMPLETION;
3671 base::string16 html_tag = GetString16Attribute(
3672 ui::AX_ATTR_HTML_TAG);
3673 switch (GetRole()) {
3674 case ui::AX_ROLE_ALERT:
3675 ia_role = ROLE_SYSTEM_ALERT;
3676 break;
3677 case ui::AX_ROLE_ALERT_DIALOG:
3678 ia_role = ROLE_SYSTEM_DIALOG;
3679 break;
3680 case ui::AX_ROLE_APPLICATION:
3681 ia_role = ROLE_SYSTEM_APPLICATION;
3682 break;
3683 case ui::AX_ROLE_ARTICLE:
3684 ia_role = ROLE_SYSTEM_DOCUMENT;
3685 ia_state |= STATE_SYSTEM_READONLY;
3686 break;
3687 case ui::AX_ROLE_BANNER:
3688 ia_role = ROLE_SYSTEM_GROUPING;
3689 ia2_role = IA2_ROLE_HEADER;
3690 break;
3691 case ui::AX_ROLE_BLOCKQUOTE:
3692 role_name = html_tag;
3693 ia2_role = IA2_ROLE_SECTION;
3694 break;
3695 case ui::AX_ROLE_BUSY_INDICATOR:
3696 ia_role = ROLE_SYSTEM_ANIMATION;
3697 ia_state |= STATE_SYSTEM_READONLY;
3698 break;
3699 case ui::AX_ROLE_BUTTON:
3700 ia_role = ROLE_SYSTEM_PUSHBUTTON;
3701 break;
3702 case ui::AX_ROLE_CANVAS:
3703 if (GetBoolAttribute(ui::AX_ATTR_CANVAS_HAS_FALLBACK)) {
3704 role_name = L"canvas";
3705 ia2_role = IA2_ROLE_CANVAS;
3706 } else {
3707 ia_role = ROLE_SYSTEM_GRAPHIC;
3709 break;
3710 case ui::AX_ROLE_CAPTION:
3711 ia_role = ROLE_SYSTEM_TEXT;
3712 ia2_role = IA2_ROLE_CAPTION;
3713 break;
3714 case ui::AX_ROLE_CELL:
3715 ia_role = ROLE_SYSTEM_CELL;
3716 break;
3717 case ui::AX_ROLE_CHECK_BOX:
3718 ia_role = ROLE_SYSTEM_CHECKBUTTON;
3719 ia2_state |= IA2_STATE_CHECKABLE;
3720 break;
3721 case ui::AX_ROLE_COLOR_WELL:
3722 ia_role = ROLE_SYSTEM_TEXT;
3723 ia2_role = IA2_ROLE_COLOR_CHOOSER;
3724 break;
3725 case ui::AX_ROLE_COLUMN:
3726 ia_role = ROLE_SYSTEM_COLUMN;
3727 break;
3728 case ui::AX_ROLE_COLUMN_HEADER:
3729 ia_role = ROLE_SYSTEM_COLUMNHEADER;
3730 break;
3731 case ui::AX_ROLE_COMBO_BOX:
3732 ia_role = ROLE_SYSTEM_COMBOBOX;
3733 break;
3734 case ui::AX_ROLE_COMPLEMENTARY:
3735 ia_role = ROLE_SYSTEM_GROUPING;
3736 ia2_role = IA2_ROLE_NOTE;
3737 break;
3738 case ui::AX_ROLE_CONTENT_INFO:
3739 ia_role = ROLE_SYSTEM_TEXT;
3740 ia2_role = IA2_ROLE_PARAGRAPH;
3741 break;
3742 case ui::AX_ROLE_DATE:
3743 case ui::AX_ROLE_DATE_TIME:
3744 ia_role = ROLE_SYSTEM_DROPLIST;
3745 ia2_role = IA2_ROLE_DATE_EDITOR;
3746 break;
3747 case ui::AX_ROLE_DIV:
3748 role_name = L"div";
3749 ia_role = ROLE_SYSTEM_GROUPING;
3750 ia2_role = IA2_ROLE_SECTION;
3751 break;
3752 case ui::AX_ROLE_DEFINITION:
3753 role_name = html_tag;
3754 ia2_role = IA2_ROLE_PARAGRAPH;
3755 ia_state |= STATE_SYSTEM_READONLY;
3756 break;
3757 case ui::AX_ROLE_DESCRIPTION_LIST_DETAIL:
3758 role_name = html_tag;
3759 ia_role = ROLE_SYSTEM_TEXT;
3760 ia2_role = IA2_ROLE_PARAGRAPH;
3761 break;
3762 case ui::AX_ROLE_DESCRIPTION_LIST:
3763 role_name = html_tag;
3764 ia_role = ROLE_SYSTEM_LIST;
3765 ia_state |= STATE_SYSTEM_READONLY;
3766 break;
3767 case ui::AX_ROLE_DESCRIPTION_LIST_TERM:
3768 ia_role = ROLE_SYSTEM_LISTITEM;
3769 ia_state |= STATE_SYSTEM_READONLY;
3770 break;
3771 case ui::AX_ROLE_DETAILS:
3772 role_name = html_tag;
3773 ia_role = ROLE_SYSTEM_GROUPING;
3774 break;
3775 case ui::AX_ROLE_DIALOG:
3776 ia_role = ROLE_SYSTEM_DIALOG;
3777 break;
3778 case ui::AX_ROLE_DISCLOSURE_TRIANGLE:
3779 ia_role = ROLE_SYSTEM_PUSHBUTTON;
3780 break;
3781 case ui::AX_ROLE_DOCUMENT:
3782 case ui::AX_ROLE_ROOT_WEB_AREA:
3783 case ui::AX_ROLE_WEB_AREA:
3784 ia_role = ROLE_SYSTEM_DOCUMENT;
3785 ia_state |= STATE_SYSTEM_READONLY;
3786 ia_state |= STATE_SYSTEM_FOCUSABLE;
3787 break;
3788 case ui::AX_ROLE_EMBEDDED_OBJECT:
3789 ia_role = ROLE_SYSTEM_CLIENT;
3790 ia2_role = IA2_ROLE_EMBEDDED_OBJECT;
3791 break;
3792 case ui::AX_ROLE_FIGCAPTION:
3793 role_name = html_tag;
3794 ia2_role = IA2_ROLE_CAPTION;
3795 break;
3796 case ui::AX_ROLE_FIGURE:
3797 ia_role = ROLE_SYSTEM_GROUPING;
3798 break;
3799 case ui::AX_ROLE_FORM:
3800 role_name = L"form";
3801 ia2_role = IA2_ROLE_FORM;
3802 break;
3803 case ui::AX_ROLE_FOOTER:
3804 ia_role = ROLE_SYSTEM_GROUPING;
3805 ia2_role = IA2_ROLE_FOOTER;
3806 break;
3807 case ui::AX_ROLE_GRID:
3808 ia_role = ROLE_SYSTEM_TABLE;
3809 ia_state |= STATE_SYSTEM_READONLY;
3810 break;
3811 case ui::AX_ROLE_GROUP: {
3812 base::string16 aria_role = GetString16Attribute(
3813 ui::AX_ATTR_ROLE);
3814 if (aria_role == L"group" || html_tag == L"fieldset") {
3815 ia_role = ROLE_SYSTEM_GROUPING;
3816 } else if (html_tag == L"li") {
3817 ia_role = ROLE_SYSTEM_LISTITEM;
3818 ia_state |= STATE_SYSTEM_READONLY;
3819 } else {
3820 if (html_tag.empty())
3821 role_name = L"div";
3822 else
3823 role_name = html_tag;
3824 ia2_role = IA2_ROLE_SECTION;
3826 break;
3828 case ui::AX_ROLE_HEADING:
3829 role_name = html_tag;
3830 ia2_role = IA2_ROLE_HEADING;
3831 break;
3832 case ui::AX_ROLE_IFRAME:
3833 ia_role = ROLE_SYSTEM_DOCUMENT;
3834 ia2_role = IA2_ROLE_INTERNAL_FRAME;
3835 ia_state = STATE_SYSTEM_READONLY;
3836 break;
3837 case ui::AX_ROLE_IFRAME_PRESENTATIONAL:
3838 ia_role = ROLE_SYSTEM_GROUPING;
3839 break;
3840 case ui::AX_ROLE_IMAGE:
3841 ia_role = ROLE_SYSTEM_GRAPHIC;
3842 ia_state |= STATE_SYSTEM_READONLY;
3843 break;
3844 case ui::AX_ROLE_IMAGE_MAP:
3845 role_name = html_tag;
3846 ia2_role = IA2_ROLE_IMAGE_MAP;
3847 ia_state |= STATE_SYSTEM_READONLY;
3848 break;
3849 case ui::AX_ROLE_IMAGE_MAP_LINK:
3850 ia_role = ROLE_SYSTEM_LINK;
3851 ia_state |= STATE_SYSTEM_LINKED;
3852 ia_state |= STATE_SYSTEM_READONLY;
3853 break;
3854 case ui::AX_ROLE_LABEL_TEXT:
3855 case ui::AX_ROLE_LEGEND:
3856 ia_role = ROLE_SYSTEM_TEXT;
3857 ia2_role = IA2_ROLE_LABEL;
3858 break;
3859 case ui::AX_ROLE_LINK:
3860 ia_role = ROLE_SYSTEM_LINK;
3861 ia_state |= STATE_SYSTEM_LINKED;
3862 break;
3863 case ui::AX_ROLE_LIST:
3864 ia_role = ROLE_SYSTEM_LIST;
3865 ia_state |= STATE_SYSTEM_READONLY;
3866 break;
3867 case ui::AX_ROLE_LIST_BOX:
3868 ia_role = ROLE_SYSTEM_LIST;
3869 break;
3870 case ui::AX_ROLE_LIST_BOX_OPTION:
3871 ia_role = ROLE_SYSTEM_LISTITEM;
3872 if (ia_state & STATE_SYSTEM_SELECTABLE) {
3873 ia_state |= STATE_SYSTEM_FOCUSABLE;
3874 if (HasState(ui::AX_STATE_FOCUSED))
3875 ia_state |= STATE_SYSTEM_FOCUSED;
3877 break;
3878 case ui::AX_ROLE_LIST_ITEM:
3879 ia_role = ROLE_SYSTEM_LISTITEM;
3880 ia_state |= STATE_SYSTEM_READONLY;
3881 break;
3882 case ui::AX_ROLE_MAIN:
3883 ia_role = ROLE_SYSTEM_GROUPING;
3884 ia2_role = IA2_ROLE_PARAGRAPH;
3885 break;
3886 case ui::AX_ROLE_MARQUEE:
3887 ia_role = ROLE_SYSTEM_ANIMATION;
3888 break;
3889 case ui::AX_ROLE_MATH:
3890 ia_role = ROLE_SYSTEM_EQUATION;
3891 break;
3892 case ui::AX_ROLE_MENU:
3893 case ui::AX_ROLE_MENU_BUTTON:
3894 ia_role = ROLE_SYSTEM_MENUPOPUP;
3895 break;
3896 case ui::AX_ROLE_MENU_BAR:
3897 ia_role = ROLE_SYSTEM_MENUBAR;
3898 break;
3899 case ui::AX_ROLE_MENU_ITEM:
3900 ia_role = ROLE_SYSTEM_MENUITEM;
3901 break;
3902 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
3903 ia_role = ROLE_SYSTEM_MENUITEM;
3904 ia2_role = IA2_ROLE_CHECK_MENU_ITEM;
3905 ia2_state |= IA2_STATE_CHECKABLE;
3906 break;
3907 case ui::AX_ROLE_MENU_ITEM_RADIO:
3908 ia_role = ROLE_SYSTEM_MENUITEM;
3909 ia2_role = IA2_ROLE_RADIO_MENU_ITEM;
3910 break;
3911 case ui::AX_ROLE_MENU_LIST_POPUP:
3912 ia_role = ROLE_SYSTEM_CLIENT;
3913 break;
3914 case ui::AX_ROLE_MENU_LIST_OPTION:
3915 ia_role = ROLE_SYSTEM_LISTITEM;
3916 if (ia_state & STATE_SYSTEM_SELECTABLE) {
3917 ia_state |= STATE_SYSTEM_FOCUSABLE;
3918 if (HasState(ui::AX_STATE_FOCUSED))
3919 ia_state |= STATE_SYSTEM_FOCUSED;
3921 break;
3922 case ui::AX_ROLE_METER:
3923 role_name = html_tag;
3924 ia_role = ROLE_SYSTEM_PROGRESSBAR;
3925 break;
3926 case ui::AX_ROLE_NAVIGATION:
3927 ia_role = ROLE_SYSTEM_GROUPING;
3928 ia2_role = IA2_ROLE_SECTION;
3929 break;
3930 case ui::AX_ROLE_NOTE:
3931 ia_role = ROLE_SYSTEM_GROUPING;
3932 ia2_role = IA2_ROLE_NOTE;
3933 break;
3934 case ui::AX_ROLE_OUTLINE:
3935 ia_role = ROLE_SYSTEM_OUTLINE;
3936 break;
3937 case ui::AX_ROLE_PARAGRAPH:
3938 role_name = L"P";
3939 ia2_role = IA2_ROLE_PARAGRAPH;
3940 break;
3941 case ui::AX_ROLE_POP_UP_BUTTON:
3942 if (html_tag == L"select") {
3943 ia_role = ROLE_SYSTEM_COMBOBOX;
3944 } else {
3945 ia_role = ROLE_SYSTEM_BUTTONMENU;
3947 break;
3948 case ui::AX_ROLE_PRE:
3949 role_name = html_tag;
3950 ia_role = ROLE_SYSTEM_TEXT;
3951 ia2_role = IA2_ROLE_PARAGRAPH;
3952 break;
3953 case ui::AX_ROLE_PROGRESS_INDICATOR:
3954 ia_role = ROLE_SYSTEM_PROGRESSBAR;
3955 ia_state |= STATE_SYSTEM_READONLY;
3956 break;
3957 case ui::AX_ROLE_RADIO_BUTTON:
3958 ia_role = ROLE_SYSTEM_RADIOBUTTON;
3959 ia2_state = IA2_STATE_CHECKABLE;
3960 break;
3961 case ui::AX_ROLE_RADIO_GROUP:
3962 ia_role = ROLE_SYSTEM_GROUPING;
3963 break;
3964 case ui::AX_ROLE_REGION:
3965 if (html_tag == L"section") {
3966 ia_role = ROLE_SYSTEM_GROUPING;
3967 ia2_role = IA2_ROLE_SECTION;
3968 } else {
3969 ia_role = ROLE_SYSTEM_PANE;
3971 break;
3972 case ui::AX_ROLE_ROW:
3973 ia_role = ROLE_SYSTEM_ROW;
3974 break;
3975 case ui::AX_ROLE_ROW_HEADER:
3976 ia_role = ROLE_SYSTEM_ROWHEADER;
3977 break;
3978 case ui::AX_ROLE_RUBY:
3979 ia_role = ROLE_SYSTEM_TEXT;
3980 ia2_role = IA2_ROLE_TEXT_FRAME;
3981 break;
3982 case ui::AX_ROLE_RULER:
3983 ia_role = ROLE_SYSTEM_CLIENT;
3984 ia2_role = IA2_ROLE_RULER;
3985 ia_state |= STATE_SYSTEM_READONLY;
3986 break;
3987 case ui::AX_ROLE_SCROLL_AREA:
3988 ia_role = ROLE_SYSTEM_CLIENT;
3989 ia2_role = IA2_ROLE_SCROLL_PANE;
3990 ia_state |= STATE_SYSTEM_READONLY;
3991 ia2_state &= ~(IA2_STATE_EDITABLE);
3992 break;
3993 case ui::AX_ROLE_SCROLL_BAR:
3994 ia_role = ROLE_SYSTEM_SCROLLBAR;
3995 break;
3996 case ui::AX_ROLE_SEARCH:
3997 ia_role = ROLE_SYSTEM_GROUPING;
3998 ia2_role = IA2_ROLE_SECTION;
3999 break;
4000 case ui::AX_ROLE_SLIDER:
4001 ia_role = ROLE_SYSTEM_SLIDER;
4002 break;
4003 case ui::AX_ROLE_SPIN_BUTTON:
4004 ia_role = ROLE_SYSTEM_SPINBUTTON;
4005 break;
4006 case ui::AX_ROLE_SPIN_BUTTON_PART:
4007 ia_role = ROLE_SYSTEM_PUSHBUTTON;
4008 break;
4009 case ui::AX_ROLE_ANNOTATION:
4010 case ui::AX_ROLE_LIST_MARKER:
4011 case ui::AX_ROLE_STATIC_TEXT:
4012 ia_role = ROLE_SYSTEM_STATICTEXT;
4013 break;
4014 case ui::AX_ROLE_STATUS:
4015 ia_role = ROLE_SYSTEM_STATUSBAR;
4016 break;
4017 case ui::AX_ROLE_SPLITTER:
4018 ia_role = ROLE_SYSTEM_SEPARATOR;
4019 break;
4020 case ui::AX_ROLE_SVG_ROOT:
4021 ia_role = ROLE_SYSTEM_GRAPHIC;
4022 break;
4023 case ui::AX_ROLE_SWITCH:
4024 role_name = L"switch";
4025 ia2_role = IA2_ROLE_TOGGLE_BUTTON;
4026 break;
4027 case ui::AX_ROLE_TAB:
4028 ia_role = ROLE_SYSTEM_PAGETAB;
4029 break;
4030 case ui::AX_ROLE_TABLE: {
4031 base::string16 aria_role = GetString16Attribute(
4032 ui::AX_ATTR_ROLE);
4033 if (aria_role == L"treegrid") {
4034 ia_role = ROLE_SYSTEM_OUTLINE;
4035 } else {
4036 ia_role = ROLE_SYSTEM_TABLE;
4038 break;
4040 case ui::AX_ROLE_TABLE_HEADER_CONTAINER:
4041 ia_role = ROLE_SYSTEM_GROUPING;
4042 ia2_role = IA2_ROLE_SECTION;
4043 ia_state |= STATE_SYSTEM_READONLY;
4044 break;
4045 case ui::AX_ROLE_TAB_LIST:
4046 ia_role = ROLE_SYSTEM_PAGETABLIST;
4047 break;
4048 case ui::AX_ROLE_TAB_PANEL:
4049 ia_role = ROLE_SYSTEM_PROPERTYPAGE;
4050 break;
4051 case ui::AX_ROLE_TOGGLE_BUTTON:
4052 ia_role = ROLE_SYSTEM_PUSHBUTTON;
4053 ia2_role = IA2_ROLE_TOGGLE_BUTTON;
4054 break;
4055 case ui::AX_ROLE_TEXT_FIELD:
4056 case ui::AX_ROLE_SEARCH_BOX:
4057 ia_role = ROLE_SYSTEM_TEXT;
4058 if (HasState(ui::AX_STATE_MULTILINE))
4059 ia2_state |= IA2_STATE_MULTI_LINE;
4060 else
4061 ia2_state |= IA2_STATE_SINGLE_LINE;
4062 ia2_state |= IA2_STATE_EDITABLE;
4063 ia2_state |= IA2_STATE_SELECTABLE_TEXT;
4064 break;
4065 case ui::AX_ROLE_TIME:
4066 ia_role = ROLE_SYSTEM_SPINBUTTON;
4067 break;
4068 case ui::AX_ROLE_TIMER:
4069 ia_role = ROLE_SYSTEM_CLOCK;
4070 ia_state |= STATE_SYSTEM_READONLY;
4071 break;
4072 case ui::AX_ROLE_TOOLBAR:
4073 ia_role = ROLE_SYSTEM_TOOLBAR;
4074 ia_state |= STATE_SYSTEM_READONLY;
4075 break;
4076 case ui::AX_ROLE_TOOLTIP:
4077 ia_role = ROLE_SYSTEM_TOOLTIP;
4078 ia_state |= STATE_SYSTEM_READONLY;
4079 break;
4080 case ui::AX_ROLE_TREE:
4081 ia_role = ROLE_SYSTEM_OUTLINE;
4082 break;
4083 case ui::AX_ROLE_TREE_GRID:
4084 ia_role = ROLE_SYSTEM_OUTLINE;
4085 break;
4086 case ui::AX_ROLE_TREE_ITEM:
4087 ia_role = ROLE_SYSTEM_OUTLINEITEM;
4088 break;
4089 case ui::AX_ROLE_LINE_BREAK:
4090 ia_role = ROLE_SYSTEM_WHITESPACE;
4091 break;
4092 case ui::AX_ROLE_WINDOW:
4093 ia_role = ROLE_SYSTEM_WINDOW;
4094 break;
4096 // TODO(dmazzoni): figure out the proper MSAA role for all of these.
4097 case ui::AX_ROLE_DIRECTORY:
4098 case ui::AX_ROLE_IGNORED:
4099 case ui::AX_ROLE_LOG:
4100 case ui::AX_ROLE_NONE:
4101 case ui::AX_ROLE_PRESENTATIONAL:
4102 case ui::AX_ROLE_SLIDER_THUMB:
4103 default:
4104 ia_role = ROLE_SYSTEM_CLIENT;
4105 break;
4108 // Compute the final value of READONLY for MSAA.
4110 // We always set the READONLY state for elements that have the
4111 // aria-readonly attribute and for a few roles (in the switch above).
4112 // We clear the READONLY state on focusable controls and on a document.
4113 // Everything else, the majority of objects, do not have this state set.
4114 if (HasState(ui::AX_STATE_FOCUSABLE) &&
4115 ia_role != ROLE_SYSTEM_DOCUMENT) {
4116 ia_state &= ~(STATE_SYSTEM_READONLY);
4118 if (!HasState(ui::AX_STATE_READ_ONLY))
4119 ia_state &= ~(STATE_SYSTEM_READONLY);
4120 if (GetBoolAttribute(ui::AX_ATTR_ARIA_READONLY))
4121 ia_state |= STATE_SYSTEM_READONLY;
4123 // The role should always be set.
4124 DCHECK(!role_name.empty() || ia_role);
4126 // If we didn't explicitly set the IAccessible2 role, make it the same
4127 // as the MSAA role.
4128 if (!ia2_role)
4129 ia2_role = ia_role;
4131 win_attributes_->ia_role = ia_role;
4132 win_attributes_->ia_state = ia_state;
4133 win_attributes_->role_name = role_name;
4134 win_attributes_->ia2_role = ia2_role;
4135 win_attributes_->ia2_state = ia2_state;
4138 } // namespace content