| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/automation_internal/automation_internal_
api.h" | 5 #include "chrome/browser/extensions/api/automation_internal/automation_internal_
api.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/accessibility/ax_tree_id_registry.h" | 12 #include "chrome/browser/accessibility/ax_tree_id_registry.h" |
| 13 #include "chrome/browser/extensions/api/automation_internal/automation_action_ad
apter.h" | 13 #include "chrome/browser/extensions/api/automation_internal/automation_action_ad
apter.h" |
| 14 #include "chrome/browser/extensions/api/automation_internal/automation_event_rou
ter.h" | 14 #include "chrome/browser/extensions/api/automation_internal/automation_event_rou
ter.h" |
| 15 #include "chrome/browser/extensions/api/automation_internal/automation_util.h" | |
| 16 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 15 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 17 #include "chrome/browser/extensions/extension_tab_util.h" | 16 #include "chrome/browser/extensions/extension_tab_util.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/common/extensions/api/automation_internal.h" | 20 #include "chrome/common/extensions/api/automation_internal.h" |
| 22 #include "chrome/common/extensions/chrome_extension_messages.h" | 21 #include "chrome/common/extensions/chrome_extension_messages.h" |
| 23 #include "chrome/common/extensions/manifest_handlers/automation.h" | 22 #include "chrome/common/extensions/manifest_handlers/automation.h" |
| 24 #include "content/public/browser/ax_event_notification_details.h" | 23 #include "content/public/browser/ax_event_notification_details.h" |
| 25 #include "content/public/browser/browser_accessibility_state.h" | 24 #include "content/public/browser/browser_accessibility_state.h" |
| 25 #include "content/public/browser/browser_context.h" |
| 26 #include "content/public/browser/browser_plugin_guest_manager.h" |
| 26 #include "content/public/browser/render_frame_host.h" | 27 #include "content/public/browser/render_frame_host.h" |
| 27 #include "content/public/browser/render_process_host.h" | 28 #include "content/public/browser/render_process_host.h" |
| 28 #include "content/public/browser/render_view_host.h" | 29 #include "content/public/browser/render_view_host.h" |
| 29 #include "content/public/browser/render_widget_host.h" | 30 #include "content/public/browser/render_widget_host.h" |
| 30 #include "content/public/browser/render_widget_host_view.h" | 31 #include "content/public/browser/render_widget_host_view.h" |
| 31 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/browser/web_contents_observer.h" |
| 34 #include "content/public/browser/web_contents_user_data.h" |
| 32 #include "extensions/common/extension_messages.h" | 35 #include "extensions/common/extension_messages.h" |
| 33 #include "extensions/common/permissions/permissions_data.h" | 36 #include "extensions/common/permissions/permissions_data.h" |
| 34 | 37 |
| 35 #if defined(USE_AURA) | 38 #if defined(USE_AURA) |
| 36 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" | 39 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" |
| 37 #endif | 40 #endif |
| 38 | 41 |
| 39 namespace extensions { | 42 namespace extensions { |
| 40 class AutomationWebContentsObserver; | 43 class AutomationWebContentsObserver; |
| 41 } // namespace extensions | 44 } // namespace extensions |
| 42 | 45 |
| 43 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver); | 46 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver); |
| 44 | 47 |
| 45 namespace extensions { | 48 namespace extensions { |
| 46 | 49 |
| 47 namespace { | 50 namespace { |
| 51 |
| 48 const int kDesktopTreeID = 0; | 52 const int kDesktopTreeID = 0; |
| 49 const char kCannotRequestAutomationOnPage[] = | 53 const char kCannotRequestAutomationOnPage[] = |
| 50 "Cannot request automation tree on url \"*\". " | 54 "Cannot request automation tree on url \"*\". " |
| 51 "Extension manifest must request permission to access this host."; | 55 "Extension manifest must request permission to access this host."; |
| 52 const char kRendererDestroyed[] = "The tab was closed."; | 56 const char kRendererDestroyed[] = "The tab was closed."; |
| 53 const char kNoMainFrame[] = "No main frame."; | 57 const char kNoMainFrame[] = "No main frame."; |
| 54 const char kNoDocument[] = "No document."; | 58 const char kNoDocument[] = "No document."; |
| 55 const char kNodeDestroyed[] = | 59 const char kNodeDestroyed[] = |
| 56 "domQuerySelector sent on node which is no longer in the tree."; | 60 "domQuerySelector sent on node which is no longer in the tree."; |
| 57 | 61 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 class AutomationWebContentsObserver | 190 class AutomationWebContentsObserver |
| 187 : public content::WebContentsObserver, | 191 : public content::WebContentsObserver, |
| 188 public content::WebContentsUserData<AutomationWebContentsObserver> { | 192 public content::WebContentsUserData<AutomationWebContentsObserver> { |
| 189 public: | 193 public: |
| 190 ~AutomationWebContentsObserver() override {} | 194 ~AutomationWebContentsObserver() override {} |
| 191 | 195 |
| 192 // content::WebContentsObserver overrides. | 196 // content::WebContentsObserver overrides. |
| 193 void AccessibilityEventReceived( | 197 void AccessibilityEventReceived( |
| 194 const std::vector<content::AXEventNotificationDetails>& details) | 198 const std::vector<content::AXEventNotificationDetails>& details) |
| 195 override { | 199 override { |
| 196 automation_util::DispatchAccessibilityEventsToAutomation( | 200 std::vector<content::AXEventNotificationDetails>::const_iterator iter = |
| 197 details, browser_context_, | 201 details.begin(); |
| 198 web_contents()->GetContainerBounds().OffsetFromOrigin()); | 202 for (; iter != details.end(); ++iter) { |
| 203 const content::AXEventNotificationDetails& event = *iter; |
| 204 int tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( |
| 205 event.process_id, event.routing_id); |
| 206 ExtensionMsg_AccessibilityEventParams params; |
| 207 params.tree_id = tree_id; |
| 208 params.id = event.id; |
| 209 params.event_type = event.event_type; |
| 210 params.update.node_id_to_clear = event.node_id_to_clear; |
| 211 params.update.nodes = event.nodes; |
| 212 params.location_offset = |
| 213 web_contents()->GetContainerBounds().OffsetFromOrigin(); |
| 214 |
| 215 for (size_t i = 0; i < params.update.nodes.size(); ++i) { |
| 216 ui::AXNodeData& node = params.update.nodes[i]; |
| 217 if (node.HasBoolAttribute(ui::AX_ATTR_IS_AX_TREE_HOST)) { |
| 218 const auto& iter = event.node_to_browser_plugin_instance_id_map.find( |
| 219 node.id); |
| 220 if (iter != event.node_to_browser_plugin_instance_id_map.end()) { |
| 221 int instance_id = iter->second; |
| 222 content::BrowserPluginGuestManager* guest_manager = |
| 223 browser_context_->GetGuestManager(); |
| 224 content::WebContents* guest_web_contents = |
| 225 guest_manager->GetGuestByInstanceID(event.process_id, |
| 226 instance_id); |
| 227 if (guest_web_contents) { |
| 228 content::RenderFrameHost* guest_rfh = |
| 229 guest_web_contents->GetMainFrame(); |
| 230 int guest_tree_id = |
| 231 AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( |
| 232 guest_rfh->GetProcess()->GetID(), |
| 233 guest_rfh->GetRoutingID()); |
| 234 node.AddIntAttribute(ui::AX_ATTR_CHILD_TREE_ID, guest_tree_id); |
| 235 } |
| 236 } |
| 237 } |
| 238 } |
| 239 |
| 240 AutomationEventRouter* router = AutomationEventRouter::GetInstance(); |
| 241 router->DispatchAccessibilityEvent(params); |
| 242 } |
| 199 } | 243 } |
| 200 | 244 |
| 201 void RenderFrameDeleted( | 245 void RenderFrameDeleted( |
| 202 content::RenderFrameHost* render_frame_host) override { | 246 content::RenderFrameHost* render_frame_host) override { |
| 203 automation_util::DispatchTreeDestroyedEventToAutomation( | 247 int tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( |
| 204 render_frame_host->GetProcess()->GetID(), | 248 render_frame_host->GetProcess()->GetID(), |
| 205 render_frame_host->GetRoutingID(), | 249 render_frame_host->GetRoutingID()); |
| 250 AXTreeIDRegistry::GetInstance()->RemoveAXTreeID(tree_id); |
| 251 AutomationEventRouter::GetInstance()->DispatchTreeDestroyedEvent( |
| 252 tree_id, |
| 206 browser_context_); | 253 browser_context_); |
| 207 } | 254 } |
| 208 | 255 |
| 209 private: | 256 private: |
| 210 friend class content::WebContentsUserData<AutomationWebContentsObserver>; | 257 friend class content::WebContentsUserData<AutomationWebContentsObserver>; |
| 211 | 258 |
| 212 AutomationWebContentsObserver( | 259 explicit AutomationWebContentsObserver(content::WebContents* web_contents) |
| 213 content::WebContents* web_contents) | |
| 214 : content::WebContentsObserver(web_contents), | 260 : content::WebContentsObserver(web_contents), |
| 215 browser_context_(web_contents->GetBrowserContext()) {} | 261 browser_context_(web_contents->GetBrowserContext()) {} |
| 216 | 262 |
| 217 content::BrowserContext* browser_context_; | 263 content::BrowserContext* browser_context_; |
| 218 | 264 |
| 219 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver); | 265 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver); |
| 220 }; | 266 }; |
| 221 | 267 |
| 222 ExtensionFunction::ResponseAction | 268 ExtensionFunction::ResponseAction |
| 223 AutomationInternalEnableTabFunction::Run() { | 269 AutomationInternalEnableTabFunction::Run() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 238 &contents, | 284 &contents, |
| 239 NULL /* tab_index out param */)) { | 285 NULL /* tab_index out param */)) { |
| 240 return RespondNow( | 286 return RespondNow( |
| 241 Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id))); | 287 Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id))); |
| 242 } | 288 } |
| 243 } else { | 289 } else { |
| 244 contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents(); | 290 contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents(); |
| 245 if (!contents) | 291 if (!contents) |
| 246 return RespondNow(Error("No active tab")); | 292 return RespondNow(Error("No active tab")); |
| 247 } | 293 } |
| 294 |
| 248 content::RenderFrameHost* rfh = contents->GetMainFrame(); | 295 content::RenderFrameHost* rfh = contents->GetMainFrame(); |
| 249 if (!rfh) | 296 if (!rfh) |
| 250 return RespondNow(Error("Could not enable accessibility for active tab")); | 297 return RespondNow(Error("Could not enable accessibility for active tab")); |
| 251 | 298 |
| 252 if (!CanRequestAutomation(extension(), automation_info, contents)) { | 299 if (!CanRequestAutomation(extension(), automation_info, contents)) { |
| 253 return RespondNow( | 300 return RespondNow( |
| 254 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); | 301 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); |
| 255 } | 302 } |
| 256 | 303 |
| 257 AutomationWebContentsObserver::CreateForWebContents(contents); | 304 AutomationWebContentsObserver::CreateForWebContents(contents); |
| 258 contents->EnableTreeOnlyAccessibilityMode(); | 305 contents->EnableTreeOnlyAccessibilityMode(); |
| 306 |
| 259 int ax_tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | 307 int ax_tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( |
| 260 rfh->GetProcess()->GetID(), rfh->GetRoutingID()); | 308 rfh->GetProcess()->GetID(), rfh->GetRoutingID()); |
| 261 | 309 |
| 262 // This gets removed when the extension process dies. | 310 // This gets removed when the extension process dies. |
| 263 AutomationEventRouter::GetInstance()->RegisterListenerForOneTree( | 311 AutomationEventRouter::GetInstance()->RegisterListenerForOneTree( |
| 264 source_process_id(), | 312 source_process_id(), |
| 265 params->args.routing_id, | 313 params->args.routing_id, |
| 266 ax_tree_id); | 314 ax_tree_id); |
| 267 | 315 |
| 268 return RespondNow(ArgumentList( | 316 return RespondNow(ArgumentList( |
| 269 api::automation_internal::EnableTab::Results::Create(ax_tree_id))); | 317 api::automation_internal::EnableTab::Results::Create(ax_tree_id))); |
| 270 } | 318 } |
| 271 | 319 |
| 272 ExtensionFunction::ResponseAction AutomationInternalEnableFrameFunction::Run() { | 320 ExtensionFunction::ResponseAction AutomationInternalEnableFrameFunction::Run() { |
| 273 // TODO(dtseng): Limited to desktop tree for now pending out of proc iframes. | 321 // TODO(dtseng): Limited to desktop tree for now pending out of proc iframes. |
| 274 using api::automation_internal::EnableFrame::Params; | 322 using api::automation_internal::EnableFrame::Params; |
| 275 | 323 |
| 276 scoped_ptr<Params> params(Params::Create(*args_)); | 324 scoped_ptr<Params> params(Params::Create(*args_)); |
| 277 EXTENSION_FUNCTION_VALIDATE(params.get()); | 325 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 326 |
| 278 AXTreeIDRegistry::FrameID frame_id = | 327 AXTreeIDRegistry::FrameID frame_id = |
| 279 AXTreeIDRegistry::GetInstance()->GetFrameID(params->tree_id); | 328 AXTreeIDRegistry::GetInstance()->GetFrameID(params->tree_id); |
| 280 content::RenderFrameHost* rfh = | 329 content::RenderFrameHost* rfh = |
| 281 content::RenderFrameHost::FromID(frame_id.first, frame_id.second); | 330 content::RenderFrameHost::FromID(frame_id.first, frame_id.second); |
| 282 if (!rfh) | 331 if (!rfh) |
| 283 return RespondNow(Error("unable to load tab")); | 332 return RespondNow(Error("unable to load tab")); |
| 284 | 333 |
| 285 content::WebContents* contents = | 334 content::WebContents* contents = |
| 286 content::WebContents::FromRenderFrameHost(rfh); | 335 content::WebContents::FromRenderFrameHost(rfh); |
| 287 AutomationWebContentsObserver::CreateForWebContents(contents); | 336 AutomationWebContentsObserver::CreateForWebContents(contents); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 int result_acc_obj_id) { | 476 int result_acc_obj_id) { |
| 428 if (!error.empty()) { | 477 if (!error.empty()) { |
| 429 Respond(Error(error)); | 478 Respond(Error(error)); |
| 430 return; | 479 return; |
| 431 } | 480 } |
| 432 | 481 |
| 433 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id))); | 482 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id))); |
| 434 } | 483 } |
| 435 | 484 |
| 436 } // namespace extensions | 485 } // namespace extensions |
| OLD | NEW |