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.
4
5 #include "chrome/browser/ui/ash/accessibility/automation_manager_ash.h"
6
7 #include <vector>
8
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"
19
20 using content::BrowserContext;
21
22 // static
GetInstance()23 AutomationManagerAsh* AutomationManagerAsh::GetInstance() {
24 return Singleton<AutomationManagerAsh>::get();
25 }
26
Enable(BrowserContext * context)27 void AutomationManagerAsh::Enable(BrowserContext* context) {
28 enabled_ = true;
29 Reset();
30 SendEvent(context, current_tree_->GetRoot(), ui::AX_EVENT_LOAD_COMPLETE);
31 }
32
Disable()33 void AutomationManagerAsh::Disable() {
34 enabled_ = false;
35
36 // Reset the serializer to save memory.
37 current_tree_serializer_->Reset();
38 }
39
HandleEvent(BrowserContext * context,views::View * view,ui::AXEvent event_type)40 void AutomationManagerAsh::HandleEvent(BrowserContext* context,
41 views::View* view,
42 ui::AXEvent event_type) {
43 if (!enabled_) {
44 return;
45 }
46
47 // TODO(dtseng): Events should only be delivered to extensions with the
48 // desktop permission.
49 views::Widget* widget = view->GetWidget();
50 if (!widget)
51 return;
52
53 if (!context && g_browser_process->profile_manager()) {
54 context = g_browser_process->profile_manager()->GetLastUsedProfile();
55 }
56 if (!context) {
57 LOG(WARNING) << "Accessibility notification but no browser context";
58 return;
59 }
60
61 views::AXAuraObjWrapper* aura_obj =
62 views::AXAuraObjCache::GetInstance()->GetOrCreate(view);
63 SendEvent(context, aura_obj, event_type);
64 }
65
DoDefault(int32 id)66 void AutomationManagerAsh::DoDefault(int32 id) {
67 CHECK(enabled_);
68 current_tree_->DoDefault(id);
69 }
70
Focus(int32 id)71 void AutomationManagerAsh::Focus(int32 id) {
72 CHECK(enabled_);
73 current_tree_->Focus(id);
74 }
75
MakeVisible(int32 id)76 void AutomationManagerAsh::MakeVisible(int32 id) {
77 CHECK(enabled_);
78 current_tree_->MakeVisible(id);
79 }
80
SetSelection(int32 id,int32 start,int32 end)81 void AutomationManagerAsh::SetSelection(int32 id, int32 start, int32 end) {
82 CHECK(enabled_);
83 current_tree_->SetSelection(id, start, end);
84 }
85
AutomationManagerAsh()86 AutomationManagerAsh::AutomationManagerAsh() : enabled_(false) {}
87
~AutomationManagerAsh()88 AutomationManagerAsh::~AutomationManagerAsh() {}
89
Reset()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()));
95 }
96
SendEvent(BrowserContext * context,views::AXAuraObjWrapper * aura_obj,ui::AXEvent event_type)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);
102
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.nodes,
107 event_type,
108 aura_obj->GetID(),
109 0, /* process_id */
110 0 /* routing_id */);
111 std::vector<content::AXEventNotificationDetails> details;
112 details.push_back(detail);
113 extensions::automation_util::DispatchAccessibilityEventsToAutomation(
114 details, context);
115 }
116