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