1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "AccessibleNode.h"
7 #include "mozilla/dom/AccessibleNodeBinding.h"
8 #include "mozilla/dom/BindingDeclarations.h"
9 #include "mozilla/dom/DOMStringList.h"
10 #include "mozilla/StaticPrefs_accessibility.h"
11 #include "nsContentUtils.h"
12 #include "nsISimpleEnumerator.h"
13 
14 #include "AccAttributes.h"
15 #include "LocalAccessible-inl.h"
16 #include "nsAccessibilityService.h"
17 #include "DocAccessible.h"
18 
19 #include "mozilla/dom/Document.h"  // for inline nsINode::GetParentObject
20 #include "mozilla/dom/ToJSValue.h"
21 
22 using namespace mozilla;
23 using namespace mozilla::a11y;
24 using namespace mozilla::dom;
25 
IsAOMEnabled(JSContext * aCx,JSObject *)26 bool AccessibleNode::IsAOMEnabled(JSContext* aCx, JSObject* /*unused*/) {
27   return nsContentUtils::IsSystemCaller(aCx) ||
28          StaticPrefs::accessibility_AOM_enabled();
29 }
30 
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(AccessibleNode,mRelationProperties,mIntl,mDOMNode,mStates)31 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(AccessibleNode, mRelationProperties,
32                                       mIntl, mDOMNode, mStates)
33 
34 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AccessibleNode)
35   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
36   NS_INTERFACE_MAP_ENTRY(nsISupports)
37 NS_INTERFACE_MAP_END
38 
39 NS_IMPL_CYCLE_COLLECTING_ADDREF(AccessibleNode)
40 NS_IMPL_CYCLE_COLLECTING_RELEASE(AccessibleNode)
41 
42 AccessibleNode::AccessibleNode(nsINode* aNode)
43     : mDoubleProperties(3),
44       mIntProperties(3),
45       mUIntProperties(6),
46       mBooleanProperties(0),
47       mRelationProperties(3),
48       mStringProperties(16),
49       mDOMNode(aNode) {
50   nsAccessibilityService* accService = GetOrCreateAccService();
51   if (!accService) {
52     return;
53   }
54 
55   DocAccessible* doc = accService->GetDocAccessible(mDOMNode->OwnerDoc());
56   if (doc) {
57     mIntl = doc->GetAccessible(mDOMNode);
58   }
59 }
60 
~AccessibleNode()61 AccessibleNode::~AccessibleNode() {}
62 
63 /* virtual */
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)64 JSObject* AccessibleNode::WrapObject(JSContext* aCx,
65                                      JS::Handle<JSObject*> aGivenProto) {
66   return AccessibleNode_Binding::Wrap(aCx, this, aGivenProto);
67 }
68 
69 /* virtual */
GetParentObject() const70 ParentObject AccessibleNode::GetParentObject() const {
71   return mDOMNode->GetParentObject();
72 }
73 
GetComputedRole(nsAString & aRole)74 void AccessibleNode::GetComputedRole(nsAString& aRole) {
75   if (mIntl) {
76     nsAccessibilityService* accService = GetOrCreateAccService();
77     if (accService) {
78       accService->GetStringRole(mIntl->Role(), aRole);
79       return;
80     }
81   }
82 
83   aRole.AssignLiteral("unknown");
84 }
85 
GetStates(nsTArray<nsString> & aStates)86 void AccessibleNode::GetStates(nsTArray<nsString>& aStates) {
87   nsAccessibilityService* accService = GetOrCreateAccService();
88   if (!mIntl || !accService) {
89     aStates.AppendElement(u"defunct"_ns);
90     return;
91   }
92 
93   if (mStates) {
94     aStates = mStates->StringArray().Clone();
95     return;
96   }
97 
98   mStates = accService->GetStringStates(mIntl->State());
99   aStates = mStates->StringArray().Clone();
100 }
101 
GetAttributes(nsTArray<nsString> & aAttributes)102 void AccessibleNode::GetAttributes(nsTArray<nsString>& aAttributes) {
103   if (!mIntl) {
104     return;
105   }
106 
107   RefPtr<AccAttributes> attrs = mIntl->Attributes();
108 
109   for (auto iter : *attrs) {
110     aAttributes.AppendElement(nsAtomString(iter.Name()));
111   }
112 }
113 
Is(const Sequence<nsString> & aFlavors)114 bool AccessibleNode::Is(const Sequence<nsString>& aFlavors) {
115   nsAccessibilityService* accService = GetOrCreateAccService();
116   if (!mIntl || !accService) {
117     for (const auto& flavor : aFlavors) {
118       if (!flavor.EqualsLiteral("unknown") &&
119           !flavor.EqualsLiteral("defunct")) {
120         return false;
121       }
122     }
123     return true;
124   }
125 
126   nsAutoString role;
127   accService->GetStringRole(mIntl->Role(), role);
128 
129   if (!mStates) {
130     mStates = accService->GetStringStates(mIntl->State());
131   }
132 
133   for (const auto& flavor : aFlavors) {
134     if (!flavor.Equals(role) && !mStates->Contains(flavor)) {
135       return false;
136     }
137   }
138   return true;
139 }
140 
Has(const Sequence<nsString> & aAttributes)141 bool AccessibleNode::Has(const Sequence<nsString>& aAttributes) {
142   if (!mIntl) {
143     return false;
144   }
145   RefPtr<AccAttributes> attrs = mIntl->Attributes();
146   for (const auto& attr : aAttributes) {
147     RefPtr<nsAtom> attrAtom = NS_Atomize(attr);
148     if (!attrs->HasAttribute(attrAtom)) {
149       return false;
150     }
151   }
152   return true;
153 }
154 
Get(JSContext * aCX,const nsAString & aAttribute,JS::MutableHandle<JS::Value> aValue,ErrorResult & aRv)155 void AccessibleNode::Get(JSContext* aCX, const nsAString& aAttribute,
156                          JS::MutableHandle<JS::Value> aValue,
157                          ErrorResult& aRv) {
158   if (!mIntl) {
159     aRv.ThrowInvalidStateError("No attributes available");
160     return;
161   }
162 
163   RefPtr<nsAtom> attrAtom = NS_Atomize(aAttribute);
164   RefPtr<AccAttributes> attrs = mIntl->Attributes();
165   nsAutoString valueStr;
166   attrs->GetAttribute(attrAtom, valueStr);
167   if (!ToJSValue(aCX, valueStr, aValue)) {
168     aRv.NoteJSContextException(aCX);
169     return;
170   }
171 }
172 
GetDOMNode()173 nsINode* AccessibleNode::GetDOMNode() { return mDOMNode; }
174