blob: af0c49e5a8623389955dc22f338da37009a70999 [file] [log] [blame]
// Copyright 2017 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 "ui/accessibility/platform/ax_platform_node_auralinux.h"
#include <dlfcn.h>
#include <stdint.h>
#include <algorithm>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/leak_annotations.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/numerics/ranges.h"
#include "base/optional.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_enum_util.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_mode_observer.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/ax_tree_data.h"
#include "ui/accessibility/platform/atk_util_auralinux.h"
#include "ui/accessibility/platform/ax_platform_atk_hyperlink.h"
#include "ui/accessibility/platform/ax_platform_node_delegate.h"
#include "ui/accessibility/platform/ax_platform_node_delegate_base.h"
#include "ui/accessibility/platform/ax_platform_text_boundary.h"
#include "ui/gfx/geometry/rect_conversions.h"
#if defined(ATK_CHECK_VERSION) && ATK_CHECK_VERSION(2, 10, 0)
#define ATK_210
#endif
#if defined(ATK_CHECK_VERSION) && ATK_CHECK_VERSION(2, 12, 0)
#define ATK_212
#endif
#if defined(ATK_CHECK_VERSION) && ATK_CHECK_VERSION(2, 16, 0)
#define ATK_216
#endif
#if defined(ATK_CHECK_VERSION) && ATK_CHECK_VERSION(2, 26, 0)
#define ATK_226
#endif
#if defined(ATK_CHECK_VERSION) && ATK_CHECK_VERSION(2, 30, 0)
#define ATK_230
#endif
#if defined(ATK_CHECK_VERSION) && ATK_CHECK_VERSION(2, 32, 0)
#define ATK_232
#endif
#if defined(ATK_CHECK_VERSION) && ATK_CHECK_VERSION(2, 34, 0)
#define ATK_234
#endif
namespace ui {
namespace {
// IMPORTANT!
// These values are written to logs. Do not renumber or delete
// existing items; add new entries to the end of the list.
enum class UmaAtkApi {
kGetName = 0,
kGetDescription = 1,
kGetNChildren = 2,
kRefChild = 3,
kGetIndexInParent = 4,
kGetParent = 5,
kRefRelationSet = 6,
kGetAttributes = 7,
kGetRole = 8,
kRefStateSet = 9,
// This must always be the last enum. It's okay for its value to
// increase, but none of the other enum values may change.
kMaxValue = kRefStateSet,
};
void RecordAccessibilityAtkApi(UmaAtkApi enum_value) {
UMA_HISTOGRAM_ENUMERATION("Accessibility.ATK-APIs", enum_value);
}
// When accepting input from clients calling the API, an ATK character offset
// of -1 can often represent the length of the string.
static const int kStringLengthOffset = -1;
// We must forward declare this because it is used by the traditional GObject
// type manipulation macros.
namespace atk_object {
GType GetType();
} // namespace atk_object
//
// ax_platform_node_auralinux AtkObject definition and implementation.
//
#define AX_PLATFORM_NODE_AURALINUX_TYPE (atk_object::GetType())
#define AX_PLATFORM_NODE_AURALINUX(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), AX_PLATFORM_NODE_AURALINUX_TYPE, \
AXPlatformNodeAuraLinuxObject))
#define AX_PLATFORM_NODE_AURALINUX_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), AX_PLATFORM_NODE_AURALINUX_TYPE, \
AXPlatformNodeAuraLinuxClass))
#define IS_AX_PLATFORM_NODE_AURALINUX(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), AX_PLATFORM_NODE_AURALINUX_TYPE))
#define IS_AX_PLATFORM_NODE_AURALINUX_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass), AX_PLATFORM_NODE_AURALINUX_TYPE))
#define AX_PLATFORM_NODE_AURALINUX_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), AX_PLATFORM_NODE_AURALINUX_TYPE, \
AXPlatformNodeAuraLinuxClass))
typedef struct _AXPlatformNodeAuraLinuxObject AXPlatformNodeAuraLinuxObject;
typedef struct _AXPlatformNodeAuraLinuxClass AXPlatformNodeAuraLinuxClass;
struct _AXPlatformNodeAuraLinuxObject {
AtkObject parent;
AXPlatformNodeAuraLinux* m_object;
};
struct _AXPlatformNodeAuraLinuxClass {
AtkObjectClass parent_class;
};
// The root-level Application object that's the parent of all top-level windows.
AXPlatformNode* g_root_application = nullptr;
// The last AtkObject with keyboard focus. Tracking this is required to emit the
// ATK_STATE_FOCUSED change to false.
AtkObject* g_current_focused = nullptr;
// The last AtkObject which was the active descendant in the currently-focused
// object (example: The highlighted option within a focused select element).
// As with g_current_focused, we track this to emit events when this object is
// no longer the active descendant.
AtkObject* g_current_active_descendant = nullptr;
// The last object which was selected. Tracking this is required because
// widgets in the browser UI only emit notifications upon becoming selected,
// but clients also expect notifications when items become unselected.
AXPlatformNodeAuraLinux* g_current_selected = nullptr;
// The AtkObject with role=ATK_ROLE_FRAME that represents the toplevel desktop
// window with focus. If this window is not one of our windows, this value
// should be null. This is a weak pointer as well, so its value will also be
// null if if the AtkObject is destroyed.
AtkObject* g_active_top_level_frame = nullptr;
AtkObject* g_active_views_dialog = nullptr;
#if defined(ATK_216)
constexpr AtkRole kStaticRole = ATK_ROLE_STATIC;
constexpr AtkRole kSubscriptRole = ATK_ROLE_SUBSCRIPT;
constexpr AtkRole kSuperscriptRole = ATK_ROLE_SUPERSCRIPT;
#else
constexpr AtkRole kStaticRole = ATK_ROLE_TEXT;
constexpr AtkRole kSubscriptRole = ATK_ROLE_TEXT;
constexpr AtkRole kSuperscriptRole = ATK_ROLE_TEXT;
#endif
#if defined(ATK_226)
constexpr AtkRole kAtkFootnoteRole = ATK_ROLE_FOOTNOTE;
#else
constexpr AtkRole kAtkFootnoteRole = ATK_ROLE_LIST_ITEM;
#endif
#if defined(ATK_234)
constexpr AtkRole kAtkRoleContentDeletion = ATK_ROLE_CONTENT_DELETION;
constexpr AtkRole kAtkRoleContentInsertion = ATK_ROLE_CONTENT_INSERTION;
#else
constexpr AtkRole kAtkRoleContentDeletion = ATK_ROLE_SECTION;
constexpr AtkRole kAtkRoleContentInsertion = ATK_ROLE_SECTION;
#endif
using GetTypeFunc = GType (*)();
using GetColumnHeaderCellsFunc = GPtrArray* (*)(AtkTableCell* cell);
using GetRowHeaderCellsFunc = GPtrArray* (*)(AtkTableCell* cell);
using GetRowColumnSpanFunc = bool (*)(AtkTableCell* cell,
gint* row,
gint* column,
gint* row_span,
gint* col_span);
static GetTypeFunc g_atk_table_cell_get_type;
static GetColumnHeaderCellsFunc g_atk_table_cell_get_column_header_cells;
static GetRowHeaderCellsFunc g_atk_table_cell_get_row_header_cells;
static GetRowColumnSpanFunc g_atk_table_cell_get_row_column_span;
// The ATK API often requires pointers to be used as out arguments, while
// allowing for those pointers to be null if the caller is not interested in
// the value. This function is a simpler helper to avoid continually checking
// for null and to help prevent forgetting to check for null.
void SetIntPointerValueIfNotNull(int* pointer, int value) {
if (pointer)
*pointer = value;
}
#if defined(ATK_230)
bool SupportsAtkComponentScrollingInterface() {
return dlsym(RTLD_DEFAULT, "atk_component_scroll_to_point");
}
#endif
#if defined(ATK_232)
bool SupportsAtkTextScrollingInterface() {
return dlsym(RTLD_DEFAULT, "atk_text_scroll_substring_to_point");
}
#endif
AtkObject* FindAtkObjectParentFrame(AtkObject* atk_object) {
AXPlatformNodeAuraLinux* node =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
while (node) {
if (node->GetAtkRole() == ATK_ROLE_FRAME)
return node->GetNativeViewAccessible();
node = AXPlatformNodeAuraLinux::FromAtkObject(node->GetParent());
}
return nullptr;
}
AtkObject* FindAtkObjectToplevelParentDocument(AtkObject* atk_object) {
AXPlatformNodeAuraLinux* node =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
AtkObject* toplevel_document = nullptr;
while (node) {
if (node->GetAtkRole() == ATK_ROLE_DOCUMENT_WEB)
toplevel_document = node->GetNativeViewAccessible();
node = AXPlatformNodeAuraLinux::FromAtkObject(node->GetParent());
}
return toplevel_document;
}
bool IsFrameAncestorOfAtkObject(AtkObject* frame, AtkObject* atk_object) {
AtkObject* current_frame = FindAtkObjectParentFrame(atk_object);
while (current_frame) {
if (current_frame == frame)
return true;
AXPlatformNodeAuraLinux* frame_node =
AXPlatformNodeAuraLinux::FromAtkObject(current_frame);
current_frame = FindAtkObjectParentFrame(frame_node->GetParent());
}
return false;
}
// Returns a stack of AtkObjects of activated popup menus. Since each popup
// menu and submenu has its own native window, we want to properly manage the
// activated state for their containing frames.
std::vector<AtkObject*>& GetActiveMenus() {
static base::NoDestructor<std::vector<AtkObject*>> active_menus;
return *active_menus;
}
std::map<AtkObject*, FindInPageResultInfo>& GetActiveFindInPageResults() {
static base::NoDestructor<std::map<AtkObject*, FindInPageResultInfo>>
active_results;
return *active_results;
}
// The currently active frame is g_active_top_level_frame, unless there is an
// active menu. If there is an active menu the parent frame of the
// most-recently opened active menu should be the currently active frame.
AtkObject* ComputeActiveTopLevelFrame() {
if (!GetActiveMenus().empty())
return FindAtkObjectParentFrame(GetActiveMenus().back());
return g_active_top_level_frame;
}
const char* GetUniqueAccessibilityGTypeName(
ImplementedAtkInterfaces interface_mask) {
// 37 characters is enough for "AXPlatformNodeAuraLinux%x" with any integer
// value.
static char name[37];
snprintf(name, sizeof(name), "AXPlatformNodeAuraLinux%x",
interface_mask.value());
return name;
}
void SetWeakGPtrToAtkObject(AtkObject** weak_pointer, AtkObject* new_value) {
DCHECK(weak_pointer);
if (*weak_pointer == new_value)
return;
if (*weak_pointer) {
g_object_remove_weak_pointer(G_OBJECT(*weak_pointer),
reinterpret_cast<void**>(weak_pointer));
}
*weak_pointer = new_value;
if (new_value) {
g_object_add_weak_pointer(G_OBJECT(new_value),
reinterpret_cast<void**>(weak_pointer));
}
}
void SetActiveTopLevelFrame(AtkObject* new_top_level_frame) {
SetWeakGPtrToAtkObject(&g_active_top_level_frame, new_top_level_frame);
}
AXCoordinateSystem AtkCoordTypeToAXCoordinateSystem(
AtkCoordType coordinate_type) {
switch (coordinate_type) {
case ATK_XY_SCREEN:
return AXCoordinateSystem::kScreenDIPs;
case ATK_XY_WINDOW:
return AXCoordinateSystem::kRootFrame;
#if defined(ATK_230)
case ATK_XY_PARENT:
// AXCoordinateSystem does not support parent coordinates.
NOTIMPLEMENTED();
return AXCoordinateSystem::kFrame;
#endif
default:
return AXCoordinateSystem::kScreenDIPs;
}
}
const char* BuildDescriptionFromHeaders(AXPlatformNodeDelegate* delegate,
const std::vector<int32_t>& ids) {
std::vector<std::string> names;
for (const auto& node_id : ids) {
if (AXPlatformNode* header = delegate->GetFromNodeID(node_id)) {
if (AtkObject* atk_header = header->GetNativeViewAccessible())
names.push_back(atk_object_get_name(atk_header));
}
}
std::string result = base::JoinString(names, " ");
#if defined(LEAK_SANITIZER) && !defined(OS_NACL)
// http://crbug.com/982839
// atk_table_get_column_description and atk_table_get_row_description return
// const gchar*, which suggests the caller does not gain ownership of the
// returned string. The g_strdup below causes a new allocation, which does not
// fit that pattern and causes a leak in tests.
ScopedLeakSanitizerDisabler lsan_disabler;
#endif
return g_strdup(result.c_str());
}
gfx::Point FindAtkObjectParentCoords(AtkObject* atk_object) {
if (!atk_object)
return gfx::Point(0, 0);
AXPlatformNodeAuraLinux* node =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (node->GetAtkRole() == ATK_ROLE_FRAME) {
int x, y;
atk_component_get_extents(ATK_COMPONENT(atk_object), &x, &y, nullptr,
nullptr, ATK_XY_WINDOW);
gfx::Point window_coords(x, y);
return window_coords;
}
atk_object = node->GetParent();
return FindAtkObjectParentCoords(atk_object);
}
AtkAttributeSet* PrependAtkAttributeToAtkAttributeSet(
const char* name,
const char* value,
AtkAttributeSet* attribute_set) {
AtkAttribute* attribute =
static_cast<AtkAttribute*>(g_malloc(sizeof(AtkAttribute)));
attribute->name = g_strdup(name);
attribute->value = g_strdup(value);
return g_slist_prepend(attribute_set, attribute);
}
AtkObject* GetActiveDescendantOfCurrentFocused() {
if (!g_current_focused)
return nullptr;
auto* node = AXPlatformNodeAuraLinux::FromAtkObject(g_current_focused);
if (!node)
return nullptr;
int32_t id =
node->GetIntAttribute(ax::mojom::IntAttribute::kActivedescendantId);
if (auto* descendant = node->GetDelegate()->GetFromNodeID(id))
return descendant->GetNativeViewAccessible();
return nullptr;
}
void PrependTextAttributeToSet(const std::string& attribute,
const std::string& value,
AtkAttributeSet** attributes) {
DCHECK(attributes);
AtkAttribute* new_attribute =
static_cast<AtkAttribute*>(g_malloc(sizeof(AtkAttribute)));
new_attribute->name = g_strdup(attribute.c_str());
new_attribute->value = g_strdup(value.c_str());
*attributes = g_slist_prepend(*attributes, new_attribute);
}
void PrependAtkTextAttributeToSet(const AtkTextAttribute attribute,
const std::string& value,
AtkAttributeSet** attributes) {
PrependTextAttributeToSet(atk_text_attribute_get_name(attribute), value,
attributes);
}
std::string ToAtkTextAttributeColor(const std::string color) {
// The platform-independent color string is in the form "rgb(r, g, b)",
// but ATK expects a string like "r, g, b". We convert the string here
// by stripping away the unnecessary characters.
DCHECK(base::StartsWith(color, "rgb(", base::CompareCase::INSENSITIVE_ASCII));
DCHECK(base::EndsWith(color, ")", base::CompareCase::INSENSITIVE_ASCII));
return color.substr(4, color.length() - 5);
}
AtkAttributeSet* ToAtkAttributeSet(const TextAttributeList& attributes) {
AtkAttributeSet* copied_attributes = nullptr;
for (const auto& attribute : attributes) {
if (attribute.first == "background-color") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_BG_COLOR,
ToAtkTextAttributeColor(attribute.second),
&copied_attributes);
} else if (attribute.first == "color") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_FG_COLOR,
ToAtkTextAttributeColor(attribute.second),
&copied_attributes);
} else if (attribute.first == "font-family") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_FAMILY_NAME, attribute.second,
&copied_attributes);
} else if (attribute.first == "font-size") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_SIZE, attribute.second,
&copied_attributes);
} else if (attribute.first == "font-weight" && attribute.second == "bold") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_WEIGHT, "700",
&copied_attributes);
} else if (attribute.first == "font-style") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_STYLE, "italic",
&copied_attributes);
} else if (attribute.first == "text-line-through-style") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_STRIKETHROUGH, "true",
&copied_attributes);
} else if (attribute.first == "text-underline-style") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_UNDERLINE, "single",
&copied_attributes);
} else if (attribute.first == "invalid") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_INVALID, attribute.second,
&copied_attributes);
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_UNDERLINE, "error",
&copied_attributes);
} else if (attribute.first == "language") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_LANGUAGE, attribute.second,
&copied_attributes);
} else if (attribute.first == "writing-mode") {
PrependAtkTextAttributeToSet(ATK_TEXT_ATTR_DIRECTION, attribute.second,
&copied_attributes);
} else if (attribute.first == "text-position") {
PrependTextAttributeToSet(attribute.first, attribute.second,
&copied_attributes);
}
}
return g_slist_reverse(copied_attributes);
}
namespace atk_component {
void GetExtents(AtkComponent* atk_component,
gint* x,
gint* y,
gint* width,
gint* height,
AtkCoordType coord_type) {
g_return_if_fail(ATK_IS_COMPONENT(atk_component));
if (x)
*x = 0;
if (y)
*y = 0;
if (width)
*width = 0;
if (height)
*height = 0;
AtkObject* atk_object = ATK_OBJECT(atk_component);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetExtents(x, y, width, height, coord_type);
}
void GetPosition(AtkComponent* atk_component,
gint* x,
gint* y,
AtkCoordType coord_type) {
g_return_if_fail(ATK_IS_COMPONENT(atk_component));
if (x)
*x = 0;
if (y)
*y = 0;
AtkObject* atk_object = ATK_OBJECT(atk_component);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetPosition(x, y, coord_type);
}
void GetSize(AtkComponent* atk_component, gint* width, gint* height) {
g_return_if_fail(ATK_IS_COMPONENT(atk_component));
if (width)
*width = 0;
if (height)
*height = 0;
AtkObject* atk_object = ATK_OBJECT(atk_component);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetSize(width, height);
}
AtkObject* RefAccesibleAtPoint(AtkComponent* atk_component,
gint x,
gint y,
AtkCoordType coord_type) {
g_return_val_if_fail(ATK_IS_COMPONENT(atk_component), nullptr);
AtkObject* atk_object = ATK_OBJECT(atk_component);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
AtkObject* result = obj->HitTestSync(x, y, coord_type);
if (result)
g_object_ref(result);
return result;
}
gboolean GrabFocus(AtkComponent* atk_component) {
g_return_val_if_fail(ATK_IS_COMPONENT(atk_component), FALSE);
AtkObject* atk_object = ATK_OBJECT(atk_component);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return FALSE;
return obj->GrabFocus();
}
#if defined(ATK_230)
gboolean ScrollTo(AtkComponent* atk_component, AtkScrollType scroll_type) {
g_return_val_if_fail(ATK_IS_COMPONENT(atk_component), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_component));
if (!obj)
return FALSE;
obj->ScrollNodeIntoView(scroll_type);
return TRUE;
}
gboolean ScrollToPoint(AtkComponent* atk_component,
AtkCoordType atk_coord_type,
gint x,
gint y) {
g_return_val_if_fail(ATK_IS_COMPONENT(atk_component), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_component));
if (!obj)
return FALSE;
obj->ScrollToPoint(atk_coord_type, x, y);
return TRUE;
}
#endif
void Init(AtkComponentIface* iface) {
iface->get_extents = GetExtents;
iface->get_position = GetPosition;
iface->get_size = GetSize;
iface->ref_accessible_at_point = RefAccesibleAtPoint;
iface->grab_focus = GrabFocus;
#if defined(ATK_230)
if (SupportsAtkComponentScrollingInterface()) {
iface->scroll_to = ScrollTo;
iface->scroll_to_point = ScrollToPoint;
}
#endif
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_component
namespace atk_action {
gboolean DoAction(AtkAction* atk_action, gint index) {
g_return_val_if_fail(ATK_IS_ACTION(atk_action), FALSE);
g_return_val_if_fail(!index, FALSE);
AtkObject* atk_object = ATK_OBJECT(atk_action);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return FALSE;
return obj->DoDefaultAction();
}
gint GetNActions(AtkAction* atk_action) {
g_return_val_if_fail(ATK_IS_ACTION(atk_action), 0);
AtkObject* atk_object = ATK_OBJECT(atk_action);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return 0;
return 1;
}
const gchar* GetDescription(AtkAction*, gint) {
// Not implemented. Right now Orca does not provide this and
// Chromium is not providing a string for the action description.
return nullptr;
}
const gchar* GetName(AtkAction* atk_action, gint index) {
g_return_val_if_fail(ATK_IS_ACTION(atk_action), nullptr);
g_return_val_if_fail(!index, nullptr);
AtkObject* atk_object = ATK_OBJECT(atk_action);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return obj->GetDefaultActionName();
}
const gchar* GetKeybinding(AtkAction* atk_action, gint index) {
g_return_val_if_fail(ATK_IS_ACTION(atk_action), nullptr);
g_return_val_if_fail(!index, nullptr);
AtkObject* atk_object = ATK_OBJECT(atk_action);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return obj->GetStringAttribute(ax::mojom::StringAttribute::kAccessKey)
.c_str();
}
void Init(AtkActionIface* iface) {
iface->do_action = DoAction;
iface->get_n_actions = GetNActions;
iface->get_description = GetDescription;
iface->get_name = GetName;
iface->get_keybinding = GetKeybinding;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_action
namespace atk_document {
const gchar* GetDocumentAttributeValue(AtkDocument* atk_doc,
const gchar* attribute) {
g_return_val_if_fail(ATK_IS_DOCUMENT(atk_doc), nullptr);
AtkObject* atk_object = ATK_OBJECT(atk_doc);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return obj->GetDocumentAttributeValue(attribute);
}
AtkAttributeSet* GetDocumentAttributes(AtkDocument* atk_doc) {
g_return_val_if_fail(ATK_IS_DOCUMENT(atk_doc), 0);
AtkObject* atk_object = ATK_OBJECT(atk_doc);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return obj->GetDocumentAttributes();
}
void Init(AtkDocumentIface* iface) {
iface->get_document_attribute_value = GetDocumentAttributeValue;
iface->get_document_attributes = GetDocumentAttributes;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_document
namespace atk_image {
void GetImagePosition(AtkImage* atk_img,
gint* x,
gint* y,
AtkCoordType coord_type) {
g_return_if_fail(ATK_IMAGE(atk_img));
AtkObject* atk_object = ATK_OBJECT(atk_img);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetPosition(x, y, coord_type);
}
const gchar* GetImageDescription(AtkImage* atk_img) {
g_return_val_if_fail(ATK_IMAGE(atk_img), nullptr);
AtkObject* atk_object = ATK_OBJECT(atk_img);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return obj->GetStringAttribute(ax::mojom::StringAttribute::kDescription)
.c_str();
}
void GetImageSize(AtkImage* atk_img, gint* width, gint* height) {
g_return_if_fail(ATK_IMAGE(atk_img));
AtkObject* atk_object = ATK_OBJECT(atk_img);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetSize(width, height);
}
void Init(AtkImageIface* iface) {
iface->get_image_position = GetImagePosition;
iface->get_image_description = GetImageDescription;
iface->get_image_size = GetImageSize;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_image
namespace atk_value {
void GetCurrentValue(AtkValue* atk_value, GValue* value) {
g_return_if_fail(ATK_IS_VALUE(atk_value));
AtkObject* atk_object = ATK_OBJECT(atk_value);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetFloatAttributeInGValue(ax::mojom::FloatAttribute::kValueForRange,
value);
}
void GetMinimumValue(AtkValue* atk_value, GValue* value) {
g_return_if_fail(ATK_IS_VALUE(atk_value));
AtkObject* atk_object = ATK_OBJECT(atk_value);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetFloatAttributeInGValue(ax::mojom::FloatAttribute::kMinValueForRange,
value);
}
void GetMaximumValue(AtkValue* atk_value, GValue* value) {
g_return_if_fail(ATK_IS_VALUE(atk_value));
AtkObject* atk_object = ATK_OBJECT(atk_value);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetFloatAttributeInGValue(ax::mojom::FloatAttribute::kMaxValueForRange,
value);
}
void GetMinimumIncrement(AtkValue* atk_value, GValue* value) {
g_return_if_fail(ATK_IS_VALUE(atk_value));
AtkObject* atk_object = ATK_OBJECT(atk_value);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return;
obj->GetFloatAttributeInGValue(ax::mojom::FloatAttribute::kStepValueForRange,
value);
}
gboolean SetCurrentValue(AtkValue* atk_value, const GValue* value) {
g_return_val_if_fail(ATK_IS_VALUE(atk_value), FALSE);
AtkObject* atk_object = ATK_OBJECT(atk_value);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return FALSE;
std::string new_value;
switch (G_VALUE_TYPE(value)) {
case G_TYPE_FLOAT:
new_value = base::NumberToString(g_value_get_float(value));
break;
case G_TYPE_INT:
new_value = base::NumberToString(g_value_get_int(value));
break;
case G_TYPE_INT64:
new_value = base::NumberToString(g_value_get_int64(value));
break;
case G_TYPE_STRING:
new_value = g_value_get_string(value);
break;
default:
return FALSE;
}
AXActionData data;
data.action = ax::mojom::Action::kSetValue;
data.value = new_value;
obj->GetDelegate()->AccessibilityPerformAction(data);
return TRUE;
}
void Init(AtkValueIface* iface) {
iface->get_current_value = GetCurrentValue;
iface->get_maximum_value = GetMaximumValue;
iface->get_minimum_value = GetMinimumValue;
iface->get_minimum_increment = GetMinimumIncrement;
iface->set_current_value = SetCurrentValue;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_value
namespace atk_hyperlink {
AtkHyperlink* GetHyperlink(AtkHyperlinkImpl* atk_hyperlink_impl) {
g_return_val_if_fail(ATK_HYPERLINK_IMPL(atk_hyperlink_impl), 0);
AtkObject* atk_object = ATK_OBJECT(atk_hyperlink_impl);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return 0;
AtkHyperlink* atk_hyperlink = obj->GetAtkHyperlink();
g_object_ref(atk_hyperlink);
return atk_hyperlink;
}
void Init(AtkHyperlinkImplIface* iface) {
iface->get_hyperlink = GetHyperlink;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_hyperlink
namespace atk_hypertext {
AtkHyperlink* GetLink(AtkHypertext* hypertext, int index) {
g_return_val_if_fail(ATK_HYPERTEXT(hypertext), 0);
auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(hypertext));
if (!obj)
return nullptr;
const AXHypertext& ax_hypertext = obj->GetAXHypertext();
if (index > static_cast<int>(ax_hypertext.hyperlinks.size()) || index < 0)
return nullptr;
int32_t id = ax_hypertext.hyperlinks[index];
auto* link = static_cast<AXPlatformNodeAuraLinux*>(
AXPlatformNodeBase::GetFromUniqueId(id));
if (!link)
return nullptr;
return link->GetAtkHyperlink();
}
int GetNLinks(AtkHypertext* hypertext) {
g_return_val_if_fail(ATK_HYPERTEXT(hypertext), 0);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(hypertext));
return obj ? obj->GetAXHypertext().hyperlinks.size() : 0;
}
int GetLinkIndex(AtkHypertext* hypertext, int char_index) {
g_return_val_if_fail(ATK_HYPERTEXT(hypertext), 0);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(hypertext));
if (!obj)
return -1;
auto it = obj->GetAXHypertext().hyperlink_offset_to_index.find(char_index);
if (it == obj->GetAXHypertext().hyperlink_offset_to_index.end())
return -1;
return it->second;
}
void Init(AtkHypertextIface* iface) {
iface->get_link = GetLink;
iface->get_n_links = GetNLinks;
iface->get_link_index = GetLinkIndex;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_hypertext
namespace atk_text {
gchar* GetText(AtkText* atk_text, gint start_offset, gint end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
AtkObject* atk_object = ATK_OBJECT(atk_text);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
base::string16 text = obj->GetHypertext();
start_offset = obj->UnicodeToUTF16OffsetInText(start_offset);
if (start_offset < 0 || start_offset >= static_cast<int>(text.size()))
return nullptr;
if (end_offset < 0) {
end_offset = text.size();
} else {
end_offset = obj->UnicodeToUTF16OffsetInText(end_offset);
end_offset = base::ClampToRange(int{text.size()}, start_offset, end_offset);
}
DCHECK_GE(start_offset, 0);
DCHECK_GE(end_offset, start_offset);
return g_strdup(
base::UTF16ToUTF8(text.substr(start_offset, end_offset - start_offset))
.c_str());
}
gint GetCharacterCount(AtkText* atk_text) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), 0);
AtkObject* atk_object = ATK_OBJECT(atk_text);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return 0;
return obj->UTF16ToUnicodeOffsetInText(obj->GetHypertext().length());
}
gunichar GetCharacterAtOffset(AtkText* atk_text, int offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), 0);
AtkObject* atk_object = ATK_OBJECT(atk_text);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return 0;
base::string16 text = obj->GetHypertext();
int32_t text_length = text.length();
offset = obj->UnicodeToUTF16OffsetInText(offset);
int32_t limited_offset = base::ClampToRange(offset, 0, text_length);
uint32_t code_point;
base::ReadUnicodeCharacter(text.c_str(), text_length + 1, &limited_offset,
&code_point);
return code_point;
}
gint GetOffsetAtPoint(AtkText* text, gint x, gint y, AtkCoordType coords) {
g_return_val_if_fail(ATK_IS_TEXT(text), -1);
AtkObject* atk_object = ATK_OBJECT(text);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return -1;
return obj->GetTextOffsetAtPoint(x, y, coords);
}
// This function returns a single character as a UTF-8 encoded C string because
// the character may be encoded into more than one byte.
char* GetCharacter(AtkText* atk_text,
int offset,
int* start_offset,
int* end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
*start_offset = -1;
*end_offset = -1;
AtkObject* atk_object = ATK_OBJECT(atk_text);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
if (offset < 0 || offset >= GetCharacterCount(atk_text))
return nullptr;
char* text = GetText(atk_text, offset, offset + 1);
if (!text)
return nullptr;
*start_offset = offset;
*end_offset = offset + 1;
return text;
}
char* GetTextWithBoundaryType(AtkText* atk_text,
int offset,
ax::mojom::TextBoundary boundary,
int* start_offset_ptr,
int* end_offset_ptr) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
AtkObject* atk_object = ATK_OBJECT(atk_text);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
if (offset < 0 || offset >= atk_text_get_character_count(atk_text))
return nullptr;
// The offset that we receive from the API is a Unicode character offset.
// Since we calculate boundaries in terms of UTF-16 code point offsets, we
// need to convert this input value.
offset = obj->UnicodeToUTF16OffsetInText(offset);
int start_offset = obj->FindTextBoundary(
boundary, offset, ax::mojom::MoveDirection::kBackward,
ax::mojom::TextAffinity::kDownstream);
int end_offset = obj->FindTextBoundary(boundary, offset,
ax::mojom::MoveDirection::kForward,
ax::mojom::TextAffinity::kDownstream);
if (start_offset < 0 || end_offset < 0)
return nullptr;
DCHECK_LE(start_offset, end_offset)
<< "Start offset should be less than or equal the end offset.";
// The ATK API is also expecting Unicode character offsets as output
// values.
*start_offset_ptr = obj->UTF16ToUnicodeOffsetInText(start_offset);
*end_offset_ptr = obj->UTF16ToUnicodeOffsetInText(end_offset);
base::string16 text = obj->GetHypertext();
DCHECK_LE(end_offset, static_cast<int>(text.size()));
base::string16 substr = text.substr(start_offset, end_offset - start_offset);
return g_strdup(base::UTF16ToUTF8(substr).c_str());
}
char* GetTextAtOffset(AtkText* atk_text,
int offset,
AtkTextBoundary atk_boundary,
int* start_offset,
int* end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
ax::mojom::TextBoundary boundary = FromAtkTextBoundary(atk_boundary);
return GetTextWithBoundaryType(atk_text, offset, boundary, start_offset,
end_offset);
}
char* GetTextAfterOffset(AtkText* atk_text,
int offset,
AtkTextBoundary boundary,
int* start_offset,
int* end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
if (boundary != ATK_TEXT_BOUNDARY_CHAR) {
*start_offset = -1;
*end_offset = -1;
return nullptr;
}
// ATK does not offer support for the special negative index and we don't
// want to do arithmetic on that value below.
if (offset == kStringLengthOffset)
return nullptr;
return GetCharacter(atk_text, offset + 1, start_offset, end_offset);
}
char* GetTextBeforeOffset(AtkText* atk_text,
int offset,
AtkTextBoundary boundary,
int* start_offset,
int* end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
if (boundary != ATK_TEXT_BOUNDARY_CHAR) {
*start_offset = -1;
*end_offset = -1;
return nullptr;
}
// ATK does not offer support for the special negative index and we don't
// want to do arithmetic on that value below.
if (offset == kStringLengthOffset)
return nullptr;
return GetCharacter(atk_text, offset - 1, start_offset, end_offset);
}
gint GetCaretOffset(AtkText* atk_text) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), -1);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (!obj)
return -1;
return obj->GetCaretOffset();
}
gboolean SetCaretOffset(AtkText* atk_text, gint offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (!obj)
return FALSE;
if (!obj->SetCaretOffset(offset))
return FALSE;
// Orca expects atk_text_set_caret_offset to either focus the target element
// or set the sequential focus navigation starting point there.
int utf16_offset = obj->UnicodeToUTF16OffsetInText(offset);
obj->GrabFocusOrSetSequentialFocusNavigationStartingPointAtOffset(
utf16_offset);
return TRUE;
}
int GetNSelections(AtkText* atk_text) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), 0);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (!obj)
return 0;
if (obj->HasSelection())
return 1;
base::Optional<FindInPageResultInfo> result =
obj->GetSelectionOffsetsFromFindInPage();
if (result.has_value() && result->node == ATK_OBJECT(atk_text))
return 1;
return 0;
}
gchar* GetSelection(AtkText* atk_text,
int selection_num,
int* start_offset,
int* end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (!obj)
return nullptr;
if (selection_num != 0)
return nullptr;
return obj->GetSelectionWithText(start_offset, end_offset);
}
gboolean RemoveSelection(AtkText* atk_text, int selection_num) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), FALSE);
if (selection_num != 0)
return FALSE;
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (!obj)
return FALSE;
// Simply collapse the selection to the position of the caret if a caret is
// visible, otherwise set the selection to 0.
int selection_end = obj->UTF16ToUnicodeOffsetInText(
obj->GetIntAttribute(ax::mojom::IntAttribute::kTextSelEnd));
return SetCaretOffset(atk_text, selection_end);
}
gboolean SetSelection(AtkText* atk_text,
int selection_num,
int start_offset,
int end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), FALSE);
if (selection_num != 0)
return FALSE;
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (!obj)
return FALSE;
return obj->SetTextSelectionForAtkText(start_offset, end_offset);
}
gboolean AddSelection(AtkText* atk_text, int start_offset, int end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), FALSE);
// We only support one selection.
return SetSelection(atk_text, 0, start_offset, end_offset);
}
#if defined(ATK_210)
char* GetStringAtOffset(AtkText* atk_text,
int offset,
AtkTextGranularity atk_granularity,
int* start_offset,
int* end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
*start_offset = -1;
*end_offset = -1;
ax::mojom::TextBoundary boundary = FromAtkTextGranularity(atk_granularity);
return GetTextWithBoundaryType(atk_text, offset, boundary, start_offset,
end_offset);
}
#endif
#if defined(ATK_230)
gfx::Rect GetUnclippedParentHypertextRangeBoundsRect(
AXPlatformNodeDelegate* ax_platform_node_delegate,
const int start_offset,
const int end_offset) {
const AXPlatformNode* parent_platform_node =
AXPlatformNode::FromNativeViewAccessible(
ax_platform_node_delegate->GetParent());
if (!parent_platform_node)
return gfx::Rect();
const AXPlatformNodeDelegate* parent_ax_platform_node_delegate =
parent_platform_node->GetDelegate();
if (!parent_ax_platform_node_delegate)
return gfx::Rect();
return ax_platform_node_delegate->GetHypertextRangeBoundsRect(
start_offset, end_offset, AXCoordinateSystem::kRootFrame,
AXClippingBehavior::kUnclipped) -
parent_ax_platform_node_delegate
->GetBoundsRect(AXCoordinateSystem::kRootFrame,
AXClippingBehavior::kClipped)
.OffsetFromOrigin();
}
#endif
void GetCharacterExtents(AtkText* atk_text,
int offset,
int* x,
int* y,
int* width,
int* height,
AtkCoordType coordinate_type) {
g_return_if_fail(ATK_IS_TEXT(atk_text));
gfx::Rect rect;
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (obj) {
switch (coordinate_type) {
#if defined(ATK_230)
case ATK_XY_PARENT:
rect = GetUnclippedParentHypertextRangeBoundsRect(obj->GetDelegate(),
offset, offset + 1);
break;
#endif
default:
rect = obj->GetDelegate()->GetHypertextRangeBoundsRect(
obj->UnicodeToUTF16OffsetInText(offset),
obj->UnicodeToUTF16OffsetInText(offset + 1),
AtkCoordTypeToAXCoordinateSystem(coordinate_type),
AXClippingBehavior::kUnclipped);
break;
}
}
if (x)
*x = rect.x();
if (y)
*y = rect.y();
if (width)
*width = rect.width();
if (height)
*height = rect.height();
}
void GetRangeExtents(AtkText* atk_text,
int start_offset,
int end_offset,
AtkCoordType coordinate_type,
AtkTextRectangle* out_rectangle) {
g_return_if_fail(ATK_IS_TEXT(atk_text));
if (!out_rectangle)
return;
gfx::Rect rect;
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (obj) {
switch (coordinate_type) {
#if defined(ATK_230)
case ATK_XY_PARENT:
rect = GetUnclippedParentHypertextRangeBoundsRect(
obj->GetDelegate(), start_offset, end_offset);
break;
#endif
default:
rect = obj->GetDelegate()->GetHypertextRangeBoundsRect(
obj->UnicodeToUTF16OffsetInText(start_offset),
obj->UnicodeToUTF16OffsetInText(end_offset),
AtkCoordTypeToAXCoordinateSystem(coordinate_type),
AXClippingBehavior::kUnclipped);
break;
}
}
out_rectangle->x = rect.x();
out_rectangle->y = rect.y();
out_rectangle->width = rect.width();
out_rectangle->height = rect.height();
}
AtkAttributeSet* GetRunAttributes(AtkText* atk_text,
gint offset,
gint* start_offset,
gint* end_offset) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
SetIntPointerValueIfNotNull(start_offset, -1);
SetIntPointerValueIfNotNull(end_offset, -1);
if (offset < 0 || offset > GetCharacterCount(atk_text))
return nullptr;
AtkObject* atk_object = ATK_OBJECT(atk_text);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return ToAtkAttributeSet(
obj->GetTextAttributes(offset, start_offset, end_offset));
}
AtkAttributeSet* GetDefaultAttributes(AtkText* atk_text) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), nullptr);
AtkObject* atk_object = ATK_OBJECT(atk_text);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return ToAtkAttributeSet(obj->GetDefaultTextAttributes());
}
#if defined(ATK_232)
gboolean ScrollSubstringTo(AtkText* atk_text,
gint start_offset,
gint end_offset,
AtkScrollType scroll_type) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (!obj)
return FALSE;
return obj->ScrollSubstringIntoView(scroll_type, start_offset, end_offset);
}
gboolean ScrollSubstringToPoint(AtkText* atk_text,
gint start_offset,
gint end_offset,
AtkCoordType atk_coord_type,
gint x,
gint y) {
g_return_val_if_fail(ATK_IS_TEXT(atk_text), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(atk_text));
if (!obj)
return FALSE;
return obj->ScrollSubstringToPoint(start_offset, end_offset, atk_coord_type,
x, y);
}
#endif // ATK_232
void Init(AtkTextIface* iface) {
iface->get_text = GetText;
iface->get_character_count = GetCharacterCount;
iface->get_character_at_offset = GetCharacterAtOffset;
iface->get_offset_at_point = GetOffsetAtPoint;
iface->get_text_after_offset = GetTextAfterOffset;
iface->get_text_before_offset = GetTextBeforeOffset;
iface->get_text_at_offset = GetTextAtOffset;
iface->get_caret_offset = GetCaretOffset;
iface->set_caret_offset = SetCaretOffset;
iface->get_character_extents = GetCharacterExtents;
iface->get_range_extents = GetRangeExtents;
iface->get_n_selections = GetNSelections;
iface->get_selection = GetSelection;
iface->add_selection = AddSelection;
iface->remove_selection = RemoveSelection;
iface->set_selection = SetSelection;
iface->get_run_attributes = GetRunAttributes;
iface->get_default_attributes = GetDefaultAttributes;
#if defined(ATK_210)
iface->get_string_at_offset = GetStringAtOffset;
#endif
#if defined(ATK_232)
if (SupportsAtkTextScrollingInterface()) {
iface->scroll_substring_to = ScrollSubstringTo;
iface->scroll_substring_to_point = ScrollSubstringToPoint;
}
#endif
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_text
namespace atk_window {
void Init(AtkWindowIface* iface) {}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_window
namespace atk_selection {
gboolean AddSelection(AtkSelection* selection, gint index) {
g_return_val_if_fail(ATK_IS_SELECTION(selection), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(selection));
if (!obj)
return FALSE;
if (index < 0 || index >= obj->GetChildCount())
return FALSE;
AXPlatformNodeAuraLinux* child =
AXPlatformNodeAuraLinux::FromAtkObject(obj->ChildAtIndex(index));
if (!child)
return FALSE;
if (!child->SupportsSelectionWithAtkSelection())
return FALSE;
bool selected = child->GetBoolAttribute(ax::mojom::BoolAttribute::kSelected);
if (selected)
return TRUE;
AXActionData data;
data.action = ax::mojom::Action::kDoDefault;
return child->GetDelegate()->AccessibilityPerformAction(data);
}
gboolean ClearSelection(AtkSelection* selection) {
g_return_val_if_fail(ATK_IS_SELECTION(selection), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(selection));
if (!obj)
return FALSE;
int child_count = obj->GetChildCount();
bool success = true;
for (int i = 0; i < child_count; ++i) {
AXPlatformNodeAuraLinux* child =
AXPlatformNodeAuraLinux::FromAtkObject(obj->ChildAtIndex(i));
if (!child)
continue;
if (!child->SupportsSelectionWithAtkSelection())
continue;
bool selected =
child->GetBoolAttribute(ax::mojom::BoolAttribute::kSelected);
if (!selected)
continue;
AXActionData data;
data.action = ax::mojom::Action::kDoDefault;
success = success && child->GetDelegate()->AccessibilityPerformAction(data);
}
return success;
}
AtkObject* RefSelection(AtkSelection* selection, gint requested_child_index) {
g_return_val_if_fail(ATK_IS_SELECTION(selection), nullptr);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(selection));
if (!obj)
return nullptr;
if (auto* selected_child = obj->GetSelectedItem(requested_child_index)) {
if (AtkObject* atk_object = selected_child->GetNativeViewAccessible()) {
g_object_ref(atk_object);
return atk_object;
}
}
return nullptr;
}
gint GetSelectionCount(AtkSelection* selection) {
g_return_val_if_fail(ATK_IS_SELECTION(selection), 0);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(selection));
if (!obj)
return 0;
return obj->GetSelectionCount();
}
gboolean IsChildSelected(AtkSelection* selection, gint index) {
g_return_val_if_fail(ATK_IS_SELECTION(selection), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(selection));
if (!obj)
return FALSE;
if (index < 0 || index >= obj->GetChildCount())
return FALSE;
AXPlatformNodeAuraLinux* child =
AXPlatformNodeAuraLinux::FromAtkObject(obj->ChildAtIndex(index));
return child && child->GetBoolAttribute(ax::mojom::BoolAttribute::kSelected);
}
gboolean RemoveSelection(AtkSelection* selection,
gint index_into_selected_children) {
g_return_val_if_fail(ATK_IS_SELECTION(selection), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(selection));
if (!obj)
return FALSE;
int child_count = obj->GetChildCount();
for (int i = 0; i < child_count; ++i) {
AXPlatformNodeAuraLinux* child =
AXPlatformNodeAuraLinux::FromAtkObject(obj->ChildAtIndex(i));
if (!child)
continue;
bool selected =
child->GetBoolAttribute(ax::mojom::BoolAttribute::kSelected);
if (selected && index_into_selected_children == 0) {
if (!child->SupportsSelectionWithAtkSelection())
return FALSE;
AXActionData data;
data.action = ax::mojom::Action::kDoDefault;
return child->GetDelegate()->AccessibilityPerformAction(data);
} else if (selected) {
index_into_selected_children--;
}
}
return FALSE;
}
gboolean SelectAllSelection(AtkSelection* selection) {
g_return_val_if_fail(ATK_IS_SELECTION(selection), FALSE);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(selection));
if (!obj)
return FALSE;
int child_count = obj->GetChildCount();
bool success = true;
for (int i = 0; i < child_count; ++i) {
AXPlatformNodeAuraLinux* child =
AXPlatformNodeAuraLinux::FromAtkObject(obj->ChildAtIndex(i));
if (!child)
continue;
if (!child->SupportsSelectionWithAtkSelection())
continue;
bool selected =
child->GetBoolAttribute(ax::mojom::BoolAttribute::kSelected);
if (selected)
continue;
AXActionData data;
data.action = ax::mojom::Action::kDoDefault;
success = success && child->GetDelegate()->AccessibilityPerformAction(data);
}
return success;
}
void Init(AtkSelectionIface* iface) {
iface->add_selection = AddSelection;
iface->clear_selection = ClearSelection;
iface->ref_selection = RefSelection;
iface->get_selection_count = GetSelectionCount;
iface->is_child_selected = IsChildSelected;
iface->remove_selection = RemoveSelection;
iface->select_all_selection = SelectAllSelection;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_selection
namespace atk_table {
AtkObject* RefAt(AtkTable* table, gint row, gint column) {
g_return_val_if_fail(ATK_IS_TABLE(table), nullptr);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
if (AXPlatformNodeBase* cell = obj->GetTableCell(row, column)) {
if (AtkObject* atk_cell = cell->GetNativeViewAccessible()) {
g_object_ref(atk_cell);
return atk_cell;
}
}
}
return nullptr;
}
gint GetIndexAt(AtkTable* table, gint row, gint column) {
g_return_val_if_fail(ATK_IS_TABLE(table), -1);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
if (const AXPlatformNodeBase* cell = obj->GetTableCell(row, column)) {
DCHECK(cell->GetTableCellIndex().has_value());
return cell->GetTableCellIndex().value();
}
}
return -1;
}
gint GetColumnAtIndex(AtkTable* table, gint index) {
g_return_val_if_fail(ATK_IS_TABLE(table), -1);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
if (const AXPlatformNodeBase* cell = obj->GetTableCell(index)) {
DCHECK(cell->GetTableColumn().has_value());
return cell->GetTableColumn().value();
}
}
return -1;
}
gint GetRowAtIndex(AtkTable* table, gint index) {
g_return_val_if_fail(ATK_IS_TABLE(table), -1);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
if (const AXPlatformNodeBase* cell = obj->GetTableCell(index)) {
DCHECK(cell->GetTableRow().has_value());
return cell->GetTableRow().value();
}
}
return -1;
}
gint GetNColumns(AtkTable* table) {
g_return_val_if_fail(ATK_IS_TABLE(table), 0);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
// If the object is not a table, we return 0.
return obj->GetTableColumnCount().value_or(0);
}
return 0;
}
gint GetNRows(AtkTable* table) {
g_return_val_if_fail(ATK_IS_TABLE(table), 0);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
// If the object is not a table, we return 0.
return obj->GetTableRowCount().value_or(0);
}
return 0;
}
gint GetColumnExtentAt(AtkTable* table, gint row, gint column) {
g_return_val_if_fail(ATK_IS_TABLE(table), 0);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
if (const AXPlatformNodeBase* cell = obj->GetTableCell(row, column)) {
DCHECK(cell->GetTableColumnSpan().has_value());
return cell->GetTableColumnSpan().value();
}
}
return 0;
}
gint GetRowExtentAt(AtkTable* table, gint row, gint column) {
g_return_val_if_fail(ATK_IS_TABLE(table), 0);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
if (const AXPlatformNodeBase* cell = obj->GetTableCell(row, column)) {
DCHECK(cell->GetTableRowSpan().has_value());
return cell->GetTableRowSpan().value();
}
}
return 0;
}
AtkObject* GetColumnHeader(AtkTable* table, gint column) {
g_return_val_if_fail(ATK_IS_TABLE(table), nullptr);
auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table));
if (!obj)
return nullptr;
// AtkTable supports only one column header object. So return the first one
// we find. In the case of multiple headers, ATs can fall back on the column
// description.
std::vector<int32_t> ids = obj->GetDelegate()->GetColHeaderNodeIds(column);
for (const auto& node_id : ids) {
if (AXPlatformNode* header = obj->GetDelegate()->GetFromNodeID(node_id)) {
if (AtkObject* atk_header = header->GetNativeViewAccessible()) {
g_object_ref(atk_header);
return atk_header;
}
}
}
return nullptr;
}
AtkObject* GetRowHeader(AtkTable* table, gint row) {
g_return_val_if_fail(ATK_IS_TABLE(table), nullptr);
auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table));
if (!obj)
return nullptr;
// AtkTable supports only one row header object. So return the first one
// we find. In the case of multiple headers, ATs can fall back on the row
// description.
std::vector<int32_t> ids = obj->GetDelegate()->GetRowHeaderNodeIds(row);
for (const auto& node_id : ids) {
if (AXPlatformNode* header = obj->GetDelegate()->GetFromNodeID(node_id)) {
if (AtkObject* atk_header = header->GetNativeViewAccessible()) {
g_object_ref(atk_header);
return atk_header;
}
}
}
return nullptr;
}
AtkObject* GetCaption(AtkTable* table) {
g_return_val_if_fail(ATK_IS_TABLE(table), nullptr);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table))) {
if (auto* caption = obj->GetTableCaption())
return caption->GetNativeViewAccessible();
}
return nullptr;
}
const gchar* GetColumnDescription(AtkTable* table, gint column) {
g_return_val_if_fail(ATK_IS_TABLE(table), nullptr);
auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table));
if (!obj)
return nullptr;
std::vector<int32_t> ids = obj->GetDelegate()->GetColHeaderNodeIds(column);
return BuildDescriptionFromHeaders(obj->GetDelegate(), ids);
}
const gchar* GetRowDescription(AtkTable* table, gint row) {
g_return_val_if_fail(ATK_IS_TABLE(table), nullptr);
auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(table));
if (!obj)
return nullptr;
std::vector<int32_t> ids = obj->GetDelegate()->GetRowHeaderNodeIds(row);
return BuildDescriptionFromHeaders(obj->GetDelegate(), ids);
}
void Init(AtkTableIface* iface) {
iface->ref_at = RefAt;
iface->get_index_at = GetIndexAt;
iface->get_column_at_index = GetColumnAtIndex;
iface->get_row_at_index = GetRowAtIndex;
iface->get_n_columns = GetNColumns;
iface->get_n_rows = GetNRows;
iface->get_column_extent_at = GetColumnExtentAt;
iface->get_row_extent_at = GetRowExtentAt;
iface->get_column_header = GetColumnHeader;
iface->get_row_header = GetRowHeader;
iface->get_caption = GetCaption;
iface->get_column_description = GetColumnDescription;
iface->get_row_description = GetRowDescription;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_table
// The ATK table cell interface was added in ATK 2.12.
#if defined(ATK_212)
namespace atk_table_cell {
gint GetColumnSpan(AtkTableCell* cell) {
DCHECK(g_atk_table_cell_get_type);
g_return_val_if_fail(
G_TYPE_CHECK_INSTANCE_TYPE((cell), AtkTableCellInterface::GetType()), 0);
if (const AXPlatformNodeBase* obj =
AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(cell))) {
// If the object is not a cell, we return 0.
return obj->GetTableColumnSpan().value_or(0);
}
return 0;
}
GPtrArray* GetColumnHeaderCells(AtkTableCell* cell) {
DCHECK(g_atk_table_cell_get_type);
g_return_val_if_fail(
G_TYPE_CHECK_INSTANCE_TYPE((cell), AtkTableCellInterface::GetType()),
nullptr);
GPtrArray* array = g_ptr_array_new_with_free_func(g_object_unref);
auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(cell));
if (!obj)
return array;
// AtkTableCell is implemented on cells, row headers, and column headers.
// Calling GetColHeaderNodeIds() on a column header cell will include that
// column header, along with any other column headers in the column which
// may or may not describe the header cell in question. Therefore, just return
// headers for non-header cells.
if (obj->GetAtkRole() != ATK_ROLE_TABLE_CELL)
return array;
base::Optional<int> col_index = obj->GetTableColumn();
if (!col_index)
return array;
const std::vector<int32_t> ids =
obj->GetDelegate()->GetColHeaderNodeIds(*col_index);
for (const auto& node_id : ids) {
if (AXPlatformNode* node = obj->GetDelegate()->GetFromNodeID(node_id)) {
if (AtkObject* atk_node = node->GetNativeViewAccessible()) {
g_ptr_array_add(array, g_object_ref(atk_node));
}
}
}
return array;
}
gboolean GetCellPosition(AtkTableCell* cell, gint* row, gint* column) {
DCHECK(g_atk_table_cell_get_type);
g_return_val_if_fail(
G_TYPE_CHECK_INSTANCE_TYPE((cell), AtkTableCellInterface::GetType()),
FALSE);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(cell))) {
base::Optional<int> row_index = obj->GetTableRow();
base::Optional<int> col_index = obj->GetTableColumn();
if (!row_index || !col_index)
return false;
*row = *row_index;
*column = *col_index;
return true;
}
return false;
}
gint GetRowSpan(AtkTableCell* cell) {
DCHECK(g_atk_table_cell_get_type);
g_return_val_if_fail(
G_TYPE_CHECK_INSTANCE_TYPE((cell), AtkTableCellInterface::GetType()), 0);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(cell))) {
// If the object is not a cell, we return 0.
return obj->GetTableRowSpan().value_or(0);
}
return 0;
}
GPtrArray* GetRowHeaderCells(AtkTableCell* cell) {
DCHECK(g_atk_table_cell_get_type);
g_return_val_if_fail(
G_TYPE_CHECK_INSTANCE_TYPE((cell), AtkTableCellInterface::GetType()),
nullptr);
GPtrArray* array = g_ptr_array_new_with_free_func(g_object_unref);
auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(cell));
if (!obj)
return array;
// AtkTableCell is implemented on cells, row headers, and column headers.
// Calling GetRowHeaderNodeIds() on a row header cell will include that
// row header, along with any other row headers in the row which may or
// may not describe the header cell in question. Therefore, just return
// headers for non-header cells.
if (obj->GetAtkRole() != ATK_ROLE_TABLE_CELL)
return array;
base::Optional<int> row_index = obj->GetTableRow();
if (!row_index)
return array;
const std::vector<int32_t> ids =
obj->GetDelegate()->GetRowHeaderNodeIds(*row_index);
for (const auto& node_id : ids) {
if (AXPlatformNode* node = obj->GetDelegate()->GetFromNodeID(node_id)) {
if (AtkObject* atk_node = node->GetNativeViewAccessible()) {
g_ptr_array_add(array, g_object_ref(atk_node));
}
}
}
return array;
}
AtkObject* GetTable(AtkTableCell* cell) {
DCHECK(g_atk_table_cell_get_type);
g_return_val_if_fail(
G_TYPE_CHECK_INSTANCE_TYPE((cell), AtkTableCellInterface::GetType()),
nullptr);
if (auto* obj = AXPlatformNodeAuraLinux::FromAtkObject(ATK_OBJECT(cell))) {
if (auto* table = obj->GetTable())
return table->GetNativeViewAccessible();
}
return nullptr;
}
using AtkTableCellIface = struct _AtkTableCellIface;
void Init(AtkTableCellIface* iface) {
iface->get_column_span = GetColumnSpan;
iface->get_column_header_cells = GetColumnHeaderCells;
iface->get_position = GetCellPosition;
iface->get_row_span = GetRowSpan;
iface->get_row_header_cells = GetRowHeaderCells;
iface->get_table = GetTable;
}
const GInterfaceInfo Info = {reinterpret_cast<GInterfaceInitFunc>(Init),
nullptr, nullptr};
} // namespace atk_table_cell
#endif // ATK_212
namespace atk_object {
gpointer kAXPlatformNodeAuraLinuxParentClass = nullptr;
const gchar* GetName(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), nullptr);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
if (!obj->IsNameExposed())
return nullptr;
ax::mojom::NameFrom name_from = obj->GetData().GetNameFrom();
if (obj->GetName().empty() &&
name_from != ax::mojom::NameFrom::kAttributeExplicitlyEmpty)
return nullptr;
obj->accessible_name_ = obj->GetName();
return obj->accessible_name_.c_str();
}
const gchar* AtkGetName(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kGetName);
return GetName(atk_object);
}
const gchar* GetDescription(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), nullptr);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return obj->GetStringAttribute(ax::mojom::StringAttribute::kDescription)
.c_str();
}
const gchar* AtkGetDescription(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kGetDescription);
return GetDescription(atk_object);
}
gint GetNChildren(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), 0);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return 0;
return obj->GetChildCount();
}
gint AtkGetNChildren(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kGetNChildren);
return GetNChildren(atk_object);
}
AtkObject* RefChild(AtkObject* atk_object, gint index) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), nullptr);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
if (index < 0 || index >= obj->GetChildCount())
return nullptr;
AtkObject* result = obj->ChildAtIndex(index);
if (result)
g_object_ref(result);
return result;
}
AtkObject* AtkRefChild(AtkObject* atk_object, gint index) {
RecordAccessibilityAtkApi(UmaAtkApi::kRefChild);
return RefChild(atk_object, index);
}
gint GetIndexInParent(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), -1);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return -1;
return obj->GetIndexInParent().value_or(-1);
}
gint AtkGetIndexInParent(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kGetIndexInParent);
return GetIndexInParent(atk_object);
}
AtkObject* GetParent(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), nullptr);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return obj->GetParent();
}
AtkObject* AtkGetParent(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kGetParent);
return GetParent(atk_object);
}
AtkRelationSet* RefRelationSet(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), nullptr);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return atk_relation_set_new();
return obj->GetAtkRelations();
}
AtkRelationSet* AtkRefRelationSet(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kRefRelationSet);
// Enables AX mode. Most AT does not call AtkRefRelationSet, but Orca does,
// which is why it's a good signal to enable accessibility for Orca users
// without too many false positives.
AXPlatformNodeAuraLinux::EnableAXMode();
return RefRelationSet(atk_object);
}
AtkAttributeSet* GetAttributes(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), nullptr);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return nullptr;
return obj->GetAtkAttributes();
}
AtkAttributeSet* AtkGetAttributes(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kGetAttributes);
// Enables AX mode. Most AT does not call AtkGetAttributes, but Orca does,
// which is why it's a good signal to enable accessibility for Orca users
// without too many false positives.
AXPlatformNodeAuraLinux::EnableAXMode();
return GetAttributes(atk_object);
}
AtkRole GetRole(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), ATK_ROLE_INVALID);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj)
return ATK_ROLE_INVALID;
return obj->GetAtkRole();
}
AtkRole AtkGetRole(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kGetRole);
return GetRole(atk_object);
}
AtkStateSet* RefStateSet(AtkObject* atk_object) {
g_return_val_if_fail(ATK_IS_OBJECT(atk_object), nullptr);
AtkStateSet* atk_state_set =
ATK_OBJECT_CLASS(kAXPlatformNodeAuraLinuxParentClass)
->ref_state_set(atk_object);
AXPlatformNodeAuraLinux* obj =
AXPlatformNodeAuraLinux::FromAtkObject(atk_object);
if (!obj) {
atk_state_set_add_state(atk_state_set, ATK_STATE_DEFUNCT);
} else {
obj->GetAtkState(atk_state_set);
}
return atk_state_set;
}
AtkStateSet* AtkRefStateSet(AtkObject* atk_object) {
RecordAccessibilityAtkApi(UmaAtkApi::kRefStateSet);
return RefStateSet(atk_object);
}
void Initialize(AtkObject* atk_object, gpointer data) {
if (ATK_OBJECT_CLASS(kAXPlatformNodeAuraLinuxParentClass)->initialize) {
ATK_OBJECT_CLASS(kAXPlatformNodeAuraLinuxParentClass)
->initialize(atk_object, data);
}
AX_PLATFORM_NODE_AURALINUX(atk_object)->m_object =
reinterpret_cast<AXPlatformNodeAuraLinux*>(data);
}
void Finalize(GObject* atk_object) {
G_OBJECT_CLASS(kAXPlatformNodeAuraLinuxParentClass)->finalize(atk_object);
}
void ClassInit(gpointer class_pointer, gpointer /* class_data */) {
GObjectClass* gobject_class = G_OBJECT_CLASS(class_pointer);
kAXPlatformNodeAuraLinuxParentClass = g_type_class_peek_parent(gobject_class);
gobject_class->finalize = Finalize;
AtkObjectClass* atk_object_class = ATK_OBJECT_CLASS(gobject_class);
atk_object_class->initialize = Initialize;
atk_object_class->get_name = AtkGetName;
atk_object_class->get_description = AtkGetDescription;
atk_object_class->get_parent = AtkGetParent;
atk_object_class->get_n_children = AtkGetNChildren;
atk_object_class->ref_child = AtkRefChild;
atk_object_class->get_role = AtkGetRole;
atk_object_class->ref_state_set = AtkRefStateSet;
atk_object_class->get_index_in_parent = AtkGetIndexInParent;
atk_object_class->ref_relation_set = AtkRefRelationSet;
atk_object_class->get_attributes = AtkGetAttributes;
}
GType GetType() {
AXPlatformNodeAuraLinux::EnsureGTypeInit();
static volatile gsize type_volatile = 0;
if (g_once_init_enter(&type_volatile)) {
static const GTypeInfo type_info = {
sizeof(AXPlatformNodeAuraLinuxClass), // class_size
nullptr, // base_init
nullptr, // base_finalize
atk_object::ClassInit,
nullptr, // class_finalize
nullptr, // class_data
sizeof(AXPlatformNodeAuraLinuxObject), // instance_size
0, // n_preallocs
nullptr, // instance_init
nullptr // value_table
};
GType type = g_type_register_static(
ATK_TYPE_OBJECT, "AXPlatformNodeAuraLinux", &type_info, GTypeFlags(0));
g_once_init_leave(&type_volatile, type);
}
return type_volatile;
}
void Detach(AXPlatformNodeAuraLinuxObject* atk_object) {
if (!atk_object->m_object)
return;
atk_object->m_object = nullptr;
atk_object_notify_state_change(ATK_OBJECT(atk_object), ATK_STATE_DEFUNCT,
TRUE);
}
} // namespace atk_object
} // namespace
// static
NO_SANITIZE("cfi-icall")
GType AtkTableCellInterface::GetType() {
return g_atk_table_cell_get_type();
}
// static
NO_SANITIZE("cfi-icall")
GPtrArray* AtkTableCellInterface::GetColumnHeaderCells(AtkTableCell* cell) {
return g_atk_table_cell_get_column_header_cells(cell);
}
// static
NO_SANITIZE("cfi-icall")
GPtrArray* AtkTableCellInterface::GetRowHeaderCells(AtkTableCell* cell) {
return g_atk_table_cell_get_row_header_cells(cell);
}
// static
NO_SANITIZE("cfi-icall")
bool AtkTableCellInterface::GetRowColumnSpan(AtkTableCell* cell,
gint* row,
gint* column,
gint* row_span,
gint* col_span) {
return g_atk_table_cell_get_row_column_span(cell, row, column, row_span,
col_span);
}
// static
bool AtkTableCellInterface::Exists() {
g_atk_table_cell_get_type = reinterpret_cast<GetTypeFunc>(
dlsym(RTLD_DEFAULT, "atk_table_cell_get_type"));
g_atk_table_cell_get_column_header_cells =
reinterpret_cast<GetColumnHeaderCellsFunc>(
dlsym(RTLD_DEFAULT, "atk_table_cell_get_column_header_cells"));
g_atk_table_cell_get_row_header_cells =
reinterpret_cast<GetRowHeaderCellsFunc>(
dlsym(RTLD_DEFAULT, "atk_table_cell_get_row_header_cells"));
g_atk_table_cell_get_row_column_span = reinterpret_cast<GetRowColumnSpanFunc>(
dlsym(RTLD_DEFAULT, "atk_table_cell_get_row_column_span"));
return *g_atk_table_cell_get_type;
}
void AXPlatformNodeAuraLinux::EnsureGTypeInit() {
#if !GLIB_CHECK_VERSION(2, 36, 0)
static bool first_time = true;
if (UNLIKELY(first_time)) {
g_type_init();
first_time = false;
}
#endif
}
// static
ImplementedAtkInterfaces AXPlatformNodeAuraLinux::GetGTypeInterfaceMask(
const AXNodeData& data) {
// The default implementation set includes the AtkComponent and AtkAction
// interfaces, which are provided by all the AtkObjects that we produce.
ImplementedAtkInterfaces interface_mask;
if (!IsImageOrVideo(data.role)) {
interface_mask.Add(ImplementedAtkInterfaces::Value::kText);
if (!data.IsPlainTextField())
interface_mask.Add(ImplementedAtkInterfaces::Value::kHypertext);
}
if (data.IsRangeValueSupported())
interface_mask.Add(ImplementedAtkInterfaces::Value::kValue);
if (ui::IsDocument(data.role))
interface_mask.Add(ImplementedAtkInterfaces::Value::kDocument);
if (IsImage(data.role))
interface_mask.Add(ImplementedAtkInterfaces::Value::kImage);
// The AtkHyperlinkImpl interface allows getting a AtkHyperlink from an
// AtkObject. It is indeed implemented by actual web hyperlinks, but also by
// objects that will become embedded objects in ATK hypertext, so the name is
// a bit of a misnomer from the ATK API.
if (IsLink(data.role) || data.role == ax::mojom::Role::kAnchor ||
!ui::IsText(data.role)) {
interface_mask.Add(ImplementedAtkInterfaces::Value::kHyperlink);
}
if (data.role == ax::mojom::Role::kWindow)
interface_mask.Add(ImplementedAtkInterfaces::Value::kWindow);
if (IsContainerWithSelectableChildren(data.role))
interface_mask.Add(ImplementedAtkInterfaces::Value::kSelection);
if (IsTableLike(data.role))
interface_mask.Add(ImplementedAtkInterfaces::Value::kTable);
// Because the TableCell Interface is only supported in ATK version 2.12 and
// later, GetAccessibilityGType has a runtime check to verify we have a recent
// enough version. If we don't, GetAccessibilityGType will exclude
// AtkTableCell from the supported interfaces and none of its methods or
// properties will be exposed to assistive technologies.
if (IsCellOrTableHeader(data.role))
interface_mask.Add(ImplementedAtkInterfaces::Value::kTableCell);
return interface_mask;
}
GType AXPlatformNodeAuraLinux::GetAccessibilityGType() {
static const GTypeInfo type_info = {
sizeof(AXPlatformNodeAuraLinuxClass),
(GBaseInitFunc) nullptr,
(GBaseFinalizeFunc) nullptr,
(GClassInitFunc) nullptr,
(GClassFinalizeFunc) nullptr,
nullptr, /* class data */
sizeof(AXPlatformNodeAuraLinuxObject), /* instance size */
0, /* nb preallocs */
(GInstanceInitFunc) nullptr,
nullptr /* value table */
};
const char* atk_type_name = GetUniqueAccessibilityGTypeName(interface_mask_);
GType type = g_type_from_name(atk_type_name);
if (type)
return type;
type = g_type_register_static(AX_PLATFORM_NODE_AURALINUX_TYPE, atk_type_name,
&type_info, GTypeFlags(0));
// The AtkComponent and AtkAction interfaces are always supported.
g_type_add_interface_static(type, ATK_TYPE_COMPONENT, &atk_component::Info);
g_type_add_interface_static(type, ATK_TYPE_ACTION, &atk_action::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kDocument))
g_type_add_interface_static(type, ATK_TYPE_DOCUMENT, &atk_document::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kImage))
g_type_add_interface_static(type, ATK_TYPE_IMAGE, &atk_image::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kValue))
g_type_add_interface_static(type, ATK_TYPE_VALUE, &atk_value::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kHyperlink)) {
g_type_add_interface_static(type, ATK_TYPE_HYPERLINK_IMPL,
&atk_hyperlink::Info);
}
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kHypertext))
g_type_add_interface_static(type, ATK_TYPE_HYPERTEXT, &atk_hypertext::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kText))
g_type_add_interface_static(type, ATK_TYPE_TEXT, &atk_text::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kWindow))
g_type_add_interface_static(type, ATK_TYPE_WINDOW, &atk_window::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kSelection))
g_type_add_interface_static(type, ATK_TYPE_SELECTION, &atk_selection::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kTable))
g_type_add_interface_static(type, ATK_TYPE_TABLE, &atk_table::Info);
if (interface_mask_.Implements(ImplementedAtkInterfaces::Value::kTableCell)) {
// Run-time check to ensure AtkTableCell is supported (requires ATK 2.12).
if (AtkTableCellInterface::Exists()) {
g_type_add_interface_static(type, AtkTableCellInterface::GetType(),
&atk_table_cell::Info);
}
}
return type;
}
void AXPlatformNodeAuraLinux::SetDocumentParentOnFrameIfNecessary() {
if (GetAtkRole() != ATK_ROLE_DOCUMENT_WEB)
return;
if (!GetDelegate()->IsWebContent())
return;
AtkObject* parent_atk_object = GetParent();
AXPlatformNodeAuraLinux* parent =
AXPlatformNodeAuraLinux::FromAtkObject(parent_atk_object);
if (!parent)
return;
if (parent->GetDelegate()->IsWebContent())
return;
AXPlatformNodeAuraLinux* frame = AXPlatformNodeAuraLinux::FromAtkObject(
FindAtkObjectParentFrame(parent_atk_object));
if (!frame)
return;
frame->SetDocumentParent(parent_atk_object);
}
AtkObject* AXPlatformNodeAuraLinux::FindPrimaryWebContentDocument() {
// It could get multiple web contents since additional web content is added,
// when the DevTools window is opened.
std::vector<AtkObject*> web_content_candidates;
for (auto child_iterator_ptr = GetDelegate()->ChildrenBegin();
*child_iterator_ptr != *GetDelegate()->ChildrenEnd();
++(*child_iterator_ptr)) {
AtkObject* child = child_iterator_ptr->GetNativeViewAccessible();
auto* child_node = AXPlatformNodeAuraLinux::FromAtkObject(child);
if (!child_node)
continue;
if (!child_node->GetDelegate()->IsWebContent())
continue;
if (child_node->GetAtkRole() != ATK_ROLE_DOCUMENT_WEB)
continue;
web_content_candidates.push_back(child);
}
if (web_content_candidates.empty())
return nullptr;
// If it finds just one web content, return it.
if (web_content_candidates.size() == 1)
return web_content_candidates[0];
for (auto* object : web_content_candidates) {
auto* child_node = AXPlatformNodeAuraLinux::FromAtkObject(object);
// If it is a primary web contents, return it.
if (child_node->IsPrimaryWebContentsForWindow())
return object;
}
return nullptr;
}
bool AXPlatformNodeAuraLinux::IsWebDocumentForRelations() {
AtkObject* atk_object = GetOrCreateAtkObject();
if (!atk_object)
return false;
AXPlatformNodeAuraLinux* parent = FromAtkObject(GetParent());
if (!parent || !GetDelegate()->IsWebContent() ||
GetAtkRole() != ATK_ROLE_DOCUMENT_WEB)
return false;
return parent->FindPrimaryWebContentDocument() == atk_object;
}
AtkObject* AXPlatformNodeAuraLinux::CreateAtkObject() {
if (GetData().role != ax::mojom::Role::kApplication &&
!GetDelegate()->IsToplevelBrowserWindow() &&
!GetAccessibilityMode().has_mode(AXMode::kNativeAPIs))
return nullptr;
if (GetDelegate()->IsChildOfLeaf())
return nullptr;
EnsureGTypeInit();
interface_mask_ = GetGTypeInterfaceMask(GetData());
GType type = GetAccessibilityGType();
AtkObject* atk_object = static_cast<AtkObject*>(g_object_new(type, nullptr));
atk_object_initialize(atk_object, this);
SetDocumentParentOnFrameIfNecessary();
return ATK_OBJECT(atk_object);
}
void AXPlatformNodeAuraLinux::DestroyAtkObjects() {
if (atk_hyperlink_) {
ax_platform_atk_hyperlink_set_object(
AX_PLATFORM_ATK_HYPERLINK(atk_hyperlink_), nullptr);
g_object_unref(atk_hyperlink_);
atk_hyperlink_ = nullptr;
}
if (atk_object_) {
// We explicitly clear g_current_focused and g_current_active_descendant
// just in case there is another reference to atk_object_ somewhere.
if (atk_object_ == g_current_focused)
SetWeakGPtrToAtkObject(&g_current_focused, nullptr);
if (atk_object_ == g_current_active_descendant)
SetWeakGPtrToAtkObject(&g_current_active_descendant, nullptr);
atk_object::Detach(AX_PLATFORM_NODE_AURALINUX(atk_object_));
g_object_unref(atk_object_);
atk_object_ = nullptr;
}
}
// static
AXPlatformNode* AXPlatformNode::Create(AXPlatformNodeDelegate* delegate) {
AXPlatformNodeAuraLinux* node = new AXPlatformNodeAuraLinux();
node->Init(delegate);
return node;
}
// static
AXPlatformNode* AXPlatformNode::FromNativeViewAccessible(
gfx::NativeViewAccessible accessible) {
return AXPlatformNodeAuraLinux::FromAtkObject(accessible);
}
//
// AXPlatformNodeAuraLinux implementation.
//
// static
AXPlatformNodeAuraLinux* AXPlatformNodeAuraLinux::FromAtkObject(
const AtkObject* atk_object) {
if (!atk_object)
return nullptr;
if (IS_AX_PLATFORM_NODE_AURALINUX(atk_object)) {
AXPlatformNodeAuraLinuxObject* platform_object =
AX_PLATFORM_NODE_AURALINUX(atk_object);
return platform_object->m_object;
}
return nullptr;
}
// static
void AXPlatformNodeAuraLinux::SetApplication(AXPlatformNode* application) {
g_root_application = application;
}
// static
AXPlatformNode* AXPlatformNodeAuraLinux::application() {
return g_root_application;
}
// static
void AXPlatformNodeAuraLinux::StaticInitialize() {
AtkUtilAuraLinux::GetInstance()->InitializeAsync();
}
// static
void AXPlatformNodeAuraLinux::EnableAXMode() {
AXPlatformNode::NotifyAddAXModeFlags(kAXModeComplete);
}
AtkRole AXPlatformNodeAuraLinux::GetAtkRole() const {
switch (GetData().role) {
case ax::mojom::Role::kAlert:
return ATK_ROLE_NOTIFICATION;
case ax::mojom::Role::kAlertDialog:
return ATK_ROLE_ALERT;
case ax::mojom::Role::kAnchor:
return ATK_ROLE_LINK;
case ax::mojom::Role::kComment:
case ax::mojom::Role::kSuggestion:
return ATK_ROLE_SECTION;
case ax::mojom::Role::kApplication:
// Only use ATK_ROLE_APPLICATION for elements with no parent, since it
// is only for top level app windows and not ARIA applications.
if (!GetParent()) {
return ATK_ROLE_APPLICATION;
} else {
return ATK_ROLE_EMBEDDED;
}
case ax::mojom::Role::kArticle:
return ATK_ROLE_ARTICLE;
case ax::mojom::Role::kAudio:
return ATK_ROLE_AUDIO;
case ax::mojom::Role::kBanner:
case ax::mojom::Role::kHeader:
return ATK_ROLE_LANDMARK;
case ax::mojom::Role::kBlockquote:
return ATK_ROLE_BLOCK_QUOTE;
case ax::mojom::Role::kCaret:
return ATK_ROLE_UNKNOWN;
case ax::mojom::Role::kButton:
return ATK_ROLE_PUSH_BUTTON;
case ax::mojom::Role::kCanvas:
return ATK_ROLE_CANVAS;
case ax::mojom::Role::kCaption:
return ATK_ROLE_CAPTION;
case ax::mojom::Role::kCell:
return ATK_ROLE_TABLE_CELL;
case ax::mojom::Role::kCheckBox:
return ATK_ROLE_CHECK_BOX;
case ax::mojom::Role::kSwitch:
return ATK_ROLE_TOGGLE_BUTTON;
case ax::mojom::Role::kColorWell:
return ATK_ROLE_PUSH_BUTTON;
case ax::mojom::Role::kColumn:
return ATK_ROLE_UNKNOWN;
case ax::mojom::Role::kColumnHeader:
return ATK_ROLE_COLUMN_HEADER;
case ax::mojom::Role::kComboBoxGrouping:
return ATK_ROLE_COMBO_BOX;
case ax::mojom::Role::kComboBoxMenuButton:
return ATK_ROLE_COMBO_BOX;
case ax::mojom::Role::kComplementary:
return ATK_ROLE_LANDMARK;
case ax::mojom::Role::kContentDeletion:
return kAtkRoleContentDeletion;
case ax::mojom::