File: ax_dump_tree.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (125 lines) | stat: -rw-r--r-- 3,992 bytes parent folder | download | duplicates (4)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>

#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "content/public/browser/ax_inspect_factory.h"
#include "tools/accessibility/inspect/ax_tree_server.h"
#include "tools/accessibility/inspect/ax_utils.h"
#include "ui/accessibility/platform/inspect/ax_inspect.h"
#include "ui/accessibility/platform/inspect/ax_inspect_scenario.h"

constexpr char kApiSwitch[] = "api";
constexpr char kHelpSwitch[] = "help";

bool AXDumpTreeLogMessageHandler(int severity,
                                 const char* file,
                                 int line,
                                 size_t message_start,
                                 const std::string& str) {
  printf("%s", str.substr(message_start).c_str());
  return true;
}

// SupportedApis is a wrapper around content::AXInspectFactory::SupportedApis
// to filter out the Blink formatter option, as ax_dump_tree does not support
// outputting the chromium internal Blink tree. In the future we should support
// outputting the Blink tree when dumping chromium or chrome.
std::vector<ui::AXApiType::Type> SupportedApis() {
  std::vector<ui::AXApiType::Type> apis =
      content::AXInspectFactory::SupportedApis();
  std::vector<ui::AXApiType::Type> filter_apis;
  std::ranges::copy_if(
      apis, std::back_inserter(filter_apis),
      [](ui::AXApiType::Type t) { return t != ui::AXApiType::kBlink; });
  return filter_apis;
}

void PrintHelp() {
  std::vector<ui::AXApiType::Type> v = SupportedApis();
  std::string supported_apis = std::accumulate(
      begin(v), end(v), std::string{},
      [](std::string r, ui::AXApiType::Type p) {
        std::string comma;
        if (!r.empty()) {
          comma = ", ";
        }
        return std::move(r) + comma + static_cast<std::string>(p);
      });

  printf(
      "ax_dump_tree is a tool designed to dump platform accessible trees "
      "of running applications.\n");
  printf("\nusage: ax_dump_tree <options>\n");
  tools::PrintHelpShared();
  printf(
      "  --api\t\tAccessibility API for the current platform.\n"
      "  \t\tValid options are: %s\n",
      supported_apis.c_str());
}

int main(int argc, char** argv) {
  logging::SetLogMessageHandler(AXDumpTreeLogMessageHandler);

  base::AtExitManager at_exit_manager;

  base::CommandLine::Init(argc, argv);
  const base::CommandLine* command_line =
      base::CommandLine::ForCurrentProcess();

  if (command_line->HasSwitch(kHelpSwitch)) {
    PrintHelp();
    return 0;
  }

  std::optional<ui::AXTreeSelector> selector =
      tools::TreeSelectorFromCommandLine(*command_line);

  if (!selector || selector->empty()) {
    LOG(ERROR)
        << "Error: no accessible tree to dump. Run with --help for help.";
    return 1;
  }

  std::string api_str = command_line->GetSwitchValueASCII(kApiSwitch);
  ui::AXApiType::Type api = ui::AXApiType::kNone;
  std::vector<ui::AXApiType::Type> apis = SupportedApis();
  if (!api_str.empty()) {
    api = ui::AXApiType::From(api_str);
    if (api == ui::AXApiType::kNone) {
      LOG(ERROR) << "Unknown API type: " << api_str;
      return 1;
    }
    if (!base::Contains(apis, api)) {
      LOG(ERROR) << "Unsupported API for this platform: "
                 << static_cast<std::string>(api);
      return 1;
    }
  }
  // Choose the default API if one is not specified.
  if (api == ui::AXApiType::kNone && !apis.empty())
    api = apis[0];

  std::optional<ui::AXInspectScenario> scenario =
      tools::ScenarioFromCommandLine(*command_line, api);
  if (!scenario) {
    return 1;
  }

  auto server =
      std::make_unique<content::AXTreeServer>(*selector, *scenario, api);

  if (server->error) {
    return 1;
  }
  return 0;
}