Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_auralinux.cc
blob9fb8e4b3e93d24be6d40de299c934cc7e4746bf0
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/accessibility/browser_accessibility_auralinux.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/browser/accessibility/browser_accessibility_manager_auralinux.h"
9 #include "content/common/accessibility_messages.h"
11 namespace content {
13 static gpointer browser_accessibility_parent_class = NULL;
15 static BrowserAccessibilityAuraLinux* ToBrowserAccessibilityAuraLinux(
16 BrowserAccessibilityAtk* atk_object) {
17 if (!atk_object)
18 return NULL;
20 return atk_object->m_object;
24 // AtkAction interface.
27 static BrowserAccessibilityAuraLinux* ToBrowserAccessibilityAuraLinux(
28 AtkAction* atk_action) {
29 if (!IS_BROWSER_ACCESSIBILITY(atk_action))
30 return NULL;
32 return ToBrowserAccessibilityAuraLinux(BROWSER_ACCESSIBILITY(atk_action));
35 static gboolean browser_accessibility_do_action(AtkAction* atk_action,
36 gint index) {
37 g_return_val_if_fail(ATK_IS_ACTION(atk_action), FALSE);
38 g_return_val_if_fail(!index, FALSE);
40 BrowserAccessibilityAuraLinux* obj =
41 ToBrowserAccessibilityAuraLinux(atk_action);
42 if (!obj)
43 return FALSE;
45 obj->manager()->DoDefaultAction(*obj);
47 return TRUE;
50 static gint browser_accessibility_get_n_actions(AtkAction* atk_action) {
51 g_return_val_if_fail(ATK_IS_ACTION(atk_action), 0);
53 BrowserAccessibilityAuraLinux* obj =
54 ToBrowserAccessibilityAuraLinux(atk_action);
55 if (!obj)
56 return 0;
58 return 1;
61 static const gchar* browser_accessibility_get_description(AtkAction* atk_action,
62 gint) {
63 g_return_val_if_fail(ATK_IS_ACTION(atk_action), 0);
64 BrowserAccessibilityAuraLinux* obj =
65 ToBrowserAccessibilityAuraLinux(atk_action);
66 if (!obj)
67 return 0;
69 return 0;
72 static const gchar* browser_accessibility_get_name(AtkAction* atk_action,
73 gint index) {
74 g_return_val_if_fail(ATK_IS_ACTION(atk_action), 0);
75 g_return_val_if_fail(!index, 0);
76 BrowserAccessibilityAuraLinux* obj =
77 ToBrowserAccessibilityAuraLinux(atk_action);
78 if (!obj)
79 return 0;
81 return obj->GetStringAttribute(ui::AX_ATTR_ACTION).c_str();
84 static const gchar* browser_accessibility_get_keybinding(AtkAction* atk_action,
85 gint index) {
86 g_return_val_if_fail(ATK_IS_ACTION(atk_action), 0);
87 g_return_val_if_fail(!index, 0);
88 BrowserAccessibilityAuraLinux* obj =
89 ToBrowserAccessibilityAuraLinux(atk_action);
90 if (!obj)
91 return 0;
93 return obj->GetStringAttribute(ui::AX_ATTR_ACCESS_KEY).c_str();
96 static void ActionInterfaceInit(AtkActionIface* iface) {
97 iface->do_action = browser_accessibility_do_action;
98 iface->get_n_actions = browser_accessibility_get_n_actions;
99 iface->get_description = browser_accessibility_get_description;
100 iface->get_name = browser_accessibility_get_name;
101 iface->get_keybinding = browser_accessibility_get_keybinding;
104 static const GInterfaceInfo ActionInfo = {
105 reinterpret_cast<GInterfaceInitFunc>(ActionInterfaceInit), 0, 0};
108 // AtkComponent interface.
111 static BrowserAccessibilityAuraLinux* ToBrowserAccessibilityAuraLinux(
112 AtkComponent* atk_component) {
113 if (!IS_BROWSER_ACCESSIBILITY(atk_component))
114 return NULL;
116 return ToBrowserAccessibilityAuraLinux(BROWSER_ACCESSIBILITY(atk_component));
119 static AtkObject* browser_accessibility_accessible_at_point(
120 AtkComponent* atk_component,
121 gint x,
122 gint y,
123 AtkCoordType coord_type) {
124 g_return_val_if_fail(ATK_IS_COMPONENT(atk_component), 0);
126 BrowserAccessibilityAuraLinux* obj =
127 ToBrowserAccessibilityAuraLinux(atk_component);
128 if (!obj)
129 return NULL;
131 gfx::Point point(x, y);
132 if (!obj->GetGlobalBoundsRect().Contains(point))
133 return NULL;
135 BrowserAccessibility* result = obj->BrowserAccessibilityForPoint(point);
136 if (!result)
137 return NULL;
139 AtkObject* atk_result =
140 result->ToBrowserAccessibilityAuraLinux()->GetAtkObject();
141 g_object_ref(atk_result);
142 return atk_result;
145 static void browser_accessibility_get_extents(AtkComponent* atk_component,
146 gint* x,
147 gint* y,
148 gint* width,
149 gint* height,
150 AtkCoordType coord_type) {
151 g_return_if_fail(ATK_IS_COMPONENT(atk_component));
153 BrowserAccessibilityAuraLinux* obj =
154 ToBrowserAccessibilityAuraLinux(atk_component);
155 if (!obj)
156 return;
158 gfx::Rect bounds = obj->GetGlobalBoundsRect();
159 if (x)
160 *x = bounds.x();
161 if (y)
162 *y = bounds.y();
163 if (width)
164 *width = bounds.width();
165 if (height)
166 *height = bounds.height();
169 static gboolean browser_accessibility_grab_focus(AtkComponent* atk_component) {
170 g_return_val_if_fail(ATK_IS_COMPONENT(atk_component), FALSE);
172 BrowserAccessibilityAuraLinux* obj =
173 ToBrowserAccessibilityAuraLinux(atk_component);
174 if (!obj)
175 return false;
177 obj->manager()->SetFocus(obj, true);
178 return true;
181 static void ComponentInterfaceInit(AtkComponentIface* iface) {
182 iface->ref_accessible_at_point = browser_accessibility_accessible_at_point;
183 iface->get_extents = browser_accessibility_get_extents;
184 iface->grab_focus = browser_accessibility_grab_focus;
187 static const GInterfaceInfo ComponentInfo = {
188 reinterpret_cast<GInterfaceInitFunc>(ComponentInterfaceInit),
193 // AtkDocument interface.
196 static BrowserAccessibilityAuraLinux* ToBrowserAccessibilityAuraLinux(
197 AtkDocument* atk_doc) {
198 if (!IS_BROWSER_ACCESSIBILITY(atk_doc))
199 return NULL;
201 return ToBrowserAccessibilityAuraLinux(BROWSER_ACCESSIBILITY(atk_doc));
204 static const gchar* GetDocumentAttributeValue(
205 BrowserAccessibilityAuraLinux* obj,
206 const gchar* attribute) {
207 if (!g_ascii_strcasecmp(attribute, "DocType"))
208 return obj->GetStringAttribute(ui::AX_ATTR_DOC_DOCTYPE).c_str();
209 else if (!g_ascii_strcasecmp(attribute, "MimeType"))
210 return obj->GetStringAttribute(ui::AX_ATTR_DOC_MIMETYPE).c_str();
211 else if (!g_ascii_strcasecmp(attribute, "Title"))
212 return obj->GetStringAttribute(ui::AX_ATTR_DOC_TITLE).c_str();
213 else if (!g_ascii_strcasecmp(attribute, "URI"))
214 return obj->GetStringAttribute(ui::AX_ATTR_DOC_URL).c_str();
216 return 0;
219 AtkAttributeSet* SetAtkAttributeSet(AtkAttributeSet* attribute_set,
220 const char* name,
221 const char* value) {
222 AtkAttribute* attribute =
223 static_cast<AtkAttribute*>(g_malloc(sizeof(AtkAttribute)));
224 attribute->name = g_strdup(name);
225 attribute->value = g_strdup(value);
226 attribute_set = g_slist_prepend(attribute_set, attribute);
227 return attribute_set;
230 static const gchar* browser_accessibility_get_attribute_value(
231 AtkDocument* atk_doc,
232 const gchar* attribute) {
233 g_return_val_if_fail(ATK_IS_DOCUMENT(atk_doc), 0);
234 BrowserAccessibilityAuraLinux* obj = ToBrowserAccessibilityAuraLinux(atk_doc);
235 if (!obj)
236 return 0;
238 return GetDocumentAttributeValue(obj, attribute);
241 static AtkAttributeSet* browser_accessibility_get_attributes(
242 AtkDocument* atk_doc) {
243 g_return_val_if_fail(ATK_IS_DOCUMENT(atk_doc), 0);
244 BrowserAccessibilityAuraLinux* obj = ToBrowserAccessibilityAuraLinux(atk_doc);
245 if (!obj)
246 return 0;
248 AtkAttributeSet* attribute_set = 0;
249 const gchar* doc_attributes[] = {"DocType", "MimeType", "Title", "URI"};
250 const gchar* value = 0;
252 for (unsigned i = 0; i < G_N_ELEMENTS(doc_attributes); i++) {
253 value = GetDocumentAttributeValue(obj, doc_attributes[i]);
254 if (value)
255 attribute_set =
256 SetAtkAttributeSet(attribute_set, doc_attributes[i], value);
259 return attribute_set;
262 static void DocumentInterfaceInit(AtkDocumentIface* iface) {
263 iface->get_document_attribute_value =
264 browser_accessibility_get_attribute_value;
265 iface->get_document_attributes = browser_accessibility_get_attributes;
268 static const GInterfaceInfo DocumentInfo = {
269 reinterpret_cast<GInterfaceInitFunc>(DocumentInterfaceInit), 0, 0};
272 // AtkImage interface.
275 static BrowserAccessibilityAuraLinux* ToBrowserAccessibilityAuraLinux(
276 AtkImage* atk_img) {
277 if (!IS_BROWSER_ACCESSIBILITY(atk_img))
278 return NULL;
280 return ToBrowserAccessibilityAuraLinux(BROWSER_ACCESSIBILITY(atk_img));
283 void GetImagePositionSize(BrowserAccessibilityAuraLinux* obj,
284 gint* x,
285 gint* y,
286 gint* width,
287 gint* height) {
288 gfx::Rect img_pos_size = obj->GetGlobalBoundsRect();
290 if (x)
291 *x = img_pos_size.x();
292 if (y)
293 *y = img_pos_size.y();
294 if (width)
295 *width = img_pos_size.width();
296 if (height)
297 *height = img_pos_size.height();
300 static void browser_accessibility_get_image_position(AtkImage* atk_img,
301 gint* x,
302 gint* y,
303 AtkCoordType coordType) {
304 g_return_if_fail(ATK_IMAGE(atk_img));
305 BrowserAccessibilityAuraLinux* obj = ToBrowserAccessibilityAuraLinux(atk_img);
306 if (!obj)
307 return;
309 GetImagePositionSize(obj, x, y, nullptr, nullptr);
312 static const gchar* browser_accessibility_get_image_description(
313 AtkImage* atk_img) {
314 g_return_val_if_fail(ATK_IMAGE(atk_img), 0);
315 BrowserAccessibilityAuraLinux* obj = ToBrowserAccessibilityAuraLinux(atk_img);
316 if (!obj)
317 return 0;
319 return obj->GetStringAttribute(ui::AX_ATTR_DESCRIPTION).c_str();
322 static void browser_accessibility_get_image_size(AtkImage* atk_img,
323 gint* width,
324 gint* height) {
325 g_return_if_fail(ATK_IMAGE(atk_img));
326 BrowserAccessibilityAuraLinux* obj = ToBrowserAccessibilityAuraLinux(atk_img);
327 if (!obj)
328 return;
330 GetImagePositionSize(obj, nullptr, nullptr, width, height);
333 void ImageInterfaceInit(AtkImageIface* iface) {
334 iface->get_image_position = browser_accessibility_get_image_position;
335 iface->get_image_description = browser_accessibility_get_image_description;
336 iface->get_image_size = browser_accessibility_get_image_size;
339 static const GInterfaceInfo ImageInfo = {
340 reinterpret_cast<GInterfaceInitFunc>(ImageInterfaceInit), 0, 0};
343 // AtkValue interface.
346 static BrowserAccessibilityAuraLinux* ToBrowserAccessibilityAuraLinux(
347 AtkValue* atk_object) {
348 if (!IS_BROWSER_ACCESSIBILITY(atk_object))
349 return NULL;
351 return ToBrowserAccessibilityAuraLinux(BROWSER_ACCESSIBILITY(atk_object));
354 static float GetStepAttribute(BrowserAccessibilityAuraLinux* obj) {
355 // TODO(shreeram.k): Get Correct value of Step attribute.
356 return 1.0;
359 static void browser_accessibility_get_current_value(AtkValue* atk_value,
360 GValue* value) {
361 g_return_if_fail(ATK_VALUE(atk_value));
363 BrowserAccessibilityAuraLinux* obj =
364 ToBrowserAccessibilityAuraLinux(atk_value);
365 if (!obj)
366 return;
368 float float_val;
369 if (obj->GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE, &float_val)) {
370 memset(value, 0, sizeof(*value));
371 g_value_init(value, G_TYPE_FLOAT);
372 g_value_set_float(value, float_val);
376 static void browser_accessibility_get_minimum_value(AtkValue* atk_value,
377 GValue* value) {
378 g_return_if_fail(ATK_VALUE(atk_value));
380 BrowserAccessibilityAuraLinux* obj =
381 ToBrowserAccessibilityAuraLinux(atk_value);
382 if (!obj)
383 return;
385 float float_val;
386 if (obj->GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE, &float_val)) {
387 memset(value, 0, sizeof(*value));
388 g_value_init(value, G_TYPE_FLOAT);
389 g_value_set_float(value, float_val);
393 static void browser_accessibility_get_maximum_value(AtkValue* atk_value,
394 GValue* value) {
395 g_return_if_fail(ATK_VALUE(atk_value));
397 BrowserAccessibilityAuraLinux* obj =
398 ToBrowserAccessibilityAuraLinux(atk_value);
399 if (!obj)
400 return;
402 float float_val;
403 if (obj->GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE, &float_val)) {
404 memset(value, 0, sizeof(*value));
405 g_value_init(value, G_TYPE_FLOAT);
406 g_value_set_float(value, float_val);
410 static void browser_accessibility_get_minimum_increment(AtkValue* atk_value,
411 GValue* gValue) {
412 g_return_if_fail(ATK_VALUE(atk_value));
414 memset(gValue, 0, sizeof(GValue));
415 g_value_init(gValue, G_TYPE_FLOAT);
417 BrowserAccessibilityAuraLinux* obj =
418 ToBrowserAccessibilityAuraLinux(atk_value);
419 if (!obj)
420 return;
422 g_value_set_float(gValue, GetStepAttribute(obj));
425 static void ValueInterfaceInit(AtkValueIface* iface) {
426 iface->get_current_value = browser_accessibility_get_current_value;
427 iface->get_minimum_value = browser_accessibility_get_minimum_value;
428 iface->get_maximum_value = browser_accessibility_get_maximum_value;
429 iface->get_minimum_increment = browser_accessibility_get_minimum_increment;
432 static const GInterfaceInfo ValueInfo = {
433 reinterpret_cast<GInterfaceInitFunc>(ValueInterfaceInit),
438 // AtkObject interface
441 static BrowserAccessibilityAuraLinux* ToBrowserAccessibilityAuraLinux(
442 AtkObject* atk_object) {
443 if (!IS_BROWSER_ACCESSIBILITY(atk_object))
444 return NULL;
446 return ToBrowserAccessibilityAuraLinux(BROWSER_ACCESSIBILITY(atk_object));
449 static const gchar* browser_accessibility_get_name(AtkObject* atk_object) {
450 BrowserAccessibilityAuraLinux* obj =
451 ToBrowserAccessibilityAuraLinux(atk_object);
452 if (!obj)
453 return NULL;
455 return obj->GetStringAttribute(ui::AX_ATTR_NAME).c_str();
458 static const gchar* browser_accessibility_get_description(
459 AtkObject* atk_object) {
460 BrowserAccessibilityAuraLinux* obj =
461 ToBrowserAccessibilityAuraLinux(atk_object);
462 if (!obj)
463 return NULL;
465 return obj->GetStringAttribute(ui::AX_ATTR_DESCRIPTION).c_str();
468 static AtkObject* browser_accessibility_get_parent(AtkObject* atk_object) {
469 BrowserAccessibilityAuraLinux* obj =
470 ToBrowserAccessibilityAuraLinux(atk_object);
471 if (!obj)
472 return NULL;
473 if (obj->GetParent())
474 return obj->GetParent()->ToBrowserAccessibilityAuraLinux()->GetAtkObject();
476 BrowserAccessibilityManagerAuraLinux* manager =
477 static_cast<BrowserAccessibilityManagerAuraLinux*>(obj->manager());
478 return manager->parent_object();
481 static gint browser_accessibility_get_n_children(AtkObject* atk_object) {
482 BrowserAccessibilityAuraLinux* obj =
483 ToBrowserAccessibilityAuraLinux(atk_object);
484 if (!obj)
485 return 0;
487 return obj->PlatformChildCount();
490 static AtkObject* browser_accessibility_ref_child(AtkObject* atk_object,
491 gint index) {
492 BrowserAccessibilityAuraLinux* obj =
493 ToBrowserAccessibilityAuraLinux(atk_object);
494 if (!obj)
495 return NULL;
497 if (index < 0 || index >= static_cast<gint>(obj->PlatformChildCount()))
498 return NULL;
500 AtkObject* result = obj->InternalGetChild(index)
501 ->ToBrowserAccessibilityAuraLinux()
502 ->GetAtkObject();
503 g_object_ref(result);
504 return result;
507 static gint browser_accessibility_get_index_in_parent(AtkObject* atk_object) {
508 BrowserAccessibilityAuraLinux* obj =
509 ToBrowserAccessibilityAuraLinux(atk_object);
510 if (!obj)
511 return 0;
512 return obj->GetIndexInParent();
515 static AtkAttributeSet* browser_accessibility_get_attributes(
516 AtkObject* atk_object) {
517 return NULL;
520 static AtkRole browser_accessibility_get_role(AtkObject* atk_object) {
521 BrowserAccessibilityAuraLinux* obj =
522 ToBrowserAccessibilityAuraLinux(atk_object);
523 if (!obj)
524 return ATK_ROLE_INVALID;
525 return obj->atk_role();
528 static AtkStateSet* browser_accessibility_ref_state_set(AtkObject* atk_object) {
529 BrowserAccessibilityAuraLinux* obj =
530 ToBrowserAccessibilityAuraLinux(atk_object);
531 if (!obj)
532 return NULL;
533 AtkStateSet* state_set = ATK_OBJECT_CLASS(browser_accessibility_parent_class)
534 ->ref_state_set(atk_object);
535 int32 state = obj->GetState();
537 if (state & (1 << ui::AX_STATE_FOCUSABLE))
538 atk_state_set_add_state(state_set, ATK_STATE_FOCUSABLE);
539 if (obj->manager()->GetFocus(NULL) == obj)
540 atk_state_set_add_state(state_set, ATK_STATE_FOCUSED);
541 if (state & (1 << ui::AX_STATE_ENABLED))
542 atk_state_set_add_state(state_set, ATK_STATE_ENABLED);
544 return state_set;
547 static AtkRelationSet* browser_accessibility_ref_relation_set(
548 AtkObject* atk_object) {
549 AtkRelationSet* relation_set =
550 ATK_OBJECT_CLASS(browser_accessibility_parent_class)
551 ->ref_relation_set(atk_object);
552 return relation_set;
556 // The rest of the BrowserAccessibilityAuraLinux code, not specific to one
557 // of the Atk* interfaces.
560 static void browser_accessibility_init(AtkObject* atk_object, gpointer data) {
561 if (ATK_OBJECT_CLASS(browser_accessibility_parent_class)->initialize) {
562 ATK_OBJECT_CLASS(browser_accessibility_parent_class)
563 ->initialize(atk_object, data);
566 BROWSER_ACCESSIBILITY(atk_object)->m_object =
567 reinterpret_cast<BrowserAccessibilityAuraLinux*>(data);
570 static void browser_accessibility_finalize(GObject* atk_object) {
571 G_OBJECT_CLASS(browser_accessibility_parent_class)->finalize(atk_object);
574 static void browser_accessibility_class_init(AtkObjectClass* klass) {
575 GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
576 browser_accessibility_parent_class = g_type_class_peek_parent(klass);
578 gobject_class->finalize = browser_accessibility_finalize;
579 klass->initialize = browser_accessibility_init;
580 klass->get_name = browser_accessibility_get_name;
581 klass->get_description = browser_accessibility_get_description;
582 klass->get_parent = browser_accessibility_get_parent;
583 klass->get_n_children = browser_accessibility_get_n_children;
584 klass->ref_child = browser_accessibility_ref_child;
585 klass->get_role = browser_accessibility_get_role;
586 klass->ref_state_set = browser_accessibility_ref_state_set;
587 klass->get_index_in_parent = browser_accessibility_get_index_in_parent;
588 klass->get_attributes = browser_accessibility_get_attributes;
589 klass->ref_relation_set = browser_accessibility_ref_relation_set;
592 GType browser_accessibility_get_type() {
593 static volatile gsize type_volatile = 0;
595 #if !GLIB_CHECK_VERSION(2, 36, 0)
596 g_type_init();
597 #endif
599 if (g_once_init_enter(&type_volatile)) {
600 static const GTypeInfo tinfo = {
601 sizeof(BrowserAccessibilityAtkClass),
602 (GBaseInitFunc)0,
603 (GBaseFinalizeFunc)0,
604 (GClassInitFunc)browser_accessibility_class_init,
605 (GClassFinalizeFunc)0,
606 0, /* class data */
607 sizeof(BrowserAccessibilityAtk), /* instance size */
608 0, /* nb preallocs */
609 (GInstanceInitFunc)0,
610 0 /* value table */
613 GType type = g_type_register_static(ATK_TYPE_OBJECT, "BrowserAccessibility",
614 &tinfo, GTypeFlags(0));
615 g_once_init_leave(&type_volatile, type);
618 return type_volatile;
621 static const char* GetUniqueAccessibilityTypeName(int interface_mask) {
622 // 20 characters is enough for "Chrome%x" with any integer value.
623 static char name[20];
624 snprintf(name, sizeof(name), "Chrome%x", interface_mask);
625 return name;
628 enum AtkInterfaces {
629 ATK_ACTION_INTERFACE,
630 ATK_COMPONENT_INTERFACE,
631 ATK_DOCUMENT_INTERFACE,
632 ATK_EDITABLE_TEXT_INTERFACE,
633 ATK_HYPERLINK_INTERFACE,
634 ATK_HYPERTEXT_INTERFACE,
635 ATK_IMAGE_INTERFACE,
636 ATK_SELECTION_INTERFACE,
637 ATK_TABLE_INTERFACE,
638 ATK_TEXT_INTERFACE,
639 ATK_VALUE_INTERFACE,
642 static int GetInterfaceMaskFromObject(BrowserAccessibilityAuraLinux* obj) {
643 int interface_mask = 0;
645 // Component interface is always supported.
646 interface_mask |= 1 << ATK_COMPONENT_INTERFACE;
648 // Action interface is basic one. It just relays on executing default action
649 // for each object.
650 interface_mask |= 1 << ATK_ACTION_INTERFACE;
652 // Value Interface
653 int role = obj->GetRole();
654 if (role == ui::AX_ROLE_PROGRESS_INDICATOR ||
655 role == ui::AX_ROLE_SCROLL_BAR || role == ui::AX_ROLE_SLIDER) {
656 interface_mask |= 1 << ATK_VALUE_INTERFACE;
659 // Document Interface
660 if (role == ui::AX_ROLE_DOCUMENT || role == ui::AX_ROLE_ROOT_WEB_AREA ||
661 role == ui::AX_ROLE_WEB_AREA)
662 interface_mask |= 1 << ATK_DOCUMENT_INTERFACE;
664 // Image Interface
665 if (role == ui::AX_ROLE_IMAGE || role == ui::AX_ROLE_IMAGE_MAP)
666 interface_mask |= 1 << ATK_IMAGE_INTERFACE;
668 return interface_mask;
671 static GType GetAccessibilityTypeFromObject(
672 BrowserAccessibilityAuraLinux* obj) {
673 static const GTypeInfo type_info = {
674 sizeof(BrowserAccessibilityAtkClass),
675 (GBaseInitFunc)0,
676 (GBaseFinalizeFunc)0,
677 (GClassInitFunc)0,
678 (GClassFinalizeFunc)0,
679 0, /* class data */
680 sizeof(BrowserAccessibilityAtk), /* instance size */
681 0, /* nb preallocs */
682 (GInstanceInitFunc)0,
683 0 /* value table */
686 int interface_mask = GetInterfaceMaskFromObject(obj);
687 const char* atk_type_name = GetUniqueAccessibilityTypeName(interface_mask);
688 GType type = g_type_from_name(atk_type_name);
689 if (type)
690 return type;
692 type = g_type_register_static(BROWSER_ACCESSIBILITY_TYPE, atk_type_name,
693 &type_info, GTypeFlags(0));
694 if (interface_mask & (1 << ATK_ACTION_INTERFACE))
695 g_type_add_interface_static(type, ATK_TYPE_ACTION, &ActionInfo);
696 if (interface_mask & (1 << ATK_COMPONENT_INTERFACE))
697 g_type_add_interface_static(type, ATK_TYPE_COMPONENT, &ComponentInfo);
698 if (interface_mask & (1 << ATK_DOCUMENT_INTERFACE))
699 g_type_add_interface_static(type, ATK_TYPE_DOCUMENT, &DocumentInfo);
700 if (interface_mask & (1 << ATK_IMAGE_INTERFACE))
701 g_type_add_interface_static(type, ATK_TYPE_IMAGE, &ImageInfo);
702 if (interface_mask & (1 << ATK_VALUE_INTERFACE))
703 g_type_add_interface_static(type, ATK_TYPE_VALUE, &ValueInfo);
705 return type;
708 BrowserAccessibilityAtk* browser_accessibility_new(
709 BrowserAccessibilityAuraLinux* obj) {
710 GType type = GetAccessibilityTypeFromObject(obj);
711 AtkObject* atk_object = static_cast<AtkObject*>(g_object_new(type, 0));
713 atk_object_initialize(atk_object, obj);
715 return BROWSER_ACCESSIBILITY(atk_object);
718 void browser_accessibility_detach(BrowserAccessibilityAtk* atk_object) {
719 atk_object->m_object = NULL;
722 // static
723 BrowserAccessibility* BrowserAccessibility::Create() {
724 return new BrowserAccessibilityAuraLinux();
727 BrowserAccessibilityAuraLinux*
728 BrowserAccessibility::ToBrowserAccessibilityAuraLinux() {
729 return static_cast<BrowserAccessibilityAuraLinux*>(this);
732 BrowserAccessibilityAuraLinux::BrowserAccessibilityAuraLinux()
733 : atk_object_(NULL) {
736 BrowserAccessibilityAuraLinux::~BrowserAccessibilityAuraLinux() {
737 browser_accessibility_detach(BROWSER_ACCESSIBILITY(atk_object_));
738 if (atk_object_)
739 g_object_unref(atk_object_);
742 AtkObject* BrowserAccessibilityAuraLinux::GetAtkObject() const {
743 if (!G_IS_OBJECT(atk_object_))
744 return NULL;
745 return atk_object_;
748 void BrowserAccessibilityAuraLinux::OnDataChanged() {
749 BrowserAccessibility::OnDataChanged();
750 InitRoleAndState();
752 if (atk_object_) {
753 // If the object's role changes and that causes its
754 // interface mask to change, we need to create a new
755 // AtkObject for it.
756 int interface_mask = GetInterfaceMaskFromObject(this);
757 if (interface_mask != interface_mask_) {
758 g_object_unref(atk_object_);
759 atk_object_ = NULL;
763 if (!atk_object_) {
764 interface_mask_ = GetInterfaceMaskFromObject(this);
765 atk_object_ = ATK_OBJECT(browser_accessibility_new(this));
766 if (this->GetParent()) {
767 atk_object_set_parent(
768 atk_object_,
769 this->GetParent()->ToBrowserAccessibilityAuraLinux()->GetAtkObject());
774 bool BrowserAccessibilityAuraLinux::IsNative() const {
775 return true;
778 void BrowserAccessibilityAuraLinux::InitRoleAndState() {
779 switch (GetRole()) {
780 case ui::AX_ROLE_DOCUMENT:
781 case ui::AX_ROLE_ROOT_WEB_AREA:
782 case ui::AX_ROLE_WEB_AREA:
783 atk_role_ = ATK_ROLE_DOCUMENT_WEB;
784 break;
785 case ui::AX_ROLE_ALERT:
786 case ui::AX_ROLE_ALERT_DIALOG:
787 atk_role_ = ATK_ROLE_ALERT;
788 break;
789 case ui::AX_ROLE_APPLICATION:
790 atk_role_ = ATK_ROLE_APPLICATION;
791 break;
792 case ui::AX_ROLE_BUTTON:
793 atk_role_ = ATK_ROLE_PUSH_BUTTON;
794 break;
795 case ui::AX_ROLE_CANVAS:
796 atk_role_ = ATK_ROLE_CANVAS;
797 break;
798 case ui::AX_ROLE_CAPTION:
799 atk_role_ = ATK_ROLE_CAPTION;
800 break;
801 case ui::AX_ROLE_CHECK_BOX:
802 atk_role_ = ATK_ROLE_CHECK_BOX;
803 break;
804 case ui::AX_ROLE_COLOR_WELL:
805 atk_role_ = ATK_ROLE_COLOR_CHOOSER;
806 break;
807 case ui::AX_ROLE_COLUMN_HEADER:
808 atk_role_ = ATK_ROLE_COLUMN_HEADER;
809 break;
810 case ui::AX_ROLE_COMBO_BOX:
811 atk_role_ = ATK_ROLE_COMBO_BOX;
812 break;
813 case ui::AX_ROLE_DATE:
814 case ui::AX_ROLE_DATE_TIME:
815 atk_role_ = ATK_ROLE_DATE_EDITOR;
816 break;
817 case ui::AX_ROLE_DIALOG:
818 atk_role_ = ATK_ROLE_DIALOG;
819 break;
820 case ui::AX_ROLE_DIV:
821 case ui::AX_ROLE_GROUP:
822 atk_role_ = ATK_ROLE_SECTION;
823 break;
824 case ui::AX_ROLE_FORM:
825 atk_role_ = ATK_ROLE_FORM;
826 break;
827 case ui::AX_ROLE_IMAGE:
828 atk_role_ = ATK_ROLE_IMAGE;
829 break;
830 case ui::AX_ROLE_IMAGE_MAP:
831 atk_role_ = ATK_ROLE_IMAGE_MAP;
832 break;
833 case ui::AX_ROLE_LABEL_TEXT:
834 atk_role_ = ATK_ROLE_LABEL;
835 break;
836 case ui::AX_ROLE_LINK:
837 atk_role_ = ATK_ROLE_LINK;
838 break;
839 case ui::AX_ROLE_LIST:
840 atk_role_ = ATK_ROLE_LIST;
841 break;
842 case ui::AX_ROLE_LIST_BOX:
843 atk_role_ = ATK_ROLE_LIST_BOX;
844 break;
845 case ui::AX_ROLE_LIST_ITEM:
846 atk_role_ = ATK_ROLE_LIST_ITEM;
847 break;
848 case ui::AX_ROLE_MENU:
849 atk_role_ = ATK_ROLE_MENU;
850 break;
851 case ui::AX_ROLE_MENU_BAR:
852 atk_role_ = ATK_ROLE_MENU_BAR;
853 break;
854 case ui::AX_ROLE_MENU_ITEM:
855 atk_role_ = ATK_ROLE_MENU_ITEM;
856 break;
857 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
858 atk_role_ = ATK_ROLE_CHECK_MENU_ITEM;
859 break;
860 case ui::AX_ROLE_MENU_ITEM_RADIO:
861 atk_role_ = ATK_ROLE_RADIO_MENU_ITEM;
862 break;
863 case ui::AX_ROLE_METER:
864 atk_role_ = ATK_ROLE_PROGRESS_BAR;
865 break;
866 case ui::AX_ROLE_PARAGRAPH:
867 atk_role_ = ATK_ROLE_PARAGRAPH;
868 break;
869 case ui::AX_ROLE_RADIO_BUTTON:
870 atk_role_ = ATK_ROLE_RADIO_BUTTON;
871 break;
872 case ui::AX_ROLE_ROW_HEADER:
873 atk_role_ = ATK_ROLE_ROW_HEADER;
874 break;
875 case ui::AX_ROLE_SCROLL_BAR:
876 atk_role_ = ATK_ROLE_SCROLL_BAR;
877 break;
878 case ui::AX_ROLE_SLIDER:
879 atk_role_ = ATK_ROLE_SLIDER;
880 break;
881 case ui::AX_ROLE_SPIN_BUTTON:
882 atk_role_ = ATK_ROLE_SPIN_BUTTON;
883 break;
884 case ui::AX_ROLE_SPLITTER:
885 atk_role_ = ATK_ROLE_SEPARATOR;
886 break;
887 case ui::AX_ROLE_STATIC_TEXT:
888 atk_role_ = ATK_ROLE_TEXT;
889 break;
890 case ui::AX_ROLE_STATUS:
891 atk_role_ = ATK_ROLE_STATUSBAR;
892 break;
893 case ui::AX_ROLE_TAB:
894 atk_role_ = ATK_ROLE_PAGE_TAB;
895 break;
896 case ui::AX_ROLE_TABLE:
897 atk_role_ = ATK_ROLE_TABLE;
898 break;
899 case ui::AX_ROLE_TAB_LIST:
900 atk_role_ = ATK_ROLE_PAGE_TAB_LIST;
901 break;
902 case ui::AX_ROLE_TOGGLE_BUTTON:
903 atk_role_ = ATK_ROLE_TOGGLE_BUTTON;
904 break;
905 case ui::AX_ROLE_TOOLBAR:
906 atk_role_ = ATK_ROLE_TOOL_BAR;
907 break;
908 case ui::AX_ROLE_TOOLTIP:
909 atk_role_ = ATK_ROLE_TOOL_TIP;
910 break;
911 case ui::AX_ROLE_TEXT_FIELD:
912 atk_role_ = ATK_ROLE_ENTRY;
913 break;
914 case ui::AX_ROLE_TREE:
915 atk_role_ = ATK_ROLE_TREE;
916 break;
917 case ui::AX_ROLE_TREE_ITEM:
918 atk_role_ = ATK_ROLE_TREE_ITEM;
919 break;
920 default:
921 atk_role_ = ATK_ROLE_UNKNOWN;
922 break;
926 } // namespace content