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/ash/accessibility/automation_manager_ash.h"
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/views/accessibility/ax_aura_obj_cache.h"
16 #include "ui/views/accessibility/ax_aura_obj_wrapper.h"
17 #include "ui/views/view.h"
18 #include "ui/views/widget/widget.h"
20 using content::BrowserContext;
23 AutomationManagerAsh* AutomationManagerAsh::GetInstance() {
24 return Singleton<AutomationManagerAsh>::get();
27 void AutomationManagerAsh::Enable(BrowserContext* context) {
30 SendEvent(context, current_tree_->GetRoot(), ui::AX_EVENT_LOAD_COMPLETE);
33 void AutomationManagerAsh::Disable() {
36 // Reset the serializer to save memory.
37 current_tree_serializer_->Reset();
40 void AutomationManagerAsh::HandleEvent(BrowserContext* context,
42 ui::AXEvent event_type) {
47 // TODO(dtseng): Events should only be delivered to extensions with the
48 // desktop permission.
49 views::Widget* widget = view->GetWidget();
53 if (!context && g_browser_process->profile_manager()) {
54 context = g_browser_process->profile_manager()->GetLastUsedProfile();
57 LOG(WARNING) << "Accessibility notification but no browser context";
61 views::AXAuraObjWrapper* aura_obj =
62 views::AXAuraObjCache::GetInstance()->GetOrCreate(view);
63 SendEvent(context, aura_obj, event_type);
66 void AutomationManagerAsh::DoDefault(int32 id) {
68 current_tree_->DoDefault(id);
71 void AutomationManagerAsh::Focus(int32 id) {
73 current_tree_->Focus(id);
76 void AutomationManagerAsh::MakeVisible(int32 id) {
78 current_tree_->MakeVisible(id);
81 void AutomationManagerAsh::SetSelection(int32 id, int32 start, int32 end) {
83 current_tree_->SetSelection(id, start, end);
86 AutomationManagerAsh::AutomationManagerAsh() : enabled_(false) {}
88 AutomationManagerAsh::~AutomationManagerAsh() {}
90 void AutomationManagerAsh::Reset() {
91 current_tree_.reset(new AXTreeSourceAsh());
92 current_tree_serializer_.reset(
93 new ui::AXTreeSerializer<views::AXAuraObjWrapper*>(
94 current_tree_.get()));
97 void AutomationManagerAsh::SendEvent(BrowserContext* context,
98 views::AXAuraObjWrapper* aura_obj,
99 ui::AXEvent event_type) {
100 ui::AXTreeUpdate update;
101 current_tree_serializer_->SerializeChanges(aura_obj, &update);
103 // Route this event to special process/routing ids recognized by the
104 // Automation API as the desktop tree.
105 // TODO(dtseng): Would idealy define these special desktop constants in idl.
106 content::AXEventNotificationDetails detail(update.node_id_to_clear,
112 std::vector<content::AXEventNotificationDetails> details;
113 details.push_back(detail);
114 extensions::automation_util::DispatchAccessibilityEventsToAutomation(