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

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: devlin review 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/string16.h"
9 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/api/automation_internal/automation_action_ad apter.h" 12 #include "chrome/browser/extensions/api/automation_internal/automation_action_ad apter.h"
11 #include "chrome/browser/extensions/api/automation_internal/automation_util.h" 13 #include "chrome/browser/extensions/api/automation_internal/automation_util.h"
12 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" 14 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
13 #include "chrome/browser/extensions/extension_tab_util.h" 15 #include "chrome/browser/extensions/extension_tab_util.h"
14 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/extensions/api/automation_internal.h" 19 #include "chrome/common/extensions/api/automation_internal.h"
18 #include "chrome/common/extensions/manifest_handlers/automation.h" 20 #include "chrome/common/extensions/manifest_handlers/automation.h"
19 #include "content/public/browser/ax_event_notification_details.h" 21 #include "content/public/browser/ax_event_notification_details.h"
20 #include "content/public/browser/render_frame_host.h" 22 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/render_process_host.h" 23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/render_widget_host.h" 25 #include "content/public/browser/render_widget_host.h"
23 #include "content/public/browser/render_widget_host_view.h" 26 #include "content/public/browser/render_widget_host_view.h"
24 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
28 #include "extensions/common/extension_messages.h"
25 #include "extensions/common/permissions/permissions_data.h" 29 #include "extensions/common/permissions/permissions_data.h"
26 30
27 #if defined(OS_CHROMEOS) 31 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/ui/ash/accessibility/automation_manager_ash.h" 32 #include "chrome/browser/ui/ash/accessibility/automation_manager_ash.h"
29 #endif 33 #endif
30 34
31 namespace extensions { 35 namespace extensions {
32 class AutomationWebContentsObserver; 36 class AutomationWebContentsObserver;
33 } // namespace extensions 37 } // namespace extensions
34 38
35 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver); 39 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver);
36 40
41 namespace extensions {
42
37 namespace { 43 namespace {
38 const int kDesktopProcessID = 0; 44 const int kDesktopProcessID = 0;
39 const int kDesktopRoutingID = 0; 45 const int kDesktopRoutingID = 0;
40 46
41 const char kCannotRequestAutomationOnPage[] = 47 const char kCannotRequestAutomationOnPage[] =
42 "Cannot request automation tree on url \"*\". " 48 "Cannot request automation tree on url \"*\". "
43 "Extension manifest must request permission to access this host."; 49 "Extension manifest must request permission to access this host.";
44 } // namespace 50 const char kRendererDestroyed[] = "The tab was closed.";
45 51
46 namespace extensions { 52 // Handles sending and receiving IPCs for a single querySelector request. On
53 // creation, sends the request IPC, and is destroyed either when the response is
54 // received or the renderer is destroyed.
55 class QuerySelectorHandler : public content::WebContentsObserver {
56 public:
57 QuerySelectorHandler(
58 content::WebContents* web_contents,
59 int request_id,
60 int acc_obj_id,
61 const base::string16& query,
62 const extensions::AutomationInternalQuerySelectorFunction::Callback&
63 callback)
64 : content::WebContentsObserver(web_contents),
65 request_id_(request_id),
66 callback_(callback) {
67 content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
68 rvh->Send(new ExtensionMsg_AutomationQuerySelector(
69 rvh->GetRoutingID(), request_id, acc_obj_id, query));
70 }
71
72 ~QuerySelectorHandler() override {}
73
74 bool OnMessageReceived(const IPC::Message& message) override {
75 if (message.type() != ExtensionHostMsg_AutomationQuerySelector_Result::ID)
76 return false;
77
78 // There may be several requests in flight; check this response matches.
79 int message_request_id = 0;
80 PickleIterator iter(message);
81 CHECK(message.ReadInt(&iter, &message_request_id));
82
83 if (message_request_id != request_id_)
84 return false;
85
86 IPC_BEGIN_MESSAGE_MAP(QuerySelectorHandler, message)
87 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AutomationQuerySelector_Result,
88 OnQueryResponse)
89 IPC_END_MESSAGE_MAP()
90 return true;
91 }
92
93 void WebContentsDestroyed() override {
94 callback_.Run(kRendererDestroyed, 0);
95 delete this;
96 }
97
98 private:
99 void OnQueryResponse(int request_id,
100 const std::string& error,
101 int result_acc_obj_id) {
102 callback_.Run(error, result_acc_obj_id);
103 delete this;
104 }
105
106 int request_id_;
107 const extensions::AutomationInternalQuerySelectorFunction::Callback callback_;
108 };
47 109
48 bool CanRequestAutomation(const Extension* extension, 110 bool CanRequestAutomation(const Extension* extension,
49 const AutomationInfo* automation_info, 111 const AutomationInfo* automation_info,
50 const content::WebContents* contents) { 112 const content::WebContents* contents) {
51 if (automation_info->desktop) 113 if (automation_info->desktop)
52 return true; 114 return true;
53 115
54 const GURL& url = contents->GetURL(); 116 const GURL& url = contents->GetURL();
55 // TODO(aboxhall): check for webstore URL 117 // TODO(aboxhall): check for webstore URL
56 if (automation_info->matches.MatchesURL(url)) 118 if (automation_info->matches.MatchesURL(url))
57 return true; 119 return true;
58 120
59 int tab_id = ExtensionTabUtil::GetTabId(contents); 121 int tab_id = ExtensionTabUtil::GetTabId(contents);
60 content::RenderProcessHost* process = contents->GetRenderProcessHost(); 122 content::RenderProcessHost* process = contents->GetRenderProcessHost();
61 int process_id = process ? process->GetID() : -1; 123 int process_id = process ? process->GetID() : -1;
62 std::string unused_error; 124 std::string unused_error;
63 return extension->permissions_data()->CanAccessPage( 125 return extension->permissions_data()->CanAccessPage(
64 extension, url, url, tab_id, process_id, &unused_error); 126 extension, url, url, tab_id, process_id, &unused_error);
65 } 127 }
66 128
129 // Helper class that implements an action adapter for a |RenderFrameHost|.
130 class RenderFrameHostActionAdapter : public AutomationActionAdapter {
131 public:
132 explicit RenderFrameHostActionAdapter(content::RenderFrameHost* rfh)
133 : rfh_(rfh) {}
134
135 virtual ~RenderFrameHostActionAdapter() {}
136
137 // AutomationActionAdapter implementation.
138 void DoDefault(int32 id) override { rfh_->AccessibilityDoDefaultAction(id); }
139
140 void Focus(int32 id) override { rfh_->AccessibilitySetFocus(id); }
palmer 2014/11/01 00:20:48 Are these |id|s the same things as are declared as
aboxhall 2014/11/03 17:27:11 Note that this isn't new code. Also, the code whic
141
142 void MakeVisible(int32 id) override {
143 rfh_->AccessibilityScrollToMakeVisible(id, gfx::Rect());
144 }
145
146 void SetSelection(int32 id, int32 start, int32 end) override {
147 rfh_->AccessibilitySetTextSelection(id, start, end);
148 }
149
150 private:
151 content::RenderFrameHost* rfh_;
152
153 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostActionAdapter);
154 };
155
156 } // namespace
157
67 // Helper class that receives accessibility data from |WebContents|. 158 // Helper class that receives accessibility data from |WebContents|.
68 class AutomationWebContentsObserver 159 class AutomationWebContentsObserver
69 : public content::WebContentsObserver, 160 : public content::WebContentsObserver,
70 public content::WebContentsUserData<AutomationWebContentsObserver> { 161 public content::WebContentsUserData<AutomationWebContentsObserver> {
71 public: 162 public:
72 ~AutomationWebContentsObserver() override {} 163 ~AutomationWebContentsObserver() override {}
73 164
74 // content::WebContentsObserver overrides. 165 // content::WebContentsObserver overrides.
75 void AccessibilityEventReceived( 166 void AccessibilityEventReceived(
76 const std::vector<content::AXEventNotificationDetails>& details) 167 const std::vector<content::AXEventNotificationDetails>& details)
(...skipping 17 matching lines...) Expand all
94 AutomationWebContentsObserver( 185 AutomationWebContentsObserver(
95 content::WebContents* web_contents) 186 content::WebContents* web_contents)
96 : content::WebContentsObserver(web_contents), 187 : content::WebContentsObserver(web_contents),
97 browser_context_(web_contents->GetBrowserContext()) {} 188 browser_context_(web_contents->GetBrowserContext()) {}
98 189
99 content::BrowserContext* browser_context_; 190 content::BrowserContext* browser_context_;
100 191
101 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver); 192 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver);
102 }; 193 };
103 194
104 // Helper class that implements an action adapter for a |RenderFrameHost|.
105 class RenderFrameHostActionAdapter : public AutomationActionAdapter {
106 public:
107 explicit RenderFrameHostActionAdapter(content::RenderFrameHost* rfh)
108 : rfh_(rfh) {}
109
110 virtual ~RenderFrameHostActionAdapter() {}
111
112 // AutomationActionAdapter implementation.
113 void DoDefault(int32 id) override { rfh_->AccessibilityDoDefaultAction(id); }
114
115 void Focus(int32 id) override { rfh_->AccessibilitySetFocus(id); }
116
117 void MakeVisible(int32 id) override {
118 rfh_->AccessibilityScrollToMakeVisible(id, gfx::Rect());
119 }
120
121 void SetSelection(int32 id, int32 start, int32 end) override {
122 rfh_->AccessibilitySetTextSelection(id, start, end);
123 }
124
125 private:
126 content::RenderFrameHost* rfh_;
127
128 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostActionAdapter);
129 };
130
131 ExtensionFunction::ResponseAction 195 ExtensionFunction::ResponseAction
132 AutomationInternalEnableTabFunction::Run() { 196 AutomationInternalEnableTabFunction::Run() {
133 const AutomationInfo* automation_info = AutomationInfo::Get(extension()); 197 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
134 EXTENSION_FUNCTION_VALIDATE(automation_info); 198 EXTENSION_FUNCTION_VALIDATE(automation_info);
135 199
136 using api::automation_internal::EnableTab::Params; 200 using api::automation_internal::EnableTab::Params;
137 scoped_ptr<Params> params(Params::Create(*args_)); 201 scoped_ptr<Params> params(Params::Create(*args_));
138 EXTENSION_FUNCTION_VALIDATE(params.get()); 202 EXTENSION_FUNCTION_VALIDATE(params.get());
139 content::WebContents* contents = NULL; 203 content::WebContents* contents = NULL;
140 if (params->tab_id.get()) { 204 if (params->tab_id.get()) {
(...skipping 19 matching lines...) Expand all
160 224
161 if (!CanRequestAutomation(extension(), automation_info, contents)) { 225 if (!CanRequestAutomation(extension(), automation_info, contents)) {
162 return RespondNow( 226 return RespondNow(
163 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); 227 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec()));
164 } 228 }
165 AutomationWebContentsObserver::CreateForWebContents(contents); 229 AutomationWebContentsObserver::CreateForWebContents(contents);
166 contents->EnableTreeOnlyAccessibilityMode(); 230 contents->EnableTreeOnlyAccessibilityMode();
167 return RespondNow( 231 return RespondNow(
168 ArgumentList(api::automation_internal::EnableTab::Results::Create( 232 ArgumentList(api::automation_internal::EnableTab::Results::Create(
169 rfh->GetProcess()->GetID(), rfh->GetRoutingID()))); 233 rfh->GetProcess()->GetID(), rfh->GetRoutingID())));
170 } 234 }
171 235
172 ExtensionFunction::ResponseAction 236 ExtensionFunction::ResponseAction
173 AutomationInternalPerformActionFunction::Run() { 237 AutomationInternalPerformActionFunction::Run() {
174 const AutomationInfo* automation_info = AutomationInfo::Get(extension()); 238 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
175 EXTENSION_FUNCTION_VALIDATE(automation_info && automation_info->interact); 239 EXTENSION_FUNCTION_VALIDATE(automation_info && automation_info->interact);
176 240
177 using api::automation_internal::PerformAction::Params; 241 using api::automation_internal::PerformAction::Params;
178 scoped_ptr<Params> params(Params::Create(*args_)); 242 scoped_ptr<Params> params(Params::Create(*args_));
179 EXTENSION_FUNCTION_VALIDATE(params.get()); 243 EXTENSION_FUNCTION_VALIDATE(params.get());
180 244
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 if (!automation_info || !automation_info->desktop) 308 if (!automation_info || !automation_info->desktop)
245 return RespondNow(Error("desktop permission must be requested")); 309 return RespondNow(Error("desktop permission must be requested"));
246 310
247 AutomationManagerAsh::GetInstance()->Enable(browser_context()); 311 AutomationManagerAsh::GetInstance()->Enable(browser_context());
248 return RespondNow(NoArguments()); 312 return RespondNow(NoArguments());
249 #else 313 #else
250 return RespondNow(Error("getDesktop is unsupported by this platform")); 314 return RespondNow(Error("getDesktop is unsupported by this platform"));
251 #endif // defined(OS_CHROMEOS) 315 #endif // defined(OS_CHROMEOS)
252 } 316 }
253 317
318 // static
319 int AutomationInternalQuerySelectorFunction::query_request_id_counter_ = 0;
320
321 ExtensionFunction::ResponseAction
322 AutomationInternalQuerySelectorFunction::Run() {
323 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
324 EXTENSION_FUNCTION_VALIDATE(automation_info);
325
326 using api::automation_internal::QuerySelector::Params;
327 scoped_ptr<Params> params(Params::Create(*args_));
328 EXTENSION_FUNCTION_VALIDATE(params.get());
329
330 if (params->args.process_id == kDesktopProcessID &&
331 params->args.routing_id == kDesktopRoutingID) {
332 return RespondNow(
333 Error("querySelector queries may not be used on the desktop."));
334 }
335 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
336 params->args.process_id, params->args.routing_id);
337 if (!rfh)
338 return RespondNow(Error("querySelector query sent on destroyed tree."));
339
340 content::WebContents* contents =
341 content::WebContents::FromRenderFrameHost(rfh);
342
343 int request_id = query_request_id_counter_++;
344 base::string16 selector = base::UTF8ToUTF16(params->args.selector);
345
346 // QuerySelectorHandler handles IPCs and deletes itself on completion.
347 new QuerySelectorHandler(
348 contents, request_id, params->args.automation_node_id, selector,
349 base::Bind(&AutomationInternalQuerySelectorFunction::OnResponse, this));
350
351 return RespondLater();
352 }
353
354 void AutomationInternalQuerySelectorFunction::OnResponse(
355 const std::string& error,
356 int result_acc_obj_id) {
357 if (!error.empty()) {
358 Respond(Error(error));
359 return;
360 }
361
362 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id)));
363 }
364
254 } // namespace extensions 365 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698