Fixed IAccessibleText::TextAtOffset with IA2_TEXT_BOUNDARY_WORD to return text that...
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_win.cc
blob228fb6307f9d7aad9cc20e30f17abbe85a1ba6bd
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 if (GetRole() == ui::AX_ROLE_LIST_BOX_OPTION &&
907 GetParent() &&
908 GetParent()->GetRole() == ui::AX_ROLE_LIST_BOX) {
909 *group_level = 0;
910 *similar_items_in_group = GetParent()->PlatformChildCount();
911 *position_in_group = GetIndexInParent() + 1;
912 return S_OK;
915 return E_NOTIMPL;
919 // IAccessibleApplication methods.
922 STDMETHODIMP BrowserAccessibilityWin::get_appName(BSTR* app_name) {
923 // No need to check |instance_active()| because this interface is
924 // global, and doesn't depend on any local state.
926 if (!app_name)
927 return E_INVALIDARG;
929 // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out
930 // the part before the "/".
931 std::vector<std::string> product_components;
932 base::SplitString(GetContentClient()->GetProduct(), '/', &product_components);
933 DCHECK_EQ(2U, product_components.size());
934 if (product_components.size() != 2)
935 return E_FAIL;
936 *app_name = SysAllocString(base::UTF8ToUTF16(product_components[0]).c_str());
937 DCHECK(*app_name);
938 return *app_name ? S_OK : E_FAIL;
941 STDMETHODIMP BrowserAccessibilityWin::get_appVersion(BSTR* app_version) {
942 // No need to check |instance_active()| because this interface is
943 // global, and doesn't depend on any local state.
945 if (!app_version)
946 return E_INVALIDARG;
948 // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out
949 // the part after the "/".
950 std::vector<std::string> product_components;
951 base::SplitString(GetContentClient()->GetProduct(), '/', &product_components);
952 DCHECK_EQ(2U, product_components.size());
953 if (product_components.size() != 2)
954 return E_FAIL;
955 *app_version =
956 SysAllocString(base::UTF8ToUTF16(product_components[1]).c_str());
957 DCHECK(*app_version);
958 return *app_version ? S_OK : E_FAIL;
961 STDMETHODIMP BrowserAccessibilityWin::get_toolkitName(BSTR* toolkit_name) {
962 // No need to check |instance_active()| because this interface is
963 // global, and doesn't depend on any local state.
965 if (!toolkit_name)
966 return E_INVALIDARG;
968 // This is hard-coded; all products based on the Chromium engine
969 // will have the same toolkit name, so that assistive technology can
970 // detect any Chrome-based product.
971 *toolkit_name = SysAllocString(L"Chrome");
972 DCHECK(*toolkit_name);
973 return *toolkit_name ? S_OK : E_FAIL;
976 STDMETHODIMP BrowserAccessibilityWin::get_toolkitVersion(
977 BSTR* toolkit_version) {
978 // No need to check |instance_active()| because this interface is
979 // global, and doesn't depend on any local state.
981 if (!toolkit_version)
982 return E_INVALIDARG;
984 std::string user_agent = GetContentClient()->GetUserAgent();
985 *toolkit_version = SysAllocString(base::UTF8ToUTF16(user_agent).c_str());
986 DCHECK(*toolkit_version);
987 return *toolkit_version ? S_OK : E_FAIL;
991 // IAccessibleImage methods.
994 STDMETHODIMP BrowserAccessibilityWin::get_description(BSTR* desc) {
995 if (!instance_active())
996 return E_FAIL;
998 if (!desc)
999 return E_INVALIDARG;
1001 if (description().empty())
1002 return S_FALSE;
1004 *desc = SysAllocString(description().c_str());
1006 DCHECK(*desc);
1007 return S_OK;
1010 STDMETHODIMP BrowserAccessibilityWin::get_imagePosition(
1011 enum IA2CoordinateType coordinate_type,
1012 LONG* x,
1013 LONG* y) {
1014 if (!instance_active())
1015 return E_FAIL;
1017 if (!x || !y)
1018 return E_INVALIDARG;
1020 if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
1021 HWND parent_hwnd =
1022 manager()->ToBrowserAccessibilityManagerWin()->GetParentHWND();
1023 if (!parent_hwnd)
1024 return E_FAIL;
1025 POINT top_left = {0, 0};
1026 ::ClientToScreen(parent_hwnd, &top_left);
1027 *x = GetLocation().x() + top_left.x;
1028 *y = GetLocation().y() + top_left.y;
1029 } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
1030 *x = GetLocation().x();
1031 *y = GetLocation().y();
1032 if (GetParent()) {
1033 *x -= GetParent()->GetLocation().x();
1034 *y -= GetParent()->GetLocation().y();
1036 } else {
1037 return E_INVALIDARG;
1040 return S_OK;
1043 STDMETHODIMP BrowserAccessibilityWin::get_imageSize(LONG* height, LONG* width) {
1044 if (!instance_active())
1045 return E_FAIL;
1047 if (!height || !width)
1048 return E_INVALIDARG;
1050 *height = GetLocation().height();
1051 *width = GetLocation().width();
1052 return S_OK;
1056 // IAccessibleTable methods.
1059 STDMETHODIMP BrowserAccessibilityWin::get_accessibleAt(
1060 long row,
1061 long column,
1062 IUnknown** accessible) {
1063 if (!instance_active())
1064 return E_FAIL;
1066 if (!accessible)
1067 return E_INVALIDARG;
1069 int columns;
1070 int rows;
1071 if (!GetIntAttribute(
1072 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1073 !GetIntAttribute(
1074 ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1075 columns <= 0 ||
1076 rows <= 0) {
1077 return S_FALSE;
1080 if (row < 0 || row >= rows || column < 0 || column >= columns)
1081 return E_INVALIDARG;
1083 const std::vector<int32>& cell_ids = GetIntListAttribute(
1084 ui::AX_ATTR_CELL_IDS);
1085 DCHECK_EQ(columns * rows, static_cast<int>(cell_ids.size()));
1087 int cell_id = cell_ids[row * columns + column];
1088 BrowserAccessibilityWin* cell = GetFromID(cell_id);
1089 if (cell) {
1090 *accessible = static_cast<IAccessible*>(cell->NewReference());
1091 return S_OK;
1094 *accessible = NULL;
1095 return E_INVALIDARG;
1098 STDMETHODIMP BrowserAccessibilityWin::get_caption(IUnknown** accessible) {
1099 if (!instance_active())
1100 return E_FAIL;
1102 if (!accessible)
1103 return E_INVALIDARG;
1105 // TODO(dmazzoni): implement
1106 return S_FALSE;
1109 STDMETHODIMP BrowserAccessibilityWin::get_childIndex(long row,
1110 long column,
1111 long* cell_index) {
1112 if (!instance_active())
1113 return E_FAIL;
1115 if (!cell_index)
1116 return E_INVALIDARG;
1118 int columns;
1119 int rows;
1120 if (!GetIntAttribute(
1121 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1122 !GetIntAttribute(
1123 ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1124 columns <= 0 ||
1125 rows <= 0) {
1126 return S_FALSE;
1129 if (row < 0 || row >= rows || column < 0 || column >= columns)
1130 return E_INVALIDARG;
1132 const std::vector<int32>& cell_ids = GetIntListAttribute(
1133 ui::AX_ATTR_CELL_IDS);
1134 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1135 ui::AX_ATTR_UNIQUE_CELL_IDS);
1136 DCHECK_EQ(columns * rows, static_cast<int>(cell_ids.size()));
1137 int cell_id = cell_ids[row * columns + column];
1138 for (size_t i = 0; i < unique_cell_ids.size(); ++i) {
1139 if (unique_cell_ids[i] == cell_id) {
1140 *cell_index = (long)i;
1141 return S_OK;
1145 return S_FALSE;
1148 STDMETHODIMP BrowserAccessibilityWin::get_columnDescription(long column,
1149 BSTR* description) {
1150 if (!instance_active())
1151 return E_FAIL;
1153 if (!description)
1154 return E_INVALIDARG;
1156 int columns;
1157 int rows;
1158 if (!GetIntAttribute(
1159 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1160 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1161 columns <= 0 ||
1162 rows <= 0) {
1163 return S_FALSE;
1166 if (column < 0 || column >= columns)
1167 return E_INVALIDARG;
1169 const std::vector<int32>& cell_ids = GetIntListAttribute(
1170 ui::AX_ATTR_CELL_IDS);
1171 for (int i = 0; i < rows; ++i) {
1172 int cell_id = cell_ids[i * columns + column];
1173 BrowserAccessibilityWin* cell = static_cast<BrowserAccessibilityWin*>(
1174 manager()->GetFromID(cell_id));
1175 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER) {
1176 base::string16 cell_name = cell->GetString16Attribute(
1177 ui::AX_ATTR_NAME);
1178 if (cell_name.size() > 0) {
1179 *description = SysAllocString(cell_name.c_str());
1180 return S_OK;
1183 if (cell->description().size() > 0) {
1184 *description = SysAllocString(cell->description().c_str());
1185 return S_OK;
1190 return S_FALSE;
1193 STDMETHODIMP BrowserAccessibilityWin::get_columnExtentAt(
1194 long row,
1195 long column,
1196 long* n_columns_spanned) {
1197 if (!instance_active())
1198 return E_FAIL;
1200 if (!n_columns_spanned)
1201 return E_INVALIDARG;
1203 int columns;
1204 int rows;
1205 if (!GetIntAttribute(
1206 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1207 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1208 columns <= 0 ||
1209 rows <= 0) {
1210 return S_FALSE;
1213 if (row < 0 || row >= rows || column < 0 || column >= columns)
1214 return E_INVALIDARG;
1216 const std::vector<int32>& cell_ids = GetIntListAttribute(
1217 ui::AX_ATTR_CELL_IDS);
1218 int cell_id = cell_ids[row * columns + column];
1219 BrowserAccessibilityWin* cell = static_cast<BrowserAccessibilityWin*>(
1220 manager()->GetFromID(cell_id));
1221 int colspan;
1222 if (cell &&
1223 cell->GetIntAttribute(
1224 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1225 colspan >= 1) {
1226 *n_columns_spanned = colspan;
1227 return S_OK;
1230 return S_FALSE;
1233 STDMETHODIMP BrowserAccessibilityWin::get_columnHeader(
1234 IAccessibleTable** accessible_table,
1235 long* starting_row_index) {
1236 // TODO(dmazzoni): implement
1237 return E_NOTIMPL;
1240 STDMETHODIMP BrowserAccessibilityWin::get_columnIndex(long cell_index,
1241 long* column_index) {
1242 if (!instance_active())
1243 return E_FAIL;
1245 if (!column_index)
1246 return E_INVALIDARG;
1248 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1249 ui::AX_ATTR_UNIQUE_CELL_IDS);
1250 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1251 if (cell_index < 0)
1252 return E_INVALIDARG;
1253 if (cell_index >= cell_id_count)
1254 return S_FALSE;
1256 int cell_id = unique_cell_ids[cell_index];
1257 BrowserAccessibilityWin* cell =
1258 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1259 int col_index;
1260 if (cell &&
1261 cell->GetIntAttribute(
1262 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &col_index)) {
1263 *column_index = col_index;
1264 return S_OK;
1267 return S_FALSE;
1270 STDMETHODIMP BrowserAccessibilityWin::get_nColumns(long* column_count) {
1271 if (!instance_active())
1272 return E_FAIL;
1274 if (!column_count)
1275 return E_INVALIDARG;
1277 int columns;
1278 if (GetIntAttribute(
1279 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns)) {
1280 *column_count = columns;
1281 return S_OK;
1284 return S_FALSE;
1287 STDMETHODIMP BrowserAccessibilityWin::get_nRows(long* row_count) {
1288 if (!instance_active())
1289 return E_FAIL;
1291 if (!row_count)
1292 return E_INVALIDARG;
1294 int rows;
1295 if (GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1296 *row_count = rows;
1297 return S_OK;
1300 return S_FALSE;
1303 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedChildren(long* cell_count) {
1304 if (!instance_active())
1305 return E_FAIL;
1307 if (!cell_count)
1308 return E_INVALIDARG;
1310 // TODO(dmazzoni): add support for selected cells/rows/columns in tables.
1311 *cell_count = 0;
1312 return S_OK;
1315 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedColumns(long* column_count) {
1316 if (!instance_active())
1317 return E_FAIL;
1319 if (!column_count)
1320 return E_INVALIDARG;
1322 *column_count = 0;
1323 return S_OK;
1326 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedRows(long* row_count) {
1327 if (!instance_active())
1328 return E_FAIL;
1330 if (!row_count)
1331 return E_INVALIDARG;
1333 *row_count = 0;
1334 return S_OK;
1337 STDMETHODIMP BrowserAccessibilityWin::get_rowDescription(long row,
1338 BSTR* description) {
1339 if (!instance_active())
1340 return E_FAIL;
1342 if (!description)
1343 return E_INVALIDARG;
1345 int columns;
1346 int rows;
1347 if (!GetIntAttribute(
1348 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1349 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1350 columns <= 0 ||
1351 rows <= 0) {
1352 return S_FALSE;
1355 if (row < 0 || row >= rows)
1356 return E_INVALIDARG;
1358 const std::vector<int32>& cell_ids = GetIntListAttribute(
1359 ui::AX_ATTR_CELL_IDS);
1360 for (int i = 0; i < columns; ++i) {
1361 int cell_id = cell_ids[row * columns + i];
1362 BrowserAccessibilityWin* cell =
1363 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1364 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER) {
1365 base::string16 cell_name = cell->GetString16Attribute(
1366 ui::AX_ATTR_NAME);
1367 if (cell_name.size() > 0) {
1368 *description = SysAllocString(cell_name.c_str());
1369 return S_OK;
1372 if (cell->description().size() > 0) {
1373 *description = SysAllocString(cell->description().c_str());
1374 return S_OK;
1379 return S_FALSE;
1382 STDMETHODIMP BrowserAccessibilityWin::get_rowExtentAt(long row,
1383 long column,
1384 long* n_rows_spanned) {
1385 if (!instance_active())
1386 return E_FAIL;
1388 if (!n_rows_spanned)
1389 return E_INVALIDARG;
1391 int columns;
1392 int rows;
1393 if (!GetIntAttribute(
1394 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1395 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1396 columns <= 0 ||
1397 rows <= 0) {
1398 return S_FALSE;
1401 if (row < 0 || row >= rows || column < 0 || column >= columns)
1402 return E_INVALIDARG;
1404 const std::vector<int32>& cell_ids = GetIntListAttribute(
1405 ui::AX_ATTR_CELL_IDS);
1406 int cell_id = cell_ids[row * columns + column];
1407 BrowserAccessibilityWin* cell =
1408 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1409 int rowspan;
1410 if (cell &&
1411 cell->GetIntAttribute(
1412 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1413 rowspan >= 1) {
1414 *n_rows_spanned = rowspan;
1415 return S_OK;
1418 return S_FALSE;
1421 STDMETHODIMP BrowserAccessibilityWin::get_rowHeader(
1422 IAccessibleTable** accessible_table,
1423 long* starting_column_index) {
1424 // TODO(dmazzoni): implement
1425 return E_NOTIMPL;
1428 STDMETHODIMP BrowserAccessibilityWin::get_rowIndex(long cell_index,
1429 long* row_index) {
1430 if (!instance_active())
1431 return E_FAIL;
1433 if (!row_index)
1434 return E_INVALIDARG;
1436 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1437 ui::AX_ATTR_UNIQUE_CELL_IDS);
1438 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1439 if (cell_index < 0)
1440 return E_INVALIDARG;
1441 if (cell_index >= cell_id_count)
1442 return S_FALSE;
1444 int cell_id = unique_cell_ids[cell_index];
1445 BrowserAccessibilityWin* cell =
1446 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1447 int cell_row_index;
1448 if (cell &&
1449 cell->GetIntAttribute(
1450 ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &cell_row_index)) {
1451 *row_index = cell_row_index;
1452 return S_OK;
1455 return S_FALSE;
1458 STDMETHODIMP BrowserAccessibilityWin::get_selectedChildren(long max_children,
1459 long** children,
1460 long* n_children) {
1461 if (!instance_active())
1462 return E_FAIL;
1464 if (!children || !n_children)
1465 return E_INVALIDARG;
1467 // TODO(dmazzoni): Implement this.
1468 *n_children = 0;
1469 return S_OK;
1472 STDMETHODIMP BrowserAccessibilityWin::get_selectedColumns(long max_columns,
1473 long** columns,
1474 long* n_columns) {
1475 if (!instance_active())
1476 return E_FAIL;
1478 if (!columns || !n_columns)
1479 return E_INVALIDARG;
1481 // TODO(dmazzoni): Implement this.
1482 *n_columns = 0;
1483 return S_OK;
1486 STDMETHODIMP BrowserAccessibilityWin::get_selectedRows(long max_rows,
1487 long** rows,
1488 long* n_rows) {
1489 if (!instance_active())
1490 return E_FAIL;
1492 if (!rows || !n_rows)
1493 return E_INVALIDARG;
1495 // TODO(dmazzoni): Implement this.
1496 *n_rows = 0;
1497 return S_OK;
1500 STDMETHODIMP BrowserAccessibilityWin::get_summary(IUnknown** accessible) {
1501 if (!instance_active())
1502 return E_FAIL;
1504 if (!accessible)
1505 return E_INVALIDARG;
1507 // TODO(dmazzoni): implement
1508 return S_FALSE;
1511 STDMETHODIMP BrowserAccessibilityWin::get_isColumnSelected(
1512 long column,
1513 boolean* is_selected) {
1514 if (!instance_active())
1515 return E_FAIL;
1517 if (!is_selected)
1518 return E_INVALIDARG;
1520 // TODO(dmazzoni): Implement this.
1521 *is_selected = false;
1522 return S_OK;
1525 STDMETHODIMP BrowserAccessibilityWin::get_isRowSelected(long row,
1526 boolean* is_selected) {
1527 if (!instance_active())
1528 return E_FAIL;
1530 if (!is_selected)
1531 return E_INVALIDARG;
1533 // TODO(dmazzoni): Implement this.
1534 *is_selected = false;
1535 return S_OK;
1538 STDMETHODIMP BrowserAccessibilityWin::get_isSelected(long row,
1539 long column,
1540 boolean* is_selected) {
1541 if (!instance_active())
1542 return E_FAIL;
1544 if (!is_selected)
1545 return E_INVALIDARG;
1547 // TODO(dmazzoni): Implement this.
1548 *is_selected = false;
1549 return S_OK;
1552 STDMETHODIMP BrowserAccessibilityWin::get_rowColumnExtentsAtIndex(
1553 long index,
1554 long* row,
1555 long* column,
1556 long* row_extents,
1557 long* column_extents,
1558 boolean* is_selected) {
1559 if (!instance_active())
1560 return E_FAIL;
1562 if (!row || !column || !row_extents || !column_extents || !is_selected)
1563 return E_INVALIDARG;
1565 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1566 ui::AX_ATTR_UNIQUE_CELL_IDS);
1567 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1568 if (index < 0)
1569 return E_INVALIDARG;
1570 if (index >= cell_id_count)
1571 return S_FALSE;
1573 int cell_id = unique_cell_ids[index];
1574 BrowserAccessibilityWin* cell =
1575 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1576 int rowspan;
1577 int colspan;
1578 if (cell &&
1579 cell->GetIntAttribute(
1580 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1581 cell->GetIntAttribute(
1582 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1583 rowspan >= 1 &&
1584 colspan >= 1) {
1585 *row_extents = rowspan;
1586 *column_extents = colspan;
1587 return S_OK;
1590 return S_FALSE;
1594 // IAccessibleTable2 methods.
1597 STDMETHODIMP BrowserAccessibilityWin::get_cellAt(long row,
1598 long column,
1599 IUnknown** cell) {
1600 return get_accessibleAt(row, column, cell);
1603 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedCells(long* cell_count) {
1604 return get_nSelectedChildren(cell_count);
1607 STDMETHODIMP BrowserAccessibilityWin::get_selectedCells(
1608 IUnknown*** cells,
1609 long* n_selected_cells) {
1610 if (!instance_active())
1611 return E_FAIL;
1613 if (!cells || !n_selected_cells)
1614 return E_INVALIDARG;
1616 // TODO(dmazzoni): Implement this.
1617 *n_selected_cells = 0;
1618 return S_OK;
1621 STDMETHODIMP BrowserAccessibilityWin::get_selectedColumns(long** columns,
1622 long* n_columns) {
1623 if (!instance_active())
1624 return E_FAIL;
1626 if (!columns || !n_columns)
1627 return E_INVALIDARG;
1629 // TODO(dmazzoni): Implement this.
1630 *n_columns = 0;
1631 return S_OK;
1634 STDMETHODIMP BrowserAccessibilityWin::get_selectedRows(long** rows,
1635 long* n_rows) {
1636 if (!instance_active())
1637 return E_FAIL;
1639 if (!rows || !n_rows)
1640 return E_INVALIDARG;
1642 // TODO(dmazzoni): Implement this.
1643 *n_rows = 0;
1644 return S_OK;
1649 // IAccessibleTableCell methods.
1652 STDMETHODIMP BrowserAccessibilityWin::get_columnExtent(
1653 long* n_columns_spanned) {
1654 if (!instance_active())
1655 return E_FAIL;
1657 if (!n_columns_spanned)
1658 return E_INVALIDARG;
1660 int colspan;
1661 if (GetIntAttribute(
1662 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1663 colspan >= 1) {
1664 *n_columns_spanned = colspan;
1665 return S_OK;
1668 return S_FALSE;
1671 STDMETHODIMP BrowserAccessibilityWin::get_columnHeaderCells(
1672 IUnknown*** cell_accessibles,
1673 long* n_column_header_cells) {
1674 if (!instance_active())
1675 return E_FAIL;
1677 if (!cell_accessibles || !n_column_header_cells)
1678 return E_INVALIDARG;
1680 *n_column_header_cells = 0;
1682 int column;
1683 if (!GetIntAttribute(
1684 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column)) {
1685 return S_FALSE;
1688 BrowserAccessibility* table = GetParent();
1689 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
1690 table = table->GetParent();
1691 if (!table) {
1692 NOTREACHED();
1693 return S_FALSE;
1696 int columns;
1697 int rows;
1698 if (!table->GetIntAttribute(
1699 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1700 !table->GetIntAttribute(
1701 ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1702 return S_FALSE;
1704 if (columns <= 0 || rows <= 0 || column < 0 || column >= columns)
1705 return S_FALSE;
1707 const std::vector<int32>& cell_ids = table->GetIntListAttribute(
1708 ui::AX_ATTR_CELL_IDS);
1710 for (int i = 0; i < rows; ++i) {
1711 int cell_id = cell_ids[i * columns + column];
1712 BrowserAccessibilityWin* cell =
1713 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1714 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER)
1715 (*n_column_header_cells)++;
1718 *cell_accessibles = static_cast<IUnknown**>(CoTaskMemAlloc(
1719 (*n_column_header_cells) * sizeof(cell_accessibles[0])));
1720 int index = 0;
1721 for (int i = 0; i < rows; ++i) {
1722 int cell_id = cell_ids[i * columns + column];
1723 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1724 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER) {
1725 (*cell_accessibles)[index] = static_cast<IAccessible*>(
1726 cell->ToBrowserAccessibilityWin()->NewReference());
1727 ++index;
1731 return S_OK;
1734 STDMETHODIMP BrowserAccessibilityWin::get_columnIndex(long* column_index) {
1735 if (!instance_active())
1736 return E_FAIL;
1738 if (!column_index)
1739 return E_INVALIDARG;
1741 int column;
1742 if (GetIntAttribute(
1743 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column)) {
1744 *column_index = column;
1745 return S_OK;
1748 return S_FALSE;
1751 STDMETHODIMP BrowserAccessibilityWin::get_rowExtent(long* n_rows_spanned) {
1752 if (!instance_active())
1753 return E_FAIL;
1755 if (!n_rows_spanned)
1756 return E_INVALIDARG;
1758 int rowspan;
1759 if (GetIntAttribute(
1760 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1761 rowspan >= 1) {
1762 *n_rows_spanned = rowspan;
1763 return S_OK;
1766 return S_FALSE;
1769 STDMETHODIMP BrowserAccessibilityWin::get_rowHeaderCells(
1770 IUnknown*** cell_accessibles,
1771 long* n_row_header_cells) {
1772 if (!instance_active())
1773 return E_FAIL;
1775 if (!cell_accessibles || !n_row_header_cells)
1776 return E_INVALIDARG;
1778 *n_row_header_cells = 0;
1780 int row;
1781 if (!GetIntAttribute(
1782 ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row)) {
1783 return S_FALSE;
1786 BrowserAccessibility* table = GetParent();
1787 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
1788 table = table->GetParent();
1789 if (!table) {
1790 NOTREACHED();
1791 return S_FALSE;
1794 int columns;
1795 int rows;
1796 if (!table->GetIntAttribute(
1797 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1798 !table->GetIntAttribute(
1799 ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1800 return S_FALSE;
1802 if (columns <= 0 || rows <= 0 || row < 0 || row >= rows)
1803 return S_FALSE;
1805 const std::vector<int32>& cell_ids = table->GetIntListAttribute(
1806 ui::AX_ATTR_CELL_IDS);
1808 for (int i = 0; i < columns; ++i) {
1809 int cell_id = cell_ids[row * columns + i];
1810 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1811 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER)
1812 (*n_row_header_cells)++;
1815 *cell_accessibles = static_cast<IUnknown**>(CoTaskMemAlloc(
1816 (*n_row_header_cells) * sizeof(cell_accessibles[0])));
1817 int index = 0;
1818 for (int i = 0; i < columns; ++i) {
1819 int cell_id = cell_ids[row * columns + i];
1820 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1821 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER) {
1822 (*cell_accessibles)[index] = static_cast<IAccessible*>(
1823 cell->ToBrowserAccessibilityWin()->NewReference());
1824 ++index;
1828 return S_OK;
1831 STDMETHODIMP BrowserAccessibilityWin::get_rowIndex(long* row_index) {
1832 if (!instance_active())
1833 return E_FAIL;
1835 if (!row_index)
1836 return E_INVALIDARG;
1838 int row;
1839 if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row)) {
1840 *row_index = row;
1841 return S_OK;
1843 return S_FALSE;
1846 STDMETHODIMP BrowserAccessibilityWin::get_isSelected(boolean* is_selected) {
1847 if (!instance_active())
1848 return E_FAIL;
1850 if (!is_selected)
1851 return E_INVALIDARG;
1853 *is_selected = false;
1854 return S_OK;
1857 STDMETHODIMP BrowserAccessibilityWin::get_rowColumnExtents(
1858 long* row_index,
1859 long* column_index,
1860 long* row_extents,
1861 long* column_extents,
1862 boolean* is_selected) {
1863 if (!instance_active())
1864 return E_FAIL;
1866 if (!row_index ||
1867 !column_index ||
1868 !row_extents ||
1869 !column_extents ||
1870 !is_selected) {
1871 return E_INVALIDARG;
1874 int row;
1875 int column;
1876 int rowspan;
1877 int colspan;
1878 if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row) &&
1879 GetIntAttribute(
1880 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column) &&
1881 GetIntAttribute(
1882 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1883 GetIntAttribute(
1884 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan)) {
1885 *row_index = row;
1886 *column_index = column;
1887 *row_extents = rowspan;
1888 *column_extents = colspan;
1889 *is_selected = false;
1890 return S_OK;
1893 return S_FALSE;
1896 STDMETHODIMP BrowserAccessibilityWin::get_table(IUnknown** table) {
1897 if (!instance_active())
1898 return E_FAIL;
1900 if (!table)
1901 return E_INVALIDARG;
1904 int row;
1905 int column;
1906 GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row);
1907 GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column);
1909 BrowserAccessibility* find_table = GetParent();
1910 while (find_table && find_table->GetRole() != ui::AX_ROLE_TABLE)
1911 find_table = find_table->GetParent();
1912 if (!find_table) {
1913 NOTREACHED();
1914 return S_FALSE;
1917 *table = static_cast<IAccessibleTable*>(
1918 find_table->ToBrowserAccessibilityWin()->NewReference());
1920 return S_OK;
1924 // IAccessibleText methods.
1927 STDMETHODIMP BrowserAccessibilityWin::get_nCharacters(LONG* n_characters) {
1928 if (!instance_active())
1929 return E_FAIL;
1931 if (!n_characters)
1932 return E_INVALIDARG;
1934 *n_characters = TextForIAccessibleText().length();
1935 return S_OK;
1938 STDMETHODIMP BrowserAccessibilityWin::get_caretOffset(LONG* offset) {
1939 if (!instance_active())
1940 return E_FAIL;
1942 if (!offset)
1943 return E_INVALIDARG;
1945 *offset = 0;
1946 if (GetRole() == ui::AX_ROLE_TEXT_FIELD ||
1947 GetRole() == ui::AX_ROLE_TEXT_AREA) {
1948 int sel_start = 0;
1949 if (GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START,
1950 &sel_start))
1951 *offset = sel_start;
1954 return S_OK;
1957 STDMETHODIMP BrowserAccessibilityWin::get_characterExtents(
1958 LONG offset,
1959 enum IA2CoordinateType coordinate_type,
1960 LONG* out_x,
1961 LONG* out_y,
1962 LONG* out_width,
1963 LONG* out_height) {
1964 if (!instance_active())
1965 return E_FAIL;
1967 if (!out_x || !out_y || !out_width || !out_height)
1968 return E_INVALIDARG;
1970 const base::string16& text_str = TextForIAccessibleText();
1971 HandleSpecialTextOffset(text_str, &offset);
1973 if (offset < 0 || offset > static_cast<LONG>(text_str.size()))
1974 return E_INVALIDARG;
1976 gfx::Rect character_bounds;
1977 if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
1978 character_bounds = GetGlobalBoundsForRange(offset, 1);
1979 } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
1980 character_bounds = GetLocalBoundsForRange(offset, 1);
1981 character_bounds -= GetLocation().OffsetFromOrigin();
1982 } else {
1983 return E_INVALIDARG;
1986 *out_x = character_bounds.x();
1987 *out_y = character_bounds.y();
1988 *out_width = character_bounds.width();
1989 *out_height = character_bounds.height();
1991 return S_OK;
1994 STDMETHODIMP BrowserAccessibilityWin::get_nSelections(LONG* n_selections) {
1995 if (!instance_active())
1996 return E_FAIL;
1998 if (!n_selections)
1999 return E_INVALIDARG;
2001 *n_selections = 0;
2002 if (GetRole() == ui::AX_ROLE_TEXT_FIELD ||
2003 GetRole() == ui::AX_ROLE_TEXT_AREA) {
2004 int sel_start = 0;
2005 int sel_end = 0;
2006 if (GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START,
2007 &sel_start) &&
2008 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end) &&
2009 sel_start != sel_end)
2010 *n_selections = 1;
2013 return S_OK;
2016 STDMETHODIMP BrowserAccessibilityWin::get_selection(LONG selection_index,
2017 LONG* start_offset,
2018 LONG* end_offset) {
2019 if (!instance_active())
2020 return E_FAIL;
2022 if (!start_offset || !end_offset || selection_index != 0)
2023 return E_INVALIDARG;
2025 *start_offset = 0;
2026 *end_offset = 0;
2027 if (GetRole() == ui::AX_ROLE_TEXT_FIELD ||
2028 GetRole() == ui::AX_ROLE_TEXT_AREA) {
2029 int sel_start = 0;
2030 int sel_end = 0;
2031 if (GetIntAttribute(
2032 ui::AX_ATTR_TEXT_SEL_START, &sel_start) &&
2033 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end)) {
2034 *start_offset = sel_start;
2035 *end_offset = sel_end;
2039 return S_OK;
2042 STDMETHODIMP BrowserAccessibilityWin::get_text(LONG start_offset,
2043 LONG end_offset,
2044 BSTR* text) {
2045 if (!instance_active())
2046 return E_FAIL;
2048 if (!text)
2049 return E_INVALIDARG;
2051 const base::string16& text_str = TextForIAccessibleText();
2053 // Handle special text offsets.
2054 HandleSpecialTextOffset(text_str, &start_offset);
2055 HandleSpecialTextOffset(text_str, &end_offset);
2057 // The spec allows the arguments to be reversed.
2058 if (start_offset > end_offset) {
2059 LONG tmp = start_offset;
2060 start_offset = end_offset;
2061 end_offset = tmp;
2064 // The spec does not allow the start or end offsets to be out or range;
2065 // we must return an error if so.
2066 LONG len = text_str.length();
2067 if (start_offset < 0)
2068 return E_INVALIDARG;
2069 if (end_offset > len)
2070 return E_INVALIDARG;
2072 base::string16 substr = text_str.substr(start_offset,
2073 end_offset - start_offset);
2074 if (substr.empty())
2075 return S_FALSE;
2077 *text = SysAllocString(substr.c_str());
2078 DCHECK(*text);
2079 return S_OK;
2082 STDMETHODIMP BrowserAccessibilityWin::get_textAtOffset(
2083 LONG offset,
2084 enum IA2TextBoundaryType boundary_type,
2085 LONG* start_offset,
2086 LONG* end_offset,
2087 BSTR* text) {
2088 if (!instance_active())
2089 return E_FAIL;
2091 if (!start_offset || !end_offset || !text)
2092 return E_INVALIDARG;
2094 const base::string16& text_str = TextForIAccessibleText();
2095 HandleSpecialTextOffset(text_str, &offset);
2096 if (offset < 0)
2097 return E_INVALIDARG;
2099 LONG text_len = text_str.length();
2100 if (offset > text_len)
2101 return E_INVALIDARG;
2103 // The IAccessible2 spec says we don't have to implement the "sentence"
2104 // boundary type, we can just let the screenreader handle it.
2105 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2106 *start_offset = 0;
2107 *end_offset = 0;
2108 *text = NULL;
2109 return S_FALSE;
2112 // According to the IA2 Spec, only line boundaries should succeed when
2113 // the offset is one past the end of the text.
2114 if (offset == text_len && boundary_type != IA2_TEXT_BOUNDARY_LINE) {
2115 *start_offset = 0;
2116 *end_offset = 0;
2117 *text = nullptr;
2118 return S_FALSE;
2121 *start_offset = FindBoundary(
2122 text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
2123 *end_offset = FindBoundary(
2124 text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
2125 return get_text(*start_offset, *end_offset, text);
2128 STDMETHODIMP BrowserAccessibilityWin::get_textBeforeOffset(
2129 LONG offset,
2130 enum IA2TextBoundaryType boundary_type,
2131 LONG* start_offset,
2132 LONG* end_offset,
2133 BSTR* text) {
2134 if (!instance_active())
2135 return E_FAIL;
2137 if (!start_offset || !end_offset || !text)
2138 return E_INVALIDARG;
2140 // The IAccessible2 spec says we don't have to implement the "sentence"
2141 // boundary type, we can just let the screenreader handle it.
2142 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2143 *start_offset = 0;
2144 *end_offset = 0;
2145 *text = NULL;
2146 return S_FALSE;
2149 const base::string16& text_str = TextForIAccessibleText();
2151 *start_offset = FindBoundary(
2152 text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
2153 *end_offset = offset;
2154 return get_text(*start_offset, *end_offset, text);
2157 STDMETHODIMP BrowserAccessibilityWin::get_textAfterOffset(
2158 LONG offset,
2159 enum IA2TextBoundaryType boundary_type,
2160 LONG* start_offset,
2161 LONG* end_offset,
2162 BSTR* text) {
2163 if (!instance_active())
2164 return E_FAIL;
2166 if (!start_offset || !end_offset || !text)
2167 return E_INVALIDARG;
2169 // The IAccessible2 spec says we don't have to implement the "sentence"
2170 // boundary type, we can just let the screenreader handle it.
2171 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2172 *start_offset = 0;
2173 *end_offset = 0;
2174 *text = NULL;
2175 return S_FALSE;
2178 const base::string16& text_str = TextForIAccessibleText();
2180 *start_offset = offset;
2181 *end_offset = FindBoundary(
2182 text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
2183 return get_text(*start_offset, *end_offset, text);
2186 STDMETHODIMP BrowserAccessibilityWin::get_newText(IA2TextSegment* new_text) {
2187 if (!instance_active())
2188 return E_FAIL;
2190 if (!new_text)
2191 return E_INVALIDARG;
2193 if (!old_win_attributes_)
2194 return E_FAIL;
2196 int start, old_len, new_len;
2197 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
2198 if (new_len == 0)
2199 return E_FAIL;
2201 base::string16 substr = hypertext().substr(start, new_len);
2202 new_text->text = SysAllocString(substr.c_str());
2203 new_text->start = static_cast<long>(start);
2204 new_text->end = static_cast<long>(start + new_len);
2205 return S_OK;
2208 STDMETHODIMP BrowserAccessibilityWin::get_oldText(IA2TextSegment* old_text) {
2209 if (!instance_active())
2210 return E_FAIL;
2212 if (!old_text)
2213 return E_INVALIDARG;
2215 if (!old_win_attributes_)
2216 return E_FAIL;
2218 int start, old_len, new_len;
2219 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
2220 if (old_len == 0)
2221 return E_FAIL;
2223 base::string16 old_hypertext = old_win_attributes_->hypertext;
2224 base::string16 substr = old_hypertext.substr(start, old_len);
2225 old_text->text = SysAllocString(substr.c_str());
2226 old_text->start = static_cast<long>(start);
2227 old_text->end = static_cast<long>(start + old_len);
2228 return S_OK;
2231 STDMETHODIMP BrowserAccessibilityWin::get_offsetAtPoint(
2232 LONG x,
2233 LONG y,
2234 enum IA2CoordinateType coord_type,
2235 LONG* offset) {
2236 if (!instance_active())
2237 return E_FAIL;
2239 if (!offset)
2240 return E_INVALIDARG;
2242 // TODO(dmazzoni): implement this. We're returning S_OK for now so that
2243 // screen readers still return partially accurate results rather than
2244 // completely failing.
2245 *offset = 0;
2246 return S_OK;
2249 STDMETHODIMP BrowserAccessibilityWin::scrollSubstringTo(
2250 LONG start_index,
2251 LONG end_index,
2252 enum IA2ScrollType scroll_type) {
2253 // TODO(dmazzoni): adjust this for the start and end index, too.
2254 return scrollTo(scroll_type);
2257 STDMETHODIMP BrowserAccessibilityWin::scrollSubstringToPoint(
2258 LONG start_index,
2259 LONG end_index,
2260 enum IA2CoordinateType coordinate_type,
2261 LONG x, LONG y) {
2262 // TODO(dmazzoni): adjust this for the start and end index, too.
2263 return scrollToPoint(coordinate_type, x, y);
2266 STDMETHODIMP BrowserAccessibilityWin::addSelection(LONG start_offset,
2267 LONG end_offset) {
2268 if (!instance_active())
2269 return E_FAIL;
2271 const base::string16& text_str = TextForIAccessibleText();
2272 HandleSpecialTextOffset(text_str, &start_offset);
2273 HandleSpecialTextOffset(text_str, &end_offset);
2275 manager()->SetTextSelection(*this, start_offset, end_offset);
2276 return S_OK;
2279 STDMETHODIMP BrowserAccessibilityWin::removeSelection(LONG selection_index) {
2280 if (!instance_active())
2281 return E_FAIL;
2283 if (selection_index != 0)
2284 return E_INVALIDARG;
2286 manager()->SetTextSelection(*this, 0, 0);
2287 return S_OK;
2290 STDMETHODIMP BrowserAccessibilityWin::setCaretOffset(LONG offset) {
2291 if (!instance_active())
2292 return E_FAIL;
2294 const base::string16& text_str = TextForIAccessibleText();
2295 HandleSpecialTextOffset(text_str, &offset);
2296 manager()->SetTextSelection(*this, offset, offset);
2297 return S_OK;
2300 STDMETHODIMP BrowserAccessibilityWin::setSelection(LONG selection_index,
2301 LONG start_offset,
2302 LONG end_offset) {
2303 if (!instance_active())
2304 return E_FAIL;
2306 if (selection_index != 0)
2307 return E_INVALIDARG;
2309 const base::string16& text_str = TextForIAccessibleText();
2310 HandleSpecialTextOffset(text_str, &start_offset);
2311 HandleSpecialTextOffset(text_str, &end_offset);
2313 manager()->SetTextSelection(*this, start_offset, end_offset);
2314 return S_OK;
2318 // IAccessibleHypertext methods.
2321 STDMETHODIMP BrowserAccessibilityWin::get_nHyperlinks(long* hyperlink_count) {
2322 if (!instance_active())
2323 return E_FAIL;
2325 if (!hyperlink_count)
2326 return E_INVALIDARG;
2328 *hyperlink_count = hyperlink_offset_to_index().size();
2329 return S_OK;
2332 STDMETHODIMP BrowserAccessibilityWin::get_hyperlink(
2333 long index,
2334 IAccessibleHyperlink** hyperlink) {
2335 if (!instance_active())
2336 return E_FAIL;
2338 if (!hyperlink ||
2339 index < 0 ||
2340 index >= static_cast<long>(hyperlinks().size())) {
2341 return E_INVALIDARG;
2344 int32 id = hyperlinks()[index];
2345 BrowserAccessibilityWin* child =
2346 manager()->GetFromID(id)->ToBrowserAccessibilityWin();
2347 if (child) {
2348 *hyperlink = static_cast<IAccessibleHyperlink*>(child->NewReference());
2349 return S_OK;
2352 return E_FAIL;
2355 STDMETHODIMP BrowserAccessibilityWin::get_hyperlinkIndex(
2356 long char_index,
2357 long* hyperlink_index) {
2358 if (!instance_active())
2359 return E_FAIL;
2361 if (!hyperlink_index)
2362 return E_INVALIDARG;
2364 *hyperlink_index = -1;
2366 if (char_index < 0 ||
2367 char_index >= static_cast<long>(hypertext().size())) {
2368 return E_INVALIDARG;
2371 std::map<int32, int32>::iterator it =
2372 hyperlink_offset_to_index().find(char_index);
2373 if (it == hyperlink_offset_to_index().end())
2374 return E_FAIL;
2376 *hyperlink_index = it->second;
2377 return S_OK;
2381 // IAccessibleValue methods.
2384 STDMETHODIMP BrowserAccessibilityWin::get_currentValue(VARIANT* value) {
2385 if (!instance_active())
2386 return E_FAIL;
2388 if (!value)
2389 return E_INVALIDARG;
2391 float float_val;
2392 if (GetFloatAttribute(
2393 ui::AX_ATTR_VALUE_FOR_RANGE, &float_val)) {
2394 value->vt = VT_R8;
2395 value->dblVal = float_val;
2396 return S_OK;
2399 value->vt = VT_EMPTY;
2400 return S_FALSE;
2403 STDMETHODIMP BrowserAccessibilityWin::get_minimumValue(VARIANT* value) {
2404 if (!instance_active())
2405 return E_FAIL;
2407 if (!value)
2408 return E_INVALIDARG;
2410 float float_val;
2411 if (GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE,
2412 &float_val)) {
2413 value->vt = VT_R8;
2414 value->dblVal = float_val;
2415 return S_OK;
2418 value->vt = VT_EMPTY;
2419 return S_FALSE;
2422 STDMETHODIMP BrowserAccessibilityWin::get_maximumValue(VARIANT* value) {
2423 if (!instance_active())
2424 return E_FAIL;
2426 if (!value)
2427 return E_INVALIDARG;
2429 float float_val;
2430 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE,
2431 &float_val)) {
2432 value->vt = VT_R8;
2433 value->dblVal = float_val;
2434 return S_OK;
2437 value->vt = VT_EMPTY;
2438 return S_FALSE;
2441 STDMETHODIMP BrowserAccessibilityWin::setCurrentValue(VARIANT new_value) {
2442 // TODO(dmazzoni): Implement this.
2443 return E_NOTIMPL;
2447 // ISimpleDOMDocument methods.
2450 STDMETHODIMP BrowserAccessibilityWin::get_URL(BSTR* url) {
2451 if (!instance_active())
2452 return E_FAIL;
2454 if (!url)
2455 return E_INVALIDARG;
2457 return GetStringAttributeAsBstr(ui::AX_ATTR_DOC_URL, url);
2460 STDMETHODIMP BrowserAccessibilityWin::get_title(BSTR* title) {
2461 if (!instance_active())
2462 return E_FAIL;
2464 if (!title)
2465 return E_INVALIDARG;
2467 return GetStringAttributeAsBstr(ui::AX_ATTR_DOC_TITLE, title);
2470 STDMETHODIMP BrowserAccessibilityWin::get_mimeType(BSTR* mime_type) {
2471 if (!instance_active())
2472 return E_FAIL;
2474 if (!mime_type)
2475 return E_INVALIDARG;
2477 return GetStringAttributeAsBstr(
2478 ui::AX_ATTR_DOC_MIMETYPE, mime_type);
2481 STDMETHODIMP BrowserAccessibilityWin::get_docType(BSTR* doc_type) {
2482 if (!instance_active())
2483 return E_FAIL;
2485 if (!doc_type)
2486 return E_INVALIDARG;
2488 return GetStringAttributeAsBstr(
2489 ui::AX_ATTR_DOC_DOCTYPE, doc_type);
2493 // ISimpleDOMNode methods.
2496 STDMETHODIMP BrowserAccessibilityWin::get_nodeInfo(
2497 BSTR* node_name,
2498 short* name_space_id,
2499 BSTR* node_value,
2500 unsigned int* num_children,
2501 unsigned int* unique_id,
2502 unsigned short* node_type) {
2503 if (!instance_active())
2504 return E_FAIL;
2506 if (!node_name || !name_space_id || !node_value || !num_children ||
2507 !unique_id || !node_type) {
2508 return E_INVALIDARG;
2511 base::string16 tag;
2512 if (GetString16Attribute(ui::AX_ATTR_HTML_TAG, &tag))
2513 *node_name = SysAllocString(tag.c_str());
2514 else
2515 *node_name = NULL;
2517 *name_space_id = 0;
2518 *node_value = SysAllocString(value().c_str());
2519 *num_children = PlatformChildCount();
2520 *unique_id = unique_id_win_;
2522 if (ia_role() == ROLE_SYSTEM_DOCUMENT) {
2523 *node_type = NODETYPE_DOCUMENT;
2524 } else if (ia_role() == ROLE_SYSTEM_TEXT &&
2525 ((ia2_state() & IA2_STATE_EDITABLE) == 0)) {
2526 *node_type = NODETYPE_TEXT;
2527 } else {
2528 *node_type = NODETYPE_ELEMENT;
2531 return S_OK;
2534 STDMETHODIMP BrowserAccessibilityWin::get_attributes(
2535 unsigned short max_attribs,
2536 BSTR* attrib_names,
2537 short* name_space_id,
2538 BSTR* attrib_values,
2539 unsigned short* num_attribs) {
2540 if (!instance_active())
2541 return E_FAIL;
2543 if (!attrib_names || !name_space_id || !attrib_values || !num_attribs)
2544 return E_INVALIDARG;
2546 *num_attribs = max_attribs;
2547 if (*num_attribs > GetHtmlAttributes().size())
2548 *num_attribs = GetHtmlAttributes().size();
2550 for (unsigned short i = 0; i < *num_attribs; ++i) {
2551 attrib_names[i] = SysAllocString(
2552 base::UTF8ToUTF16(GetHtmlAttributes()[i].first).c_str());
2553 name_space_id[i] = 0;
2554 attrib_values[i] = SysAllocString(
2555 base::UTF8ToUTF16(GetHtmlAttributes()[i].second).c_str());
2557 return S_OK;
2560 STDMETHODIMP BrowserAccessibilityWin::get_attributesForNames(
2561 unsigned short num_attribs,
2562 BSTR* attrib_names,
2563 short* name_space_id,
2564 BSTR* attrib_values) {
2565 if (!instance_active())
2566 return E_FAIL;
2568 if (!attrib_names || !name_space_id || !attrib_values)
2569 return E_INVALIDARG;
2571 for (unsigned short i = 0; i < num_attribs; ++i) {
2572 name_space_id[i] = 0;
2573 bool found = false;
2574 std::string name = base::UTF16ToUTF8((LPCWSTR)attrib_names[i]);
2575 for (unsigned int j = 0; j < GetHtmlAttributes().size(); ++j) {
2576 if (GetHtmlAttributes()[j].first == name) {
2577 attrib_values[i] = SysAllocString(
2578 base::UTF8ToUTF16(GetHtmlAttributes()[j].second).c_str());
2579 found = true;
2580 break;
2583 if (!found) {
2584 attrib_values[i] = NULL;
2587 return S_OK;
2590 STDMETHODIMP BrowserAccessibilityWin::get_computedStyle(
2591 unsigned short max_style_properties,
2592 boolean use_alternate_view,
2593 BSTR* style_properties,
2594 BSTR* style_values,
2595 unsigned short *num_style_properties) {
2596 if (!instance_active())
2597 return E_FAIL;
2599 if (!style_properties || !style_values)
2600 return E_INVALIDARG;
2602 // We only cache a single style property for now: DISPLAY
2604 base::string16 display;
2605 if (max_style_properties == 0 ||
2606 !GetString16Attribute(ui::AX_ATTR_DISPLAY, &display)) {
2607 *num_style_properties = 0;
2608 return S_OK;
2611 *num_style_properties = 1;
2612 style_properties[0] = SysAllocString(L"display");
2613 style_values[0] = SysAllocString(display.c_str());
2615 return S_OK;
2618 STDMETHODIMP BrowserAccessibilityWin::get_computedStyleForProperties(
2619 unsigned short num_style_properties,
2620 boolean use_alternate_view,
2621 BSTR* style_properties,
2622 BSTR* style_values) {
2623 if (!instance_active())
2624 return E_FAIL;
2626 if (!style_properties || !style_values)
2627 return E_INVALIDARG;
2629 // We only cache a single style property for now: DISPLAY
2631 for (unsigned short i = 0; i < num_style_properties; ++i) {
2632 base::string16 name = (LPCWSTR)style_properties[i];
2633 base::StringToLowerASCII(&name);
2634 if (name == L"display") {
2635 base::string16 display = GetString16Attribute(
2636 ui::AX_ATTR_DISPLAY);
2637 style_values[i] = SysAllocString(display.c_str());
2638 } else {
2639 style_values[i] = NULL;
2643 return S_OK;
2646 STDMETHODIMP BrowserAccessibilityWin::scrollTo(boolean placeTopLeft) {
2647 return scrollTo(placeTopLeft ?
2648 IA2_SCROLL_TYPE_TOP_LEFT : IA2_SCROLL_TYPE_ANYWHERE);
2651 STDMETHODIMP BrowserAccessibilityWin::get_parentNode(ISimpleDOMNode** node) {
2652 if (!instance_active())
2653 return E_FAIL;
2655 if (!node)
2656 return E_INVALIDARG;
2658 *node = GetParent()->ToBrowserAccessibilityWin()->NewReference();
2659 return S_OK;
2662 STDMETHODIMP BrowserAccessibilityWin::get_firstChild(ISimpleDOMNode** node) {
2663 if (!instance_active())
2664 return E_FAIL;
2666 if (!node)
2667 return E_INVALIDARG;
2669 if (PlatformChildCount() == 0) {
2670 *node = NULL;
2671 return S_FALSE;
2674 *node = PlatformGetChild(0)->ToBrowserAccessibilityWin()->NewReference();
2675 return S_OK;
2678 STDMETHODIMP BrowserAccessibilityWin::get_lastChild(ISimpleDOMNode** node) {
2679 if (!instance_active())
2680 return E_FAIL;
2682 if (!node)
2683 return E_INVALIDARG;
2685 if (PlatformChildCount() == 0) {
2686 *node = NULL;
2687 return S_FALSE;
2690 *node = PlatformGetChild(PlatformChildCount() - 1)
2691 ->ToBrowserAccessibilityWin()->NewReference();
2692 return S_OK;
2695 STDMETHODIMP BrowserAccessibilityWin::get_previousSibling(
2696 ISimpleDOMNode** node) {
2697 if (!instance_active())
2698 return E_FAIL;
2700 if (!node)
2701 return E_INVALIDARG;
2703 if (!GetParent() || GetIndexInParent() <= 0) {
2704 *node = NULL;
2705 return S_FALSE;
2708 *node = GetParent()->InternalGetChild(GetIndexInParent() - 1)->
2709 ToBrowserAccessibilityWin()->NewReference();
2710 return S_OK;
2713 STDMETHODIMP BrowserAccessibilityWin::get_nextSibling(ISimpleDOMNode** node) {
2714 if (!instance_active())
2715 return E_FAIL;
2717 if (!node)
2718 return E_INVALIDARG;
2720 if (!GetParent() ||
2721 GetIndexInParent() < 0 ||
2722 GetIndexInParent() >= static_cast<int>(
2723 GetParent()->InternalChildCount()) - 1) {
2724 *node = NULL;
2725 return S_FALSE;
2728 *node = GetParent()->InternalGetChild(GetIndexInParent() + 1)->
2729 ToBrowserAccessibilityWin()->NewReference();
2730 return S_OK;
2733 STDMETHODIMP BrowserAccessibilityWin::get_childAt(
2734 unsigned int child_index,
2735 ISimpleDOMNode** node) {
2736 if (!instance_active())
2737 return E_FAIL;
2739 if (!node)
2740 return E_INVALIDARG;
2742 if (child_index >= PlatformChildCount())
2743 return E_INVALIDARG;
2745 BrowserAccessibility* child = PlatformGetChild(child_index);
2746 if (!child) {
2747 *node = NULL;
2748 return S_FALSE;
2751 *node = child->ToBrowserAccessibilityWin()->NewReference();
2752 return S_OK;
2756 // ISimpleDOMText methods.
2759 STDMETHODIMP BrowserAccessibilityWin::get_domText(BSTR* dom_text) {
2760 if (!instance_active())
2761 return E_FAIL;
2763 if (!dom_text)
2764 return E_INVALIDARG;
2766 return GetStringAttributeAsBstr(
2767 ui::AX_ATTR_NAME, dom_text);
2770 STDMETHODIMP BrowserAccessibilityWin::get_clippedSubstringBounds(
2771 unsigned int start_index,
2772 unsigned int end_index,
2773 int* out_x,
2774 int* out_y,
2775 int* out_width,
2776 int* out_height) {
2777 // TODO(dmazzoni): fully support this API by intersecting the
2778 // rect with the container's rect.
2779 return get_unclippedSubstringBounds(
2780 start_index, end_index, out_x, out_y, out_width, out_height);
2783 STDMETHODIMP BrowserAccessibilityWin::get_unclippedSubstringBounds(
2784 unsigned int start_index,
2785 unsigned int end_index,
2786 int* out_x,
2787 int* out_y,
2788 int* out_width,
2789 int* out_height) {
2790 if (!instance_active())
2791 return E_FAIL;
2793 if (!out_x || !out_y || !out_width || !out_height)
2794 return E_INVALIDARG;
2796 const base::string16& text_str = TextForIAccessibleText();
2797 if (start_index > text_str.size() ||
2798 end_index > text_str.size() ||
2799 start_index > end_index) {
2800 return E_INVALIDARG;
2803 gfx::Rect bounds = GetGlobalBoundsForRange(
2804 start_index, end_index - start_index);
2805 *out_x = bounds.x();
2806 *out_y = bounds.y();
2807 *out_width = bounds.width();
2808 *out_height = bounds.height();
2809 return S_OK;
2812 STDMETHODIMP BrowserAccessibilityWin::scrollToSubstring(
2813 unsigned int start_index,
2814 unsigned int end_index) {
2815 if (!instance_active())
2816 return E_FAIL;
2818 const base::string16& text_str = TextForIAccessibleText();
2819 if (start_index > text_str.size() ||
2820 end_index > text_str.size() ||
2821 start_index > end_index) {
2822 return E_INVALIDARG;
2825 manager()->ScrollToMakeVisible(*this, GetLocalBoundsForRange(
2826 start_index, end_index - start_index));
2827 manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
2829 return S_OK;
2833 // IServiceProvider methods.
2836 STDMETHODIMP BrowserAccessibilityWin::QueryService(REFGUID guidService,
2837 REFIID riid,
2838 void** object) {
2839 if (!instance_active())
2840 return E_FAIL;
2842 // The system uses IAccessible APIs for many purposes, but only
2843 // assistive technology like screen readers uses IAccessible2.
2844 // Enable full accessibility support when IAccessible2 APIs are queried.
2845 if (riid == IID_IAccessible2)
2846 BrowserAccessibilityStateImpl::GetInstance()->EnableAccessibility();
2848 if (guidService == GUID_IAccessibleContentDocument) {
2849 // Special Mozilla extension: return the accessible for the root document.
2850 // Screen readers use this to distinguish between a document loaded event
2851 // on the root document vs on an iframe.
2852 return manager()->GetRoot()->ToBrowserAccessibilityWin()->QueryInterface(
2853 IID_IAccessible2, object);
2856 if (guidService == IID_IAccessible ||
2857 guidService == IID_IAccessible2 ||
2858 guidService == IID_IAccessibleAction ||
2859 guidService == IID_IAccessibleApplication ||
2860 guidService == IID_IAccessibleHyperlink ||
2861 guidService == IID_IAccessibleHypertext ||
2862 guidService == IID_IAccessibleImage ||
2863 guidService == IID_IAccessibleTable ||
2864 guidService == IID_IAccessibleTable2 ||
2865 guidService == IID_IAccessibleTableCell ||
2866 guidService == IID_IAccessibleText ||
2867 guidService == IID_IAccessibleValue ||
2868 guidService == IID_ISimpleDOMDocument ||
2869 guidService == IID_ISimpleDOMNode ||
2870 guidService == IID_ISimpleDOMText ||
2871 guidService == GUID_ISimpleDOM) {
2872 return QueryInterface(riid, object);
2875 // We only support the IAccessibleEx interface on Windows 8 and above. This
2876 // is needed for the on-screen Keyboard to show up in metro mode, when the
2877 // user taps an editable portion on the page.
2878 // All methods in the IAccessibleEx interface are unimplemented.
2879 if (riid == IID_IAccessibleEx &&
2880 base::win::GetVersion() >= base::win::VERSION_WIN8) {
2881 return QueryInterface(riid, object);
2884 *object = NULL;
2885 return E_FAIL;
2888 STDMETHODIMP BrowserAccessibilityWin::GetPatternProvider(PATTERNID id,
2889 IUnknown** provider) {
2890 DVLOG(1) << "In Function: "
2891 << __FUNCTION__
2892 << " for pattern id: "
2893 << id;
2894 if (id == UIA_ValuePatternId || id == UIA_TextPatternId) {
2895 if (IsEditableText()) {
2896 DVLOG(1) << "Returning UIA text provider";
2897 base::win::UIATextProvider::CreateTextProvider(
2898 GetValueText(), true, provider);
2899 return S_OK;
2902 return E_NOTIMPL;
2905 STDMETHODIMP BrowserAccessibilityWin::GetPropertyValue(PROPERTYID id,
2906 VARIANT* ret) {
2907 DVLOG(1) << "In Function: "
2908 << __FUNCTION__
2909 << " for property id: "
2910 << id;
2911 V_VT(ret) = VT_EMPTY;
2912 if (id == UIA_ControlTypePropertyId) {
2913 if (IsEditableText()) {
2914 V_VT(ret) = VT_I4;
2915 ret->lVal = UIA_EditControlTypeId;
2916 DVLOG(1) << "Returning Edit control type";
2917 } else {
2918 DVLOG(1) << "Returning empty control type";
2921 return S_OK;
2925 // CComObjectRootEx methods.
2928 // static
2929 HRESULT WINAPI BrowserAccessibilityWin::InternalQueryInterface(
2930 void* this_ptr,
2931 const _ATL_INTMAP_ENTRY* entries,
2932 REFIID iid,
2933 void** object) {
2934 BrowserAccessibilityWin* accessibility =
2935 reinterpret_cast<BrowserAccessibilityWin*>(this_ptr);
2936 int32 ia_role = accessibility->ia_role();
2937 if (iid == IID_IAccessibleImage) {
2938 if (ia_role != ROLE_SYSTEM_GRAPHIC) {
2939 *object = NULL;
2940 return E_NOINTERFACE;
2942 } else if (iid == IID_IAccessibleTable || iid == IID_IAccessibleTable2) {
2943 if (ia_role != ROLE_SYSTEM_TABLE) {
2944 *object = NULL;
2945 return E_NOINTERFACE;
2947 } else if (iid == IID_IAccessibleTableCell) {
2948 if (!accessibility->IsCellOrTableHeaderRole()) {
2949 *object = NULL;
2950 return E_NOINTERFACE;
2952 } else if (iid == IID_IAccessibleValue) {
2953 if (ia_role != ROLE_SYSTEM_PROGRESSBAR &&
2954 ia_role != ROLE_SYSTEM_SCROLLBAR &&
2955 ia_role != ROLE_SYSTEM_SLIDER) {
2956 *object = NULL;
2957 return E_NOINTERFACE;
2959 } else if (iid == IID_ISimpleDOMDocument) {
2960 if (ia_role != ROLE_SYSTEM_DOCUMENT) {
2961 *object = NULL;
2962 return E_NOINTERFACE;
2966 return CComObjectRootBase::InternalQueryInterface(
2967 this_ptr, entries, iid, object);
2971 // Private methods.
2974 void BrowserAccessibilityWin::UpdateStep1ComputeWinAttributes() {
2975 // Swap win_attributes_ to old_win_attributes_, allowing us to see
2976 // exactly what changed and fire appropriate events. Note that
2977 // old_win_attributes_ is cleared at the end of UpdateStep3FireEvents.
2978 old_win_attributes_.swap(win_attributes_);
2979 win_attributes_.reset(new WinAttributes());
2981 InitRoleAndState();
2983 win_attributes_->ia2_attributes.clear();
2985 // Expose autocomplete attribute for combobox and textbox.
2986 StringAttributeToIA2(ui::AX_ATTR_AUTO_COMPLETE, "autocomplete");
2988 // Expose the "display" and "tag" attributes.
2989 StringAttributeToIA2(ui::AX_ATTR_DISPLAY, "display");
2990 StringAttributeToIA2(ui::AX_ATTR_DROPEFFECT, "dropeffect");
2991 StringAttributeToIA2(ui::AX_ATTR_TEXT_INPUT_TYPE, "text-input-type");
2992 StringAttributeToIA2(ui::AX_ATTR_HTML_TAG, "tag");
2993 StringAttributeToIA2(ui::AX_ATTR_ROLE, "xml-roles");
2995 // Expose "level" attribute for headings, trees, etc.
2996 IntAttributeToIA2(ui::AX_ATTR_HIERARCHICAL_LEVEL, "level");
2998 // Expose the set size and position in set for listbox options.
2999 if (GetRole() == ui::AX_ROLE_LIST_BOX_OPTION &&
3000 GetParent() &&
3001 GetParent()->GetRole() == ui::AX_ROLE_LIST_BOX) {
3002 win_attributes_->ia2_attributes.push_back(
3003 L"setsize:" + base::IntToString16(GetParent()->PlatformChildCount()));
3004 win_attributes_->ia2_attributes.push_back(
3005 L"setsize:" + base::IntToString16(GetIndexInParent() + 1));
3008 if (ia_role() == ROLE_SYSTEM_CHECKBUTTON ||
3009 ia_role() == ROLE_SYSTEM_RADIOBUTTON ||
3010 ia2_role() == IA2_ROLE_CHECK_MENU_ITEM ||
3011 ia2_role() == IA2_ROLE_RADIO_MENU_ITEM ||
3012 ia2_role() == IA2_ROLE_TOGGLE_BUTTON) {
3013 win_attributes_->ia2_attributes.push_back(L"checkable:true");
3016 // Expose live region attributes.
3017 StringAttributeToIA2(ui::AX_ATTR_LIVE_STATUS, "live");
3018 StringAttributeToIA2(ui::AX_ATTR_LIVE_RELEVANT, "relevant");
3019 BoolAttributeToIA2(ui::AX_ATTR_LIVE_ATOMIC, "atomic");
3020 BoolAttributeToIA2(ui::AX_ATTR_LIVE_BUSY, "busy");
3022 // Expose aria-grabbed attributes.
3023 BoolAttributeToIA2(ui::AX_ATTR_GRABBED, "grabbed");
3025 // Expose container live region attributes.
3026 StringAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_STATUS,
3027 "container-live");
3028 StringAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_RELEVANT,
3029 "container-relevant");
3030 BoolAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_ATOMIC,
3031 "container-atomic");
3032 BoolAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_BUSY,
3033 "container-busy");
3035 // Expose table cell index.
3036 if (IsCellOrTableHeaderRole()) {
3037 BrowserAccessibility* table = GetParent();
3038 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
3039 table = table->GetParent();
3040 if (table) {
3041 const std::vector<int32>& unique_cell_ids = table->GetIntListAttribute(
3042 ui::AX_ATTR_UNIQUE_CELL_IDS);
3043 for (size_t i = 0; i < unique_cell_ids.size(); ++i) {
3044 if (unique_cell_ids[i] == GetId()) {
3045 win_attributes_->ia2_attributes.push_back(
3046 base::string16(L"table-cell-index:") + base::IntToString16(i));
3052 // Expose invalid state for form controls and elements with aria-invalid.
3053 int invalid_state;
3054 if (GetIntAttribute(ui::AX_ATTR_INVALID_STATE, &invalid_state)) {
3055 // TODO(nektar): Handle the possibility of having multiple aria-invalid
3056 // attributes defined, e.g., "invalid:spelling,grammar".
3057 switch (invalid_state) {
3058 case ui::AX_INVALID_STATE_FALSE:
3059 win_attributes_->ia2_attributes.push_back(L"invalid:false");
3060 break;
3061 case ui::AX_INVALID_STATE_TRUE:
3062 win_attributes_->ia2_attributes.push_back(L"invalid:true");
3063 break;
3064 case ui::AX_INVALID_STATE_SPELLING:
3065 win_attributes_->ia2_attributes.push_back(L"invalid:spelling");
3066 break;
3067 case ui::AX_INVALID_STATE_GRAMMAR:
3068 win_attributes_->ia2_attributes.push_back(L"invalid:grammar");
3069 break;
3070 case ui::AX_INVALID_STATE_OTHER:
3072 base::string16 aria_invalid_value;
3073 if (GetString16Attribute(ui::AX_ATTR_ARIA_INVALID_VALUE,
3074 &aria_invalid_value)) {
3075 win_attributes_->ia2_attributes.push_back(
3076 L"invalid:" + aria_invalid_value);
3077 } else {
3078 // Set the attribute to L"true", since we cannot be more specific.
3079 win_attributes_->ia2_attributes.push_back(L"invalid:true");
3082 break;
3083 default:
3084 NOTREACHED();
3088 // Expose row or column header sort direction.
3089 int32 sort_direction;
3090 if ((ia_role() == ROLE_SYSTEM_COLUMNHEADER ||
3091 ia_role() == ROLE_SYSTEM_ROWHEADER) &&
3092 GetIntAttribute(ui::AX_ATTR_SORT_DIRECTION, &sort_direction)) {
3093 switch (sort_direction) {
3094 case ui::AX_SORT_DIRECTION_UNSORTED:
3095 win_attributes_->ia2_attributes.push_back(L"sort:none");
3096 break;
3097 case ui::AX_SORT_DIRECTION_ASCENDING:
3098 win_attributes_->ia2_attributes.push_back(L"sort:ascending");
3099 break;
3100 case ui::AX_SORT_DIRECTION_DESCENDING:
3101 win_attributes_->ia2_attributes.push_back(L"sort:descending");
3102 break;
3103 case ui::AX_SORT_DIRECTION_OTHER:
3104 win_attributes_->ia2_attributes.push_back(L"sort:other");
3105 break;
3106 default:
3107 NOTREACHED();
3111 // The calculation of the accessible name of an element has been
3112 // standardized in the HTML to Platform Accessibility APIs Implementation
3113 // Guide (http://www.w3.org/TR/html-aapi/). In order to return the
3114 // appropriate accessible name on Windows, we need to apply some logic
3115 // to the fields we get from WebKit.
3117 // TODO(dmazzoni): move most of this logic into WebKit.
3119 // WebKit gives us:
3121 // name: the default name, e.g. inner text
3122 // title ui element: a reference to a <label> element on the same
3123 // page that labels this node.
3124 // description: accessible labels that override the default name:
3125 // aria-label or aria-labelledby or aria-describedby
3126 // help: the value of the "title" attribute
3128 // On Windows, the logic we apply lets some fields take precedence and
3129 // always returns the primary name in "name" and the secondary name,
3130 // if any, in "description".
3132 int title_elem_id = GetIntAttribute(ui::AX_ATTR_TITLE_UI_ELEMENT);
3133 base::string16 name = GetString16Attribute(ui::AX_ATTR_NAME);
3134 base::string16 description = GetString16Attribute(ui::AX_ATTR_DESCRIPTION);
3135 base::string16 help = GetString16Attribute(ui::AX_ATTR_HELP);
3136 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
3138 // WebKit annoyingly puts the title in the description if there's no other
3139 // description, which just confuses the rest of the logic. Put it back.
3140 // Now "help" is always the value of the "title" attribute, if present.
3141 base::string16 title_attr;
3142 if (GetHtmlAttribute("title", &title_attr) &&
3143 description == title_attr &&
3144 help.empty()) {
3145 help = description;
3146 description.clear();
3149 // Now implement the main logic: the descripion should become the name if
3150 // it's nonempty, and the help should become the description if
3151 // there's no description - or the name if there's no name or description.
3152 if (!description.empty()) {
3153 name = description;
3154 description.clear();
3156 if (!help.empty() && description.empty()) {
3157 description = help;
3158 help.clear();
3160 if (!description.empty() && name.empty() && !title_elem_id) {
3161 name = description;
3162 description.clear();
3165 // If it's a text field, also consider the placeholder.
3166 base::string16 placeholder;
3167 if (GetRole() == ui::AX_ROLE_TEXT_FIELD &&
3168 HasState(ui::AX_STATE_FOCUSABLE) &&
3169 GetHtmlAttribute("placeholder", &placeholder)) {
3170 if (name.empty() && !title_elem_id) {
3171 name = placeholder;
3172 } else if (description.empty()) {
3173 description = placeholder;
3177 // On Windows, the value of a document should be its url.
3178 if (GetRole() == ui::AX_ROLE_ROOT_WEB_AREA ||
3179 GetRole() == ui::AX_ROLE_WEB_AREA) {
3180 value = GetString16Attribute(ui::AX_ATTR_DOC_URL);
3183 // For certain roles (listbox option, static text, and list marker)
3184 // WebKit stores the main accessible text in the "value" - swap it so
3185 // that it's the "name".
3186 if (name.empty() &&
3187 (GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
3188 GetRole() == ui::AX_ROLE_STATIC_TEXT ||
3189 GetRole() == ui::AX_ROLE_LIST_MARKER)) {
3190 base::string16 tmp = value;
3191 value = name;
3192 name = tmp;
3195 // If this doesn't have a value and is linked then set its value to the url
3196 // attribute. This allows screen readers to read an empty link's destination.
3197 if (value.empty() && (ia_state() & STATE_SYSTEM_LINKED))
3198 value = GetString16Attribute(ui::AX_ATTR_URL);
3200 win_attributes_->name = name;
3201 win_attributes_->description = description;
3202 win_attributes_->help = help;
3203 win_attributes_->value = value;
3205 // Clear any old relationships between this node and other nodes.
3206 for (size_t i = 0; i < relations_.size(); ++i)
3207 relations_[i]->Release();
3208 relations_.clear();
3210 // Handle title UI element.
3211 if (title_elem_id) {
3212 // Add a labelled by relationship.
3213 CComObject<BrowserAccessibilityRelation>* relation;
3214 HRESULT hr = CComObject<BrowserAccessibilityRelation>::CreateInstance(
3215 &relation);
3216 DCHECK(SUCCEEDED(hr));
3217 relation->AddRef();
3218 relation->Initialize(this, IA2_RELATION_LABELLED_BY);
3219 relation->AddTarget(title_elem_id);
3220 relations_.push_back(relation);
3223 // Expose slider value.
3224 if (ia_role() == ROLE_SYSTEM_PROGRESSBAR ||
3225 ia_role() == ROLE_SYSTEM_SCROLLBAR ||
3226 ia_role() == ROLE_SYSTEM_SLIDER) {
3227 win_attributes_->ia2_attributes.push_back(L"valuetext:" + GetValueText());
3230 // If this is a web area for a presentational iframe, give it a role of
3231 // something other than DOCUMENT so that the fact that it's a separate doc
3232 // is not exposed to AT.
3233 if (IsWebAreaForPresentationalIframe()) {
3234 win_attributes_->ia_role = ROLE_SYSTEM_GROUPING;
3235 win_attributes_->ia2_role = ROLE_SYSTEM_GROUPING;
3239 void BrowserAccessibilityWin::UpdateStep2ComputeHypertext() {
3240 // Construct the hypertext for this node, which contains the concatenation
3241 // of all of the static text of this node's children and an embedded object
3242 // character for all non-static-text children. Build up a map from the
3243 // character index of each embedded object character to the id of the
3244 // child object it points to.
3245 for (unsigned int i = 0; i < PlatformChildCount(); ++i) {
3246 BrowserAccessibilityWin* child =
3247 PlatformGetChild(i)->ToBrowserAccessibilityWin();
3248 if (child->GetRole() == ui::AX_ROLE_STATIC_TEXT) {
3249 win_attributes_->hypertext += child->name();
3250 } else {
3251 int32 char_offset = hypertext().size();
3252 int32 child_id = child->GetId();
3253 int32 index = hyperlinks().size();
3254 win_attributes_->hyperlink_offset_to_index[char_offset] = index;
3255 win_attributes_->hyperlinks.push_back(child_id);
3256 win_attributes_->hypertext += kEmbeddedCharacter;
3261 void BrowserAccessibilityWin::UpdateStep3FireEvents(bool is_subtree_creation) {
3262 BrowserAccessibilityManagerWin* manager =
3263 this->manager()->ToBrowserAccessibilityManagerWin();
3265 // Fire an event when an alert first appears.
3266 if (ia_role() == ROLE_SYSTEM_ALERT &&
3267 old_win_attributes_->ia_role != ROLE_SYSTEM_ALERT) {
3268 manager->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
3271 // Fire an event when a new subtree is created.
3272 if (is_subtree_creation)
3273 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SHOW, this);
3275 // The rest of the events only fire on changes, not on new objects.
3276 if (old_win_attributes_->ia_role != 0 ||
3277 !old_win_attributes_->role_name.empty()) {
3278 // Fire an event if the name, description, help, or value changes.
3279 if (name() != old_win_attributes_->name)
3280 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, this);
3281 if (description() != old_win_attributes_->description)
3282 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_DESCRIPTIONCHANGE, this);
3283 if (help() != old_win_attributes_->help)
3284 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_HELPCHANGE, this);
3285 if (value() != old_win_attributes_->value)
3286 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_VALUECHANGE, this);
3287 if (ia_state() != old_win_attributes_->ia_state)
3288 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_STATECHANGE, this);
3290 // Normally focus events are handled elsewhere, however
3291 // focus for managed descendants is platform-specific.
3292 // Fire a focus event if the focused descendant in a multi-select
3293 // list box changes.
3294 if (GetRole() == ui::AX_ROLE_LIST_BOX_OPTION &&
3295 (ia_state() & STATE_SYSTEM_FOCUSABLE) &&
3296 (ia_state() & STATE_SYSTEM_SELECTABLE) &&
3297 (ia_state() & STATE_SYSTEM_FOCUSED) &&
3298 !(old_win_attributes_->ia_state & STATE_SYSTEM_FOCUSED)) {
3299 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_FOCUS, this);
3302 // Handle selection being added or removed.
3303 bool is_selected_now = (ia_state() & STATE_SYSTEM_SELECTED) != 0;
3304 bool was_selected_before =
3305 (old_win_attributes_->ia_state & STATE_SYSTEM_SELECTED) != 0;
3306 if (is_selected_now && !was_selected_before) {
3307 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTIONADD, this);
3308 } else if (!is_selected_now && was_selected_before) {
3309 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTIONREMOVE, this);
3312 // Fire an event if this container object has scrolled.
3313 int sx = 0;
3314 int sy = 0;
3315 if (GetIntAttribute(ui::AX_ATTR_SCROLL_X, &sx) &&
3316 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &sy)) {
3317 if (sx != previous_scroll_x_ || sy != previous_scroll_y_)
3318 manager->MaybeCallNotifyWinEvent(EVENT_SYSTEM_SCROLLINGEND, this);
3319 previous_scroll_x_ = sx;
3320 previous_scroll_y_ = sy;
3323 // Changing a static text node can affect the IAccessibleText hypertext
3324 // of the parent node, so force an update on the parent.
3325 BrowserAccessibilityWin* parent = GetParent()->ToBrowserAccessibilityWin();
3326 if (parent &&
3327 GetRole() == ui::AX_ROLE_STATIC_TEXT &&
3328 name() != old_win_attributes_->name) {
3329 parent->UpdateStep1ComputeWinAttributes();
3330 parent->UpdateStep2ComputeHypertext();
3331 parent->UpdateStep3FireEvents(false);
3334 // Fire hypertext-related events.
3335 int start, old_len, new_len;
3336 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
3337 if (old_len > 0) {
3338 // In-process screen readers may call IAccessibleText::get_oldText
3339 // in reaction to this event to retrieve the text that was removed.
3340 manager->MaybeCallNotifyWinEvent(IA2_EVENT_TEXT_REMOVED, this);
3342 if (new_len > 0) {
3343 // In-process screen readers may call IAccessibleText::get_newText
3344 // in reaction to this event to retrieve the text that was inserted.
3345 manager->MaybeCallNotifyWinEvent(IA2_EVENT_TEXT_INSERTED, this);
3349 old_win_attributes_.reset(nullptr);
3352 void BrowserAccessibilityWin::OnSubtreeWillBeDeleted() {
3353 manager()->ToBrowserAccessibilityManagerWin()->MaybeCallNotifyWinEvent(
3354 EVENT_OBJECT_HIDE, this);
3357 void BrowserAccessibilityWin::NativeAddReference() {
3358 AddRef();
3361 void BrowserAccessibilityWin::NativeReleaseReference() {
3362 Release();
3365 bool BrowserAccessibilityWin::IsNative() const {
3366 return true;
3369 void BrowserAccessibilityWin::OnLocationChanged() {
3370 manager()->ToBrowserAccessibilityManagerWin()->MaybeCallNotifyWinEvent(
3371 EVENT_OBJECT_LOCATIONCHANGE, this);
3374 BrowserAccessibilityWin* BrowserAccessibilityWin::NewReference() {
3375 AddRef();
3376 return this;
3379 BrowserAccessibilityWin* BrowserAccessibilityWin::GetTargetFromChildID(
3380 const VARIANT& var_id) {
3381 if (var_id.vt != VT_I4)
3382 return NULL;
3384 LONG child_id = var_id.lVal;
3385 if (child_id == CHILDID_SELF)
3386 return this;
3388 if (child_id >= 1 && child_id <= static_cast<LONG>(PlatformChildCount()))
3389 return PlatformGetChild(child_id - 1)->ToBrowserAccessibilityWin();
3391 return manager()->ToBrowserAccessibilityManagerWin()->
3392 GetFromUniqueIdWin(child_id);
3395 HRESULT BrowserAccessibilityWin::GetStringAttributeAsBstr(
3396 ui::AXStringAttribute attribute,
3397 BSTR* value_bstr) {
3398 base::string16 str;
3400 if (!GetString16Attribute(attribute, &str))
3401 return S_FALSE;
3403 if (str.empty())
3404 return S_FALSE;
3406 *value_bstr = SysAllocString(str.c_str());
3407 DCHECK(*value_bstr);
3409 return S_OK;
3412 void BrowserAccessibilityWin::StringAttributeToIA2(
3413 ui::AXStringAttribute attribute,
3414 const char* ia2_attr) {
3415 base::string16 value;
3416 if (GetString16Attribute(attribute, &value)) {
3417 win_attributes_->ia2_attributes.push_back(
3418 base::ASCIIToUTF16(ia2_attr) + L":" + value);
3422 void BrowserAccessibilityWin::BoolAttributeToIA2(
3423 ui::AXBoolAttribute attribute,
3424 const char* ia2_attr) {
3425 bool value;
3426 if (GetBoolAttribute(attribute, &value)) {
3427 win_attributes_->ia2_attributes.push_back(
3428 (base::ASCIIToUTF16(ia2_attr) + L":") +
3429 (value ? L"true" : L"false"));
3433 void BrowserAccessibilityWin::IntAttributeToIA2(
3434 ui::AXIntAttribute attribute,
3435 const char* ia2_attr) {
3436 int value;
3437 if (GetIntAttribute(attribute, &value)) {
3438 win_attributes_->ia2_attributes.push_back(
3439 base::ASCIIToUTF16(ia2_attr) + L":" +
3440 base::IntToString16(value));
3444 base::string16 BrowserAccessibilityWin::GetNameRecursive() const {
3445 if (!name().empty()) {
3446 return name();
3449 base::string16 result;
3450 for (uint32 i = 0; i < PlatformChildCount(); ++i) {
3451 result += PlatformGetChild(i)->ToBrowserAccessibilityWin()->
3452 GetNameRecursive();
3454 return result;
3457 base::string16 BrowserAccessibilityWin::GetValueText() {
3458 float fval;
3459 base::string16 value = this->value();
3461 if (value.empty() &&
3462 GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE, &fval)) {
3463 value = base::UTF8ToUTF16(base::DoubleToString(fval));
3465 return value;
3468 base::string16 BrowserAccessibilityWin::TextForIAccessibleText() {
3469 if (IsEditableText())
3470 return value();
3471 return (GetRole() == ui::AX_ROLE_STATIC_TEXT) ? name() : hypertext();
3474 bool BrowserAccessibilityWin::IsSameHypertextCharacter(size_t old_char_index,
3475 size_t new_char_index) {
3476 CHECK(old_win_attributes_);
3478 // For anything other than the "embedded character", we just compare the
3479 // characters directly.
3480 base::char16 old_ch = old_win_attributes_->hypertext[old_char_index];
3481 base::char16 new_ch = win_attributes_->hypertext[new_char_index];
3482 if (old_ch != new_ch)
3483 return false;
3484 if (old_ch == new_ch && new_ch != kEmbeddedCharacter)
3485 return true;
3487 // If it's an embedded character, they're only identical if the child id
3488 // the hyperlink points to is the same.
3489 std::map<int32, int32>& old_offset_to_index =
3490 old_win_attributes_->hyperlink_offset_to_index;
3491 std::vector<int32>& old_hyperlinks = old_win_attributes_->hyperlinks;
3492 int32 old_hyperlinks_count = static_cast<int32>(old_hyperlinks.size());
3493 std::map<int32, int32>::iterator iter;
3494 iter = old_offset_to_index.find(old_char_index);
3495 int old_index = (iter != old_offset_to_index.end()) ? iter->second : -1;
3496 int old_child_id = (old_index >= 0 && old_index < old_hyperlinks_count) ?
3497 old_hyperlinks[old_index] : -1;
3499 std::map<int32, int32>& new_offset_to_index =
3500 win_attributes_->hyperlink_offset_to_index;
3501 std::vector<int32>& new_hyperlinks = win_attributes_->hyperlinks;
3502 int32 new_hyperlinks_count = static_cast<int32>(new_hyperlinks.size());
3503 iter = new_offset_to_index.find(new_char_index);
3504 int new_index = (iter != new_offset_to_index.end()) ? iter->second : -1;
3505 int new_child_id = (new_index >= 0 && new_index < new_hyperlinks_count) ?
3506 new_hyperlinks[new_index] : -1;
3508 return old_child_id == new_child_id;
3511 void BrowserAccessibilityWin::ComputeHypertextRemovedAndInserted(
3512 int* start, int* old_len, int* new_len) {
3513 CHECK(old_win_attributes_);
3515 *start = 0;
3516 *old_len = 0;
3517 *new_len = 0;
3519 const base::string16& old_text = old_win_attributes_->hypertext;
3520 const base::string16& new_text = hypertext();
3522 size_t common_prefix = 0;
3523 while (common_prefix < old_text.size() &&
3524 common_prefix < new_text.size() &&
3525 IsSameHypertextCharacter(common_prefix, common_prefix)) {
3526 ++common_prefix;
3529 size_t common_suffix = 0;
3530 while (common_prefix + common_suffix < old_text.size() &&
3531 common_prefix + common_suffix < new_text.size() &&
3532 IsSameHypertextCharacter(
3533 old_text.size() - common_suffix - 1,
3534 new_text.size() - common_suffix - 1)) {
3535 ++common_suffix;
3538 *start = common_prefix;
3539 *old_len = old_text.size() - common_prefix - common_suffix;
3540 *new_len = new_text.size() - common_prefix - common_suffix;
3543 void BrowserAccessibilityWin::HandleSpecialTextOffset(
3544 const base::string16& text,
3545 LONG* offset) {
3546 if (*offset == IA2_TEXT_OFFSET_LENGTH)
3547 *offset = static_cast<LONG>(text.size());
3548 else if (*offset == IA2_TEXT_OFFSET_CARET)
3549 get_caretOffset(offset);
3552 ui::TextBoundaryType BrowserAccessibilityWin::IA2TextBoundaryToTextBoundary(
3553 IA2TextBoundaryType ia2_boundary) {
3554 switch(ia2_boundary) {
3555 case IA2_TEXT_BOUNDARY_CHAR:
3556 return ui::CHAR_BOUNDARY;
3557 case IA2_TEXT_BOUNDARY_WORD:
3558 return ui::WORD_BOUNDARY;
3559 case IA2_TEXT_BOUNDARY_LINE:
3560 return ui::LINE_BOUNDARY;
3561 case IA2_TEXT_BOUNDARY_SENTENCE:
3562 return ui::SENTENCE_BOUNDARY;
3563 case IA2_TEXT_BOUNDARY_PARAGRAPH:
3564 return ui::PARAGRAPH_BOUNDARY;
3565 case IA2_TEXT_BOUNDARY_ALL:
3566 return ui::ALL_BOUNDARY;
3567 default:
3568 NOTREACHED();
3570 return ui::CHAR_BOUNDARY;
3573 LONG BrowserAccessibilityWin::FindBoundary(
3574 const base::string16& text,
3575 IA2TextBoundaryType ia2_boundary,
3576 LONG start_offset,
3577 ui::TextBoundaryDirection direction) {
3578 HandleSpecialTextOffset(text, &start_offset);
3579 if (ia2_boundary == IA2_TEXT_BOUNDARY_WORD &&
3580 GetRole() == ui::AX_ROLE_TEXT_FIELD) {
3581 return GetWordStartBoundary(static_cast<int>(start_offset), direction);
3584 ui::TextBoundaryType boundary = IA2TextBoundaryToTextBoundary(ia2_boundary);
3585 const std::vector<int32>& line_breaks = GetIntListAttribute(
3586 ui::AX_ATTR_LINE_BREAKS);
3587 return ui::FindAccessibleTextBoundary(
3588 text, line_breaks, boundary, start_offset, direction);
3591 BrowserAccessibilityWin* BrowserAccessibilityWin::GetFromID(int32 id) {
3592 return manager()->GetFromID(id)->ToBrowserAccessibilityWin();
3595 void BrowserAccessibilityWin::InitRoleAndState() {
3596 int32 ia_role = 0;
3597 int32 ia_state = 0;
3598 base::string16 role_name;
3599 int32 ia2_role = 0;
3600 int32 ia2_state = IA2_STATE_OPAQUE;
3602 if (HasState(ui::AX_STATE_BUSY))
3603 ia_state |= STATE_SYSTEM_BUSY;
3604 if (HasState(ui::AX_STATE_CHECKED))
3605 ia_state |= STATE_SYSTEM_CHECKED;
3606 if (HasState(ui::AX_STATE_COLLAPSED))
3607 ia_state |= STATE_SYSTEM_COLLAPSED;
3608 if (HasState(ui::AX_STATE_EXPANDED))
3609 ia_state |= STATE_SYSTEM_EXPANDED;
3610 if (HasState(ui::AX_STATE_FOCUSABLE))
3611 ia_state |= STATE_SYSTEM_FOCUSABLE;
3612 if (HasState(ui::AX_STATE_HASPOPUP))
3613 ia_state |= STATE_SYSTEM_HASPOPUP;
3614 if (HasState(ui::AX_STATE_INDETERMINATE))
3615 ia_state |= STATE_SYSTEM_INDETERMINATE;
3616 if (HasIntAttribute(ui::AX_ATTR_INVALID_STATE) &&
3617 GetIntAttribute(ui::AX_ATTR_INVALID_STATE) != ui::AX_INVALID_STATE_FALSE)
3618 ia2_state |= IA2_STATE_INVALID_ENTRY;
3619 if (HasState(ui::AX_STATE_INVISIBLE))
3620 ia_state |= STATE_SYSTEM_INVISIBLE;
3621 if (HasState(ui::AX_STATE_LINKED))
3622 ia_state |= STATE_SYSTEM_LINKED;
3623 if (HasState(ui::AX_STATE_MULTISELECTABLE)) {
3624 ia_state |= STATE_SYSTEM_EXTSELECTABLE;
3625 ia_state |= STATE_SYSTEM_MULTISELECTABLE;
3627 // TODO(ctguil): Support STATE_SYSTEM_EXTSELECTABLE/accSelect.
3628 if (HasState(ui::AX_STATE_OFFSCREEN))
3629 ia_state |= STATE_SYSTEM_OFFSCREEN;
3630 if (HasState(ui::AX_STATE_PRESSED))
3631 ia_state |= STATE_SYSTEM_PRESSED;
3632 if (HasState(ui::AX_STATE_PROTECTED))
3633 ia_state |= STATE_SYSTEM_PROTECTED;
3634 if (HasState(ui::AX_STATE_REQUIRED))
3635 ia2_state |= IA2_STATE_REQUIRED;
3636 if (HasState(ui::AX_STATE_SELECTABLE))
3637 ia_state |= STATE_SYSTEM_SELECTABLE;
3638 if (HasState(ui::AX_STATE_SELECTED))
3639 ia_state |= STATE_SYSTEM_SELECTED;
3640 if (HasState(ui::AX_STATE_VISITED))
3641 ia_state |= STATE_SYSTEM_TRAVERSED;
3642 if (!HasState(ui::AX_STATE_ENABLED))
3643 ia_state |= STATE_SYSTEM_UNAVAILABLE;
3644 if (HasState(ui::AX_STATE_VERTICAL))
3645 ia2_state |= IA2_STATE_VERTICAL;
3646 if (HasState(ui::AX_STATE_HORIZONTAL))
3647 ia2_state |= IA2_STATE_HORIZONTAL;
3648 if (HasState(ui::AX_STATE_VISITED))
3649 ia_state |= STATE_SYSTEM_TRAVERSED;
3651 // Expose whether or not the mouse is over an element, but suppress
3652 // this for tests because it can make the test results flaky depending
3653 // on the position of the mouse.
3654 BrowserAccessibilityStateImpl* accessibility_state =
3655 BrowserAccessibilityStateImpl::GetInstance();
3656 if (!accessibility_state->disable_hot_tracking_for_testing()) {
3657 if (HasState(ui::AX_STATE_HOVERED))
3658 ia_state |= STATE_SYSTEM_HOTTRACKED;
3661 // WebKit marks everything as readonly unless it's editable text, so if it's
3662 // not readonly, mark it as editable now. The final computation of the
3663 // READONLY state for MSAA is below, after the switch.
3664 if (!HasState(ui::AX_STATE_READ_ONLY))
3665 ia2_state |= IA2_STATE_EDITABLE;
3667 if (GetBoolAttribute(ui::AX_ATTR_BUTTON_MIXED))
3668 ia_state |= STATE_SYSTEM_MIXED;
3670 if (GetBoolAttribute(ui::AX_ATTR_CAN_SET_VALUE))
3671 ia2_state |= IA2_STATE_EDITABLE;
3673 if (!GetStringAttribute(ui::AX_ATTR_AUTO_COMPLETE).empty())
3674 ia2_state |= IA2_STATE_SUPPORTS_AUTOCOMPLETION;
3676 base::string16 html_tag = GetString16Attribute(
3677 ui::AX_ATTR_HTML_TAG);
3678 switch (GetRole()) {
3679 case ui::AX_ROLE_ALERT:
3680 ia_role = ROLE_SYSTEM_ALERT;
3681 break;
3682 case ui::AX_ROLE_ALERT_DIALOG:
3683 ia_role = ROLE_SYSTEM_DIALOG;
3684 break;
3685 case ui::AX_ROLE_APPLICATION:
3686 ia_role = ROLE_SYSTEM_APPLICATION;
3687 break;
3688 case ui::AX_ROLE_ARTICLE:
3689 ia_role = ROLE_SYSTEM_DOCUMENT;
3690 ia_state |= STATE_SYSTEM_READONLY;
3691 break;
3692 case ui::AX_ROLE_BANNER:
3693 ia_role = ROLE_SYSTEM_GROUPING;
3694 ia2_role = IA2_ROLE_HEADER;
3695 break;
3696 case ui::AX_ROLE_BLOCKQUOTE:
3697 role_name = html_tag;
3698 ia2_role = IA2_ROLE_SECTION;
3699 break;
3700 case ui::AX_ROLE_BUSY_INDICATOR:
3701 ia_role = ROLE_SYSTEM_ANIMATION;
3702 ia_state |= STATE_SYSTEM_READONLY;
3703 break;
3704 case ui::AX_ROLE_BUTTON:
3705 ia_role = ROLE_SYSTEM_PUSHBUTTON;
3706 break;
3707 case ui::AX_ROLE_CANVAS:
3708 if (GetBoolAttribute(ui::AX_ATTR_CANVAS_HAS_FALLBACK)) {
3709 role_name = L"canvas";
3710 ia2_role = IA2_ROLE_CANVAS;
3711 } else {
3712 ia_role = ROLE_SYSTEM_GRAPHIC;
3714 break;
3715 case ui::AX_ROLE_CAPTION:
3716 ia_role = ROLE_SYSTEM_TEXT;
3717 ia2_role = IA2_ROLE_CAPTION;
3718 break;
3719 case ui::AX_ROLE_CELL:
3720 ia_role = ROLE_SYSTEM_CELL;
3721 break;
3722 case ui::AX_ROLE_CHECK_BOX:
3723 ia_role = ROLE_SYSTEM_CHECKBUTTON;
3724 ia2_state |= IA2_STATE_CHECKABLE;
3725 break;
3726 case ui::AX_ROLE_COLOR_WELL:
3727 ia_role = ROLE_SYSTEM_TEXT;
3728 ia2_role = IA2_ROLE_COLOR_CHOOSER;
3729 break;
3730 case ui::AX_ROLE_COLUMN:
3731 ia_role = ROLE_SYSTEM_COLUMN;
3732 break;
3733 case ui::AX_ROLE_COLUMN_HEADER:
3734 ia_role = ROLE_SYSTEM_COLUMNHEADER;
3735 break;
3736 case ui::AX_ROLE_COMBO_BOX:
3737 ia_role = ROLE_SYSTEM_COMBOBOX;
3738 break;
3739 case ui::AX_ROLE_COMPLEMENTARY:
3740 ia_role = ROLE_SYSTEM_GROUPING;
3741 ia2_role = IA2_ROLE_NOTE;
3742 break;
3743 case ui::AX_ROLE_CONTENT_INFO:
3744 ia_role = ROLE_SYSTEM_TEXT;
3745 ia2_role = IA2_ROLE_PARAGRAPH;
3746 break;
3747 case ui::AX_ROLE_DATE:
3748 case ui::AX_ROLE_DATE_TIME:
3749 ia_role = ROLE_SYSTEM_DROPLIST;
3750 ia2_role = IA2_ROLE_DATE_EDITOR;
3751 break;
3752 case ui::AX_ROLE_DIV:
3753 role_name = L"div";
3754 ia_role = ROLE_SYSTEM_GROUPING;
3755 ia2_role = IA2_ROLE_SECTION;
3756 break;
3757 case ui::AX_ROLE_DEFINITION:
3758 role_name = html_tag;
3759 ia2_role = IA2_ROLE_PARAGRAPH;
3760 ia_state |= STATE_SYSTEM_READONLY;
3761 break;
3762 case ui::AX_ROLE_DESCRIPTION_LIST_DETAIL:
3763 role_name = html_tag;
3764 ia_role = ROLE_SYSTEM_TEXT;
3765 ia2_role = IA2_ROLE_PARAGRAPH;
3766 break;
3767 case ui::AX_ROLE_DESCRIPTION_LIST:
3768 role_name = html_tag;
3769 ia_role = ROLE_SYSTEM_LIST;
3770 ia_state |= STATE_SYSTEM_READONLY;
3771 break;
3772 case ui::AX_ROLE_DESCRIPTION_LIST_TERM:
3773 ia_role = ROLE_SYSTEM_LISTITEM;
3774 ia_state |= STATE_SYSTEM_READONLY;
3775 break;
3776 case ui::AX_ROLE_DETAILS:
3777 role_name = html_tag;
3778 ia_role = ROLE_SYSTEM_GROUPING;
3779 break;
3780 case ui::AX_ROLE_DIALOG:
3781 ia_role = ROLE_SYSTEM_DIALOG;
3782 break;
3783 case ui::AX_ROLE_DISCLOSURE_TRIANGLE:
3784 ia_role = ROLE_SYSTEM_PUSHBUTTON;
3785 break;
3786 case ui::AX_ROLE_DOCUMENT:
3787 case ui::AX_ROLE_ROOT_WEB_AREA:
3788 case ui::AX_ROLE_WEB_AREA:
3789 ia_role = ROLE_SYSTEM_DOCUMENT;
3790 ia_state |= STATE_SYSTEM_READONLY;
3791 ia_state |= STATE_SYSTEM_FOCUSABLE;
3792 break;
3793 case ui::AX_ROLE_EMBEDDED_OBJECT:
3794 ia_role = ROLE_SYSTEM_CLIENT;
3795 ia2_role = IA2_ROLE_EMBEDDED_OBJECT;
3796 break;
3797 case ui::AX_ROLE_FIGCAPTION:
3798 role_name = html_tag;
3799 ia2_role = IA2_ROLE_CAPTION;
3800 break;
3801 case ui::AX_ROLE_FIGURE:
3802 ia_role = ROLE_SYSTEM_GROUPING;
3803 break;
3804 case ui::AX_ROLE_FORM:
3805 role_name = L"form";
3806 ia2_role = IA2_ROLE_FORM;
3807 break;
3808 case ui::AX_ROLE_FOOTER:
3809 ia_role = ROLE_SYSTEM_GROUPING;
3810 ia2_role = IA2_ROLE_FOOTER;
3811 break;
3812 case ui::AX_ROLE_GRID:
3813 ia_role = ROLE_SYSTEM_TABLE;
3814 ia_state |= STATE_SYSTEM_READONLY;
3815 break;
3816 case ui::AX_ROLE_GROUP: {
3817 base::string16 aria_role = GetString16Attribute(
3818 ui::AX_ATTR_ROLE);
3819 if (aria_role == L"group" || html_tag == L"fieldset") {
3820 ia_role = ROLE_SYSTEM_GROUPING;
3821 } else if (html_tag == L"li") {
3822 ia_role = ROLE_SYSTEM_LISTITEM;
3823 ia_state |= STATE_SYSTEM_READONLY;
3824 } else {
3825 if (html_tag.empty())
3826 role_name = L"div";
3827 else
3828 role_name = html_tag;
3829 ia2_role = IA2_ROLE_SECTION;
3831 break;
3833 case ui::AX_ROLE_HEADING:
3834 role_name = html_tag;
3835 ia2_role = IA2_ROLE_HEADING;
3836 break;
3837 case ui::AX_ROLE_IFRAME:
3838 ia_role = ROLE_SYSTEM_DOCUMENT;
3839 ia2_role = IA2_ROLE_INTERNAL_FRAME;
3840 ia_state = STATE_SYSTEM_READONLY;
3841 break;
3842 case ui::AX_ROLE_IFRAME_PRESENTATIONAL:
3843 ia_role = ROLE_SYSTEM_GROUPING;
3844 break;
3845 case ui::AX_ROLE_IMAGE:
3846 ia_role = ROLE_SYSTEM_GRAPHIC;
3847 ia_state |= STATE_SYSTEM_READONLY;
3848 break;
3849 case ui::AX_ROLE_IMAGE_MAP:
3850 role_name = html_tag;
3851 ia2_role = IA2_ROLE_IMAGE_MAP;
3852 ia_state |= STATE_SYSTEM_READONLY;
3853 break;
3854 case ui::AX_ROLE_IMAGE_MAP_LINK:
3855 ia_role = ROLE_SYSTEM_LINK;
3856 ia_state |= STATE_SYSTEM_LINKED;
3857 ia_state |= STATE_SYSTEM_READONLY;
3858 break;
3859 case ui::AX_ROLE_LABEL_TEXT:
3860 case ui::AX_ROLE_LEGEND:
3861 ia_role = ROLE_SYSTEM_TEXT;
3862 ia2_role = IA2_ROLE_LABEL;
3863 break;
3864 case ui::AX_ROLE_SEARCH:
3865 ia_role = ROLE_SYSTEM_GROUPING;
3866 ia2_role = IA2_ROLE_SECTION;
3867 break;
3868 case ui::AX_ROLE_LINK:
3869 ia_role = ROLE_SYSTEM_LINK;
3870 ia_state |= STATE_SYSTEM_LINKED;
3871 break;
3872 case ui::AX_ROLE_LIST:
3873 ia_role = ROLE_SYSTEM_LIST;
3874 ia_state |= STATE_SYSTEM_READONLY;
3875 break;
3876 case ui::AX_ROLE_LIST_BOX:
3877 ia_role = ROLE_SYSTEM_LIST;
3878 break;
3879 case ui::AX_ROLE_LIST_BOX_OPTION:
3880 ia_role = ROLE_SYSTEM_LISTITEM;
3881 if (ia_state & STATE_SYSTEM_SELECTABLE) {
3882 ia_state |= STATE_SYSTEM_FOCUSABLE;
3883 if (HasState(ui::AX_STATE_FOCUSED))
3884 ia_state |= STATE_SYSTEM_FOCUSED;
3886 break;
3887 case ui::AX_ROLE_LIST_ITEM:
3888 ia_role = ROLE_SYSTEM_LISTITEM;
3889 ia_state |= STATE_SYSTEM_READONLY;
3890 break;
3891 case ui::AX_ROLE_MAIN:
3892 ia_role = ROLE_SYSTEM_GROUPING;
3893 ia2_role = IA2_ROLE_PARAGRAPH;
3894 break;
3895 case ui::AX_ROLE_MARQUEE:
3896 ia_role = ROLE_SYSTEM_ANIMATION;
3897 break;
3898 case ui::AX_ROLE_MATH:
3899 ia_role = ROLE_SYSTEM_EQUATION;
3900 break;
3901 case ui::AX_ROLE_MENU:
3902 case ui::AX_ROLE_MENU_BUTTON:
3903 ia_role = ROLE_SYSTEM_MENUPOPUP;
3904 break;
3905 case ui::AX_ROLE_MENU_BAR:
3906 ia_role = ROLE_SYSTEM_MENUBAR;
3907 break;
3908 case ui::AX_ROLE_MENU_ITEM:
3909 ia_role = ROLE_SYSTEM_MENUITEM;
3910 break;
3911 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
3912 ia_role = ROLE_SYSTEM_MENUITEM;
3913 ia2_role = IA2_ROLE_CHECK_MENU_ITEM;
3914 ia2_state |= IA2_STATE_CHECKABLE;
3915 break;
3916 case ui::AX_ROLE_MENU_ITEM_RADIO:
3917 ia_role = ROLE_SYSTEM_MENUITEM;
3918 ia2_role = IA2_ROLE_RADIO_MENU_ITEM;
3919 break;
3920 case ui::AX_ROLE_MENU_LIST_POPUP:
3921 ia_role = ROLE_SYSTEM_CLIENT;
3922 break;
3923 case ui::AX_ROLE_MENU_LIST_OPTION:
3924 ia_role = ROLE_SYSTEM_LISTITEM;
3925 if (ia_state & STATE_SYSTEM_SELECTABLE) {
3926 ia_state |= STATE_SYSTEM_FOCUSABLE;
3927 if (HasState(ui::AX_STATE_FOCUSED))
3928 ia_state |= STATE_SYSTEM_FOCUSED;
3930 break;
3931 case ui::AX_ROLE_METER:
3932 role_name = html_tag;
3933 ia_role = ROLE_SYSTEM_PROGRESSBAR;
3934 break;
3935 case ui::AX_ROLE_NAVIGATION:
3936 ia_role = ROLE_SYSTEM_GROUPING;
3937 ia2_role = IA2_ROLE_SECTION;
3938 break;
3939 case ui::AX_ROLE_NOTE:
3940 ia_role = ROLE_SYSTEM_GROUPING;
3941 ia2_role = IA2_ROLE_NOTE;
3942 break;
3943 case ui::AX_ROLE_OUTLINE:
3944 ia_role = ROLE_SYSTEM_OUTLINE;
3945 break;
3946 case ui::AX_ROLE_PARAGRAPH:
3947 role_name = L"P";
3948 ia2_role = IA2_ROLE_PARAGRAPH;
3949 break;
3950 case ui::AX_ROLE_POP_UP_BUTTON:
3951 if (html_tag == L"select") {
3952 ia_role = ROLE_SYSTEM_COMBOBOX;
3953 } else {
3954 ia_role = ROLE_SYSTEM_BUTTONMENU;
3956 break;
3957 case ui::AX_ROLE_PRE:
3958 role_name = html_tag;
3959 ia_role = ROLE_SYSTEM_TEXT;
3960 ia2_role = IA2_ROLE_PARAGRAPH;
3961 break;
3962 case ui::AX_ROLE_PROGRESS_INDICATOR:
3963 ia_role = ROLE_SYSTEM_PROGRESSBAR;
3964 ia_state |= STATE_SYSTEM_READONLY;
3965 break;
3966 case ui::AX_ROLE_RADIO_BUTTON:
3967 ia_role = ROLE_SYSTEM_RADIOBUTTON;
3968 ia2_state = IA2_STATE_CHECKABLE;
3969 break;
3970 case ui::AX_ROLE_RADIO_GROUP:
3971 ia_role = ROLE_SYSTEM_GROUPING;
3972 break;
3973 case ui::AX_ROLE_REGION:
3974 if (html_tag == L"section") {
3975 ia_role = ROLE_SYSTEM_GROUPING;
3976 ia2_role = IA2_ROLE_SECTION;
3977 } else {
3978 ia_role = ROLE_SYSTEM_PANE;
3980 break;
3981 case ui::AX_ROLE_ROW:
3982 ia_role = ROLE_SYSTEM_ROW;
3983 break;
3984 case ui::AX_ROLE_ROW_HEADER:
3985 ia_role = ROLE_SYSTEM_ROWHEADER;
3986 break;
3987 case ui::AX_ROLE_RUBY:
3988 ia_role = ROLE_SYSTEM_TEXT;
3989 ia2_role = IA2_ROLE_TEXT_FRAME;
3990 break;
3991 case ui::AX_ROLE_RULER:
3992 ia_role = ROLE_SYSTEM_CLIENT;
3993 ia2_role = IA2_ROLE_RULER;
3994 ia_state |= STATE_SYSTEM_READONLY;
3995 break;
3996 case ui::AX_ROLE_SCROLL_AREA:
3997 ia_role = ROLE_SYSTEM_CLIENT;
3998 ia2_role = IA2_ROLE_SCROLL_PANE;
3999 ia_state |= STATE_SYSTEM_READONLY;
4000 ia2_state &= ~(IA2_STATE_EDITABLE);
4001 break;
4002 case ui::AX_ROLE_SCROLL_BAR:
4003 ia_role = ROLE_SYSTEM_SCROLLBAR;
4004 break;
4005 case ui::AX_ROLE_SLIDER:
4006 ia_role = ROLE_SYSTEM_SLIDER;
4007 break;
4008 case ui::AX_ROLE_SPIN_BUTTON:
4009 ia_role = ROLE_SYSTEM_SPINBUTTON;
4010 break;
4011 case ui::AX_ROLE_SPIN_BUTTON_PART:
4012 ia_role = ROLE_SYSTEM_PUSHBUTTON;
4013 break;
4014 case ui::AX_ROLE_ANNOTATION:
4015 case ui::AX_ROLE_LIST_MARKER:
4016 case ui::AX_ROLE_STATIC_TEXT:
4017 ia_role = ROLE_SYSTEM_STATICTEXT;
4018 break;
4019 case ui::AX_ROLE_STATUS:
4020 ia_role = ROLE_SYSTEM_STATUSBAR;
4021 break;
4022 case ui::AX_ROLE_SPLITTER:
4023 ia_role = ROLE_SYSTEM_SEPARATOR;
4024 break;
4025 case ui::AX_ROLE_SVG_ROOT:
4026 ia_role = ROLE_SYSTEM_GRAPHIC;
4027 break;
4028 case ui::AX_ROLE_TAB:
4029 ia_role = ROLE_SYSTEM_PAGETAB;
4030 break;
4031 case ui::AX_ROLE_TABLE: {
4032 base::string16 aria_role = GetString16Attribute(
4033 ui::AX_ATTR_ROLE);
4034 if (aria_role == L"treegrid") {
4035 ia_role = ROLE_SYSTEM_OUTLINE;
4036 } else {
4037 ia_role = ROLE_SYSTEM_TABLE;
4039 break;
4041 case ui::AX_ROLE_TABLE_HEADER_CONTAINER:
4042 ia_role = ROLE_SYSTEM_GROUPING;
4043 ia2_role = IA2_ROLE_SECTION;
4044 ia_state |= STATE_SYSTEM_READONLY;
4045 break;
4046 case ui::AX_ROLE_TAB_LIST:
4047 ia_role = ROLE_SYSTEM_PAGETABLIST;
4048 break;
4049 case ui::AX_ROLE_TAB_PANEL:
4050 ia_role = ROLE_SYSTEM_PROPERTYPAGE;
4051 break;
4052 case ui::AX_ROLE_TOGGLE_BUTTON:
4053 ia_role = ROLE_SYSTEM_PUSHBUTTON;
4054 ia2_role = IA2_ROLE_TOGGLE_BUTTON;
4055 break;
4056 case ui::AX_ROLE_TEXT_AREA:
4057 ia_role = ROLE_SYSTEM_TEXT;
4058 ia2_state |= IA2_STATE_MULTI_LINE;
4059 ia2_state |= IA2_STATE_EDITABLE;
4060 ia2_state |= IA2_STATE_SELECTABLE_TEXT;
4061 break;
4062 case ui::AX_ROLE_TEXT_FIELD:
4063 ia_role = ROLE_SYSTEM_TEXT;
4064 ia2_state |= IA2_STATE_SINGLE_LINE;
4065 ia2_state |= IA2_STATE_EDITABLE;
4066 ia2_state |= IA2_STATE_SELECTABLE_TEXT;
4067 break;
4068 case ui::AX_ROLE_TIME:
4069 ia_role = ROLE_SYSTEM_SPINBUTTON;
4070 break;
4071 case ui::AX_ROLE_TIMER:
4072 ia_role = ROLE_SYSTEM_CLOCK;
4073 ia_state |= STATE_SYSTEM_READONLY;
4074 break;
4075 case ui::AX_ROLE_TOOLBAR:
4076 ia_role = ROLE_SYSTEM_TOOLBAR;
4077 ia_state |= STATE_SYSTEM_READONLY;
4078 break;
4079 case ui::AX_ROLE_TOOLTIP:
4080 ia_role = ROLE_SYSTEM_TOOLTIP;
4081 ia_state |= STATE_SYSTEM_READONLY;
4082 break;
4083 case ui::AX_ROLE_TREE:
4084 ia_role = ROLE_SYSTEM_OUTLINE;
4085 break;
4086 case ui::AX_ROLE_TREE_GRID:
4087 ia_role = ROLE_SYSTEM_OUTLINE;
4088 break;
4089 case ui::AX_ROLE_TREE_ITEM:
4090 ia_role = ROLE_SYSTEM_OUTLINEITEM;
4091 break;
4092 case ui::AX_ROLE_LINE_BREAK:
4093 ia_role = ROLE_SYSTEM_WHITESPACE;
4094 break;
4095 case ui::AX_ROLE_WINDOW:
4096 ia_role = ROLE_SYSTEM_WINDOW;
4097 break;
4099 // TODO(dmazzoni): figure out the proper MSAA role for all of these.
4100 case ui::AX_ROLE_DIRECTORY:
4101 case ui::AX_ROLE_IGNORED:
4102 case ui::AX_ROLE_LOG:
4103 case ui::AX_ROLE_NONE:
4104 case ui::AX_ROLE_PRESENTATIONAL:
4105 case ui::AX_ROLE_SLIDER_THUMB:
4106 default:
4107 ia_role = ROLE_SYSTEM_CLIENT;
4108 break;
4111 // Compute the final value of READONLY for MSAA.
4113 // We always set the READONLY state for elements that have the
4114 // aria-readonly attribute and for a few roles (in the switch above).
4115 // We clear the READONLY state on focusable controls and on a document.
4116 // Everything else, the majority of objects, do not have this state set.
4117 if (HasState(ui::AX_STATE_FOCUSABLE) &&
4118 ia_role != ROLE_SYSTEM_DOCUMENT) {
4119 ia_state &= ~(STATE_SYSTEM_READONLY);
4121 if (!HasState(ui::AX_STATE_READ_ONLY))
4122 ia_state &= ~(STATE_SYSTEM_READONLY);
4123 if (GetBoolAttribute(ui::AX_ATTR_ARIA_READONLY))
4124 ia_state |= STATE_SYSTEM_READONLY;
4126 // The role should always be set.
4127 DCHECK(!role_name.empty() || ia_role);
4129 // If we didn't explicitly set the IAccessible2 role, make it the same
4130 // as the MSAA role.
4131 if (!ia2_role)
4132 ia2_role = ia_role;
4134 win_attributes_->ia_role = ia_role;
4135 win_attributes_->ia_state = ia_state;
4136 win_attributes_->role_name = role_name;
4137 win_attributes_->ia2_role = ia2_role;
4138 win_attributes_->ia2_state = ia2_state;
4141 } // namespace content