[MacViews] Show comboboxes with a native NSMenu
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_win.cc
blob76d15c781ee1483f5875d06a18d728e1b57ece95
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 <algorithm>
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/win/enum_variant.h"
17 #include "base/win/scoped_comptr.h"
18 #include "base/win/windows_version.h"
19 #include "content/browser/accessibility/browser_accessibility_manager_win.h"
20 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
21 #include "content/common/accessibility_messages.h"
22 #include "content/public/common/content_client.h"
23 #include "ui/accessibility/ax_text_utils.h"
24 #include "ui/base/win/accessibility_ids_win.h"
25 #include "ui/base/win/accessibility_misc_utils.h"
26 #include "ui/base/win/atl_module.h"
28 namespace content {
30 // These nonstandard GUIDs are taken directly from the Mozilla sources
31 // (accessible/src/msaa/nsAccessNodeWrap.cpp); some documentation is here:
32 // http://developer.mozilla.org/en/Accessibility/AT-APIs/ImplementationFeatures/MSAA
33 const GUID GUID_ISimpleDOM = {0x0c539790,
34 0x12e4,
35 0x11cf,
36 {0xb6, 0x61, 0x00, 0xaa, 0x00, 0x4c, 0xd6, 0xd8}};
37 const GUID GUID_IAccessibleContentDocument = {
38 0xa5d8e1f3,
39 0x3571,
40 0x4d8f,
41 {0x95, 0x21, 0x07, 0xed, 0x28, 0xfb, 0x07, 0x2e}};
43 const base::char16 BrowserAccessibilityWin::kEmbeddedCharacter = L'\xfffc';
45 // static
46 LONG BrowserAccessibilityWin::next_unique_id_win_ =
47 base::win::kFirstBrowserAccessibilityManagerAccessibilityId;
50 // BrowserAccessibilityRelation
52 // A simple implementation of IAccessibleRelation, used to represent
53 // a relationship between two accessible nodes in the tree.
56 class BrowserAccessibilityRelation
57 : public CComObjectRootEx<CComMultiThreadModel>,
58 public IAccessibleRelation {
59 BEGIN_COM_MAP(BrowserAccessibilityRelation)
60 COM_INTERFACE_ENTRY(IAccessibleRelation)
61 END_COM_MAP()
63 CONTENT_EXPORT BrowserAccessibilityRelation() {}
64 CONTENT_EXPORT virtual ~BrowserAccessibilityRelation() {}
66 CONTENT_EXPORT void Initialize(BrowserAccessibilityWin* owner,
67 const base::string16& type);
68 CONTENT_EXPORT void AddTarget(int target_id);
70 // IAccessibleRelation methods.
71 CONTENT_EXPORT STDMETHODIMP get_relationType(BSTR* relation_type) override;
72 CONTENT_EXPORT STDMETHODIMP get_nTargets(long* n_targets) override;
73 CONTENT_EXPORT STDMETHODIMP
74 get_target(long target_index, IUnknown** target) override;
75 CONTENT_EXPORT STDMETHODIMP
76 get_targets(long max_targets, IUnknown** targets, long* n_targets) override;
78 // IAccessibleRelation methods not implemented.
79 CONTENT_EXPORT STDMETHODIMP
80 get_localizedRelationType(BSTR* relation_type) override {
81 return E_NOTIMPL;
84 private:
85 base::string16 type_;
86 base::win::ScopedComPtr<BrowserAccessibilityWin> owner_;
87 std::vector<int> target_ids_;
90 void BrowserAccessibilityRelation::Initialize(BrowserAccessibilityWin* owner,
91 const base::string16& type) {
92 owner_ = owner;
93 type_ = type;
96 void BrowserAccessibilityRelation::AddTarget(int target_id) {
97 target_ids_.push_back(target_id);
100 STDMETHODIMP BrowserAccessibilityRelation::get_relationType(
101 BSTR* relation_type) {
102 if (!relation_type)
103 return E_INVALIDARG;
105 if (!owner_->instance_active())
106 return E_FAIL;
108 *relation_type = SysAllocString(type_.c_str());
109 DCHECK(*relation_type);
110 return S_OK;
113 STDMETHODIMP BrowserAccessibilityRelation::get_nTargets(long* n_targets) {
114 if (!n_targets)
115 return E_INVALIDARG;
117 if (!owner_->instance_active())
118 return E_FAIL;
120 *n_targets = static_cast<long>(target_ids_.size());
122 BrowserAccessibilityManager* manager = owner_->manager();
123 for (long i = *n_targets - 1; i >= 0; --i) {
124 BrowserAccessibility* result = manager->GetFromID(target_ids_[i]);
125 if (!result || !result->instance_active()) {
126 *n_targets = 0;
127 break;
130 return S_OK;
133 STDMETHODIMP BrowserAccessibilityRelation::get_target(long target_index,
134 IUnknown** target) {
135 if (!target)
136 return E_INVALIDARG;
138 if (!owner_->instance_active())
139 return E_FAIL;
141 if (target_index < 0 ||
142 target_index >= static_cast<long>(target_ids_.size())) {
143 return E_INVALIDARG;
146 BrowserAccessibilityManager* manager = owner_->manager();
147 BrowserAccessibility* result =
148 manager->GetFromID(target_ids_[target_index]);
149 if (!result || !result->instance_active())
150 return E_FAIL;
152 *target = static_cast<IAccessible*>(
153 result->ToBrowserAccessibilityWin()->NewReference());
154 return S_OK;
157 STDMETHODIMP BrowserAccessibilityRelation::get_targets(long max_targets,
158 IUnknown** targets,
159 long* n_targets) {
160 if (!targets || !n_targets)
161 return E_INVALIDARG;
163 if (!owner_->instance_active())
164 return E_FAIL;
166 long count = static_cast<long>(target_ids_.size());
167 if (count > max_targets)
168 count = max_targets;
170 *n_targets = count;
171 if (count == 0)
172 return S_FALSE;
174 for (long i = 0; i < count; ++i) {
175 HRESULT result = get_target(i, &targets[i]);
176 if (result != S_OK)
177 return result;
180 return S_OK;
184 // BrowserAccessibilityWin::WinAttributes
187 BrowserAccessibilityWin::WinAttributes::WinAttributes()
188 : ia_role(0),
189 ia_state(0),
190 ia2_role(0),
191 ia2_state(0) {
194 BrowserAccessibilityWin::WinAttributes::~WinAttributes() {
198 // BrowserAccessibilityWin
201 // static
202 BrowserAccessibility* BrowserAccessibility::Create() {
203 ui::win::CreateATLModuleIfNeeded();
204 CComObject<BrowserAccessibilityWin>* instance;
205 HRESULT hr = CComObject<BrowserAccessibilityWin>::CreateInstance(&instance);
206 DCHECK(SUCCEEDED(hr));
207 return instance->NewReference();
210 BrowserAccessibilityWin* BrowserAccessibility::ToBrowserAccessibilityWin() {
211 return static_cast<BrowserAccessibilityWin*>(this);
214 BrowserAccessibilityWin::BrowserAccessibilityWin()
215 : win_attributes_(new WinAttributes()),
216 previous_scroll_x_(0),
217 previous_scroll_y_(0) {
218 // Start unique IDs at -1 and decrement each time, because get_accChild
219 // uses positive IDs to enumerate children, so we use negative IDs to
220 // clearly distinguish between indices and unique IDs.
221 unique_id_win_ = next_unique_id_win_;
222 if (next_unique_id_win_ ==
223 base::win::kLastBrowserAccessibilityManagerAccessibilityId) {
224 next_unique_id_win_ =
225 base::win::kFirstBrowserAccessibilityManagerAccessibilityId;
227 next_unique_id_win_--;
230 BrowserAccessibilityWin::~BrowserAccessibilityWin() {
231 for (size_t i = 0; i < relations_.size(); ++i)
232 relations_[i]->Release();
236 // IAccessible methods.
238 // Conventions:
239 // * Always test for instance_active() first and return E_FAIL if it's false.
240 // * Always check for invalid arguments first, even if they're unused.
241 // * Return S_FALSE if the only output is a string argument and it's empty.
244 HRESULT BrowserAccessibilityWin::accDoDefaultAction(VARIANT var_id) {
245 if (!instance_active())
246 return E_FAIL;
248 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
249 if (!target)
250 return E_INVALIDARG;
252 manager()->DoDefaultAction(*target);
253 return S_OK;
256 STDMETHODIMP BrowserAccessibilityWin::accHitTest(LONG x_left,
257 LONG y_top,
258 VARIANT* child) {
259 if (!instance_active())
260 return E_FAIL;
262 if (!child)
263 return E_INVALIDARG;
265 gfx::Point point(x_left, y_top);
266 if (!GetGlobalBoundsRect().Contains(point)) {
267 // Return S_FALSE and VT_EMPTY when the outside the object's boundaries.
268 child->vt = VT_EMPTY;
269 return S_FALSE;
272 BrowserAccessibility* result = BrowserAccessibilityForPoint(point);
273 if (result == this) {
274 // Point is within this object.
275 child->vt = VT_I4;
276 child->lVal = CHILDID_SELF;
277 } else {
278 child->vt = VT_DISPATCH;
279 child->pdispVal = result->ToBrowserAccessibilityWin()->NewReference();
281 return S_OK;
284 STDMETHODIMP BrowserAccessibilityWin::accLocation(LONG* x_left,
285 LONG* y_top,
286 LONG* width,
287 LONG* height,
288 VARIANT var_id) {
289 if (!instance_active())
290 return E_FAIL;
292 if (!x_left || !y_top || !width || !height)
293 return E_INVALIDARG;
295 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
296 if (!target)
297 return E_INVALIDARG;
299 gfx::Rect bounds = target->GetGlobalBoundsRect();
300 *x_left = bounds.x();
301 *y_top = bounds.y();
302 *width = bounds.width();
303 *height = bounds.height();
305 return S_OK;
308 STDMETHODIMP BrowserAccessibilityWin::accNavigate(LONG nav_dir,
309 VARIANT start,
310 VARIANT* end) {
311 BrowserAccessibilityWin* target = GetTargetFromChildID(start);
312 if (!target)
313 return E_INVALIDARG;
315 if ((nav_dir == NAVDIR_LASTCHILD || nav_dir == NAVDIR_FIRSTCHILD) &&
316 start.lVal != CHILDID_SELF) {
317 // MSAA states that navigating to first/last child can only be from self.
318 return E_INVALIDARG;
321 uint32 child_count = target->PlatformChildCount();
323 BrowserAccessibility* result = NULL;
324 switch (nav_dir) {
325 case NAVDIR_DOWN:
326 case NAVDIR_UP:
327 case NAVDIR_LEFT:
328 case NAVDIR_RIGHT:
329 // These directions are not implemented, matching Mozilla and IE.
330 return E_NOTIMPL;
331 case NAVDIR_FIRSTCHILD:
332 if (child_count > 0)
333 result = target->PlatformGetChild(0);
334 break;
335 case NAVDIR_LASTCHILD:
336 if (child_count > 0)
337 result = target->PlatformGetChild(child_count - 1);
338 break;
339 case NAVDIR_NEXT:
340 result = target->GetNextSibling();
341 break;
342 case NAVDIR_PREVIOUS:
343 result = target->GetPreviousSibling();
344 break;
347 if (!result) {
348 end->vt = VT_EMPTY;
349 return S_FALSE;
352 end->vt = VT_DISPATCH;
353 end->pdispVal = result->ToBrowserAccessibilityWin()->NewReference();
354 return S_OK;
357 STDMETHODIMP BrowserAccessibilityWin::get_accChild(VARIANT var_child,
358 IDispatch** disp_child) {
359 if (!instance_active())
360 return E_FAIL;
362 if (!disp_child)
363 return E_INVALIDARG;
365 *disp_child = NULL;
367 BrowserAccessibilityWin* target = GetTargetFromChildID(var_child);
368 if (!target)
369 return E_INVALIDARG;
371 (*disp_child) = target->NewReference();
372 return S_OK;
375 STDMETHODIMP BrowserAccessibilityWin::get_accChildCount(LONG* child_count) {
376 if (!instance_active())
377 return E_FAIL;
379 if (!child_count)
380 return E_INVALIDARG;
382 *child_count = PlatformChildCount();
384 return S_OK;
387 STDMETHODIMP BrowserAccessibilityWin::get_accDefaultAction(VARIANT var_id,
388 BSTR* def_action) {
389 if (!instance_active())
390 return E_FAIL;
392 if (!def_action)
393 return E_INVALIDARG;
395 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
396 if (!target)
397 return E_INVALIDARG;
399 return target->GetStringAttributeAsBstr(
400 ui::AX_ATTR_ACTION, def_action);
403 STDMETHODIMP BrowserAccessibilityWin::get_accDescription(VARIANT var_id,
404 BSTR* desc) {
405 if (!instance_active())
406 return E_FAIL;
408 if (!desc)
409 return E_INVALIDARG;
411 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
412 if (!target)
413 return E_INVALIDARG;
415 base::string16 description_str = target->description();
416 if (description_str.empty())
417 return S_FALSE;
419 *desc = SysAllocString(description_str.c_str());
421 DCHECK(*desc);
422 return S_OK;
425 STDMETHODIMP BrowserAccessibilityWin::get_accFocus(VARIANT* focus_child) {
426 if (!instance_active())
427 return E_FAIL;
429 if (!focus_child)
430 return E_INVALIDARG;
432 BrowserAccessibilityWin* focus = static_cast<BrowserAccessibilityWin*>(
433 manager()->GetFocus(this));
434 if (focus == this) {
435 focus_child->vt = VT_I4;
436 focus_child->lVal = CHILDID_SELF;
437 } else if (focus == NULL) {
438 focus_child->vt = VT_EMPTY;
439 } else {
440 focus_child->vt = VT_DISPATCH;
441 focus_child->pdispVal = focus->NewReference();
444 return S_OK;
447 STDMETHODIMP BrowserAccessibilityWin::get_accHelp(VARIANT var_id, BSTR* help) {
448 if (!instance_active())
449 return E_FAIL;
451 if (!help)
452 return E_INVALIDARG;
454 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
455 if (!target)
456 return E_INVALIDARG;
458 base::string16 help_str = target->help();
459 if (help_str.empty())
460 return S_FALSE;
462 *help = SysAllocString(help_str.c_str());
464 DCHECK(*help);
465 return S_OK;
468 STDMETHODIMP BrowserAccessibilityWin::get_accKeyboardShortcut(VARIANT var_id,
469 BSTR* acc_key) {
470 if (!instance_active())
471 return E_FAIL;
473 if (!acc_key)
474 return E_INVALIDARG;
476 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
477 if (!target)
478 return E_INVALIDARG;
480 return target->GetStringAttributeAsBstr(
481 ui::AX_ATTR_SHORTCUT, acc_key);
484 STDMETHODIMP BrowserAccessibilityWin::get_accName(VARIANT var_id, BSTR* name) {
485 if (!instance_active())
486 return E_FAIL;
488 if (!name)
489 return E_INVALIDARG;
491 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
492 if (!target)
493 return E_INVALIDARG;
495 base::string16 name_str = target->name();
497 // If the name is empty, see if it's labeled by another element.
498 if (name_str.empty()) {
499 int title_elem_id;
500 if (target->GetIntAttribute(ui::AX_ATTR_TITLE_UI_ELEMENT,
501 &title_elem_id)) {
502 BrowserAccessibilityWin* title_elem =
503 manager()->GetFromID(title_elem_id)->ToBrowserAccessibilityWin();
504 if (title_elem)
505 name_str = title_elem->GetNameRecursive();
509 if (name_str.empty())
510 return S_FALSE;
512 *name = SysAllocString(name_str.c_str());
514 DCHECK(*name);
515 return S_OK;
518 STDMETHODIMP BrowserAccessibilityWin::get_accParent(IDispatch** disp_parent) {
519 if (!instance_active())
520 return E_FAIL;
522 if (!disp_parent)
523 return E_INVALIDARG;
525 IAccessible* parent_obj = GetParent()->ToBrowserAccessibilityWin();
526 if (parent_obj == NULL) {
527 // This happens if we're the root of the tree;
528 // return the IAccessible for the window.
529 parent_obj =
530 manager()->ToBrowserAccessibilityManagerWin()->GetParentIAccessible();
531 // |parent| can only be NULL if the manager was created before the parent
532 // IAccessible was known and it wasn't subsequently set before a client
533 // requested it. This has been fixed. |parent| may also be NULL during
534 // destruction. Possible cases where this could occur include tabs being
535 // dragged to a new window, etc.
536 if (!parent_obj) {
537 DVLOG(1) << "In Function: "
538 << __FUNCTION__
539 << ". Parent IAccessible interface is NULL. Returning failure";
540 return E_FAIL;
543 parent_obj->AddRef();
544 *disp_parent = parent_obj;
545 return S_OK;
548 STDMETHODIMP BrowserAccessibilityWin::get_accRole(VARIANT var_id,
549 VARIANT* role) {
550 if (!instance_active())
551 return E_FAIL;
553 if (!role)
554 return E_INVALIDARG;
556 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
557 if (!target)
558 return E_INVALIDARG;
560 if (!target->role_name().empty()) {
561 role->vt = VT_BSTR;
562 role->bstrVal = SysAllocString(target->role_name().c_str());
563 } else {
564 role->vt = VT_I4;
565 role->lVal = target->ia_role();
567 return S_OK;
570 STDMETHODIMP BrowserAccessibilityWin::get_accState(VARIANT var_id,
571 VARIANT* state) {
572 if (!instance_active())
573 return E_FAIL;
575 if (!state)
576 return E_INVALIDARG;
578 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
579 if (!target)
580 return E_INVALIDARG;
582 state->vt = VT_I4;
583 state->lVal = target->ia_state();
584 if (manager()->GetFocus(NULL) == this)
585 state->lVal |= STATE_SYSTEM_FOCUSED;
587 return S_OK;
590 STDMETHODIMP BrowserAccessibilityWin::get_accValue(VARIANT var_id,
591 BSTR* value) {
592 if (!instance_active())
593 return E_FAIL;
595 if (!value)
596 return E_INVALIDARG;
598 BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
599 if (!target)
600 return E_INVALIDARG;
602 if (target->ia_role() == ROLE_SYSTEM_PROGRESSBAR ||
603 target->ia_role() == ROLE_SYSTEM_SCROLLBAR ||
604 target->ia_role() == ROLE_SYSTEM_SLIDER) {
605 base::string16 value_text = target->GetValueText();
606 *value = SysAllocString(value_text.c_str());
607 DCHECK(*value);
608 return S_OK;
611 // Expose color well value.
612 if (target->ia2_role() == IA2_ROLE_COLOR_CHOOSER) {
613 int color = target->GetIntAttribute(ui::AX_ATTR_COLOR_VALUE);
614 int red = (color >> 16) & 0xFF;
615 int green = (color >> 8) & 0xFF;
616 int blue = color & 0xFF;
617 base::string16 value_text;
618 value_text = base::IntToString16((red * 100) / 255) + L"% red " +
619 base::IntToString16((green * 100) / 255) + L"% green " +
620 base::IntToString16((blue * 100) / 255) + L"% blue";
621 *value = SysAllocString(value_text.c_str());
622 DCHECK(*value);
623 return S_OK;
626 *value = SysAllocString(target->value().c_str());
627 DCHECK(*value);
628 return S_OK;
631 STDMETHODIMP BrowserAccessibilityWin::get_accHelpTopic(BSTR* help_file,
632 VARIANT var_id,
633 LONG* topic_id) {
634 return E_NOTIMPL;
637 STDMETHODIMP BrowserAccessibilityWin::get_accSelection(VARIANT* selected) {
638 if (!instance_active())
639 return E_FAIL;
641 if (GetRole() != ui::AX_ROLE_LIST_BOX)
642 return E_NOTIMPL;
644 unsigned long selected_count = 0;
645 for (size_t i = 0; i < InternalChildCount(); ++i) {
646 if (InternalGetChild(i)->HasState(ui::AX_STATE_SELECTED))
647 ++selected_count;
650 if (selected_count == 0) {
651 selected->vt = VT_EMPTY;
652 return S_OK;
655 if (selected_count == 1) {
656 for (size_t i = 0; i < InternalChildCount(); ++i) {
657 if (InternalGetChild(i)->HasState(ui::AX_STATE_SELECTED)) {
658 selected->vt = VT_DISPATCH;
659 selected->pdispVal =
660 InternalGetChild(i)->ToBrowserAccessibilityWin()->NewReference();
661 return S_OK;
666 // Multiple items are selected.
667 base::win::EnumVariant* enum_variant =
668 new base::win::EnumVariant(selected_count);
669 enum_variant->AddRef();
670 unsigned long index = 0;
671 for (size_t i = 0; i < InternalChildCount(); ++i) {
672 if (InternalGetChild(i)->HasState(ui::AX_STATE_SELECTED)) {
673 enum_variant->ItemAt(index)->vt = VT_DISPATCH;
674 enum_variant->ItemAt(index)->pdispVal =
675 InternalGetChild(i)->ToBrowserAccessibilityWin()->NewReference();
676 ++index;
679 selected->vt = VT_UNKNOWN;
680 selected->punkVal = static_cast<IUnknown*>(
681 static_cast<base::win::IUnknownImpl*>(enum_variant));
682 return S_OK;
685 STDMETHODIMP BrowserAccessibilityWin::accSelect(
686 LONG flags_sel, VARIANT var_id) {
687 if (!instance_active())
688 return E_FAIL;
690 if (flags_sel & SELFLAG_TAKEFOCUS) {
691 manager()->SetFocus(this, true);
692 return S_OK;
695 return S_FALSE;
698 STDMETHODIMP
699 BrowserAccessibilityWin::put_accName(VARIANT var_id, BSTR put_name) {
700 return E_NOTIMPL;
702 STDMETHODIMP
703 BrowserAccessibilityWin::put_accValue(VARIANT var_id, BSTR put_val) {
704 return E_NOTIMPL;
708 // IAccessible2 methods.
711 STDMETHODIMP BrowserAccessibilityWin::role(LONG* role) {
712 if (!instance_active())
713 return E_FAIL;
715 if (!role)
716 return E_INVALIDARG;
718 *role = ia2_role();
720 return S_OK;
723 STDMETHODIMP BrowserAccessibilityWin::get_attributes(BSTR* attributes) {
724 if (!instance_active())
725 return E_FAIL;
727 if (!attributes)
728 return E_INVALIDARG;
730 // The iaccessible2 attributes are a set of key-value pairs
731 // separated by semicolons, with a colon between the key and the value.
732 base::string16 str;
733 const std::vector<base::string16>& attributes_list = ia2_attributes();
734 for (unsigned int i = 0; i < attributes_list.size(); ++i) {
735 str += attributes_list[i] + L';';
738 if (str.empty())
739 return S_FALSE;
741 *attributes = SysAllocString(str.c_str());
742 DCHECK(*attributes);
743 return S_OK;
746 STDMETHODIMP BrowserAccessibilityWin::get_states(AccessibleStates* states) {
747 if (!instance_active())
748 return E_FAIL;
750 if (!states)
751 return E_INVALIDARG;
753 *states = ia2_state();
755 return S_OK;
758 STDMETHODIMP BrowserAccessibilityWin::get_uniqueID(LONG* unique_id) {
759 if (!instance_active())
760 return E_FAIL;
762 if (!unique_id)
763 return E_INVALIDARG;
765 *unique_id = unique_id_win_;
766 return S_OK;
769 STDMETHODIMP BrowserAccessibilityWin::get_windowHandle(HWND* window_handle) {
770 if (!instance_active())
771 return E_FAIL;
773 if (!window_handle)
774 return E_INVALIDARG;
776 *window_handle =
777 manager()->ToBrowserAccessibilityManagerWin()->GetParentHWND();
778 if (!*window_handle)
779 return E_FAIL;
781 return S_OK;
784 STDMETHODIMP BrowserAccessibilityWin::get_indexInParent(LONG* index_in_parent) {
785 if (!instance_active())
786 return E_FAIL;
788 if (!index_in_parent)
789 return E_INVALIDARG;
791 *index_in_parent = this->GetIndexInParent();
792 return S_OK;
795 STDMETHODIMP BrowserAccessibilityWin::get_nRelations(LONG* n_relations) {
796 if (!instance_active())
797 return E_FAIL;
799 if (!n_relations)
800 return E_INVALIDARG;
802 *n_relations = relations_.size();
803 return S_OK;
806 STDMETHODIMP BrowserAccessibilityWin::get_relation(
807 LONG relation_index,
808 IAccessibleRelation** relation) {
809 if (!instance_active())
810 return E_FAIL;
812 if (relation_index < 0 ||
813 relation_index >= static_cast<long>(relations_.size())) {
814 return E_INVALIDARG;
817 if (!relation)
818 return E_INVALIDARG;
820 relations_[relation_index]->AddRef();
821 *relation = relations_[relation_index];
822 return S_OK;
825 STDMETHODIMP BrowserAccessibilityWin::get_relations(
826 LONG max_relations,
827 IAccessibleRelation** relations,
828 LONG* n_relations) {
829 if (!instance_active())
830 return E_FAIL;
832 if (!relations || !n_relations)
833 return E_INVALIDARG;
835 long count = static_cast<long>(relations_.size());
836 *n_relations = count;
837 if (count == 0)
838 return S_FALSE;
840 for (long i = 0; i < count; ++i) {
841 relations_[i]->AddRef();
842 relations[i] = relations_[i];
845 return S_OK;
848 STDMETHODIMP BrowserAccessibilityWin::scrollTo(enum IA2ScrollType scroll_type) {
849 if (!instance_active())
850 return E_FAIL;
852 gfx::Rect r = GetLocation();
853 switch(scroll_type) {
854 case IA2_SCROLL_TYPE_TOP_LEFT:
855 manager()->ScrollToMakeVisible(*this, gfx::Rect(r.x(), r.y(), 0, 0));
856 break;
857 case IA2_SCROLL_TYPE_BOTTOM_RIGHT:
858 manager()->ScrollToMakeVisible(
859 *this, gfx::Rect(r.right(), r.bottom(), 0, 0));
860 break;
861 case IA2_SCROLL_TYPE_TOP_EDGE:
862 manager()->ScrollToMakeVisible(
863 *this, gfx::Rect(r.x(), r.y(), r.width(), 0));
864 break;
865 case IA2_SCROLL_TYPE_BOTTOM_EDGE:
866 manager()->ScrollToMakeVisible(
867 *this, gfx::Rect(r.x(), r.bottom(), r.width(), 0));
868 break;
869 case IA2_SCROLL_TYPE_LEFT_EDGE:
870 manager()->ScrollToMakeVisible(
871 *this, gfx::Rect(r.x(), r.y(), 0, r.height()));
872 break;
873 case IA2_SCROLL_TYPE_RIGHT_EDGE:
874 manager()->ScrollToMakeVisible(
875 *this, gfx::Rect(r.right(), r.y(), 0, r.height()));
876 break;
877 case IA2_SCROLL_TYPE_ANYWHERE:
878 default:
879 manager()->ScrollToMakeVisible(*this, r);
880 break;
883 manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
885 return S_OK;
888 STDMETHODIMP BrowserAccessibilityWin::scrollToPoint(
889 enum IA2CoordinateType coordinate_type,
890 LONG x,
891 LONG y) {
892 if (!instance_active())
893 return E_FAIL;
895 gfx::Point scroll_to(x, y);
897 if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
898 scroll_to -= manager()->GetViewBounds().OffsetFromOrigin();
899 } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
900 if (GetParent())
901 scroll_to += GetParent()->GetLocation().OffsetFromOrigin();
902 } else {
903 return E_INVALIDARG;
906 manager()->ScrollToPoint(*this, scroll_to);
907 manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
909 return S_OK;
912 STDMETHODIMP BrowserAccessibilityWin::get_groupPosition(
913 LONG* group_level,
914 LONG* similar_items_in_group,
915 LONG* position_in_group) {
916 if (!instance_active())
917 return E_FAIL;
919 if (!group_level || !similar_items_in_group || !position_in_group)
920 return E_INVALIDARG;
922 *group_level = 0;
923 *similar_items_in_group = GetIntAttribute(ui::AX_ATTR_SET_SIZE);
924 *position_in_group = GetIntAttribute(ui::AX_ATTR_POS_IN_SET);
925 return S_OK;
929 // IAccessibleEx methods not implemented.
932 STDMETHODIMP BrowserAccessibilityWin::get_extendedRole(BSTR* extended_role) {
933 return E_NOTIMPL;
935 STDMETHODIMP
936 BrowserAccessibilityWin::get_localizedExtendedRole(
937 BSTR* localized_extended_role) {
938 return E_NOTIMPL;
940 STDMETHODIMP
941 BrowserAccessibilityWin::get_nExtendedStates(LONG* n_extended_states) {
942 return E_NOTIMPL;
944 STDMETHODIMP
945 BrowserAccessibilityWin::get_extendedStates(LONG max_extended_states,
946 BSTR** extended_states,
947 LONG* n_extended_states) {
948 return E_NOTIMPL;
950 STDMETHODIMP
951 BrowserAccessibilityWin::get_localizedExtendedStates(
952 LONG max_localized_extended_states,
953 BSTR** localized_extended_states,
954 LONG* n_localized_extended_states) {
955 return E_NOTIMPL;
957 STDMETHODIMP BrowserAccessibilityWin::get_locale(IA2Locale* locale) {
958 return E_NOTIMPL;
962 // IAccessibleApplication methods.
965 STDMETHODIMP BrowserAccessibilityWin::get_appName(BSTR* app_name) {
966 // No need to check |instance_active()| because this interface is
967 // global, and doesn't depend on any local state.
969 if (!app_name)
970 return E_INVALIDARG;
972 // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out
973 // the part before the "/".
974 std::vector<std::string> product_components = base::SplitString(
975 GetContentClient()->GetProduct(), "/",
976 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
977 DCHECK_EQ(2U, product_components.size());
978 if (product_components.size() != 2)
979 return E_FAIL;
980 *app_name = SysAllocString(base::UTF8ToUTF16(product_components[0]).c_str());
981 DCHECK(*app_name);
982 return *app_name ? S_OK : E_FAIL;
985 STDMETHODIMP BrowserAccessibilityWin::get_appVersion(BSTR* app_version) {
986 // No need to check |instance_active()| because this interface is
987 // global, and doesn't depend on any local state.
989 if (!app_version)
990 return E_INVALIDARG;
992 // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out
993 // the part after the "/".
994 std::vector<std::string> product_components = base::SplitString(
995 GetContentClient()->GetProduct(), "/",
996 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
997 DCHECK_EQ(2U, product_components.size());
998 if (product_components.size() != 2)
999 return E_FAIL;
1000 *app_version =
1001 SysAllocString(base::UTF8ToUTF16(product_components[1]).c_str());
1002 DCHECK(*app_version);
1003 return *app_version ? S_OK : E_FAIL;
1006 STDMETHODIMP BrowserAccessibilityWin::get_toolkitName(BSTR* toolkit_name) {
1007 // No need to check |instance_active()| because this interface is
1008 // global, and doesn't depend on any local state.
1010 if (!toolkit_name)
1011 return E_INVALIDARG;
1013 // This is hard-coded; all products based on the Chromium engine
1014 // will have the same toolkit name, so that assistive technology can
1015 // detect any Chrome-based product.
1016 *toolkit_name = SysAllocString(L"Chrome");
1017 DCHECK(*toolkit_name);
1018 return *toolkit_name ? S_OK : E_FAIL;
1021 STDMETHODIMP BrowserAccessibilityWin::get_toolkitVersion(
1022 BSTR* toolkit_version) {
1023 // No need to check |instance_active()| because this interface is
1024 // global, and doesn't depend on any local state.
1026 if (!toolkit_version)
1027 return E_INVALIDARG;
1029 std::string user_agent = GetContentClient()->GetUserAgent();
1030 *toolkit_version = SysAllocString(base::UTF8ToUTF16(user_agent).c_str());
1031 DCHECK(*toolkit_version);
1032 return *toolkit_version ? S_OK : E_FAIL;
1036 // IAccessibleImage methods.
1039 STDMETHODIMP BrowserAccessibilityWin::get_description(BSTR* desc) {
1040 if (!instance_active())
1041 return E_FAIL;
1043 if (!desc)
1044 return E_INVALIDARG;
1046 if (description().empty())
1047 return S_FALSE;
1049 *desc = SysAllocString(description().c_str());
1051 DCHECK(*desc);
1052 return S_OK;
1055 STDMETHODIMP BrowserAccessibilityWin::get_imagePosition(
1056 enum IA2CoordinateType coordinate_type,
1057 LONG* x,
1058 LONG* y) {
1059 if (!instance_active())
1060 return E_FAIL;
1062 if (!x || !y)
1063 return E_INVALIDARG;
1065 if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
1066 HWND parent_hwnd =
1067 manager()->ToBrowserAccessibilityManagerWin()->GetParentHWND();
1068 if (!parent_hwnd)
1069 return E_FAIL;
1070 POINT top_left = {0, 0};
1071 ::ClientToScreen(parent_hwnd, &top_left);
1072 *x = GetLocation().x() + top_left.x;
1073 *y = GetLocation().y() + top_left.y;
1074 } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
1075 *x = GetLocation().x();
1076 *y = GetLocation().y();
1077 if (GetParent()) {
1078 *x -= GetParent()->GetLocation().x();
1079 *y -= GetParent()->GetLocation().y();
1081 } else {
1082 return E_INVALIDARG;
1085 return S_OK;
1088 STDMETHODIMP BrowserAccessibilityWin::get_imageSize(LONG* height, LONG* width) {
1089 if (!instance_active())
1090 return E_FAIL;
1092 if (!height || !width)
1093 return E_INVALIDARG;
1095 *height = GetLocation().height();
1096 *width = GetLocation().width();
1097 return S_OK;
1101 // IAccessibleTable methods.
1104 STDMETHODIMP BrowserAccessibilityWin::get_accessibleAt(
1105 long row,
1106 long column,
1107 IUnknown** accessible) {
1108 if (!instance_active())
1109 return E_FAIL;
1111 if (!accessible)
1112 return E_INVALIDARG;
1114 int columns;
1115 int rows;
1116 if (!GetIntAttribute(
1117 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1118 !GetIntAttribute(
1119 ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1120 columns <= 0 ||
1121 rows <= 0) {
1122 return S_FALSE;
1125 if (row < 0 || row >= rows || column < 0 || column >= columns)
1126 return E_INVALIDARG;
1128 const std::vector<int32>& cell_ids = GetIntListAttribute(
1129 ui::AX_ATTR_CELL_IDS);
1130 DCHECK_EQ(columns * rows, static_cast<int>(cell_ids.size()));
1132 int cell_id = cell_ids[row * columns + column];
1133 BrowserAccessibilityWin* cell = GetFromID(cell_id);
1134 if (cell) {
1135 *accessible = static_cast<IAccessible*>(cell->NewReference());
1136 return S_OK;
1139 *accessible = NULL;
1140 return E_INVALIDARG;
1143 STDMETHODIMP BrowserAccessibilityWin::get_caption(IUnknown** accessible) {
1144 if (!instance_active())
1145 return E_FAIL;
1147 if (!accessible)
1148 return E_INVALIDARG;
1150 // TODO(dmazzoni): implement
1151 return S_FALSE;
1154 STDMETHODIMP BrowserAccessibilityWin::get_childIndex(long row,
1155 long column,
1156 long* cell_index) {
1157 if (!instance_active())
1158 return E_FAIL;
1160 if (!cell_index)
1161 return E_INVALIDARG;
1163 int columns;
1164 int rows;
1165 if (!GetIntAttribute(
1166 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1167 !GetIntAttribute(
1168 ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1169 columns <= 0 ||
1170 rows <= 0) {
1171 return S_FALSE;
1174 if (row < 0 || row >= rows || column < 0 || column >= columns)
1175 return E_INVALIDARG;
1177 const std::vector<int32>& cell_ids = GetIntListAttribute(
1178 ui::AX_ATTR_CELL_IDS);
1179 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1180 ui::AX_ATTR_UNIQUE_CELL_IDS);
1181 DCHECK_EQ(columns * rows, static_cast<int>(cell_ids.size()));
1182 int cell_id = cell_ids[row * columns + column];
1183 for (size_t i = 0; i < unique_cell_ids.size(); ++i) {
1184 if (unique_cell_ids[i] == cell_id) {
1185 *cell_index = (long)i;
1186 return S_OK;
1190 return S_FALSE;
1193 STDMETHODIMP BrowserAccessibilityWin::get_columnDescription(long column,
1194 BSTR* description) {
1195 if (!instance_active())
1196 return E_FAIL;
1198 if (!description)
1199 return E_INVALIDARG;
1201 int columns;
1202 int rows;
1203 if (!GetIntAttribute(
1204 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1205 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1206 columns <= 0 ||
1207 rows <= 0) {
1208 return S_FALSE;
1211 if (column < 0 || column >= columns)
1212 return E_INVALIDARG;
1214 const std::vector<int32>& cell_ids = GetIntListAttribute(
1215 ui::AX_ATTR_CELL_IDS);
1216 for (int i = 0; i < rows; ++i) {
1217 int cell_id = cell_ids[i * columns + column];
1218 BrowserAccessibilityWin* cell = static_cast<BrowserAccessibilityWin*>(
1219 manager()->GetFromID(cell_id));
1220 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER) {
1221 base::string16 cell_name = cell->GetString16Attribute(
1222 ui::AX_ATTR_NAME);
1223 if (cell_name.size() > 0) {
1224 *description = SysAllocString(cell_name.c_str());
1225 return S_OK;
1228 if (cell->description().size() > 0) {
1229 *description = SysAllocString(cell->description().c_str());
1230 return S_OK;
1235 return S_FALSE;
1238 STDMETHODIMP BrowserAccessibilityWin::get_columnExtentAt(
1239 long row,
1240 long column,
1241 long* n_columns_spanned) {
1242 if (!instance_active())
1243 return E_FAIL;
1245 if (!n_columns_spanned)
1246 return E_INVALIDARG;
1248 int columns;
1249 int rows;
1250 if (!GetIntAttribute(
1251 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1252 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1253 columns <= 0 ||
1254 rows <= 0) {
1255 return S_FALSE;
1258 if (row < 0 || row >= rows || column < 0 || column >= columns)
1259 return E_INVALIDARG;
1261 const std::vector<int32>& cell_ids = GetIntListAttribute(
1262 ui::AX_ATTR_CELL_IDS);
1263 int cell_id = cell_ids[row * columns + column];
1264 BrowserAccessibilityWin* cell = static_cast<BrowserAccessibilityWin*>(
1265 manager()->GetFromID(cell_id));
1266 int colspan;
1267 if (cell &&
1268 cell->GetIntAttribute(
1269 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1270 colspan >= 1) {
1271 *n_columns_spanned = colspan;
1272 return S_OK;
1275 return S_FALSE;
1278 STDMETHODIMP BrowserAccessibilityWin::get_columnHeader(
1279 IAccessibleTable** accessible_table,
1280 long* starting_row_index) {
1281 // TODO(dmazzoni): implement
1282 return E_NOTIMPL;
1285 STDMETHODIMP BrowserAccessibilityWin::get_columnIndex(long cell_index,
1286 long* column_index) {
1287 if (!instance_active())
1288 return E_FAIL;
1290 if (!column_index)
1291 return E_INVALIDARG;
1293 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1294 ui::AX_ATTR_UNIQUE_CELL_IDS);
1295 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1296 if (cell_index < 0)
1297 return E_INVALIDARG;
1298 if (cell_index >= cell_id_count)
1299 return S_FALSE;
1301 int cell_id = unique_cell_ids[cell_index];
1302 BrowserAccessibilityWin* cell =
1303 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1304 int col_index;
1305 if (cell &&
1306 cell->GetIntAttribute(
1307 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &col_index)) {
1308 *column_index = col_index;
1309 return S_OK;
1312 return S_FALSE;
1315 STDMETHODIMP BrowserAccessibilityWin::get_nColumns(long* column_count) {
1316 if (!instance_active())
1317 return E_FAIL;
1319 if (!column_count)
1320 return E_INVALIDARG;
1322 int columns;
1323 if (GetIntAttribute(
1324 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns)) {
1325 *column_count = columns;
1326 return S_OK;
1329 return S_FALSE;
1332 STDMETHODIMP BrowserAccessibilityWin::get_nRows(long* row_count) {
1333 if (!instance_active())
1334 return E_FAIL;
1336 if (!row_count)
1337 return E_INVALIDARG;
1339 int rows;
1340 if (GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1341 *row_count = rows;
1342 return S_OK;
1345 return S_FALSE;
1348 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedChildren(long* cell_count) {
1349 if (!instance_active())
1350 return E_FAIL;
1352 if (!cell_count)
1353 return E_INVALIDARG;
1355 // TODO(dmazzoni): add support for selected cells/rows/columns in tables.
1356 *cell_count = 0;
1357 return S_OK;
1360 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedColumns(long* column_count) {
1361 if (!instance_active())
1362 return E_FAIL;
1364 if (!column_count)
1365 return E_INVALIDARG;
1367 *column_count = 0;
1368 return S_OK;
1371 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedRows(long* row_count) {
1372 if (!instance_active())
1373 return E_FAIL;
1375 if (!row_count)
1376 return E_INVALIDARG;
1378 *row_count = 0;
1379 return S_OK;
1382 STDMETHODIMP BrowserAccessibilityWin::get_rowDescription(long row,
1383 BSTR* description) {
1384 if (!instance_active())
1385 return E_FAIL;
1387 if (!description)
1388 return E_INVALIDARG;
1390 int columns;
1391 int rows;
1392 if (!GetIntAttribute(
1393 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1394 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1395 columns <= 0 ||
1396 rows <= 0) {
1397 return S_FALSE;
1400 if (row < 0 || row >= rows)
1401 return E_INVALIDARG;
1403 const std::vector<int32>& cell_ids = GetIntListAttribute(
1404 ui::AX_ATTR_CELL_IDS);
1405 for (int i = 0; i < columns; ++i) {
1406 int cell_id = cell_ids[row * columns + i];
1407 BrowserAccessibilityWin* cell =
1408 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1409 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER) {
1410 base::string16 cell_name = cell->GetString16Attribute(
1411 ui::AX_ATTR_NAME);
1412 if (cell_name.size() > 0) {
1413 *description = SysAllocString(cell_name.c_str());
1414 return S_OK;
1417 if (cell->description().size() > 0) {
1418 *description = SysAllocString(cell->description().c_str());
1419 return S_OK;
1424 return S_FALSE;
1427 STDMETHODIMP BrowserAccessibilityWin::get_rowExtentAt(long row,
1428 long column,
1429 long* n_rows_spanned) {
1430 if (!instance_active())
1431 return E_FAIL;
1433 if (!n_rows_spanned)
1434 return E_INVALIDARG;
1436 int columns;
1437 int rows;
1438 if (!GetIntAttribute(
1439 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1440 !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1441 columns <= 0 ||
1442 rows <= 0) {
1443 return S_FALSE;
1446 if (row < 0 || row >= rows || column < 0 || column >= columns)
1447 return E_INVALIDARG;
1449 const std::vector<int32>& cell_ids = GetIntListAttribute(
1450 ui::AX_ATTR_CELL_IDS);
1451 int cell_id = cell_ids[row * columns + column];
1452 BrowserAccessibilityWin* cell =
1453 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1454 int rowspan;
1455 if (cell &&
1456 cell->GetIntAttribute(
1457 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1458 rowspan >= 1) {
1459 *n_rows_spanned = rowspan;
1460 return S_OK;
1463 return S_FALSE;
1466 STDMETHODIMP BrowserAccessibilityWin::get_rowHeader(
1467 IAccessibleTable** accessible_table,
1468 long* starting_column_index) {
1469 // TODO(dmazzoni): implement
1470 return E_NOTIMPL;
1473 STDMETHODIMP BrowserAccessibilityWin::get_rowIndex(long cell_index,
1474 long* row_index) {
1475 if (!instance_active())
1476 return E_FAIL;
1478 if (!row_index)
1479 return E_INVALIDARG;
1481 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1482 ui::AX_ATTR_UNIQUE_CELL_IDS);
1483 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1484 if (cell_index < 0)
1485 return E_INVALIDARG;
1486 if (cell_index >= cell_id_count)
1487 return S_FALSE;
1489 int cell_id = unique_cell_ids[cell_index];
1490 BrowserAccessibilityWin* cell =
1491 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1492 int cell_row_index;
1493 if (cell &&
1494 cell->GetIntAttribute(
1495 ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &cell_row_index)) {
1496 *row_index = cell_row_index;
1497 return S_OK;
1500 return S_FALSE;
1503 STDMETHODIMP BrowserAccessibilityWin::get_selectedChildren(long max_children,
1504 long** children,
1505 long* n_children) {
1506 if (!instance_active())
1507 return E_FAIL;
1509 if (!children || !n_children)
1510 return E_INVALIDARG;
1512 // TODO(dmazzoni): Implement this.
1513 *n_children = 0;
1514 return S_OK;
1517 STDMETHODIMP BrowserAccessibilityWin::get_selectedColumns(long max_columns,
1518 long** columns,
1519 long* n_columns) {
1520 if (!instance_active())
1521 return E_FAIL;
1523 if (!columns || !n_columns)
1524 return E_INVALIDARG;
1526 // TODO(dmazzoni): Implement this.
1527 *n_columns = 0;
1528 return S_OK;
1531 STDMETHODIMP BrowserAccessibilityWin::get_selectedRows(long max_rows,
1532 long** rows,
1533 long* n_rows) {
1534 if (!instance_active())
1535 return E_FAIL;
1537 if (!rows || !n_rows)
1538 return E_INVALIDARG;
1540 // TODO(dmazzoni): Implement this.
1541 *n_rows = 0;
1542 return S_OK;
1545 STDMETHODIMP BrowserAccessibilityWin::get_summary(IUnknown** accessible) {
1546 if (!instance_active())
1547 return E_FAIL;
1549 if (!accessible)
1550 return E_INVALIDARG;
1552 // TODO(dmazzoni): implement
1553 return S_FALSE;
1556 STDMETHODIMP BrowserAccessibilityWin::get_isColumnSelected(
1557 long column,
1558 boolean* is_selected) {
1559 if (!instance_active())
1560 return E_FAIL;
1562 if (!is_selected)
1563 return E_INVALIDARG;
1565 // TODO(dmazzoni): Implement this.
1566 *is_selected = false;
1567 return S_OK;
1570 STDMETHODIMP BrowserAccessibilityWin::get_isRowSelected(long row,
1571 boolean* is_selected) {
1572 if (!instance_active())
1573 return E_FAIL;
1575 if (!is_selected)
1576 return E_INVALIDARG;
1578 // TODO(dmazzoni): Implement this.
1579 *is_selected = false;
1580 return S_OK;
1583 STDMETHODIMP BrowserAccessibilityWin::get_isSelected(long row,
1584 long column,
1585 boolean* is_selected) {
1586 if (!instance_active())
1587 return E_FAIL;
1589 if (!is_selected)
1590 return E_INVALIDARG;
1592 // TODO(dmazzoni): Implement this.
1593 *is_selected = false;
1594 return S_OK;
1597 STDMETHODIMP BrowserAccessibilityWin::get_rowColumnExtentsAtIndex(
1598 long index,
1599 long* row,
1600 long* column,
1601 long* row_extents,
1602 long* column_extents,
1603 boolean* is_selected) {
1604 if (!instance_active())
1605 return E_FAIL;
1607 if (!row || !column || !row_extents || !column_extents || !is_selected)
1608 return E_INVALIDARG;
1610 const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1611 ui::AX_ATTR_UNIQUE_CELL_IDS);
1612 int cell_id_count = static_cast<int>(unique_cell_ids.size());
1613 if (index < 0)
1614 return E_INVALIDARG;
1615 if (index >= cell_id_count)
1616 return S_FALSE;
1618 int cell_id = unique_cell_ids[index];
1619 BrowserAccessibilityWin* cell =
1620 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1621 int rowspan;
1622 int colspan;
1623 if (cell &&
1624 cell->GetIntAttribute(
1625 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1626 cell->GetIntAttribute(
1627 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1628 rowspan >= 1 &&
1629 colspan >= 1) {
1630 *row_extents = rowspan;
1631 *column_extents = colspan;
1632 return S_OK;
1635 return S_FALSE;
1638 STDMETHODIMP BrowserAccessibilityWin::selectRow(long row) {
1639 return E_NOTIMPL;
1642 STDMETHODIMP BrowserAccessibilityWin::selectColumn(long column) {
1643 return E_NOTIMPL;
1646 STDMETHODIMP BrowserAccessibilityWin::unselectRow(long row) {
1647 return E_NOTIMPL;
1650 STDMETHODIMP BrowserAccessibilityWin::unselectColumn(long column) {
1651 return E_NOTIMPL;
1654 STDMETHODIMP
1655 BrowserAccessibilityWin::get_modelChange(IA2TableModelChange* model_change) {
1656 return E_NOTIMPL;
1660 // IAccessibleTable2 methods.
1663 STDMETHODIMP BrowserAccessibilityWin::get_cellAt(long row,
1664 long column,
1665 IUnknown** cell) {
1666 return get_accessibleAt(row, column, cell);
1669 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedCells(long* cell_count) {
1670 return get_nSelectedChildren(cell_count);
1673 STDMETHODIMP BrowserAccessibilityWin::get_selectedCells(
1674 IUnknown*** cells,
1675 long* n_selected_cells) {
1676 if (!instance_active())
1677 return E_FAIL;
1679 if (!cells || !n_selected_cells)
1680 return E_INVALIDARG;
1682 // TODO(dmazzoni): Implement this.
1683 *n_selected_cells = 0;
1684 return S_OK;
1687 STDMETHODIMP BrowserAccessibilityWin::get_selectedColumns(long** columns,
1688 long* n_columns) {
1689 if (!instance_active())
1690 return E_FAIL;
1692 if (!columns || !n_columns)
1693 return E_INVALIDARG;
1695 // TODO(dmazzoni): Implement this.
1696 *n_columns = 0;
1697 return S_OK;
1700 STDMETHODIMP BrowserAccessibilityWin::get_selectedRows(long** rows,
1701 long* n_rows) {
1702 if (!instance_active())
1703 return E_FAIL;
1705 if (!rows || !n_rows)
1706 return E_INVALIDARG;
1708 // TODO(dmazzoni): Implement this.
1709 *n_rows = 0;
1710 return S_OK;
1715 // IAccessibleTableCell methods.
1718 STDMETHODIMP BrowserAccessibilityWin::get_columnExtent(
1719 long* n_columns_spanned) {
1720 if (!instance_active())
1721 return E_FAIL;
1723 if (!n_columns_spanned)
1724 return E_INVALIDARG;
1726 int colspan;
1727 if (GetIntAttribute(
1728 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1729 colspan >= 1) {
1730 *n_columns_spanned = colspan;
1731 return S_OK;
1734 return S_FALSE;
1737 STDMETHODIMP BrowserAccessibilityWin::get_columnHeaderCells(
1738 IUnknown*** cell_accessibles,
1739 long* n_column_header_cells) {
1740 if (!instance_active())
1741 return E_FAIL;
1743 if (!cell_accessibles || !n_column_header_cells)
1744 return E_INVALIDARG;
1746 *n_column_header_cells = 0;
1748 int column;
1749 if (!GetIntAttribute(
1750 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column)) {
1751 return S_FALSE;
1754 BrowserAccessibility* table = GetParent();
1755 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
1756 table = table->GetParent();
1757 if (!table) {
1758 NOTREACHED();
1759 return S_FALSE;
1762 int columns;
1763 int rows;
1764 if (!table->GetIntAttribute(
1765 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1766 !table->GetIntAttribute(
1767 ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1768 return S_FALSE;
1770 if (columns <= 0 || rows <= 0 || column < 0 || column >= columns)
1771 return S_FALSE;
1773 const std::vector<int32>& cell_ids = table->GetIntListAttribute(
1774 ui::AX_ATTR_CELL_IDS);
1776 for (int i = 0; i < rows; ++i) {
1777 int cell_id = cell_ids[i * columns + column];
1778 BrowserAccessibilityWin* cell =
1779 manager()->GetFromID(cell_id)->ToBrowserAccessibilityWin();
1780 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER)
1781 (*n_column_header_cells)++;
1784 *cell_accessibles = static_cast<IUnknown**>(CoTaskMemAlloc(
1785 (*n_column_header_cells) * sizeof(cell_accessibles[0])));
1786 int index = 0;
1787 for (int i = 0; i < rows; ++i) {
1788 int cell_id = cell_ids[i * columns + column];
1789 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1790 if (cell && cell->GetRole() == ui::AX_ROLE_COLUMN_HEADER) {
1791 (*cell_accessibles)[index] = static_cast<IAccessible*>(
1792 cell->ToBrowserAccessibilityWin()->NewReference());
1793 ++index;
1797 return S_OK;
1800 STDMETHODIMP BrowserAccessibilityWin::get_columnIndex(long* column_index) {
1801 if (!instance_active())
1802 return E_FAIL;
1804 if (!column_index)
1805 return E_INVALIDARG;
1807 int column;
1808 if (GetIntAttribute(
1809 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column)) {
1810 *column_index = column;
1811 return S_OK;
1814 return S_FALSE;
1817 STDMETHODIMP BrowserAccessibilityWin::get_rowExtent(long* n_rows_spanned) {
1818 if (!instance_active())
1819 return E_FAIL;
1821 if (!n_rows_spanned)
1822 return E_INVALIDARG;
1824 int rowspan;
1825 if (GetIntAttribute(
1826 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1827 rowspan >= 1) {
1828 *n_rows_spanned = rowspan;
1829 return S_OK;
1832 return S_FALSE;
1835 STDMETHODIMP BrowserAccessibilityWin::get_rowHeaderCells(
1836 IUnknown*** cell_accessibles,
1837 long* n_row_header_cells) {
1838 if (!instance_active())
1839 return E_FAIL;
1841 if (!cell_accessibles || !n_row_header_cells)
1842 return E_INVALIDARG;
1844 *n_row_header_cells = 0;
1846 int row;
1847 if (!GetIntAttribute(
1848 ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row)) {
1849 return S_FALSE;
1852 BrowserAccessibility* table = GetParent();
1853 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
1854 table = table->GetParent();
1855 if (!table) {
1856 NOTREACHED();
1857 return S_FALSE;
1860 int columns;
1861 int rows;
1862 if (!table->GetIntAttribute(
1863 ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1864 !table->GetIntAttribute(
1865 ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1866 return S_FALSE;
1868 if (columns <= 0 || rows <= 0 || row < 0 || row >= rows)
1869 return S_FALSE;
1871 const std::vector<int32>& cell_ids = table->GetIntListAttribute(
1872 ui::AX_ATTR_CELL_IDS);
1874 for (int i = 0; i < columns; ++i) {
1875 int cell_id = cell_ids[row * columns + i];
1876 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1877 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER)
1878 (*n_row_header_cells)++;
1881 *cell_accessibles = static_cast<IUnknown**>(CoTaskMemAlloc(
1882 (*n_row_header_cells) * sizeof(cell_accessibles[0])));
1883 int index = 0;
1884 for (int i = 0; i < columns; ++i) {
1885 int cell_id = cell_ids[row * columns + i];
1886 BrowserAccessibility* cell = manager()->GetFromID(cell_id);
1887 if (cell && cell->GetRole() == ui::AX_ROLE_ROW_HEADER) {
1888 (*cell_accessibles)[index] = static_cast<IAccessible*>(
1889 cell->ToBrowserAccessibilityWin()->NewReference());
1890 ++index;
1894 return S_OK;
1897 STDMETHODIMP BrowserAccessibilityWin::get_rowIndex(long* row_index) {
1898 if (!instance_active())
1899 return E_FAIL;
1901 if (!row_index)
1902 return E_INVALIDARG;
1904 int row;
1905 if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row)) {
1906 *row_index = row;
1907 return S_OK;
1909 return S_FALSE;
1912 STDMETHODIMP BrowserAccessibilityWin::get_isSelected(boolean* is_selected) {
1913 if (!instance_active())
1914 return E_FAIL;
1916 if (!is_selected)
1917 return E_INVALIDARG;
1919 *is_selected = false;
1920 return S_OK;
1923 STDMETHODIMP BrowserAccessibilityWin::get_rowColumnExtents(
1924 long* row_index,
1925 long* column_index,
1926 long* row_extents,
1927 long* column_extents,
1928 boolean* is_selected) {
1929 if (!instance_active())
1930 return E_FAIL;
1932 if (!row_index ||
1933 !column_index ||
1934 !row_extents ||
1935 !column_extents ||
1936 !is_selected) {
1937 return E_INVALIDARG;
1940 int row;
1941 int column;
1942 int rowspan;
1943 int colspan;
1944 if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row) &&
1945 GetIntAttribute(
1946 ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column) &&
1947 GetIntAttribute(
1948 ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1949 GetIntAttribute(
1950 ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan)) {
1951 *row_index = row;
1952 *column_index = column;
1953 *row_extents = rowspan;
1954 *column_extents = colspan;
1955 *is_selected = false;
1956 return S_OK;
1959 return S_FALSE;
1962 STDMETHODIMP BrowserAccessibilityWin::get_table(IUnknown** table) {
1963 if (!instance_active())
1964 return E_FAIL;
1966 if (!table)
1967 return E_INVALIDARG;
1970 int row;
1971 int column;
1972 GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row);
1973 GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column);
1975 BrowserAccessibility* find_table = GetParent();
1976 while (find_table && find_table->GetRole() != ui::AX_ROLE_TABLE)
1977 find_table = find_table->GetParent();
1978 if (!find_table) {
1979 NOTREACHED();
1980 return S_FALSE;
1983 *table = static_cast<IAccessibleTable*>(
1984 find_table->ToBrowserAccessibilityWin()->NewReference());
1986 return S_OK;
1990 // IAccessibleText methods.
1993 STDMETHODIMP BrowserAccessibilityWin::get_nCharacters(LONG* n_characters) {
1994 if (!instance_active())
1995 return E_FAIL;
1997 if (!n_characters)
1998 return E_INVALIDARG;
2000 *n_characters = TextForIAccessibleText().length();
2001 return S_OK;
2004 STDMETHODIMP BrowserAccessibilityWin::get_caretOffset(LONG* offset) {
2005 if (!instance_active())
2006 return E_FAIL;
2008 if (!offset)
2009 return E_INVALIDARG;
2011 int selection_start, selection_end;
2012 GetSelectionOffsets(&selection_start, &selection_end);
2013 *offset = selection_start;
2014 if (selection_start < 0)
2015 return S_FALSE;
2017 return S_OK;
2020 STDMETHODIMP BrowserAccessibilityWin::get_characterExtents(
2021 LONG offset,
2022 enum IA2CoordinateType coordinate_type,
2023 LONG* out_x,
2024 LONG* out_y,
2025 LONG* out_width,
2026 LONG* out_height) {
2027 if (!instance_active())
2028 return E_FAIL;
2030 if (!out_x || !out_y || !out_width || !out_height)
2031 return E_INVALIDARG;
2033 const base::string16& text_str = TextForIAccessibleText();
2034 HandleSpecialTextOffset(text_str, &offset);
2036 if (offset < 0 || offset > static_cast<LONG>(text_str.size()))
2037 return E_INVALIDARG;
2039 gfx::Rect character_bounds;
2040 if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
2041 character_bounds = GetGlobalBoundsForRange(offset, 1);
2042 } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
2043 character_bounds = GetLocalBoundsForRange(offset, 1);
2044 character_bounds -= GetLocation().OffsetFromOrigin();
2045 } else {
2046 return E_INVALIDARG;
2049 *out_x = character_bounds.x();
2050 *out_y = character_bounds.y();
2051 *out_width = character_bounds.width();
2052 *out_height = character_bounds.height();
2054 return S_OK;
2057 STDMETHODIMP BrowserAccessibilityWin::get_nSelections(LONG* n_selections) {
2058 if (!instance_active())
2059 return E_FAIL;
2061 if (!n_selections)
2062 return E_INVALIDARG;
2064 *n_selections = 0;
2065 int selection_start, selection_end;
2066 GetSelectionOffsets(&selection_start, &selection_end);
2067 if (selection_start >= 0 && selection_end >= 0 &&
2068 selection_start != selection_end)
2069 *n_selections = 1;
2071 return S_OK;
2074 STDMETHODIMP BrowserAccessibilityWin::get_selection(LONG selection_index,
2075 LONG* start_offset,
2076 LONG* end_offset) {
2077 if (!instance_active())
2078 return E_FAIL;
2080 if (!start_offset || !end_offset || selection_index != 0)
2081 return E_INVALIDARG;
2083 LONG n_selections = 0;
2084 if (FAILED(get_nSelections(&n_selections)) || n_selections < 1)
2085 return E_INVALIDARG;
2087 *start_offset = 0;
2088 *end_offset = 0;
2089 int selection_start, selection_end;
2090 GetSelectionOffsets(&selection_start, &selection_end);
2091 if (selection_start >= 0 && selection_end >= 0) {
2092 *start_offset = selection_start;
2093 *end_offset = selection_end;
2096 return S_OK;
2099 STDMETHODIMP BrowserAccessibilityWin::get_text(LONG start_offset,
2100 LONG end_offset,
2101 BSTR* text) {
2102 if (!instance_active())
2103 return E_FAIL;
2105 if (!text)
2106 return E_INVALIDARG;
2108 const base::string16& text_str = TextForIAccessibleText();
2110 // Handle special text offsets.
2111 HandleSpecialTextOffset(text_str, &start_offset);
2112 HandleSpecialTextOffset(text_str, &end_offset);
2114 // The spec allows the arguments to be reversed.
2115 if (start_offset > end_offset) {
2116 LONG tmp = start_offset;
2117 start_offset = end_offset;
2118 end_offset = tmp;
2121 // The spec does not allow the start or end offsets to be out or range;
2122 // we must return an error if so.
2123 LONG len = text_str.length();
2124 if (start_offset < 0)
2125 return E_INVALIDARG;
2126 if (end_offset > len)
2127 return E_INVALIDARG;
2129 base::string16 substr = text_str.substr(start_offset,
2130 end_offset - start_offset);
2131 if (substr.empty())
2132 return S_FALSE;
2134 *text = SysAllocString(substr.c_str());
2135 DCHECK(*text);
2136 return S_OK;
2139 STDMETHODIMP BrowserAccessibilityWin::get_textAtOffset(
2140 LONG offset,
2141 enum IA2TextBoundaryType boundary_type,
2142 LONG* start_offset,
2143 LONG* end_offset,
2144 BSTR* text) {
2145 if (!instance_active())
2146 return E_FAIL;
2148 if (!start_offset || !end_offset || !text)
2149 return E_INVALIDARG;
2151 const base::string16& text_str = TextForIAccessibleText();
2152 HandleSpecialTextOffset(text_str, &offset);
2153 if (offset < 0)
2154 return E_INVALIDARG;
2156 LONG text_len = text_str.length();
2157 if (offset > text_len)
2158 return E_INVALIDARG;
2160 // The IAccessible2 spec says we don't have to implement the "sentence"
2161 // boundary type, we can just let the screenreader handle it.
2162 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2163 *start_offset = 0;
2164 *end_offset = 0;
2165 *text = NULL;
2166 return S_FALSE;
2169 // According to the IA2 Spec, only line boundaries should succeed when
2170 // the offset is one past the end of the text.
2171 if (offset == text_len && boundary_type != IA2_TEXT_BOUNDARY_LINE) {
2172 *start_offset = 0;
2173 *end_offset = 0;
2174 *text = nullptr;
2175 return S_FALSE;
2178 *start_offset = FindBoundary(
2179 text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
2180 *end_offset = FindBoundary(
2181 text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
2182 return get_text(*start_offset, *end_offset, text);
2185 STDMETHODIMP BrowserAccessibilityWin::get_textBeforeOffset(
2186 LONG offset,
2187 enum IA2TextBoundaryType boundary_type,
2188 LONG* start_offset,
2189 LONG* end_offset,
2190 BSTR* text) {
2191 if (!instance_active())
2192 return E_FAIL;
2194 if (!start_offset || !end_offset || !text)
2195 return E_INVALIDARG;
2197 // The IAccessible2 spec says we don't have to implement the "sentence"
2198 // boundary type, we can just let the screenreader handle it.
2199 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2200 *start_offset = 0;
2201 *end_offset = 0;
2202 *text = NULL;
2203 return S_FALSE;
2206 const base::string16& text_str = TextForIAccessibleText();
2208 *start_offset = FindBoundary(
2209 text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
2210 *end_offset = offset;
2211 return get_text(*start_offset, *end_offset, text);
2214 STDMETHODIMP BrowserAccessibilityWin::get_textAfterOffset(
2215 LONG offset,
2216 enum IA2TextBoundaryType boundary_type,
2217 LONG* start_offset,
2218 LONG* end_offset,
2219 BSTR* text) {
2220 if (!instance_active())
2221 return E_FAIL;
2223 if (!start_offset || !end_offset || !text)
2224 return E_INVALIDARG;
2226 // The IAccessible2 spec says we don't have to implement the "sentence"
2227 // boundary type, we can just let the screenreader handle it.
2228 if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2229 *start_offset = 0;
2230 *end_offset = 0;
2231 *text = NULL;
2232 return S_FALSE;
2235 const base::string16& text_str = TextForIAccessibleText();
2237 *start_offset = offset;
2238 *end_offset = FindBoundary(
2239 text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
2240 return get_text(*start_offset, *end_offset, text);
2243 STDMETHODIMP BrowserAccessibilityWin::get_newText(IA2TextSegment* new_text) {
2244 if (!instance_active())
2245 return E_FAIL;
2247 if (!new_text)
2248 return E_INVALIDARG;
2250 if (!old_win_attributes_)
2251 return E_FAIL;
2253 int start, old_len, new_len;
2254 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
2255 if (new_len == 0)
2256 return E_FAIL;
2258 base::string16 substr = hypertext().substr(start, new_len);
2259 new_text->text = SysAllocString(substr.c_str());
2260 new_text->start = static_cast<long>(start);
2261 new_text->end = static_cast<long>(start + new_len);
2262 return S_OK;
2265 STDMETHODIMP BrowserAccessibilityWin::get_oldText(IA2TextSegment* old_text) {
2266 if (!instance_active())
2267 return E_FAIL;
2269 if (!old_text)
2270 return E_INVALIDARG;
2272 if (!old_win_attributes_)
2273 return E_FAIL;
2275 int start, old_len, new_len;
2276 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
2277 if (old_len == 0)
2278 return E_FAIL;
2280 base::string16 old_hypertext = old_win_attributes_->hypertext;
2281 base::string16 substr = old_hypertext.substr(start, old_len);
2282 old_text->text = SysAllocString(substr.c_str());
2283 old_text->start = static_cast<long>(start);
2284 old_text->end = static_cast<long>(start + old_len);
2285 return S_OK;
2288 STDMETHODIMP BrowserAccessibilityWin::get_offsetAtPoint(
2289 LONG x,
2290 LONG y,
2291 enum IA2CoordinateType coord_type,
2292 LONG* offset) {
2293 if (!instance_active())
2294 return E_FAIL;
2296 if (!offset)
2297 return E_INVALIDARG;
2299 // TODO(dmazzoni): implement this. We're returning S_OK for now so that
2300 // screen readers still return partially accurate results rather than
2301 // completely failing.
2302 *offset = 0;
2303 return S_OK;
2306 STDMETHODIMP BrowserAccessibilityWin::scrollSubstringTo(
2307 LONG start_index,
2308 LONG end_index,
2309 enum IA2ScrollType scroll_type) {
2310 // TODO(dmazzoni): adjust this for the start and end index, too.
2311 return scrollTo(scroll_type);
2314 STDMETHODIMP BrowserAccessibilityWin::scrollSubstringToPoint(
2315 LONG start_index,
2316 LONG end_index,
2317 enum IA2CoordinateType coordinate_type,
2318 LONG x, LONG y) {
2319 // TODO(dmazzoni): adjust this for the start and end index, too.
2320 return scrollToPoint(coordinate_type, x, y);
2323 STDMETHODIMP BrowserAccessibilityWin::addSelection(LONG start_offset,
2324 LONG end_offset) {
2325 if (!instance_active())
2326 return E_FAIL;
2328 const base::string16& text_str = TextForIAccessibleText();
2329 HandleSpecialTextOffset(text_str, &start_offset);
2330 HandleSpecialTextOffset(text_str, &end_offset);
2332 manager()->SetTextSelection(*this, start_offset, end_offset);
2333 return S_OK;
2336 STDMETHODIMP BrowserAccessibilityWin::removeSelection(LONG selection_index) {
2337 if (!instance_active())
2338 return E_FAIL;
2340 if (selection_index != 0)
2341 return E_INVALIDARG;
2343 manager()->SetTextSelection(*this, 0, 0);
2344 return S_OK;
2347 STDMETHODIMP BrowserAccessibilityWin::setCaretOffset(LONG offset) {
2348 if (!instance_active())
2349 return E_FAIL;
2351 const base::string16& text_str = TextForIAccessibleText();
2352 HandleSpecialTextOffset(text_str, &offset);
2353 manager()->SetTextSelection(*this, offset, offset);
2354 return S_OK;
2357 STDMETHODIMP BrowserAccessibilityWin::setSelection(LONG selection_index,
2358 LONG start_offset,
2359 LONG end_offset) {
2360 if (!instance_active())
2361 return E_FAIL;
2363 if (selection_index != 0)
2364 return E_INVALIDARG;
2366 const base::string16& text_str = TextForIAccessibleText();
2367 HandleSpecialTextOffset(text_str, &start_offset);
2368 HandleSpecialTextOffset(text_str, &end_offset);
2370 manager()->SetTextSelection(*this, start_offset, end_offset);
2371 return S_OK;
2375 // IAccessibleText methods not implemented.
2378 STDMETHODIMP BrowserAccessibilityWin::get_attributes(LONG offset,
2379 LONG* start_offset,
2380 LONG* end_offset,
2381 BSTR* text_attributes) {
2382 return E_NOTIMPL;
2386 // IAccessibleHypertext methods.
2389 STDMETHODIMP BrowserAccessibilityWin::get_nHyperlinks(long* hyperlink_count) {
2390 if (!instance_active())
2391 return E_FAIL;
2393 if (!hyperlink_count)
2394 return E_INVALIDARG;
2396 *hyperlink_count = hyperlink_offset_to_index().size();
2397 return S_OK;
2400 STDMETHODIMP BrowserAccessibilityWin::get_hyperlink(
2401 long index,
2402 IAccessibleHyperlink** hyperlink) {
2403 if (!instance_active())
2404 return E_FAIL;
2406 if (!hyperlink ||
2407 index < 0 ||
2408 index >= static_cast<long>(hyperlinks().size())) {
2409 return E_INVALIDARG;
2412 int32 id = hyperlinks()[index];
2413 BrowserAccessibilityWin* child =
2414 manager()->GetFromID(id)->ToBrowserAccessibilityWin();
2415 if (child) {
2416 *hyperlink = static_cast<IAccessibleHyperlink*>(child->NewReference());
2417 return S_OK;
2420 return E_FAIL;
2423 STDMETHODIMP BrowserAccessibilityWin::get_hyperlinkIndex(
2424 long char_index,
2425 long* hyperlink_index) {
2426 if (!instance_active())
2427 return E_FAIL;
2429 if (!hyperlink_index)
2430 return E_INVALIDARG;
2432 *hyperlink_index = -1;
2434 if (char_index < 0 ||
2435 char_index >= static_cast<long>(hypertext().size())) {
2436 return E_INVALIDARG;
2439 std::map<int32, int32>::iterator it =
2440 hyperlink_offset_to_index().find(char_index);
2441 if (it == hyperlink_offset_to_index().end())
2442 return E_FAIL;
2444 *hyperlink_index = it->second;
2445 return S_OK;
2449 // IAccessibleHyperlink not implemented.
2452 STDMETHODIMP BrowserAccessibilityWin::get_anchor(long index, VARIANT* anchor) {
2453 return E_NOTIMPL;
2455 STDMETHODIMP
2456 BrowserAccessibilityWin::get_anchorTarget(long index, VARIANT* anchor_target) {
2457 return E_NOTIMPL;
2459 STDMETHODIMP BrowserAccessibilityWin::get_startIndex(long* index) {
2460 return E_NOTIMPL;
2462 STDMETHODIMP BrowserAccessibilityWin::get_endIndex(long* index) {
2463 return E_NOTIMPL;
2465 STDMETHODIMP BrowserAccessibilityWin::get_valid(boolean* valid) {
2466 return E_NOTIMPL;
2470 // IAccessibleAction not implemented.
2473 STDMETHODIMP BrowserAccessibilityWin::nActions(long* n_actions) {
2474 return E_NOTIMPL;
2476 STDMETHODIMP BrowserAccessibilityWin::doAction(long action_index) {
2477 return E_NOTIMPL;
2479 STDMETHODIMP
2480 BrowserAccessibilityWin::get_description(long action_index, BSTR* description) {
2481 return E_NOTIMPL;
2483 STDMETHODIMP BrowserAccessibilityWin::get_keyBinding(long action_index,
2484 long n_max_bindings,
2485 BSTR** key_bindings,
2486 long* n_bindings) {
2487 return E_NOTIMPL;
2489 STDMETHODIMP BrowserAccessibilityWin::get_name(long action_index, BSTR* name) {
2490 return E_NOTIMPL;
2492 STDMETHODIMP
2493 BrowserAccessibilityWin::get_localizedName(long action_index,
2494 BSTR* localized_name) {
2495 return E_NOTIMPL;
2499 // IAccessibleValue methods.
2502 STDMETHODIMP BrowserAccessibilityWin::get_currentValue(VARIANT* value) {
2503 if (!instance_active())
2504 return E_FAIL;
2506 if (!value)
2507 return E_INVALIDARG;
2509 float float_val;
2510 if (GetFloatAttribute(
2511 ui::AX_ATTR_VALUE_FOR_RANGE, &float_val)) {
2512 value->vt = VT_R8;
2513 value->dblVal = float_val;
2514 return S_OK;
2517 value->vt = VT_EMPTY;
2518 return S_FALSE;
2521 STDMETHODIMP BrowserAccessibilityWin::get_minimumValue(VARIANT* value) {
2522 if (!instance_active())
2523 return E_FAIL;
2525 if (!value)
2526 return E_INVALIDARG;
2528 float float_val;
2529 if (GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE,
2530 &float_val)) {
2531 value->vt = VT_R8;
2532 value->dblVal = float_val;
2533 return S_OK;
2536 value->vt = VT_EMPTY;
2537 return S_FALSE;
2540 STDMETHODIMP BrowserAccessibilityWin::get_maximumValue(VARIANT* value) {
2541 if (!instance_active())
2542 return E_FAIL;
2544 if (!value)
2545 return E_INVALIDARG;
2547 float float_val;
2548 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE,
2549 &float_val)) {
2550 value->vt = VT_R8;
2551 value->dblVal = float_val;
2552 return S_OK;
2555 value->vt = VT_EMPTY;
2556 return S_FALSE;
2559 STDMETHODIMP BrowserAccessibilityWin::setCurrentValue(VARIANT new_value) {
2560 // TODO(dmazzoni): Implement this.
2561 return E_NOTIMPL;
2565 // ISimpleDOMDocument methods.
2568 STDMETHODIMP BrowserAccessibilityWin::get_URL(BSTR* url) {
2569 if (!instance_active())
2570 return E_FAIL;
2572 if (!url)
2573 return E_INVALIDARG;
2575 return GetStringAttributeAsBstr(ui::AX_ATTR_DOC_URL, url);
2578 STDMETHODIMP BrowserAccessibilityWin::get_title(BSTR* title) {
2579 if (!instance_active())
2580 return E_FAIL;
2582 if (!title)
2583 return E_INVALIDARG;
2585 return GetStringAttributeAsBstr(ui::AX_ATTR_DOC_TITLE, title);
2588 STDMETHODIMP BrowserAccessibilityWin::get_mimeType(BSTR* mime_type) {
2589 if (!instance_active())
2590 return E_FAIL;
2592 if (!mime_type)
2593 return E_INVALIDARG;
2595 return GetStringAttributeAsBstr(
2596 ui::AX_ATTR_DOC_MIMETYPE, mime_type);
2599 STDMETHODIMP BrowserAccessibilityWin::get_docType(BSTR* doc_type) {
2600 if (!instance_active())
2601 return E_FAIL;
2603 if (!doc_type)
2604 return E_INVALIDARG;
2606 return GetStringAttributeAsBstr(
2607 ui::AX_ATTR_DOC_DOCTYPE, doc_type);
2610 STDMETHODIMP
2611 BrowserAccessibilityWin::get_nameSpaceURIForID(short name_space_id,
2612 BSTR* name_space_uri) {
2613 return E_NOTIMPL;
2615 STDMETHODIMP
2616 BrowserAccessibilityWin::put_alternateViewMediaTypes(
2617 BSTR* comma_separated_media_types) {
2618 return E_NOTIMPL;
2622 // ISimpleDOMNode methods.
2625 STDMETHODIMP BrowserAccessibilityWin::get_nodeInfo(
2626 BSTR* node_name,
2627 short* name_space_id,
2628 BSTR* node_value,
2629 unsigned int* num_children,
2630 unsigned int* unique_id,
2631 unsigned short* node_type) {
2632 if (!instance_active())
2633 return E_FAIL;
2635 if (!node_name || !name_space_id || !node_value || !num_children ||
2636 !unique_id || !node_type) {
2637 return E_INVALIDARG;
2640 base::string16 tag;
2641 if (GetString16Attribute(ui::AX_ATTR_HTML_TAG, &tag))
2642 *node_name = SysAllocString(tag.c_str());
2643 else
2644 *node_name = NULL;
2646 *name_space_id = 0;
2647 *node_value = SysAllocString(value().c_str());
2648 *num_children = PlatformChildCount();
2649 *unique_id = unique_id_win_;
2651 if (ia_role() == ROLE_SYSTEM_DOCUMENT) {
2652 *node_type = NODETYPE_DOCUMENT;
2653 } else if (ia_role() == ROLE_SYSTEM_TEXT &&
2654 ((ia2_state() & IA2_STATE_EDITABLE) == 0)) {
2655 *node_type = NODETYPE_TEXT;
2656 } else {
2657 *node_type = NODETYPE_ELEMENT;
2660 return S_OK;
2663 STDMETHODIMP BrowserAccessibilityWin::get_attributes(
2664 unsigned short max_attribs,
2665 BSTR* attrib_names,
2666 short* name_space_id,
2667 BSTR* attrib_values,
2668 unsigned short* num_attribs) {
2669 if (!instance_active())
2670 return E_FAIL;
2672 if (!attrib_names || !name_space_id || !attrib_values || !num_attribs)
2673 return E_INVALIDARG;
2675 *num_attribs = max_attribs;
2676 if (*num_attribs > GetHtmlAttributes().size())
2677 *num_attribs = GetHtmlAttributes().size();
2679 for (unsigned short i = 0; i < *num_attribs; ++i) {
2680 attrib_names[i] = SysAllocString(
2681 base::UTF8ToUTF16(GetHtmlAttributes()[i].first).c_str());
2682 name_space_id[i] = 0;
2683 attrib_values[i] = SysAllocString(
2684 base::UTF8ToUTF16(GetHtmlAttributes()[i].second).c_str());
2686 return S_OK;
2689 STDMETHODIMP BrowserAccessibilityWin::get_attributesForNames(
2690 unsigned short num_attribs,
2691 BSTR* attrib_names,
2692 short* name_space_id,
2693 BSTR* attrib_values) {
2694 if (!instance_active())
2695 return E_FAIL;
2697 if (!attrib_names || !name_space_id || !attrib_values)
2698 return E_INVALIDARG;
2700 for (unsigned short i = 0; i < num_attribs; ++i) {
2701 name_space_id[i] = 0;
2702 bool found = false;
2703 std::string name = base::UTF16ToUTF8((LPCWSTR)attrib_names[i]);
2704 for (unsigned int j = 0; j < GetHtmlAttributes().size(); ++j) {
2705 if (GetHtmlAttributes()[j].first == name) {
2706 attrib_values[i] = SysAllocString(
2707 base::UTF8ToUTF16(GetHtmlAttributes()[j].second).c_str());
2708 found = true;
2709 break;
2712 if (!found) {
2713 attrib_values[i] = NULL;
2716 return S_OK;
2719 STDMETHODIMP BrowserAccessibilityWin::get_computedStyle(
2720 unsigned short max_style_properties,
2721 boolean use_alternate_view,
2722 BSTR* style_properties,
2723 BSTR* style_values,
2724 unsigned short *num_style_properties) {
2725 if (!instance_active())
2726 return E_FAIL;
2728 if (!style_properties || !style_values)
2729 return E_INVALIDARG;
2731 // We only cache a single style property for now: DISPLAY
2733 base::string16 display;
2734 if (max_style_properties == 0 ||
2735 !GetString16Attribute(ui::AX_ATTR_DISPLAY, &display)) {
2736 *num_style_properties = 0;
2737 return S_OK;
2740 *num_style_properties = 1;
2741 style_properties[0] = SysAllocString(L"display");
2742 style_values[0] = SysAllocString(display.c_str());
2744 return S_OK;
2747 STDMETHODIMP BrowserAccessibilityWin::get_computedStyleForProperties(
2748 unsigned short num_style_properties,
2749 boolean use_alternate_view,
2750 BSTR* style_properties,
2751 BSTR* style_values) {
2752 if (!instance_active())
2753 return E_FAIL;
2755 if (!style_properties || !style_values)
2756 return E_INVALIDARG;
2758 // We only cache a single style property for now: DISPLAY
2760 for (unsigned short i = 0; i < num_style_properties; ++i) {
2761 base::string16 name = base::ToLowerASCII(
2762 reinterpret_cast<const base::char16*>(style_properties[i]));
2763 if (name == L"display") {
2764 base::string16 display = GetString16Attribute(
2765 ui::AX_ATTR_DISPLAY);
2766 style_values[i] = SysAllocString(display.c_str());
2767 } else {
2768 style_values[i] = NULL;
2772 return S_OK;
2775 STDMETHODIMP BrowserAccessibilityWin::scrollTo(boolean placeTopLeft) {
2776 return scrollTo(placeTopLeft ?
2777 IA2_SCROLL_TYPE_TOP_LEFT : IA2_SCROLL_TYPE_ANYWHERE);
2780 STDMETHODIMP BrowserAccessibilityWin::get_parentNode(ISimpleDOMNode** node) {
2781 if (!instance_active())
2782 return E_FAIL;
2784 if (!node)
2785 return E_INVALIDARG;
2787 *node = GetParent()->ToBrowserAccessibilityWin()->NewReference();
2788 return S_OK;
2791 STDMETHODIMP BrowserAccessibilityWin::get_firstChild(ISimpleDOMNode** node) {
2792 if (!instance_active())
2793 return E_FAIL;
2795 if (!node)
2796 return E_INVALIDARG;
2798 if (PlatformChildCount() == 0) {
2799 *node = NULL;
2800 return S_FALSE;
2803 *node = PlatformGetChild(0)->ToBrowserAccessibilityWin()->NewReference();
2804 return S_OK;
2807 STDMETHODIMP BrowserAccessibilityWin::get_lastChild(ISimpleDOMNode** node) {
2808 if (!instance_active())
2809 return E_FAIL;
2811 if (!node)
2812 return E_INVALIDARG;
2814 if (PlatformChildCount() == 0) {
2815 *node = NULL;
2816 return S_FALSE;
2819 *node = PlatformGetChild(PlatformChildCount() - 1)
2820 ->ToBrowserAccessibilityWin()->NewReference();
2821 return S_OK;
2824 STDMETHODIMP BrowserAccessibilityWin::get_previousSibling(
2825 ISimpleDOMNode** node) {
2826 if (!instance_active())
2827 return E_FAIL;
2829 if (!node)
2830 return E_INVALIDARG;
2832 if (!GetParent() || GetIndexInParent() <= 0) {
2833 *node = NULL;
2834 return S_FALSE;
2837 *node = GetParent()->InternalGetChild(GetIndexInParent() - 1)->
2838 ToBrowserAccessibilityWin()->NewReference();
2839 return S_OK;
2842 STDMETHODIMP BrowserAccessibilityWin::get_nextSibling(ISimpleDOMNode** node) {
2843 if (!instance_active())
2844 return E_FAIL;
2846 if (!node)
2847 return E_INVALIDARG;
2849 if (!GetParent() ||
2850 GetIndexInParent() < 0 ||
2851 GetIndexInParent() >= static_cast<int>(
2852 GetParent()->InternalChildCount()) - 1) {
2853 *node = NULL;
2854 return S_FALSE;
2857 *node = GetParent()->InternalGetChild(GetIndexInParent() + 1)->
2858 ToBrowserAccessibilityWin()->NewReference();
2859 return S_OK;
2862 STDMETHODIMP BrowserAccessibilityWin::get_childAt(
2863 unsigned int child_index,
2864 ISimpleDOMNode** node) {
2865 if (!instance_active())
2866 return E_FAIL;
2868 if (!node)
2869 return E_INVALIDARG;
2871 if (child_index >= PlatformChildCount())
2872 return E_INVALIDARG;
2874 BrowserAccessibility* child = PlatformGetChild(child_index);
2875 if (!child) {
2876 *node = NULL;
2877 return S_FALSE;
2880 *node = child->ToBrowserAccessibilityWin()->NewReference();
2881 return S_OK;
2884 STDMETHODIMP BrowserAccessibilityWin::get_innerHTML(BSTR* innerHTML) {
2885 return E_NOTIMPL;
2888 STDMETHODIMP
2889 BrowserAccessibilityWin::get_localInterface(void** local_interface) {
2890 return E_NOTIMPL;
2893 STDMETHODIMP BrowserAccessibilityWin::get_language(BSTR* language) {
2894 return E_NOTIMPL;
2898 // ISimpleDOMText methods.
2901 STDMETHODIMP BrowserAccessibilityWin::get_domText(BSTR* dom_text) {
2902 if (!instance_active())
2903 return E_FAIL;
2905 if (!dom_text)
2906 return E_INVALIDARG;
2908 return GetStringAttributeAsBstr(
2909 ui::AX_ATTR_NAME, dom_text);
2912 STDMETHODIMP BrowserAccessibilityWin::get_clippedSubstringBounds(
2913 unsigned int start_index,
2914 unsigned int end_index,
2915 int* out_x,
2916 int* out_y,
2917 int* out_width,
2918 int* out_height) {
2919 // TODO(dmazzoni): fully support this API by intersecting the
2920 // rect with the container's rect.
2921 return get_unclippedSubstringBounds(
2922 start_index, end_index, out_x, out_y, out_width, out_height);
2925 STDMETHODIMP BrowserAccessibilityWin::get_unclippedSubstringBounds(
2926 unsigned int start_index,
2927 unsigned int end_index,
2928 int* out_x,
2929 int* out_y,
2930 int* out_width,
2931 int* out_height) {
2932 if (!instance_active())
2933 return E_FAIL;
2935 if (!out_x || !out_y || !out_width || !out_height)
2936 return E_INVALIDARG;
2938 const base::string16& text_str = TextForIAccessibleText();
2939 if (start_index > text_str.size() ||
2940 end_index > text_str.size() ||
2941 start_index > end_index) {
2942 return E_INVALIDARG;
2945 gfx::Rect bounds = GetGlobalBoundsForRange(
2946 start_index, end_index - start_index);
2947 *out_x = bounds.x();
2948 *out_y = bounds.y();
2949 *out_width = bounds.width();
2950 *out_height = bounds.height();
2951 return S_OK;
2954 STDMETHODIMP BrowserAccessibilityWin::scrollToSubstring(
2955 unsigned int start_index,
2956 unsigned int end_index) {
2957 if (!instance_active())
2958 return E_FAIL;
2960 const base::string16& text_str = TextForIAccessibleText();
2961 if (start_index > text_str.size() ||
2962 end_index > text_str.size() ||
2963 start_index > end_index) {
2964 return E_INVALIDARG;
2967 manager()->ScrollToMakeVisible(*this, GetLocalBoundsForRange(
2968 start_index, end_index - start_index));
2969 manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
2971 return S_OK;
2974 STDMETHODIMP BrowserAccessibilityWin::get_fontFamily(BSTR* font_family) {
2975 return E_NOTIMPL;
2979 // IServiceProvider methods.
2982 STDMETHODIMP BrowserAccessibilityWin::QueryService(REFGUID guidService,
2983 REFIID riid,
2984 void** object) {
2985 if (!instance_active())
2986 return E_FAIL;
2988 // The system uses IAccessible APIs for many purposes, but only
2989 // assistive technology like screen readers uses IAccessible2.
2990 // Enable full accessibility support when IAccessible2 APIs are queried.
2991 if (riid == IID_IAccessible2)
2992 BrowserAccessibilityStateImpl::GetInstance()->EnableAccessibility();
2994 if (guidService == GUID_IAccessibleContentDocument) {
2995 // Special Mozilla extension: return the accessible for the root document.
2996 // Screen readers use this to distinguish between a document loaded event
2997 // on the root document vs on an iframe.
2998 return manager()->GetRoot()->ToBrowserAccessibilityWin()->QueryInterface(
2999 IID_IAccessible2, object);
3002 if (guidService == IID_IAccessible ||
3003 guidService == IID_IAccessible2 ||
3004 guidService == IID_IAccessibleAction ||
3005 guidService == IID_IAccessibleApplication ||
3006 guidService == IID_IAccessibleHyperlink ||
3007 guidService == IID_IAccessibleHypertext ||
3008 guidService == IID_IAccessibleImage ||
3009 guidService == IID_IAccessibleTable ||
3010 guidService == IID_IAccessibleTable2 ||
3011 guidService == IID_IAccessibleTableCell ||
3012 guidService == IID_IAccessibleText ||
3013 guidService == IID_IAccessibleValue ||
3014 guidService == IID_ISimpleDOMDocument ||
3015 guidService == IID_ISimpleDOMNode ||
3016 guidService == IID_ISimpleDOMText ||
3017 guidService == GUID_ISimpleDOM) {
3018 return QueryInterface(riid, object);
3021 // We only support the IAccessibleEx interface on Windows 8 and above. This
3022 // is needed for the on-screen Keyboard to show up in metro mode, when the
3023 // user taps an editable portion on the page.
3024 // All methods in the IAccessibleEx interface are unimplemented.
3025 if (riid == IID_IAccessibleEx &&
3026 base::win::GetVersion() >= base::win::VERSION_WIN8) {
3027 return QueryInterface(riid, object);
3030 *object = NULL;
3031 return E_FAIL;
3034 STDMETHODIMP
3035 BrowserAccessibilityWin::GetObjectForChild(long child_id, IAccessibleEx** ret) {
3036 return E_NOTIMPL;
3039 STDMETHODIMP
3040 BrowserAccessibilityWin::GetIAccessiblePair(IAccessible** acc, long* child_id) {
3041 return E_NOTIMPL;
3044 STDMETHODIMP BrowserAccessibilityWin::GetRuntimeId(SAFEARRAY** runtime_id) {
3045 return E_NOTIMPL;
3048 STDMETHODIMP
3049 BrowserAccessibilityWin::ConvertReturnedElement(
3050 IRawElementProviderSimple* element,
3051 IAccessibleEx** acc) {
3052 return E_NOTIMPL;
3055 STDMETHODIMP BrowserAccessibilityWin::GetPatternProvider(PATTERNID id,
3056 IUnknown** provider) {
3057 DVLOG(1) << "In Function: "
3058 << __FUNCTION__
3059 << " for pattern id: "
3060 << id;
3061 if (id == UIA_ValuePatternId || id == UIA_TextPatternId) {
3062 if (IsEditableText()) {
3063 DVLOG(1) << "Returning UIA text provider";
3064 base::win::UIATextProvider::CreateTextProvider(
3065 GetValueText(), true, provider);
3066 return S_OK;
3069 return E_NOTIMPL;
3072 STDMETHODIMP BrowserAccessibilityWin::GetPropertyValue(PROPERTYID id,
3073 VARIANT* ret) {
3074 DVLOG(1) << "In Function: "
3075 << __FUNCTION__
3076 << " for property id: "
3077 << id;
3078 V_VT(ret) = VT_EMPTY;
3079 if (id == UIA_ControlTypePropertyId) {
3080 if (IsEditableText()) {
3081 V_VT(ret) = VT_I4;
3082 ret->lVal = UIA_EditControlTypeId;
3083 DVLOG(1) << "Returning Edit control type";
3084 } else {
3085 DVLOG(1) << "Returning empty control type";
3088 return S_OK;
3091 STDMETHODIMP BrowserAccessibilityWin::get_ProviderOptions(
3092 enum ProviderOptions* ret) {
3093 return E_NOTIMPL;
3096 STDMETHODIMP BrowserAccessibilityWin::get_HostRawElementProvider(
3097 IRawElementProviderSimple** provider) {
3098 return E_NOTIMPL;
3102 // CComObjectRootEx methods.
3105 // static
3106 HRESULT WINAPI BrowserAccessibilityWin::InternalQueryInterface(
3107 void* this_ptr,
3108 const _ATL_INTMAP_ENTRY* entries,
3109 REFIID iid,
3110 void** object) {
3111 BrowserAccessibilityWin* accessibility =
3112 reinterpret_cast<BrowserAccessibilityWin*>(this_ptr);
3113 int32 ia_role = accessibility->ia_role();
3114 if (iid == IID_IAccessibleImage) {
3115 if (ia_role != ROLE_SYSTEM_GRAPHIC) {
3116 *object = NULL;
3117 return E_NOINTERFACE;
3119 } else if (iid == IID_IAccessibleTable || iid == IID_IAccessibleTable2) {
3120 if (ia_role != ROLE_SYSTEM_TABLE) {
3121 *object = NULL;
3122 return E_NOINTERFACE;
3124 } else if (iid == IID_IAccessibleTableCell) {
3125 if (!accessibility->IsCellOrTableHeaderRole()) {
3126 *object = NULL;
3127 return E_NOINTERFACE;
3129 } else if (iid == IID_IAccessibleValue) {
3130 if (ia_role != ROLE_SYSTEM_PROGRESSBAR &&
3131 ia_role != ROLE_SYSTEM_SCROLLBAR &&
3132 ia_role != ROLE_SYSTEM_SLIDER) {
3133 *object = NULL;
3134 return E_NOINTERFACE;
3136 } else if (iid == IID_ISimpleDOMDocument) {
3137 if (ia_role != ROLE_SYSTEM_DOCUMENT) {
3138 *object = NULL;
3139 return E_NOINTERFACE;
3143 return CComObjectRootBase::InternalQueryInterface(
3144 this_ptr, entries, iid, object);
3148 // Private methods.
3151 void BrowserAccessibilityWin::UpdateStep1ComputeWinAttributes() {
3152 // Swap win_attributes_ to old_win_attributes_, allowing us to see
3153 // exactly what changed and fire appropriate events. Note that
3154 // old_win_attributes_ is cleared at the end of UpdateStep3FireEvents.
3155 old_win_attributes_.swap(win_attributes_);
3156 win_attributes_.reset(new WinAttributes());
3158 InitRoleAndState();
3160 win_attributes_->ia2_attributes.clear();
3162 // Expose autocomplete attribute for combobox and textbox.
3163 StringAttributeToIA2(ui::AX_ATTR_AUTO_COMPLETE, "autocomplete");
3165 // Expose the "display" and "tag" attributes.
3166 StringAttributeToIA2(ui::AX_ATTR_DISPLAY, "display");
3167 StringAttributeToIA2(ui::AX_ATTR_HTML_TAG, "tag");
3168 StringAttributeToIA2(ui::AX_ATTR_ROLE, "xml-roles");
3170 // Expose "level" attribute for headings, trees, etc.
3171 IntAttributeToIA2(ui::AX_ATTR_HIERARCHICAL_LEVEL, "level");
3173 // Expose the set size and position in set.
3174 IntAttributeToIA2(ui::AX_ATTR_SET_SIZE, "setsize");
3175 IntAttributeToIA2(ui::AX_ATTR_POS_IN_SET, "posinset");
3177 if (ia_role() == ROLE_SYSTEM_CHECKBUTTON ||
3178 ia_role() == ROLE_SYSTEM_RADIOBUTTON ||
3179 ia2_role() == IA2_ROLE_CHECK_MENU_ITEM ||
3180 ia2_role() == IA2_ROLE_RADIO_MENU_ITEM ||
3181 ia2_role() == IA2_ROLE_TOGGLE_BUTTON) {
3182 win_attributes_->ia2_attributes.push_back(L"checkable:true");
3185 // Expose live region attributes.
3186 StringAttributeToIA2(ui::AX_ATTR_LIVE_STATUS, "live");
3187 StringAttributeToIA2(ui::AX_ATTR_LIVE_RELEVANT, "relevant");
3188 BoolAttributeToIA2(ui::AX_ATTR_LIVE_ATOMIC, "atomic");
3189 BoolAttributeToIA2(ui::AX_ATTR_LIVE_BUSY, "busy");
3191 // Expose container live region attributes.
3192 StringAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_STATUS,
3193 "container-live");
3194 StringAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_RELEVANT,
3195 "container-relevant");
3196 BoolAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_ATOMIC,
3197 "container-atomic");
3198 BoolAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_BUSY,
3199 "container-busy");
3201 // Expose table cell index.
3202 if (IsCellOrTableHeaderRole()) {
3203 BrowserAccessibility* table = GetParent();
3204 while (table && table->GetRole() != ui::AX_ROLE_TABLE)
3205 table = table->GetParent();
3206 if (table) {
3207 const std::vector<int32>& unique_cell_ids = table->GetIntListAttribute(
3208 ui::AX_ATTR_UNIQUE_CELL_IDS);
3209 for (size_t i = 0; i < unique_cell_ids.size(); ++i) {
3210 if (unique_cell_ids[i] == GetId()) {
3211 win_attributes_->ia2_attributes.push_back(
3212 base::string16(L"table-cell-index:") + base::IntToString16(i));
3218 // Expose invalid state for form controls and elements with aria-invalid.
3219 int invalid_state;
3220 if (GetIntAttribute(ui::AX_ATTR_INVALID_STATE, &invalid_state)) {
3221 // TODO(nektar): Handle the possibility of having multiple aria-invalid
3222 // attributes defined, e.g., "invalid:spelling,grammar".
3223 switch (invalid_state) {
3224 case ui::AX_INVALID_STATE_FALSE:
3225 win_attributes_->ia2_attributes.push_back(L"invalid:false");
3226 break;
3227 case ui::AX_INVALID_STATE_TRUE:
3228 win_attributes_->ia2_attributes.push_back(L"invalid:true");
3229 break;
3230 case ui::AX_INVALID_STATE_SPELLING:
3231 win_attributes_->ia2_attributes.push_back(L"invalid:spelling");
3232 break;
3233 case ui::AX_INVALID_STATE_GRAMMAR:
3234 win_attributes_->ia2_attributes.push_back(L"invalid:grammar");
3235 break;
3236 case ui::AX_INVALID_STATE_OTHER:
3238 base::string16 aria_invalid_value;
3239 if (GetString16Attribute(ui::AX_ATTR_ARIA_INVALID_VALUE,
3240 &aria_invalid_value)) {
3241 win_attributes_->ia2_attributes.push_back(
3242 L"invalid:" + aria_invalid_value);
3243 } else {
3244 // Set the attribute to L"true", since we cannot be more specific.
3245 win_attributes_->ia2_attributes.push_back(L"invalid:true");
3248 break;
3249 default:
3250 NOTREACHED();
3254 // Expose row or column header sort direction.
3255 int32 sort_direction;
3256 if ((ia_role() == ROLE_SYSTEM_COLUMNHEADER ||
3257 ia_role() == ROLE_SYSTEM_ROWHEADER) &&
3258 GetIntAttribute(ui::AX_ATTR_SORT_DIRECTION, &sort_direction)) {
3259 switch (sort_direction) {
3260 case ui::AX_SORT_DIRECTION_UNSORTED:
3261 win_attributes_->ia2_attributes.push_back(L"sort:none");
3262 break;
3263 case ui::AX_SORT_DIRECTION_ASCENDING:
3264 win_attributes_->ia2_attributes.push_back(L"sort:ascending");
3265 break;
3266 case ui::AX_SORT_DIRECTION_DESCENDING:
3267 win_attributes_->ia2_attributes.push_back(L"sort:descending");
3268 break;
3269 case ui::AX_SORT_DIRECTION_OTHER:
3270 win_attributes_->ia2_attributes.push_back(L"sort:other");
3271 break;
3272 default:
3273 NOTREACHED();
3277 // The calculation of the accessible name of an element has been
3278 // standardized in the HTML to Platform Accessibility APIs Implementation
3279 // Guide (http://www.w3.org/TR/html-aapi/). In order to return the
3280 // appropriate accessible name on Windows, we need to apply some logic
3281 // to the fields we get from WebKit.
3283 // TODO(dmazzoni): move most of this logic into WebKit.
3285 // WebKit gives us:
3287 // name: the default name, e.g. inner text
3288 // title ui element: a reference to a <label> element on the same
3289 // page that labels this node.
3290 // description: accessible labels that override the default name:
3291 // aria-label or aria-labelledby or aria-describedby
3292 // help: the value of the "title" attribute
3294 // On Windows, the logic we apply lets some fields take precedence and
3295 // always returns the primary name in "name" and the secondary name,
3296 // if any, in "description".
3298 int title_elem_id = GetIntAttribute(ui::AX_ATTR_TITLE_UI_ELEMENT);
3299 base::string16 name = GetString16Attribute(ui::AX_ATTR_NAME);
3300 base::string16 description = GetString16Attribute(ui::AX_ATTR_DESCRIPTION);
3301 base::string16 help = GetString16Attribute(ui::AX_ATTR_HELP);
3302 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
3304 // WebKit annoyingly puts the title in the description if there's no other
3305 // description, which just confuses the rest of the logic. Put it back.
3306 // Now "help" is always the value of the "title" attribute, if present.
3307 base::string16 title_attr;
3308 if (GetHtmlAttribute("title", &title_attr) &&
3309 description == title_attr &&
3310 help.empty()) {
3311 help = description;
3312 description.clear();
3315 // Now implement the main logic: the descripion should become the name if
3316 // it's nonempty, and the help should become the description if
3317 // there's no description - or the name if there's no name or description.
3318 if (!description.empty()) {
3319 name = description;
3320 description.clear();
3322 if (!help.empty() && description.empty()) {
3323 description = help;
3324 help.clear();
3326 if (!description.empty() && name.empty() && !title_elem_id) {
3327 name = description;
3328 description.clear();
3331 // If it's a text field, also consider the placeholder.
3332 base::string16 placeholder;
3333 if (GetRole() == ui::AX_ROLE_TEXT_FIELD &&
3334 HasState(ui::AX_STATE_FOCUSABLE) &&
3335 GetHtmlAttribute("placeholder", &placeholder)) {
3336 if (name.empty() && !title_elem_id) {
3337 name = placeholder;
3338 } else if (description.empty()) {
3339 description = placeholder;
3343 // On Windows, the value of a document should be its url.
3344 if (GetRole() == ui::AX_ROLE_ROOT_WEB_AREA ||
3345 GetRole() == ui::AX_ROLE_WEB_AREA) {
3346 value = GetString16Attribute(ui::AX_ATTR_DOC_URL);
3349 // For certain roles (listbox option, static text, and list marker)
3350 // WebKit stores the main accessible text in the "value" - swap it so
3351 // that it's the "name".
3352 if (name.empty() &&
3353 (GetRole() == ui::AX_ROLE_STATIC_TEXT ||
3354 GetRole() == ui::AX_ROLE_LIST_MARKER ||
3355 IsListBoxOptionOrMenuListOption())) {
3356 base::string16 tmp = value;
3357 value = name;
3358 name = tmp;
3361 // If this doesn't have a value and is linked then set its value to the url
3362 // attribute. This allows screen readers to read an empty link's destination.
3363 if (value.empty() && (ia_state() & STATE_SYSTEM_LINKED))
3364 value = GetString16Attribute(ui::AX_ATTR_URL);
3366 win_attributes_->name = name;
3367 win_attributes_->description = description;
3368 win_attributes_->help = help;
3369 win_attributes_->value = value;
3371 // Clear any old relationships between this node and other nodes.
3372 for (size_t i = 0; i < relations_.size(); ++i)
3373 relations_[i]->Release();
3374 relations_.clear();
3376 // Handle title UI element.
3377 if (title_elem_id) {
3378 // Add a labelled by relationship.
3379 CComObject<BrowserAccessibilityRelation>* relation;
3380 HRESULT hr = CComObject<BrowserAccessibilityRelation>::CreateInstance(
3381 &relation);
3382 DCHECK(SUCCEEDED(hr));
3383 relation->AddRef();
3384 relation->Initialize(this, IA2_RELATION_LABELLED_BY);
3385 relation->AddTarget(title_elem_id);
3386 relations_.push_back(relation);
3389 // Expose slider value.
3390 if (ia_role() == ROLE_SYSTEM_PROGRESSBAR ||
3391 ia_role() == ROLE_SYSTEM_SCROLLBAR ||
3392 ia_role() == ROLE_SYSTEM_SLIDER) {
3393 win_attributes_->ia2_attributes.push_back(L"valuetext:" + GetValueText());
3396 // Expose dropeffect attribute.
3397 base::string16 dropEffect;
3398 if (GetHtmlAttribute("aria-dropeffect", &dropEffect))
3399 win_attributes_->ia2_attributes.push_back(L"dropeffect:" + dropEffect);
3401 // Expose grabbed attribute.
3402 base::string16 grabbed;
3403 if (GetHtmlAttribute("aria-grabbed", &grabbed))
3404 win_attributes_->ia2_attributes.push_back(L"grabbed:" + grabbed);
3406 // Expose datetime attribute.
3407 base::string16 datetime;
3408 if (GetRole() == ui::AX_ROLE_TIME &&
3409 GetHtmlAttribute("datetime", &datetime))
3410 win_attributes_->ia2_attributes.push_back(L"datetime:" + datetime);
3412 // Expose input-text type attribute.
3413 base::string16 type;
3414 if (GetRole() == ui::AX_ROLE_TEXT_FIELD &&
3415 GetHtmlAttribute("type", &type))
3416 win_attributes_->ia2_attributes.push_back(L"text-input-type:" + type);
3418 // If this is a web area for a presentational iframe, give it a role of
3419 // something other than DOCUMENT so that the fact that it's a separate doc
3420 // is not exposed to AT.
3421 if (IsWebAreaForPresentationalIframe()) {
3422 win_attributes_->ia_role = ROLE_SYSTEM_GROUPING;
3423 win_attributes_->ia2_role = ROLE_SYSTEM_GROUPING;
3427 void BrowserAccessibilityWin::UpdateStep2ComputeHypertext() {
3428 if (!PlatformChildCount()) {
3429 win_attributes_->hypertext += name();
3430 return;
3433 // Construct the hypertext for this node, which contains the concatenation
3434 // of all of the static text and widespace of this node's children and an
3435 // embedded object character for all the other children. Build up a map from
3436 // the character index of each embedded object character to the id of the
3437 // child object it points to.
3438 for (unsigned int i = 0; i < PlatformChildCount(); ++i) {
3439 BrowserAccessibilityWin* child =
3440 PlatformGetChild(i)->ToBrowserAccessibilityWin();
3441 DCHECK(child);
3442 if (child->IsTextOnlyObject()) {
3443 win_attributes_->hypertext += child->name();
3444 } else {
3445 int32 char_offset = hypertext().size();
3446 int32 child_id = child->GetId();
3447 int32 index = hyperlinks().size();
3448 win_attributes_->hyperlink_offset_to_index[char_offset] = index;
3449 win_attributes_->hyperlinks.push_back(child_id);
3450 win_attributes_->hypertext += kEmbeddedCharacter;
3455 void BrowserAccessibilityWin::UpdateStep3FireEvents(bool is_subtree_creation) {
3456 BrowserAccessibilityManagerWin* manager =
3457 this->manager()->ToBrowserAccessibilityManagerWin();
3459 // Fire an event when an alert first appears.
3460 if (ia_role() == ROLE_SYSTEM_ALERT &&
3461 old_win_attributes_->ia_role != ROLE_SYSTEM_ALERT) {
3462 manager->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
3465 // Fire an event when a new subtree is created.
3466 if (is_subtree_creation)
3467 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SHOW, this);
3469 // The rest of the events only fire on changes, not on new objects.
3470 if (old_win_attributes_->ia_role != 0 ||
3471 !old_win_attributes_->role_name.empty()) {
3472 // Fire an event if the name, description, help, or value changes.
3473 if (name() != old_win_attributes_->name)
3474 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_NAMECHANGE, this);
3475 if (description() != old_win_attributes_->description)
3476 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_DESCRIPTIONCHANGE, this);
3477 if (help() != old_win_attributes_->help)
3478 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_HELPCHANGE, this);
3479 if (value() != old_win_attributes_->value)
3480 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_VALUECHANGE, this);
3481 if (ia_state() != old_win_attributes_->ia_state)
3482 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_STATECHANGE, this);
3484 // Normally focus events are handled elsewhere, however
3485 // focus for managed descendants is platform-specific.
3486 // Fire a focus event if the focused descendant in a multi-select
3487 // list box changes.
3488 if (GetRole() == ui::AX_ROLE_LIST_BOX_OPTION &&
3489 (ia_state() & STATE_SYSTEM_FOCUSABLE) &&
3490 (ia_state() & STATE_SYSTEM_SELECTABLE) &&
3491 (ia_state() & STATE_SYSTEM_FOCUSED) &&
3492 !(old_win_attributes_->ia_state & STATE_SYSTEM_FOCUSED)) {
3493 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_FOCUS, this);
3496 // Handle selection being added or removed.
3497 bool is_selected_now = (ia_state() & STATE_SYSTEM_SELECTED) != 0;
3498 bool was_selected_before =
3499 (old_win_attributes_->ia_state & STATE_SYSTEM_SELECTED) != 0;
3500 if (is_selected_now || was_selected_before) {
3501 bool multiselect = false;
3502 if (GetParent() && GetParent()->HasState(ui::AX_STATE_MULTISELECTABLE))
3503 multiselect = true;
3505 if (multiselect) {
3506 // In a multi-select box, fire SELECTIONADD and SELECTIONREMOVE events.
3507 if (is_selected_now && !was_selected_before) {
3508 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTIONADD, this);
3509 } else if (!is_selected_now && was_selected_before) {
3510 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTIONREMOVE, this);
3512 } else if (is_selected_now && !was_selected_before) {
3513 // In a single-select box, only fire SELECTION events.
3514 manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTION, this);
3518 // Fire an event if this container object has scrolled.
3519 int sx = 0;
3520 int sy = 0;
3521 if (GetIntAttribute(ui::AX_ATTR_SCROLL_X, &sx) &&
3522 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &sy)) {
3523 if (sx != previous_scroll_x_ || sy != previous_scroll_y_)
3524 manager->MaybeCallNotifyWinEvent(EVENT_SYSTEM_SCROLLINGEND, this);
3525 previous_scroll_x_ = sx;
3526 previous_scroll_y_ = sy;
3529 // Fire hypertext-related events.
3530 int start, old_len, new_len;
3531 ComputeHypertextRemovedAndInserted(&start, &old_len, &new_len);
3532 if (old_len > 0) {
3533 // In-process screen readers may call IAccessibleText::get_oldText
3534 // in reaction to this event to retrieve the text that was removed.
3535 manager->MaybeCallNotifyWinEvent(IA2_EVENT_TEXT_REMOVED, this);
3537 if (new_len > 0) {
3538 // In-process screen readers may call IAccessibleText::get_newText
3539 // in reaction to this event to retrieve the text that was inserted.
3540 manager->MaybeCallNotifyWinEvent(IA2_EVENT_TEXT_INSERTED, this);
3543 // Changing a static text node can affect the IAccessibleText hypertext
3544 // of the parent node, so force an update on the parent.
3545 BrowserAccessibilityWin* parent = GetParent()->ToBrowserAccessibilityWin();
3546 if (parent && IsTextOnlyObject() &&
3547 name() != old_win_attributes_->name) {
3548 parent->UpdateStep1ComputeWinAttributes();
3549 parent->UpdateStep2ComputeHypertext();
3550 parent->UpdateStep3FireEvents(false);
3554 old_win_attributes_.reset(nullptr);
3557 void BrowserAccessibilityWin::OnSubtreeWillBeDeleted() {
3558 manager()->ToBrowserAccessibilityManagerWin()->MaybeCallNotifyWinEvent(
3559 EVENT_OBJECT_HIDE, this);
3562 void BrowserAccessibilityWin::NativeAddReference() {
3563 AddRef();
3566 void BrowserAccessibilityWin::NativeReleaseReference() {
3567 Release();
3570 bool BrowserAccessibilityWin::IsNative() const {
3571 return true;
3574 void BrowserAccessibilityWin::OnLocationChanged() {
3575 manager()->ToBrowserAccessibilityManagerWin()->MaybeCallNotifyWinEvent(
3576 EVENT_OBJECT_LOCATIONCHANGE, this);
3579 BrowserAccessibilityWin* BrowserAccessibilityWin::NewReference() {
3580 AddRef();
3581 return this;
3584 BrowserAccessibilityWin* BrowserAccessibilityWin::GetTargetFromChildID(
3585 const VARIANT& var_id) {
3586 if (var_id.vt != VT_I4)
3587 return NULL;
3589 LONG child_id = var_id.lVal;
3590 if (child_id == CHILDID_SELF)
3591 return this;
3593 if (child_id >= 1 && child_id <= static_cast<LONG>(PlatformChildCount()))
3594 return PlatformGetChild(child_id - 1)->ToBrowserAccessibilityWin();
3596 return manager()->ToBrowserAccessibilityManagerWin()->
3597 GetFromUniqueIdWin(child_id);
3600 HRESULT BrowserAccessibilityWin::GetStringAttributeAsBstr(
3601 ui::AXStringAttribute attribute,
3602 BSTR* value_bstr) {
3603 base::string16 str;
3605 if (!GetString16Attribute(attribute, &str))
3606 return S_FALSE;
3608 if (str.empty())
3609 return S_FALSE;
3611 *value_bstr = SysAllocString(str.c_str());
3612 DCHECK(*value_bstr);
3614 return S_OK;
3617 void BrowserAccessibilityWin::StringAttributeToIA2(
3618 ui::AXStringAttribute attribute,
3619 const char* ia2_attr) {
3620 base::string16 value;
3621 if (GetString16Attribute(attribute, &value)) {
3622 win_attributes_->ia2_attributes.push_back(
3623 base::ASCIIToUTF16(ia2_attr) + L":" + value);
3627 void BrowserAccessibilityWin::BoolAttributeToIA2(
3628 ui::AXBoolAttribute attribute,
3629 const char* ia2_attr) {
3630 bool value;
3631 if (GetBoolAttribute(attribute, &value)) {
3632 win_attributes_->ia2_attributes.push_back(
3633 (base::ASCIIToUTF16(ia2_attr) + L":") +
3634 (value ? L"true" : L"false"));
3638 void BrowserAccessibilityWin::IntAttributeToIA2(
3639 ui::AXIntAttribute attribute,
3640 const char* ia2_attr) {
3641 int value;
3642 if (GetIntAttribute(attribute, &value)) {
3643 win_attributes_->ia2_attributes.push_back(
3644 base::ASCIIToUTF16(ia2_attr) + L":" +
3645 base::IntToString16(value));
3649 int32 BrowserAccessibilityWin::GetHyperlinkIndexFromChild(
3650 const BrowserAccessibilityWin& child) const {
3651 auto iterator = std::find(
3652 hyperlinks().begin(), hyperlinks().end(), child.GetId());
3653 if (iterator == hyperlinks().end())
3654 return -1;
3656 return static_cast<int32>(iterator - hyperlinks().begin());
3659 int32 BrowserAccessibilityWin::GetHypertextOffsetFromHyperlinkIndex(
3660 int32 hyperlink_index) const {
3661 auto& offsets_map = hyperlink_offset_to_index();
3662 for (auto& offset_index : offsets_map) {
3663 if (offset_index.second == hyperlink_index)
3664 return offset_index.first;
3667 return -1;
3670 int32 BrowserAccessibilityWin::GetHypertextOffsetFromChild(
3671 const BrowserAccessibilityWin& child) const {
3672 int32 hyperlink_index = GetHyperlinkIndexFromChild(child);
3673 if (hyperlink_index < 0)
3674 return -1;
3676 return GetHypertextOffsetFromHyperlinkIndex(hyperlink_index);
3679 int32 BrowserAccessibilityWin::GetHypertextOffsetFromDescendant(
3680 const BrowserAccessibilityWin& descendant) const {
3681 auto parent_object = descendant.GetParent()->ToBrowserAccessibilityWin();
3682 auto current_object = const_cast<BrowserAccessibilityWin*>(&descendant);
3683 while (parent_object && parent_object != this) {
3684 current_object = parent_object;
3685 parent_object = current_object->GetParent()->ToBrowserAccessibilityWin();
3687 if (!parent_object)
3688 return -1;
3690 return parent_object->GetHypertextOffsetFromChild(*current_object);
3693 int BrowserAccessibilityWin::GetSelectionAnchor() const {
3694 BrowserAccessibility* root = manager()->GetRoot();
3695 int32 anchor_id;
3696 if (!root || !root->GetIntAttribute(ui::AX_ATTR_ANCHOR_OBJECT_ID, &anchor_id))
3697 return -1;
3699 BrowserAccessibilityWin* anchor_object = manager()->GetFromID(
3700 anchor_id)->ToBrowserAccessibilityWin();
3701 if (!anchor_object)
3702 return -1;
3704 // Includes the case when anchor_object == this.
3705 if (IsDescendantOf(anchor_object) ||
3706 // Text only objects that are direct descendants should behave as if they
3707 // are part of this object when computing hypertext.
3708 (anchor_object->GetParent() == this &&
3709 anchor_object->IsTextOnlyObject())) {
3710 int anchor_offset;
3711 if (!root->GetIntAttribute(ui::AX_ATTR_ANCHOR_OFFSET, &anchor_offset))
3712 return -1;
3714 return anchor_offset;
3717 if (anchor_object->IsDescendantOf(this))
3718 return GetHypertextOffsetFromDescendant(*anchor_object);
3720 return -1;
3723 int BrowserAccessibilityWin::GetSelectionFocus() const {
3724 BrowserAccessibility* root = manager()->GetRoot();
3725 int32 focus_id;
3726 if (!root || !root->GetIntAttribute(ui::AX_ATTR_FOCUS_OBJECT_ID, &focus_id))
3727 return -1;
3729 BrowserAccessibilityWin* focus_object = manager()->GetFromID(
3730 focus_id)->ToBrowserAccessibilityWin();
3731 if (!focus_object)
3732 return -1;
3734 // Includes the case when focus_object == this.
3735 if (IsDescendantOf(focus_object) ||
3736 // Text only objects that are direct descendants should behave as if they
3737 // are part of this object when computing hypertext.
3738 (focus_object->GetParent() == this && focus_object->IsTextOnlyObject())) {
3739 int focus_offset;
3740 if (!root->GetIntAttribute(ui::AX_ATTR_FOCUS_OFFSET, &focus_offset))
3741 return -1;
3743 return focus_offset;
3746 if (focus_object->IsDescendantOf(this))
3747 return GetHypertextOffsetFromDescendant(*focus_object);
3749 return -1;
3752 void BrowserAccessibilityWin::GetSelectionOffsets(
3753 int* selection_start, int* selection_end) const {
3754 DCHECK(selection_start && selection_end);
3756 if (GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START, selection_start) &&
3757 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, selection_end)) {
3758 return;
3761 *selection_start = GetSelectionAnchor();
3762 *selection_end = GetSelectionFocus();
3763 if (*selection_start < 0 || *selection_end < 0)
3764 return;
3766 if (*selection_end < *selection_start)
3767 std::swap(*selection_start, *selection_end);
3769 // IA2 Spec says that the end of the selection should be after the last
3770 // embedded object character that is part of the selection.
3771 ++(*selection_end);
3774 base::string16 BrowserAccessibilityWin::GetNameRecursive() const {
3775 if (!name().empty()) {
3776 return name();
3779 base::string16 result;
3780 for (uint32 i = 0; i < PlatformChildCount(); ++i) {
3781 result += PlatformGetChild(i)->ToBrowserAccessibilityWin()->
3782 GetNameRecursive();
3784 return result;
3787 base::string16 BrowserAccessibilityWin::GetValueText() {
3788 float fval;
3789 base::string16 value = this->value();
3791 if (value.empty() &&
3792 GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE, &fval)) {
3793 value = base::UTF8ToUTF16(base::DoubleToString(fval));
3795 return value;
3798 base::string16 BrowserAccessibilityWin::TextForIAccessibleText() {
3799 switch (GetRole()) {
3800 case ui::AX_ROLE_TEXT_FIELD:
3801 case ui::AX_ROLE_MENU_LIST_OPTION:
3802 return value();
3803 default:
3804 return hypertext();
3808 bool BrowserAccessibilityWin::IsSameHypertextCharacter(size_t old_char_index,
3809 size_t new_char_index) {
3810 CHECK(old_win_attributes_);
3812 // For anything other than the "embedded character", we just compare the
3813 // characters directly.
3814 base::char16 old_ch = old_win_attributes_->hypertext[old_char_index];
3815 base::char16 new_ch = win_attributes_->hypertext[new_char_index];
3816 if (old_ch != new_ch)
3817 return false;
3818 if (old_ch == new_ch && new_ch != kEmbeddedCharacter)
3819 return true;
3821 // If it's an embedded character, they're only identical if the child id
3822 // the hyperlink points to is the same.
3823 std::map<int32, int32>& old_offset_to_index =
3824 old_win_attributes_->hyperlink_offset_to_index;
3825 std::vector<int32>& old_hyperlinks = old_win_attributes_->hyperlinks;
3826 int32 old_hyperlinks_count = static_cast<int32>(old_hyperlinks.size());
3827 std::map<int32, int32>::iterator iter;
3828 iter = old_offset_to_index.find(old_char_index);
3829 int old_index = (iter != old_offset_to_index.end()) ? iter->second : -1;
3830 int old_child_id = (old_index >= 0 && old_index < old_hyperlinks_count) ?
3831 old_hyperlinks[old_index] : -1;
3833 std::map<int32, int32>& new_offset_to_index =
3834 win_attributes_->hyperlink_offset_to_index;
3835 std::vector<int32>& new_hyperlinks = win_attributes_->hyperlinks;
3836 int32 new_hyperlinks_count = static_cast<int32>(new_hyperlinks.size());
3837 iter = new_offset_to_index.find(new_char_index);
3838 int new_index = (iter != new_offset_to_index.end()) ? iter->second : -1;
3839 int new_child_id = (new_index >= 0 && new_index < new_hyperlinks_count) ?
3840 new_hyperlinks[new_index] : -1;
3842 return old_child_id == new_child_id;
3845 void BrowserAccessibilityWin::ComputeHypertextRemovedAndInserted(
3846 int* start, int* old_len, int* new_len) {
3847 CHECK(old_win_attributes_);
3849 *start = 0;
3850 *old_len = 0;
3851 *new_len = 0;
3853 const base::string16& old_text = old_win_attributes_->hypertext;
3854 const base::string16& new_text = hypertext();
3856 size_t common_prefix = 0;
3857 while (common_prefix < old_text.size() &&
3858 common_prefix < new_text.size() &&
3859 IsSameHypertextCharacter(common_prefix, common_prefix)) {
3860 ++common_prefix;
3863 size_t common_suffix = 0;
3864 while (common_prefix + common_suffix < old_text.size() &&
3865 common_prefix + common_suffix < new_text.size() &&
3866 IsSameHypertextCharacter(
3867 old_text.size() - common_suffix - 1,
3868 new_text.size() - common_suffix - 1)) {
3869 ++common_suffix;
3872 *start = common_prefix;
3873 *old_len = old_text.size() - common_prefix - common_suffix;
3874 *new_len = new_text.size() - common_prefix - common_suffix;
3877 void BrowserAccessibilityWin::HandleSpecialTextOffset(
3878 const base::string16& text,
3879 LONG* offset) {
3880 if (*offset == IA2_TEXT_OFFSET_LENGTH)
3881 *offset = static_cast<LONG>(text.size());
3882 else if (*offset == IA2_TEXT_OFFSET_CARET)
3883 get_caretOffset(offset);
3886 ui::TextBoundaryType BrowserAccessibilityWin::IA2TextBoundaryToTextBoundary(
3887 IA2TextBoundaryType ia2_boundary) {
3888 switch(ia2_boundary) {
3889 case IA2_TEXT_BOUNDARY_CHAR:
3890 return ui::CHAR_BOUNDARY;
3891 case IA2_TEXT_BOUNDARY_WORD:
3892 return ui::WORD_BOUNDARY;
3893 case IA2_TEXT_BOUNDARY_LINE:
3894 return ui::LINE_BOUNDARY;
3895 case IA2_TEXT_BOUNDARY_SENTENCE:
3896 return ui::SENTENCE_BOUNDARY;
3897 case IA2_TEXT_BOUNDARY_PARAGRAPH:
3898 return ui::PARAGRAPH_BOUNDARY;
3899 case IA2_TEXT_BOUNDARY_ALL:
3900 return ui::ALL_BOUNDARY;
3901 default:
3902 NOTREACHED();
3904 return ui::CHAR_BOUNDARY;
3907 LONG BrowserAccessibilityWin::FindBoundary(
3908 const base::string16& text,
3909 IA2TextBoundaryType ia2_boundary,
3910 LONG start_offset,
3911 ui::TextBoundaryDirection direction) {
3912 HandleSpecialTextOffset(text, &start_offset);
3913 if (ia2_boundary == IA2_TEXT_BOUNDARY_WORD &&
3914 GetRole() == ui::AX_ROLE_TEXT_FIELD) {
3915 return GetWordStartBoundary(static_cast<int>(start_offset), direction);
3918 ui::TextBoundaryType boundary = IA2TextBoundaryToTextBoundary(ia2_boundary);
3919 const std::vector<int32>& line_breaks = GetIntListAttribute(
3920 ui::AX_ATTR_LINE_BREAKS);
3921 return ui::FindAccessibleTextBoundary(
3922 text, line_breaks, boundary, start_offset, direction);
3925 BrowserAccessibilityWin* BrowserAccessibilityWin::GetFromID(int32 id) {
3926 return manager()->GetFromID(id)->ToBrowserAccessibilityWin();
3929 bool BrowserAccessibilityWin::IsListBoxOptionOrMenuListOption() {
3930 if (!GetParent())
3931 return false;
3933 int32 role = GetRole();
3934 int32 parent_role = GetParent()->GetRole();
3936 if (role == ui::AX_ROLE_LIST_BOX_OPTION &&
3937 parent_role == ui::AX_ROLE_LIST_BOX) {
3938 return true;
3941 if (role == ui::AX_ROLE_MENU_LIST_OPTION &&
3942 parent_role == ui::AX_ROLE_MENU_LIST_POPUP) {
3943 return true;
3946 return false;
3949 void BrowserAccessibilityWin::InitRoleAndState() {
3950 int32 ia_role = 0;
3951 int32 ia_state = 0;
3952 base::string16 role_name;
3953 int32 ia2_role = 0;
3954 int32 ia2_state = IA2_STATE_OPAQUE;
3956 if (HasState(ui::AX_STATE_BUSY))
3957 ia_state |= STATE_SYSTEM_BUSY;
3958 if (HasState(ui::AX_STATE_CHECKED))
3959 ia_state |= STATE_SYSTEM_CHECKED;
3960 if (HasState(ui::AX_STATE_COLLAPSED))
3961 ia_state |= STATE_SYSTEM_COLLAPSED;
3962 if (HasState(ui::AX_STATE_EXPANDED))
3963 ia_state |= STATE_SYSTEM_EXPANDED;
3964 if (HasState(ui::AX_STATE_FOCUSABLE))
3965 ia_state |= STATE_SYSTEM_FOCUSABLE;
3966 if (HasState(ui::AX_STATE_HASPOPUP))
3967 ia_state |= STATE_SYSTEM_HASPOPUP;
3968 if (HasState(ui::AX_STATE_INDETERMINATE))
3969 ia_state |= STATE_SYSTEM_INDETERMINATE;
3970 if (HasIntAttribute(ui::AX_ATTR_INVALID_STATE) &&
3971 GetIntAttribute(ui::AX_ATTR_INVALID_STATE) != ui::AX_INVALID_STATE_FALSE)
3972 ia2_state |= IA2_STATE_INVALID_ENTRY;
3973 if (HasState(ui::AX_STATE_INVISIBLE))
3974 ia_state |= STATE_SYSTEM_INVISIBLE;
3975 if (HasState(ui::AX_STATE_LINKED))
3976 ia_state |= STATE_SYSTEM_LINKED;
3977 if (HasState(ui::AX_STATE_MULTISELECTABLE)) {
3978 ia_state |= STATE_SYSTEM_EXTSELECTABLE;
3979 ia_state |= STATE_SYSTEM_MULTISELECTABLE;
3981 // TODO(ctguil): Support STATE_SYSTEM_EXTSELECTABLE/accSelect.
3982 if (HasState(ui::AX_STATE_OFFSCREEN))
3983 ia_state |= STATE_SYSTEM_OFFSCREEN;
3984 if (HasState(ui::AX_STATE_PRESSED))
3985 ia_state |= STATE_SYSTEM_PRESSED;
3986 if (HasState(ui::AX_STATE_PROTECTED))
3987 ia_state |= STATE_SYSTEM_PROTECTED;
3988 if (HasState(ui::AX_STATE_REQUIRED))
3989 ia2_state |= IA2_STATE_REQUIRED;
3990 if (HasState(ui::AX_STATE_SELECTABLE))
3991 ia_state |= STATE_SYSTEM_SELECTABLE;
3992 if (HasState(ui::AX_STATE_SELECTED))
3993 ia_state |= STATE_SYSTEM_SELECTED;
3994 if (HasState(ui::AX_STATE_VISITED))
3995 ia_state |= STATE_SYSTEM_TRAVERSED;
3996 if (!HasState(ui::AX_STATE_ENABLED))
3997 ia_state |= STATE_SYSTEM_UNAVAILABLE;
3998 if (HasState(ui::AX_STATE_VERTICAL))
3999 ia2_state |= IA2_STATE_VERTICAL;
4000 if (HasState(ui::AX_STATE_HORIZONTAL))
4001 ia2_state |= IA2_STATE_HORIZONTAL;
4002 if (HasState(ui::AX_STATE_VISITED))
4003 ia_state |= STATE_SYSTEM_TRAVERSED;
4005 // Expose whether or not the mouse is over an element, but suppress
4006 // this for tests because it can make the test results flaky depending
4007 // on the position of the mouse.
4008 BrowserAccessibilityStateImpl* accessibility_state =
4009 BrowserAccessibilityStateImpl::GetInstance();
4010 if (!accessibility_state->disable_hot_tracking_for_testing()) {
4011 if (HasState(ui::AX_STATE_HOVERED))
4012 ia_state |= STATE_SYSTEM_HOTTRACKED;
4015 // WebKit marks everything as readonly unless it's editable text, so if it's
4016 // not readonly, mark it as editable now. The final computation of the
4017 // READONLY state for MSAA is below, after the switch.
4018 if (!HasState(ui::AX_STATE_READ_ONLY))
4019 ia2_state |= IA2_STATE_EDITABLE;
4021 if (GetBoolAttribute(ui::AX_ATTR_BUTTON_MIXED))
4022 ia_state |= STATE_SYSTEM_MIXED;
4024 if (GetBoolAttribute(ui::AX_ATTR_CAN_SET_VALUE))
4025 ia2_state |= IA2_STATE_EDITABLE;
4027 if (!GetStringAttribute(ui::AX_ATTR_AUTO_COMPLETE).empty())
4028 ia2_state |= IA2_STATE_SUPPORTS_AUTOCOMPLETION;
4030 base::string16 html_tag = GetString16Attribute(
4031 ui::AX_ATTR_HTML_TAG);
4032 switch (GetRole()) {
4033 case ui::AX_ROLE_ALERT:
4034 ia_role = ROLE_SYSTEM_ALERT;
4035 break;
4036 case ui::AX_ROLE_ALERT_DIALOG:
4037 ia_role = ROLE_SYSTEM_DIALOG;
4038 break;
4039 case ui::AX_ROLE_APPLICATION:
4040 ia_role = ROLE_SYSTEM_APPLICATION;
4041 break;
4042 case ui::AX_ROLE_ARTICLE:
4043 ia_role = ROLE_SYSTEM_DOCUMENT;
4044 ia_state |= STATE_SYSTEM_READONLY;
4045 break;
4046 case ui::AX_ROLE_BANNER:
4047 ia_role = ROLE_SYSTEM_GROUPING;
4048 ia2_role = IA2_ROLE_HEADER;
4049 break;
4050 case ui::AX_ROLE_BLOCKQUOTE:
4051 role_name = html_tag;
4052 ia2_role = IA2_ROLE_SECTION;
4053 break;
4054 case ui::AX_ROLE_BUSY_INDICATOR:
4055 ia_role = ROLE_SYSTEM_ANIMATION;
4056 ia_state |= STATE_SYSTEM_READONLY;
4057 break;
4058 case ui::AX_ROLE_BUTTON:
4059 ia_role = ROLE_SYSTEM_PUSHBUTTON;
4060 break;
4061 case ui::AX_ROLE_CANVAS:
4062 if (GetBoolAttribute(ui::AX_ATTR_CANVAS_HAS_FALLBACK)) {
4063 role_name = L"canvas";
4064 ia2_role = IA2_ROLE_CANVAS;
4065 } else {
4066 ia_role = ROLE_SYSTEM_GRAPHIC;
4068 break;
4069 case ui::AX_ROLE_CAPTION:
4070 ia_role = ROLE_SYSTEM_TEXT;
4071 ia2_role = IA2_ROLE_CAPTION;
4072 break;
4073 case ui::AX_ROLE_CELL:
4074 ia_role = ROLE_SYSTEM_CELL;
4075 break;
4076 case ui::AX_ROLE_CHECK_BOX:
4077 ia_role = ROLE_SYSTEM_CHECKBUTTON;
4078 ia2_state |= IA2_STATE_CHECKABLE;
4079 break;
4080 case ui::AX_ROLE_COLOR_WELL:
4081 ia_role = ROLE_SYSTEM_TEXT;
4082 ia2_role = IA2_ROLE_COLOR_CHOOSER;
4083 break;
4084 case ui::AX_ROLE_COLUMN:
4085 ia_role = ROLE_SYSTEM_COLUMN;
4086 break;
4087 case ui::AX_ROLE_COLUMN_HEADER:
4088 ia_role = ROLE_SYSTEM_COLUMNHEADER;
4089 break;
4090 case ui::AX_ROLE_COMBO_BOX:
4091 ia_role = ROLE_SYSTEM_COMBOBOX;
4092 break;
4093 case ui::AX_ROLE_COMPLEMENTARY:
4094 ia_role = ROLE_SYSTEM_GROUPING;
4095 ia2_role = IA2_ROLE_NOTE;
4096 break;
4097 case ui::AX_ROLE_CONTENT_INFO:
4098 ia_role = ROLE_SYSTEM_TEXT;
4099 ia2_role = IA2_ROLE_PARAGRAPH;
4100 break;
4101 case ui::AX_ROLE_DATE:
4102 case ui::AX_ROLE_DATE_TIME:
4103 ia_role = ROLE_SYSTEM_DROPLIST;
4104 ia2_role = IA2_ROLE_DATE_EDITOR;
4105 break;
4106 case ui::AX_ROLE_DIV:
4107 role_name = L"div";
4108 ia_role = ROLE_SYSTEM_GROUPING;
4109 ia2_role = IA2_ROLE_SECTION;
4110 break;
4111 case ui::AX_ROLE_DEFINITION:
4112 role_name = html_tag;
4113 ia2_role = IA2_ROLE_PARAGRAPH;
4114 ia_state |= STATE_SYSTEM_READONLY;
4115 break;
4116 case ui::AX_ROLE_DESCRIPTION_LIST_DETAIL:
4117 role_name = html_tag;
4118 ia_role = ROLE_SYSTEM_TEXT;
4119 ia2_role = IA2_ROLE_PARAGRAPH;
4120 break;
4121 case ui::AX_ROLE_DESCRIPTION_LIST:
4122 role_name = html_tag;
4123 ia_role = ROLE_SYSTEM_LIST;
4124 ia_state |= STATE_SYSTEM_READONLY;
4125 break;
4126 case ui::AX_ROLE_DESCRIPTION_LIST_TERM:
4127 ia_role = ROLE_SYSTEM_LISTITEM;
4128 ia_state |= STATE_SYSTEM_READONLY;
4129 break;
4130 case ui::AX_ROLE_DETAILS:
4131 role_name = html_tag;
4132 ia_role = ROLE_SYSTEM_GROUPING;
4133 break;
4134 case ui::AX_ROLE_DIALOG:
4135 ia_role = ROLE_SYSTEM_DIALOG;
4136 break;
4137 case ui::AX_ROLE_DISCLOSURE_TRIANGLE:
4138 ia_role = ROLE_SYSTEM_PUSHBUTTON;
4139 break;
4140 case ui::AX_ROLE_DOCUMENT:
4141 case ui::AX_ROLE_ROOT_WEB_AREA:
4142 case ui::AX_ROLE_WEB_AREA:
4143 ia_role = ROLE_SYSTEM_DOCUMENT;
4144 ia_state |= STATE_SYSTEM_READONLY;
4145 ia_state |= STATE_SYSTEM_FOCUSABLE;
4146 break;
4147 case ui::AX_ROLE_EMBEDDED_OBJECT:
4148 ia_role = ROLE_SYSTEM_CLIENT;
4149 ia2_role = IA2_ROLE_EMBEDDED_OBJECT;
4150 break;
4151 case ui::AX_ROLE_FIGCAPTION:
4152 role_name = html_tag;
4153 ia2_role = IA2_ROLE_CAPTION;
4154 break;
4155 case ui::AX_ROLE_FIGURE:
4156 ia_role = ROLE_SYSTEM_GROUPING;
4157 break;
4158 case ui::AX_ROLE_FORM:
4159 role_name = L"form";
4160 ia2_role = IA2_ROLE_FORM;
4161 break;
4162 case ui::AX_ROLE_FOOTER:
4163 ia_role = ROLE_SYSTEM_GROUPING;
4164 ia2_role = IA2_ROLE_FOOTER;
4165 break;
4166 case ui::AX_ROLE_GRID:
4167 ia_role = ROLE_SYSTEM_TABLE;
4168 ia_state |= STATE_SYSTEM_READONLY;
4169 break;
4170 case ui::AX_ROLE_GROUP: {
4171 base::string16 aria_role = GetString16Attribute(
4172 ui::AX_ATTR_ROLE);
4173 if (aria_role == L"group" || html_tag == L"fieldset") {
4174 ia_role = ROLE_SYSTEM_GROUPING;
4175 } else if (html_tag == L"li") {
4176 ia_role = ROLE_SYSTEM_LISTITEM;
4177 ia_state |= STATE_SYSTEM_READONLY;
4178 } else {
4179 if (html_tag.empty())
4180 role_name = L"div";
4181 else
4182 role_name = html_tag;
4183 ia2_role = IA2_ROLE_SECTION;
4185 break;
4187 case ui::AX_ROLE_HEADING:
4188 role_name = html_tag;
4189 ia2_role = IA2_ROLE_HEADING;
4190 break;
4191 case ui::AX_ROLE_IFRAME:
4192 ia_role = ROLE_SYSTEM_DOCUMENT;
4193 ia2_role = IA2_ROLE_INTERNAL_FRAME;
4194 ia_state = STATE_SYSTEM_READONLY;
4195 break;
4196 case ui::AX_ROLE_IFRAME_PRESENTATIONAL:
4197 ia_role = ROLE_SYSTEM_GROUPING;
4198 break;
4199 case ui::AX_ROLE_IMAGE:
4200 ia_role = ROLE_SYSTEM_GRAPHIC;
4201 ia_state |= STATE_SYSTEM_READONLY;
4202 break;
4203 case ui::AX_ROLE_IMAGE_MAP:
4204 role_name = html_tag;
4205 ia2_role = IA2_ROLE_IMAGE_MAP;
4206 ia_state |= STATE_SYSTEM_READONLY;
4207 break;
4208 case ui::AX_ROLE_IMAGE_MAP_LINK:
4209 ia_role = ROLE_SYSTEM_LINK;
4210 ia_state |= STATE_SYSTEM_LINKED;
4211 ia_state |= STATE_SYSTEM_READONLY;
4212 break;
4213 case ui::AX_ROLE_INPUT_TIME:
4214 ia_role = ROLE_SYSTEM_GROUPING;
4215 break;
4216 case ui::AX_ROLE_LABEL_TEXT:
4217 case ui::AX_ROLE_LEGEND:
4218 ia_role = ROLE_SYSTEM_TEXT;
4219 ia2_role = IA2_ROLE_LABEL;
4220 break;
4221 case ui::AX_ROLE_LINK:
4222 ia_role = ROLE_SYSTEM_LINK;
4223 ia_state |= STATE_SYSTEM_LINKED;
4224 break;
4225 case ui::AX_ROLE_LIST:
4226 ia_role = ROLE_SYSTEM_LIST;
4227 ia_state |= STATE_SYSTEM_READONLY;
4228 break;
4229 case ui::AX_ROLE_LIST_BOX:
4230 ia_role = ROLE_SYSTEM_LIST;
4231 break;
4232 case ui::AX_ROLE_LIST_BOX_OPTION:
4233 ia_role = ROLE_SYSTEM_LISTITEM;
4234 if (ia_state & STATE_SYSTEM_SELECTABLE) {
4235 ia_state |= STATE_SYSTEM_FOCUSABLE;
4236 if (HasState(ui::AX_STATE_FOCUSED))
4237 ia_state |= STATE_SYSTEM_FOCUSED;
4239 break;
4240 case ui::AX_ROLE_LIST_ITEM:
4241 ia_role = ROLE_SYSTEM_LISTITEM;
4242 ia_state |= STATE_SYSTEM_READONLY;
4243 break;
4244 case ui::AX_ROLE_MAIN:
4245 ia_role = ROLE_SYSTEM_GROUPING;
4246 ia2_role = IA2_ROLE_PARAGRAPH;
4247 break;
4248 case ui::AX_ROLE_MARK:
4249 ia_role = ROLE_SYSTEM_TEXT;
4250 ia2_role = IA2_ROLE_TEXT_FRAME;
4251 break;
4252 case ui::AX_ROLE_MARQUEE:
4253 ia_role = ROLE_SYSTEM_ANIMATION;
4254 break;
4255 case ui::AX_ROLE_MATH:
4256 ia_role = ROLE_SYSTEM_EQUATION;
4257 break;
4258 case ui::AX_ROLE_MENU:
4259 case ui::AX_ROLE_MENU_BUTTON:
4260 ia_role = ROLE_SYSTEM_MENUPOPUP;
4261 break;
4262 case ui::AX_ROLE_MENU_BAR:
4263 ia_role = ROLE_SYSTEM_MENUBAR;
4264 break;
4265 case ui::AX_ROLE_MENU_ITEM:
4266 ia_role = ROLE_SYSTEM_MENUITEM;
4267 break;
4268 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
4269 ia_role = ROLE_SYSTEM_MENUITEM;
4270 ia2_role = IA2_ROLE_CHECK_MENU_ITEM;
4271 ia2_state |= IA2_STATE_CHECKABLE;
4272 break;
4273 case ui::AX_ROLE_MENU_ITEM_RADIO:
4274 ia_role = ROLE_SYSTEM_MENUITEM;
4275 ia2_role = IA2_ROLE_RADIO_MENU_ITEM;
4276 break;
4277 case ui::AX_ROLE_MENU_LIST_POPUP:
4278 ia_role = ROLE_SYSTEM_LIST;
4279 ia2_state &= ~(IA2_STATE_EDITABLE);
4280 break;
4281 case ui::AX_ROLE_MENU_LIST_OPTION:
4282 ia_role = ROLE_SYSTEM_LISTITEM;
4283 ia2_state &= ~(IA2_STATE_EDITABLE);
4284 if (ia_state & STATE_SYSTEM_SELECTABLE) {
4285 ia_state |= STATE_SYSTEM_FOCUSABLE;
4286 if (HasState(ui::AX_STATE_FOCUSED))
4287 ia_state |= STATE_SYSTEM_FOCUSED;
4289 break;
4290 case ui::AX_ROLE_METER:
4291 role_name = html_tag;
4292 ia_role = ROLE_SYSTEM_PROGRESSBAR;
4293 break;
4294 case ui::AX_ROLE_NAVIGATION:
4295 ia_role = ROLE_SYSTEM_GROUPING;
4296 ia2_role = IA2_ROLE_SECTION;
4297 break;
4298 case ui::AX_ROLE_NOTE:
4299 ia_role = ROLE_SYSTEM_GROUPING;
4300 ia2_role = IA2_ROLE_NOTE;
4301 break;
4302 case ui::AX_ROLE_OUTLINE:
4303 ia_role = ROLE_SYSTEM_OUTLINE;
4304 break;
4305 case ui::AX_ROLE_PARAGRAPH:
4306 role_name = L"P";
4307 ia2_role = IA2_ROLE_PARAGRAPH;
4308 break;
4309 case ui::AX_ROLE_POP_UP_BUTTON:
4310 if (html_tag == L"select") {
4311 ia_role = ROLE_SYSTEM_COMBOBOX;
4312 } else {
4313 ia_role = ROLE_SYSTEM_BUTTONMENU;
4315 break;
4316 case ui::AX_ROLE_PRE:
4317 role_name = html_tag;
4318 ia_role = ROLE_SYSTEM_TEXT;
4319 ia2_role = IA2_ROLE_PARAGRAPH;
4320 break;
4321 case ui::AX_ROLE_PROGRESS_INDICATOR:
4322 ia_role = ROLE_SYSTEM_PROGRESSBAR;
4323 ia_state |= STATE_SYSTEM_READONLY;
4324 break;
4325 case ui::AX_ROLE_RADIO_BUTTON:
4326 ia_role = ROLE_SYSTEM_RADIOBUTTON;
4327 ia2_state = IA2_STATE_CHECKABLE;
4328 break;
4329 case ui::AX_ROLE_RADIO_GROUP:
4330 ia_role = ROLE_SYSTEM_GROUPING;
4331 break;
4332 case ui::AX_ROLE_REGION:
4333 if (html_tag == L"section") {
4334 ia_role = ROLE_SYSTEM_GROUPING;
4335 ia2_role = IA2_ROLE_SECTION;
4336 } else {
4337 ia_role = ROLE_SYSTEM_PANE;
4339 break;
4340 case ui::AX_ROLE_ROW:
4341 ia_role = ROLE_SYSTEM_ROW;
4342 break;
4343 case ui::AX_ROLE_ROW_HEADER:
4344 ia_role = ROLE_SYSTEM_ROWHEADER;
4345 break;
4346 case ui::AX_ROLE_RUBY:
4347 ia_role = ROLE_SYSTEM_TEXT;
4348 ia2_role = IA2_ROLE_TEXT_FRAME;
4349 break;
4350 case ui::AX_ROLE_RULER:
4351 ia_role = ROLE_SYSTEM_CLIENT;
4352 ia2_role = IA2_ROLE_RULER;
4353 ia_state |= STATE_SYSTEM_READONLY;
4354 break;
4355 case ui::AX_ROLE_SCROLL_AREA:
4356 ia_role = ROLE_SYSTEM_CLIENT;
4357 ia2_role = IA2_ROLE_SCROLL_PANE;
4358 ia_state |= STATE_SYSTEM_READONLY;
4359 ia2_state &= ~(IA2_STATE_EDITABLE);
4360 break;
4361 case ui::AX_ROLE_SCROLL_BAR:
4362 ia_role = ROLE_SYSTEM_SCROLLBAR;
4363 break;
4364 case ui::AX_ROLE_SEARCH:
4365 ia_role = ROLE_SYSTEM_GROUPING;
4366 ia2_role = IA2_ROLE_SECTION;
4367 break;
4368 case ui::AX_ROLE_SLIDER:
4369 ia_role = ROLE_SYSTEM_SLIDER;
4370 break;
4371 case ui::AX_ROLE_SPIN_BUTTON:
4372 ia_role = ROLE_SYSTEM_SPINBUTTON;
4373 break;
4374 case ui::AX_ROLE_SPIN_BUTTON_PART:
4375 ia_role = ROLE_SYSTEM_PUSHBUTTON;
4376 break;
4377 case ui::AX_ROLE_ANNOTATION:
4378 case ui::AX_ROLE_LIST_MARKER:
4379 case ui::AX_ROLE_STATIC_TEXT:
4380 ia_role = ROLE_SYSTEM_STATICTEXT;
4381 break;
4382 case ui::AX_ROLE_STATUS:
4383 ia_role = ROLE_SYSTEM_STATUSBAR;
4384 break;
4385 case ui::AX_ROLE_SPLITTER:
4386 ia_role = ROLE_SYSTEM_SEPARATOR;
4387 break;
4388 case ui::AX_ROLE_SVG_ROOT:
4389 ia_role = ROLE_SYSTEM_GRAPHIC;
4390 break;
4391 case ui::AX_ROLE_SWITCH:
4392 role_name = L"switch";
4393 ia2_role = IA2_ROLE_TOGGLE_BUTTON;
4394 break;
4395 case ui::AX_ROLE_TAB:
4396 ia_role = ROLE_SYSTEM_PAGETAB;
4397 break;
4398 case ui::AX_ROLE_TABLE: {
4399 base::string16 aria_role = GetString16Attribute(
4400 ui::AX_ATTR_ROLE);
4401 if (aria_role == L"treegrid") {
4402 ia_role = ROLE_SYSTEM_OUTLINE;
4403 } else {
4404 ia_role = ROLE_SYSTEM_TABLE;
4406 break;
4408 case ui::AX_ROLE_TABLE_HEADER_CONTAINER:
4409 ia_role = ROLE_SYSTEM_GROUPING;
4410 ia2_role = IA2_ROLE_SECTION;
4411 ia_state |= STATE_SYSTEM_READONLY;
4412 break;
4413 case ui::AX_ROLE_TAB_LIST:
4414 ia_role = ROLE_SYSTEM_PAGETABLIST;
4415 break;
4416 case ui::AX_ROLE_TAB_PANEL:
4417 ia_role = ROLE_SYSTEM_PROPERTYPAGE;
4418 break;
4419 case ui::AX_ROLE_TOGGLE_BUTTON:
4420 ia_role = ROLE_SYSTEM_PUSHBUTTON;
4421 ia2_role = IA2_ROLE_TOGGLE_BUTTON;
4422 break;
4423 case ui::AX_ROLE_TEXT_FIELD:
4424 case ui::AX_ROLE_SEARCH_BOX:
4425 ia_role = ROLE_SYSTEM_TEXT;
4426 if (HasState(ui::AX_STATE_MULTILINE))
4427 ia2_state |= IA2_STATE_MULTI_LINE;
4428 else
4429 ia2_state |= IA2_STATE_SINGLE_LINE;
4430 ia2_state |= IA2_STATE_EDITABLE;
4431 ia2_state |= IA2_STATE_SELECTABLE_TEXT;
4432 break;
4433 case ui::AX_ROLE_TIME:
4434 role_name = html_tag;
4435 ia_role = ROLE_SYSTEM_TEXT;
4436 ia2_role = IA2_ROLE_TEXT_FRAME;
4437 break;
4438 case ui::AX_ROLE_TIMER:
4439 ia_role = ROLE_SYSTEM_CLOCK;
4440 ia_state |= STATE_SYSTEM_READONLY;
4441 break;
4442 case ui::AX_ROLE_TOOLBAR:
4443 ia_role = ROLE_SYSTEM_TOOLBAR;
4444 ia_state |= STATE_SYSTEM_READONLY;
4445 break;
4446 case ui::AX_ROLE_TOOLTIP:
4447 ia_role = ROLE_SYSTEM_TOOLTIP;
4448 ia_state |= STATE_SYSTEM_READONLY;
4449 break;
4450 case ui::AX_ROLE_TREE:
4451 ia_role = ROLE_SYSTEM_OUTLINE;
4452 break;
4453 case ui::AX_ROLE_TREE_GRID:
4454 ia_role = ROLE_SYSTEM_OUTLINE;
4455 break;
4456 case ui::AX_ROLE_TREE_ITEM:
4457 ia_role = ROLE_SYSTEM_OUTLINEITEM;
4458 break;
4459 case ui::AX_ROLE_LINE_BREAK:
4460 ia_role = ROLE_SYSTEM_WHITESPACE;
4461 break;
4462 case ui::AX_ROLE_WINDOW:
4463 ia_role = ROLE_SYSTEM_WINDOW;
4464 break;
4466 // TODO(dmazzoni): figure out the proper MSAA role for all of these.
4467 case ui::AX_ROLE_DIRECTORY:
4468 case ui::AX_ROLE_IGNORED:
4469 case ui::AX_ROLE_LOG:
4470 case ui::AX_ROLE_NONE:
4471 case ui::AX_ROLE_PRESENTATIONAL:
4472 case ui::AX_ROLE_SLIDER_THUMB:
4473 default:
4474 ia_role = ROLE_SYSTEM_CLIENT;
4475 break;
4478 // Compute the final value of READONLY for MSAA.
4480 // We always set the READONLY state for elements that have the
4481 // aria-readonly attribute and for a few roles (in the switch above).
4482 // We clear the READONLY state on focusable controls and on a document.
4483 // Everything else, the majority of objects, do not have this state set.
4484 if (HasState(ui::AX_STATE_FOCUSABLE) &&
4485 ia_role != ROLE_SYSTEM_DOCUMENT) {
4486 ia_state &= ~(STATE_SYSTEM_READONLY);
4488 if (!HasState(ui::AX_STATE_READ_ONLY))
4489 ia_state &= ~(STATE_SYSTEM_READONLY);
4490 if (GetBoolAttribute(ui::AX_ATTR_ARIA_READONLY))
4491 ia_state |= STATE_SYSTEM_READONLY;
4493 // The role should always be set.
4494 DCHECK(!role_name.empty() || ia_role);
4496 // If we didn't explicitly set the IAccessible2 role, make it the same
4497 // as the MSAA role.
4498 if (!ia2_role)
4499 ia2_role = ia_role;
4501 win_attributes_->ia_role = ia_role;
4502 win_attributes_->ia_state = ia_state;
4503 win_attributes_->role_name = role_name;
4504 win_attributes_->ia2_role = ia2_role;
4505 win_attributes_->ia2_state = ia2_state;
4508 } // namespace content