Skip to content

Commit

Permalink
Disable accessibility on Chrome OS when all extension clients are gone
Browse files Browse the repository at this point in the history
R=dmazzoni@chromium.org

Change-Id: I345f38507ab27b6d5fabb6a3f989973b861ee1f5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2718488
Commit-Queue: David Tseng <dtseng@chromium.org>
Reviewed-by: Dominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#857472}
  • Loading branch information
dtsengchromium authored and Chromium LUCI CQ committed Feb 25, 2021
1 parent f05a527 commit 99e58af
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
15 changes: 14 additions & 1 deletion chrome/browser/ui/aura/accessibility/automation_manager_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,22 @@ void AutomationManagerAura::Enable() {
PostEvent(focus->GetUniqueId(), ax::mojom::Event::kChildrenChanged);
}
#endif

if (!automation_event_router_observer_.IsObserving()) {
automation_event_router_observer_.Observe(
extensions::AutomationEventRouter::GetInstance());
}
}

void AutomationManagerAura::Disable() {
enabled_ = false;
cache_ = std::make_unique<views::AXAuraObjCache>();
tree_.reset();
Reset(true);
tree_serializer_.reset();
alert_window_.reset();

if (automation_event_router_observer_.IsObserving())
automation_event_router_observer_.Reset();
}

void AutomationManagerAura::OnViewEvent(views::View* view,
Expand All @@ -94,6 +103,10 @@ void AutomationManagerAura::OnViewEvent(views::View* view,
PostEvent(obj->GetUniqueId(), event_type);
}

void AutomationManagerAura::AllAutomationExtensionsGone() {
Disable();
}

void AutomationManagerAura::HandleEvent(ax::mojom::Event event_type) {
views::AXAuraObjWrapper* obj = tree_->GetRoot();
if (!obj)
Expand Down
12 changes: 11 additions & 1 deletion chrome/browser/ui/aura/accessibility/automation_manager_aura.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <vector>

#include "base/macros.h"
#include "base/scoped_observation.h"
#include "extensions/browser/api/automation_internal/automation_event_router.h"
#include "ui/accessibility/ax_action_handler.h"
#include "ui/accessibility/ax_tree_serializer.h"
#include "ui/views/accessibility/ax_aura_obj_cache.h"
Expand All @@ -39,7 +41,8 @@ using AuraAXTreeSerializer = ui::AXTreeSerializer<views::AXAuraObjWrapper*>;
// Manages a tree of automation nodes backed by aura constructs.
class AutomationManagerAura : public ui::AXActionHandler,
public views::AXAuraObjCache::Delegate,
public views::AXEventObserver {
public views::AXEventObserver,
public extensions::AutomationEventRouterObserver {
public:
// Get the single instance of this class.
static AutomationManagerAura* GetInstance();
Expand Down Expand Up @@ -69,6 +72,9 @@ class AutomationManagerAura : public ui::AXActionHandler,
// views::AXEventObserver:
void OnViewEvent(views::View* view, ax::mojom::Event event_type) override;

// AutomationEventRouterObserver:
void AllAutomationExtensionsGone() override;

void set_event_bundle_sink(ui::AXEventBundleSink* sink) {
event_bundle_sink_ = sink;
}
Expand Down Expand Up @@ -137,6 +143,10 @@ class AutomationManagerAura : public ui::AXActionHandler,

bool is_performing_action_ = false;

base::ScopedObservation<extensions::AutomationEventRouter,
extensions::AutomationEventRouterObserver>
automation_event_router_observer_{this};

DISALLOW_COPY_AND_ASSIGN(AutomationManagerAura);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ AutomationEventRouter::AutomationEventRouter()
#endif
}

AutomationEventRouter::~AutomationEventRouter() {}
AutomationEventRouter::~AutomationEventRouter() = default;

void AutomationEventRouter::RegisterListenerForOneTree(
const ExtensionId& extension_id,
Expand Down Expand Up @@ -170,12 +170,22 @@ void AutomationEventRouter::DispatchGetTextLocationDataResult(
->DispatchEventToExtension(data.source_extension_id, std::move(event));
}

AutomationEventRouter::AutomationListener::AutomationListener() {}
void AutomationEventRouter::AddObserver(
AutomationEventRouterObserver* observer) {
observers_.AddObserver(observer);
}

void AutomationEventRouter::RemoveObserver(
AutomationEventRouterObserver* observer) {
observers_.RemoveObserver(observer);
}

AutomationEventRouter::AutomationListener::AutomationListener() = default;

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

AutomationEventRouter::AutomationListener::~AutomationListener() {}
AutomationEventRouter::AutomationListener::~AutomationListener() = default;

void AutomationEventRouter::Register(const ExtensionId& extension_id,
int listener_process_id,
Expand Down Expand Up @@ -238,6 +248,11 @@ void AutomationEventRouter::RenderProcessHostDestroyed(
});
rph_observers_.Remove(host);
UpdateActiveProfile();

if (rph_observers_.GetSourcesCount() == 0) {
for (AutomationEventRouterObserver& observer : observers_)
observer.AllAutomationExtensionsGone();
}
}

void AutomationEventRouter::UpdateActiveProfile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ struct ExtensionMsg_AccessibilityLocationChangeParams;
namespace extensions {
struct AutomationListener;

class AutomationEventRouterObserver {
public:
virtual void AllAutomationExtensionsGone() = 0;
};

class AutomationEventRouter : public ui::AXEventBundleSink,
public content::RenderProcessHostObserver,
public AutomationEventRouterInterface {
Expand Down Expand Up @@ -78,6 +83,9 @@ class AutomationEventRouter : public ui::AXEventBundleSink,
const ui::AXActionData& data,
const base::Optional<gfx::Rect>& rect) override;

void AddObserver(AutomationEventRouterObserver* observer);
void RemoveObserver(AutomationEventRouterObserver* observer);

private:
struct AutomationListener {
AutomationListener();
Expand Down Expand Up @@ -131,6 +139,8 @@ class AutomationEventRouter : public ui::AXEventBundleSink,
ScopedObserver<content::RenderProcessHost, content::RenderProcessHostObserver>
rph_observers_{this};

base::ObserverList<AutomationEventRouterObserver>::Unchecked observers_;

friend struct base::DefaultSingletonTraits<AutomationEventRouter>;

DISALLOW_COPY_AND_ASSIGN(AutomationEventRouter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "base/bind.h"
#include "base/macros.h"
#include "base/scoped_observation.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
Expand Down Expand Up @@ -140,9 +141,12 @@ base::LazyInstance<OldAXTreeIdMap>::DestructorAtExit g_old_ax_tree =
// Helper class that receives accessibility data from |WebContents|.
class AutomationWebContentsObserver
: public content::WebContentsObserver,
public content::WebContentsUserData<AutomationWebContentsObserver> {
public content::WebContentsUserData<AutomationWebContentsObserver>,
public AutomationEventRouterObserver {
public:
~AutomationWebContentsObserver() override {}
~AutomationWebContentsObserver() override {
automation_event_router_observer_.Reset();
}

// content::WebContentsObserver overrides.
void AccessibilityEventReceived(const content::AXEventNotificationDetails&
Expand Down Expand Up @@ -232,6 +236,17 @@ class AutomationWebContentsObserver
AccessibilityEventReceived(content_event_bundle);
}

// AutomationEventRouterObserver overrides.
void AllAutomationExtensionsGone() override {
if (!web_contents())
return;

ui::AXMode new_mode = web_contents()->GetAccessibilityMode();
uint8_t flags = ui::kAXModeWebContentsOnly.mode();
new_mode.set_mode(flags, false);
web_contents()->SetAccessibilityMode(std::move(new_mode));
}

private:
friend class content::WebContentsUserData<AutomationWebContentsObserver>;

Expand All @@ -250,10 +265,17 @@ class AutomationWebContentsObserver
ax::mojom::Event::kMediaStartedPlaying;
AccessibilityEventReceived(content_event_bundle);
}

automation_event_router_observer_.Observe(
AutomationEventRouter::GetInstance());
}

content::BrowserContext* browser_context_;

base::ScopedObservation<extensions::AutomationEventRouter,
extensions::AutomationEventRouterObserver>
automation_event_router_observer_{this};

WEB_CONTENTS_USER_DATA_KEY_DECL();

DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver);
Expand Down

0 comments on commit 99e58af

Please sign in to comment.