OLD | NEW |
| (Empty) |
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/extensions/api/automation_internal/automation_util.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 | |
10 #include "base/values.h" | |
11 #include "chrome/browser/accessibility/ax_tree_id_registry.h" | |
12 #include "chrome/common/extensions/api/automation_internal.h" | |
13 #include "content/public/browser/browser_context.h" | |
14 #include "content/public/browser/browser_plugin_guest_manager.h" | |
15 #include "content/public/browser/render_frame_host.h" | |
16 #include "content/public/browser/render_process_host.h" | |
17 #include "content/public/browser/web_contents.h" | |
18 #include "extensions/browser/event_router.h" | |
19 #include "ui/accessibility/ax_enums.h" | |
20 #include "ui/accessibility/ax_node_data.h" | |
21 | |
22 namespace extensions { | |
23 | |
24 namespace { | |
25 | |
26 void PopulateNodeData(const ui::AXNodeData& node_data, | |
27 linked_ptr< api::automation_internal::AXNodeData>& out_node_data) { | |
28 out_node_data->id = node_data.id; | |
29 out_node_data->role = ToString(node_data.role); | |
30 | |
31 uint32 state_pos = 0, state_shifter = node_data.state; | |
32 while (state_shifter) { | |
33 if (state_shifter & 1) { | |
34 out_node_data->state.additional_properties.SetBoolean( | |
35 ToString(static_cast<ui::AXState>(state_pos)), true); | |
36 } | |
37 state_shifter = state_shifter >> 1; | |
38 state_pos++; | |
39 } | |
40 | |
41 out_node_data->location.left = node_data.location.x(); | |
42 out_node_data->location.top = node_data.location.y(); | |
43 out_node_data->location.width = node_data.location.width(); | |
44 out_node_data->location.height = node_data.location.height(); | |
45 | |
46 if (!node_data.bool_attributes.empty()) { | |
47 out_node_data->bool_attributes.reset( | |
48 new api::automation_internal::AXNodeData::BoolAttributes()); | |
49 for (size_t i = 0; i < node_data.bool_attributes.size(); ++i) { | |
50 std::pair<ui::AXBoolAttribute, bool> attr = | |
51 node_data.bool_attributes[i]; | |
52 out_node_data->bool_attributes->additional_properties.SetBoolean( | |
53 ToString(attr.first), attr.second); | |
54 } | |
55 } | |
56 | |
57 if (!node_data.float_attributes.empty()) { | |
58 out_node_data->float_attributes.reset( | |
59 new api::automation_internal::AXNodeData::FloatAttributes()); | |
60 for (size_t i = 0; i < node_data.float_attributes.size(); ++i) { | |
61 std::pair<ui::AXFloatAttribute, float> attr = | |
62 node_data.float_attributes[i]; | |
63 out_node_data->float_attributes->additional_properties.SetDouble( | |
64 ToString(attr.first), attr.second); | |
65 } | |
66 } | |
67 | |
68 if (!node_data.html_attributes.empty()) { | |
69 out_node_data->html_attributes.reset( | |
70 new api::automation_internal::AXNodeData::HtmlAttributes()); | |
71 for (size_t i = 0; i < node_data.html_attributes.size(); ++i) { | |
72 std::pair<std::string, std::string> attr = node_data.html_attributes[i]; | |
73 out_node_data->html_attributes->additional_properties.SetString( | |
74 attr.first, attr.second); | |
75 } | |
76 } | |
77 | |
78 if (!node_data.int_attributes.empty()) { | |
79 out_node_data->int_attributes.reset( | |
80 new api::automation_internal::AXNodeData::IntAttributes()); | |
81 for (size_t i = 0; i < node_data.int_attributes.size(); ++i) { | |
82 std::pair<ui::AXIntAttribute, int> attr = node_data.int_attributes[i]; | |
83 out_node_data->int_attributes->additional_properties.SetInteger( | |
84 ToString(attr.first), attr.second); | |
85 } | |
86 } | |
87 | |
88 if (!node_data.intlist_attributes.empty()) { | |
89 out_node_data->intlist_attributes.reset( | |
90 new api::automation_internal::AXNodeData::IntlistAttributes()); | |
91 for (size_t i = 0; i < node_data.intlist_attributes.size(); ++i) { | |
92 std::pair<ui::AXIntListAttribute, std::vector<int32> > attr = | |
93 node_data.intlist_attributes[i]; | |
94 base::ListValue* intlist = new base::ListValue(); | |
95 for (size_t j = 0; j < attr.second.size(); ++j) | |
96 intlist->AppendInteger(attr.second[j]); | |
97 out_node_data->intlist_attributes->additional_properties.Set( | |
98 ToString(attr.first), intlist); | |
99 } | |
100 } | |
101 | |
102 if (!node_data.string_attributes.empty()) { | |
103 out_node_data->string_attributes.reset( | |
104 new api::automation_internal::AXNodeData::StringAttributes()); | |
105 for (size_t i = 0; i < node_data.string_attributes.size(); ++i) { | |
106 std::pair<ui::AXStringAttribute, std::string> attr = | |
107 node_data.string_attributes[i]; | |
108 out_node_data->string_attributes->additional_properties.SetString( | |
109 ToString(attr.first), attr.second); | |
110 } | |
111 } | |
112 | |
113 for (size_t i = 0; i < node_data.child_ids.size(); ++i) { | |
114 out_node_data->child_ids.push_back(node_data.child_ids[i]); | |
115 } | |
116 } | |
117 | |
118 void DispatchEventInternal(content::BrowserContext* context, | |
119 const std::string& event_name, | |
120 scoped_ptr<base::ListValue> args) { | |
121 if (context && EventRouter::Get(context)) { | |
122 scoped_ptr<Event> event( | |
123 new Event(events::UNKNOWN, event_name, args.Pass())); | |
124 event->restrict_to_browser_context = context; | |
125 EventRouter::Get(context)->BroadcastEvent(event.Pass()); | |
126 } | |
127 } | |
128 | |
129 } // namespace | |
130 | |
131 namespace automation_util { | |
132 | |
133 void DispatchAccessibilityEventsToAutomation( | |
134 const std::vector<content::AXEventNotificationDetails>& details, | |
135 content::BrowserContext* browser_context, | |
136 const gfx::Vector2d& location_offset) { | |
137 using api::automation_internal::AXEventParams; | |
138 using api::automation_internal::AXTreeUpdate; | |
139 | |
140 std::vector<content::AXEventNotificationDetails>::const_iterator iter = | |
141 details.begin(); | |
142 for (; iter != details.end(); ++iter) { | |
143 const content::AXEventNotificationDetails& event = *iter; | |
144 | |
145 AXEventParams ax_event_params; | |
146 ax_event_params.tree_id = | |
147 AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID(event.process_id, | |
148 event.routing_id); | |
149 ax_event_params.event_type = ToString(iter->event_type); | |
150 ax_event_params.target_id = event.id; | |
151 | |
152 AXTreeUpdate& ax_tree_update = ax_event_params.update; | |
153 ax_tree_update.node_id_to_clear = event.node_id_to_clear; | |
154 for (size_t i = 0; i < event.nodes.size(); ++i) { | |
155 ui::AXNodeData src = event.nodes[i]; | |
156 src.location.Offset(location_offset); | |
157 linked_ptr<api::automation_internal::AXNodeData> out_node( | |
158 new api::automation_internal::AXNodeData()); | |
159 PopulateNodeData(src, out_node); | |
160 if (src.HasBoolAttribute(ui::AX_ATTR_IS_AX_TREE_HOST)) { | |
161 const auto& iter = event.node_to_browser_plugin_instance_id_map.find( | |
162 src.id); | |
163 if (iter != event.node_to_browser_plugin_instance_id_map.end()) { | |
164 int instance_id = iter->second; | |
165 content::BrowserPluginGuestManager* guest_manager = | |
166 browser_context->GetGuestManager(); | |
167 content::WebContents* guest_web_contents = | |
168 guest_manager->GetGuestByInstanceID(event.process_id, | |
169 instance_id); | |
170 if (guest_web_contents) { | |
171 content::RenderFrameHost* guest_rfh = | |
172 guest_web_contents->GetMainFrame(); | |
173 int guest_tree_id = | |
174 AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | |
175 guest_rfh->GetProcess()->GetID(), | |
176 guest_rfh->GetRoutingID()); | |
177 out_node->int_attributes->additional_properties.SetInteger( | |
178 ToString(ui::AX_ATTR_CHILD_TREE_ID), | |
179 guest_tree_id); | |
180 } | |
181 } | |
182 } | |
183 ax_tree_update.nodes.push_back(out_node); | |
184 } | |
185 | |
186 // TODO(dtseng/aboxhall): Why are we sending only one update at a time? We | |
187 // should match the behavior from renderer -> browser and send a | |
188 // collection of tree updates over (to the extension); see | |
189 // |AccessibilityHostMsg_EventParams| and |AccessibilityHostMsg_Events|. | |
190 DispatchEventInternal( | |
191 browser_context, | |
192 api::automation_internal::OnAccessibilityEvent::kEventName, | |
193 api::automation_internal::OnAccessibilityEvent::Create( | |
194 ax_event_params)); | |
195 } | |
196 } | |
197 | |
198 void DispatchTreeDestroyedEventToAutomation( | |
199 int process_id, | |
200 int routing_id, | |
201 content::BrowserContext* browser_context) { | |
202 int tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | |
203 process_id, routing_id); | |
204 DispatchEventInternal( | |
205 browser_context, | |
206 api::automation_internal::OnAccessibilityTreeDestroyed::kEventName, | |
207 api::automation_internal::OnAccessibilityTreeDestroyed::Create(tree_id)); | |
208 AXTreeIDRegistry::GetInstance()->RemoveAXTreeID(tree_id); | |
209 } | |
210 | |
211 } // namespace automation_util | |
212 | |
213 } // namespace extensions | |
OLD | NEW |