451043b6dc76246293c09ed57f9976f0006251f2
[platform/framework/web/crosswalk.git] / src / content / browser / accessibility / browser_accessibility_win.cc
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.
4
5 #include "content/browser/accessibility/browser_accessibility_win.h"
6
7 #include <UIAutomationClient.h>
8 #include <UIAutomationCoreApi.h>
9
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/enum_variant.h"
15 #include "base/win/scoped_comptr.h"
16 #include "base/win/windows_version.h"
17 #include "content/browser/accessibility/browser_accessibility_manager_win.h"
18 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
19 #include "content/common/accessibility_messages.h"
20 #include "content/public/common/content_client.h"
21 #include "ui/base/accessibility/accessible_text_utils.h"
22 #include "ui/base/win/accessibility_ids_win.h"
23 #include "ui/base/win/accessibility_misc_utils.h"
24
25 namespace content {
26
27 // These nonstandard GUIDs are taken directly from the Mozilla sources
28 // (accessible/src/msaa/nsAccessNodeWrap.cpp); some documentation is here:
29 // http://developer.mozilla.org/en/Accessibility/AT-APIs/ImplementationFeatures/MSAA
30 const GUID GUID_ISimpleDOM = {
31     0x0c539790, 0x12e4, 0x11cf,
32     0xb6, 0x61, 0x00, 0xaa, 0x00, 0x4c, 0xd6, 0xd8};
33 const GUID GUID_IAccessibleContentDocument = {
34     0xa5d8e1f3, 0x3571, 0x4d8f,
35     0x95, 0x21, 0x07, 0xed, 0x28, 0xfb, 0x07, 0x2e};
36
37 const base::char16 BrowserAccessibilityWin::kEmbeddedCharacter[] = L"\xfffc";
38
39 // static
40 LONG BrowserAccessibilityWin::next_unique_id_win_ =
41     base::win::kFirstBrowserAccessibilityManagerAccessibilityId;
42
43 //
44 // BrowserAccessibilityRelation
45 //
46 // A simple implementation of IAccessibleRelation, used to represent
47 // a relationship between two accessible nodes in the tree.
48 //
49
50 class BrowserAccessibilityRelation
51     : public CComObjectRootEx<CComMultiThreadModel>,
52       public IAccessibleRelation {
53   BEGIN_COM_MAP(BrowserAccessibilityRelation)
54     COM_INTERFACE_ENTRY(IAccessibleRelation)
55   END_COM_MAP()
56
57   CONTENT_EXPORT BrowserAccessibilityRelation() {}
58   CONTENT_EXPORT virtual ~BrowserAccessibilityRelation() {}
59
60   CONTENT_EXPORT void Initialize(BrowserAccessibilityWin* owner,
61                                  const base::string16& type);
62   CONTENT_EXPORT void AddTarget(int target_id);
63
64   // IAccessibleRelation methods.
65   CONTENT_EXPORT STDMETHODIMP get_relationType(BSTR* relation_type);
66   CONTENT_EXPORT STDMETHODIMP get_nTargets(long* n_targets);
67   CONTENT_EXPORT STDMETHODIMP get_target(long target_index, IUnknown** target);
68   CONTENT_EXPORT STDMETHODIMP get_targets(long max_targets,
69                                           IUnknown** targets,
70                                           long* n_targets);
71
72   // IAccessibleRelation methods not implemented.
73   CONTENT_EXPORT STDMETHODIMP get_localizedRelationType(BSTR* relation_type) {
74     return E_NOTIMPL;
75   }
76
77  private:
78   base::string16 type_;
79   base::win::ScopedComPtr<BrowserAccessibilityWin> owner_;
80   std::vector<int> target_ids_;
81 };
82
83 void BrowserAccessibilityRelation::Initialize(BrowserAccessibilityWin* owner,
84                                               const base::string16& type) {
85   owner_ = owner;
86   type_ = type;
87 }
88
89 void BrowserAccessibilityRelation::AddTarget(int target_id) {
90   target_ids_.push_back(target_id);
91 }
92
93 STDMETHODIMP BrowserAccessibilityRelation::get_relationType(
94     BSTR* relation_type) {
95   if (!relation_type)
96     return E_INVALIDARG;
97
98   if (!owner_->instance_active())
99     return E_FAIL;
100
101   *relation_type = SysAllocString(type_.c_str());
102   DCHECK(*relation_type);
103   return S_OK;
104 }
105
106 STDMETHODIMP BrowserAccessibilityRelation::get_nTargets(long* n_targets) {
107   if (!n_targets)
108     return E_INVALIDARG;
109
110   if (!owner_->instance_active())
111     return E_FAIL;
112
113   *n_targets = static_cast<long>(target_ids_.size());
114
115   BrowserAccessibilityManager* manager = owner_->manager();
116   for (long i = *n_targets - 1; i >= 0; --i) {
117     BrowserAccessibility* result = manager->GetFromRendererID(target_ids_[i]);
118     if (!result || !result->instance_active()) {
119       *n_targets = 0;
120       break;
121     }
122   }
123   return S_OK;
124 }
125
126 STDMETHODIMP BrowserAccessibilityRelation::get_target(long target_index,
127                                                       IUnknown** target) {
128   if (!target)
129     return E_INVALIDARG;
130
131   if (!owner_->instance_active())
132     return E_FAIL;
133
134   if (target_index < 0 ||
135       target_index >= static_cast<long>(target_ids_.size())) {
136     return E_INVALIDARG;
137   }
138
139   BrowserAccessibilityManager* manager = owner_->manager();
140   BrowserAccessibility* result =
141       manager->GetFromRendererID(target_ids_[target_index]);
142   if (!result || !result->instance_active())
143     return E_FAIL;
144
145   *target = static_cast<IAccessible*>(
146       result->ToBrowserAccessibilityWin()->NewReference());
147   return S_OK;
148 }
149
150 STDMETHODIMP BrowserAccessibilityRelation::get_targets(long max_targets,
151                                                        IUnknown** targets,
152                                                        long* n_targets) {
153   if (!targets || !n_targets)
154     return E_INVALIDARG;
155
156   if (!owner_->instance_active())
157     return E_FAIL;
158
159   long count = static_cast<long>(target_ids_.size());
160   if (count > max_targets)
161     count = max_targets;
162
163   *n_targets = count;
164   if (count == 0)
165     return S_FALSE;
166
167   for (long i = 0; i < count; ++i) {
168     HRESULT result = get_target(i, &targets[i]);
169     if (result != S_OK)
170       return result;
171   }
172
173   return S_OK;
174 }
175
176 //
177 // BrowserAccessibilityWin
178 //
179
180 // static
181 BrowserAccessibility* BrowserAccessibility::Create() {
182   CComObject<BrowserAccessibilityWin>* instance;
183   HRESULT hr = CComObject<BrowserAccessibilityWin>::CreateInstance(&instance);
184   DCHECK(SUCCEEDED(hr));
185   return instance->NewReference();
186 }
187
188 BrowserAccessibilityWin* BrowserAccessibility::ToBrowserAccessibilityWin() {
189   return static_cast<BrowserAccessibilityWin*>(this);
190 }
191
192 BrowserAccessibilityWin::BrowserAccessibilityWin()
193     : ia_role_(0),
194       ia_state_(0),
195       ia2_role_(0),
196       ia2_state_(0),
197       first_time_(true),
198       old_ia_state_(0),
199       previous_scroll_x_(0),
200       previous_scroll_y_(0) {
201   // Start unique IDs at -1 and decrement each time, because get_accChild
202   // uses positive IDs to enumerate children, so we use negative IDs to
203   // clearly distinguish between indices and unique IDs.
204   unique_id_win_ = next_unique_id_win_;
205   if (next_unique_id_win_ ==
206           base::win::kLastBrowserAccessibilityManagerAccessibilityId) {
207     next_unique_id_win_ =
208         base::win::kFirstBrowserAccessibilityManagerAccessibilityId;
209   }
210   next_unique_id_win_--;
211 }
212
213 BrowserAccessibilityWin::~BrowserAccessibilityWin() {
214   for (size_t i = 0; i < relations_.size(); ++i)
215     relations_[i]->Release();
216 }
217
218 //
219 // IAccessible methods.
220 //
221 // Conventions:
222 // * Always test for instance_active() first and return E_FAIL if it's false.
223 // * Always check for invalid arguments first, even if they're unused.
224 // * Return S_FALSE if the only output is a string argument and it's empty.
225 //
226
227 HRESULT BrowserAccessibilityWin::accDoDefaultAction(VARIANT var_id) {
228   if (!instance_active())
229     return E_FAIL;
230
231   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
232   if (!target)
233     return E_INVALIDARG;
234
235   manager()->DoDefaultAction(*target);
236   return S_OK;
237 }
238
239 STDMETHODIMP BrowserAccessibilityWin::accHitTest(LONG x_left,
240                                                  LONG y_top,
241                                                  VARIANT* child) {
242   if (!instance_active())
243     return E_FAIL;
244
245   if (!child)
246     return E_INVALIDARG;
247
248   gfx::Point point(x_left, y_top);
249   if (!GetGlobalBoundsRect().Contains(point)) {
250     // Return S_FALSE and VT_EMPTY when the outside the object's boundaries.
251     child->vt = VT_EMPTY;
252     return S_FALSE;
253   }
254
255   BrowserAccessibility* result = BrowserAccessibilityForPoint(point);
256   if (result == this) {
257     // Point is within this object.
258     child->vt = VT_I4;
259     child->lVal = CHILDID_SELF;
260   } else {
261     child->vt = VT_DISPATCH;
262     child->pdispVal = result->ToBrowserAccessibilityWin()->NewReference();
263   }
264   return S_OK;
265 }
266
267 STDMETHODIMP BrowserAccessibilityWin::accLocation(LONG* x_left,
268                                                   LONG* y_top,
269                                                   LONG* width,
270                                                   LONG* height,
271                                                   VARIANT var_id) {
272   if (!instance_active())
273     return E_FAIL;
274
275   if (!x_left || !y_top || !width || !height)
276     return E_INVALIDARG;
277
278   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
279   if (!target)
280     return E_INVALIDARG;
281
282   gfx::Rect bounds = target->GetGlobalBoundsRect();
283   *x_left = bounds.x();
284   *y_top  = bounds.y();
285   *width  = bounds.width();
286   *height = bounds.height();
287
288   return S_OK;
289 }
290
291 STDMETHODIMP BrowserAccessibilityWin::accNavigate(LONG nav_dir,
292                                                   VARIANT start,
293                                                   VARIANT* end) {
294   BrowserAccessibilityWin* target = GetTargetFromChildID(start);
295   if (!target)
296     return E_INVALIDARG;
297
298   if ((nav_dir == NAVDIR_LASTCHILD || nav_dir == NAVDIR_FIRSTCHILD) &&
299       start.lVal != CHILDID_SELF) {
300     // MSAA states that navigating to first/last child can only be from self.
301     return E_INVALIDARG;
302   }
303
304   uint32 child_count = target->PlatformChildCount();
305
306   BrowserAccessibility* result = NULL;
307   switch (nav_dir) {
308     case NAVDIR_DOWN:
309     case NAVDIR_UP:
310     case NAVDIR_LEFT:
311     case NAVDIR_RIGHT:
312       // These directions are not implemented, matching Mozilla and IE.
313       return E_NOTIMPL;
314     case NAVDIR_FIRSTCHILD:
315       if (child_count > 0)
316         result = target->PlatformGetChild(0);
317       break;
318     case NAVDIR_LASTCHILD:
319       if (child_count > 0)
320         result = target->PlatformGetChild(child_count - 1);
321       break;
322     case NAVDIR_NEXT:
323       result = target->GetNextSibling();
324       break;
325     case NAVDIR_PREVIOUS:
326       result = target->GetPreviousSibling();
327       break;
328   }
329
330   if (!result) {
331     end->vt = VT_EMPTY;
332     return S_FALSE;
333   }
334
335   end->vt = VT_DISPATCH;
336   end->pdispVal = result->ToBrowserAccessibilityWin()->NewReference();
337   return S_OK;
338 }
339
340 STDMETHODIMP BrowserAccessibilityWin::get_accChild(VARIANT var_child,
341                                                    IDispatch** disp_child) {
342   if (!instance_active())
343     return E_FAIL;
344
345   if (!disp_child)
346     return E_INVALIDARG;
347
348   *disp_child = NULL;
349
350   BrowserAccessibilityWin* target = GetTargetFromChildID(var_child);
351   if (!target)
352     return E_INVALIDARG;
353
354   (*disp_child) = target->NewReference();
355   return S_OK;
356 }
357
358 STDMETHODIMP BrowserAccessibilityWin::get_accChildCount(LONG* child_count) {
359   if (!instance_active())
360     return E_FAIL;
361
362   if (!child_count)
363     return E_INVALIDARG;
364
365   *child_count = PlatformChildCount();
366
367   return S_OK;
368 }
369
370 STDMETHODIMP BrowserAccessibilityWin::get_accDefaultAction(VARIANT var_id,
371                                                            BSTR* def_action) {
372   if (!instance_active())
373     return E_FAIL;
374
375   if (!def_action)
376     return E_INVALIDARG;
377
378   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
379   if (!target)
380     return E_INVALIDARG;
381
382   return target->GetStringAttributeAsBstr(
383       ui::AX_ATTR_SHORTCUT, def_action);
384 }
385
386 STDMETHODIMP BrowserAccessibilityWin::get_accDescription(VARIANT var_id,
387                                                          BSTR* desc) {
388   if (!instance_active())
389     return E_FAIL;
390
391   if (!desc)
392     return E_INVALIDARG;
393
394   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
395   if (!target)
396     return E_INVALIDARG;
397
398   return target->GetStringAttributeAsBstr(
399       ui::AX_ATTR_DESCRIPTION, desc);
400 }
401
402 STDMETHODIMP BrowserAccessibilityWin::get_accFocus(VARIANT* focus_child) {
403   if (!instance_active())
404     return E_FAIL;
405
406   if (!focus_child)
407     return E_INVALIDARG;
408
409   BrowserAccessibilityWin* focus = static_cast<BrowserAccessibilityWin*>(
410       manager()->GetFocus(this));
411   if (focus == this) {
412     focus_child->vt = VT_I4;
413     focus_child->lVal = CHILDID_SELF;
414   } else if (focus == NULL) {
415     focus_child->vt = VT_EMPTY;
416   } else {
417     focus_child->vt = VT_DISPATCH;
418     focus_child->pdispVal = focus->NewReference();
419   }
420
421   return S_OK;
422 }
423
424 STDMETHODIMP BrowserAccessibilityWin::get_accHelp(VARIANT var_id, BSTR* help) {
425   if (!instance_active())
426     return E_FAIL;
427
428   if (!help)
429     return E_INVALIDARG;
430
431   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
432   if (!target)
433     return E_INVALIDARG;
434
435   return target->GetStringAttributeAsBstr(
436       ui::AX_ATTR_HELP, help);
437 }
438
439 STDMETHODIMP BrowserAccessibilityWin::get_accKeyboardShortcut(VARIANT var_id,
440                                                               BSTR* acc_key) {
441   if (!instance_active())
442     return E_FAIL;
443
444   if (!acc_key)
445     return E_INVALIDARG;
446
447   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
448   if (!target)
449     return E_INVALIDARG;
450
451   return target->GetStringAttributeAsBstr(
452       ui::AX_ATTR_SHORTCUT, acc_key);
453 }
454
455 STDMETHODIMP BrowserAccessibilityWin::get_accName(VARIANT var_id, BSTR* name) {
456   if (!instance_active())
457     return E_FAIL;
458
459   if (!name)
460     return E_INVALIDARG;
461
462   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
463   if (!target)
464     return E_INVALIDARG;
465
466   std::string name_str = target->name();
467
468   // If the name is empty, see if it's labeled by another element.
469   if (name_str.empty()) {
470     int title_elem_id;
471     if (target->GetIntAttribute(ui::AX_ATTR_TITLE_UI_ELEMENT,
472                                 &title_elem_id)) {
473       BrowserAccessibility* title_elem =
474           manager()->GetFromRendererID(title_elem_id);
475       if (title_elem)
476         name_str = title_elem->GetTextRecursive();
477     }
478   }
479
480   if (name_str.empty())
481     return S_FALSE;
482
483   *name = SysAllocString(base::UTF8ToUTF16(name_str).c_str());
484
485   DCHECK(*name);
486   return S_OK;
487 }
488
489 STDMETHODIMP BrowserAccessibilityWin::get_accParent(IDispatch** disp_parent) {
490   if (!instance_active())
491     return E_FAIL;
492
493   if (!disp_parent)
494     return E_INVALIDARG;
495
496   IAccessible* parent_obj = parent()->ToBrowserAccessibilityWin();
497   if (parent_obj == NULL) {
498     // This happens if we're the root of the tree;
499     // return the IAccessible for the window.
500     parent_obj =
501          manager()->ToBrowserAccessibilityManagerWin()->parent_iaccessible();
502
503     // |parent| can only be NULL if the manager was created before the parent
504     // IAccessible was known and it wasn't subsequently set before a client
505     // requested it. Crash hard if this happens so that we get crash reports.
506     CHECK(parent_obj);
507   }
508
509   parent_obj->AddRef();
510   *disp_parent = parent_obj;
511   return S_OK;
512 }
513
514 STDMETHODIMP BrowserAccessibilityWin::get_accRole(VARIANT var_id,
515                                                   VARIANT* role) {
516   if (!instance_active())
517     return E_FAIL;
518
519   if (!role)
520     return E_INVALIDARG;
521
522   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
523   if (!target)
524     return E_INVALIDARG;
525
526   if (!target->role_name_.empty()) {
527     role->vt = VT_BSTR;
528     role->bstrVal = SysAllocString(target->role_name_.c_str());
529   } else {
530     role->vt = VT_I4;
531     role->lVal = target->ia_role_;
532   }
533   return S_OK;
534 }
535
536 STDMETHODIMP BrowserAccessibilityWin::get_accState(VARIANT var_id,
537                                                    VARIANT* state) {
538   if (!instance_active())
539     return E_FAIL;
540
541   if (!state)
542     return E_INVALIDARG;
543
544   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
545   if (!target)
546     return E_INVALIDARG;
547
548   state->vt = VT_I4;
549   state->lVal = target->ia_state_;
550   if (manager()->GetFocus(NULL) == this)
551     state->lVal |= STATE_SYSTEM_FOCUSED;
552
553   return S_OK;
554 }
555
556 STDMETHODIMP BrowserAccessibilityWin::get_accValue(VARIANT var_id,
557                                                    BSTR* value) {
558   if (!instance_active())
559     return E_FAIL;
560
561   if (!value)
562     return E_INVALIDARG;
563
564   BrowserAccessibilityWin* target = GetTargetFromChildID(var_id);
565   if (!target)
566     return E_INVALIDARG;
567
568   if (target->ia_role() == ROLE_SYSTEM_PROGRESSBAR ||
569       target->ia_role() == ROLE_SYSTEM_SCROLLBAR ||
570       target->ia_role() == ROLE_SYSTEM_SLIDER) {
571     base::string16 value_text = target->GetValueText();
572     *value = SysAllocString(value_text.c_str());
573     DCHECK(*value);
574     return S_OK;
575   }
576
577   // Expose color well value.
578   if (target->ia2_role() == IA2_ROLE_COLOR_CHOOSER) {
579     int r = target->GetIntAttribute(
580         ui::AX_ATTR_COLOR_VALUE_RED);
581     int g = target->GetIntAttribute(
582         ui::AX_ATTR_COLOR_VALUE_GREEN);
583     int b = target->GetIntAttribute(
584         ui::AX_ATTR_COLOR_VALUE_BLUE);
585     base::string16 value_text;
586     value_text = base::IntToString16((r * 100) / 255) + L"% red " +
587                  base::IntToString16((g * 100) / 255) + L"% green " +
588                  base::IntToString16((b * 100) / 255) + L"% blue";
589     *value = SysAllocString(value_text.c_str());
590     DCHECK(*value);
591     return S_OK;
592   }
593
594   *value = SysAllocString(base::UTF8ToUTF16(target->value()).c_str());
595   DCHECK(*value);
596   return S_OK;
597 }
598
599 STDMETHODIMP BrowserAccessibilityWin::get_accHelpTopic(BSTR* help_file,
600                                                        VARIANT var_id,
601                                                        LONG* topic_id) {
602   return E_NOTIMPL;
603 }
604
605 STDMETHODIMP BrowserAccessibilityWin::get_accSelection(VARIANT* selected) {
606   if (!instance_active())
607     return E_FAIL;
608
609   if (blink_role() != ui::AX_ROLE_LIST_BOX)
610     return E_NOTIMPL;
611
612   unsigned long selected_count = 0;
613   for (size_t i = 0; i < children().size(); ++i) {
614     if (children()[i]->HasState(ui::AX_STATE_SELECTED))
615       ++selected_count;
616   }
617
618   if (selected_count == 0) {
619     selected->vt = VT_EMPTY;
620     return S_OK;
621   }
622
623   if (selected_count == 1) {
624     for (size_t i = 0; i < children().size(); ++i) {
625       if (children()[i]->HasState(ui::AX_STATE_SELECTED)) {
626         selected->vt = VT_DISPATCH;
627         selected->pdispVal =
628             children()[i]->ToBrowserAccessibilityWin()->NewReference();
629         return S_OK;
630       }
631     }
632   }
633
634   // Multiple items are selected.
635   base::win::EnumVariant* enum_variant =
636       new base::win::EnumVariant(selected_count);
637   enum_variant->AddRef();
638   unsigned long index = 0;
639   for (size_t i = 0; i < children().size(); ++i) {
640     if (children()[i]->HasState(ui::AX_STATE_SELECTED)) {
641       enum_variant->ItemAt(index)->vt = VT_DISPATCH;
642       enum_variant->ItemAt(index)->pdispVal =
643         children()[i]->ToBrowserAccessibilityWin()->NewReference();
644       ++index;
645     }
646   }
647   selected->vt = VT_UNKNOWN;
648   selected->punkVal = static_cast<IUnknown*>(
649       static_cast<base::win::IUnknownImpl*>(enum_variant));
650   return S_OK;
651 }
652
653 STDMETHODIMP BrowserAccessibilityWin::accSelect(
654     LONG flags_sel, VARIANT var_id) {
655   if (!instance_active())
656     return E_FAIL;
657
658   if (flags_sel & SELFLAG_TAKEFOCUS) {
659     manager()->SetFocus(this, true);
660     return S_OK;
661   }
662
663   return S_FALSE;
664 }
665
666 //
667 // IAccessible2 methods.
668 //
669
670 STDMETHODIMP BrowserAccessibilityWin::role(LONG* role) {
671   if (!instance_active())
672     return E_FAIL;
673
674   if (!role)
675     return E_INVALIDARG;
676
677   *role = ia2_role_;
678
679   return S_OK;
680 }
681
682 STDMETHODIMP BrowserAccessibilityWin::get_attributes(BSTR* attributes) {
683   if (!instance_active())
684     return E_FAIL;
685
686   if (!attributes)
687     return E_INVALIDARG;
688
689   // The iaccessible2 attributes are a set of key-value pairs
690   // separated by semicolons, with a colon between the key and the value.
691   base::string16 str;
692   for (unsigned int i = 0; i < ia2_attributes_.size(); ++i) {
693     if (i != 0)
694       str += L';';
695     str += ia2_attributes_[i];
696   }
697
698   if (str.empty())
699     return S_FALSE;
700
701   *attributes = SysAllocString(str.c_str());
702   DCHECK(*attributes);
703   return S_OK;
704 }
705
706 STDMETHODIMP BrowserAccessibilityWin::get_states(AccessibleStates* states) {
707   if (!instance_active())
708     return E_FAIL;
709
710   if (!states)
711     return E_INVALIDARG;
712
713   *states = ia2_state_;
714
715   return S_OK;
716 }
717
718 STDMETHODIMP BrowserAccessibilityWin::get_uniqueID(LONG* unique_id) {
719   if (!instance_active())
720     return E_FAIL;
721
722   if (!unique_id)
723     return E_INVALIDARG;
724
725   *unique_id = unique_id_win_;
726   return S_OK;
727 }
728
729 STDMETHODIMP BrowserAccessibilityWin::get_windowHandle(HWND* window_handle) {
730   if (!instance_active())
731     return E_FAIL;
732
733   if (!window_handle)
734     return E_INVALIDARG;
735
736   *window_handle = manager()->ToBrowserAccessibilityManagerWin()->parent_hwnd();
737   return S_OK;
738 }
739
740 STDMETHODIMP BrowserAccessibilityWin::get_indexInParent(LONG* index_in_parent) {
741   if (!instance_active())
742     return E_FAIL;
743
744   if (!index_in_parent)
745     return E_INVALIDARG;
746
747   *index_in_parent = this->index_in_parent();
748   return S_OK;
749 }
750
751 STDMETHODIMP BrowserAccessibilityWin::get_nRelations(LONG* n_relations) {
752   if (!instance_active())
753     return E_FAIL;
754
755   if (!n_relations)
756     return E_INVALIDARG;
757
758   *n_relations = relations_.size();
759   return S_OK;
760 }
761
762 STDMETHODIMP BrowserAccessibilityWin::get_relation(
763     LONG relation_index,
764     IAccessibleRelation** relation) {
765   if (!instance_active())
766     return E_FAIL;
767
768   if (relation_index < 0 ||
769       relation_index >= static_cast<long>(relations_.size())) {
770     return E_INVALIDARG;
771   }
772
773   if (!relation)
774     return E_INVALIDARG;
775
776   relations_[relation_index]->AddRef();
777   *relation = relations_[relation_index];
778   return S_OK;
779 }
780
781 STDMETHODIMP BrowserAccessibilityWin::get_relations(
782     LONG max_relations,
783     IAccessibleRelation** relations,
784     LONG* n_relations) {
785   if (!instance_active())
786     return E_FAIL;
787
788   if (!relations || !n_relations)
789     return E_INVALIDARG;
790
791   long count = static_cast<long>(relations_.size());
792   *n_relations = count;
793   if (count == 0)
794     return S_FALSE;
795
796   for (long i = 0; i < count; ++i) {
797     relations_[i]->AddRef();
798     relations[i] = relations_[i];
799   }
800
801   return S_OK;
802 }
803
804 STDMETHODIMP BrowserAccessibilityWin::scrollTo(enum IA2ScrollType scroll_type) {
805   if (!instance_active())
806     return E_FAIL;
807
808   gfx::Rect r = location();
809   switch(scroll_type) {
810     case IA2_SCROLL_TYPE_TOP_LEFT:
811       manager()->ScrollToMakeVisible(*this, gfx::Rect(r.x(), r.y(), 0, 0));
812       break;
813     case IA2_SCROLL_TYPE_BOTTOM_RIGHT:
814       manager()->ScrollToMakeVisible(
815           *this, gfx::Rect(r.right(), r.bottom(), 0, 0));
816       break;
817     case IA2_SCROLL_TYPE_TOP_EDGE:
818       manager()->ScrollToMakeVisible(
819           *this, gfx::Rect(r.x(), r.y(), r.width(), 0));
820       break;
821     case IA2_SCROLL_TYPE_BOTTOM_EDGE:
822       manager()->ScrollToMakeVisible(
823           *this, gfx::Rect(r.x(), r.bottom(), r.width(), 0));
824     break;
825     case IA2_SCROLL_TYPE_LEFT_EDGE:
826       manager()->ScrollToMakeVisible(
827           *this, gfx::Rect(r.x(), r.y(), 0, r.height()));
828       break;
829     case IA2_SCROLL_TYPE_RIGHT_EDGE:
830       manager()->ScrollToMakeVisible(
831           *this, gfx::Rect(r.right(), r.y(), 0, r.height()));
832       break;
833     case IA2_SCROLL_TYPE_ANYWHERE:
834     default:
835       manager()->ScrollToMakeVisible(*this, r);
836       break;
837   }
838
839   manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
840
841   return S_OK;
842 }
843
844 STDMETHODIMP BrowserAccessibilityWin::scrollToPoint(
845     enum IA2CoordinateType coordinate_type,
846     LONG x,
847     LONG y) {
848   if (!instance_active())
849     return E_FAIL;
850
851   gfx::Point scroll_to(x, y);
852
853   if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
854     scroll_to -= manager()->GetViewBounds().OffsetFromOrigin();
855   } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
856     if (parent())
857       scroll_to += parent()->location().OffsetFromOrigin();
858   } else {
859     return E_INVALIDARG;
860   }
861
862   manager()->ScrollToPoint(*this, scroll_to);
863   manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
864
865   return S_OK;
866 }
867
868 STDMETHODIMP BrowserAccessibilityWin::get_groupPosition(
869     LONG* group_level,
870     LONG* similar_items_in_group,
871     LONG* position_in_group) {
872   if (!instance_active())
873     return E_FAIL;
874
875   if (!group_level || !similar_items_in_group || !position_in_group)
876     return E_INVALIDARG;
877
878   if (blink_role() == ui::AX_ROLE_LIST_BOX_OPTION &&
879       parent() &&
880       parent()->role() == ui::AX_ROLE_LIST_BOX) {
881     *group_level = 0;
882     *similar_items_in_group = parent()->PlatformChildCount();
883     *position_in_group = index_in_parent() + 1;
884     return S_OK;
885   }
886
887   return E_NOTIMPL;
888 }
889
890 //
891 // IAccessibleApplication methods.
892 //
893
894 STDMETHODIMP BrowserAccessibilityWin::get_appName(BSTR* app_name) {
895   // No need to check |instance_active()| because this interface is
896   // global, and doesn't depend on any local state.
897
898   if (!app_name)
899     return E_INVALIDARG;
900
901   // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out
902   // the part before the "/".
903   std::vector<std::string> product_components;
904   base::SplitString(GetContentClient()->GetProduct(), '/', &product_components);
905   DCHECK_EQ(2U, product_components.size());
906   if (product_components.size() != 2)
907     return E_FAIL;
908   *app_name = SysAllocString(base::UTF8ToUTF16(product_components[0]).c_str());
909   DCHECK(*app_name);
910   return *app_name ? S_OK : E_FAIL;
911 }
912
913 STDMETHODIMP BrowserAccessibilityWin::get_appVersion(BSTR* app_version) {
914   // No need to check |instance_active()| because this interface is
915   // global, and doesn't depend on any local state.
916
917   if (!app_version)
918     return E_INVALIDARG;
919
920   // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out
921   // the part after the "/".
922   std::vector<std::string> product_components;
923   base::SplitString(GetContentClient()->GetProduct(), '/', &product_components);
924   DCHECK_EQ(2U, product_components.size());
925   if (product_components.size() != 2)
926     return E_FAIL;
927   *app_version =
928       SysAllocString(base::UTF8ToUTF16(product_components[1]).c_str());
929   DCHECK(*app_version);
930   return *app_version ? S_OK : E_FAIL;
931 }
932
933 STDMETHODIMP BrowserAccessibilityWin::get_toolkitName(BSTR* toolkit_name) {
934   // No need to check |instance_active()| because this interface is
935   // global, and doesn't depend on any local state.
936
937   if (!toolkit_name)
938     return E_INVALIDARG;
939
940   // This is hard-coded; all products based on the Chromium engine
941   // will have the same toolkit name, so that assistive technology can
942   // detect any Chrome-based product.
943   *toolkit_name = SysAllocString(L"Chrome");
944   DCHECK(*toolkit_name);
945   return *toolkit_name ? S_OK : E_FAIL;
946 }
947
948 STDMETHODIMP BrowserAccessibilityWin::get_toolkitVersion(
949     BSTR* toolkit_version) {
950   // No need to check |instance_active()| because this interface is
951   // global, and doesn't depend on any local state.
952
953   if (!toolkit_version)
954     return E_INVALIDARG;
955
956   std::string user_agent = GetContentClient()->GetUserAgent();
957   *toolkit_version = SysAllocString(base::UTF8ToUTF16(user_agent).c_str());
958   DCHECK(*toolkit_version);
959   return *toolkit_version ? S_OK : E_FAIL;
960 }
961
962 //
963 // IAccessibleImage methods.
964 //
965
966 STDMETHODIMP BrowserAccessibilityWin::get_description(BSTR* desc) {
967   if (!instance_active())
968     return E_FAIL;
969
970   if (!desc)
971     return E_INVALIDARG;
972
973   return GetStringAttributeAsBstr(
974       ui::AX_ATTR_DESCRIPTION, desc);
975 }
976
977 STDMETHODIMP BrowserAccessibilityWin::get_imagePosition(
978     enum IA2CoordinateType coordinate_type,
979     LONG* x,
980     LONG* y) {
981   if (!instance_active())
982     return E_FAIL;
983
984   if (!x || !y)
985     return E_INVALIDARG;
986
987   if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
988     HWND parent_hwnd =
989         manager()->ToBrowserAccessibilityManagerWin()->parent_hwnd();
990     POINT top_left = {0, 0};
991     ::ClientToScreen(parent_hwnd, &top_left);
992     *x = location().x() + top_left.x;
993     *y = location().y() + top_left.y;
994   } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
995     *x = location().x();
996     *y = location().y();
997     if (parent()) {
998       *x -= parent()->location().x();
999       *y -= parent()->location().y();
1000     }
1001   } else {
1002     return E_INVALIDARG;
1003   }
1004
1005   return S_OK;
1006 }
1007
1008 STDMETHODIMP BrowserAccessibilityWin::get_imageSize(LONG* height, LONG* width) {
1009   if (!instance_active())
1010     return E_FAIL;
1011
1012   if (!height || !width)
1013     return E_INVALIDARG;
1014
1015   *height = location().height();
1016   *width = location().width();
1017   return S_OK;
1018 }
1019
1020 //
1021 // IAccessibleTable methods.
1022 //
1023
1024 STDMETHODIMP BrowserAccessibilityWin::get_accessibleAt(
1025     long row,
1026     long column,
1027     IUnknown** accessible) {
1028   if (!instance_active())
1029     return E_FAIL;
1030
1031   if (!accessible)
1032     return E_INVALIDARG;
1033
1034   int columns;
1035   int rows;
1036   if (!GetIntAttribute(
1037           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1038       !GetIntAttribute(
1039           ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1040       columns <= 0 ||
1041       rows <= 0) {
1042     return S_FALSE;
1043   }
1044
1045   if (row < 0 || row >= rows || column < 0 || column >= columns)
1046     return E_INVALIDARG;
1047
1048   const std::vector<int32>& cell_ids = GetIntListAttribute(
1049       ui::AX_ATTR_CELL_IDS);
1050   DCHECK_EQ(columns * rows, static_cast<int>(cell_ids.size()));
1051
1052   int cell_id = cell_ids[row * columns + column];
1053   BrowserAccessibilityWin* cell = GetFromRendererID(cell_id);
1054   if (cell) {
1055     *accessible = static_cast<IAccessible*>(cell->NewReference());
1056     return S_OK;
1057   }
1058
1059   *accessible = NULL;
1060   return E_INVALIDARG;
1061 }
1062
1063 STDMETHODIMP BrowserAccessibilityWin::get_caption(IUnknown** accessible) {
1064   if (!instance_active())
1065     return E_FAIL;
1066
1067   if (!accessible)
1068     return E_INVALIDARG;
1069
1070   // TODO(dmazzoni): implement
1071   return S_FALSE;
1072 }
1073
1074 STDMETHODIMP BrowserAccessibilityWin::get_childIndex(long row,
1075                                                      long column,
1076                                                      long* cell_index) {
1077   if (!instance_active())
1078     return E_FAIL;
1079
1080   if (!cell_index)
1081     return E_INVALIDARG;
1082
1083   int columns;
1084   int rows;
1085   if (!GetIntAttribute(
1086           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1087       !GetIntAttribute(
1088           ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1089       columns <= 0 ||
1090       rows <= 0) {
1091     return S_FALSE;
1092   }
1093
1094   if (row < 0 || row >= rows || column < 0 || column >= columns)
1095     return E_INVALIDARG;
1096
1097   const std::vector<int32>& cell_ids = GetIntListAttribute(
1098       ui::AX_ATTR_CELL_IDS);
1099   const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1100       ui::AX_ATTR_UNIQUE_CELL_IDS);
1101   DCHECK_EQ(columns * rows, static_cast<int>(cell_ids.size()));
1102   int cell_id = cell_ids[row * columns + column];
1103   for (size_t i = 0; i < unique_cell_ids.size(); ++i) {
1104     if (unique_cell_ids[i] == cell_id) {
1105       *cell_index = (long)i;
1106       return S_OK;
1107     }
1108   }
1109
1110   return S_FALSE;
1111 }
1112
1113 STDMETHODIMP BrowserAccessibilityWin::get_columnDescription(long column,
1114                                                             BSTR* description) {
1115   if (!instance_active())
1116     return E_FAIL;
1117
1118   if (!description)
1119     return E_INVALIDARG;
1120
1121   int columns;
1122   int rows;
1123   if (!GetIntAttribute(
1124           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1125       !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1126       columns <= 0 ||
1127       rows <= 0) {
1128     return S_FALSE;
1129   }
1130
1131   if (column < 0 || column >= columns)
1132     return E_INVALIDARG;
1133
1134   const std::vector<int32>& cell_ids = GetIntListAttribute(
1135       ui::AX_ATTR_CELL_IDS);
1136   for (int i = 0; i < rows; ++i) {
1137     int cell_id = cell_ids[i * columns + column];
1138     BrowserAccessibilityWin* cell = static_cast<BrowserAccessibilityWin*>(
1139         manager()->GetFromRendererID(cell_id));
1140     if (cell && cell->blink_role() == ui::AX_ROLE_COLUMN_HEADER) {
1141       base::string16 cell_name = cell->GetString16Attribute(
1142           ui::AX_ATTR_NAME);
1143       if (cell_name.size() > 0) {
1144         *description = SysAllocString(cell_name.c_str());
1145         return S_OK;
1146       }
1147
1148       return cell->GetStringAttributeAsBstr(
1149           ui::AX_ATTR_DESCRIPTION, description);
1150     }
1151   }
1152
1153   return S_FALSE;
1154 }
1155
1156 STDMETHODIMP BrowserAccessibilityWin::get_columnExtentAt(
1157     long row,
1158     long column,
1159     long* n_columns_spanned) {
1160   if (!instance_active())
1161     return E_FAIL;
1162
1163   if (!n_columns_spanned)
1164     return E_INVALIDARG;
1165
1166   int columns;
1167   int rows;
1168   if (!GetIntAttribute(
1169           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1170       !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1171       columns <= 0 ||
1172       rows <= 0) {
1173     return S_FALSE;
1174   }
1175
1176   if (row < 0 || row >= rows || column < 0 || column >= columns)
1177     return E_INVALIDARG;
1178
1179   const std::vector<int32>& cell_ids = GetIntListAttribute(
1180       ui::AX_ATTR_CELL_IDS);
1181   int cell_id = cell_ids[row * columns + column];
1182   BrowserAccessibilityWin* cell = static_cast<BrowserAccessibilityWin*>(
1183       manager()->GetFromRendererID(cell_id));
1184   int colspan;
1185   if (cell &&
1186       cell->GetIntAttribute(
1187           ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1188       colspan >= 1) {
1189     *n_columns_spanned = colspan;
1190     return S_OK;
1191   }
1192
1193   return S_FALSE;
1194 }
1195
1196 STDMETHODIMP BrowserAccessibilityWin::get_columnHeader(
1197     IAccessibleTable** accessible_table,
1198     long* starting_row_index) {
1199   // TODO(dmazzoni): implement
1200   return E_NOTIMPL;
1201 }
1202
1203 STDMETHODIMP BrowserAccessibilityWin::get_columnIndex(long cell_index,
1204                                                       long* column_index) {
1205   if (!instance_active())
1206     return E_FAIL;
1207
1208   if (!column_index)
1209     return E_INVALIDARG;
1210
1211   const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1212       ui::AX_ATTR_UNIQUE_CELL_IDS);
1213   int cell_id_count = static_cast<int>(unique_cell_ids.size());
1214   if (cell_index < 0)
1215     return E_INVALIDARG;
1216   if (cell_index >= cell_id_count)
1217     return S_FALSE;
1218
1219   int cell_id = unique_cell_ids[cell_index];
1220   BrowserAccessibilityWin* cell =
1221       manager()->GetFromRendererID(cell_id)->ToBrowserAccessibilityWin();
1222   int col_index;
1223   if (cell &&
1224       cell->GetIntAttribute(
1225           ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &col_index)) {
1226     *column_index = col_index;
1227     return S_OK;
1228   }
1229
1230   return S_FALSE;
1231 }
1232
1233 STDMETHODIMP BrowserAccessibilityWin::get_nColumns(long* column_count) {
1234   if (!instance_active())
1235     return E_FAIL;
1236
1237   if (!column_count)
1238     return E_INVALIDARG;
1239
1240   int columns;
1241   if (GetIntAttribute(
1242           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns)) {
1243     *column_count = columns;
1244     return S_OK;
1245   }
1246
1247   return S_FALSE;
1248 }
1249
1250 STDMETHODIMP BrowserAccessibilityWin::get_nRows(long* row_count) {
1251   if (!instance_active())
1252     return E_FAIL;
1253
1254   if (!row_count)
1255     return E_INVALIDARG;
1256
1257   int rows;
1258   if (GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1259     *row_count = rows;
1260     return S_OK;
1261   }
1262
1263   return S_FALSE;
1264 }
1265
1266 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedChildren(long* cell_count) {
1267   if (!instance_active())
1268     return E_FAIL;
1269
1270   if (!cell_count)
1271     return E_INVALIDARG;
1272
1273   // TODO(dmazzoni): add support for selected cells/rows/columns in tables.
1274   *cell_count = 0;
1275   return S_OK;
1276 }
1277
1278 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedColumns(long* column_count) {
1279   if (!instance_active())
1280     return E_FAIL;
1281
1282   if (!column_count)
1283     return E_INVALIDARG;
1284
1285   *column_count = 0;
1286   return S_OK;
1287 }
1288
1289 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedRows(long* row_count) {
1290   if (!instance_active())
1291     return E_FAIL;
1292
1293   if (!row_count)
1294     return E_INVALIDARG;
1295
1296   *row_count = 0;
1297   return S_OK;
1298 }
1299
1300 STDMETHODIMP BrowserAccessibilityWin::get_rowDescription(long row,
1301                                                          BSTR* description) {
1302   if (!instance_active())
1303     return E_FAIL;
1304
1305   if (!description)
1306     return E_INVALIDARG;
1307
1308   int columns;
1309   int rows;
1310   if (!GetIntAttribute(
1311           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1312       !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1313       columns <= 0 ||
1314       rows <= 0) {
1315     return S_FALSE;
1316   }
1317
1318   if (row < 0 || row >= rows)
1319     return E_INVALIDARG;
1320
1321   const std::vector<int32>& cell_ids = GetIntListAttribute(
1322       ui::AX_ATTR_CELL_IDS);
1323   for (int i = 0; i < columns; ++i) {
1324     int cell_id = cell_ids[row * columns + i];
1325     BrowserAccessibilityWin* cell =
1326         manager()->GetFromRendererID(cell_id)->ToBrowserAccessibilityWin();
1327     if (cell && cell->blink_role() == ui::AX_ROLE_ROW_HEADER) {
1328       base::string16 cell_name = cell->GetString16Attribute(
1329           ui::AX_ATTR_NAME);
1330       if (cell_name.size() > 0) {
1331         *description = SysAllocString(cell_name.c_str());
1332         return S_OK;
1333       }
1334
1335       return cell->GetStringAttributeAsBstr(
1336           ui::AX_ATTR_DESCRIPTION, description);
1337     }
1338   }
1339
1340   return S_FALSE;
1341 }
1342
1343 STDMETHODIMP BrowserAccessibilityWin::get_rowExtentAt(long row,
1344                                                       long column,
1345                                                       long* n_rows_spanned) {
1346   if (!instance_active())
1347     return E_FAIL;
1348
1349   if (!n_rows_spanned)
1350     return E_INVALIDARG;
1351
1352   int columns;
1353   int rows;
1354   if (!GetIntAttribute(
1355           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1356       !GetIntAttribute(ui::AX_ATTR_TABLE_ROW_COUNT, &rows) ||
1357       columns <= 0 ||
1358       rows <= 0) {
1359     return S_FALSE;
1360   }
1361
1362   if (row < 0 || row >= rows || column < 0 || column >= columns)
1363     return E_INVALIDARG;
1364
1365   const std::vector<int32>& cell_ids = GetIntListAttribute(
1366       ui::AX_ATTR_CELL_IDS);
1367   int cell_id = cell_ids[row * columns + column];
1368   BrowserAccessibilityWin* cell =
1369       manager()->GetFromRendererID(cell_id)->ToBrowserAccessibilityWin();
1370   int rowspan;
1371   if (cell &&
1372       cell->GetIntAttribute(
1373           ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1374       rowspan >= 1) {
1375     *n_rows_spanned = rowspan;
1376     return S_OK;
1377   }
1378
1379   return S_FALSE;
1380 }
1381
1382 STDMETHODIMP BrowserAccessibilityWin::get_rowHeader(
1383     IAccessibleTable** accessible_table,
1384     long* starting_column_index) {
1385   // TODO(dmazzoni): implement
1386   return E_NOTIMPL;
1387 }
1388
1389 STDMETHODIMP BrowserAccessibilityWin::get_rowIndex(long cell_index,
1390                                                    long* row_index) {
1391   if (!instance_active())
1392     return E_FAIL;
1393
1394   if (!row_index)
1395     return E_INVALIDARG;
1396
1397   const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1398       ui::AX_ATTR_UNIQUE_CELL_IDS);
1399   int cell_id_count = static_cast<int>(unique_cell_ids.size());
1400   if (cell_index < 0)
1401     return E_INVALIDARG;
1402   if (cell_index >= cell_id_count)
1403     return S_FALSE;
1404
1405   int cell_id = unique_cell_ids[cell_index];
1406   BrowserAccessibilityWin* cell =
1407       manager()->GetFromRendererID(cell_id)->ToBrowserAccessibilityWin();
1408   int cell_row_index;
1409   if (cell &&
1410       cell->GetIntAttribute(
1411           ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &cell_row_index)) {
1412     *row_index = cell_row_index;
1413     return S_OK;
1414   }
1415
1416   return S_FALSE;
1417 }
1418
1419 STDMETHODIMP BrowserAccessibilityWin::get_selectedChildren(long max_children,
1420                                                            long** children,
1421                                                            long* n_children) {
1422   if (!instance_active())
1423     return E_FAIL;
1424
1425   if (!children || !n_children)
1426     return E_INVALIDARG;
1427
1428   // TODO(dmazzoni): Implement this.
1429   *n_children = 0;
1430   return S_OK;
1431 }
1432
1433 STDMETHODIMP BrowserAccessibilityWin::get_selectedColumns(long max_columns,
1434                                                           long** columns,
1435                                                           long* n_columns) {
1436   if (!instance_active())
1437     return E_FAIL;
1438
1439   if (!columns || !n_columns)
1440     return E_INVALIDARG;
1441
1442   // TODO(dmazzoni): Implement this.
1443   *n_columns = 0;
1444   return S_OK;
1445 }
1446
1447 STDMETHODIMP BrowserAccessibilityWin::get_selectedRows(long max_rows,
1448                                                        long** rows,
1449                                                        long* n_rows) {
1450   if (!instance_active())
1451     return E_FAIL;
1452
1453   if (!rows || !n_rows)
1454     return E_INVALIDARG;
1455
1456   // TODO(dmazzoni): Implement this.
1457   *n_rows = 0;
1458   return S_OK;
1459 }
1460
1461 STDMETHODIMP BrowserAccessibilityWin::get_summary(IUnknown** accessible) {
1462   if (!instance_active())
1463     return E_FAIL;
1464
1465   if (!accessible)
1466     return E_INVALIDARG;
1467
1468   // TODO(dmazzoni): implement
1469   return S_FALSE;
1470 }
1471
1472 STDMETHODIMP BrowserAccessibilityWin::get_isColumnSelected(
1473     long column,
1474     boolean* is_selected) {
1475   if (!instance_active())
1476     return E_FAIL;
1477
1478   if (!is_selected)
1479     return E_INVALIDARG;
1480
1481   // TODO(dmazzoni): Implement this.
1482   *is_selected = false;
1483   return S_OK;
1484 }
1485
1486 STDMETHODIMP BrowserAccessibilityWin::get_isRowSelected(long row,
1487                                                         boolean* is_selected) {
1488   if (!instance_active())
1489     return E_FAIL;
1490
1491   if (!is_selected)
1492     return E_INVALIDARG;
1493
1494   // TODO(dmazzoni): Implement this.
1495   *is_selected = false;
1496   return S_OK;
1497 }
1498
1499 STDMETHODIMP BrowserAccessibilityWin::get_isSelected(long row,
1500                                                      long column,
1501                                                      boolean* is_selected) {
1502   if (!instance_active())
1503     return E_FAIL;
1504
1505   if (!is_selected)
1506     return E_INVALIDARG;
1507
1508   // TODO(dmazzoni): Implement this.
1509   *is_selected = false;
1510   return S_OK;
1511 }
1512
1513 STDMETHODIMP BrowserAccessibilityWin::get_rowColumnExtentsAtIndex(
1514     long index,
1515     long* row,
1516     long* column,
1517     long* row_extents,
1518     long* column_extents,
1519     boolean* is_selected) {
1520   if (!instance_active())
1521     return E_FAIL;
1522
1523   if (!row || !column || !row_extents || !column_extents || !is_selected)
1524     return E_INVALIDARG;
1525
1526   const std::vector<int32>& unique_cell_ids = GetIntListAttribute(
1527       ui::AX_ATTR_UNIQUE_CELL_IDS);
1528   int cell_id_count = static_cast<int>(unique_cell_ids.size());
1529   if (index < 0)
1530     return E_INVALIDARG;
1531   if (index >= cell_id_count)
1532     return S_FALSE;
1533
1534   int cell_id = unique_cell_ids[index];
1535   BrowserAccessibilityWin* cell =
1536       manager()->GetFromRendererID(cell_id)->ToBrowserAccessibilityWin();
1537   int rowspan;
1538   int colspan;
1539   if (cell &&
1540       cell->GetIntAttribute(
1541           ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1542       cell->GetIntAttribute(
1543           ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1544       rowspan >= 1 &&
1545       colspan >= 1) {
1546     *row_extents = rowspan;
1547     *column_extents = colspan;
1548     return S_OK;
1549   }
1550
1551   return S_FALSE;
1552 }
1553
1554 //
1555 // IAccessibleTable2 methods.
1556 //
1557
1558 STDMETHODIMP BrowserAccessibilityWin::get_cellAt(long row,
1559                                                  long column,
1560                                                  IUnknown** cell) {
1561   return get_accessibleAt(row, column, cell);
1562 }
1563
1564 STDMETHODIMP BrowserAccessibilityWin::get_nSelectedCells(long* cell_count) {
1565   return get_nSelectedChildren(cell_count);
1566 }
1567
1568 STDMETHODIMP BrowserAccessibilityWin::get_selectedCells(
1569     IUnknown*** cells,
1570     long* n_selected_cells) {
1571   if (!instance_active())
1572     return E_FAIL;
1573
1574   if (!cells || !n_selected_cells)
1575     return E_INVALIDARG;
1576
1577   // TODO(dmazzoni): Implement this.
1578   *n_selected_cells = 0;
1579   return S_OK;
1580 }
1581
1582 STDMETHODIMP BrowserAccessibilityWin::get_selectedColumns(long** columns,
1583                                                           long* n_columns) {
1584   if (!instance_active())
1585     return E_FAIL;
1586
1587   if (!columns || !n_columns)
1588     return E_INVALIDARG;
1589
1590   // TODO(dmazzoni): Implement this.
1591   *n_columns = 0;
1592   return S_OK;
1593 }
1594
1595 STDMETHODIMP BrowserAccessibilityWin::get_selectedRows(long** rows,
1596                                                        long* n_rows) {
1597   if (!instance_active())
1598     return E_FAIL;
1599
1600   if (!rows || !n_rows)
1601     return E_INVALIDARG;
1602
1603   // TODO(dmazzoni): Implement this.
1604   *n_rows = 0;
1605   return S_OK;
1606 }
1607
1608
1609 //
1610 // IAccessibleTableCell methods.
1611 //
1612
1613 STDMETHODIMP BrowserAccessibilityWin::get_columnExtent(
1614     long* n_columns_spanned) {
1615   if (!instance_active())
1616     return E_FAIL;
1617
1618   if (!n_columns_spanned)
1619     return E_INVALIDARG;
1620
1621   int colspan;
1622   if (GetIntAttribute(
1623           ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan) &&
1624       colspan >= 1) {
1625     *n_columns_spanned = colspan;
1626     return S_OK;
1627   }
1628
1629   return S_FALSE;
1630 }
1631
1632 STDMETHODIMP BrowserAccessibilityWin::get_columnHeaderCells(
1633     IUnknown*** cell_accessibles,
1634     long* n_column_header_cells) {
1635   if (!instance_active())
1636     return E_FAIL;
1637
1638   if (!cell_accessibles || !n_column_header_cells)
1639     return E_INVALIDARG;
1640
1641   *n_column_header_cells = 0;
1642
1643   int column;
1644   if (!GetIntAttribute(
1645           ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column)) {
1646     return S_FALSE;
1647   }
1648
1649   BrowserAccessibility* table = parent();
1650   while (table && table->role() != ui::AX_ROLE_TABLE)
1651     table = table->parent();
1652   if (!table) {
1653     NOTREACHED();
1654     return S_FALSE;
1655   }
1656
1657   int columns;
1658   int rows;
1659   if (!table->GetIntAttribute(
1660           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1661       !table->GetIntAttribute(
1662           ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1663     return S_FALSE;
1664   }
1665   if (columns <= 0 || rows <= 0 || column < 0 || column >= columns)
1666     return S_FALSE;
1667
1668   const std::vector<int32>& cell_ids = table->GetIntListAttribute(
1669       ui::AX_ATTR_CELL_IDS);
1670
1671   for (int i = 0; i < rows; ++i) {
1672     int cell_id = cell_ids[i * columns + column];
1673     BrowserAccessibilityWin* cell =
1674         manager()->GetFromRendererID(cell_id)->ToBrowserAccessibilityWin();
1675     if (cell && cell->blink_role() == ui::AX_ROLE_COLUMN_HEADER)
1676       (*n_column_header_cells)++;
1677   }
1678
1679   *cell_accessibles = static_cast<IUnknown**>(CoTaskMemAlloc(
1680       (*n_column_header_cells) * sizeof(cell_accessibles[0])));
1681   int index = 0;
1682   for (int i = 0; i < rows; ++i) {
1683     int cell_id = cell_ids[i * columns + column];
1684     BrowserAccessibility* cell = manager()->GetFromRendererID(cell_id);
1685     if (cell && cell->role() == ui::AX_ROLE_COLUMN_HEADER) {
1686       (*cell_accessibles)[index] = static_cast<IAccessible*>(
1687           cell->ToBrowserAccessibilityWin()->NewReference());
1688       ++index;
1689     }
1690   }
1691
1692   return S_OK;
1693 }
1694
1695 STDMETHODIMP BrowserAccessibilityWin::get_columnIndex(long* column_index) {
1696   if (!instance_active())
1697     return E_FAIL;
1698
1699   if (!column_index)
1700     return E_INVALIDARG;
1701
1702   int column;
1703   if (GetIntAttribute(
1704           ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column)) {
1705     *column_index = column;
1706     return S_OK;
1707   }
1708
1709   return S_FALSE;
1710 }
1711
1712 STDMETHODIMP BrowserAccessibilityWin::get_rowExtent(long* n_rows_spanned) {
1713   if (!instance_active())
1714     return E_FAIL;
1715
1716   if (!n_rows_spanned)
1717     return E_INVALIDARG;
1718
1719   int rowspan;
1720   if (GetIntAttribute(
1721           ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1722       rowspan >= 1) {
1723     *n_rows_spanned = rowspan;
1724     return S_OK;
1725   }
1726
1727   return S_FALSE;
1728 }
1729
1730 STDMETHODIMP BrowserAccessibilityWin::get_rowHeaderCells(
1731     IUnknown*** cell_accessibles,
1732     long* n_row_header_cells) {
1733   if (!instance_active())
1734     return E_FAIL;
1735
1736   if (!cell_accessibles || !n_row_header_cells)
1737     return E_INVALIDARG;
1738
1739   *n_row_header_cells = 0;
1740
1741   int row;
1742   if (!GetIntAttribute(
1743           ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row)) {
1744     return S_FALSE;
1745   }
1746
1747   BrowserAccessibility* table = parent();
1748   while (table && table->role() != ui::AX_ROLE_TABLE)
1749     table = table->parent();
1750   if (!table) {
1751     NOTREACHED();
1752     return S_FALSE;
1753   }
1754
1755   int columns;
1756   int rows;
1757   if (!table->GetIntAttribute(
1758           ui::AX_ATTR_TABLE_COLUMN_COUNT, &columns) ||
1759       !table->GetIntAttribute(
1760           ui::AX_ATTR_TABLE_ROW_COUNT, &rows)) {
1761     return S_FALSE;
1762   }
1763   if (columns <= 0 || rows <= 0 || row < 0 || row >= rows)
1764     return S_FALSE;
1765
1766   const std::vector<int32>& cell_ids = table->GetIntListAttribute(
1767       ui::AX_ATTR_CELL_IDS);
1768
1769   for (int i = 0; i < columns; ++i) {
1770     int cell_id = cell_ids[row * columns + i];
1771     BrowserAccessibility* cell = manager()->GetFromRendererID(cell_id);
1772     if (cell && cell->role() == ui::AX_ROLE_ROW_HEADER)
1773       (*n_row_header_cells)++;
1774   }
1775
1776   *cell_accessibles = static_cast<IUnknown**>(CoTaskMemAlloc(
1777       (*n_row_header_cells) * sizeof(cell_accessibles[0])));
1778   int index = 0;
1779   for (int i = 0; i < columns; ++i) {
1780     int cell_id = cell_ids[row * columns + i];
1781     BrowserAccessibility* cell = manager()->GetFromRendererID(cell_id);
1782     if (cell && cell->role() == ui::AX_ROLE_ROW_HEADER) {
1783       (*cell_accessibles)[index] = static_cast<IAccessible*>(
1784           cell->ToBrowserAccessibilityWin()->NewReference());
1785       ++index;
1786     }
1787   }
1788
1789   return S_OK;
1790 }
1791
1792 STDMETHODIMP BrowserAccessibilityWin::get_rowIndex(long* row_index) {
1793   if (!instance_active())
1794     return E_FAIL;
1795
1796   if (!row_index)
1797     return E_INVALIDARG;
1798
1799   int row;
1800   if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row)) {
1801     *row_index = row;
1802     return S_OK;
1803   }
1804   return S_FALSE;
1805 }
1806
1807 STDMETHODIMP BrowserAccessibilityWin::get_isSelected(boolean* is_selected) {
1808   if (!instance_active())
1809     return E_FAIL;
1810
1811   if (!is_selected)
1812     return E_INVALIDARG;
1813
1814   *is_selected = false;
1815   return S_OK;
1816 }
1817
1818 STDMETHODIMP BrowserAccessibilityWin::get_rowColumnExtents(
1819     long* row_index,
1820     long* column_index,
1821     long* row_extents,
1822     long* column_extents,
1823     boolean* is_selected) {
1824   if (!instance_active())
1825     return E_FAIL;
1826
1827   if (!row_index ||
1828       !column_index ||
1829       !row_extents ||
1830       !column_extents ||
1831       !is_selected) {
1832     return E_INVALIDARG;
1833   }
1834
1835   int row;
1836   int column;
1837   int rowspan;
1838   int colspan;
1839   if (GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row) &&
1840       GetIntAttribute(
1841           ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column) &&
1842       GetIntAttribute(
1843           ui::AX_ATTR_TABLE_CELL_ROW_SPAN, &rowspan) &&
1844       GetIntAttribute(
1845           ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN, &colspan)) {
1846     *row_index = row;
1847     *column_index = column;
1848     *row_extents = rowspan;
1849     *column_extents = colspan;
1850     *is_selected = false;
1851     return S_OK;
1852   }
1853
1854   return S_FALSE;
1855 }
1856
1857 STDMETHODIMP BrowserAccessibilityWin::get_table(IUnknown** table) {
1858   if (!instance_active())
1859     return E_FAIL;
1860
1861   if (!table)
1862     return E_INVALIDARG;
1863
1864
1865   int row;
1866   int column;
1867   GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX, &row);
1868   GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX, &column);
1869
1870   BrowserAccessibility* find_table = parent();
1871   while (find_table && find_table->role() != ui::AX_ROLE_TABLE)
1872     find_table = find_table->parent();
1873   if (!find_table) {
1874     NOTREACHED();
1875     return S_FALSE;
1876   }
1877
1878   *table = static_cast<IAccessibleTable*>(
1879       find_table->ToBrowserAccessibilityWin()->NewReference());
1880
1881   return S_OK;
1882 }
1883
1884 //
1885 // IAccessibleText methods.
1886 //
1887
1888 STDMETHODIMP BrowserAccessibilityWin::get_nCharacters(LONG* n_characters) {
1889   if (!instance_active())
1890     return E_FAIL;
1891
1892   if (!n_characters)
1893     return E_INVALIDARG;
1894
1895   *n_characters = TextForIAccessibleText().length();
1896   return S_OK;
1897 }
1898
1899 STDMETHODIMP BrowserAccessibilityWin::get_caretOffset(LONG* offset) {
1900   if (!instance_active())
1901     return E_FAIL;
1902
1903   if (!offset)
1904     return E_INVALIDARG;
1905
1906   *offset = 0;
1907   if (blink_role() == ui::AX_ROLE_TEXT_FIELD ||
1908       blink_role() == ui::AX_ROLE_TEXT_AREA) {
1909     int sel_start = 0;
1910     if (GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START,
1911                         &sel_start))
1912       *offset = sel_start;
1913   }
1914
1915   return S_OK;
1916 }
1917
1918 STDMETHODIMP BrowserAccessibilityWin::get_characterExtents(
1919     LONG offset,
1920     enum IA2CoordinateType coordinate_type,
1921     LONG* out_x,
1922     LONG* out_y,
1923     LONG* out_width,
1924     LONG* out_height) {
1925   if (!instance_active())
1926     return E_FAIL;
1927
1928   if (!out_x || !out_y || !out_width || !out_height)
1929     return E_INVALIDARG;
1930
1931   const base::string16& text_str = TextForIAccessibleText();
1932   HandleSpecialTextOffset(text_str, &offset);
1933
1934   if (offset < 0 || offset > static_cast<LONG>(text_str.size()))
1935     return E_INVALIDARG;
1936
1937   gfx::Rect character_bounds;
1938   if (coordinate_type == IA2_COORDTYPE_SCREEN_RELATIVE) {
1939     character_bounds = GetGlobalBoundsForRange(offset, 1);
1940   } else if (coordinate_type == IA2_COORDTYPE_PARENT_RELATIVE) {
1941     character_bounds = GetLocalBoundsForRange(offset, 1);
1942     character_bounds -= location().OffsetFromOrigin();
1943   } else {
1944     return E_INVALIDARG;
1945   }
1946
1947   *out_x = character_bounds.x();
1948   *out_y = character_bounds.y();
1949   *out_width = character_bounds.width();
1950   *out_height = character_bounds.height();
1951
1952   return S_OK;
1953 }
1954
1955 STDMETHODIMP BrowserAccessibilityWin::get_nSelections(LONG* n_selections) {
1956   if (!instance_active())
1957     return E_FAIL;
1958
1959   if (!n_selections)
1960     return E_INVALIDARG;
1961
1962   *n_selections = 0;
1963   if (blink_role() == ui::AX_ROLE_TEXT_FIELD ||
1964       blink_role() == ui::AX_ROLE_TEXT_AREA) {
1965     int sel_start = 0;
1966     int sel_end = 0;
1967     if (GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START,
1968                         &sel_start) &&
1969         GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end) &&
1970         sel_start != sel_end)
1971       *n_selections = 1;
1972   }
1973
1974   return S_OK;
1975 }
1976
1977 STDMETHODIMP BrowserAccessibilityWin::get_selection(LONG selection_index,
1978                                                     LONG* start_offset,
1979                                                     LONG* end_offset) {
1980   if (!instance_active())
1981     return E_FAIL;
1982
1983   if (!start_offset || !end_offset || selection_index != 0)
1984     return E_INVALIDARG;
1985
1986   *start_offset = 0;
1987   *end_offset = 0;
1988   if (blink_role() == ui::AX_ROLE_TEXT_FIELD ||
1989       blink_role() == ui::AX_ROLE_TEXT_AREA) {
1990     int sel_start = 0;
1991     int sel_end = 0;
1992     if (GetIntAttribute(
1993             ui::AX_ATTR_TEXT_SEL_START, &sel_start) &&
1994         GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end)) {
1995       *start_offset = sel_start;
1996       *end_offset = sel_end;
1997     }
1998   }
1999
2000   return S_OK;
2001 }
2002
2003 STDMETHODIMP BrowserAccessibilityWin::get_text(LONG start_offset,
2004                                                LONG end_offset,
2005                                                BSTR* text) {
2006   if (!instance_active())
2007     return E_FAIL;
2008
2009   if (!text)
2010     return E_INVALIDARG;
2011
2012   const base::string16& text_str = TextForIAccessibleText();
2013
2014   // Handle special text offsets.
2015   HandleSpecialTextOffset(text_str, &start_offset);
2016   HandleSpecialTextOffset(text_str, &end_offset);
2017
2018   // The spec allows the arguments to be reversed.
2019   if (start_offset > end_offset) {
2020     LONG tmp = start_offset;
2021     start_offset = end_offset;
2022     end_offset = tmp;
2023   }
2024
2025   // The spec does not allow the start or end offsets to be out or range;
2026   // we must return an error if so.
2027   LONG len = text_str.length();
2028   if (start_offset < 0)
2029     return E_INVALIDARG;
2030   if (end_offset > len)
2031     return E_INVALIDARG;
2032
2033   base::string16 substr = text_str.substr(start_offset,
2034                                           end_offset - start_offset);
2035   if (substr.empty())
2036     return S_FALSE;
2037
2038   *text = SysAllocString(substr.c_str());
2039   DCHECK(*text);
2040   return S_OK;
2041 }
2042
2043 STDMETHODIMP BrowserAccessibilityWin::get_textAtOffset(
2044     LONG offset,
2045     enum IA2TextBoundaryType boundary_type,
2046     LONG* start_offset,
2047     LONG* end_offset,
2048     BSTR* text) {
2049   if (!instance_active())
2050     return E_FAIL;
2051
2052   if (!start_offset || !end_offset || !text)
2053     return E_INVALIDARG;
2054
2055   // The IAccessible2 spec says we don't have to implement the "sentence"
2056   // boundary type, we can just let the screenreader handle it.
2057   if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2058     *start_offset = 0;
2059     *end_offset = 0;
2060     *text = NULL;
2061     return S_FALSE;
2062   }
2063
2064   const base::string16& text_str = TextForIAccessibleText();
2065
2066   *start_offset = FindBoundary(
2067       text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
2068   *end_offset = FindBoundary(
2069       text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
2070   return get_text(*start_offset, *end_offset, text);
2071 }
2072
2073 STDMETHODIMP BrowserAccessibilityWin::get_textBeforeOffset(
2074     LONG offset,
2075     enum IA2TextBoundaryType boundary_type,
2076     LONG* start_offset,
2077     LONG* end_offset,
2078     BSTR* text) {
2079   if (!instance_active())
2080     return E_FAIL;
2081
2082   if (!start_offset || !end_offset || !text)
2083     return E_INVALIDARG;
2084
2085   // The IAccessible2 spec says we don't have to implement the "sentence"
2086   // boundary type, we can just let the screenreader handle it.
2087   if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2088     *start_offset = 0;
2089     *end_offset = 0;
2090     *text = NULL;
2091     return S_FALSE;
2092   }
2093
2094   const base::string16& text_str = TextForIAccessibleText();
2095
2096   *start_offset = FindBoundary(
2097       text_str, boundary_type, offset, ui::BACKWARDS_DIRECTION);
2098   *end_offset = offset;
2099   return get_text(*start_offset, *end_offset, text);
2100 }
2101
2102 STDMETHODIMP BrowserAccessibilityWin::get_textAfterOffset(
2103     LONG offset,
2104     enum IA2TextBoundaryType boundary_type,
2105     LONG* start_offset,
2106     LONG* end_offset,
2107     BSTR* text) {
2108   if (!instance_active())
2109     return E_FAIL;
2110
2111   if (!start_offset || !end_offset || !text)
2112     return E_INVALIDARG;
2113
2114   // The IAccessible2 spec says we don't have to implement the "sentence"
2115   // boundary type, we can just let the screenreader handle it.
2116   if (boundary_type == IA2_TEXT_BOUNDARY_SENTENCE) {
2117     *start_offset = 0;
2118     *end_offset = 0;
2119     *text = NULL;
2120     return S_FALSE;
2121   }
2122
2123   const base::string16& text_str = TextForIAccessibleText();
2124
2125   *start_offset = offset;
2126   *end_offset = FindBoundary(
2127       text_str, boundary_type, offset, ui::FORWARDS_DIRECTION);
2128   return get_text(*start_offset, *end_offset, text);
2129 }
2130
2131 STDMETHODIMP BrowserAccessibilityWin::get_newText(IA2TextSegment* new_text) {
2132   if (!instance_active())
2133     return E_FAIL;
2134
2135   if (!new_text)
2136     return E_INVALIDARG;
2137
2138   base::string16 text = TextForIAccessibleText();
2139
2140   new_text->text = SysAllocString(text.c_str());
2141   new_text->start = 0;
2142   new_text->end = static_cast<long>(text.size());
2143   return S_OK;
2144 }
2145
2146 STDMETHODIMP BrowserAccessibilityWin::get_oldText(IA2TextSegment* old_text) {
2147   if (!instance_active())
2148     return E_FAIL;
2149
2150   if (!old_text)
2151     return E_INVALIDARG;
2152
2153   old_text->text = SysAllocString(old_text_.c_str());
2154   old_text->start = 0;
2155   old_text->end = static_cast<long>(old_text_.size());
2156   return S_OK;
2157 }
2158
2159 STDMETHODIMP BrowserAccessibilityWin::get_offsetAtPoint(
2160     LONG x,
2161     LONG y,
2162     enum IA2CoordinateType coord_type,
2163     LONG* offset) {
2164   if (!instance_active())
2165     return E_FAIL;
2166
2167   if (!offset)
2168     return E_INVALIDARG;
2169
2170   // TODO(dmazzoni): implement this. We're returning S_OK for now so that
2171   // screen readers still return partially accurate results rather than
2172   // completely failing.
2173   *offset = 0;
2174   return S_OK;
2175 }
2176
2177 STDMETHODIMP BrowserAccessibilityWin::scrollSubstringTo(
2178     LONG start_index,
2179     LONG end_index,
2180     enum IA2ScrollType scroll_type) {
2181   // TODO(dmazzoni): adjust this for the start and end index, too.
2182   return scrollTo(scroll_type);
2183 }
2184
2185 STDMETHODIMP BrowserAccessibilityWin::scrollSubstringToPoint(
2186     LONG start_index,
2187     LONG end_index,
2188     enum IA2CoordinateType coordinate_type,
2189     LONG x, LONG y) {
2190   // TODO(dmazzoni): adjust this for the start and end index, too.
2191   return scrollToPoint(coordinate_type, x, y);
2192 }
2193
2194 STDMETHODIMP BrowserAccessibilityWin::addSelection(LONG start_offset,
2195                                                    LONG end_offset) {
2196   if (!instance_active())
2197     return E_FAIL;
2198
2199   const base::string16& text_str = TextForIAccessibleText();
2200   HandleSpecialTextOffset(text_str, &start_offset);
2201   HandleSpecialTextOffset(text_str, &end_offset);
2202
2203   manager()->SetTextSelection(*this, start_offset, end_offset);
2204   return S_OK;
2205 }
2206
2207 STDMETHODIMP BrowserAccessibilityWin::removeSelection(LONG selection_index) {
2208   if (!instance_active())
2209     return E_FAIL;
2210
2211   if (selection_index != 0)
2212     return E_INVALIDARG;
2213
2214   manager()->SetTextSelection(*this, 0, 0);
2215   return S_OK;
2216 }
2217
2218 STDMETHODIMP BrowserAccessibilityWin::setCaretOffset(LONG offset) {
2219   if (!instance_active())
2220     return E_FAIL;
2221
2222   const base::string16& text_str = TextForIAccessibleText();
2223   HandleSpecialTextOffset(text_str, &offset);
2224   manager()->SetTextSelection(*this, offset, offset);
2225   return S_OK;
2226 }
2227
2228 STDMETHODIMP BrowserAccessibilityWin::setSelection(LONG selection_index,
2229                                                    LONG start_offset,
2230                                                    LONG end_offset) {
2231   if (!instance_active())
2232     return E_FAIL;
2233
2234   if (selection_index != 0)
2235     return E_INVALIDARG;
2236
2237   const base::string16& text_str = TextForIAccessibleText();
2238   HandleSpecialTextOffset(text_str, &start_offset);
2239   HandleSpecialTextOffset(text_str, &end_offset);
2240
2241   manager()->SetTextSelection(*this, start_offset, end_offset);
2242   return S_OK;
2243 }
2244
2245 //
2246 // IAccessibleHypertext methods.
2247 //
2248
2249 STDMETHODIMP BrowserAccessibilityWin::get_nHyperlinks(long* hyperlink_count) {
2250   if (!instance_active())
2251     return E_FAIL;
2252
2253   if (!hyperlink_count)
2254     return E_INVALIDARG;
2255
2256   *hyperlink_count = hyperlink_offset_to_index_.size();
2257   return S_OK;
2258 }
2259
2260 STDMETHODIMP BrowserAccessibilityWin::get_hyperlink(
2261     long index,
2262     IAccessibleHyperlink** hyperlink) {
2263   if (!instance_active())
2264     return E_FAIL;
2265
2266   if (!hyperlink ||
2267       index < 0 ||
2268       index >= static_cast<long>(hyperlinks_.size())) {
2269     return E_INVALIDARG;
2270   }
2271
2272   BrowserAccessibilityWin* child =
2273       children()[hyperlinks_[index]]->ToBrowserAccessibilityWin();
2274   *hyperlink = static_cast<IAccessibleHyperlink*>(child->NewReference());
2275   return S_OK;
2276 }
2277
2278 STDMETHODIMP BrowserAccessibilityWin::get_hyperlinkIndex(
2279     long char_index,
2280     long* hyperlink_index) {
2281   if (!instance_active())
2282     return E_FAIL;
2283
2284   if (!hyperlink_index)
2285     return E_INVALIDARG;
2286
2287   *hyperlink_index = -1;
2288
2289   if (char_index < 0 || char_index >= static_cast<long>(hypertext_.size()))
2290     return E_INVALIDARG;
2291
2292   std::map<int32, int32>::iterator it =
2293       hyperlink_offset_to_index_.find(char_index);
2294   if (it == hyperlink_offset_to_index_.end())
2295     return E_FAIL;
2296
2297   *hyperlink_index = it->second;
2298   return S_OK;
2299 }
2300
2301 //
2302 // IAccessibleValue methods.
2303 //
2304
2305 STDMETHODIMP BrowserAccessibilityWin::get_currentValue(VARIANT* value) {
2306   if (!instance_active())
2307     return E_FAIL;
2308
2309   if (!value)
2310     return E_INVALIDARG;
2311
2312   float float_val;
2313   if (GetFloatAttribute(
2314           ui::AX_ATTR_VALUE_FOR_RANGE, &float_val)) {
2315     value->vt = VT_R8;
2316     value->dblVal = float_val;
2317     return S_OK;
2318   }
2319
2320   value->vt = VT_EMPTY;
2321   return S_FALSE;
2322 }
2323
2324 STDMETHODIMP BrowserAccessibilityWin::get_minimumValue(VARIANT* value) {
2325   if (!instance_active())
2326     return E_FAIL;
2327
2328   if (!value)
2329     return E_INVALIDARG;
2330
2331   float float_val;
2332   if (GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE,
2333                         &float_val)) {
2334     value->vt = VT_R8;
2335     value->dblVal = float_val;
2336     return S_OK;
2337   }
2338
2339   value->vt = VT_EMPTY;
2340   return S_FALSE;
2341 }
2342
2343 STDMETHODIMP BrowserAccessibilityWin::get_maximumValue(VARIANT* value) {
2344   if (!instance_active())
2345     return E_FAIL;
2346
2347   if (!value)
2348     return E_INVALIDARG;
2349
2350   float float_val;
2351   if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE,
2352                         &float_val)) {
2353     value->vt = VT_R8;
2354     value->dblVal = float_val;
2355     return S_OK;
2356   }
2357
2358   value->vt = VT_EMPTY;
2359   return S_FALSE;
2360 }
2361
2362 STDMETHODIMP BrowserAccessibilityWin::setCurrentValue(VARIANT new_value) {
2363   // TODO(dmazzoni): Implement this.
2364   return E_NOTIMPL;
2365 }
2366
2367 //
2368 // ISimpleDOMDocument methods.
2369 //
2370
2371 STDMETHODIMP BrowserAccessibilityWin::get_URL(BSTR* url) {
2372   if (!instance_active())
2373     return E_FAIL;
2374
2375   if (!url)
2376     return E_INVALIDARG;
2377
2378   return GetStringAttributeAsBstr(ui::AX_ATTR_DOC_URL, url);
2379 }
2380
2381 STDMETHODIMP BrowserAccessibilityWin::get_title(BSTR* title) {
2382   if (!instance_active())
2383     return E_FAIL;
2384
2385   if (!title)
2386     return E_INVALIDARG;
2387
2388   return GetStringAttributeAsBstr(ui::AX_ATTR_DOC_TITLE, title);
2389 }
2390
2391 STDMETHODIMP BrowserAccessibilityWin::get_mimeType(BSTR* mime_type) {
2392   if (!instance_active())
2393     return E_FAIL;
2394
2395   if (!mime_type)
2396     return E_INVALIDARG;
2397
2398   return GetStringAttributeAsBstr(
2399       ui::AX_ATTR_DOC_MIMETYPE, mime_type);
2400 }
2401
2402 STDMETHODIMP BrowserAccessibilityWin::get_docType(BSTR* doc_type) {
2403   if (!instance_active())
2404     return E_FAIL;
2405
2406   if (!doc_type)
2407     return E_INVALIDARG;
2408
2409   return GetStringAttributeAsBstr(
2410       ui::AX_ATTR_DOC_DOCTYPE, doc_type);
2411 }
2412
2413 //
2414 // ISimpleDOMNode methods.
2415 //
2416
2417 STDMETHODIMP BrowserAccessibilityWin::get_nodeInfo(
2418     BSTR* node_name,
2419     short* name_space_id,
2420     BSTR* node_value,
2421     unsigned int* num_children,
2422     unsigned int* unique_id,
2423     unsigned short* node_type) {
2424   if (!instance_active())
2425     return E_FAIL;
2426
2427   if (!node_name || !name_space_id || !node_value || !num_children ||
2428       !unique_id || !node_type) {
2429     return E_INVALIDARG;
2430   }
2431
2432   base::string16 tag;
2433   if (GetString16Attribute(ui::AX_ATTR_HTML_TAG, &tag))
2434     *node_name = SysAllocString(tag.c_str());
2435   else
2436     *node_name = NULL;
2437
2438   *name_space_id = 0;
2439   *node_value = SysAllocString(base::UTF8ToUTF16(value()).c_str());
2440   *num_children = PlatformChildCount();
2441   *unique_id = unique_id_win_;
2442
2443   if (ia_role_ == ROLE_SYSTEM_DOCUMENT) {
2444     *node_type = NODETYPE_DOCUMENT;
2445   } else if (ia_role_ == ROLE_SYSTEM_TEXT &&
2446              ((ia2_state_ & IA2_STATE_EDITABLE) == 0)) {
2447     *node_type = NODETYPE_TEXT;
2448   } else {
2449     *node_type = NODETYPE_ELEMENT;
2450   }
2451
2452   return S_OK;
2453 }
2454
2455 STDMETHODIMP BrowserAccessibilityWin::get_attributes(
2456     unsigned short max_attribs,
2457     BSTR* attrib_names,
2458     short* name_space_id,
2459     BSTR* attrib_values,
2460     unsigned short* num_attribs) {
2461   if (!instance_active())
2462     return E_FAIL;
2463
2464   if (!attrib_names || !name_space_id || !attrib_values || !num_attribs)
2465     return E_INVALIDARG;
2466
2467   *num_attribs = max_attribs;
2468   if (*num_attribs > html_attributes().size())
2469     *num_attribs = html_attributes().size();
2470
2471   for (unsigned short i = 0; i < *num_attribs; ++i) {
2472     attrib_names[i] = SysAllocString(
2473         base::UTF8ToUTF16(html_attributes()[i].first).c_str());
2474     name_space_id[i] = 0;
2475     attrib_values[i] = SysAllocString(
2476         base::UTF8ToUTF16(html_attributes()[i].second).c_str());
2477   }
2478   return S_OK;
2479 }
2480
2481 STDMETHODIMP BrowserAccessibilityWin::get_attributesForNames(
2482     unsigned short num_attribs,
2483     BSTR* attrib_names,
2484     short* name_space_id,
2485     BSTR* attrib_values) {
2486   if (!instance_active())
2487     return E_FAIL;
2488
2489   if (!attrib_names || !name_space_id || !attrib_values)
2490     return E_INVALIDARG;
2491
2492   for (unsigned short i = 0; i < num_attribs; ++i) {
2493     name_space_id[i] = 0;
2494     bool found = false;
2495     std::string name = base::UTF16ToUTF8((LPCWSTR)attrib_names[i]);
2496     for (unsigned int j = 0;  j < html_attributes().size(); ++j) {
2497       if (html_attributes()[j].first == name) {
2498         attrib_values[i] = SysAllocString(
2499             base::UTF8ToUTF16(html_attributes()[j].second).c_str());
2500         found = true;
2501         break;
2502       }
2503     }
2504     if (!found) {
2505       attrib_values[i] = NULL;
2506     }
2507   }
2508   return S_OK;
2509 }
2510
2511 STDMETHODIMP BrowserAccessibilityWin::get_computedStyle(
2512     unsigned short max_style_properties,
2513     boolean use_alternate_view,
2514     BSTR* style_properties,
2515     BSTR* style_values,
2516     unsigned short *num_style_properties)  {
2517   if (!instance_active())
2518     return E_FAIL;
2519
2520   if (!style_properties || !style_values)
2521     return E_INVALIDARG;
2522
2523   // We only cache a single style property for now: DISPLAY
2524
2525   base::string16 display;
2526   if (max_style_properties == 0 ||
2527       !GetString16Attribute(ui::AX_ATTR_DISPLAY, &display)) {
2528     *num_style_properties = 0;
2529     return S_OK;
2530   }
2531
2532   *num_style_properties = 1;
2533   style_properties[0] = SysAllocString(L"display");
2534   style_values[0] = SysAllocString(display.c_str());
2535
2536   return S_OK;
2537 }
2538
2539 STDMETHODIMP BrowserAccessibilityWin::get_computedStyleForProperties(
2540     unsigned short num_style_properties,
2541     boolean use_alternate_view,
2542     BSTR* style_properties,
2543     BSTR* style_values) {
2544   if (!instance_active())
2545     return E_FAIL;
2546
2547   if (!style_properties || !style_values)
2548     return E_INVALIDARG;
2549
2550   // We only cache a single style property for now: DISPLAY
2551
2552   for (unsigned short i = 0; i < num_style_properties; ++i) {
2553     base::string16 name = (LPCWSTR)style_properties[i];
2554     StringToLowerASCII(&name);
2555     if (name == L"display") {
2556       base::string16 display = GetString16Attribute(
2557           ui::AX_ATTR_DISPLAY);
2558       style_values[i] = SysAllocString(display.c_str());
2559     } else {
2560       style_values[i] = NULL;
2561     }
2562   }
2563
2564   return S_OK;
2565 }
2566
2567 STDMETHODIMP BrowserAccessibilityWin::scrollTo(boolean placeTopLeft) {
2568   return scrollTo(placeTopLeft ?
2569       IA2_SCROLL_TYPE_TOP_LEFT : IA2_SCROLL_TYPE_ANYWHERE);
2570 }
2571
2572 STDMETHODIMP BrowserAccessibilityWin::get_parentNode(ISimpleDOMNode** node) {
2573   if (!instance_active())
2574     return E_FAIL;
2575
2576   if (!node)
2577     return E_INVALIDARG;
2578
2579   *node = parent()->ToBrowserAccessibilityWin()->NewReference();
2580   return S_OK;
2581 }
2582
2583 STDMETHODIMP BrowserAccessibilityWin::get_firstChild(ISimpleDOMNode** node)  {
2584   if (!instance_active())
2585     return E_FAIL;
2586
2587   if (!node)
2588     return E_INVALIDARG;
2589
2590   if (PlatformChildCount() == 0) {
2591     *node = NULL;
2592     return S_FALSE;
2593   }
2594
2595   *node = PlatformGetChild(0)->ToBrowserAccessibilityWin()->NewReference();
2596   return S_OK;
2597 }
2598
2599 STDMETHODIMP BrowserAccessibilityWin::get_lastChild(ISimpleDOMNode** node) {
2600   if (!instance_active())
2601     return E_FAIL;
2602
2603   if (!node)
2604     return E_INVALIDARG;
2605
2606   if (PlatformChildCount() == 0) {
2607     *node = NULL;
2608     return S_FALSE;
2609   }
2610
2611   *node = PlatformGetChild(PlatformChildCount() - 1)
2612       ->ToBrowserAccessibilityWin()->NewReference();
2613   return S_OK;
2614 }
2615
2616 STDMETHODIMP BrowserAccessibilityWin::get_previousSibling(
2617     ISimpleDOMNode** node) {
2618   if (!instance_active())
2619     return E_FAIL;
2620
2621   if (!node)
2622     return E_INVALIDARG;
2623
2624   if (!parent() || index_in_parent() <= 0) {
2625     *node = NULL;
2626     return S_FALSE;
2627   }
2628
2629   *node = parent()->children()[index_in_parent() - 1]->
2630       ToBrowserAccessibilityWin()->NewReference();
2631   return S_OK;
2632 }
2633
2634 STDMETHODIMP BrowserAccessibilityWin::get_nextSibling(ISimpleDOMNode** node) {
2635   if (!instance_active())
2636     return E_FAIL;
2637
2638   if (!node)
2639     return E_INVALIDARG;
2640
2641   if (!parent() ||
2642       index_in_parent() < 0 ||
2643       index_in_parent() >= static_cast<int>(parent()->children().size()) - 1) {
2644     *node = NULL;
2645     return S_FALSE;
2646   }
2647
2648   *node = parent()->children()[index_in_parent() + 1]->
2649       ToBrowserAccessibilityWin()->NewReference();
2650   return S_OK;
2651 }
2652
2653 STDMETHODIMP BrowserAccessibilityWin::get_childAt(
2654     unsigned int child_index,
2655     ISimpleDOMNode** node) {
2656   if (!instance_active())
2657     return E_FAIL;
2658
2659   if (!node)
2660     return E_INVALIDARG;
2661
2662   if (child_index >= PlatformChildCount())
2663     return E_INVALIDARG;
2664
2665   BrowserAccessibility* child = PlatformGetChild(child_index);
2666   if (!child) {
2667     *node = NULL;
2668     return S_FALSE;
2669   }
2670
2671   *node = child->ToBrowserAccessibilityWin()->NewReference();
2672   return S_OK;
2673 }
2674
2675 //
2676 // ISimpleDOMText methods.
2677 //
2678
2679 STDMETHODIMP BrowserAccessibilityWin::get_domText(BSTR* dom_text) {
2680   if (!instance_active())
2681     return E_FAIL;
2682
2683   if (!dom_text)
2684     return E_INVALIDARG;
2685
2686   return GetStringAttributeAsBstr(
2687       ui::AX_ATTR_NAME, dom_text);
2688 }
2689
2690 STDMETHODIMP BrowserAccessibilityWin::get_clippedSubstringBounds(
2691     unsigned int start_index,
2692     unsigned int end_index,
2693     int* out_x,
2694     int* out_y,
2695     int* out_width,
2696     int* out_height) {
2697   // TODO(dmazzoni): fully support this API by intersecting the
2698   // rect with the container's rect.
2699   return get_unclippedSubstringBounds(
2700       start_index, end_index, out_x, out_y, out_width, out_height);
2701 }
2702
2703 STDMETHODIMP BrowserAccessibilityWin::get_unclippedSubstringBounds(
2704     unsigned int start_index,
2705     unsigned int end_index,
2706     int* out_x,
2707     int* out_y,
2708     int* out_width,
2709     int* out_height) {
2710   if (!instance_active())
2711     return E_FAIL;
2712
2713   if (!out_x || !out_y || !out_width || !out_height)
2714     return E_INVALIDARG;
2715
2716   const base::string16& text_str = TextForIAccessibleText();
2717   if (start_index > text_str.size() ||
2718       end_index > text_str.size() ||
2719       start_index > end_index) {
2720     return E_INVALIDARG;
2721   }
2722
2723   gfx::Rect bounds = GetGlobalBoundsForRange(
2724       start_index, end_index - start_index);
2725   *out_x = bounds.x();
2726   *out_y = bounds.y();
2727   *out_width = bounds.width();
2728   *out_height = bounds.height();
2729   return S_OK;
2730 }
2731
2732 STDMETHODIMP BrowserAccessibilityWin::scrollToSubstring(
2733     unsigned int start_index,
2734     unsigned int end_index) {
2735   if (!instance_active())
2736     return E_FAIL;
2737
2738   const base::string16& text_str = TextForIAccessibleText();
2739   if (start_index > text_str.size() ||
2740       end_index > text_str.size() ||
2741       start_index > end_index) {
2742     return E_INVALIDARG;
2743   }
2744
2745   manager()->ScrollToMakeVisible(*this, GetLocalBoundsForRange(
2746       start_index, end_index - start_index));
2747   manager()->ToBrowserAccessibilityManagerWin()->TrackScrollingObject(this);
2748
2749   return S_OK;
2750 }
2751
2752 //
2753 // IServiceProvider methods.
2754 //
2755
2756 STDMETHODIMP BrowserAccessibilityWin::QueryService(REFGUID guidService,
2757                                                    REFIID riid,
2758                                                    void** object) {
2759   if (!instance_active())
2760     return E_FAIL;
2761
2762   // The system uses IAccessible APIs for many purposes, but only
2763   // assistive technology like screen readers uses IAccessible2.
2764   // Enable full accessibility support when IAccessible2 APIs are queried.
2765   if (riid == IID_IAccessible2)
2766     BrowserAccessibilityStateImpl::GetInstance()->EnableAccessibility();
2767
2768   if (guidService == GUID_IAccessibleContentDocument) {
2769     // Special Mozilla extension: return the accessible for the root document.
2770     // Screen readers use this to distinguish between a document loaded event
2771     // on the root document vs on an iframe.
2772     return manager()->GetRoot()->ToBrowserAccessibilityWin()->QueryInterface(
2773         IID_IAccessible2, object);
2774   }
2775
2776   if (guidService == IID_IAccessible ||
2777       guidService == IID_IAccessible2 ||
2778       guidService == IID_IAccessibleAction ||
2779       guidService == IID_IAccessibleApplication ||
2780       guidService == IID_IAccessibleHyperlink ||
2781       guidService == IID_IAccessibleHypertext ||
2782       guidService == IID_IAccessibleImage ||
2783       guidService == IID_IAccessibleTable ||
2784       guidService == IID_IAccessibleTable2 ||
2785       guidService == IID_IAccessibleTableCell ||
2786       guidService == IID_IAccessibleText ||
2787       guidService == IID_IAccessibleValue ||
2788       guidService == IID_ISimpleDOMDocument ||
2789       guidService == IID_ISimpleDOMNode ||
2790       guidService == IID_ISimpleDOMText ||
2791       guidService == GUID_ISimpleDOM) {
2792     return QueryInterface(riid, object);
2793   }
2794
2795   // We only support the IAccessibleEx interface on Windows 8 and above. This
2796   // is needed for the on-screen Keyboard to show up in metro mode, when the
2797   // user taps an editable portion on the page.
2798   // All methods in the IAccessibleEx interface are unimplemented.
2799   if (riid == IID_IAccessibleEx &&
2800       base::win::GetVersion() >= base::win::VERSION_WIN8) {
2801     return QueryInterface(riid, object);
2802   }
2803
2804   *object = NULL;
2805   return E_FAIL;
2806 }
2807
2808 STDMETHODIMP BrowserAccessibilityWin::GetPatternProvider(PATTERNID id,
2809                                                          IUnknown** provider) {
2810   DVLOG(1) << "In Function: "
2811            << __FUNCTION__
2812            << " for pattern id: "
2813            << id;
2814   if (id == UIA_ValuePatternId || id == UIA_TextPatternId) {
2815     if (IsEditableText()) {
2816       // The BrowserAccessibilityManager keeps track of instances when
2817       // we don't want to show the on-screen keyboard.
2818       if (!manager()->IsOSKAllowed(GetGlobalBoundsRect()))
2819         return E_NOTIMPL;
2820
2821       DVLOG(1) << "Returning UIA text provider";
2822       base::win::UIATextProvider::CreateTextProvider(true, provider);
2823       return S_OK;
2824     }
2825   }
2826   return E_NOTIMPL;
2827 }
2828
2829 STDMETHODIMP BrowserAccessibilityWin::GetPropertyValue(PROPERTYID id,
2830                                                        VARIANT* ret) {
2831   DVLOG(1) << "In Function: "
2832            << __FUNCTION__
2833            << " for property id: "
2834            << id;
2835   V_VT(ret) = VT_EMPTY;
2836   if (id == UIA_ControlTypePropertyId) {
2837     if (IsEditableText()) {
2838       V_VT(ret) = VT_I4;
2839       ret->lVal = UIA_EditControlTypeId;
2840       DVLOG(1) << "Returning Edit control type";
2841     } else {
2842       DVLOG(1) << "Returning empty control type";
2843     }
2844   }
2845   return S_OK;
2846 }
2847
2848 //
2849 // CComObjectRootEx methods.
2850 //
2851
2852 HRESULT WINAPI BrowserAccessibilityWin::InternalQueryInterface(
2853     void* this_ptr,
2854     const _ATL_INTMAP_ENTRY* entries,
2855     REFIID iid,
2856     void** object) {
2857   if (iid == IID_IAccessibleImage) {
2858     if (ia_role_ != ROLE_SYSTEM_GRAPHIC) {
2859       *object = NULL;
2860       return E_NOINTERFACE;
2861     }
2862   } else if (iid == IID_IAccessibleTable || iid == IID_IAccessibleTable2) {
2863     if (ia_role_ != ROLE_SYSTEM_TABLE) {
2864       *object = NULL;
2865       return E_NOINTERFACE;
2866     }
2867   } else if (iid == IID_IAccessibleTableCell) {
2868     if (ia_role_ != ROLE_SYSTEM_CELL) {
2869       *object = NULL;
2870       return E_NOINTERFACE;
2871     }
2872   } else if (iid == IID_IAccessibleValue) {
2873     if (ia_role_ != ROLE_SYSTEM_PROGRESSBAR &&
2874         ia_role_ != ROLE_SYSTEM_SCROLLBAR &&
2875         ia_role_ != ROLE_SYSTEM_SLIDER) {
2876       *object = NULL;
2877       return E_NOINTERFACE;
2878     }
2879   } else if (iid == IID_ISimpleDOMDocument) {
2880     if (ia_role_ != ROLE_SYSTEM_DOCUMENT) {
2881       *object = NULL;
2882       return E_NOINTERFACE;
2883     }
2884   }
2885
2886   return CComObjectRootBase::InternalQueryInterface(
2887       this_ptr, entries, iid, object);
2888 }
2889
2890 //
2891 // Private methods.
2892 //
2893
2894 // Initialize this object and mark it as active.
2895 void BrowserAccessibilityWin::PreInitialize() {
2896   BrowserAccessibility::PreInitialize();
2897
2898   InitRoleAndState();
2899
2900   // Expose the "display" and "tag" attributes.
2901   StringAttributeToIA2(ui::AX_ATTR_DISPLAY, "display");
2902   StringAttributeToIA2(ui::AX_ATTR_HTML_TAG, "tag");
2903   StringAttributeToIA2(ui::AX_ATTR_ROLE, "xml-roles");
2904
2905   // Expose "level" attribute for headings, trees, etc.
2906   IntAttributeToIA2(ui::AX_ATTR_HIERARCHICAL_LEVEL, "level");
2907
2908   // Expose the set size and position in set for listbox options.
2909   if (blink_role() == ui::AX_ROLE_LIST_BOX_OPTION &&
2910       parent() &&
2911       parent()->role() == ui::AX_ROLE_LIST_BOX) {
2912     ia2_attributes_.push_back(
2913         L"setsize:" + base::IntToString16(parent()->PlatformChildCount()));
2914     ia2_attributes_.push_back(
2915         L"setsize:" + base::IntToString16(index_in_parent() + 1));
2916   }
2917
2918   if (ia_role_ == ROLE_SYSTEM_CHECKBUTTON ||
2919       ia_role_ == ROLE_SYSTEM_RADIOBUTTON ||
2920       ia2_role_ == IA2_ROLE_TOGGLE_BUTTON) {
2921     ia2_attributes_.push_back(L"checkable:true");
2922   }
2923
2924   // Expose live region attributes.
2925   StringAttributeToIA2(ui::AX_ATTR_LIVE_STATUS, "live");
2926   StringAttributeToIA2(ui::AX_ATTR_LIVE_RELEVANT, "relevant");
2927   BoolAttributeToIA2(ui::AX_ATTR_LIVE_ATOMIC, "atomic");
2928   BoolAttributeToIA2(ui::AX_ATTR_LIVE_BUSY, "busy");
2929
2930   // Expose container live region attributes.
2931   StringAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_STATUS,
2932                        "container-live");
2933   StringAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_RELEVANT,
2934                        "container-relevant");
2935   BoolAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_ATOMIC,
2936                      "container-atomic");
2937   BoolAttributeToIA2(ui::AX_ATTR_CONTAINER_LIVE_BUSY,
2938                      "container-busy");
2939
2940   // Expose slider value.
2941   if (ia_role_ == ROLE_SYSTEM_PROGRESSBAR ||
2942       ia_role_ == ROLE_SYSTEM_SCROLLBAR ||
2943       ia_role_ == ROLE_SYSTEM_SLIDER) {
2944     ia2_attributes_.push_back(L"valuetext:" + GetValueText());
2945   }
2946
2947   // Expose table cell index.
2948   if (ia_role_ == ROLE_SYSTEM_CELL) {
2949     BrowserAccessibility* table = parent();
2950     while (table && table->role() != ui::AX_ROLE_TABLE)
2951       table = table->parent();
2952     if (table) {
2953       const std::vector<int32>& unique_cell_ids = table->GetIntListAttribute(
2954           ui::AX_ATTR_UNIQUE_CELL_IDS);
2955       for (size_t i = 0; i < unique_cell_ids.size(); ++i) {
2956         if (unique_cell_ids[i] == renderer_id()) {
2957           ia2_attributes_.push_back(
2958               base::string16(L"table-cell-index:") + base::IntToString16(i));
2959         }
2960       }
2961     }
2962   }
2963
2964   // The calculation of the accessible name of an element has been
2965   // standardized in the HTML to Platform Accessibility APIs Implementation
2966   // Guide (http://www.w3.org/TR/html-aapi/). In order to return the
2967   // appropriate accessible name on Windows, we need to apply some logic
2968   // to the fields we get from WebKit.
2969   //
2970   // TODO(dmazzoni): move most of this logic into WebKit.
2971   //
2972   // WebKit gives us:
2973   //
2974   //   name: the default name, e.g. inner text
2975   //   title ui element: a reference to a <label> element on the same
2976   //       page that labels this node.
2977   //   description: accessible labels that override the default name:
2978   //       aria-label or aria-labelledby or aria-describedby
2979   //   help: the value of the "title" attribute
2980   //
2981   // On Windows, the logic we apply lets some fields take precedence and
2982   // always returns the primary name in "name" and the secondary name,
2983   // if any, in "description".
2984
2985   int title_elem_id = GetIntAttribute(
2986       ui::AX_ATTR_TITLE_UI_ELEMENT);
2987   std::string help = GetStringAttribute(ui::AX_ATTR_HELP);
2988   std::string description = GetStringAttribute(
2989       ui::AX_ATTR_DESCRIPTION);
2990
2991   // WebKit annoyingly puts the title in the description if there's no other
2992   // description, which just confuses the rest of the logic. Put it back.
2993   // Now "help" is always the value of the "title" attribute, if present.
2994   std::string title_attr;
2995   if (GetHtmlAttribute("title", &title_attr) &&
2996       description == title_attr &&
2997       help.empty()) {
2998     help = description;
2999     description.clear();
3000   }
3001
3002   // Now implement the main logic: the descripion should become the name if
3003   // it's nonempty, and the help should become the description if
3004   // there's no description - or the name if there's no name or description.
3005   if (!description.empty()) {
3006     set_name(description);
3007     description.clear();
3008   }
3009   if (!help.empty() && description.empty()) {
3010     description = help;
3011     help.clear();
3012   }
3013   if (!description.empty() && name().empty() && !title_elem_id) {
3014     set_name(description);
3015     description.clear();
3016   }
3017
3018   // If it's a text field, also consider the placeholder.
3019   std::string placeholder;
3020   if (blink_role() == ui::AX_ROLE_TEXT_FIELD &&
3021       HasState(ui::AX_STATE_FOCUSABLE) &&
3022       GetHtmlAttribute("placeholder", &placeholder)) {
3023     if (name().empty() && !title_elem_id) {
3024       set_name(placeholder);
3025     } else if (description.empty()) {
3026       description = placeholder;
3027     }
3028   }
3029
3030   SetStringAttribute(ui::AX_ATTR_DESCRIPTION, description);
3031   SetStringAttribute(ui::AX_ATTR_HELP, help);
3032
3033   // On Windows, the value of a document should be its url.
3034   if (blink_role() == ui::AX_ROLE_ROOT_WEB_AREA ||
3035       blink_role() == ui::AX_ROLE_WEB_AREA) {
3036     set_value(GetStringAttribute(ui::AX_ATTR_DOC_URL));
3037   }
3038
3039   // For certain roles (listbox option, static text, and list marker)
3040   // WebKit stores the main accessible text in the "value" - swap it so
3041   // that it's the "name".
3042   if (name().empty() &&
3043       (blink_role() == ui::AX_ROLE_LIST_BOX_OPTION ||
3044        blink_role() == ui::AX_ROLE_STATIC_TEXT ||
3045        blink_role() == ui::AX_ROLE_LIST_MARKER)) {
3046     std::string tmp = value();
3047     set_value(name());
3048     set_name(tmp);
3049   }
3050
3051   // If this doesn't have a value and is linked then set its value to the url
3052   // attribute. This allows screen readers to read an empty link's destination.
3053   if (value().empty() && (ia_state_ & STATE_SYSTEM_LINKED))
3054     set_value(GetStringAttribute(ui::AX_ATTR_URL));
3055
3056   // Clear any old relationships between this node and other nodes.
3057   for (size_t i = 0; i < relations_.size(); ++i)
3058     relations_[i]->Release();
3059   relations_.clear();
3060
3061   // Handle title UI element.
3062   if (title_elem_id) {
3063     // Add a labelled by relationship.
3064     CComObject<BrowserAccessibilityRelation>* relation;
3065     HRESULT hr = CComObject<BrowserAccessibilityRelation>::CreateInstance(
3066         &relation);
3067     DCHECK(SUCCEEDED(hr));
3068     relation->AddRef();
3069     relation->Initialize(this, IA2_RELATION_LABELLED_BY);
3070     relation->AddTarget(title_elem_id);
3071     relations_.push_back(relation);
3072   }
3073 }
3074
3075 void BrowserAccessibilityWin::PostInitialize() {
3076   BrowserAccessibility::PostInitialize();
3077
3078   // Construct the hypertext for this node.
3079   hyperlink_offset_to_index_.clear();
3080   hyperlinks_.clear();
3081   hypertext_.clear();
3082   for (unsigned int i = 0; i < PlatformChildCount(); ++i) {
3083     BrowserAccessibility* child = PlatformGetChild(i);
3084     if (child->role() == ui::AX_ROLE_STATIC_TEXT) {
3085       hypertext_ += base::UTF8ToUTF16(child->name());
3086     } else {
3087       hyperlink_offset_to_index_[hypertext_.size()] = hyperlinks_.size();
3088       hypertext_ += kEmbeddedCharacter;
3089       hyperlinks_.push_back(i);
3090     }
3091   }
3092   DCHECK_EQ(hyperlink_offset_to_index_.size(), hyperlinks_.size());
3093
3094   // Fire an event when an alert first appears.
3095   if (blink_role() == ui::AX_ROLE_ALERT && first_time_)
3096     manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
3097
3098   // Fire events if text has changed.
3099   base::string16 text = TextForIAccessibleText();
3100   if (previous_text_ != text) {
3101     if (!previous_text_.empty() && !text.empty()) {
3102       manager()->NotifyAccessibilityEvent(
3103           ui::AX_EVENT_SHOW, this);
3104     }
3105
3106     // TODO(dmazzoni): Look into HIDE events, too.
3107
3108     old_text_ = previous_text_;
3109     previous_text_ = text;
3110   }
3111
3112   BrowserAccessibilityManagerWin* manager =
3113       this->manager()->ToBrowserAccessibilityManagerWin();
3114
3115   // Fire events if the state has changed.
3116   if (!first_time_ && ia_state_ != old_ia_state_) {
3117     // Normally focus events are handled elsewhere, however
3118     // focus for managed descendants is platform-specific.
3119     // Fire a focus event if the focused descendant in a multi-select
3120     // list box changes.
3121     if (blink_role() == ui::AX_ROLE_LIST_BOX_OPTION &&
3122         (ia_state_ & STATE_SYSTEM_FOCUSABLE) &&
3123         (ia_state_ & STATE_SYSTEM_SELECTABLE) &&
3124         (ia_state_ & STATE_SYSTEM_FOCUSED) &&
3125         !(old_ia_state_ & STATE_SYSTEM_FOCUSED)) {
3126       manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_FOCUS, unique_id_win());
3127     }
3128
3129     if ((ia_state_ & STATE_SYSTEM_SELECTED) &&
3130         !(old_ia_state_ & STATE_SYSTEM_SELECTED)) {
3131       manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTIONADD,
3132                                        unique_id_win());
3133     } else if (!(ia_state_ & STATE_SYSTEM_SELECTED) &&
3134                (old_ia_state_ & STATE_SYSTEM_SELECTED)) {
3135       manager->MaybeCallNotifyWinEvent(EVENT_OBJECT_SELECTIONREMOVE,
3136                                        unique_id_win());
3137     }
3138
3139     old_ia_state_ = ia_state_;
3140   }
3141
3142   // Fire an event if this container object has scrolled.
3143   int sx = 0;
3144   int sy = 0;
3145   if (GetIntAttribute(ui::AX_ATTR_SCROLL_X, &sx) &&
3146       GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &sy)) {
3147     if (!first_time_ &&
3148         (sx != previous_scroll_x_ || sy != previous_scroll_y_)) {
3149       manager->MaybeCallNotifyWinEvent(EVENT_SYSTEM_SCROLLINGEND,
3150                                        unique_id_win());
3151     }
3152     previous_scroll_x_ = sx;
3153     previous_scroll_y_ = sy;
3154   }
3155
3156   first_time_ = false;
3157 }
3158
3159 void BrowserAccessibilityWin::NativeAddReference() {
3160   AddRef();
3161 }
3162
3163 void BrowserAccessibilityWin::NativeReleaseReference() {
3164   Release();
3165 }
3166
3167 bool BrowserAccessibilityWin::IsNative() const {
3168   return true;
3169 }
3170
3171 void BrowserAccessibilityWin::SetLocation(const gfx::Rect& new_location) {
3172   BrowserAccessibility::SetLocation(new_location);
3173   manager()->ToBrowserAccessibilityManagerWin()->MaybeCallNotifyWinEvent(
3174       EVENT_OBJECT_LOCATIONCHANGE, unique_id_win());
3175 }
3176
3177 BrowserAccessibilityWin* BrowserAccessibilityWin::NewReference() {
3178   AddRef();
3179   return this;
3180 }
3181
3182 BrowserAccessibilityWin* BrowserAccessibilityWin::GetTargetFromChildID(
3183     const VARIANT& var_id) {
3184   if (var_id.vt != VT_I4)
3185     return NULL;
3186
3187   LONG child_id = var_id.lVal;
3188   if (child_id == CHILDID_SELF)
3189     return this;
3190
3191   if (child_id >= 1 && child_id <= static_cast<LONG>(PlatformChildCount()))
3192     return PlatformGetChild(child_id - 1)->ToBrowserAccessibilityWin();
3193
3194   return manager()->ToBrowserAccessibilityManagerWin()->
3195       GetFromUniqueIdWin(child_id);
3196 }
3197
3198 HRESULT BrowserAccessibilityWin::GetStringAttributeAsBstr(
3199     ui::AXStringAttribute attribute,
3200     BSTR* value_bstr) {
3201   base::string16 str;
3202
3203   if (!GetString16Attribute(attribute, &str))
3204     return S_FALSE;
3205
3206   if (str.empty())
3207     return S_FALSE;
3208
3209   *value_bstr = SysAllocString(str.c_str());
3210   DCHECK(*value_bstr);
3211
3212   return S_OK;
3213 }
3214
3215 void BrowserAccessibilityWin::StringAttributeToIA2(
3216     ui::AXStringAttribute attribute,
3217     const char* ia2_attr) {
3218   base::string16 value;
3219   if (GetString16Attribute(attribute, &value))
3220     ia2_attributes_.push_back(base::ASCIIToUTF16(ia2_attr) + L":" + value);
3221 }
3222
3223 void BrowserAccessibilityWin::BoolAttributeToIA2(
3224     ui::AXBoolAttribute attribute,
3225     const char* ia2_attr) {
3226   bool value;
3227   if (GetBoolAttribute(attribute, &value)) {
3228     ia2_attributes_.push_back((base::ASCIIToUTF16(ia2_attr) + L":") +
3229                               (value ? L"true" : L"false"));
3230   }
3231 }
3232
3233 void BrowserAccessibilityWin::IntAttributeToIA2(
3234     ui::AXIntAttribute attribute,
3235     const char* ia2_attr) {
3236   int value;
3237   if (GetIntAttribute(attribute, &value)) {
3238     ia2_attributes_.push_back(base::ASCIIToUTF16(ia2_attr) + L":" +
3239                               base::IntToString16(value));
3240   }
3241 }
3242
3243 base::string16 BrowserAccessibilityWin::GetValueText() {
3244   float fval;
3245   base::string16 value = base::UTF8ToUTF16(this->value());
3246
3247   if (value.empty() &&
3248       GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE, &fval)) {
3249     value = base::UTF8ToUTF16(base::DoubleToString(fval));
3250   }
3251   return value;
3252 }
3253
3254 base::string16 BrowserAccessibilityWin::TextForIAccessibleText() {
3255   if (IsEditableText())
3256     return base::UTF8ToUTF16(value());
3257   return (blink_role() == ui::AX_ROLE_STATIC_TEXT) ?
3258       base::UTF8ToUTF16(name()) : hypertext_;
3259 }
3260
3261 void BrowserAccessibilityWin::HandleSpecialTextOffset(
3262     const base::string16& text,
3263     LONG* offset) {
3264   if (*offset == IA2_TEXT_OFFSET_LENGTH)
3265     *offset = static_cast<LONG>(text.size());
3266   else if (*offset == IA2_TEXT_OFFSET_CARET)
3267     get_caretOffset(offset);
3268 }
3269
3270 ui::TextBoundaryType BrowserAccessibilityWin::IA2TextBoundaryToTextBoundary(
3271     IA2TextBoundaryType ia2_boundary) {
3272   switch(ia2_boundary) {
3273     case IA2_TEXT_BOUNDARY_CHAR: return ui::CHAR_BOUNDARY;
3274     case IA2_TEXT_BOUNDARY_WORD: return ui::WORD_BOUNDARY;
3275     case IA2_TEXT_BOUNDARY_LINE: return ui::LINE_BOUNDARY;
3276     case IA2_TEXT_BOUNDARY_SENTENCE: return ui::SENTENCE_BOUNDARY;
3277     case IA2_TEXT_BOUNDARY_PARAGRAPH: return ui::PARAGRAPH_BOUNDARY;
3278     case IA2_TEXT_BOUNDARY_ALL: return ui::ALL_BOUNDARY;
3279     default:
3280       NOTREACHED();
3281       return ui::CHAR_BOUNDARY;
3282   }
3283 }
3284
3285 LONG BrowserAccessibilityWin::FindBoundary(
3286     const base::string16& text,
3287     IA2TextBoundaryType ia2_boundary,
3288     LONG start_offset,
3289     ui::TextBoundaryDirection direction) {
3290   HandleSpecialTextOffset(text, &start_offset);
3291   ui::TextBoundaryType boundary = IA2TextBoundaryToTextBoundary(ia2_boundary);
3292   const std::vector<int32>& line_breaks = GetIntListAttribute(
3293       ui::AX_ATTR_LINE_BREAKS);
3294   return ui::FindAccessibleTextBoundary(
3295       text, line_breaks, boundary, start_offset, direction);
3296 }
3297
3298 BrowserAccessibilityWin* BrowserAccessibilityWin::GetFromRendererID(
3299     int32 renderer_id) {
3300   return manager()->GetFromRendererID(renderer_id)->ToBrowserAccessibilityWin();
3301 }
3302
3303 void BrowserAccessibilityWin::InitRoleAndState() {
3304   ia_state_ = 0;
3305   ia2_state_ = IA2_STATE_OPAQUE;
3306   ia2_attributes_.clear();
3307
3308   if (HasState(ui::AX_STATE_BUSY))
3309     ia_state_ |= STATE_SYSTEM_BUSY;
3310   if (HasState(ui::AX_STATE_CHECKED))
3311     ia_state_ |= STATE_SYSTEM_CHECKED;
3312   if (HasState(ui::AX_STATE_COLLAPSED))
3313     ia_state_ |= STATE_SYSTEM_COLLAPSED;
3314   if (HasState(ui::AX_STATE_EXPANDED))
3315     ia_state_ |= STATE_SYSTEM_EXPANDED;
3316   if (HasState(ui::AX_STATE_FOCUSABLE))
3317     ia_state_ |= STATE_SYSTEM_FOCUSABLE;
3318   if (HasState(ui::AX_STATE_HASPOPUP))
3319     ia_state_ |= STATE_SYSTEM_HASPOPUP;
3320   if (HasState(ui::AX_STATE_HOVERED))
3321     ia_state_ |= STATE_SYSTEM_HOTTRACKED;
3322   if (HasState(ui::AX_STATE_INDETERMINATE))
3323     ia_state_ |= STATE_SYSTEM_INDETERMINATE;
3324   if (HasState(ui::AX_STATE_INVISIBLE))
3325     ia_state_ |= STATE_SYSTEM_INVISIBLE;
3326   if (HasState(ui::AX_STATE_LINKED))
3327     ia_state_ |= STATE_SYSTEM_LINKED;
3328   if (HasState(ui::AX_STATE_MULTISELECTABLE)) {
3329     ia_state_ |= STATE_SYSTEM_EXTSELECTABLE;
3330     ia_state_ |= STATE_SYSTEM_MULTISELECTABLE;
3331   }
3332   // TODO(ctguil): Support STATE_SYSTEM_EXTSELECTABLE/accSelect.
3333   if (HasState(ui::AX_STATE_OFFSCREEN))
3334     ia_state_ |= STATE_SYSTEM_OFFSCREEN;
3335   if (HasState(ui::AX_STATE_PRESSED))
3336     ia_state_ |= STATE_SYSTEM_PRESSED;
3337   if (HasState(ui::AX_STATE_PROTECTED))
3338     ia_state_ |= STATE_SYSTEM_PROTECTED;
3339   if (HasState(ui::AX_STATE_REQUIRED))
3340     ia2_state_ |= IA2_STATE_REQUIRED;
3341   if (HasState(ui::AX_STATE_SELECTABLE))
3342     ia_state_ |= STATE_SYSTEM_SELECTABLE;
3343   if (HasState(ui::AX_STATE_SELECTED))
3344     ia_state_ |= STATE_SYSTEM_SELECTED;
3345   if (HasState(ui::AX_STATE_VISITED))
3346     ia_state_ |= STATE_SYSTEM_TRAVERSED;
3347   if (!HasState(ui::AX_STATE_ENABLED))
3348     ia_state_ |= STATE_SYSTEM_UNAVAILABLE;
3349   if (HasState(ui::AX_STATE_VERTICAL)) {
3350     ia2_state_ |= IA2_STATE_VERTICAL;
3351   } else {
3352     ia2_state_ |= IA2_STATE_HORIZONTAL;
3353   }
3354   if (HasState(ui::AX_STATE_VISITED))
3355     ia_state_ |= STATE_SYSTEM_TRAVERSED;
3356
3357   // WebKit marks everything as readonly unless it's editable text, so if it's
3358   // not readonly, mark it as editable now. The final computation of the
3359   // READONLY state for MSAA is below, after the switch.
3360   if (!HasState(ui::AX_STATE_READONLY))
3361     ia2_state_ |= IA2_STATE_EDITABLE;
3362
3363   base::string16 invalid;
3364   if (GetHtmlAttribute("aria-invalid", &invalid))
3365     ia2_state_ |= IA2_STATE_INVALID_ENTRY;
3366
3367   if (GetBoolAttribute(ui::AX_ATTR_BUTTON_MIXED))
3368     ia_state_ |= STATE_SYSTEM_MIXED;
3369
3370   if (GetBoolAttribute(ui::AX_ATTR_CAN_SET_VALUE))
3371     ia2_state_ |= IA2_STATE_EDITABLE;
3372
3373   base::string16 html_tag = GetString16Attribute(
3374       ui::AX_ATTR_HTML_TAG);
3375   ia_role_ = 0;
3376   ia2_role_ = 0;
3377   switch (blink_role()) {
3378     case ui::AX_ROLE_ALERT:
3379       ia_role_ = ROLE_SYSTEM_ALERT;
3380       break;
3381     case ui::AX_ROLE_ALERT_DIALOG:
3382       ia_role_ = ROLE_SYSTEM_DIALOG;
3383       break;
3384     case ui::AX_ROLE_APPLICATION:
3385       ia_role_ = ROLE_SYSTEM_APPLICATION;
3386       break;
3387     case ui::AX_ROLE_ARTICLE:
3388       ia_role_ = ROLE_SYSTEM_GROUPING;
3389       ia2_role_ = IA2_ROLE_SECTION;
3390       ia_state_ |= STATE_SYSTEM_READONLY;
3391       break;
3392     case ui::AX_ROLE_BUSY_INDICATOR:
3393       ia_role_ = ROLE_SYSTEM_ANIMATION;
3394       ia_state_ |= STATE_SYSTEM_READONLY;
3395       break;
3396     case ui::AX_ROLE_BUTTON:
3397       ia_role_ = ROLE_SYSTEM_PUSHBUTTON;
3398       bool is_aria_pressed_defined;
3399       bool is_mixed;
3400       if (GetAriaTristate("aria-pressed", &is_aria_pressed_defined, &is_mixed))
3401         ia_state_ |= STATE_SYSTEM_PRESSED;
3402       if (is_aria_pressed_defined)
3403         ia2_role_ = IA2_ROLE_TOGGLE_BUTTON;
3404       if (is_mixed)
3405         ia_state_ |= STATE_SYSTEM_MIXED;
3406       break;
3407     case ui::AX_ROLE_CANVAS:
3408       if (GetBoolAttribute(ui::AX_ATTR_CANVAS_HAS_FALLBACK)) {
3409         role_name_ = L"canvas";
3410         ia2_role_ = IA2_ROLE_CANVAS;
3411       } else {
3412         ia_role_ = ROLE_SYSTEM_GRAPHIC;
3413       }
3414       break;
3415     case ui::AX_ROLE_CELL:
3416       ia_role_ = ROLE_SYSTEM_CELL;
3417       break;
3418     case ui::AX_ROLE_CHECK_BOX:
3419       ia_role_ = ROLE_SYSTEM_CHECKBUTTON;
3420       break;
3421     case ui::AX_ROLE_COLOR_WELL:
3422       ia_role_ = ROLE_SYSTEM_CLIENT;
3423       ia2_role_ = IA2_ROLE_COLOR_CHOOSER;
3424       break;
3425     case ui::AX_ROLE_COLUMN:
3426       ia_role_ = ROLE_SYSTEM_COLUMN;
3427       ia_state_ |= STATE_SYSTEM_READONLY;
3428       break;
3429     case ui::AX_ROLE_COLUMN_HEADER:
3430       ia_role_ = ROLE_SYSTEM_COLUMNHEADER;
3431       ia_state_ |= STATE_SYSTEM_READONLY;
3432       break;
3433     case ui::AX_ROLE_COMBO_BOX:
3434       ia_role_ = ROLE_SYSTEM_COMBOBOX;
3435       break;
3436     case ui::AX_ROLE_DIV:
3437       role_name_ = L"div";
3438       ia2_role_ = IA2_ROLE_SECTION;
3439       break;
3440     case ui::AX_ROLE_DEFINITION:
3441       role_name_ = html_tag;
3442       ia2_role_ = IA2_ROLE_PARAGRAPH;
3443       ia_state_ |= STATE_SYSTEM_READONLY;
3444       break;
3445     case ui::AX_ROLE_DESCRIPTION_LIST_DETAIL:
3446       role_name_ = html_tag;
3447       ia2_role_ = IA2_ROLE_PARAGRAPH;
3448       ia_state_ |= STATE_SYSTEM_READONLY;
3449       break;
3450     case ui::AX_ROLE_DESCRIPTION_LIST_TERM:
3451       ia_role_ = ROLE_SYSTEM_LISTITEM;
3452       ia_state_ |= STATE_SYSTEM_READONLY;
3453       break;
3454     case ui::AX_ROLE_DIALOG:
3455       ia_role_ = ROLE_SYSTEM_DIALOG;
3456       ia_state_ |= STATE_SYSTEM_READONLY;
3457       break;
3458     case ui::AX_ROLE_DISCLOSURE_TRIANGLE:
3459       ia_role_ = ROLE_SYSTEM_OUTLINEBUTTON;
3460       ia_state_ |= STATE_SYSTEM_READONLY;
3461       break;
3462     case ui::AX_ROLE_DOCUMENT:
3463     case ui::AX_ROLE_ROOT_WEB_AREA:
3464     case ui::AX_ROLE_WEB_AREA:
3465       ia_role_ = ROLE_SYSTEM_DOCUMENT;
3466       ia_state_ |= STATE_SYSTEM_READONLY;
3467       ia_state_ |= STATE_SYSTEM_FOCUSABLE;
3468       break;
3469     case ui::AX_ROLE_EDITABLE_TEXT:
3470       ia_role_ = ROLE_SYSTEM_TEXT;
3471       ia2_state_ |= IA2_STATE_SINGLE_LINE;
3472       ia2_state_ |= IA2_STATE_EDITABLE;
3473       break;
3474     case ui::AX_ROLE_FORM:
3475       role_name_ = L"form";
3476       ia2_role_ = IA2_ROLE_FORM;
3477       break;
3478     case ui::AX_ROLE_FOOTER:
3479       ia_role_ = IA2_ROLE_FOOTER;
3480       ia_state_ |= STATE_SYSTEM_READONLY;
3481       break;
3482     case ui::AX_ROLE_GRID:
3483       ia_role_ = ROLE_SYSTEM_TABLE;
3484       ia_state_ |= STATE_SYSTEM_READONLY;
3485       break;
3486     case ui::AX_ROLE_GROUP: {
3487       base::string16 aria_role = GetString16Attribute(
3488           ui::AX_ATTR_ROLE);
3489       if (aria_role == L"group" || html_tag == L"fieldset") {
3490         ia_role_ = ROLE_SYSTEM_GROUPING;
3491       } else if (html_tag == L"li") {
3492         ia_role_ = ROLE_SYSTEM_LISTITEM;
3493       } else {
3494         if (html_tag.empty())
3495           role_name_ = L"div";
3496         else
3497           role_name_ = html_tag;
3498         ia2_role_ = IA2_ROLE_SECTION;
3499       }
3500       ia_state_ |= STATE_SYSTEM_READONLY;
3501       break;
3502     }
3503     case ui::AX_ROLE_GROW_AREA:
3504       ia_role_ = ROLE_SYSTEM_GRIP;
3505       ia_state_ |= STATE_SYSTEM_READONLY;
3506       break;
3507     case ui::AX_ROLE_HEADING:
3508       role_name_ = html_tag;
3509       ia2_role_ = IA2_ROLE_HEADING;
3510       ia_state_ |= STATE_SYSTEM_READONLY;
3511       break;
3512     case ui::AX_ROLE_HORIZONTAL_RULE:
3513       ia_role_ = ROLE_SYSTEM_SEPARATOR;
3514       break;
3515     case ui::AX_ROLE_IMAGE:
3516       ia_role_ = ROLE_SYSTEM_GRAPHIC;
3517       ia_state_ |= STATE_SYSTEM_READONLY;
3518       break;
3519     case ui::AX_ROLE_IMAGE_MAP:
3520       role_name_ = html_tag;
3521       ia2_role_ = IA2_ROLE_IMAGE_MAP;
3522       ia_state_ |= STATE_SYSTEM_READONLY;
3523       break;
3524     case ui::AX_ROLE_IMAGE_MAP_LINK:
3525       ia_role_ = ROLE_SYSTEM_LINK;
3526       ia_state_ |= STATE_SYSTEM_LINKED;
3527       ia_state_ |= STATE_SYSTEM_READONLY;
3528       break;
3529     case ui::AX_ROLE_LABEL:
3530       ia_role_ = ROLE_SYSTEM_TEXT;
3531       ia2_role_ = IA2_ROLE_LABEL;
3532       break;
3533     case ui::AX_ROLE_BANNER:
3534     case ui::AX_ROLE_COMPLEMENTARY:
3535     case ui::AX_ROLE_CONTENT_INFO:
3536     case ui::AX_ROLE_MAIN:
3537     case ui::AX_ROLE_NAVIGATION:
3538     case ui::AX_ROLE_SEARCH:
3539       ia_role_ = ROLE_SYSTEM_GROUPING;
3540       ia2_role_ = IA2_ROLE_SECTION;
3541       ia_state_ |= STATE_SYSTEM_READONLY;
3542       break;
3543     case ui::AX_ROLE_LINK:
3544       ia_role_ = ROLE_SYSTEM_LINK;
3545       ia_state_ |= STATE_SYSTEM_LINKED;
3546       break;
3547     case ui::AX_ROLE_LIST:
3548       ia_role_ = ROLE_SYSTEM_LIST;
3549       ia_state_ |= STATE_SYSTEM_READONLY;
3550       break;
3551     case ui::AX_ROLE_LIST_BOX:
3552       ia_role_ = ROLE_SYSTEM_LIST;
3553       break;
3554     case ui::AX_ROLE_LIST_BOX_OPTION:
3555       ia_role_ = ROLE_SYSTEM_LISTITEM;
3556       if (ia_state_ & STATE_SYSTEM_SELECTABLE) {
3557         ia_state_ |= STATE_SYSTEM_FOCUSABLE;
3558         if (HasState(ui::AX_STATE_FOCUSED))
3559           ia_state_ |= STATE_SYSTEM_FOCUSED;
3560       }
3561       break;
3562     case ui::AX_ROLE_LIST_ITEM:
3563       ia_role_ = ROLE_SYSTEM_LISTITEM;
3564       ia_state_ |= STATE_SYSTEM_READONLY;
3565       break;
3566     case ui::AX_ROLE_LIST_MARKER:
3567       ia_role_ = ROLE_SYSTEM_TEXT;
3568       ia_state_ |= STATE_SYSTEM_READONLY;
3569       break;
3570     case ui::AX_ROLE_MATH_ELEMENT:
3571       ia_role_ = ROLE_SYSTEM_EQUATION;
3572       ia_state_ |= STATE_SYSTEM_READONLY;
3573       break;
3574     case ui::AX_ROLE_MENU:
3575     case ui::AX_ROLE_MENU_BUTTON:
3576       ia_role_ = ROLE_SYSTEM_MENUPOPUP;
3577       break;
3578     case ui::AX_ROLE_MENU_BAR:
3579       ia_role_ = ROLE_SYSTEM_MENUBAR;
3580       break;
3581     case ui::AX_ROLE_MENU_ITEM:
3582       ia_role_ = ROLE_SYSTEM_MENUITEM;
3583       break;
3584     case ui::AX_ROLE_MENU_LIST_POPUP:
3585       ia_role_ = ROLE_SYSTEM_CLIENT;
3586       break;
3587     case ui::AX_ROLE_MENU_LIST_OPTION:
3588       ia_role_ = ROLE_SYSTEM_LISTITEM;
3589       if (ia_state_ & STATE_SYSTEM_SELECTABLE) {
3590         ia_state_ |= STATE_SYSTEM_FOCUSABLE;
3591         if (HasState(ui::AX_STATE_FOCUSED))
3592           ia_state_ |= STATE_SYSTEM_FOCUSED;
3593       }
3594       break;
3595     case ui::AX_ROLE_NOTE:
3596       ia_role_ = ROLE_SYSTEM_GROUPING;
3597       ia2_role_ = IA2_ROLE_NOTE;
3598       ia_state_ |= STATE_SYSTEM_READONLY;
3599       break;
3600     case ui::AX_ROLE_OUTLINE:
3601       ia_role_ = ROLE_SYSTEM_OUTLINE;
3602       ia_state_ |= STATE_SYSTEM_READONLY;
3603       break;
3604     case ui::AX_ROLE_PARAGRAPH:
3605       role_name_ = L"P";
3606       ia2_role_ = IA2_ROLE_PARAGRAPH;
3607       break;
3608     case ui::AX_ROLE_POP_UP_BUTTON:
3609       if (html_tag == L"select") {
3610         ia_role_ = ROLE_SYSTEM_COMBOBOX;
3611       } else {
3612         ia_role_ = ROLE_SYSTEM_BUTTONMENU;
3613       }
3614       break;
3615     case ui::AX_ROLE_PROGRESS_INDICATOR:
3616       ia_role_ = ROLE_SYSTEM_PROGRESSBAR;
3617       ia_state_ |= STATE_SYSTEM_READONLY;
3618       break;
3619     case ui::AX_ROLE_RADIO_BUTTON:
3620       ia_role_ = ROLE_SYSTEM_RADIOBUTTON;
3621       break;
3622     case ui::AX_ROLE_RADIO_GROUP:
3623       ia_role_ = ROLE_SYSTEM_GROUPING;
3624       ia2_role_ = IA2_ROLE_SECTION;
3625       break;
3626     case ui::AX_ROLE_REGION:
3627       ia_role_ = ROLE_SYSTEM_GROUPING;
3628       ia2_role_ = IA2_ROLE_SECTION;
3629       ia_state_ |= STATE_SYSTEM_READONLY;
3630       break;
3631     case ui::AX_ROLE_ROW:
3632       ia_role_ = ROLE_SYSTEM_ROW;
3633       ia_state_ |= STATE_SYSTEM_READONLY;
3634       break;
3635     case ui::AX_ROLE_ROW_HEADER:
3636       ia_role_ = ROLE_SYSTEM_ROWHEADER;
3637       ia_state_ |= STATE_SYSTEM_READONLY;
3638       break;
3639     case ui::AX_ROLE_RULER:
3640       ia_role_ = ROLE_SYSTEM_CLIENT;
3641       ia2_role_ = IA2_ROLE_RULER;
3642       ia_state_ |= STATE_SYSTEM_READONLY;
3643       break;
3644     case ui::AX_ROLE_SCROLL_AREA:
3645       ia_role_ = ROLE_SYSTEM_CLIENT;
3646       ia2_role_ = IA2_ROLE_SCROLL_PANE;
3647       ia_state_ |= STATE_SYSTEM_READONLY;
3648       break;
3649     case ui::AX_ROLE_SCROLL_BAR:
3650       ia_role_ = ROLE_SYSTEM_SCROLLBAR;
3651       break;
3652     case ui::AX_ROLE_SLIDER:
3653       ia_role_ = ROLE_SYSTEM_SLIDER;
3654       break;
3655     case ui::AX_ROLE_SPIN_BUTTON:
3656       ia_role_ = ROLE_SYSTEM_SPINBUTTON;
3657       break;
3658     case ui::AX_ROLE_SPIN_BUTTON_PART:
3659       ia_role_ = ROLE_SYSTEM_PUSHBUTTON;
3660       break;
3661     case ui::AX_ROLE_SPLIT_GROUP:
3662       ia_role_ = ROLE_SYSTEM_CLIENT;
3663       ia2_role_ = IA2_ROLE_SPLIT_PANE;
3664       ia_state_ |= STATE_SYSTEM_READONLY;
3665       break;
3666     case ui::AX_ROLE_ANNOTATION:
3667     case ui::AX_ROLE_STATIC_TEXT:
3668       ia_role_ = ROLE_SYSTEM_TEXT;
3669       ia_state_ |= STATE_SYSTEM_READONLY;
3670       break;
3671     case ui::AX_ROLE_STATUS:
3672       ia_role_ = ROLE_SYSTEM_STATUSBAR;
3673       ia_state_ |= STATE_SYSTEM_READONLY;
3674       break;
3675     case ui::AX_ROLE_SPLITTER:
3676       ia_role_ = ROLE_SYSTEM_SEPARATOR;
3677       break;
3678     case ui::AX_ROLE_SVG_ROOT:
3679       ia_role_ = ROLE_SYSTEM_GRAPHIC;
3680       break;
3681     case ui::AX_ROLE_TAB:
3682       ia_role_ = ROLE_SYSTEM_PAGETAB;
3683       break;
3684     case ui::AX_ROLE_TABLE: {
3685       base::string16 aria_role = GetString16Attribute(
3686           ui::AX_ATTR_ROLE);
3687       if (aria_role == L"treegrid") {
3688         ia_role_ = ROLE_SYSTEM_OUTLINE;
3689       } else {
3690         ia_role_ = ROLE_SYSTEM_TABLE;
3691         ia_state_ |= STATE_SYSTEM_READONLY;
3692       }
3693       break;
3694     }
3695     case ui::AX_ROLE_TABLE_HEADER_CONTAINER:
3696       ia_role_ = ROLE_SYSTEM_GROUPING;
3697       ia2_role_ = IA2_ROLE_SECTION;
3698       ia_state_ |= STATE_SYSTEM_READONLY;
3699       break;
3700     case ui::AX_ROLE_TAB_LIST:
3701       ia_role_ = ROLE_SYSTEM_PAGETABLIST;
3702       break;
3703     case ui::AX_ROLE_TAB_PANEL:
3704       ia_role_ = ROLE_SYSTEM_PROPERTYPAGE;
3705       break;
3706     case ui::AX_ROLE_TOGGLE_BUTTON:
3707       ia_role_ = ROLE_SYSTEM_PUSHBUTTON;
3708       ia2_role_ = IA2_ROLE_TOGGLE_BUTTON;
3709       break;
3710     case ui::AX_ROLE_TEXT_AREA:
3711       ia_role_ = ROLE_SYSTEM_TEXT;
3712       ia2_state_ |= IA2_STATE_MULTI_LINE;
3713       ia2_state_ |= IA2_STATE_EDITABLE;
3714       ia2_state_ |= IA2_STATE_SELECTABLE_TEXT;
3715       break;
3716     case ui::AX_ROLE_TEXT_FIELD:
3717       ia_role_ = ROLE_SYSTEM_TEXT;
3718       ia2_state_ |= IA2_STATE_SINGLE_LINE;
3719       ia2_state_ |= IA2_STATE_EDITABLE;
3720       ia2_state_ |= IA2_STATE_SELECTABLE_TEXT;
3721       break;
3722     case ui::AX_ROLE_TIMER:
3723       ia_role_ = ROLE_SYSTEM_CLOCK;
3724       ia_state_ |= STATE_SYSTEM_READONLY;
3725       break;
3726     case ui::AX_ROLE_TOOLBAR:
3727       ia_role_ = ROLE_SYSTEM_TOOLBAR;
3728       ia_state_ |= STATE_SYSTEM_READONLY;
3729       break;
3730     case ui::AX_ROLE_TOOLTIP:
3731       ia_role_ = ROLE_SYSTEM_TOOLTIP;
3732       ia_state_ |= STATE_SYSTEM_READONLY;
3733       break;
3734     case ui::AX_ROLE_TREE:
3735       ia_role_ = ROLE_SYSTEM_OUTLINE;
3736       ia_state_ |= STATE_SYSTEM_READONLY;
3737       break;
3738     case ui::AX_ROLE_TREE_GRID:
3739       ia_role_ = ROLE_SYSTEM_OUTLINE;
3740       ia_state_ |= STATE_SYSTEM_READONLY;
3741       break;
3742     case ui::AX_ROLE_TREE_ITEM:
3743       ia_role_ = ROLE_SYSTEM_OUTLINEITEM;
3744       ia_state_ |= STATE_SYSTEM_READONLY;
3745       break;
3746     case ui::AX_ROLE_WINDOW:
3747       ia_role_ = ROLE_SYSTEM_WINDOW;
3748       break;
3749
3750     // TODO(dmazzoni): figure out the proper MSAA role for all of these.
3751     case ui::AX_ROLE_BROWSER:
3752     case ui::AX_ROLE_DIRECTORY:
3753     case ui::AX_ROLE_DRAWER:
3754     case ui::AX_ROLE_HELP_TAG:
3755     case ui::AX_ROLE_IGNORED:
3756     case ui::AX_ROLE_INCREMENTOR:
3757     case ui::AX_ROLE_LOG:
3758     case ui::AX_ROLE_MARQUEE:
3759     case ui::AX_ROLE_MATTE:
3760     case ui::AX_ROLE_PRESENTATIONAL:
3761     case ui::AX_ROLE_RULER_MARKER:
3762     case ui::AX_ROLE_SHEET:
3763     case ui::AX_ROLE_SLIDER_THUMB:
3764     case ui::AX_ROLE_SYSTEM_WIDE:
3765     case ui::AX_ROLE_VALUE_INDICATOR:
3766     default:
3767       ia_role_ = ROLE_SYSTEM_CLIENT;
3768       break;
3769   }
3770
3771   // Compute the final value of READONLY for MSAA.
3772   //
3773   // We always set the READONLY state for elements that have the
3774   // aria-readonly attribute and for a few roles (in the switch above).
3775   // We clear the READONLY state on focusable controls and on a document.
3776   // Everything else, the majority of objects, do not have this state set.
3777   if (HasState(ui::AX_STATE_FOCUSABLE) &&
3778       ia_role_ != ROLE_SYSTEM_DOCUMENT) {
3779     ia_state_ &= ~(STATE_SYSTEM_READONLY);
3780   }
3781   if (!HasState(ui::AX_STATE_READONLY))
3782     ia_state_ &= ~(STATE_SYSTEM_READONLY);
3783   if (GetBoolAttribute(ui::AX_ATTR_ARIA_READONLY))
3784     ia_state_ |= STATE_SYSTEM_READONLY;
3785
3786   // The role should always be set.
3787   DCHECK(!role_name_.empty() || ia_role_);
3788
3789   // If we didn't explicitly set the IAccessible2 role, make it the same
3790   // as the MSAA role.
3791   if (!ia2_role_)
3792     ia2_role_ = ia_role_;
3793 }
3794
3795 }  // namespace content