File: browser_accessibility_state_impl_auralinux.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (183 lines) | stat: -rw-r--r-- 6,361 bytes parent folder | download | duplicates (2)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

#include "base/metrics/histogram_macros.h"
#include "base/task/thread_pool.h"
#include "content/browser/accessibility/browser_accessibility_state_impl.h"
#include "content/public/browser/scoped_accessibility_mode.h"
#include "third_party/re2/src/re2/re2.h"

namespace content {

namespace {

// Custom deleter function for DIR* that calls closedir
void CloseDir(DIR* dir) {
  if (dir) {
    closedir(dir);
  }
}

bool CheckCmdlineForOrca(const std::string& cmdline_all) {
  std::string cmdline;
  std::stringstream ss(cmdline_all);
  while (std::getline(ss, cmdline, '\0')) {
    re2::RE2 orca_regex(R"((^|/)(usr/)?bin/orca(\s|$))");
    if (re2::RE2::PartialMatch(cmdline, orca_regex)) {
      return true;  // Orca was found
    }
  }
  return false;  // Orca was not found
}

// Returns true if Orca is active.
bool DiscoverOrca() {
  // NOTE: this method is run from another thread to reduce jank, since
  // there's no guarantee these system calls will return quickly.
  std::unique_ptr<DIR, decltype(&CloseDir)> proc_dir(opendir("/proc"),
                                                     &CloseDir);
  if (proc_dir == nullptr) {
    LOG(ERROR) << "Error opening /proc directory.";
    return false;
  }

  bool is_orca_active = false;
  raw_ptr<dirent> entry;
  while (!is_orca_active && (entry = readdir(proc_dir.get())) != nullptr) {
    if (isdigit(entry->d_name[0])) {
      std::string pidStr(entry->d_name);

      struct stat stat_buf;
      std::string stat_path = "/proc/" + pidStr;
      if (stat(stat_path.c_str(), &stat_buf) == 0) {
        if (stat_buf.st_uid == getuid()) {
          std::ifstream cmdline_file("/proc/" + pidStr + "/cmdline");
          if (cmdline_file.is_open()) {
            std::stringstream buffer;
            buffer << cmdline_file.rdbuf();
            std::string cmdline_all = buffer.str();
            is_orca_active = CheckCmdlineForOrca(cmdline_all);
            cmdline_file.close();
          } else {
            DVLOG(1) << "Error opening cmdline for pid: " << pidStr;
          }
        }
      } else {
        DVLOG(1) << "Error with stat for pid: " << pidStr;
      }
    }
  }

  return is_orca_active;
}

}  // namespace

class BrowserAccessibilityStateImplAuralinux
    : public BrowserAccessibilityStateImpl {
 public:
  BrowserAccessibilityStateImplAuralinux() = default;

  // BrowserAccessibilityStateImpl:
  void RefreshAssistiveTech() override;
  void RefreshAssistiveTechIfNecessary(ui::AXMode new_mode) override;

 private:
  void OnDiscoveredOrca(bool is_orca_active);

  // A ScopedAccessibilityMode that holds AXMode::kScreenReader when
  // an active screen reader has been detected.
  std::unique_ptr<ScopedAccessibilityMode> screen_reader_mode_;

  // The presence of an AssistiveTech is currently being recomputed.
  // Will be updated via DiscoverOrca().
  bool awaiting_known_assistive_tech_computation_ = false;
};

void BrowserAccessibilityStateImplAuralinux::RefreshAssistiveTech() {
  if (!awaiting_known_assistive_tech_computation_) {
    awaiting_known_assistive_tech_computation_ = true;
    // Using base::Unretained() instead of a weak pointer as the lifetime of
    // this is tied to BrowserMainLoop.
    base::ThreadPool::PostTaskAndReplyWithResult(
        FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
        base::BindOnce(&DiscoverOrca),
        base::BindOnce(
            &BrowserAccessibilityStateImplAuralinux::OnDiscoveredOrca,
            base::Unretained(this)));
  }
}

void BrowserAccessibilityStateImplAuralinux::RefreshAssistiveTechIfNecessary(
    ui::AXMode new_mode) {
  bool was_screen_reader_active = ax_platform().IsScreenReaderActive();
  bool has_screen_reader_mode = new_mode.has_mode(ui::AXMode::kScreenReader);
  if (was_screen_reader_active != has_screen_reader_mode) {
    OnAssistiveTechFound(has_screen_reader_mode
                             ? ui::AssistiveTech::kGenericScreenReader
                             : ui::AssistiveTech::kNone);
    return;
  }

  // An expensive check is required to determine which type of assistive tech is
  // in use. Make this check only when `kExtendedProperties` is added or removed
  // from the process-wide mode flags and no previous assistive tech has been
  // discovered (in the former case) or one had been discovered (in the latter
  // case). `kScreenReader` will be added/removed from the process-wide mode
  // flags on completion and `OnAssistiveTechFound()` will be called with the
  // results of the check.
  bool has_extended_properties =
      new_mode.has_mode(ui::AXMode::kExtendedProperties);
  if (was_screen_reader_active != has_extended_properties) {
    // Perform expensive assistive tech detection.
    RefreshAssistiveTech();
  }
}

void BrowserAccessibilityStateImplAuralinux::OnDiscoveredOrca(
    bool is_orca_active) {
  awaiting_known_assistive_tech_computation_ = false;

  if (ActiveAssistiveTech() == ui::AssistiveTech::kGenericScreenReader) {
    // A test has overridden the screen reader state manually.
    // In such cases, we don't want to alter it.
    return;
  }

  UMA_HISTOGRAM_BOOLEAN("Accessibility.Linux.Orca", is_orca_active);
  static auto* ax_orca_crash_key = base::debug::AllocateCrashKeyString(
      "ax_orca", base::debug::CrashKeySize::Size32);
  // Save the current assistive tech before toggling AXModes, so
  // that RefreshAssistiveTechIfNecessary() is a noop.
  if (is_orca_active) {
    base::debug::SetCrashKeyString(ax_orca_crash_key, "true");
    OnAssistiveTechFound(ui::AssistiveTech::kOrca);
    if (!screen_reader_mode_) {
      screen_reader_mode_ = CreateScopedModeForProcess(
          ui::kAXModeComplete | ui::AXMode::kScreenReader);
    }
  } else {
    base::debug::ClearCrashKeyString(ax_orca_crash_key);
    OnAssistiveTechFound(ui::AssistiveTech::kNone);
    screen_reader_mode_.reset();
  }
}

// static
std::unique_ptr<BrowserAccessibilityStateImpl>
BrowserAccessibilityStateImpl::Create() {
  return std::make_unique<BrowserAccessibilityStateImplAuralinux>();
}

}  // namespace content