Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / extensions / api / automation_internal / automation_internal_api.cc
blob9c77eda2fc5e75c92c24784360d5c792bb156feb
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/automation_internal/automation_internal_api.h"
7 #include <vector>
9 #include "base/command_line.h"
10 #include "chrome/browser/extensions/api/automation_internal/automation_util.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/extensions/api/automation_internal.h"
15 #include "content/public/browser/ax_event_notification_details.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/render_view_host.h"
18 #include "content/public/browser/render_widget_host.h"
19 #include "content/public/browser/render_widget_host_view.h"
20 #include "content/public/browser/web_contents.h"
22 namespace extensions {
23 class AutomationWebContentsObserver;
24 } // namespace extensions
26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::AutomationWebContentsObserver);
28 namespace extensions {
30 // Helper class that receives accessibility data from |WebContents|.
31 class AutomationWebContentsObserver
32 : public content::WebContentsObserver,
33 public content::WebContentsUserData<AutomationWebContentsObserver> {
34 public:
35 virtual ~AutomationWebContentsObserver() {}
37 // content::WebContentsObserver overrides.
38 virtual void AccessibilityEventReceived(
39 const std::vector<content::AXEventNotificationDetails>& details)
40 OVERRIDE {
41 automation_util::DispatchAccessibilityEventsToAutomation(
42 details, browser_context_);
45 private:
46 friend class content::WebContentsUserData<AutomationWebContentsObserver>;
48 AutomationWebContentsObserver(
49 content::WebContents* web_contents)
50 : content::WebContentsObserver(web_contents),
51 browser_context_(web_contents->GetBrowserContext()) {}
53 content::BrowserContext* browser_context_;
55 DISALLOW_COPY_AND_ASSIGN(AutomationWebContentsObserver);
58 // TODO(aboxhall/dtseng): ensure that the initial data is sent down for the tab
59 // if this doesn't turn accessibility on for the first time (e.g. if a
60 // RendererAccessibility object existed already because a screenreader has been
61 // run at some point).
62 bool AutomationInternalEnableCurrentTabFunction::RunImpl() {
63 if (!CommandLine::ForCurrentProcess()->HasSwitch(
64 switches::kEnableAutomationAPI)) {
65 return false;
68 Browser* current_browser = GetCurrentBrowser();
69 TabStripModel* tab_strip = current_browser->tab_strip_model();
70 content::WebContents* contents =
71 tab_strip->GetWebContentsAt(tab_strip->active_index());
72 if (!contents)
73 return false;
74 content::RenderWidgetHost* rwh =
75 contents->GetRenderWidgetHostView()->GetRenderWidgetHost();
76 if (!rwh)
77 return false;
79 results_ = api::automation_internal::EnableCurrentTab::Results::Create(
80 rwh->GetProcess()->GetID(), rwh->GetRoutingID());
82 SendResponse(true);
84 AutomationWebContentsObserver::CreateForWebContents(contents);
86 rwh->EnableTreeOnlyAccessibilityMode();
88 return true;
91 bool AutomationInternalPerformActionFunction::RunImpl() {
92 using api::automation_internal::PerformAction::Params;
93 scoped_ptr<Params> params(Params::Create(*args_));
94 EXTENSION_FUNCTION_VALIDATE(params.get());
96 content::RenderWidgetHost* rwh =
97 content::RenderWidgetHost::FromID(params->args.process_id,
98 params->args.routing_id);
100 switch (params->args.action_type) {
101 case api::automation_internal::ACTION_TYPE_DO_DEFAULT:
102 rwh->AccessibilityDoDefaultAction(params->args.automation_node_id);
103 break;
104 case api::automation_internal::ACTION_TYPE_FOCUS:
105 rwh->AccessibilitySetFocus(params->args.automation_node_id);
106 break;
107 case api::automation_internal::ACTION_TYPE_MAKE_VISIBLE:
108 rwh->AccessibilityScrollToMakeVisible(params->args.automation_node_id,
109 gfx::Rect());
110 break;
111 case api::automation_internal::ACTION_TYPE_SET_SELECTION: {
112 extensions::api::automation_internal::SetSelectionParams selection_params;
113 EXTENSION_FUNCTION_VALIDATE(
114 extensions::api::automation_internal::SetSelectionParams::Populate(
115 params->opt_args.additional_properties, &selection_params));
116 rwh->AccessibilitySetTextSelection(params->args.automation_node_id,
117 selection_params.start_index,
118 selection_params.end_index);
119 break;
121 default:
122 NOTREACHED();
124 return true;
127 } // namespace extensions