[Extensions] Make extension message bubble factory platform-abstract
[chromium-blink-merge.git] / chrome / browser / ui / aura / accessibility / automation_manager_aura.cc
blob8a0bc144d622f34fca4fc9779c9e0432507430be
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"
7 #include <vector>
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;
23 // static
24 AutomationManagerAura* AutomationManagerAura::GetInstance() {
25 return Singleton<AutomationManagerAura>::get();
28 void AutomationManagerAura::Enable(BrowserContext* context) {
29 enabled_ = true;
30 if (!current_tree_.get())
31 current_tree_.reset(new AXTreeSourceAura());
32 ResetSerializer();
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() {
41 enabled_ = false;
43 // Reset the serializer to save memory.
44 current_tree_serializer_->Reset();
47 void AutomationManagerAura::HandleEvent(BrowserContext* context,
48 views::View* view,
49 ui::AXEvent event_type) {
50 if (!enabled_)
51 return;
53 if (!context && g_browser_process->profile_manager())
54 context = g_browser_process->profile_manager()->GetLastUsedProfile();
56 if (!context) {
57 LOG(WARNING) << "Accessibility notification but no browser context";
58 return;
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));
66 return;
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) {
81 if (!enabled_) {
82 pending_alert_text_ = text;
83 return;
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) {
93 CHECK(enabled_);
94 current_tree_->DoDefault(id);
97 void AutomationManagerAura::Focus(int32 id) {
98 CHECK(enabled_);
99 current_tree_->Focus(id);
102 void AutomationManagerAura::MakeVisible(int32 id) {
103 CHECK(enabled_);
104 current_tree_->MakeVisible(id);
107 void AutomationManagerAura::SetSelection(int32 id, int32 start, int32 end) {
108 CHECK(enabled_);
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(),
135 0, /* process_id */
136 0 /* routing_id */);
137 std::vector<content::AXEventNotificationDetails> details;
138 details.push_back(detail);
139 extensions::automation_util::DispatchAccessibilityEventsToAutomation(
140 details, context, gfx::Vector2d());