Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/accessible/aom/AccessibleNode.cpp
Line
Count
Source (jump to first uncovered line)
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 "nsIPersistentProperties2.h"
11
#include "nsISimpleEnumerator.h"
12
13
#include "Accessible-inl.h"
14
#include "nsAccessibilityService.h"
15
#include "DocAccessible.h"
16
17
using namespace mozilla;
18
using namespace mozilla::a11y;
19
using namespace mozilla::dom;
20
21
bool
22
AccessibleNode::IsAOMEnabled(JSContext* aCx, JSObject* /*unused*/)
23
0
{
24
0
  static bool sPrefCached = false;
25
0
  static bool sPrefCacheValue = false;
26
0
27
0
  if (!sPrefCached) {
28
0
    sPrefCached = true;
29
0
    Preferences::AddBoolVarCache(&sPrefCacheValue, "accessibility.AOM.enabled");
30
0
  }
31
0
32
0
  return nsContentUtils::IsSystemCaller(aCx) || sPrefCacheValue;
33
0
}
34
35
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(AccessibleNode, mRelationProperties,
36
                                      mIntl, mDOMNode, mStates)
37
38
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AccessibleNode)
39
0
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
40
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
41
0
NS_INTERFACE_MAP_END
42
43
NS_IMPL_CYCLE_COLLECTING_ADDREF(AccessibleNode)
44
NS_IMPL_CYCLE_COLLECTING_RELEASE(AccessibleNode)
45
46
AccessibleNode::AccessibleNode(nsINode* aNode) :
47
    mDoubleProperties(3),
48
    mIntProperties(3),
49
    mUIntProperties(6),
50
    mBooleanProperties(0),
51
    mRelationProperties(3),
52
    mStringProperties(16),
53
    mDOMNode(aNode)
54
0
{
55
0
  nsAccessibilityService* accService = GetOrCreateAccService();
56
0
  if (!accService) {
57
0
    return;
58
0
  }
59
0
60
0
  DocAccessible* doc = accService->GetDocAccessible(mDOMNode->OwnerDoc());
61
0
  if (doc) {
62
0
    mIntl = doc->GetAccessible(mDOMNode);
63
0
  }
64
0
}
65
66
AccessibleNode::~AccessibleNode()
67
0
{
68
0
}
69
70
/* virtual */ JSObject*
71
AccessibleNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
72
0
{
73
0
  return AccessibleNode_Binding::Wrap(aCx, this, aGivenProto);
74
0
}
75
76
/* virtual */ ParentObject
77
AccessibleNode::GetParentObject() const
78
0
{
79
0
  return mDOMNode->GetParentObject();
80
0
}
81
82
void
83
AccessibleNode::GetComputedRole(nsAString& aRole)
84
0
{
85
0
  if (mIntl) {
86
0
    nsAccessibilityService* accService = GetOrCreateAccService();
87
0
    if (accService) {
88
0
      accService->GetStringRole(mIntl->Role(), aRole);
89
0
      return;
90
0
    }
91
0
  }
92
0
93
0
  aRole.AssignLiteral("unknown");
94
0
}
95
96
void
97
AccessibleNode::GetStates(nsTArray<nsString>& aStates)
98
0
{
99
0
  nsAccessibilityService* accService = GetOrCreateAccService();
100
0
  if (!mIntl || !accService) {
101
0
    aStates.AppendElement(NS_LITERAL_STRING("defunct"));
102
0
    return;
103
0
  }
104
0
105
0
  if (mStates) {
106
0
    aStates = mStates->StringArray();
107
0
    return;
108
0
  }
109
0
110
0
  mStates = accService->GetStringStates(mIntl->State());
111
0
  aStates = mStates->StringArray();
112
0
}
113
114
void
115
AccessibleNode::GetAttributes(nsTArray<nsString>& aAttributes)
116
0
{
117
0
  if (!mIntl) {
118
0
    return;
119
0
  }
120
0
121
0
  nsCOMPtr<nsIPersistentProperties> attrs = mIntl->Attributes();
122
0
123
0
  nsCOMPtr<nsISimpleEnumerator> props;
124
0
  attrs->Enumerate(getter_AddRefs(props));
125
0
126
0
  bool hasMore = false;
127
0
  while (NS_SUCCEEDED(props->HasMoreElements(&hasMore)) && hasMore) {
128
0
    nsCOMPtr<nsISupports> supp;
129
0
    props->GetNext(getter_AddRefs(supp));
130
0
131
0
    nsCOMPtr<nsIPropertyElement> prop(do_QueryInterface(supp));
132
0
133
0
    nsAutoCString attr;
134
0
    prop->GetKey(attr);
135
0
    aAttributes.AppendElement(NS_ConvertUTF8toUTF16(attr));
136
0
  }
137
0
}
138
139
bool
140
AccessibleNode::Is(const Sequence<nsString>& aFlavors)
141
0
{
142
0
  nsAccessibilityService* accService = GetOrCreateAccService();
143
0
  if (!mIntl || !accService) {
144
0
    for (const auto& flavor : aFlavors) {
145
0
      if (!flavor.EqualsLiteral("unknown") && !flavor.EqualsLiteral("defunct")) {
146
0
        return false;
147
0
      }
148
0
    }
149
0
    return true;
150
0
  }
151
0
152
0
  nsAutoString role;
153
0
  accService->GetStringRole(mIntl->Role(), role);
154
0
155
0
  if (!mStates) {
156
0
    mStates = accService->GetStringStates(mIntl->State());
157
0
  }
158
0
159
0
  for (const auto& flavor : aFlavors) {
160
0
    if (!flavor.Equals(role) && !mStates->Contains(flavor)) {
161
0
      return false;
162
0
    }
163
0
  }
164
0
  return true;
165
0
}
166
167
bool
168
AccessibleNode::Has(const Sequence<nsString>& aAttributes)
169
0
{
170
0
  if (!mIntl) {
171
0
    return false;
172
0
  }
173
0
  nsCOMPtr<nsIPersistentProperties> attrs = mIntl->Attributes();
174
0
  for (const auto& attr : aAttributes) {
175
0
    bool has = false;
176
0
    attrs->Has(NS_ConvertUTF16toUTF8(attr).get(), &has);
177
0
    if (!has) {
178
0
      return false;
179
0
    }
180
0
  }
181
0
  return true;
182
0
}
183
184
void
185
AccessibleNode::Get(JSContext* aCX, const nsAString& aAttribute,
186
                    JS::MutableHandle<JS::Value> aValue,
187
                    ErrorResult& aRv)
188
0
{
189
0
  if (!mIntl) {
190
0
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
191
0
    return;
192
0
  }
193
0
194
0
  nsCOMPtr<nsIPersistentProperties> attrs = mIntl->Attributes();
195
0
  nsAutoString value;
196
0
  attrs->GetStringProperty(NS_ConvertUTF16toUTF8(aAttribute), value);
197
0
198
0
  JS::Rooted<JS::Value> jsval(aCX);
199
0
  if (!ToJSValue(aCX, value, &jsval)) {
200
0
    aRv.Throw(NS_ERROR_UNEXPECTED);
201
0
    return;
202
0
  }
203
0
204
0
  aValue.set(jsval);
205
0
}
206
207
nsINode*
208
AccessibleNode::GetDOMNode()
209
0
{
210
0
  return mDOMNode;
211
0
}