Upstream version 9.38.198.0
[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   // 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
66 void AutomationManagerAsh::DoDefault(int32 id) {
67   CHECK(enabled_);
68   current_tree_->DoDefault(id);
69 }
70
71 void AutomationManagerAsh::Focus(int32 id) {
72   CHECK(enabled_);
73   current_tree_->Focus(id);
74 }
75
76 void AutomationManagerAsh::MakeVisible(int32 id) {
77   CHECK(enabled_);
78   current_tree_->MakeVisible(id);
79 }
80
81 void AutomationManagerAsh::SetSelection(int32 id, int32 start, int32 end) {
82   CHECK(enabled_);
83   current_tree_->SetSelection(id, start, end);
84 }
85
86 AutomationManagerAsh::AutomationManagerAsh() : enabled_(false) {}
87
88 AutomationManagerAsh::~AutomationManagerAsh() {}
89
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
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.node_id_to_clear,
107                                              update.nodes,
108                                              event_type,
109                                              aura_obj->GetID(),
110                                              0, /* process_id */
111                                              0 /* routing_id */);
112   std::vector<content::AXEventNotificationDetails> details;
113   details.push_back(detail);
114   extensions::automation_util::DispatchAccessibilityEventsToAutomation(
115       details, context);
116 }