Skip to content

Commit

Permalink
ax_dump_tree tool: add --pid command line argument on Mac/Linux to du…
Browse files Browse the repository at this point in the history
…mp accessible tree by process id

Bug: None
Change-Id: Ie374dfb692757f1106d81cfbde652ac31e8c9db9
AX-Relnotes: n/a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2410121
Commit-Queue: Alexander Surkov <asurkov@igalia.com>
Reviewed-by: Dominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807569}
  • Loading branch information
asurkov authored and Commit Bot committed Sep 16, 2020
1 parent 2a8af7e commit a0e9446
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
10 changes: 7 additions & 3 deletions tools/accessibility/inspect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ This tool helps to inspect accessibility trees of applications. Trees are dumped

### Run

To dump accessibility tree of application, run
On Windows run
`ax_dump_tree --window=id`
where `id` is HWND on Windows, PID on Linux and Mac.
where `id` is HWND of a window to dump accessible tree for.

On Mac and Linux, run
`ax_dump_tree --pid=id`
where `pid` is process id of an application to dump accessible tree for.

Alternatively, you can indicate an application by its title:
`ax_dump_tree --pattern=title`

Notes:
* To use a hex window handle prefix it with `0x`.
* For json output, use the `--json` option
* To filter certain properties, use `--filters=[path-to-filters.txt]` where the filters text file has a series of `@ALLOW` and/or `@DENY` lines. See example-tree-filters.txt in tools/accessibility/inspect.
* To filter certain properties, use `--filters=[absolute-path-to-filters.txt]` where the filters text file has a series of `@ALLOW` and/or `@DENY` lines. See example-tree-filters.txt in tools/accessibility/inspect.
* [Mac] You have to turn on Accessibility for Terminal in Security & Privacy System Preferences.

## Convenience PowerShell scripts
Expand Down
65 changes: 46 additions & 19 deletions tools/accessibility/inspect/ax_dump_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
#include "build/build_config.h"
#include "tools/accessibility/inspect/ax_tree_server.h"

char kWindowSwitch[] = "window";
char kIdSwitch[] =
#if defined(WINDOWS)
"window";
#else
"pid";
#endif
char kPatternSwitch[] = "pattern";
char kFiltersSwitch[] = "filters";
char kJsonSwitch[] = "json";
char kHelpSwitch[] = "help";

// Convert from string to int, whether in 0x hex format or decimal format.
bool StringToInt(std::string str, unsigned* result) {
Expand Down Expand Up @@ -44,46 +50,67 @@ gfx::AcceleratedWidget CastToAcceleratedWidget(unsigned window_id) {
#endif
}

void PrintHelp() {
printf(
"ax_dump_tree is a tool designed to dump platform accessible trees "
"of running applications.\n");
printf("\nusage: ax_dump_tree <options>\n");
printf("options:\n");
#if defined(WINDOWS)
printf(" --window\tHWND of a window to dump accessible tree for\n");
#else
printf(
" --pid\t\tprocess id of an application to dump accessible tree for\n");
#endif
printf(" --pattern\ttitle of an application to dump accessible tree for\n");
printf(
" --filters\tfile containing property filters used to filter out\n"
" \t\taccessible tree, see example-tree-filters.txt as an example\n");
printf(" --json\toutputs tree in JSON format\n");
}

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;
}

base::FilePath filters_path =
base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
kFiltersSwitch);

bool use_json =
base::CommandLine::ForCurrentProcess()->HasSwitch(kJsonSwitch);

std::string window_str =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
kWindowSwitch);
if (!window_str.empty()) {
unsigned window_id;
if (!StringToInt(window_str, &window_id)) {
command_line->GetSwitchValuePath(kFiltersSwitch);

bool use_json = command_line->HasSwitch(kJsonSwitch);

std::string id_str = command_line->GetSwitchValueASCII(kIdSwitch);
if (!id_str.empty()) {
unsigned hwnd_or_pid;
if (!StringToInt(id_str, &hwnd_or_pid)) {
LOG(ERROR) << "* Error: Could not convert window id string to integer.";
return 1;
}
gfx::AcceleratedWidget widget(CastToAcceleratedWidget(window_id));
gfx::AcceleratedWidget widget(CastToAcceleratedWidget(hwnd_or_pid));

std::unique_ptr<content::AXTreeServer> server(
new content::AXTreeServer(widget, filters_path, use_json));
return 0;
}

std::string pattern_str =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
kPatternSwitch);
std::string pattern_str = command_line->GetSwitchValueASCII(kPatternSwitch);
if (!pattern_str.empty()) {
std::unique_ptr<content::AXTreeServer> server(
new content::AXTreeServer(pattern_str, filters_path, use_json));
return 0;
}

LOG(ERROR) << "* Error: Neither window handle (--window=[window-handle]) "
"nor pattern (--pattern=[pattern]) provided.";
LOG(ERROR)
<< "* Error: no accessible tree was identified to dump. Run with --help "
"for help.";
return 1;
}

0 comments on commit a0e9446

Please sign in to comment.