Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / accessibility / accessibility_ui.cc
bloba052982957b5c7d24acb0705b15a8bc383bdf041
1 // Copyright (c) 2013 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 "content/browser/accessibility/accessibility_ui.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/json/json_writer.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "content/browser/accessibility/accessibility_tree_formatter.h"
14 #include "content/browser/accessibility/browser_accessibility_manager.h"
15 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
16 #include "content/browser/renderer_host/render_widget_host_impl.h"
17 #include "content/common/view_message_enums.h"
18 #include "content/port/browser/render_widget_host_view_port.h"
19 #include "content/public/browser/favicon_status.h"
20 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/render_widget_host.h"
24 #include "content/public/browser/render_widget_host_iterator.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_ui_data_source.h"
27 #include "content/public/common/url_constants.h"
28 #include "grit/content_resources.h"
29 #include "net/base/escape.h"
31 static const char kDataFile[] = "targets-data.json";
33 static const char kProcessIdField[] = "processId";
34 static const char kRouteIdField[] = "routeId";
35 static const char kUrlField[] = "url";
36 static const char kNameField[] = "name";
37 static const char kFaviconUrlField[] = "favicon_url";
38 static const char kPidField[] = "pid";
39 static const char kAccessibilityModeField[] = "a11y_mode";
41 namespace content {
43 namespace {
45 base::DictionaryValue* BuildTargetDescriptor(
46 const GURL& url,
47 const std::string& name,
48 const GURL& favicon_url,
49 int process_id,
50 int route_id,
51 AccessibilityMode accessibility_mode,
52 base::ProcessHandle handle = base::kNullProcessHandle) {
53 base::DictionaryValue* target_data = new base::DictionaryValue();
54 target_data->SetInteger(kProcessIdField, process_id);
55 target_data->SetInteger(kRouteIdField, route_id);
56 target_data->SetString(kUrlField, url.spec());
57 target_data->SetString(kNameField, net::EscapeForHTML(name));
58 target_data->SetInteger(kPidField, base::GetProcId(handle));
59 target_data->SetString(kFaviconUrlField, favicon_url.spec());
60 target_data->SetInteger(kAccessibilityModeField,
61 accessibility_mode);
62 return target_data;
65 base::DictionaryValue* BuildTargetDescriptor(RenderViewHost* rvh) {
66 WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
67 std::string title;
68 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(rvh);
69 AccessibilityMode accessibility_mode = rwhi->accessibility_mode();
71 GURL url;
72 GURL favicon_url;
73 if (web_contents) {
74 // TODO(nasko): Fix the following code to use a consistent set of data
75 // across the URL, title, and favicon.
76 url = web_contents->GetURL();
77 title = base::UTF16ToUTF8(web_contents->GetTitle());
78 NavigationController& controller = web_contents->GetController();
79 NavigationEntry* entry = controller.GetVisibleEntry();
80 if (entry != NULL && entry->GetURL().is_valid())
81 favicon_url = entry->GetFavicon().url;
84 return BuildTargetDescriptor(url,
85 title,
86 favicon_url,
87 rvh->GetProcess()->GetID(),
88 rvh->GetRoutingID(),
89 accessibility_mode);
92 void SendTargetsData(
93 const WebUIDataSource::GotDataCallback& callback) {
94 scoped_ptr<base::ListValue> rvh_list(new base::ListValue());
96 scoped_ptr<RenderWidgetHostIterator> widgets(
97 RenderWidgetHost::GetRenderWidgetHosts());
98 while (RenderWidgetHost* widget = widgets->GetNextHost()) {
99 // Ignore processes that don't have a connection, such as crashed tabs.
100 if (!widget->GetProcess()->HasConnection())
101 continue;
102 if (!widget->IsRenderView())
103 continue;
105 RenderViewHost* rvh = RenderViewHost::From(widget);
106 rvh_list->Append(BuildTargetDescriptor(rvh));
109 scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
110 data->Set("list", rvh_list.release());
111 scoped_ptr<base::FundamentalValue> a11y_mode(base::Value::CreateIntegerValue(
112 BrowserAccessibilityStateImpl::GetInstance()->accessibility_mode()));
113 data->Set("global_a11y_mode", a11y_mode.release());
115 std::string json_string;
116 base::JSONWriter::Write(data.get(), &json_string);
118 callback.Run(base::RefCountedString::TakeString(&json_string));
121 bool HandleRequestCallback(
122 const std::string& path,
123 const WebUIDataSource::GotDataCallback& callback) {
124 if (path != kDataFile)
125 return false;
127 SendTargetsData(callback);
128 return true;
131 } // namespace
133 AccessibilityUI::AccessibilityUI(WebUI* web_ui)
134 : WebUIController(web_ui) {
135 // Set up the chrome://accessibility source.
136 WebUIDataSource* html_source =
137 WebUIDataSource::Create(kChromeUIAccessibilityHost);
138 html_source->SetUseJsonJSFormatV2();
140 web_ui->RegisterMessageCallback(
141 "toggleAccessibility",
142 base::Bind(&AccessibilityUI::ToggleAccessibility,
143 base::Unretained(this)));
144 web_ui->RegisterMessageCallback(
145 "toggleGlobalAccessibility",
146 base::Bind(&AccessibilityUI::ToggleGlobalAccessibility,
147 base::Unretained(this)));
148 web_ui->RegisterMessageCallback(
149 "requestAccessibilityTree",
150 base::Bind(&AccessibilityUI::RequestAccessibilityTree,
151 base::Unretained(this)));
153 // Add required resources.
154 html_source->SetJsonPath("strings.js");
155 html_source->AddResourcePath("accessibility.css", IDR_ACCESSIBILITY_CSS);
156 html_source->AddResourcePath("accessibility.js", IDR_ACCESSIBILITY_JS);
157 html_source->SetDefaultResource(IDR_ACCESSIBILITY_HTML);
158 html_source->SetRequestFilter(base::Bind(&HandleRequestCallback));
160 BrowserContext* browser_context =
161 web_ui->GetWebContents()->GetBrowserContext();
162 WebUIDataSource::Add(browser_context, html_source);
165 AccessibilityUI::~AccessibilityUI() {
168 void AccessibilityUI::ToggleAccessibility(const base::ListValue* args) {
169 std::string process_id_str;
170 std::string route_id_str;
171 int process_id;
172 int route_id;
173 CHECK_EQ(2U, args->GetSize());
174 CHECK(args->GetString(0, &process_id_str));
175 CHECK(args->GetString(1, &route_id_str));
176 CHECK(base::StringToInt(process_id_str, &process_id));
177 CHECK(base::StringToInt(route_id_str, &route_id));
179 RenderViewHost* rvh = RenderViewHost::FromID(process_id, route_id);
180 if (!rvh)
181 return;
182 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(rvh);
183 if (!rwhi)
184 return;
185 AccessibilityMode mode = rwhi->accessibility_mode();
186 if ((mode & AccessibilityModeComplete) != AccessibilityModeComplete)
187 rwhi->AddAccessibilityMode(AccessibilityModeComplete);
188 else
189 rwhi->ResetAccessibilityMode();
192 void AccessibilityUI::ToggleGlobalAccessibility(const base::ListValue* args) {
193 BrowserAccessibilityStateImpl* state =
194 BrowserAccessibilityStateImpl::GetInstance();
195 AccessibilityMode mode = state->accessibility_mode();
196 if ((mode & AccessibilityModeComplete) != AccessibilityModeComplete)
197 state->EnableAccessibility();
198 else
199 state->DisableAccessibility();
202 void AccessibilityUI::RequestAccessibilityTree(const base::ListValue* args) {
203 std::string process_id_str;
204 std::string route_id_str;
205 int process_id;
206 int route_id;
207 CHECK_EQ(2U, args->GetSize());
208 CHECK(args->GetString(0, &process_id_str));
209 CHECK(args->GetString(1, &route_id_str));
210 CHECK(base::StringToInt(process_id_str, &process_id));
211 CHECK(base::StringToInt(route_id_str, &route_id));
213 RenderViewHost* rvh = RenderViewHost::FromID(process_id, route_id);
214 if (!rvh) {
215 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
216 result->SetInteger(kProcessIdField, process_id);
217 result->SetInteger(kRouteIdField, route_id);
218 result->Set("error", new base::StringValue("Renderer no longer exists."));
219 web_ui()->CallJavascriptFunction("accessibility.showTree", *(result.get()));
220 return;
223 scoped_ptr<base::DictionaryValue> result(BuildTargetDescriptor(rvh));
224 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>(
225 WebContents::FromRenderViewHost(rvh)->GetRenderWidgetHostView());
226 if (!host_view) {
227 result->Set("error",
228 new base::StringValue("Could not get accessibility tree."));
229 web_ui()->CallJavascriptFunction("accessibility.showTree", *(result.get()));
230 return;
232 scoped_ptr<AccessibilityTreeFormatter> formatter(
233 AccessibilityTreeFormatter::Create(rvh));
234 base::string16 accessibility_contents_utf16;
235 BrowserAccessibilityManager* manager =
236 host_view->GetBrowserAccessibilityManager();
237 if (!manager) {
238 result->Set("error",
239 new base::StringValue("Could not get accessibility tree."));
240 web_ui()->CallJavascriptFunction("accessibility.showTree", *(result.get()));
241 return;
243 std::vector<AccessibilityTreeFormatter::Filter> filters;
244 filters.push_back(AccessibilityTreeFormatter::Filter(
245 base::ASCIIToUTF16("*"),
246 AccessibilityTreeFormatter::Filter::ALLOW));
247 formatter->SetFilters(filters);
248 formatter->FormatAccessibilityTree(&accessibility_contents_utf16);
250 result->Set("tree",
251 new base::StringValue(
252 base::UTF16ToUTF8(accessibility_contents_utf16)));
253 web_ui()->CallJavascriptFunction("accessibility.showTree", *(result.get()));
256 } // namespace content