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"
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/browser/renderer_host/render_widget_host_view_base.h"
18 #include "content/browser/web_contents/web_contents_impl.h"
19 #include "content/common/view_message_enums.h"
20 #include "content/grit/content_resources.h"
21 #include "content/public/browser/favicon_status.h"
22 #include "content/public/browser/navigation_entry.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/render_widget_host.h"
26 #include "content/public/browser/render_widget_host_iterator.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_ui_data_source.h"
29 #include "content/public/common/url_constants.h"
30 #include "net/base/escape.h"
32 static const char kDataFile
[] = "targets-data.json";
34 static const char kProcessIdField
[] = "processId";
35 static const char kRouteIdField
[] = "routeId";
36 static const char kUrlField
[] = "url";
37 static const char kNameField
[] = "name";
38 static const char kFaviconUrlField
[] = "favicon_url";
39 static const char kPidField
[] = "pid";
40 static const char kAccessibilityModeField
[] = "a11y_mode";
46 base::DictionaryValue
* BuildTargetDescriptor(
48 const std::string
& name
,
49 const GURL
& favicon_url
,
52 AccessibilityMode accessibility_mode
,
53 base::ProcessHandle handle
= base::kNullProcessHandle
) {
54 base::DictionaryValue
* target_data
= new base::DictionaryValue();
55 target_data
->SetInteger(kProcessIdField
, process_id
);
56 target_data
->SetInteger(kRouteIdField
, route_id
);
57 target_data
->SetString(kUrlField
, url
.spec());
58 target_data
->SetString(kNameField
, net::EscapeForHTML(name
));
59 target_data
->SetInteger(kPidField
, base::GetProcId(handle
));
60 target_data
->SetString(kFaviconUrlField
, favicon_url
.spec());
61 target_data
->SetInteger(kAccessibilityModeField
,
66 base::DictionaryValue
* BuildTargetDescriptor(RenderViewHost
* rvh
) {
67 WebContentsImpl
* web_contents
= static_cast<WebContentsImpl
*>(
68 WebContents::FromRenderViewHost(rvh
));
69 AccessibilityMode accessibility_mode
= web_contents
->GetAccessibilityMode();
75 // TODO(nasko): Fix the following code to use a consistent set of data
76 // across the URL, title, and favicon.
77 url
= web_contents
->GetURL();
78 title
= base::UTF16ToUTF8(web_contents
->GetTitle());
79 NavigationController
& controller
= web_contents
->GetController();
80 NavigationEntry
* entry
= controller
.GetVisibleEntry();
81 if (entry
!= NULL
&& entry
->GetURL().is_valid())
82 favicon_url
= entry
->GetFavicon().url
;
85 return BuildTargetDescriptor(url
,
88 rvh
->GetProcess()->GetID(),
93 bool HandleRequestCallback(BrowserContext
* current_context
,
94 const std::string
& path
,
95 const WebUIDataSource::GotDataCallback
& callback
) {
96 if (path
!= kDataFile
)
98 scoped_ptr
<base::ListValue
> rvh_list(new base::ListValue());
100 scoped_ptr
<RenderWidgetHostIterator
> widgets(
101 RenderWidgetHost::GetRenderWidgetHosts());
103 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
104 // Ignore processes that don't have a connection, such as crashed tabs.
105 if (!widget
->GetProcess()->HasConnection())
107 if (!widget
->IsRenderView())
109 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(widget
);
110 BrowserContext
* context
= rwhi
->GetProcess()->GetBrowserContext();
111 if (context
!= current_context
)
114 RenderViewHost
* rvh
= RenderViewHost::From(widget
);
115 rvh_list
->Append(BuildTargetDescriptor(rvh
));
118 scoped_ptr
<base::DictionaryValue
> data(new base::DictionaryValue());
119 data
->Set("list", rvh_list
.release());
122 BrowserAccessibilityStateImpl::GetInstance()->accessibility_mode());
124 std::string json_string
;
125 base::JSONWriter::Write(data
.get(), &json_string
);
127 callback
.Run(base::RefCountedString::TakeString(&json_string
));
133 AccessibilityUI::AccessibilityUI(WebUI
* web_ui
) : WebUIController(web_ui
) {
134 // Set up the chrome://accessibility source.
135 WebUIDataSource
* html_source
=
136 WebUIDataSource::Create(kChromeUIAccessibilityHost
);
138 web_ui
->RegisterMessageCallback(
139 "toggleAccessibility",
140 base::Bind(&AccessibilityUI::ToggleAccessibility
,
141 base::Unretained(this)));
142 web_ui
->RegisterMessageCallback(
143 "toggleGlobalAccessibility",
144 base::Bind(&AccessibilityUI::ToggleGlobalAccessibility
,
145 base::Unretained(this)));
146 web_ui
->RegisterMessageCallback(
147 "requestAccessibilityTree",
148 base::Bind(&AccessibilityUI::RequestAccessibilityTree
,
149 base::Unretained(this)));
151 // Add required resources.
152 html_source
->SetJsonPath("strings.js");
153 html_source
->AddResourcePath("accessibility.css", IDR_ACCESSIBILITY_CSS
);
154 html_source
->AddResourcePath("accessibility.js", IDR_ACCESSIBILITY_JS
);
155 html_source
->SetDefaultResource(IDR_ACCESSIBILITY_HTML
);
156 html_source
->SetRequestFilter(
157 base::Bind(&HandleRequestCallback
,
158 web_ui
->GetWebContents()->GetBrowserContext()));
160 BrowserContext
* browser_context
=
161 web_ui
->GetWebContents()->GetBrowserContext();
162 WebUIDataSource::Add(browser_context
, html_source
);
165 AccessibilityUI::~AccessibilityUI() {}
167 void AccessibilityUI::ToggleAccessibility(const base::ListValue
* args
) {
168 std::string process_id_str
;
169 std::string route_id_str
;
172 CHECK_EQ(2U, args
->GetSize());
173 CHECK(args
->GetString(0, &process_id_str
));
174 CHECK(args
->GetString(1, &route_id_str
));
175 CHECK(base::StringToInt(process_id_str
, &process_id
));
176 CHECK(base::StringToInt(route_id_str
, &route_id
));
178 RenderViewHost
* rvh
= RenderViewHost::FromID(process_id
, route_id
);
181 WebContentsImpl
* web_contents
= static_cast<WebContentsImpl
*>(
182 WebContents::FromRenderViewHost(rvh
));
183 AccessibilityMode mode
= web_contents
->GetAccessibilityMode();
184 if ((mode
& AccessibilityModeComplete
) != AccessibilityModeComplete
) {
185 web_contents
->AddAccessibilityMode(AccessibilityModeComplete
);
187 web_contents
->SetAccessibilityMode(
188 BrowserAccessibilityStateImpl::GetInstance()->accessibility_mode());
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();
199 state
->DisableAccessibility();
202 void AccessibilityUI::RequestAccessibilityTree(const base::ListValue
* args
) {
203 std::string process_id_str
;
204 std::string route_id_str
;
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
);
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()));
223 scoped_ptr
<base::DictionaryValue
> result(BuildTargetDescriptor(rvh
));
224 WebContents
* web_contents
= WebContents::FromRenderViewHost(rvh
);
225 scoped_ptr
<AccessibilityTreeFormatter
> formatter(
226 AccessibilityTreeFormatter::Create(web_contents
));
227 base::string16 accessibility_contents_utf16
;
228 std::vector
<AccessibilityTreeFormatter::Filter
> filters
;
229 filters
.push_back(AccessibilityTreeFormatter::Filter(
230 base::ASCIIToUTF16("*"),
231 AccessibilityTreeFormatter::Filter::ALLOW
));
232 formatter
->SetFilters(filters
);
233 formatter
->FormatAccessibilityTree(&accessibility_contents_utf16
);
236 new base::StringValue(
237 base::UTF16ToUTF8(accessibility_contents_utf16
)));
238 web_ui()->CallJavascriptFunction("accessibility.showTree", *(result
.get()));
241 } // namespace content