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 655273005: Implement AutomationNode.querySelector(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Address comments Created 6 years, 1 month 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/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/api/automation_internal/automation_action_ad apter.h" 11 #include "chrome/browser/extensions/api/automation_internal/automation_action_ad apter.h"
11 #include "chrome/browser/extensions/api/automation_internal/automation_util.h" 12 #include "chrome/browser/extensions/api/automation_internal/automation_util.h"
12 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" 13 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
13 #include "chrome/browser/extensions/extension_tab_util.h" 14 #include "chrome/browser/extensions/extension_tab_util.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/extensions/api/automation_internal.h" 18 #include "chrome/common/extensions/api/automation_internal.h"
18 #include "chrome/common/extensions/manifest_handlers/automation.h" 19 #include "chrome/common/extensions/manifest_handlers/automation.h"
19 #include "content/public/browser/ax_event_notification_details.h" 20 #include "content/public/browser/ax_event_notification_details.h"
20 #include "content/public/browser/render_frame_host.h" 21 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/render_process_host.h" 22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/render_widget_host.h" 24 #include "content/public/browser/render_widget_host.h"
23 #include "content/public/browser/render_widget_host_view.h" 25 #include "content/public/browser/render_widget_host_view.h"
24 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
27 #include "extensions/common/extension_messages.h"
25 #include "extensions/common/permissions/permissions_data.h" 28 #include "extensions/common/permissions/permissions_data.h"
26 29
27 #if defined(OS_CHROMEOS) 30 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/ui/ash/accessibility/automation_manager_ash.h" 31 #include "chrome/browser/ui/ash/accessibility/automation_manager_ash.h"
29 #endif 32 #endif
30 33
31 namespace extensions { 34 namespace extensions {
32 class AutomationWebContentsObserver; 35 class AutomationWebContentsObserver;
33 } // namespace extensions 36 } // namespace extensions
34 37
35 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver); 38 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver);
36 39
37 namespace { 40 namespace {
38 const int kDesktopProcessID = 0; 41 const int kDesktopProcessID = 0;
39 const int kDesktopRoutingID = 0; 42 const int kDesktopRoutingID = 0;
40 43
41 const char kCannotRequestAutomationOnPage[] = 44 const char kCannotRequestAutomationOnPage[] =
42 "Cannot request automation tree on url \"*\". " 45 "Cannot request automation tree on url \"*\". "
43 "Extension manifest must request permission to access this host."; 46 "Extension manifest must request permission to access this host.";
44 } // namespace 47 const char kRendererDestroyed[] = "The tab was closed.";
48 } // anonymous namespace
45 49
46 namespace extensions { 50 namespace extensions {
51 // Handles sending and receiving IPCs for a single querySelector request. On
52 // creation, sends the request IPC, and is destroyed either when the response is
53 // received or the renderer is destroyed.
54 class QuerySelectorHandler : public content::WebContentsObserver {
Devlin 2014/10/29 21:23:43 This class should still be in an anonymous namespa
aboxhall 2014/10/30 18:34:18 Done.
55 public:
56 QuerySelectorHandler(
57 content::WebContents* web_contents,
58 int request_id,
59 int acc_obj_id,
60 const base::string16& query,
61 extensions::AutomationInternalQuerySelectorFunction::Callback callback)
62 : content::WebContentsObserver(web_contents),
63 request_id_(request_id),
64 callback_(callback) {
65 content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
66 rvh->Send(new ExtensionMsg_AutomationQuerySelector(
67 rvh->GetRoutingID(), request_id, acc_obj_id, query));
68 }
69
70 ~QuerySelectorHandler() override {}
71
72 bool OnMessageReceived(const IPC::Message& message) override {
73 if (message.type() != ExtensionHostMsg_AutomationQuerySelector_Result::ID)
74 return false;
75
76 // There may be several requests in flight; check that this response matches
77 int message_request_id;
78 PickleIterator iter(message);
79 CHECK(message.ReadInt(&iter, &message_request_id));
80
81 if (message_request_id != request_id_)
82 return false;
83
84 IPC_BEGIN_MESSAGE_MAP(QuerySelectorHandler, message)
85 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AutomationQuerySelector_Result,
86 OnQueryResponse)
87 IPC_END_MESSAGE_MAP()
88 return true;
89 }
90
91 void WebContentsDestroyed() override {
92 callback_.Run(kRendererDestroyed, 0);
93 delete this;
94 }
95
96 private:
97 void OnQueryResponse(int request_id,
98 const std::string& error,
99 int result_acc_obj_id) {
100 callback_.Run(error, result_acc_obj_id);
101 delete this;
102 }
103
104 int request_id_;
105 extensions::AutomationInternalQuerySelectorFunction::Callback callback_;
106 };
47 107
48 bool CanRequestAutomation(const Extension* extension, 108 bool CanRequestAutomation(const Extension* extension,
49 const AutomationInfo* automation_info, 109 const AutomationInfo* automation_info,
50 const content::WebContents* contents) { 110 const content::WebContents* contents) {
51 if (automation_info->desktop) 111 if (automation_info->desktop)
52 return true; 112 return true;
53 113
54 const GURL& url = contents->GetURL(); 114 const GURL& url = contents->GetURL();
55 // TODO(aboxhall): check for webstore URL 115 // TODO(aboxhall): check for webstore URL
56 if (automation_info->matches.MatchesURL(url)) 116 if (automation_info->matches.MatchesURL(url))
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 220
161 if (!CanRequestAutomation(extension(), automation_info, contents)) { 221 if (!CanRequestAutomation(extension(), automation_info, contents)) {
162 return RespondNow( 222 return RespondNow(
163 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); 223 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec()));
164 } 224 }
165 AutomationWebContentsObserver::CreateForWebContents(contents); 225 AutomationWebContentsObserver::CreateForWebContents(contents);
166 contents->EnableTreeOnlyAccessibilityMode(); 226 contents->EnableTreeOnlyAccessibilityMode();
167 return RespondNow( 227 return RespondNow(
168 ArgumentList(api::automation_internal::EnableTab::Results::Create( 228 ArgumentList(api::automation_internal::EnableTab::Results::Create(
169 rfh->GetProcess()->GetID(), rfh->GetRoutingID()))); 229 rfh->GetProcess()->GetID(), rfh->GetRoutingID())));
170 } 230 }
171 231
172 ExtensionFunction::ResponseAction 232 ExtensionFunction::ResponseAction
173 AutomationInternalPerformActionFunction::Run() { 233 AutomationInternalPerformActionFunction::Run() {
174 const AutomationInfo* automation_info = AutomationInfo::Get(extension()); 234 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
175 EXTENSION_FUNCTION_VALIDATE(automation_info && automation_info->interact); 235 EXTENSION_FUNCTION_VALIDATE(automation_info && automation_info->interact);
176 236
177 using api::automation_internal::PerformAction::Params; 237 using api::automation_internal::PerformAction::Params;
178 scoped_ptr<Params> params(Params::Create(*args_)); 238 scoped_ptr<Params> params(Params::Create(*args_));
179 EXTENSION_FUNCTION_VALIDATE(params.get()); 239 EXTENSION_FUNCTION_VALIDATE(params.get());
180 240
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 if (!automation_info || !automation_info->desktop) 304 if (!automation_info || !automation_info->desktop)
245 return RespondNow(Error("desktop permission must be requested")); 305 return RespondNow(Error("desktop permission must be requested"));
246 306
247 AutomationManagerAsh::GetInstance()->Enable(browser_context()); 307 AutomationManagerAsh::GetInstance()->Enable(browser_context());
248 return RespondNow(NoArguments()); 308 return RespondNow(NoArguments());
249 #else 309 #else
250 return RespondNow(Error("getDesktop is unsupported by this platform")); 310 return RespondNow(Error("getDesktop is unsupported by this platform"));
251 #endif // defined(OS_CHROMEOS) 311 #endif // defined(OS_CHROMEOS)
252 } 312 }
253 313
314 // static
315 int AutomationInternalQuerySelectorFunction::query_request_id_counter_ = 0;
316
317 ExtensionFunction::ResponseAction
318 AutomationInternalQuerySelectorFunction::Run() {
319 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
320 EXTENSION_FUNCTION_VALIDATE(automation_info);
321
322 using api::automation_internal::QuerySelector::Params;
323 scoped_ptr<Params> params(Params::Create(*args_));
324 EXTENSION_FUNCTION_VALIDATE(params.get());
325
326 if (params->args.process_id == kDesktopProcessID &&
327 params->args.routing_id == kDesktopRoutingID) {
328 return RespondNow(
329 Error("querySelector queries may not be used on the desktop."));
330 }
331 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
332 params->args.process_id, params->args.routing_id);
333 if (!rfh)
334 return RespondNow(Error("querySelector query sent on destroyed node"));
335
336 content::WebContents* contents =
337 content::WebContents::FromRenderFrameHost(rfh);
338
339 int request_id = query_request_id_counter_++;
340 base::string16 selector = base::UTF8ToUTF16(params->args.selector);
341
342 // QuerySelectorHandler handles IPCs and deletes itself on completion.
343 new QuerySelectorHandler(
344 contents, request_id, params->args.automation_node_id, selector,
345 base::Bind(&AutomationInternalQuerySelectorFunction::OnResponse, this));
346
347 return RespondLater();
348 }
349
350 void AutomationInternalQuerySelectorFunction::OnResponse(
351 const std::string& error,
352 int result_acc_obj_id) {
353 if (!error.empty()) {
354 Respond(Error(error));
355 return;
Devlin 2014/10/29 21:23:43 To me, this "early" return seems kind of silly. W
Devlin 2014/10/30 23:26:53 Looks like this comment went unaddressed?
aboxhall 2014/10/31 20:32:21 I prefer this style because it makes the normal vs
356 }
357
358 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id)));
359 }
360
254 } // namespace extensions 361 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698