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