File: automation_api_converters.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 5,121 bytes parent folder | download | duplicates (8)
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
// 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 "extensions/renderer/api/automation/automation_api_converters.h"
#include "base/no_destructor.h"
#include "ui/accessibility/ax_enum_util.h"
#include "ui/accessibility/ax_event_generator.h"

namespace extensions {

api::automation::MarkerType ConvertMarkerTypeFromAXToAutomation(
    ax::mojom::MarkerType ax) {
  switch (ax) {
    case ax::mojom::MarkerType::kNone:
      return api::automation::MarkerType::kNone;
    case ax::mojom::MarkerType::kSpelling:
      return api::automation::MarkerType::kSpelling;
    case ax::mojom::MarkerType::kGrammar:
      return api::automation::MarkerType::kGrammar;
    case ax::mojom::MarkerType::kTextMatch:
      return api::automation::MarkerType::kTextMatch;
    case ax::mojom::MarkerType::kActiveSuggestion:
      return api::automation::MarkerType::kActiveSuggestion;
    case ax::mojom::MarkerType::kSuggestion:
      return api::automation::MarkerType::kSuggestion;
    case ax::mojom::MarkerType::kHighlight:
      return api::automation::MarkerType::kHighlight;
  }
}

api::automation::TreeChangeType ConvertToAutomationTreeChangeType(
    ax::mojom::Mutation change_type) {
  switch (change_type) {
    case ax::mojom::Mutation::kNone:
      return api::automation::TreeChangeType::kNone;
    case ax::mojom::Mutation::kNodeCreated:
      return api::automation::TreeChangeType::kNodeCreated;
    case ax::mojom::Mutation::kSubtreeCreated:
      return api::automation::TreeChangeType::kSubtreeCreated;
    case ax::mojom::Mutation::kNodeChanged:
      return api::automation::TreeChangeType::kNodeChanged;
    case ax::mojom::Mutation::kTextChanged:
      return api::automation::TreeChangeType::kTextChanged;
    case ax::mojom::Mutation::kNodeRemoved:
      return api::automation::TreeChangeType::kNodeRemoved;
    case ax::mojom::Mutation::kSubtreeUpdateEnd:
      return api::automation::TreeChangeType::kSubtreeUpdateEnd;
  }
}

using AutomationFilter = api::automation::TreeChangeObserverFilter;
ui::TreeChangeObserverFilter ConvertAutomationTreeChangeObserverFilter(
    AutomationFilter filter) {
  switch (filter) {
    case AutomationFilter::kNone:
      return ui::TreeChangeObserverFilter::kNone;
    case AutomationFilter::kNoTreeChanges:
      return ui::TreeChangeObserverFilter::kNoTreeChanges;
    case AutomationFilter::kLiveRegionTreeChanges:
      return ui::TreeChangeObserverFilter::kLiveRegionTreeChanges;
    case AutomationFilter::kTextMarkerChanges:
      return ui::TreeChangeObserverFilter::kTextMarkerChanges;
    case AutomationFilter::kAllTreeChanges:
      return ui::TreeChangeObserverFilter::kAllTreeChanges;
  }
}

// Maps a key, a stringification of values in ui::AXEventGenerator::Event or
// ax::mojom::Event into a value, automation::api::EventType. The runtime
// invariant is that there should be exactly the same number of values in the
// map as is the size of api::automation::EventType.
api::automation::EventType AXEventToAutomationEventType(
    ax::mojom::Event event_type) {
  static base::NoDestructor<std::vector<api::automation::EventType>> enum_map;
  if (enum_map->empty()) {
    for (int i = static_cast<int>(ax::mojom::Event::kMinValue);
         i <= static_cast<int>(ax::mojom::Event::kMaxValue); i++) {
      auto ax_event_type = static_cast<ax::mojom::Event>(i);
      if (ui::ShouldIgnoreAXEventForAutomation(ax_event_type) ||
          ax_event_type == ax::mojom::Event::kNone) {
        enum_map->emplace_back(api::automation::EventType::kNone);
        continue;
      }

      const char* val = ui::ToString(ax_event_type);
      api::automation::EventType automation_event_type =
          api::automation::ParseEventType(val);
      if (automation_event_type == api::automation::EventType::kNone) {
        NOTREACHED() << "Missing mapping from ax::mojom::Event: " << val;
      }

      enum_map->emplace_back(automation_event_type);
    }
  }

  return (*enum_map)[static_cast<int>(event_type)];
}

api::automation::EventType AXGeneratedEventToAutomationEventType(
    ui::AXEventGenerator::Event event_type) {
  static base::NoDestructor<std::vector<api::automation::EventType>> enum_map;
  if (enum_map->empty()) {
    for (int i = 0;
         i <= static_cast<int>(ui::AXEventGenerator::Event::MAX_VALUE); i++) {
      auto ax_event_type = static_cast<ui::AXEventGenerator::Event>(i);
      if (ui::ShouldIgnoreGeneratedEventForAutomation(ax_event_type)) {
        enum_map->emplace_back(api::automation::EventType::kNone);
        continue;
      }

      const char* val = ui::ToString(ax_event_type);
      api::automation::EventType automation_event_type =
          api::automation::ParseEventType(val);
      if (automation_event_type == api::automation::EventType::kNone) {
        NOTREACHED() << "Missing mapping from ui::AXEventGenerator::Event: "
                     << val;
      }

      enum_map->emplace_back(automation_event_type);
    }
  }

  return (*enum_map)[static_cast<int>(event_type)];
}

}  // namespace extensions