| 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" |
| 15 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 16 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 16 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 20 #include "chrome/common/extensions/api/automation_internal.h" | 21 #include "chrome/common/extensions/api/automation_internal.h" |
| 21 #include "chrome/common/extensions/chrome_extension_messages.h" | 22 #include "chrome/common/extensions/chrome_extension_messages.h" |
| 22 #include "chrome/common/extensions/manifest_handlers/automation.h" | 23 #include "chrome/common/extensions/manifest_handlers/automation.h" |
| 23 #include "content/public/browser/ax_event_notification_details.h" | 24 #include "content/public/browser/ax_event_notification_details.h" |
| 24 #include "content/public/browser/browser_accessibility_state.h" | 25 #include "content/public/browser/browser_accessibility_state.h" |
| 25 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
| 26 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
| 27 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 28 #include "content/public/browser/render_widget_host.h" | 29 #include "content/public/browser/render_widget_host.h" |
| 29 #include "content/public/browser/render_widget_host_view.h" | 30 #include "content/public/browser/render_widget_host_view.h" |
| 30 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/browser/web_contents_observer.h" | |
| 32 #include "content/public/browser/web_contents_user_data.h" | |
| 33 #include "extensions/common/extension_messages.h" | 32 #include "extensions/common/extension_messages.h" |
| 34 #include "extensions/common/permissions/permissions_data.h" | 33 #include "extensions/common/permissions/permissions_data.h" |
| 35 | 34 |
| 36 #if defined(USE_AURA) | 35 #if defined(USE_AURA) |
| 37 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" | 36 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" |
| 38 #endif | 37 #endif |
| 39 | 38 |
| 40 namespace extensions { | 39 namespace extensions { |
| 41 class AutomationWebContentsObserver; | 40 class AutomationWebContentsObserver; |
| 42 } // namespace extensions | 41 } // namespace extensions |
| 43 | 42 |
| 44 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver); | 43 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver); |
| 45 | 44 |
| 46 namespace extensions { | 45 namespace extensions { |
| 47 | 46 |
| 48 namespace { | 47 namespace { |
| 49 | |
| 50 const int kDesktopTreeID = 0; | 48 const int kDesktopTreeID = 0; |
| 51 const char kCannotRequestAutomationOnPage[] = | 49 const char kCannotRequestAutomationOnPage[] = |
| 52 "Cannot request automation tree on url \"*\". " | 50 "Cannot request automation tree on url \"*\". " |
| 53 "Extension manifest must request permission to access this host."; | 51 "Extension manifest must request permission to access this host."; |
| 54 const char kRendererDestroyed[] = "The tab was closed."; | 52 const char kRendererDestroyed[] = "The tab was closed."; |
| 55 const char kNoMainFrame[] = "No main frame."; | 53 const char kNoMainFrame[] = "No main frame."; |
| 56 const char kNoDocument[] = "No document."; | 54 const char kNoDocument[] = "No document."; |
| 57 const char kNodeDestroyed[] = | 55 const char kNodeDestroyed[] = |
| 58 "domQuerySelector sent on node which is no longer in the tree."; | 56 "domQuerySelector sent on node which is no longer in the tree."; |
| 59 | 57 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 class AutomationWebContentsObserver | 186 class AutomationWebContentsObserver |
| 189 : public content::WebContentsObserver, | 187 : public content::WebContentsObserver, |
| 190 public content::WebContentsUserData<AutomationWebContentsObserver> { | 188 public content::WebContentsUserData<AutomationWebContentsObserver> { |
| 191 public: | 189 public: |
| 192 ~AutomationWebContentsObserver() override {} | 190 ~AutomationWebContentsObserver() override {} |
| 193 | 191 |
| 194 // content::WebContentsObserver overrides. | 192 // content::WebContentsObserver overrides. |
| 195 void AccessibilityEventReceived( | 193 void AccessibilityEventReceived( |
| 196 const std::vector<content::AXEventNotificationDetails>& details) | 194 const std::vector<content::AXEventNotificationDetails>& details) |
| 197 override { | 195 override { |
| 198 std::vector<content::AXEventNotificationDetails>::const_iterator iter = | 196 automation_util::DispatchAccessibilityEventsToAutomation( |
| 199 details.begin(); | 197 details, browser_context_, |
| 200 for (; iter != details.end(); ++iter) { | 198 web_contents()->GetContainerBounds().OffsetFromOrigin()); |
| 201 const content::AXEventNotificationDetails& event = *iter; | |
| 202 int tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | |
| 203 event.process_id, event.routing_id); | |
| 204 ExtensionMsg_AccessibilityEventParams params; | |
| 205 params.tree_id = tree_id; | |
| 206 params.id = event.id; | |
| 207 params.event_type = event.event_type; | |
| 208 params.update.node_id_to_clear = event.node_id_to_clear; | |
| 209 params.update.nodes = event.nodes; | |
| 210 params.location_offset = | |
| 211 web_contents()->GetContainerBounds().OffsetFromOrigin(); | |
| 212 | |
| 213 AutomationEventRouter* router = AutomationEventRouter::GetInstance(); | |
| 214 router->DispatchAccessibilityEvent(params); | |
| 215 } | |
| 216 } | 199 } |
| 217 | 200 |
| 218 void RenderFrameDeleted( | 201 void RenderFrameDeleted( |
| 219 content::RenderFrameHost* render_frame_host) override { | 202 content::RenderFrameHost* render_frame_host) override { |
| 220 int tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | 203 automation_util::DispatchTreeDestroyedEventToAutomation( |
| 221 render_frame_host->GetProcess()->GetID(), | 204 render_frame_host->GetProcess()->GetID(), |
| 222 render_frame_host->GetRoutingID()); | 205 render_frame_host->GetRoutingID(), |
| 223 AXTreeIDRegistry::GetInstance()->RemoveAXTreeID(tree_id); | |
| 224 AutomationEventRouter::GetInstance()->DispatchTreeDestroyedEvent( | |
| 225 tree_id, | |
| 226 browser_context_); | 206 browser_context_); |
| 227 } | 207 } |
| 228 | 208 |
| 229 private: | 209 private: |
| 230 friend class content::WebContentsUserData<AutomationWebContentsObserver>; | 210 friend class content::WebContentsUserData<AutomationWebContentsObserver>; |
| 231 | 211 |
| 232 explicit AutomationWebContentsObserver(content::WebContents* web_contents) | 212 AutomationWebContentsObserver( |
| 213 content::WebContents* web_contents) |
| 233 : content::WebContentsObserver(web_contents), | 214 : content::WebContentsObserver(web_contents), |
| 234 browser_context_(web_contents->GetBrowserContext()) {} | 215 browser_context_(web_contents->GetBrowserContext()) {} |
| 235 | 216 |
| 236 content::BrowserContext* browser_context_; | 217 content::BrowserContext* browser_context_; |
| 237 | 218 |
| 238 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver); | 219 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver); |
| 239 }; | 220 }; |
| 240 | 221 |
| 241 ExtensionFunction::ResponseAction | 222 ExtensionFunction::ResponseAction |
| 242 AutomationInternalEnableTabFunction::Run() { | 223 AutomationInternalEnableTabFunction::Run() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 257 &contents, | 238 &contents, |
| 258 NULL /* tab_index out param */)) { | 239 NULL /* tab_index out param */)) { |
| 259 return RespondNow( | 240 return RespondNow( |
| 260 Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id))); | 241 Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id))); |
| 261 } | 242 } |
| 262 } else { | 243 } else { |
| 263 contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents(); | 244 contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents(); |
| 264 if (!contents) | 245 if (!contents) |
| 265 return RespondNow(Error("No active tab")); | 246 return RespondNow(Error("No active tab")); |
| 266 } | 247 } |
| 267 | |
| 268 content::RenderFrameHost* rfh = contents->GetMainFrame(); | 248 content::RenderFrameHost* rfh = contents->GetMainFrame(); |
| 269 if (!rfh) | 249 if (!rfh) |
| 270 return RespondNow(Error("Could not enable accessibility for active tab")); | 250 return RespondNow(Error("Could not enable accessibility for active tab")); |
| 271 | 251 |
| 272 if (!CanRequestAutomation(extension(), automation_info, contents)) { | 252 if (!CanRequestAutomation(extension(), automation_info, contents)) { |
| 273 return RespondNow( | 253 return RespondNow( |
| 274 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); | 254 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); |
| 275 } | 255 } |
| 276 | 256 |
| 277 AutomationWebContentsObserver::CreateForWebContents(contents); | 257 AutomationWebContentsObserver::CreateForWebContents(contents); |
| 278 contents->EnableTreeOnlyAccessibilityMode(); | 258 contents->EnableTreeOnlyAccessibilityMode(); |
| 279 | |
| 280 int ax_tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | 259 int ax_tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( |
| 281 rfh->GetProcess()->GetID(), rfh->GetRoutingID()); | 260 rfh->GetProcess()->GetID(), rfh->GetRoutingID()); |
| 282 | 261 |
| 283 // This gets removed when the extension process dies. | 262 // This gets removed when the extension process dies. |
| 284 AutomationEventRouter::GetInstance()->RegisterListenerForOneTree( | 263 AutomationEventRouter::GetInstance()->RegisterListenerForOneTree( |
| 285 source_process_id(), | 264 source_process_id(), |
| 286 params->args.routing_id, | 265 params->args.routing_id, |
| 287 ax_tree_id); | 266 ax_tree_id); |
| 288 | 267 |
| 289 return RespondNow(ArgumentList( | 268 return RespondNow(ArgumentList( |
| 290 api::automation_internal::EnableTab::Results::Create(ax_tree_id))); | 269 api::automation_internal::EnableTab::Results::Create(ax_tree_id))); |
| 291 } | 270 } |
| 292 | 271 |
| 293 ExtensionFunction::ResponseAction AutomationInternalEnableFrameFunction::Run() { | 272 ExtensionFunction::ResponseAction AutomationInternalEnableFrameFunction::Run() { |
| 294 // TODO(dtseng): Limited to desktop tree for now pending out of proc iframes. | 273 // TODO(dtseng): Limited to desktop tree for now pending out of proc iframes. |
| 295 using api::automation_internal::EnableFrame::Params; | 274 using api::automation_internal::EnableFrame::Params; |
| 296 | 275 |
| 297 scoped_ptr<Params> params(Params::Create(*args_)); | 276 scoped_ptr<Params> params(Params::Create(*args_)); |
| 298 EXTENSION_FUNCTION_VALIDATE(params.get()); | 277 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 299 | |
| 300 AXTreeIDRegistry::FrameID frame_id = | 278 AXTreeIDRegistry::FrameID frame_id = |
| 301 AXTreeIDRegistry::GetInstance()->GetFrameID(params->tree_id); | 279 AXTreeIDRegistry::GetInstance()->GetFrameID(params->tree_id); |
| 302 content::RenderFrameHost* rfh = | 280 content::RenderFrameHost* rfh = |
| 303 content::RenderFrameHost::FromID(frame_id.first, frame_id.second); | 281 content::RenderFrameHost::FromID(frame_id.first, frame_id.second); |
| 304 if (!rfh) | 282 if (!rfh) |
| 305 return RespondNow(Error("unable to load tab")); | 283 return RespondNow(Error("unable to load tab")); |
| 306 | 284 |
| 307 content::WebContents* contents = | 285 content::WebContents* contents = |
| 308 content::WebContents::FromRenderFrameHost(rfh); | 286 content::WebContents::FromRenderFrameHost(rfh); |
| 309 AutomationWebContentsObserver::CreateForWebContents(contents); | 287 AutomationWebContentsObserver::CreateForWebContents(contents); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 int result_acc_obj_id) { | 427 int result_acc_obj_id) { |
| 450 if (!error.empty()) { | 428 if (!error.empty()) { |
| 451 Respond(Error(error)); | 429 Respond(Error(error)); |
| 452 return; | 430 return; |
| 453 } | 431 } |
| 454 | 432 |
| 455 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id))); | 433 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id))); |
| 456 } | 434 } |
| 457 | 435 |
| 458 } // namespace extensions | 436 } // namespace extensions |
| OLD | NEW |