Reland braille command support in ChromeVox.
[chromium-blink-merge.git] / chrome / browser / ui / aura / accessibility / automation_manager_aura.cc
blob958197370403460b7903dee4e7972ef05df60388
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
7 #include <vector>
9 #include "base/memory/singleton.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/api/automation_internal/automation_util.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "content/public/browser/ax_event_notification_details.h"
14 #include "content/public/browser/browser_context.h"
15 #include "ui/aura/window.h"
16 #include "ui/views/accessibility/ax_aura_obj_cache.h"
17 #include "ui/views/accessibility/ax_aura_obj_wrapper.h"
18 #include "ui/views/view.h"
19 #include "ui/views/widget/widget.h"
21 using content::BrowserContext;
23 // static
24 AutomationManagerAura* AutomationManagerAura::GetInstance() {
25 return Singleton<AutomationManagerAura>::get();
28 void AutomationManagerAura::Enable(BrowserContext* context) {
29 enabled_ = true;
30 if (!current_tree_.get())
31 current_tree_.reset(new AXTreeSourceAura());
32 ResetSerializer();
34 SendEvent(context, current_tree_->GetRoot(), ui::AX_EVENT_LOAD_COMPLETE);
35 if (focused_window_) {
36 views::AXAuraObjWrapper* focus =
37 views::AXAuraObjCache::GetInstance()->GetOrCreate(focused_window_);
38 SendEvent(context, focus, ui::AX_EVENT_CHILDREN_CHANGED);
42 void AutomationManagerAura::Disable() {
43 enabled_ = false;
45 // Reset the serializer to save memory.
46 current_tree_serializer_->Reset();
49 void AutomationManagerAura::HandleEvent(BrowserContext* context,
50 views::View* view,
51 ui::AXEvent event_type) {
52 if (view->GetWidget())
53 focused_window_ = view->GetWidget()->GetNativeView();
54 if (!enabled_)
55 return;
57 if (!context && g_browser_process->profile_manager())
58 context = g_browser_process->profile_manager()->GetLastUsedProfile();
60 if (!context) {
61 LOG(WARNING) << "Accessibility notification but no browser context";
62 return;
65 views::AXAuraObjWrapper* aura_obj =
66 views::AXAuraObjCache::GetInstance()->GetOrCreate(view);
68 if (processing_events_) {
69 pending_events_.push_back(std::make_pair(aura_obj, event_type));
70 return;
73 processing_events_ = true;
74 SendEvent(context, aura_obj, event_type);
76 for (size_t i = 0; i < pending_events_.size(); ++i)
77 SendEvent(context, pending_events_[i].first, pending_events_[i].second);
79 processing_events_ = false;
80 pending_events_.clear();
83 void AutomationManagerAura::HandleAlert(content::BrowserContext* context,
84 const std::string& text) {
85 if (!enabled_)
86 return;
88 views::AXAuraObjWrapper* obj =
89 static_cast<AXRootObjWrapper*>(current_tree_->GetRoot())
90 ->GetAlertForText(text);
91 SendEvent(context, obj, ui::AX_EVENT_ALERT);
94 void AutomationManagerAura::DoDefault(int32 id) {
95 CHECK(enabled_);
96 current_tree_->DoDefault(id);
99 void AutomationManagerAura::Focus(int32 id) {
100 CHECK(enabled_);
101 current_tree_->Focus(id);
104 void AutomationManagerAura::MakeVisible(int32 id) {
105 CHECK(enabled_);
106 current_tree_->MakeVisible(id);
109 void AutomationManagerAura::SetSelection(int32 id, int32 start, int32 end) {
110 CHECK(enabled_);
111 current_tree_->SetSelection(id, start, end);
114 void AutomationManagerAura::ShowContextMenu(int32 id) {
115 CHECK(enabled_);
116 current_tree_->ShowContextMenu(id);
119 AutomationManagerAura::AutomationManagerAura()
120 : enabled_(false), processing_events_(false), focused_window_(nullptr) {
121 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this);
124 AutomationManagerAura::~AutomationManagerAura() {
127 void AutomationManagerAura::ResetSerializer() {
128 current_tree_serializer_.reset(
129 new ui::AXTreeSerializer<views::AXAuraObjWrapper*>(current_tree_.get()));
132 void AutomationManagerAura::SendEvent(BrowserContext* context,
133 views::AXAuraObjWrapper* aura_obj,
134 ui::AXEvent event_type) {
135 ui::AXTreeUpdate update;
136 current_tree_serializer_->SerializeChanges(aura_obj, &update);
138 // Route this event to special process/routing ids recognized by the
139 // Automation API as the desktop tree.
140 // TODO(dtseng): Would idealy define these special desktop constants in idl.
141 content::AXEventNotificationDetails detail(
142 update.node_id_to_clear, update.nodes, event_type, aura_obj->GetID(),
143 0, /* process_id */
144 0 /* routing_id */);
145 std::vector<content::AXEventNotificationDetails> details;
146 details.push_back(detail);
147 extensions::automation_util::DispatchAccessibilityEventsToAutomation(
148 details, context, gfx::Vector2d());
151 void AutomationManagerAura::OnNativeFocusChanged(aura::Window* focused_now) {
152 focused_window_ = focused_now;