File: automation_event_router.cc

package info (click to toggle)
chromium 73.0.3683.75-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,792,156 kB
  • sloc: cpp: 13,473,466; ansic: 1,577,080; python: 898,539; javascript: 655,737; xml: 341,883; asm: 306,070; java: 289,969; perl: 80,911; objc: 67,198; sh: 43,184; cs: 27,853; makefile: 12,092; php: 11,064; yacc: 10,373; tcl: 8,875; ruby: 3,941; lex: 1,800; pascal: 1,473; lisp: 812; awk: 41; jsp: 39; sed: 19; sql: 3
file content (280 lines) | stat: -rw-r--r-- 10,011 bytes parent folder | download
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/automation_internal/automation_event_router.h"

#include <algorithm>
#include <memory>
#include <string>
#include <utility>

#include "base/stl_util.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/extensions/api/automation_internal.h"
#include "chrome/common/extensions/chrome_extension_messages.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/browser/event_router.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_messages.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"

#if defined(USE_AURA)
#include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
#endif

namespace extensions {

// static
AutomationEventRouter* AutomationEventRouter::GetInstance() {
  return base::Singleton<
      AutomationEventRouter,
      base::LeakySingletonTraits<AutomationEventRouter>>::get();
}

AutomationEventRouter::AutomationEventRouter()
    : active_profile_(ProfileManager::GetActiveUserProfile()) {
  registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
                 content::NotificationService::AllBrowserContextsAndSources());
  registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
                 content::NotificationService::AllBrowserContextsAndSources());
#if defined(USE_AURA)
  // Not reset because |this| is leaked.
  AutomationManagerAura::GetInstance()->set_event_bundle_sink(this);
#endif
}

AutomationEventRouter::~AutomationEventRouter() {
}

void AutomationEventRouter::RegisterListenerForOneTree(
    const ExtensionId& extension_id,
    int listener_process_id,
    ui::AXTreeID source_ax_tree_id) {
  Register(extension_id,
           listener_process_id,
           source_ax_tree_id,
           false);
}

void AutomationEventRouter::RegisterListenerWithDesktopPermission(
    const ExtensionId& extension_id,
    int listener_process_id) {
  Register(extension_id, listener_process_id, ui::AXTreeIDUnknown(), true);
}

void AutomationEventRouter::DispatchAccessibilityEvents(
    const ExtensionMsg_AccessibilityEventBundleParams& event_bundle) {
  if (active_profile_ != ProfileManager::GetActiveUserProfile()) {
    active_profile_ = ProfileManager::GetActiveUserProfile();
    UpdateActiveProfile();
  }

  for (const auto& listener : listeners_) {
    // Skip listeners that don't want to listen to this tree.
    if (!listener.desktop && listener.tree_ids.find(event_bundle.tree_id) ==
                                 listener.tree_ids.end()) {
      continue;
    }

    content::RenderProcessHost* rph =
        content::RenderProcessHost::FromID(listener.process_id);
    rph->Send(new ExtensionMsg_AccessibilityEventBundle(
        event_bundle, listener.is_active_profile));
  }
}

void AutomationEventRouter::DispatchAccessibilityLocationChange(
    const ExtensionMsg_AccessibilityLocationChangeParams& params) {
  for (const auto& listener : listeners_) {
    // Skip listeners that don't want to listen to this tree.
    if (!listener.desktop &&
        listener.tree_ids.find(params.tree_id) == listener.tree_ids.end()) {
      continue;
    }

    content::RenderProcessHost* rph =
        content::RenderProcessHost::FromID(listener.process_id);
    rph->Send(new ExtensionMsg_AccessibilityLocationChange(params));
  }
}

void AutomationEventRouter::DispatchTreeDestroyedEvent(
    ui::AXTreeID tree_id,
    content::BrowserContext* browser_context) {
  if (listeners_.empty())
    return;

  browser_context = browser_context ? browser_context : active_profile_;
  std::unique_ptr<base::ListValue> args(
      api::automation_internal::OnAccessibilityTreeDestroyed::Create(
          tree_id.ToString()));
  auto event = std::make_unique<Event>(
      events::AUTOMATION_INTERNAL_ON_ACCESSIBILITY_TREE_DESTROYED,
      api::automation_internal::OnAccessibilityTreeDestroyed::kEventName,
      std::move(args), browser_context);
  EventRouter::Get(browser_context)->BroadcastEvent(std::move(event));

  if (tree_destroyed_callback_for_test_)
    tree_destroyed_callback_for_test_.Run(tree_id);
}

void AutomationEventRouter::DispatchActionResult(const ui::AXActionData& data,
                                                 bool result) {
  CHECK(!data.source_extension_id.empty());

  if (listeners_.empty())
    return;

  std::unique_ptr<base::ListValue> args(
      api::automation_internal::OnActionResult::Create(
          data.target_tree_id.ToString(), data.request_id, result));
  auto event = std::make_unique<Event>(
      events::AUTOMATION_INTERNAL_ON_ACTION_RESULT,
      api::automation_internal::OnActionResult::kEventName, std::move(args),
      active_profile_);
  EventRouter::Get(active_profile_)
      ->DispatchEventToExtension(data.source_extension_id, std::move(event));
}

void AutomationEventRouter::SetTreeDestroyedCallbackForTest(
    base::RepeatingCallback<void(ui::AXTreeID)> cb) {
  tree_destroyed_callback_for_test_ = cb;
}

void AutomationEventRouter::DispatchGetTextLocationDataResult(
    const ui::AXActionData& data,
    const base::Optional<gfx::Rect>& rect) {
  CHECK(!data.source_extension_id.empty());

  if (listeners_.empty())
    return;
  extensions::api::automation_internal::AXTextLocationParams params;
  params.tree_id = data.target_tree_id.ToString();
  params.node_id = data.target_node_id;
  params.result = false;
  if (rect) {
    params.left = rect.value().x();
    params.top = rect.value().y();
    params.width = rect.value().width();
    params.height = rect.value().height();
    params.result = true;
  }
  params.request_id = data.request_id;

  std::unique_ptr<base::ListValue> args(
      api::automation_internal::OnGetTextLocationResult::Create(params));
  auto event = std::make_unique<Event>(
      events::AUTOMATION_INTERNAL_ON_GET_TEXT_LOCATION_RESULT,
      api::automation_internal::OnGetTextLocationResult::kEventName,
      std::move(args), active_profile_);
  EventRouter::Get(active_profile_)
      ->DispatchEventToExtension(data.source_extension_id, std::move(event));
}

AutomationEventRouter::AutomationListener::AutomationListener() {
}

AutomationEventRouter::AutomationListener::AutomationListener(
    const AutomationListener& other) = default;

AutomationEventRouter::AutomationListener::~AutomationListener() {
}

void AutomationEventRouter::Register(const ExtensionId& extension_id,
                                     int listener_process_id,
                                     ui::AXTreeID ax_tree_id,
                                     bool desktop) {
  DCHECK(desktop || ax_tree_id != ui::AXTreeIDUnknown());
  auto iter =
      std::find_if(listeners_.begin(), listeners_.end(),
                   [listener_process_id](const AutomationListener& item) {
                     return item.process_id == listener_process_id;
                   });

  // Add a new entry if we don't have one with that process.
  if (iter == listeners_.end()) {
    AutomationListener listener;
    listener.extension_id = extension_id;
    listener.process_id = listener_process_id;
    listener.desktop = desktop;
    if (!desktop)
      listener.tree_ids.insert(ax_tree_id);
    listeners_.push_back(listener);
    UpdateActiveProfile();
    return;
  }

  // We have an entry with that process so update the set of tree ids it wants
  // to listen to, and update its desktop permission.
  if (desktop)
    iter->desktop = true;
  else
    iter->tree_ids.insert(ax_tree_id);
}

void AutomationEventRouter::DispatchAccessibilityEvents(
    const ui::AXTreeID& tree_id,
    std::vector<ui::AXTreeUpdate> updates,
    const gfx::Point& mouse_location,
    std::vector<ui::AXEvent> events) {
  ExtensionMsg_AccessibilityEventBundleParams event_bundle;
  event_bundle.tree_id = tree_id;
  event_bundle.updates = std::move(updates);
  event_bundle.mouse_location = mouse_location;
  event_bundle.events = std::move(events);

  DispatchAccessibilityEvents(event_bundle);
}

void AutomationEventRouter::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  if (type != content::NOTIFICATION_RENDERER_PROCESS_TERMINATED &&
      type != content::NOTIFICATION_RENDERER_PROCESS_CLOSED) {
    NOTREACHED();
    return;
  }

  content::RenderProcessHost* rph =
      content::Source<content::RenderProcessHost>(source).ptr();
  int process_id = rph->GetID();
  base::EraseIf(listeners_, [process_id](const AutomationListener& item) {
    return item.process_id == process_id;
  });
  UpdateActiveProfile();
}

void AutomationEventRouter::UpdateActiveProfile() {
  for (auto& listener : listeners_) {
#if defined(OS_CHROMEOS)
    int extension_id_count = 0;
    for (const auto& listener2 : listeners_) {
      if (listener2.extension_id == listener.extension_id)
        extension_id_count++;
    }
    content::RenderProcessHost* rph =
        content::RenderProcessHost::FromID(listener.process_id);

    // The purpose of is_active_profile is to ensure different instances of
    // the same extension running in different profiles don't interfere with
    // one another. If an automation extension is only running in one profile,
    // always mark it as active. If it's running in two or more profiles,
    // only mark one as active.
    listener.is_active_profile = (extension_id_count == 1 ||
                                  rph->GetBrowserContext() == active_profile_);
#else
    listener.is_active_profile = true;
#endif
  }
}

}  // namespace extensions