| 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 events::HistogramValue histogram_value, | |
| 120 const std::string& event_name, | |
| 121 scoped_ptr<base::ListValue> args) { | |
| 122 if (context && EventRouter::Get(context)) { | |
| 123 scoped_ptr<Event> event( | |
| 124 new Event(histogram_value, event_name, args.Pass())); | |
| 125 event->restrict_to_browser_context = context; | |
| 126 EventRouter::Get(context)->BroadcastEvent(event.Pass()); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 } // namespace | |
| 131 | |
| 132 namespace automation_util { | |
| 133 | |
| 134 void DispatchAccessibilityEventsToAutomation( | |
| 135 const std::vector<content::AXEventNotificationDetails>& details, | |
| 136 content::BrowserContext* browser_context, | |
| 137 const gfx::Vector2d& location_offset) { | |
| 138 using api::automation_internal::AXEventParams; | |
| 139 using api::automation_internal::AXTreeUpdate; | |
| 140 | |
| 141 std::vector<content::AXEventNotificationDetails>::const_iterator iter = | |
| 142 details.begin(); | |
| 143 for (; iter != details.end(); ++iter) { | |
| 144 const content::AXEventNotificationDetails& event = *iter; | |
| 145 | |
| 146 AXEventParams ax_event_params; | |
| 147 ax_event_params.tree_id = | |
| 148 AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID(event.process_id, | |
| 149 event.routing_id); | |
| 150 ax_event_params.event_type = ToString(iter->event_type); | |
| 151 ax_event_params.target_id = event.id; | |
| 152 | |
| 153 AXTreeUpdate& ax_tree_update = ax_event_params.update; | |
| 154 ax_tree_update.node_id_to_clear = event.node_id_to_clear; | |
| 155 for (size_t i = 0; i < event.nodes.size(); ++i) { | |
| 156 ui::AXNodeData src = event.nodes[i]; | |
| 157 src.location.Offset(location_offset); | |
| 158 linked_ptr<api::automation_internal::AXNodeData> out_node( | |
| 159 new api::automation_internal::AXNodeData()); | |
| 160 PopulateNodeData(src, out_node); | |
| 161 if (src.HasBoolAttribute(ui::AX_ATTR_IS_AX_TREE_HOST)) { | |
| 162 const auto& iter = event.node_to_browser_plugin_instance_id_map.find( | |
| 163 src.id); | |
| 164 if (iter != event.node_to_browser_plugin_instance_id_map.end()) { | |
| 165 int instance_id = iter->second; | |
| 166 content::BrowserPluginGuestManager* guest_manager = | |
| 167 browser_context->GetGuestManager(); | |
| 168 content::WebContents* guest_web_contents = | |
| 169 guest_manager->GetGuestByInstanceID(event.process_id, | |
| 170 instance_id); | |
| 171 if (guest_web_contents) { | |
| 172 content::RenderFrameHost* guest_rfh = | |
| 173 guest_web_contents->GetMainFrame(); | |
| 174 int guest_tree_id = | |
| 175 AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | |
| 176 guest_rfh->GetProcess()->GetID(), | |
| 177 guest_rfh->GetRoutingID()); | |
| 178 out_node->int_attributes->additional_properties.SetInteger( | |
| 179 ToString(ui::AX_ATTR_CHILD_TREE_ID), | |
| 180 guest_tree_id); | |
| 181 } | |
| 182 } | |
| 183 } | |
| 184 ax_tree_update.nodes.push_back(out_node); | |
| 185 } | |
| 186 | |
| 187 // TODO(dtseng/aboxhall): Why are we sending only one update at a time? We | |
| 188 // should match the behavior from renderer -> browser and send a | |
| 189 // collection of tree updates over (to the extension); see | |
| 190 // |AccessibilityHostMsg_EventParams| and |AccessibilityHostMsg_Events|. | |
| 191 DispatchEventInternal( | |
| 192 browser_context, events::AUTOMATION_INTERNAL_ON_ACCESSIBILITY_EVENT, | |
| 193 api::automation_internal::OnAccessibilityEvent::kEventName, | |
| 194 api::automation_internal::OnAccessibilityEvent::Create( | |
| 195 ax_event_params)); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 void DispatchTreeDestroyedEventToAutomation( | |
| 200 int process_id, | |
| 201 int routing_id, | |
| 202 content::BrowserContext* browser_context) { | |
| 203 int tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | |
| 204 process_id, routing_id); | |
| 205 DispatchEventInternal( | |
| 206 browser_context, | |
| 207 events::AUTOMATION_INTERNAL_ON_ACCESSIBILITY_TREE_DESTROYED, | |
| 208 api::automation_internal::OnAccessibilityTreeDestroyed::kEventName, | |
| 209 api::automation_internal::OnAccessibilityTreeDestroyed::Create(tree_id)); | |
| 210 AXTreeIDRegistry::GetInstance()->RemoveAXTreeID(tree_id); | |
| 211 } | |
| 212 | |
| 213 } // namespace automation_util | |
| 214 | |
| 215 } // namespace extensions | |
| OLD | NEW |