Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: chrome/browser/extensions/api/automation_internal/automation_internal_api.cc

Issue 1155183006: Reimplement automation API on top of C++-backed AXTree. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@automation_faster_2
Patch Set: Rebase Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 } 170 }
176 171
177 private: 172 private:
178 content::RenderFrameHost* rfh_; 173 content::RenderFrameHost* rfh_;
179 174
180 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostActionAdapter); 175 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostActionAdapter);
181 }; 176 };
182 177
183 } // namespace 178 } // namespace
184 179
185 // Helper class that receives accessibility data from |WebContents|.
186 class AutomationWebContentsObserver
187 : public content::WebContentsObserver,
188 public content::WebContentsUserData<AutomationWebContentsObserver> {
189 public:
190 ~AutomationWebContentsObserver() override {}
191
192 // content::WebContentsObserver overrides.
193 void AccessibilityEventReceived(
194 const std::vector<content::AXEventNotificationDetails>& details)
195 override {
196 automation_util::DispatchAccessibilityEventsToAutomation(
197 details, browser_context_,
198 web_contents()->GetContainerBounds().OffsetFromOrigin());
199 }
200
201 void RenderFrameDeleted(
202 content::RenderFrameHost* render_frame_host) override {
203 automation_util::DispatchTreeDestroyedEventToAutomation(
204 render_frame_host->GetProcess()->GetID(),
205 render_frame_host->GetRoutingID(),
206 browser_context_);
207 }
208
209 private:
210 friend class content::WebContentsUserData<AutomationWebContentsObserver>;
211
212 AutomationWebContentsObserver(
213 content::WebContents* web_contents)
214 : content::WebContentsObserver(web_contents),
215 browser_context_(web_contents->GetBrowserContext()) {}
216
217 content::BrowserContext* browser_context_;
218
219 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver);
220 };
221
222 ExtensionFunction::ResponseAction 180 ExtensionFunction::ResponseAction
223 AutomationInternalEnableTabFunction::Run() { 181 AutomationInternalEnableTabFunction::Run() {
224 const AutomationInfo* automation_info = AutomationInfo::Get(extension()); 182 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
225 EXTENSION_FUNCTION_VALIDATE(automation_info); 183 EXTENSION_FUNCTION_VALIDATE(automation_info);
226 184
227 using api::automation_internal::EnableTab::Params; 185 using api::automation_internal::EnableTab::Params;
228 scoped_ptr<Params> params(Params::Create(*args_)); 186 scoped_ptr<Params> params(Params::Create(*args_));
229 EXTENSION_FUNCTION_VALIDATE(params.get()); 187 EXTENSION_FUNCTION_VALIDATE(params.get());
230 content::WebContents* contents = NULL; 188 content::WebContents* contents = NULL;
231 if (params->args.tab_id.get()) { 189 if (params->args.tab_id.get()) {
232 int tab_id = *params->args.tab_id; 190 int tab_id = *params->args.tab_id;
233 if (!ExtensionTabUtil::GetTabById(tab_id, 191 if (!ExtensionTabUtil::GetTabById(tab_id,
234 GetProfile(), 192 GetProfile(),
235 include_incognito(), 193 include_incognito(),
236 NULL, /* browser out param*/ 194 NULL, /* browser out param*/
237 NULL, /* tab_strip out param */ 195 NULL, /* tab_strip out param */
238 &contents, 196 &contents,
239 NULL /* tab_index out param */)) { 197 NULL /* tab_index out param */)) {
240 return RespondNow( 198 return RespondNow(
241 Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id))); 199 Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id)));
242 } 200 }
243 } else { 201 } else {
244 contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents(); 202 contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents();
245 if (!contents) 203 if (!contents)
246 return RespondNow(Error("No active tab")); 204 return RespondNow(Error("No active tab"));
247 } 205 }
206
248 content::RenderFrameHost* rfh = contents->GetMainFrame(); 207 content::RenderFrameHost* rfh = contents->GetMainFrame();
249 if (!rfh) 208 if (!rfh)
250 return RespondNow(Error("Could not enable accessibility for active tab")); 209 return RespondNow(Error("Could not enable accessibility for active tab"));
251 210
252 if (!CanRequestAutomation(extension(), automation_info, contents)) { 211 if (!CanRequestAutomation(extension(), automation_info, contents)) {
253 return RespondNow( 212 return RespondNow(
254 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); 213 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec()));
255 } 214 }
256 215
257 AutomationWebContentsObserver::CreateForWebContents(contents); 216 AutomationWebContentsObserver::CreateForWebContents(contents);
258 contents->EnableTreeOnlyAccessibilityMode(); 217 contents->EnableTreeOnlyAccessibilityMode();
218
259 int ax_tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( 219 int ax_tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID(
260 rfh->GetProcess()->GetID(), rfh->GetRoutingID()); 220 rfh->GetProcess()->GetID(), rfh->GetRoutingID());
261 221
262 // This gets removed when the extension process dies. 222 // This gets removed when the extension process dies.
263 AutomationEventRouter::GetInstance()->RegisterListenerForOneTree( 223 AutomationEventRouter::GetInstance()->RegisterListenerForOneTree(
264 source_process_id(), 224 source_process_id(),
265 params->args.routing_id, 225 params->args.routing_id,
266 ax_tree_id); 226 ax_tree_id);
267 227
268 return RespondNow(ArgumentList( 228 return RespondNow(ArgumentList(
269 api::automation_internal::EnableTab::Results::Create(ax_tree_id))); 229 api::automation_internal::EnableTab::Results::Create(ax_tree_id)));
270 } 230 }
271 231
272 ExtensionFunction::ResponseAction AutomationInternalEnableFrameFunction::Run() { 232 ExtensionFunction::ResponseAction AutomationInternalEnableFrameFunction::Run() {
273 // TODO(dtseng): Limited to desktop tree for now pending out of proc iframes. 233 // TODO(dtseng): Limited to desktop tree for now pending out of proc iframes.
274 using api::automation_internal::EnableFrame::Params; 234 using api::automation_internal::EnableFrame::Params;
275 235
276 scoped_ptr<Params> params(Params::Create(*args_)); 236 scoped_ptr<Params> params(Params::Create(*args_));
277 EXTENSION_FUNCTION_VALIDATE(params.get()); 237 EXTENSION_FUNCTION_VALIDATE(params.get());
238
278 AXTreeIDRegistry::FrameID frame_id = 239 AXTreeIDRegistry::FrameID frame_id =
279 AXTreeIDRegistry::GetInstance()->GetFrameID(params->tree_id); 240 AXTreeIDRegistry::GetInstance()->GetFrameID(params->tree_id);
280 content::RenderFrameHost* rfh = 241 content::RenderFrameHost* rfh =
281 content::RenderFrameHost::FromID(frame_id.first, frame_id.second); 242 content::RenderFrameHost::FromID(frame_id.first, frame_id.second);
282 if (!rfh) 243 if (!rfh)
283 return RespondNow(Error("unable to load tab")); 244 return RespondNow(Error("unable to load tab"));
284 245
285 content::WebContents* contents = 246 content::WebContents* contents =
286 content::WebContents::FromRenderFrameHost(rfh); 247 content::WebContents::FromRenderFrameHost(rfh);
287 AutomationWebContentsObserver::CreateForWebContents(contents); 248 AutomationWebContentsObserver::CreateForWebContents(contents);
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 int result_acc_obj_id) { 388 int result_acc_obj_id) {
428 if (!error.empty()) { 389 if (!error.empty()) {
429 Respond(Error(error)); 390 Respond(Error(error));
430 return; 391 return;
431 } 392 }
432 393
433 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id))); 394 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id)));
434 } 395 }
435 396
436 } // namespace extensions 397 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698