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
= AccessibilityModeOff
;
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
;
83 accessibility_mode
= web_contents
->GetAccessibilityMode();
86 return BuildTargetDescriptor(url
,
89 rvh
->GetProcess()->GetID(),
94 bool HandleRequestCallback(BrowserContext
* current_context
,
95 const std::string
& path
,
96 const WebUIDataSource::GotDataCallback
& callback
) {
97 if (path
!= kDataFile
)
99 scoped_ptr
<base::ListValue
> rvh_list(new base::ListValue());
101 scoped_ptr
<RenderWidgetHostIterator
> widgets(
102 RenderWidgetHost::GetRenderWidgetHosts());
104 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
105 // Ignore processes that don't have a connection, such as crashed tabs.
106 if (!widget
->GetProcess()->HasConnection())
108 if (!widget
->IsRenderView())
110 RenderWidgetHostImpl
* rwhi
= RenderWidgetHostImpl::From(widget
);
111 BrowserContext
* context
= rwhi
->GetProcess()->GetBrowserContext();
112 if (context
!= current_context
)
115 RenderViewHost
* rvh
= RenderViewHost::From(widget
);
116 rvh_list
->Append(BuildTargetDescriptor(rvh
));
119 base::DictionaryValue data
;
120 data
.Set("list", rvh_list
.release());
123 BrowserAccessibilityStateImpl::GetInstance()->accessibility_mode());
125 std::string json_string
;
126 base::JSONWriter::Write(data
, &json_string
);
128 callback
.Run(base::RefCountedString::TakeString(&json_string
));
134 AccessibilityUI::AccessibilityUI(WebUI
* web_ui
) : WebUIController(web_ui
) {
135 // Set up the chrome://accessibility source.
136 WebUIDataSource
* html_source
=
137 WebUIDataSource::Create(kChromeUIAccessibilityHost
);
139 web_ui
->RegisterMessageCallback(
140 "toggleAccessibility",
141 base::Bind(&AccessibilityUI::ToggleAccessibility
,
142 base::Unretained(this)));
143 web_ui
->RegisterMessageCallback(
144 "toggleGlobalAccessibility",
145 base::Bind(&AccessibilityUI::ToggleGlobalAccessibility
,
146 base::Unretained(this)));
147 web_ui
->RegisterMessageCallback(
148 "requestAccessibilityTree",
149 base::Bind(&AccessibilityUI::RequestAccessibilityTree
,
150 base::Unretained(this)));
152 // Add required resources.
153 html_source
->SetJsonPath("strings.js");
154 html_source
->AddResourcePath("accessibility.css", IDR_ACCESSIBILITY_CSS
);
155 html_source
->AddResourcePath("accessibility.js", IDR_ACCESSIBILITY_JS
);
156 html_source
->SetDefaultResource(IDR_ACCESSIBILITY_HTML
);
157 html_source
->SetRequestFilter(
158 base::Bind(&HandleRequestCallback
,
159 web_ui
->GetWebContents()->GetBrowserContext()));
161 BrowserContext
* browser_context
=
162 web_ui
->GetWebContents()->GetBrowserContext();
163 WebUIDataSource::Add(browser_context
, html_source
);
166 AccessibilityUI::~AccessibilityUI() {}
168 void AccessibilityUI::ToggleAccessibility(const base::ListValue
* args
) {
169 std::string process_id_str
;
170 std::string route_id_str
;
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
);
182 WebContentsImpl
* web_contents
= static_cast<WebContentsImpl
*>(
183 WebContents::FromRenderViewHost(rvh
));
184 AccessibilityMode mode
= web_contents
->GetAccessibilityMode();
185 if ((mode
& AccessibilityModeComplete
) != AccessibilityModeComplete
) {
186 web_contents
->AddAccessibilityMode(AccessibilityModeComplete
);
188 web_contents
->SetAccessibilityMode(
189 BrowserAccessibilityStateImpl::GetInstance()->accessibility_mode());
193 void AccessibilityUI::ToggleGlobalAccessibility(const base::ListValue
* args
) {
194 BrowserAccessibilityStateImpl
* state
=
195 BrowserAccessibilityStateImpl::GetInstance();
196 AccessibilityMode mode
= state
->accessibility_mode();
197 if ((mode
& AccessibilityModeComplete
) != AccessibilityModeComplete
)
198 state
->EnableAccessibility();
200 state
->DisableAccessibility();
203 void AccessibilityUI::RequestAccessibilityTree(const base::ListValue
* args
) {
204 std::string process_id_str
;
205 std::string route_id_str
;
208 CHECK_EQ(2U, args
->GetSize());
209 CHECK(args
->GetString(0, &process_id_str
));
210 CHECK(args
->GetString(1, &route_id_str
));
211 CHECK(base::StringToInt(process_id_str
, &process_id
));
212 CHECK(base::StringToInt(route_id_str
, &route_id
));
214 RenderViewHost
* rvh
= RenderViewHost::FromID(process_id
, route_id
);
216 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue());
217 result
->SetInteger(kProcessIdField
, process_id
);
218 result
->SetInteger(kRouteIdField
, route_id
);
219 result
->Set("error", new base::StringValue("Renderer no longer exists."));
220 web_ui()->CallJavascriptFunction("accessibility.showTree", *(result
.get()));
224 scoped_ptr
<base::DictionaryValue
> result(BuildTargetDescriptor(rvh
));
225 WebContents
* web_contents
= WebContents::FromRenderViewHost(rvh
);
226 scoped_ptr
<AccessibilityTreeFormatter
> formatter(
227 AccessibilityTreeFormatter::Create(web_contents
));
228 base::string16 accessibility_contents_utf16
;
229 std::vector
<AccessibilityTreeFormatter::Filter
> filters
;
230 filters
.push_back(AccessibilityTreeFormatter::Filter(
231 base::ASCIIToUTF16("*"),
232 AccessibilityTreeFormatter::Filter::ALLOW
));
233 formatter
->SetFilters(filters
);
234 formatter
->FormatAccessibilityTree(&accessibility_contents_utf16
);
237 new base::StringValue(
238 base::UTF16ToUTF8(accessibility_contents_utf16
)));
239 web_ui()->CallJavascriptFunction("accessibility.showTree", *(result
.get()));
242 } // namespace content