ff3132df83318859104908e832604604a0d2a336
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / accessibility / automation_manager_ash.cc
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
23 AutomationManagerAsh* AutomationManagerAsh::GetInstance() {
24   return Singleton<AutomationManagerAsh>::get();
25 }
26
27 void AutomationManagerAsh::Enable(BrowserContext* context) {
28   enabled_ = true;
29   Reset();
30   SendEvent(context, current_tree_->GetRoot(), ui::AX_EVENT_LOAD_COMPLETE);
31 }
32
33 void AutomationManagerAsh::Disable() {
34   enabled_ = false;
35
36   // Reset the serializer to save memory.
37   current_tree_serializer_->Reset();
38 }
39
40 void AutomationManagerAsh::HandleEvent(BrowserContext* context,
41                                          views::View* view,
42                                          ui::AXEvent event_type) {
43   if (!enabled_) {
44     return;
45   }
46
47   views::Widget* widget = view->GetWidget();
48   if (!widget)
49     return;
50
51   if (!context && g_browser_process->profile_manager()) {
52     context = g_browser_process->profile_manager()->GetLastUsedProfile();
53   }
54   if (!context) {
55     LOG(WARNING) << "Accessibility notification but no browser context";
56     return;
57   }
58
59   views::AXAuraObjWrapper* aura_obj =
60       views::AXAuraObjCache::GetInstance()->GetOrCreate(view);
61   SendEvent(context, aura_obj, event_type);
62 }
63
64 void AutomationManagerAsh::DoDefault(int32 id) {
65   CHECK(enabled_);
66   current_tree_->DoDefault(id);
67 }
68
69 void AutomationManagerAsh::Focus(int32 id) {
70   CHECK(enabled_);
71   current_tree_->Focus(id);
72 }
73
74 void AutomationManagerAsh::MakeVisible(int32 id) {
75   CHECK(enabled_);
76   current_tree_->MakeVisible(id);
77 }
78
79 void AutomationManagerAsh::SetSelection(int32 id, int32 start, int32 end) {
80   CHECK(enabled_);
81   current_tree_->SetSelection(id, start, end);
82 }
83
84 AutomationManagerAsh::AutomationManagerAsh() : enabled_(false) {
85 }
86
87 AutomationManagerAsh::~AutomationManagerAsh() {}
88
89 void AutomationManagerAsh::Reset() {
90   current_tree_.reset(new AXTreeSourceAsh());
91   current_tree_serializer_.reset(
92       new ui::AXTreeSerializer<views::AXAuraObjWrapper*>(
93           current_tree_.get()));
94 }
95
96 void AutomationManagerAsh::SendEvent(BrowserContext* context,
97                                        views::AXAuraObjWrapper* aura_obj,
98                                        ui::AXEvent event_type) {
99   ui::AXTreeUpdate update;
100   current_tree_serializer_->SerializeChanges(aura_obj, &update);
101
102   // Route this event to special process/routing ids recognized by the
103   // Automation API as the desktop tree.
104   // TODO(dtseng): Would idealy define these special desktop constants in idl.
105   content::AXEventNotificationDetails detail(update.node_id_to_clear,
106                                              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, gfx::Vector2d());
115 }