1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/automation_internal/automation_internal_api.h"
#include <vector>
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/extensions/api/automation_internal/automation_action_adapter.h"
#include "chrome/browser/extensions/api/automation_internal/automation_util.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/extensions/api/automation_internal.h"
#include "chrome/common/extensions/manifest_handlers/automation.h"
#include "content/public/browser/ax_event_notification_details.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/permissions/permissions_data.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/ui/ash/accessibility/automation_manager_ash.h"
#endif
namespace extensions {
class AutomationWebContentsObserver;
} // namespace extensions
DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver);
namespace {
const int kDesktopProcessID = 0;
const int kDesktopRoutingID = 0;
const char kCannotRequestAutomationOnPage[] =
"Cannot request automation tree on url \"*\". "
"Extension manifest must request permission to access this host.";
} // namespace
namespace extensions {
bool CanRequestAutomation(const Extension* extension,
const AutomationInfo* automation_info,
const content::WebContents* contents) {
if (automation_info->desktop)
return true;
const GURL& url = contents->GetURL();
// TODO(aboxhall): check for webstore URL
if (automation_info->matches.MatchesURL(url))
return true;
int tab_id = ExtensionTabUtil::GetTabId(contents);
content::RenderProcessHost* process = contents->GetRenderProcessHost();
int process_id = process ? process->GetID() : -1;
std::string unused_error;
return extension->permissions_data()->CanAccessPage(
extension, url, url, tab_id, process_id, &unused_error);
}
// Helper class that receives accessibility data from |WebContents|.
class AutomationWebContentsObserver
: public content::WebContentsObserver,
public content::WebContentsUserData<AutomationWebContentsObserver> {
public:
virtual ~AutomationWebContentsObserver() {}
// content::WebContentsObserver overrides.
virtual void AccessibilityEventReceived(
const std::vector<content::AXEventNotificationDetails>& details)
OVERRIDE {
automation_util::DispatchAccessibilityEventsToAutomation(
details, browser_context_);
}
private:
friend class content::WebContentsUserData<AutomationWebContentsObserver>;
AutomationWebContentsObserver(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
browser_context_(web_contents->GetBrowserContext()) {}
content::BrowserContext* browser_context_;
DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver);
};
// Helper class that implements an action adapter for a |RenderWidgetHost|.
class RenderWidgetHostActionAdapter : public AutomationActionAdapter {
public:
explicit RenderWidgetHostActionAdapter(content::RenderWidgetHost* rwh)
: rwh_(rwh) {}
virtual ~RenderWidgetHostActionAdapter() {}
// AutomationActionAdapter implementation.
virtual void DoDefault(int32 id) OVERRIDE {
rwh_->AccessibilityDoDefaultAction(id);
}
virtual void Focus(int32 id) OVERRIDE {
rwh_->AccessibilitySetFocus(id);
}
virtual void MakeVisible(int32 id) OVERRIDE {
rwh_->AccessibilityScrollToMakeVisible(id, gfx::Rect());
}
virtual void SetSelection(int32 id, int32 start, int32 end) OVERRIDE {
rwh_->AccessibilitySetTextSelection(id, start, end);
}
private:
content::RenderWidgetHost* rwh_;
DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostActionAdapter);
};
ExtensionFunction::ResponseAction
AutomationInternalEnableTabFunction::Run() {
const AutomationInfo* automation_info = AutomationInfo::Get(GetExtension());
EXTENSION_FUNCTION_VALIDATE(automation_info);
using api::automation_internal::EnableTab::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
content::WebContents* contents = NULL;
if (params->tab_id.get()) {
int tab_id = *params->tab_id;
if (!ExtensionTabUtil::GetTabById(tab_id,
GetProfile(),
include_incognito(),
NULL, /* browser out param*/
NULL, /* tab_strip out param */
&contents,
NULL /* tab_index out param */)) {
return RespondNow(
Error(tabs_constants::kTabNotFoundError, base::IntToString(tab_id)));
}
} else {
contents = GetCurrentBrowser()->tab_strip_model()->GetActiveWebContents();
if (!contents)
return RespondNow(Error("No active tab"));
}
content::RenderWidgetHost* rwh =
contents->GetRenderWidgetHostView()->GetRenderWidgetHost();
if (!rwh)
return RespondNow(Error("Could not enable accessibility for active tab"));
if (!CanRequestAutomation(GetExtension(), automation_info, contents)) {
return RespondNow(
Error(kCannotRequestAutomationOnPage, contents->GetURL().spec()));
}
AutomationWebContentsObserver::CreateForWebContents(contents);
rwh->EnableTreeOnlyAccessibilityMode();
return RespondNow(
ArgumentList(api::automation_internal::EnableTab::Results::Create(
rwh->GetProcess()->GetID(), rwh->GetRoutingID())));
}
ExtensionFunction::ResponseAction
AutomationInternalPerformActionFunction::Run() {
const AutomationInfo* automation_info = AutomationInfo::Get(GetExtension());
EXTENSION_FUNCTION_VALIDATE(automation_info && automation_info->interact);
using api::automation_internal::PerformAction::Params;
scoped_ptr<Params> params(Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
if (params->args.process_id == kDesktopProcessID &&
params->args.routing_id == kDesktopRoutingID) {
#if defined(OS_CHROMEOS)
return RouteActionToAdapter(
params.get(), AutomationManagerAsh::GetInstance());
#else
NOTREACHED();
return RespondNow(Error("Unexpected action on desktop automation tree;"
" platform does not support desktop automation"));
#endif // defined(OS_CHROMEOS)
}
content::RenderWidgetHost* rwh = content::RenderWidgetHost::FromID(
params->args.process_id, params->args.routing_id);
if (!rwh)
return RespondNow(Error("Ignoring action on destroyed node"));
if (rwh->IsRenderView()) {
const content::RenderViewHost* rvh = content::RenderViewHost::From(rwh);
const content::WebContents* contents =
content::WebContents::FromRenderViewHost(rvh);
if (!CanRequestAutomation(GetExtension(), automation_info, contents)) {
return RespondNow(
Error(kCannotRequestAutomationOnPage, contents->GetURL().spec()));
}
}
RenderWidgetHostActionAdapter adapter(rwh);
return RouteActionToAdapter(params.get(), &adapter);
}
ExtensionFunction::ResponseAction
AutomationInternalPerformActionFunction::RouteActionToAdapter(
api::automation_internal::PerformAction::Params* params,
AutomationActionAdapter* adapter) {
int32 automation_id = params->args.automation_node_id;
switch (params->args.action_type) {
case api::automation_internal::ACTION_TYPE_DODEFAULT:
adapter->DoDefault(automation_id);
break;
case api::automation_internal::ACTION_TYPE_FOCUS:
adapter->Focus(automation_id);
break;
case api::automation_internal::ACTION_TYPE_MAKEVISIBLE:
adapter->MakeVisible(automation_id);
break;
case api::automation_internal::ACTION_TYPE_SETSELECTION: {
api::automation_internal::SetSelectionParams selection_params;
EXTENSION_FUNCTION_VALIDATE(
api::automation_internal::SetSelectionParams::Populate(
params->opt_args.additional_properties, &selection_params));
adapter->SetSelection(automation_id,
selection_params.start_index,
selection_params.end_index);
break;
}
default:
NOTREACHED();
}
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
AutomationInternalEnableDesktopFunction::Run() {
#if defined(OS_CHROMEOS)
const AutomationInfo* automation_info = AutomationInfo::Get(GetExtension());
if (!automation_info || !automation_info->desktop)
return RespondNow(Error("desktop permission must be requested"));
AutomationManagerAsh::GetInstance()->Enable(browser_context());
return RespondNow(NoArguments());
#else
return RespondNow(Error("getDesktop is unsupported by this platform"));
#endif // defined(OS_CHROMEOS)
}
} // namespace extensions
|