summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/accessibility_event_router_views.cc
blob: 670e2f084084aaa2315a0eeb0f4bea6c281e7a1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/views/accessibility_event_router_views.h"

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_accessibility_api.h"
#include "chrome/browser/profile.h"
#include "chrome/common/notification_type.h"
#include "views/accessibility/accessibility_types.h"
#include "views/controls/button/image_button.h"
#include "views/controls/button/menu_button.h"
#include "views/controls/button/native_button.h"
#include "views/controls/link.h"
#include "views/controls/menu/menu_item_view.h"
#include "views/controls/menu/submenu_view.h"

using views::FocusManager;
using views::View;

AccessibilityEventRouterViews::AccessibilityEventRouterViews()
    : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
}

AccessibilityEventRouterViews::~AccessibilityEventRouterViews() {
}

// static
AccessibilityEventRouterViews* AccessibilityEventRouterViews::GetInstance() {
  return Singleton<AccessibilityEventRouterViews>::get();
}

bool AccessibilityEventRouterViews::AddViewTree(View* view, Profile* profile) {
  if (view_tree_profile_map_[view] != NULL)
    return false;

  view_tree_profile_map_[view] = profile;
  return true;
}

void AccessibilityEventRouterViews::RemoveViewTree(View* view) {
  DCHECK(view_tree_profile_map_.find(view) !=
         view_tree_profile_map_.end());
  view_tree_profile_map_.erase(view);
}

void AccessibilityEventRouterViews::IgnoreView(View* view) {
  view_info_map_[view].ignore = true;
}

void AccessibilityEventRouterViews::SetViewName(View* view, std::string name) {
  view_info_map_[view].name = name;
}

void AccessibilityEventRouterViews::RemoveView(View* view) {
  DCHECK(view_info_map_.find(view) != view_info_map_.end());
  view_info_map_.erase(view);
}

void AccessibilityEventRouterViews::HandleAccessibilityEvent(
    views::View* view, AccessibilityTypes::Event event_type) {
  switch (event_type) {
    case AccessibilityTypes::EVENT_FOCUS:
      DispatchAccessibilityNotification(
          view, NotificationType::ACCESSIBILITY_CONTROL_FOCUSED);
      break;
    case AccessibilityTypes::EVENT_MENUSTART:
    case AccessibilityTypes::EVENT_MENUPOPUPSTART:
      DispatchAccessibilityNotification(
          view, NotificationType::ACCESSIBILITY_MENU_OPENED);
      break;
    case AccessibilityTypes::EVENT_MENUEND:
    case AccessibilityTypes::EVENT_MENUPOPUPEND:
      DispatchAccessibilityNotification(
          view, NotificationType::ACCESSIBILITY_MENU_CLOSED);
      break;
  }
}

//
// Private methods
//

void AccessibilityEventRouterViews::FindView(
    View* view, Profile** profile, bool* is_accessible) {
  *is_accessible = false;

  // First see if it's a descendant of an accessible view.
  for (base::hash_map<View*, Profile*>::const_iterator iter =
           view_tree_profile_map_.begin();
       iter != view_tree_profile_map_.end();
       ++iter) {
    if (iter->first->IsParentOf(view)) {
      *is_accessible = true;
      if (profile)
        *profile = iter->second;
      break;
    }
  }
  if (!*is_accessible)
    return;

  // Now make sure it's not marked as a widget to be ignored.
  base::hash_map<View*, ViewInfo>::const_iterator iter =
      view_info_map_.find(view);
  if (iter != view_info_map_.end() && iter->second.ignore)
    *is_accessible = false;
}

std::string AccessibilityEventRouterViews::GetViewName(View* view) {
  std::string name;

  // First see if we have a name registered for this view.
  base::hash_map<View*, ViewInfo>::const_iterator iter =
      view_info_map_.find(view);
  if (iter != view_info_map_.end())
    name = iter->second.name;

  // Otherwise ask the view for its accessible name.
  if (name.empty()) {
    std::wstring wname;
    view->GetAccessibleName(&wname);
    name = WideToUTF8(wname);
  }

  return name;
}

void AccessibilityEventRouterViews::DispatchAccessibilityNotification(
    View* view, NotificationType type) {
  Profile* profile = NULL;
  bool is_accessible;
  FindView(view, &profile, &is_accessible);

  // Special case: a menu isn't associated with any particular top-level
  // window, so menu events get routed to the profile of the most recent
  // event that was associated with a window, which should be the window
  // that triggered opening the menu.
  bool is_menu_event = IsMenuEvent(view, type);
  if (is_menu_event && !profile && most_recent_profile_) {
    profile = most_recent_profile_;
    is_accessible = true;
  }

  if (!is_accessible)
    return;

  most_recent_profile_ = profile;

  AccessibilityTypes::Role role;
  view->GetAccessibleRole(&role);
  std::string class_name = view->GetClassName();

  if (class_name == views::MenuButton::kViewClassName ||
      type == NotificationType::ACCESSIBILITY_MENU_OPENED ||
      type == NotificationType::ACCESSIBILITY_MENU_CLOSED) {
    SendMenuNotification(view, type, profile);
  } else if (is_menu_event) {
    SendMenuItemNotification(view, type, profile);
  } else if (class_name == views::ImageButton::kViewClassName ||
             class_name == views::NativeButton::kViewClassName ||
             class_name == views::TextButton::kViewClassName) {
    SendButtonNotification(view, type, profile);
  } else if (class_name == views::Link::kViewClassName) {
    SendLinkNotification(view, type, profile);
  }
}

void AccessibilityEventRouterViews::SendButtonNotification(
    View* view, NotificationType type, Profile* profile) {
  AccessibilityButtonInfo info(profile, GetViewName(view));
  SendAccessibilityNotification(type, &info);
}

void AccessibilityEventRouterViews::SendLinkNotification(
    View* view, NotificationType type, Profile* profile) {
  AccessibilityLinkInfo info(profile, GetViewName(view));
  SendAccessibilityNotification(type, &info);
}

void AccessibilityEventRouterViews::SendMenuNotification(
    View* view, NotificationType type, Profile* profile) {
  AccessibilityMenuInfo info(profile, GetViewName(view));
  SendAccessibilityNotification(type, &info);
}

void AccessibilityEventRouterViews::SendMenuItemNotification(
    View* view, NotificationType type, Profile* profile) {
  std::string name = GetViewName(view);

  bool has_submenu = false;
  int index = -1;
  int count = -1;

  if (view->GetClassName() == views::MenuItemView::kViewClassName)
    has_submenu = static_cast<views::MenuItemView*>(view)->HasSubmenu();

  View* parent_menu = view->GetParent();
  while (parent_menu != NULL && parent_menu->GetClassName() !=
         views::SubmenuView::kViewClassName) {
    parent_menu = parent_menu->GetParent();
  }
  if (parent_menu) {
    count = 0;
    RecursiveGetMenuItemIndexAndCount(parent_menu, view, &index, &count);
  }

  AccessibilityMenuItemInfo info(profile, name, has_submenu, index, count);
  SendAccessibilityNotification(type, &info);
}

void AccessibilityEventRouterViews::RecursiveGetMenuItemIndexAndCount(
    views::View* menu, views::View* item, int* index, int* count) {
  for (int i = 0; i < menu->GetChildViewCount(); ++i) {
    views::View* child = menu->GetChildViewAt(i);
    int previous_count = *count;
    RecursiveGetMenuItemIndexAndCount(child, item, index, count);
    if (child->GetClassName() == views::MenuItemView::kViewClassName &&
        *count == previous_count) {
      if (item == child)
        *index = *count;
      (*count)++;
    } else if (child->GetClassName() == views::TextButton::kViewClassName) {
      if (item == child)
        *index = *count;
      (*count)++;
    }
  }
}

bool AccessibilityEventRouterViews::IsMenuEvent(
    View* view, NotificationType type) {
  if (type == NotificationType::ACCESSIBILITY_MENU_OPENED ||
      type == NotificationType::ACCESSIBILITY_MENU_CLOSED)
    return true;

  while (view) {
    AccessibilityTypes::Role role;
    view->GetAccessibleRole(&role);
    if (role == AccessibilityTypes::ROLE_MENUITEM ||
        role == AccessibilityTypes::ROLE_MENUPOPUP) {
      return true;
    }
    view = view->GetParent();
  }

  return false;
}