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"
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
;
24 AutomationManagerAura
* AutomationManagerAura::GetInstance() {
25 return Singleton
<AutomationManagerAura
>::get();
28 void AutomationManagerAura::Enable(BrowserContext
* context
) {
30 if (!current_tree_
.get())
31 current_tree_
.reset(new AXTreeSourceAura());
33 SendEvent(context
, current_tree_
->GetRoot(), ui::AX_EVENT_LOAD_COMPLETE
);
34 if (!pending_alert_text_
.empty()) {
35 HandleAlert(context
, pending_alert_text_
);
36 pending_alert_text_
.clear();
40 void AutomationManagerAura::Disable() {
43 // Reset the serializer to save memory.
44 current_tree_serializer_
->Reset();
47 void AutomationManagerAura::HandleEvent(BrowserContext
* context
,
49 ui::AXEvent event_type
) {
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
);
64 if (processing_events_
) {
65 pending_events_
.push_back(std::make_pair(aura_obj
, event_type
));
69 processing_events_
= true;
70 SendEvent(context
, aura_obj
, event_type
);
72 for (size_t i
= 0; i
< pending_events_
.size(); ++i
)
73 SendEvent(context
, pending_events_
[i
].first
, pending_events_
[i
].second
);
75 processing_events_
= false;
76 pending_events_
.clear();
79 void AutomationManagerAura::HandleAlert(content::BrowserContext
* context
,
80 const std::string
& text
) {
82 pending_alert_text_
= text
;
86 views::AXAuraObjWrapper
* obj
=
87 static_cast<AXRootObjWrapper
*>(current_tree_
->GetRoot())
88 ->GetAlertForText(text
);
89 SendEvent(context
, obj
, ui::AX_EVENT_ALERT
);
92 void AutomationManagerAura::DoDefault(int32 id
) {
94 current_tree_
->DoDefault(id
);
97 void AutomationManagerAura::Focus(int32 id
) {
99 current_tree_
->Focus(id
);
102 void AutomationManagerAura::MakeVisible(int32 id
) {
104 current_tree_
->MakeVisible(id
);
107 void AutomationManagerAura::SetSelection(int32 id
, int32 start
, int32 end
) {
109 current_tree_
->SetSelection(id
, start
, end
);
112 AutomationManagerAura::AutomationManagerAura()
113 : enabled_(false), processing_events_(false) {
116 AutomationManagerAura::~AutomationManagerAura() {
119 void AutomationManagerAura::ResetSerializer() {
120 current_tree_serializer_
.reset(
121 new ui::AXTreeSerializer
<views::AXAuraObjWrapper
*>(current_tree_
.get()));
124 void AutomationManagerAura::SendEvent(BrowserContext
* context
,
125 views::AXAuraObjWrapper
* aura_obj
,
126 ui::AXEvent event_type
) {
127 ui::AXTreeUpdate update
;
128 current_tree_serializer_
->SerializeChanges(aura_obj
, &update
);
130 // Route this event to special process/routing ids recognized by the
131 // Automation API as the desktop tree.
132 // TODO(dtseng): Would idealy define these special desktop constants in idl.
133 content::AXEventNotificationDetails
detail(
134 update
.node_id_to_clear
, update
.nodes
, event_type
, aura_obj
->GetID(),
137 std::vector
<content::AXEventNotificationDetails
> details
;
138 details
.push_back(detail
);
139 extensions::automation_util::DispatchAccessibilityEventsToAutomation(
140 details
, context
, gfx::Vector2d());