File: ax_call_statement_invoker_win.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (518 lines) | stat: -rw-r--r-- 17,695 bytes parent folder | download | duplicates (5)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/accessibility/platform/inspect/ax_call_statement_invoker_win.h"

#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_bstr.h"
#include "ui/accessibility/platform/inspect/ax_inspect_utils_win.h"
#include "ui/accessibility/platform/inspect/ax_property_node.h"

#define DEFINE_IA2_QI_ENTRY(ia2_interface)                                    \
  if (interface_name == #ia2_interface) {                                     \
    Microsoft::WRL::ComPtr<ia2_interface> obj;                                \
    HRESULT hr = IA2QueryInterface<ia2_interface>(target.Get(), &obj);        \
    if (hr == S_OK)                                                           \
      return AXOptionalObject({std::move(obj)});                              \
    if (hr == E_NOINTERFACE)                                                  \
      return AXOptionalObject::Error(interface_name + " is not implemented"); \
    return AXOptionalObject::Error("Unexpected error when querying " +        \
                                   interface_name);                           \
  }

#define CHECK_ARGS_N(arg_count, property_node)                         \
  if (property_node.arguments.size() < arg_count) {                    \
    return AXOptionalObject::Error("too few arguments to function " +  \
                                   property_node.name_or_value);       \
  }                                                                    \
  if (property_node.arguments.size() > arg_count) {                    \
    return AXOptionalObject::Error("too many arguments to function " + \
                                   property_node.name_or_value);       \
  }

#define CHECK_ARGS_1(property_node) CHECK_ARGS_N(1, property_node)

namespace ui {

// static
std::string AXCallStatementInvokerWin::ToString(
    const AXOptionalObject& optional) {
  if (optional.HasValue()) {
    return optional->ToString();
  }

  if (optional.IsError()) {
    return "Error:\"" + optional.StateText() + "\"";
  }
  return optional.StateToString();
}

AXCallStatementInvokerWin::AXCallStatementInvokerWin(
    const AXTreeIndexerWin* indexer,
    std::map<std::string, Target>* storage)
    : indexer_(indexer), storage_(storage) {}

AXOptionalObject AXCallStatementInvokerWin::Invoke(
    const AXPropertyNode& property_node) const {
  // Executes a scripting statement coded in a given property node.
  // The statement represents a chainable sequence of attribute calls, where
  // each subsequent call is invoked on an object returned by a previous call.
  // For example, p.AXChildren[0].AXRole will unroll into a sequence of
  // `p.AXChildren`, `(p.AXChildren)[0]` and `((p.AXChildren)[0]).AXRole`.

  // Get an initial target to invoke an attribute for. First, check the storage
  // if it has an associated target for the property node, then query the tree
  // indexer if the property node refers to a DOM id or line index of
  // an accessible object. If the property node doesn't provide a target then
  // use the default one (if any, the default node is provided in case of
  // a tree dumping only, the scripts never have default target).
  Target target;

  // Case 1: try to get a target from the storage. The target may refer to
  // a variable which is kept in the storage.

  // For example,
  // `text_parent:= p.parent` will define `text_leaf` variable and put it
  // into the storage, and then the variable value will be extracted from
  // the storage for other instruction referring the variable, for example,
  // `text_parent.role`.
  if (storage_) {
    auto storage_iterator = storage_->find(property_node.name_or_value);
    if (storage_iterator != storage_->end()) {
      target = storage_iterator->second;

      if (!IsIAccessibleAndNotNull(target)) {
        LOG(ERROR) << "Windows invoker only supports IAccessible variable "
                      "assignments.";
        return AXOptionalObject::Error();
      }
    }
  }

  // Case 2: try to get target from the tree indexer. The target may refer to
  // an accessible element by DOM id or by a line number (:LINE_NUM format) in
  // a result accessible tree. The tree indexer keeps the mappings between
  // accessible elements and their DOM ids and line numbers.
  if (!target) {
    target = indexer_->NodeBy(property_node.name_or_value);
  }

  // Could not find the target.
  if (!IsIAccessibleAndNotNull(target)) {
    LOG(ERROR) << "Could not find target: " << property_node.name_or_value;
    return AXOptionalObject::Error();
  }

  auto* current_node = property_node.next.get();

  // Invoke the call chain.
  while (current_node) {
    auto target_optional = InvokeFor(target, *current_node);
    // Result of the current step is state. Don't go any further.
    if (!target_optional.HasValue())
      return target_optional;

    target = *target_optional;
    current_node = current_node->next.get();
  }

  // Variable case: store the variable value in the storage.
  if (!property_node.key.empty())
    (*storage_)[property_node.key] = target;

  return AXOptionalObject(target);
}

AXOptionalObject AXCallStatementInvokerWin::InvokeFor(
    const Target& target,
    const AXPropertyNode& property_node) const {
  if (target.Is<IAccessibleComPtr>())
    return InvokeForAXElement(target.As<IAccessibleComPtr>(), property_node);

  if (target.Is<IA2ComPtr>())
    return InvokeForIA2(target.As<IA2ComPtr>(), property_node);

  if (target.Is<IA2HypertextComPtr>())
    return InvokeForIA2Hypertext(target.As<IA2HypertextComPtr>(),
                                 property_node);

  if (target.Is<IA2TableComPtr>())
    return InvokeForIA2Table(target.As<IA2TableComPtr>(), property_node);

  if (target.Is<IA2TableCellComPtr>())
    return InvokeForIA2TableCell(target.As<IA2TableCellComPtr>(),
                                 property_node);

  if (target.Is<IA2TextComPtr>())
    return InvokeForIA2Text(target.As<IA2TextComPtr>(), property_node);

  if (target.Is<IA2TextSelectionContainerComPtr>()) {
    return InvokeForIA2TextSelectionContainer(
        target.As<IA2TextSelectionContainerComPtr>(), property_node);
  }

  if (target.Is<IA2ValueComPtr>())
    return InvokeForIA2Value(target.As<IA2ValueComPtr>(), property_node);

  LOG(ERROR) << "Unexpected target type for " << property_node.ToFlatString();
  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::InvokeForAXElement(
    const IAccessibleComPtr& target,
    const AXPropertyNode& property_node) const {
  if (property_node.name_or_value == "role") {
    return GetRole(target);
  }

  if (property_node.name_or_value == "name") {
    return GetName(target);
  }

  if (property_node.name_or_value == "description") {
    return GetDescription(target);
  }

  if (property_node.name_or_value == "QueryInterface") {
    if (!property_node.arguments.size()) {
      LOG(ERROR) << "Error: " << property_node.name_or_value
                 << "called without argument";
      return AXOptionalObject::Error();
    }
    std::string interface_name = property_node.arguments[0].name_or_value;
    return QueryInterface(target, interface_name);
  }

  if (property_node.name_or_value == "hasState") {
    if (!property_node.arguments.size()) {
      LOG(ERROR) << "Error: " << property_node.name_or_value
                 << "called without argument";
      return AXOptionalObject::Error();
    }
    std::string state = property_node.arguments[0].name_or_value;
    return HasState(target, state);
  }

  // Todo: add support for
  // - [ ] test.accSelection
  // - [ ] test.get_accSelection
  // - [ ] test.hasRelation(<relation>)

  LOG(ERROR) << "Error in '" << property_node.name_or_value
             << "' called on AXElement in '" << property_node.ToFlatString()
             << "' statement";
  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::InvokeForIA2(
    const IA2ComPtr& target,
    const AXPropertyNode& property_node) const {
  if (property_node.name_or_value == "role")
    return GetIA2Role(target);

  if (property_node.name_or_value == "getAttribute") {
    return GetIA2Attribute(target, property_node);
  }

  if (property_node.name_or_value == "hasState") {
    return HasIA2State(target, property_node);
  }

  // Todo: add support for
  // - [ ] test.getInterface(IAccessible2).get_groupPosition
  // - [ ] test.getInterface(IAccessible2).get_localizedExtendedRole

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::InvokeForIA2Hypertext(
    const IA2HypertextComPtr& target,
    const AXPropertyNode& property_node) const {
  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::InvokeForIA2Table(
    const IA2TableComPtr& target,
    const AXPropertyNode& property_node) const {
  if (property_node.name_or_value == "selectedColumns") {
    return GetSelectedColumns(target);
  }

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::InvokeForIA2TableCell(
    const IA2TableCellComPtr& target,
    const AXPropertyNode& property_node) const {
  return AXOptionalObject::Error();

  // Todo: add support for
  // - [ ] test.getInterface(IAccessibleTableCell).get_rowIndex
  // - [ ] test.getInterface(IAccessibleTableCell).get_columnIndex
  // - [ ] test.getInterface(IAccessibleTableCell).get_rowExtent
  // - [ ] test.getInterface(IAccessibleTableCell).get_columnExtent
}

AXOptionalObject AXCallStatementInvokerWin::InvokeForIA2TextSelectionContainer(
    const IA2TextSelectionContainerComPtr& target,
    const AXPropertyNode& property_node) const {
  if (property_node.name_or_value == "selections") {
    return GetSelections(target);
  }

  if (property_node.name_or_value == "setSelections") {
    return SetSelections(target, property_node);
  }

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::InvokeForIA2Text(
    const IA2TextComPtr& target,
    const AXPropertyNode& property_node) const {
  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::InvokeForIA2Value(
    const IA2ValueComPtr& target,
    const AXPropertyNode& property_node) const {
  return AXOptionalObject::Error();

  // Todo: add support for
  // - [ ] test.getInterface(IAccessibleValue).get_currentValue
  // - [ ] test.getInterface(IAccessibleValue).get_minimumValue
  // - [ ] test.getInterface(IAccessibleValue).get_maximumValue
}

AXOptionalObject AXCallStatementInvokerWin::GetRole(
    const IAccessibleComPtr& target) const {
  base::win::ScopedVariant variant_self(CHILDID_SELF);
  base::win::ScopedVariant ia_role_variant;
  if (SUCCEEDED(target->get_accRole(variant_self, ia_role_variant.Receive()))) {
    return AXOptionalObject(Target(RoleVariantToString(ia_role_variant)));
  }

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::GetName(
    const IAccessibleComPtr& target) const {
  base::win::ScopedVariant variant_self(CHILDID_SELF);
  base::win::ScopedBstr name;
  auto result = target->get_accName(variant_self, name.Receive());
  if (result == S_OK)
    return AXOptionalObject(Target(base::WideToUTF8(name.Get())));
  if (result == S_FALSE)
    return AXOptionalObject(Target(std::string()));

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::GetDescription(
    const IAccessibleComPtr& target) const {
  base::win::ScopedVariant variant_self(CHILDID_SELF);
  base::win::ScopedBstr desc;
  auto result = target->get_accDescription(variant_self, desc.Receive());
  if (result == S_OK)
    return AXOptionalObject(Target(base::WideToUTF8(desc.Get())));
  if (result == S_FALSE)
    return AXOptionalObject(Target(std::string()));

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::HasState(
    const IAccessibleComPtr& target,
    std::string state) const {
  base::win::ScopedVariant variant_self(CHILDID_SELF);
  int32_t ia_state = 0;
  base::win::ScopedVariant ia_state_variant;
  if (target->get_accState(variant_self, ia_state_variant.Receive()) == S_OK &&
      ia_state_variant.type() == VT_I4) {
    std::vector<std::wstring> state_strings;
    ia_state = ia_state_variant.ptr()->intVal;
    IAccessibleStateToStringVector(ia_state, &state_strings);

    for (const auto& str : state_strings) {
      if (base::WideToUTF8(str) == state)
        return AXOptionalObject(Target(true));
    }
    return AXOptionalObject(Target(false));
  }

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::QueryInterface(
    const IAccessibleComPtr& target,
    std::string interface_name) const {
  DEFINE_IA2_QI_ENTRY(IAccessible2)
  DEFINE_IA2_QI_ENTRY(IAccessibleHypertext)
  DEFINE_IA2_QI_ENTRY(IAccessibleTable)
  DEFINE_IA2_QI_ENTRY(IAccessibleTableCell)
  DEFINE_IA2_QI_ENTRY(IAccessibleTextSelectionContainer)
  DEFINE_IA2_QI_ENTRY(IAccessibleText)
  DEFINE_IA2_QI_ENTRY(IAccessibleValue)

  return AXOptionalObject::Error("Unsupported " + interface_name +
                                 " interface");
}
#undef DEFINE_IA2_QI_ENTRY

AXOptionalObject AXCallStatementInvokerWin::GetIA2Role(
    const IA2ComPtr& target) const {
  LONG role = 0;
  if (SUCCEEDED(target->role(&role)))
    return AXOptionalObject(
        Target(base::WideToUTF8(IAccessible2RoleToString(role))));

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::GetIA2Attribute(
    const IA2ComPtr& target,
    const AXPropertyNode& property_node) const {
  CHECK_ARGS_1(property_node)

  std::string attribute = property_node.arguments[0].name_or_value;
  std::optional<std::string> value =
      GetIAccessible2Attribute(target, attribute);
  if (value)
    return AXOptionalObject(Target(*value));
  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::HasIA2State(
    const IA2ComPtr& target,
    const AXPropertyNode& property_node) const {
  CHECK_ARGS_1(property_node)
  std::string state = property_node.arguments[0].name_or_value;

  AccessibleStates states;
  if (target->get_states(&states) == S_OK) {
    std::vector<std::wstring> state_strings;
    IAccessible2StateToStringVector(states, &state_strings);

    for (const auto& str : state_strings) {
      if (base::WideToUTF8(str) == state)
        return AXOptionalObject(Target(true));
    }
    return AXOptionalObject(Target(false));
  }

  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::GetSelectedColumns(
    const IA2TableComPtr& target) const {
  ScopedCoMemArray<LONG> columns;
  if (target->get_selectedColumns(INT_MAX, columns.Receive(),
                                  columns.ReceiveSize()) == S_OK) {
    return AXOptionalObject({std::move(columns)});
  }
  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::GetSelections(
    const IA2TextSelectionContainerComPtr& target) const {
  ScopedCoMemArray<IA2TextSelection> selections;
  if (target->get_selections(selections.Receive(), selections.ReceiveSize()) ==
      S_OK) {
    return AXOptionalObject({std::move(selections)});
  }
  return AXOptionalObject::Error();
}

AXOptionalObject AXCallStatementInvokerWin::SetSelections(
    const IA2TextSelectionContainerComPtr& target,
    const AXPropertyNode& property_node) const {
  CHECK_ARGS_1(property_node)

  ScopedCoMemArray<IA2TextSelection> selections =
      PropertyNodeToIA2TextSelectionArray(property_node.arguments[0]);

  if (target->setSelections(selections.size(), selections.data()) == S_OK) {
    return AXOptionalObject({target});
  }

  return AXOptionalObject::Error();
}
#undef CHECK_ARGS_1
#undef CHECK_ARGS_N

bool AXCallStatementInvokerWin::IsIAccessibleAndNotNull(
    const Target& target) const {
  return target.Is<IAccessibleComPtr>() &&
         target.As<IAccessibleComPtr>().Get() != nullptr;
}

std::optional<IA2TextSelection>
AXCallStatementInvokerWin::PropertyNodeToIA2TextSelection(
    const AXPropertyNode& node) const {
  if (!node.IsDict()) {
    return std::nullopt;
  }

  const AXPropertyNode* start_obj_node = node.FindKey("startObj");
  if (!start_obj_node) {
    return std::nullopt;
  }

  IA2TextComPtr start_obj =
      PropertyNodeToIAccessible<IAccessibleText>(*start_obj_node);
  if (!start_obj) {
    return std::nullopt;
  }

  std::optional<int> start_offset = node.FindIntKey("startOffset");
  if (!start_offset) {
    return std::nullopt;
  }

  const AXPropertyNode* end_obj_node = node.FindKey("endObj");
  if (!end_obj_node) {
    return std::nullopt;
  }

  IA2TextComPtr end_obj =
      PropertyNodeToIAccessible<IAccessibleText>(*end_obj_node);
  if (!end_obj) {
    return std::nullopt;
  }

  std::optional<int> end_offset = node.FindIntKey("endOffset");
  if (!end_offset) {
    return std::nullopt;
  }

  IA2TextSelection text_selection{
      start_obj.Detach(),
      *start_offset,
      end_obj.Detach(),
      *end_offset,
  };
  return {std::move(text_selection)};
}

ScopedCoMemArray<IA2TextSelection>
AXCallStatementInvokerWin::PropertyNodeToIA2TextSelectionArray(
    const AXPropertyNode& node) const {
  if (!node.IsArray()) {
    return {};
  }

  std::vector<IA2TextSelection> array;
  for (const auto& item_node : node.arguments) {
    std::optional<IA2TextSelection> item =
        PropertyNodeToIA2TextSelection(item_node);
    if (!item) {
      return {};
    }
    array.push_back(std::move(*item));
  }
  return ScopedCoMemArray<IA2TextSelection>(std::move(array));
}

}  // namespace ui