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/automation_internal/automation_web_conte
nts_observer.h" |
16 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 16 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
17 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
21 #include "chrome/common/extensions/api/automation_internal.h" | 21 #include "chrome/common/extensions/api/automation_internal.h" |
22 #include "chrome/common/extensions/chrome_extension_messages.h" | 22 #include "chrome/common/extensions/chrome_extension_messages.h" |
23 #include "chrome/common/extensions/manifest_handlers/automation.h" | 23 #include "chrome/common/extensions/manifest_handlers/automation.h" |
24 #include "content/public/browser/ax_event_notification_details.h" | 24 #include "content/public/browser/ax_event_notification_details.h" |
25 #include "content/public/browser/browser_accessibility_state.h" | 25 #include "content/public/browser/browser_accessibility_state.h" |
26 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
27 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
28 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
29 #include "content/public/browser/render_widget_host.h" | 29 #include "content/public/browser/render_widget_host.h" |
30 #include "content/public/browser/render_widget_host_view.h" | 30 #include "content/public/browser/render_widget_host_view.h" |
31 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
32 #include "extensions/common/extension_messages.h" | 32 #include "extensions/common/extension_messages.h" |
33 #include "extensions/common/permissions/permissions_data.h" | 33 #include "extensions/common/permissions/permissions_data.h" |
34 | 34 |
35 #if defined(USE_AURA) | 35 #if defined(USE_AURA) |
36 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" | 36 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" |
37 #endif | 37 #endif |
38 | 38 |
39 namespace extensions { | 39 namespace extensions { |
40 class AutomationWebContentsObserver; | |
41 } // namespace extensions | |
42 | |
43 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver); | |
44 | |
45 namespace extensions { | |
46 | 40 |
47 namespace { | 41 namespace { |
| 42 |
48 const int kDesktopTreeID = 0; | 43 const int kDesktopTreeID = 0; |
49 const char kCannotRequestAutomationOnPage[] = | 44 const char kCannotRequestAutomationOnPage[] = |
50 "Cannot request automation tree on url \"*\". " | 45 "Cannot request automation tree on url \"*\". " |
51 "Extension manifest must request permission to access this host."; | 46 "Extension manifest must request permission to access this host."; |
52 const char kRendererDestroyed[] = "The tab was closed."; | 47 const char kRendererDestroyed[] = "The tab was closed."; |
53 const char kNoMainFrame[] = "No main frame."; | 48 const char kNoMainFrame[] = "No main frame."; |
54 const char kNoDocument[] = "No document."; | 49 const char kNoDocument[] = "No document."; |
55 const char kNodeDestroyed[] = | 50 const char kNodeDestroyed[] = |
56 "domQuerySelector sent on node which is no longer in the tree."; | 51 "domQuerySelector sent on node which is no longer in the tree."; |
57 | 52 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } | 166 } |
172 | 167 |
173 private: | 168 private: |
174 content::RenderFrameHost* rfh_; | 169 content::RenderFrameHost* rfh_; |
175 | 170 |
176 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostActionAdapter); | 171 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostActionAdapter); |
177 }; | 172 }; |
178 | 173 |
179 } // namespace | 174 } // namespace |
180 | 175 |
181 // Helper class that receives accessibility data from |WebContents|. | |
182 class AutomationWebContentsObserver | |
183 : public content::WebContentsObserver, | |
184 public content::WebContentsUserData<AutomationWebContentsObserver> { | |
185 public: | |
186 ~AutomationWebContentsObserver() override {} | |
187 | |
188 // content::WebContentsObserver overrides. | |
189 void AccessibilityEventReceived( | |
190 const std::vector<content::AXEventNotificationDetails>& details) | |
191 override { | |
192 automation_util::DispatchAccessibilityEventsToAutomation( | |
193 details, browser_context_, | |
194 web_contents()->GetContainerBounds().OffsetFromOrigin()); | |
195 } | |
196 | |
197 void RenderFrameDeleted( | |
198 content::RenderFrameHost* render_frame_host) override { | |
199 automation_util::DispatchTreeDestroyedEventToAutomation( | |
200 render_frame_host->GetProcess()->GetID(), | |
201 render_frame_host->GetRoutingID(), | |
202 browser_context_); | |
203 } | |
204 | |
205 private: | |
206 friend class content::WebContentsUserData<AutomationWebContentsObserver>; | |
207 | |
208 AutomationWebContentsObserver( | |
209 content::WebContents* web_contents) | |
210 : content::WebContentsObserver(web_contents), | |
211 browser_context_(web_contents->GetBrowserContext()) {} | |
212 | |
213 content::BrowserContext* browser_context_; | |
214 | |
215 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver); | |
216 }; | |
217 | |
218 ExtensionFunction::ResponseAction | 176 ExtensionFunction::ResponseAction |
219 AutomationInternalEnableTabFunction::Run() { | 177 AutomationInternalEnableTabFunction::Run() { |
220 const AutomationInfo* automation_info = AutomationInfo::Get(extension()); | 178 const AutomationInfo* automation_info = AutomationInfo::Get(extension()); |
221 EXTENSION_FUNCTION_VALIDATE(automation_info); | 179 EXTENSION_FUNCTION_VALIDATE(automation_info); |
222 | 180 |
223 using api::automation_internal::EnableTab::Params; | 181 using api::automation_internal::EnableTab::Params; |
224 scoped_ptr<Params> params(Params::Create(*args_)); | 182 scoped_ptr<Params> params(Params::Create(*args_)); |
225 EXTENSION_FUNCTION_VALIDATE(params.get()); | 183 EXTENSION_FUNCTION_VALIDATE(params.get()); |
226 content::WebContents* contents = NULL; | 184 content::WebContents* contents = NULL; |
227 if (params->args.tab_id.get()) { | 185 if (params->args.tab_id.get()) { |
228 int tab_id = *params->args.tab_id; | 186 int tab_id = *params->args.tab_id; |
229 if (!ExtensionTabUtil::GetTabById(tab_id, | 187 if (!ExtensionTabUtil::GetTabById(tab_id, |
230 GetProfile(), | 188 GetProfile(), |
231 include_incognito(), | 189 include_incognito(), |
232 NULL, /* browser out param*/ | 190 NULL, /* browser out param*/ |
233 NULL, /* tab_strip out param */ | 191 NULL, /* tab_strip out param */ |
234 &contents, | 192 &contents, |
235 NULL /* tab_index out param */)) { | 193 NULL /* tab_index out param */)) { |
236 return RespondNow( | 194 return RespondNow( |
237 Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id))); | 195 Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id))); |
238 } | 196 } |
239 } else { | 197 } else { |
240 contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents(); | 198 contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents(); |
241 if (!contents) | 199 if (!contents) |
242 return RespondNow(Error("No active tab")); | 200 return RespondNow(Error("No active tab")); |
243 } | 201 } |
| 202 |
244 content::RenderFrameHost* rfh = contents->GetMainFrame(); | 203 content::RenderFrameHost* rfh = contents->GetMainFrame(); |
245 if (!rfh) | 204 if (!rfh) |
246 return RespondNow(Error("Could not enable accessibility for active tab")); | 205 return RespondNow(Error("Could not enable accessibility for active tab")); |
247 | 206 |
248 if (!CanRequestAutomation(extension(), automation_info, contents)) { | 207 if (!CanRequestAutomation(extension(), automation_info, contents)) { |
249 return RespondNow( | 208 return RespondNow( |
250 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); | 209 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); |
251 } | 210 } |
252 | 211 |
253 AutomationWebContentsObserver::CreateForWebContents(contents); | 212 AutomationWebContentsObserver::CreateForWebContents(contents); |
254 contents->EnableTreeOnlyAccessibilityMode(); | 213 contents->EnableTreeOnlyAccessibilityMode(); |
| 214 |
255 int ax_tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | 215 int ax_tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( |
256 rfh->GetProcess()->GetID(), rfh->GetRoutingID()); | 216 rfh->GetProcess()->GetID(), rfh->GetRoutingID()); |
257 | 217 |
258 // This gets removed when the extension process dies. | 218 // This gets removed when the extension process dies. |
259 AutomationEventRouter::GetInstance()->RegisterListenerForOneTree( | 219 AutomationEventRouter::GetInstance()->RegisterListenerForOneTree( |
260 source_process_id(), | 220 source_process_id(), |
261 params->args.routing_id, | 221 params->args.routing_id, |
262 ax_tree_id); | 222 ax_tree_id); |
263 | 223 |
264 return RespondNow(ArgumentList( | 224 return RespondNow(ArgumentList( |
265 api::automation_internal::EnableTab::Results::Create(ax_tree_id))); | 225 api::automation_internal::EnableTab::Results::Create(ax_tree_id))); |
266 } | 226 } |
267 | 227 |
268 ExtensionFunction::ResponseAction AutomationInternalEnableFrameFunction::Run() { | 228 ExtensionFunction::ResponseAction AutomationInternalEnableFrameFunction::Run() { |
269 // TODO(dtseng): Limited to desktop tree for now pending out of proc iframes. | 229 // TODO(dtseng): Limited to desktop tree for now pending out of proc iframes. |
270 using api::automation_internal::EnableFrame::Params; | 230 using api::automation_internal::EnableFrame::Params; |
271 | 231 |
272 scoped_ptr<Params> params(Params::Create(*args_)); | 232 scoped_ptr<Params> params(Params::Create(*args_)); |
273 EXTENSION_FUNCTION_VALIDATE(params.get()); | 233 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 234 |
274 AXTreeIDRegistry::FrameID frame_id = | 235 AXTreeIDRegistry::FrameID frame_id = |
275 AXTreeIDRegistry::GetInstance()->GetFrameID(params->tree_id); | 236 AXTreeIDRegistry::GetInstance()->GetFrameID(params->tree_id); |
276 content::RenderFrameHost* rfh = | 237 content::RenderFrameHost* rfh = |
277 content::RenderFrameHost::FromID(frame_id.first, frame_id.second); | 238 content::RenderFrameHost::FromID(frame_id.first, frame_id.second); |
278 if (!rfh) | 239 if (!rfh) |
279 return RespondNow(Error("unable to load tab")); | 240 return RespondNow(Error("unable to load tab")); |
280 | 241 |
281 content::WebContents* contents = | 242 content::WebContents* contents = |
282 content::WebContents::FromRenderFrameHost(rfh); | 243 content::WebContents::FromRenderFrameHost(rfh); |
283 AutomationWebContentsObserver::CreateForWebContents(contents); | 244 AutomationWebContentsObserver::CreateForWebContents(contents); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 int result_acc_obj_id) { | 380 int result_acc_obj_id) { |
420 if (!error.empty()) { | 381 if (!error.empty()) { |
421 Respond(Error(error)); | 382 Respond(Error(error)); |
422 return; | 383 return; |
423 } | 384 } |
424 | 385 |
425 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id))); | 386 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id))); |
426 } | 387 } |
427 | 388 |
428 } // namespace extensions | 389 } // namespace extensions |
OLD | NEW |