summaryrefslogtreecommitdiff
path: root/chromium/ui/accessibility/platform/ax_platform_relation_win.cc
blob: 165789772ad9cb87062394728400990662a4bad3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// 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_relation_win.h"

#include <wrl/client.h>

#include <algorithm>
#include <vector>

#include "base/lazy_instance.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/enum_variant.h"
#include "base/win/scoped_variant.h"
#include "third_party/iaccessible2/ia2_api_all.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/ax_action_data.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_text_utils.h"
#include "ui/accessibility/ax_tree_data.h"
#include "ui/accessibility/platform/ax_platform_node_delegate.h"
#include "ui/accessibility/platform/ax_unique_id.h"
#include "ui/base/win/atl_module.h"
#include "ui/gfx/geometry/rect_conversions.h"

const WCHAR* const IA2_RELATION_DETAILS = L"details";
const WCHAR* const IA2_RELATION_DETAILS_FOR = L"detailsFor";
const WCHAR* const IA2_RELATION_ERROR_MESSAGE = L"errorMessage";

namespace ui {

AXPlatformRelationWin::AXPlatformRelationWin() {
  win::CreateATLModuleIfNeeded();
}

AXPlatformRelationWin::~AXPlatformRelationWin() {}

base::string16 GetIA2RelationFromIntAttr(ax::mojom::IntAttribute attribute) {
  switch (attribute) {
    case ax::mojom::IntAttribute::kDetailsId:
      return IA2_RELATION_DETAILS;
    case ax::mojom::IntAttribute::kMemberOfId:
      return IA2_RELATION_MEMBER_OF;
    case ax::mojom::IntAttribute::kErrormessageId:
      return IA2_RELATION_ERROR_MESSAGE;
    default:
      break;
  }
  return base::string16();
}

base::string16 GetIA2RelationFromIntListAttr(
    ax::mojom::IntListAttribute attribute) {
  switch (attribute) {
    case ax::mojom::IntListAttribute::kControlsIds:
      return IA2_RELATION_CONTROLLER_FOR;
    case ax::mojom::IntListAttribute::kDescribedbyIds:
      return IA2_RELATION_DESCRIBED_BY;
    case ax::mojom::IntListAttribute::kFlowtoIds:
      return IA2_RELATION_FLOWS_TO;
    case ax::mojom::IntListAttribute::kLabelledbyIds:
      return IA2_RELATION_LABELLED_BY;
    default:
      break;
  }
  return base::string16();
}

base::string16 GetIA2ReverseRelationFromIntAttr(
    ax::mojom::IntAttribute attribute) {
  switch (attribute) {
    case ax::mojom::IntAttribute::kDetailsId:
      return IA2_RELATION_DETAILS_FOR;
    default:
      break;
  }
  return base::string16();
}

base::string16 GetIA2ReverseRelationFromIntListAttr(
    ax::mojom::IntListAttribute attribute) {
  switch (attribute) {
    case ax::mojom::IntListAttribute::kControlsIds:
      return IA2_RELATION_CONTROLLED_BY;
    case ax::mojom::IntListAttribute::kDescribedbyIds:
      return IA2_RELATION_DESCRIPTION_FOR;
    case ax::mojom::IntListAttribute::kFlowtoIds:
      return IA2_RELATION_FLOWS_FROM;
    case ax::mojom::IntListAttribute::kLabelledbyIds:
      return IA2_RELATION_LABEL_FOR;
    default:
      break;
  }
  return base::string16();
}

// static
int AXPlatformRelationWin::EnumerateRelationships(
    const AXNodeData& node_data,
    AXPlatformNodeDelegate* delegate,
    int desired_index,
    const base::string16& desired_ia2_relation,
    base::string16* out_ia2_relation,
    std::set<int32_t>* out_targets) {
  // The first time this is called, populate vectors with all of the
  // int attributes and intlist attributes that have reverse relations
  // we care about on Windows. Computing these by calling
  // GetIA2ReverseRelationFrom{Int|IntList}Attr on every possible attribute
  // simplifies the work needed to support an additional relation
  // attribute in the future.
  static std::vector<ax::mojom::IntAttribute>
      int_attributes_with_reverse_relations;
  static std::vector<ax::mojom::IntListAttribute>
      intlist_attributes_with_reverse_relations;
  static bool first_time = true;
  if (first_time) {
    for (int32_t attr_index =
             static_cast<int32_t>(ax::mojom::IntAttribute::kNone);
         attr_index <= static_cast<int32_t>(ax::mojom::IntAttribute::kMaxValue);
         ++attr_index) {
      auto attr = static_cast<ax::mojom::IntAttribute>(attr_index);
      if (!GetIA2ReverseRelationFromIntAttr(attr).empty())
        int_attributes_with_reverse_relations.push_back(attr);
    }
    for (int32_t attr_index =
             static_cast<int32_t>(ax::mojom::IntListAttribute::kNone);
         attr_index <=
         static_cast<int32_t>(ax::mojom::IntListAttribute::kMaxValue);
         ++attr_index) {
      auto attr = static_cast<ax::mojom::IntListAttribute>(attr_index);
      if (!GetIA2ReverseRelationFromIntListAttr(attr).empty())
        intlist_attributes_with_reverse_relations.push_back(attr);
    }
    first_time = false;
  }

  // Enumerate all of the relations and reverse relations that
  // are exposed via IAccessible2 on Windows. We do this with a series
  // of loops. Every time we encounter one, we check if the caller
  // requested that particular relation by index, and return it.
  // Otherwise we build up and return the total number of relations found.
  int total_count = 0;

  // Iterate over all int attributes on this node to check the ones
  // that correspond to IAccessible2 relations.
  for (size_t i = 0; i < node_data.int_attributes.size(); ++i) {
    ax::mojom::IntAttribute int_attribute = node_data.int_attributes[i].first;
    base::string16 relation = GetIA2RelationFromIntAttr(int_attribute);
    if (!relation.empty() &&
        (desired_ia2_relation.empty() || desired_ia2_relation == relation)) {
      // Skip reflexive relations
      if (node_data.int_attributes[i].second == node_data.id)
        continue;
      if (desired_index == total_count) {
        *out_ia2_relation = relation;
        out_targets->insert(node_data.int_attributes[i].second);
        return 1;
      }
      total_count++;
    }
  }

  // Iterate over all of the int attributes that have reverse relations
  // in IAccessible2, and query AXTree to see if the reverse relation exists.
  for (ax::mojom::IntAttribute int_attribute :
       int_attributes_with_reverse_relations) {
    base::string16 relation = GetIA2ReverseRelationFromIntAttr(int_attribute);
    std::set<int32_t> targets =
        delegate->GetReverseRelations(int_attribute, node_data.id);
    // Erase reflexive relations.
    targets.erase(node_data.id);
    if (targets.size()) {
      if (!relation.empty() &&
          (desired_ia2_relation.empty() || desired_ia2_relation == relation)) {
        if (desired_index == total_count) {
          *out_ia2_relation = relation;
          *out_targets = targets;
          return 1;
        }
        total_count++;
      }
    }
  }

  // Iterate over all intlist attributes on this node to check the ones
  // that correspond to IAccessible2 relations.
  for (size_t i = 0; i < node_data.intlist_attributes.size(); ++i) {
    ax::mojom::IntListAttribute intlist_attribute =
        node_data.intlist_attributes[i].first;
    base::string16 relation = GetIA2RelationFromIntListAttr(intlist_attribute);
    if (!relation.empty() &&
        (desired_ia2_relation.empty() || desired_ia2_relation == relation)) {
      if (desired_index == total_count) {
        *out_ia2_relation = relation;
        for (int32_t target_id : node_data.intlist_attributes[i].second) {
          // Skip reflexive relations
          if (target_id == node_data.id)
            continue;
          out_targets->insert(target_id);
        }
        if (out_targets->size() == 0)
          continue;
        return 1;
      }
      total_count++;
    }
  }

  // Iterate over all of the intlist attributes that have reverse relations
  // in IAccessible2, and query AXTree to see if the reverse relation exists.
  for (ax::mojom::IntListAttribute intlist_attribute :
       intlist_attributes_with_reverse_relations) {
    base::string16 relation =
        GetIA2ReverseRelationFromIntListAttr(intlist_attribute);
    std::set<int32_t> targets =
        delegate->GetReverseRelations(intlist_attribute, node_data.id);
    // Erase reflexive relations.
    targets.erase(node_data.id);
    if (targets.size()) {
      if (!relation.empty() &&
          (desired_ia2_relation.empty() || desired_ia2_relation == relation)) {
        if (desired_index == total_count) {
          *out_ia2_relation = relation;
          *out_targets = targets;
          return 1;
        }
        total_count++;
      }
    }
  }

  return total_count;
}

void AXPlatformRelationWin::Initialize(const base::string16& type) {
  type_ = type;
}

void AXPlatformRelationWin::Invalidate() {
  targets_.clear();
}

void AXPlatformRelationWin::AddTarget(AXPlatformNodeWin* target) {
  targets_.push_back(target);
}

IFACEMETHODIMP AXPlatformRelationWin::get_relationType(BSTR* relation_type) {
  if (!relation_type)
    return E_INVALIDARG;

  *relation_type = SysAllocString(type_.c_str());
  DCHECK(*relation_type);
  return S_OK;
}

IFACEMETHODIMP AXPlatformRelationWin::get_nTargets(LONG* n_targets) {
  if (!n_targets)
    return E_INVALIDARG;

  *n_targets = static_cast<LONG>(targets_.size());
  return S_OK;
}

IFACEMETHODIMP AXPlatformRelationWin::get_target(LONG target_index,
                                                 IUnknown** target) {
  if (!target)
    return E_INVALIDARG;

  if (target_index < 0 || target_index >= static_cast<LONG>(targets_.size())) {
    return E_INVALIDARG;
  }

  *target = static_cast<IAccessible*>(targets_[target_index].Get());
  (*target)->AddRef();
  return S_OK;
}

IFACEMETHODIMP AXPlatformRelationWin::get_targets(LONG max_targets,
                                                  IUnknown** targets,
                                                  LONG* n_targets) {
  if (!targets || !n_targets)
    return E_INVALIDARG;

  LONG count = static_cast<LONG>(targets_.size());
  if (count > max_targets)
    count = max_targets;

  *n_targets = count;
  if (count == 0)
    return S_FALSE;

  for (LONG i = 0; i < count; ++i) {
    HRESULT result = get_target(i, &targets[i]);
    if (result != S_OK)
      return result;
  }

  return S_OK;
}

IFACEMETHODIMP
AXPlatformRelationWin::get_localizedRelationType(BSTR* relation_type) {
  return E_NOTIMPL;
}

}  // namespace ui