Commit a1a098cf authored by dpapad's avatar dpapad Committed by Commit Bot

Migrate CallJavascriptFunction to FireWebUIListener in chrome://accessibility.

Calling JS functions by name from C++ is an obsolete pattern. Use
FireWebUIListener/addWebUIListener which is the suggested approach.

Also extracting all chrome.send() calls to a local BrowserProxy class
which makes the code more readable and is consistent with the
BrowserProxy pattern used in other WebUIs.

Bug: None
Change-Id: I911b114b6687e7b0108296a38636811bc066dd12
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2450722
Commit-Queue: dpapad <dpapad@chromium.org>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814634}
parent 9893b4cf
...@@ -458,7 +458,7 @@ void AccessibilityUIMessageHandler::ToggleAccessibility( ...@@ -458,7 +458,7 @@ void AccessibilityUIMessageHandler::ToggleAccessibility(
// accessibility mode buttons are updated. // accessibility mode buttons are updated.
AllowJavascript(); AllowJavascript();
std::unique_ptr<base::DictionaryValue> new_mode(BuildTargetDescriptor(rvh)); std::unique_ptr<base::DictionaryValue> new_mode(BuildTargetDescriptor(rvh));
CallJavascriptFunction("showOrRefreshTree", *(new_mode.get())); FireWebUIListener("showOrRefreshTree", *(new_mode.get()));
} }
} }
...@@ -552,7 +552,7 @@ void AccessibilityUIMessageHandler::RequestWebContentsTree( ...@@ -552,7 +552,7 @@ void AccessibilityUIMessageHandler::RequestWebContentsTree(
result->SetInteger(kProcessIdField, process_id); result->SetInteger(kProcessIdField, process_id);
result->SetInteger(kRoutingIdField, routing_id); result->SetInteger(kRoutingIdField, routing_id);
result->SetString(kErrorField, "Renderer no longer exists."); result->SetString(kErrorField, "Renderer no longer exists.");
CallJavascriptFunction(request_type, *(result.get())); FireWebUIListener(request_type, *(result.get()));
return; return;
} }
...@@ -582,7 +582,7 @@ void AccessibilityUIMessageHandler::RequestWebContentsTree( ...@@ -582,7 +582,7 @@ void AccessibilityUIMessageHandler::RequestWebContentsTree(
std::string accessibility_contents = std::string accessibility_contents =
web_contents->DumpAccessibilityTree(internal, property_filters); web_contents->DumpAccessibilityTree(internal, property_filters);
result->SetString(kTreeField, accessibility_contents); result->SetString(kTreeField, accessibility_contents);
CallJavascriptFunction(request_type, *(result.get())); FireWebUIListener(request_type, *(result.get()));
} }
void AccessibilityUIMessageHandler::RequestNativeUITree( void AccessibilityUIMessageHandler::RequestNativeUITree(
...@@ -630,7 +630,7 @@ void AccessibilityUIMessageHandler::RequestNativeUITree( ...@@ -630,7 +630,7 @@ void AccessibilityUIMessageHandler::RequestNativeUITree(
result->SetKey(kTreeField, result->SetKey(kTreeField,
base::Value(RecursiveDumpAXPlatformNodeAsString( base::Value(RecursiveDumpAXPlatformNodeAsString(
node, 0, property_filters))); node, 0, property_filters)));
CallJavascriptFunction(request_type, *(result.get())); FireWebUIListener(request_type, *(result.get()));
return; return;
} }
} }
...@@ -640,7 +640,7 @@ void AccessibilityUIMessageHandler::RequestNativeUITree( ...@@ -640,7 +640,7 @@ void AccessibilityUIMessageHandler::RequestNativeUITree(
result->SetInteger(kSessionIdField, session_id); result->SetInteger(kSessionIdField, session_id);
result->SetString(kTypeField, kBrowser); result->SetString(kTypeField, kBrowser);
result->SetString(kErrorField, "Browser no longer exists."); result->SetString(kErrorField, "Browser no longer exists.");
CallJavascriptFunction(request_type, *(result.get())); FireWebUIListener(request_type, *(result.get()));
} }
void AccessibilityUIMessageHandler::Callback(const std::string& str) { void AccessibilityUIMessageHandler::Callback(const std::string& str) {
...@@ -693,7 +693,7 @@ void AccessibilityUIMessageHandler::RequestAccessibilityEvents( ...@@ -693,7 +693,7 @@ void AccessibilityUIMessageHandler::RequestAccessibilityEvents(
result->SetString(kEventLogsField, event_logs_str); result->SetString(kEventLogsField, event_logs_str);
event_logs_.clear(); event_logs_.clear();
CallJavascriptFunction("startOrStopEvents", *(result.get())); FireWebUIListener("startOrStopEvents", *(result.get()));
} }
} }
......
...@@ -10,5 +10,8 @@ js_type_check("closure_compile") { ...@@ -10,5 +10,8 @@ js_type_check("closure_compile") {
} }
js_library("accessibility") { js_library("accessibility") {
deps = [ "//ui/webui/resources/js:util.m" ] deps = [
"//ui/webui/resources/js:cr.m",
"//ui/webui/resources/js:util.m",
]
} }
...@@ -4,8 +4,45 @@ ...@@ -4,8 +4,45 @@
import 'chrome://resources/js/action_link.js'; import 'chrome://resources/js/action_link.js';
import {addWebUIListener} from 'chrome://resources/js/cr.m.js';
import {$} from 'chrome://resources/js/util.m.js'; import {$} from 'chrome://resources/js/util.m.js';
class BrowserProxy {
toggleAccessibility(processId, routingId, modeId, shouldRequestTree) {
chrome.send('toggleAccessibility', [{
processId,
routingId,
modeId,
shouldRequestTree,
}]);
}
requestNativeUITree(sessionId, requestType, allow, allowEmpty, deny) {
chrome.send('requestNativeUITree', [{
sessionId,
requestType,
filters: {allow, allowEmpty, deny},
}]);
}
requestWebContentsTree(
processId, routingId, requestType, allow, allowEmpty, deny) {
chrome.send('requestWebContentsTree', [
{processId, routingId, requestType, filters: {allow, allowEmpty, deny}}
]);
}
requestAccessibilityEvents(processId, routingId, start) {
chrome.send('requestAccessibilityEvents', [{processId, routingId, start}]);
}
setGlobalFlag(flagName, enabled) {
chrome.send('setGlobalFlag', [{flagName, enabled}]);
}
}
const browserProxy = new BrowserProxy();
// Note: keep these values in sync with the values in // Note: keep these values in sync with the values in
// ui/accessibility/ax_mode.h // ui/accessibility/ax_mode.h
const AXMode = { const AXMode = {
...@@ -59,12 +96,8 @@ function toggleAccessibility(data, element, mode, globalStateName) { ...@@ -59,12 +96,8 @@ function toggleAccessibility(data, element, mode, globalStateName) {
const tree = $(id + ':tree'); const tree = $(id + ':tree');
// If the tree is visible, request a new tree with the updated mode. // If the tree is visible, request a new tree with the updated mode.
const shouldRequestTree = !!tree && tree.style.display != 'none'; const shouldRequestTree = !!tree && tree.style.display != 'none';
chrome.send('toggleAccessibility', [{ browserProxy.toggleAccessibility(
'processId': data.processId, data.processId, data.routingId, mode, shouldRequestTree);
'routingId': data.routingId,
'modeId': mode,
'shouldRequestTree': shouldRequestTree
}]);
} }
function requestTree(data, element) { function requestTree(data, element) {
...@@ -83,21 +116,12 @@ function requestTree(data, element) { ...@@ -83,21 +116,12 @@ function requestTree(data, element) {
if (data.type == 'browser') { if (data.type == 'browser') {
const delay = $('native-ui-delay').value; const delay = $('native-ui-delay').value;
setTimeout(() => { setTimeout(() => {
chrome.send( browserProxy.requestNativeUITree(
'requestNativeUITree', [{ data.sessionId, requestType, allow, allowEmpty, deny);
'sessionId': data.sessionId,
'requestType': requestType,
'filters': {'allow': allow, 'allowEmpty': allowEmpty, 'deny': deny}
}]);
}, delay); }, delay);
} else { } else {
chrome.send( browserProxy.requestWebContentsTree(
'requestWebContentsTree', [{ data.processId, data.routingId, requestType, allow, allowEmpty, deny);
'processId': data.processId,
'routingId': data.routingId,
'requestType': requestType,
'filters': {'allow': allow, 'allowEmpty': allowEmpty, 'deny': deny}
}]);
} }
} }
...@@ -127,9 +151,8 @@ function requestEvents(data, element) { ...@@ -127,9 +151,8 @@ function requestEvents(data, element) {
} }
} }
} }
chrome.send('requestAccessibilityEvents', [ browserProxy.requestAccessibilityEvents(
{'processId': data.processId, 'routingId': data.routingId, 'start': start} data.processId, data.routingId, start);
]);
} }
function initialize() { function initialize() {
...@@ -164,6 +187,10 @@ function initialize() { ...@@ -164,6 +187,10 @@ function initialize() {
$('filter-allow').value = allow ? allow : '*'; $('filter-allow').value = allow ? allow : '*';
$('filter-allow-empty').value = allowEmpty ? allowEmpty : ''; $('filter-allow-empty').value = allowEmpty ? allowEmpty : '';
$('filter-deny').value = deny ? deny : ''; $('filter-deny').value = deny ? deny : '';
addWebUIListener('copyTree', copyTree);
addWebUIListener('showOrRefreshTree', showOrRefreshTree);
addWebUIListener('startOrStopEvents', startOrStopEvents);
} }
function bindCheckbox(name, value) { function bindCheckbox(name, value) {
...@@ -175,8 +202,7 @@ function bindCheckbox(name, value) { ...@@ -175,8 +202,7 @@ function bindCheckbox(name, value) {
$(name).labels[0].classList.add('disabled'); $(name).labels[0].classList.add('disabled');
} }
$(name).addEventListener('change', function() { $(name).addEventListener('change', function() {
chrome.send( browserProxy.setGlobalFlag(name, $(name).checked);
'setGlobalFlag', [{'flagName': name, 'enabled': $(name).checked}]);
document.location.reload(); document.location.reload();
}); });
} }
...@@ -418,7 +444,7 @@ function createErrorMessageElement(data) { ...@@ -418,7 +444,7 @@ function createErrorMessageElement(data) {
return errorMessageElement; return errorMessageElement;
} }
// Called from C++ // WebUI listener handler for the 'showOrRefreshTree' event.
function showOrRefreshTree(data) { function showOrRefreshTree(data) {
const id = getIdFromData(data); const id = getIdFromData(data);
const row = $(id); const row = $(id);
...@@ -431,7 +457,7 @@ function showOrRefreshTree(data) { ...@@ -431,7 +457,7 @@ function showOrRefreshTree(data) {
$(id + ':showOrRefreshTree').focus(); $(id + ':showOrRefreshTree').focus();
} }
// Called from C++ // WebUI listener handler for the 'startOrStopEvents' event.
function startOrStopEvents(data) { function startOrStopEvents(data) {
const id = getIdFromData(data); const id = getIdFromData(data);
const row = $(id); const row = $(id);
...@@ -444,7 +470,7 @@ function startOrStopEvents(data) { ...@@ -444,7 +470,7 @@ function startOrStopEvents(data) {
$(id + ':startOrStopEvents').focus(); $(id + ':startOrStopEvents').focus();
} }
// Called from C++ // WebUI listener handler for the 'copyTree' event.
function copyTree(data) { function copyTree(data) {
const id = getIdFromData(data); const id = getIdFromData(data);
const row = $(id); const row = $(id);
...@@ -501,9 +527,4 @@ function createAccessibilityOutputElement(data, id, type) { ...@@ -501,9 +527,4 @@ function createAccessibilityOutputElement(data, id, type) {
return treeElement; return treeElement;
} }
// These are the functions we export so they can be called from C++.
window.copyTree = copyTree;
window.showOrRefreshTree = showOrRefreshTree;
window.startOrStopEvents = startOrStopEvents;
document.addEventListener('DOMContentLoaded', initialize); document.addEventListener('DOMContentLoaded', initialize);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment