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

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: Use enums instead of strings for error messages 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.";
51 const char kNoMainFrame[] = "No main frame.";
52 const char kNoDocument[] = "No document.";
53 const char kNodeDestroyed[] =
54 "querySelector sent on node which is no longer in the tree.";
45 55
46 namespace extensions { 56 // Handles sending and receiving IPCs for a single querySelector request. On
57 // creation, sends the request IPC, and is destroyed either when the response is
58 // received or the renderer is destroyed.
59 class QuerySelectorHandler : public content::WebContentsObserver {
60 public:
61 QuerySelectorHandler(
62 content::WebContents* web_contents,
63 int request_id,
64 int acc_obj_id,
65 const base::string16& query,
66 const extensions::AutomationInternalQuerySelectorFunction::Callback&
67 callback)
68 : content::WebContentsObserver(web_contents),
69 request_id_(request_id),
70 callback_(callback) {
71 content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
72
73 rvh->Send(new ExtensionMsg_AutomationQuerySelector(
74 rvh->GetRoutingID(), request_id, acc_obj_id, query));
75 }
76
77 ~QuerySelectorHandler() override {}
78
79 bool OnMessageReceived(const IPC::Message& message) override {
80 if (message.type() != ExtensionHostMsg_AutomationQuerySelector_Result::ID)
81 return false;
82
83 // There may be several requests in flight; check this response matches.
84 int message_request_id = 0;
85 PickleIterator iter(message);
86 CHECK(message.ReadInt(&iter, &message_request_id));
palmer 2014/11/04 19:39:22 Browser crash on bad IPC? Better to just return fa
aboxhall 2014/11/04 22:13:49 Excellent point, done.
87
88 if (message_request_id != request_id_)
89 return false;
90
91 IPC_BEGIN_MESSAGE_MAP(QuerySelectorHandler, message)
92 IPC_MESSAGE_HANDLER(ExtensionHostMsg_AutomationQuerySelector_Result,
93 OnQueryResponse)
94 IPC_END_MESSAGE_MAP()
95 return true;
96 }
97
98 void WebContentsDestroyed() override {
99 callback_.Run(kRendererDestroyed, 0);
100 delete this;
101 }
102
103 private:
104 void OnQueryResponse(int request_id,
105 ExtensionHostMsg_AutomationQuerySelector_Error error,
106 int result_acc_obj_id) {
107 std::string error_string;
108 switch (error.value) {
109 case ExtensionHostMsg_AutomationQuerySelector_Error::kNone:
110 error_string = "";
111 break;
112 case ExtensionHostMsg_AutomationQuerySelector_Error::kNoMainFrame:
113 error_string = kNoMainFrame;
114 break;
115 case ExtensionHostMsg_AutomationQuerySelector_Error::kNoDocument:
116 error_string = kNoDocument;
117 break;
118 case ExtensionHostMsg_AutomationQuerySelector_Error::kNodeDestroyed:
119 error_string = kNodeDestroyed;
120 break;
121 }
122 callback_.Run(error_string, result_acc_obj_id);
123 delete this;
124 }
125
126 int request_id_;
127 const extensions::AutomationInternalQuerySelectorFunction::Callback callback_;
128 };
47 129
48 bool CanRequestAutomation(const Extension* extension, 130 bool CanRequestAutomation(const Extension* extension,
49 const AutomationInfo* automation_info, 131 const AutomationInfo* automation_info,
50 const content::WebContents* contents) { 132 const content::WebContents* contents) {
51 if (automation_info->desktop) 133 if (automation_info->desktop)
52 return true; 134 return true;
53 135
54 const GURL& url = contents->GetURL(); 136 const GURL& url = contents->GetURL();
55 // TODO(aboxhall): check for webstore URL 137 // TODO(aboxhall): check for webstore URL
56 if (automation_info->matches.MatchesURL(url)) 138 if (automation_info->matches.MatchesURL(url))
57 return true; 139 return true;
58 140
59 int tab_id = ExtensionTabUtil::GetTabId(contents); 141 int tab_id = ExtensionTabUtil::GetTabId(contents);
60 content::RenderProcessHost* process = contents->GetRenderProcessHost(); 142 content::RenderProcessHost* process = contents->GetRenderProcessHost();
61 int process_id = process ? process->GetID() : -1; 143 int process_id = process ? process->GetID() : -1;
62 std::string unused_error; 144 std::string unused_error;
63 return extension->permissions_data()->CanAccessPage( 145 return extension->permissions_data()->CanAccessPage(
64 extension, url, url, tab_id, process_id, &unused_error); 146 extension, url, url, tab_id, process_id, &unused_error);
65 } 147 }
66 148
149 // Helper class that implements an action adapter for a |RenderFrameHost|.
150 class RenderFrameHostActionAdapter : public AutomationActionAdapter {
151 public:
152 explicit RenderFrameHostActionAdapter(content::RenderFrameHost* rfh)
153 : rfh_(rfh) {}
154
155 virtual ~RenderFrameHostActionAdapter() {}
156
157 // AutomationActionAdapter implementation.
158 void DoDefault(int32 id) override { rfh_->AccessibilityDoDefaultAction(id); }
159
160 void Focus(int32 id) override { rfh_->AccessibilitySetFocus(id); }
161
162 void MakeVisible(int32 id) override {
163 rfh_->AccessibilityScrollToMakeVisible(id, gfx::Rect());
164 }
165
166 void SetSelection(int32 id, int32 start, int32 end) override {
167 rfh_->AccessibilitySetTextSelection(id, start, end);
168 }
169
170 private:
171 content::RenderFrameHost* rfh_;
172
173 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostActionAdapter);
174 };
175
176 } // namespace
177
67 // Helper class that receives accessibility data from |WebContents|. 178 // Helper class that receives accessibility data from |WebContents|.
68 class AutomationWebContentsObserver 179 class AutomationWebContentsObserver
69 : public content::WebContentsObserver, 180 : public content::WebContentsObserver,
70 public content::WebContentsUserData<AutomationWebContentsObserver> { 181 public content::WebContentsUserData<AutomationWebContentsObserver> {
71 public: 182 public:
72 ~AutomationWebContentsObserver() override {} 183 ~AutomationWebContentsObserver() override {}
73 184
74 // content::WebContentsObserver overrides. 185 // content::WebContentsObserver overrides.
75 void AccessibilityEventReceived( 186 void AccessibilityEventReceived(
76 const std::vector<content::AXEventNotificationDetails>& details) 187 const std::vector<content::AXEventNotificationDetails>& details)
(...skipping 17 matching lines...) Expand all
94 AutomationWebContentsObserver( 205 AutomationWebContentsObserver(
95 content::WebContents* web_contents) 206 content::WebContents* web_contents)
96 : content::WebContentsObserver(web_contents), 207 : content::WebContentsObserver(web_contents),
97 browser_context_(web_contents->GetBrowserContext()) {} 208 browser_context_(web_contents->GetBrowserContext()) {}
98 209
99 content::BrowserContext* browser_context_; 210 content::BrowserContext* browser_context_;
100 211
101 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver); 212 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver);
102 }; 213 };
103 214
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 215 ExtensionFunction::ResponseAction
132 AutomationInternalEnableTabFunction::Run() { 216 AutomationInternalEnableTabFunction::Run() {
133 const AutomationInfo* automation_info = AutomationInfo::Get(extension()); 217 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
134 EXTENSION_FUNCTION_VALIDATE(automation_info); 218 EXTENSION_FUNCTION_VALIDATE(automation_info);
135 219
136 using api::automation_internal::EnableTab::Params; 220 using api::automation_internal::EnableTab::Params;
137 scoped_ptr<Params> params(Params::Create(*args_)); 221 scoped_ptr<Params> params(Params::Create(*args_));
138 EXTENSION_FUNCTION_VALIDATE(params.get()); 222 EXTENSION_FUNCTION_VALIDATE(params.get());
139 content::WebContents* contents = NULL; 223 content::WebContents* contents = NULL;
140 if (params->tab_id.get()) { 224 if (params->tab_id.get()) {
(...skipping 19 matching lines...) Expand all
160 244
161 if (!CanRequestAutomation(extension(), automation_info, contents)) { 245 if (!CanRequestAutomation(extension(), automation_info, contents)) {
162 return RespondNow( 246 return RespondNow(
163 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec())); 247 Error(kCannotRequestAutomationOnPage, contents->GetURL().spec()));
164 } 248 }
165 AutomationWebContentsObserver::CreateForWebContents(contents); 249 AutomationWebContentsObserver::CreateForWebContents(contents);
166 contents->EnableTreeOnlyAccessibilityMode(); 250 contents->EnableTreeOnlyAccessibilityMode();
167 return RespondNow( 251 return RespondNow(
168 ArgumentList(api::automation_internal::EnableTab::Results::Create( 252 ArgumentList(api::automation_internal::EnableTab::Results::Create(
169 rfh->GetProcess()->GetID(), rfh->GetRoutingID()))); 253 rfh->GetProcess()->GetID(), rfh->GetRoutingID())));
170 } 254 }
171 255
172 ExtensionFunction::ResponseAction 256 ExtensionFunction::ResponseAction
173 AutomationInternalPerformActionFunction::Run() { 257 AutomationInternalPerformActionFunction::Run() {
174 const AutomationInfo* automation_info = AutomationInfo::Get(extension()); 258 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
175 EXTENSION_FUNCTION_VALIDATE(automation_info && automation_info->interact); 259 EXTENSION_FUNCTION_VALIDATE(automation_info && automation_info->interact);
176 260
177 using api::automation_internal::PerformAction::Params; 261 using api::automation_internal::PerformAction::Params;
178 scoped_ptr<Params> params(Params::Create(*args_)); 262 scoped_ptr<Params> params(Params::Create(*args_));
179 EXTENSION_FUNCTION_VALIDATE(params.get()); 263 EXTENSION_FUNCTION_VALIDATE(params.get());
180 264
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 if (!automation_info || !automation_info->desktop) 328 if (!automation_info || !automation_info->desktop)
245 return RespondNow(Error("desktop permission must be requested")); 329 return RespondNow(Error("desktop permission must be requested"));
246 330
247 AutomationManagerAsh::GetInstance()->Enable(browser_context()); 331 AutomationManagerAsh::GetInstance()->Enable(browser_context());
248 return RespondNow(NoArguments()); 332 return RespondNow(NoArguments());
249 #else 333 #else
250 return RespondNow(Error("getDesktop is unsupported by this platform")); 334 return RespondNow(Error("getDesktop is unsupported by this platform"));
251 #endif // defined(OS_CHROMEOS) 335 #endif // defined(OS_CHROMEOS)
252 } 336 }
253 337
338 // static
339 int AutomationInternalQuerySelectorFunction::query_request_id_counter_ = 0;
340
341 ExtensionFunction::ResponseAction
342 AutomationInternalQuerySelectorFunction::Run() {
343 const AutomationInfo* automation_info = AutomationInfo::Get(extension());
344 EXTENSION_FUNCTION_VALIDATE(automation_info);
345
346 using api::automation_internal::QuerySelector::Params;
347 scoped_ptr<Params> params(Params::Create(*args_));
348 EXTENSION_FUNCTION_VALIDATE(params.get());
349
350 if (params->args.process_id == kDesktopProcessID &&
351 params->args.routing_id == kDesktopRoutingID) {
352 return RespondNow(
353 Error("querySelector queries may not be used on the desktop."));
354 }
355 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
356 params->args.process_id, params->args.routing_id);
357 if (!rfh)
358 return RespondNow(Error("querySelector query sent on destroyed tree."));
359
360 content::WebContents* contents =
361 content::WebContents::FromRenderFrameHost(rfh);
362
363 int request_id = query_request_id_counter_++;
364 base::string16 selector = base::UTF8ToUTF16(params->args.selector);
365
366 // QuerySelectorHandler handles IPCs and deletes itself on completion.
367 new QuerySelectorHandler(
368 contents, request_id, params->args.automation_node_id, selector,
369 base::Bind(&AutomationInternalQuerySelectorFunction::OnResponse, this));
370
371 return RespondLater();
372 }
373
374 void AutomationInternalQuerySelectorFunction::OnResponse(
375 const std::string& error,
376 int result_acc_obj_id) {
377 if (!error.empty()) {
378 Respond(Error(error));
379 return;
380 }
381
382 Respond(OneArgument(new base::FundamentalValue(result_acc_obj_id)));
383 }
384
254 } // namespace extensions 385 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698