1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "InterfaceInitFuncs.h"
8 
9 #include "Accessible-inl.h"
10 #include "AccessibleWrap.h"
11 #include "nsAccUtils.h"
12 #include "nsCoreUtils.h"
13 #include "nsMai.h"
14 #include "mozilla/Likely.h"
15 #include "mozilla/a11y/DocAccessibleParent.h"
16 #include "mozilla/a11y/ProxyAccessible.h"
17 #include "mozilla/dom/BrowserParent.h"
18 
19 using namespace mozilla::a11y;
20 
21 extern "C" {
22 
refAccessibleAtPointCB(AtkComponent * aComponent,gint aAccX,gint aAccY,AtkCoordType aCoordType)23 static AtkObject* refAccessibleAtPointCB(AtkComponent* aComponent, gint aAccX,
24                                          gint aAccY, AtkCoordType aCoordType) {
25   return refAccessibleAtPointHelper(ATK_OBJECT(aComponent), aAccX, aAccY,
26                                     aCoordType);
27 }
28 
getExtentsCB(AtkComponent * aComponent,gint * aX,gint * aY,gint * aWidth,gint * aHeight,AtkCoordType aCoordType)29 static void getExtentsCB(AtkComponent* aComponent, gint* aX, gint* aY,
30                          gint* aWidth, gint* aHeight, AtkCoordType aCoordType) {
31   getExtentsHelper(ATK_OBJECT(aComponent), aX, aY, aWidth, aHeight, aCoordType);
32 }
33 
grabFocusCB(AtkComponent * aComponent)34 static gboolean grabFocusCB(AtkComponent* aComponent) {
35   AtkObject* atkObject = ATK_OBJECT(aComponent);
36   AccessibleWrap* accWrap = GetAccessibleWrap(atkObject);
37   if (accWrap) {
38     accWrap->TakeFocus();
39     return TRUE;
40   }
41 
42   ProxyAccessible* proxy = GetProxy(atkObject);
43   if (proxy) {
44     proxy->TakeFocus();
45     return TRUE;
46   }
47 
48   return FALSE;
49 }
50 
51 // ScrollType is compatible
52 MOZ_CAN_RUN_SCRIPT_BOUNDARY
scrollToCB(AtkComponent * aComponent,AtkScrollType type)53 static gboolean scrollToCB(AtkComponent* aComponent, AtkScrollType type) {
54   AtkObject* atkObject = ATK_OBJECT(aComponent);
55   if (RefPtr<AccessibleWrap> accWrap = GetAccessibleWrap(atkObject)) {
56     accWrap->ScrollTo(type);
57     return TRUE;
58   }
59 
60   ProxyAccessible* proxy = GetProxy(atkObject);
61   if (proxy) {
62     proxy->ScrollTo(type);
63     return TRUE;
64   }
65 
66   return FALSE;
67 }
68 
69 // CoordType is compatible
scrollToPointCB(AtkComponent * aComponent,AtkCoordType coords,gint x,gint y)70 static gboolean scrollToPointCB(AtkComponent* aComponent, AtkCoordType coords,
71                                 gint x, gint y) {
72   AtkObject* atkObject = ATK_OBJECT(aComponent);
73   AccessibleWrap* accWrap = GetAccessibleWrap(atkObject);
74   if (accWrap) {
75     accWrap->ScrollToPoint(coords, x, y);
76     return TRUE;
77   }
78 
79   ProxyAccessible* proxy = GetProxy(atkObject);
80   if (proxy) {
81     proxy->ScrollToPoint(coords, x, y);
82     return TRUE;
83   }
84 
85   return FALSE;
86 }
87 }
88 
refAccessibleAtPointHelper(AtkObject * aAtkObj,gint aX,gint aY,AtkCoordType aCoordType)89 AtkObject* refAccessibleAtPointHelper(AtkObject* aAtkObj, gint aX, gint aY,
90                                       AtkCoordType aCoordType) {
91   AccessibleOrProxy acc = GetInternalObj(aAtkObj);
92   if (acc.IsNull()) {
93     // This might be an ATK Socket.
94     acc = GetAccessibleWrap(aAtkObj);
95     if (acc.IsNull()) {
96       return nullptr;
97     }
98   }
99   if (acc.IsAccessible() && acc.AsAccessible()->IsDefunct()) {
100     return nullptr;
101   }
102 
103   // AccessibleOrProxy::ChildAtPoint(x,y) is in screen pixels.
104   if (aCoordType == ATK_XY_WINDOW) {
105     nsINode* node = nullptr;
106     if (acc.IsAccessible()) {
107       node = acc.AsAccessible()->GetNode();
108     } else {
109       // Use the XUL browser embedding this remote document.
110       auto browser = static_cast<mozilla::dom::BrowserParent*>(
111           acc.AsProxy()->Document()->Manager());
112       node = browser->GetOwnerElement();
113     }
114     MOZ_ASSERT(node);
115     nsIntPoint winCoords = nsCoreUtils::GetScreenCoordsForWindow(node);
116     aX += winCoords.x;
117     aY += winCoords.y;
118   }
119 
120   AccessibleOrProxy accAtPoint =
121       acc.ChildAtPoint(aX, aY, Accessible::eDeepestChild);
122   if (accAtPoint.IsNull()) {
123     return nullptr;
124   }
125   roles::Role role = accAtPoint.Role();
126   if (role == roles::TEXT_LEAF || role == roles::STATICTEXT) {
127     // We don't include text leaf nodes in the ATK tree, so return the parent.
128     accAtPoint = accAtPoint.Parent();
129     MOZ_ASSERT(!accAtPoint.IsNull(), "Text leaf should always have a parent");
130   }
131   AtkObject* atkObj = GetWrapperFor(accAtPoint);
132   if (atkObj) {
133     g_object_ref(atkObj);
134   }
135   return atkObj;
136 }
137 
getExtentsHelper(AtkObject * aAtkObj,gint * aX,gint * aY,gint * aWidth,gint * aHeight,AtkCoordType aCoordType)138 void getExtentsHelper(AtkObject* aAtkObj, gint* aX, gint* aY, gint* aWidth,
139                       gint* aHeight, AtkCoordType aCoordType) {
140   AccessibleWrap* accWrap = GetAccessibleWrap(aAtkObj);
141   *aX = *aY = *aWidth = *aHeight = -1;
142 
143   if (accWrap) {
144     if (accWrap->IsDefunct()) {
145       return;
146     }
147 
148     nsIntRect screenRect = accWrap->Bounds();
149     if (screenRect.IsEmpty()) return;
150 
151     if (aCoordType == ATK_XY_WINDOW) {
152       nsIntPoint winCoords =
153           nsCoreUtils::GetScreenCoordsForWindow(accWrap->GetNode());
154       screenRect.x -= winCoords.x;
155       screenRect.y -= winCoords.y;
156     }
157 
158     *aX = screenRect.x;
159     *aY = screenRect.y;
160     *aWidth = screenRect.width;
161     *aHeight = screenRect.height;
162     return;
163   }
164 
165   if (ProxyAccessible* proxy = GetProxy(aAtkObj)) {
166     proxy->Extents(aCoordType == ATK_XY_WINDOW, aX, aY, aWidth, aHeight);
167   }
168 }
169 
componentInterfaceInitCB(AtkComponentIface * aIface)170 void componentInterfaceInitCB(AtkComponentIface* aIface) {
171   NS_ASSERTION(aIface, "Invalid Interface");
172   if (MOZ_UNLIKELY(!aIface)) return;
173 
174   /*
175    * Use default implementation in atk for contains, get_position,
176    * and get_size
177    */
178   aIface->ref_accessible_at_point = refAccessibleAtPointCB;
179   aIface->get_extents = getExtentsCB;
180   aIface->grab_focus = grabFocusCB;
181   if (IsAtkVersionAtLeast(2, 30)) {
182     aIface->scroll_to = scrollToCB;
183     aIface->scroll_to_point = scrollToPointCB;
184   }
185 }
186