Commit 376b30f9 authored by dpapad's avatar dpapad Committed by Commit Bot

WebUI: Apply clang-format for JS under c/b/r/certificate_viewer/

This was purposefully skipped on previous CL that migrated this folder
to JS modules, to make reviewing easier.

Fixed: 1149868
Change-Id: I678d88e58a9e1412c0cda1fb9575b83ecfe431bd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2553288
Auto-Submit: dpapad <dpapad@chromium.org>
Reviewed-by: default avatarJohn Lee <johntlee@chromium.org>
Commit-Queue: John Lee <johntlee@chromium.org>
Commit-Queue: dpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#830271}
parent 042d3094
...@@ -11,217 +11,217 @@ import {TabBox} from 'chrome://resources/js/cr/ui/tabs.m.js'; ...@@ -11,217 +11,217 @@ import {TabBox} from 'chrome://resources/js/cr/ui/tabs.m.js';
import {Tree, TreeItem} from 'chrome://resources/js/cr/ui/tree.m.js'; import {Tree, TreeItem} from 'chrome://resources/js/cr/ui/tree.m.js';
import {$} from 'chrome://resources/js/util.m.js'; import {$} from 'chrome://resources/js/util.m.js';
/** /**
* @typedef {{ * @typedef {{
* general: !Object, * general: !Object,
* hierarchy: !Object, * hierarchy: !Object,
* }} * }}
*/ */
let CertificateInfo; let CertificateInfo;
/**
* Initialize the certificate viewer dialog by wiring up the close button,
* substituting in translated strings and requesting certificate details.
*/
function initialize() {
decorate('tabbox', TabBox);
const args = JSON.parse(chrome.getVariableValue('dialogArguments'));
getCertificateInfo(/** @type {!CertificateInfo} */ (args));
/** /**
* Initialize the certificate viewer dialog by wiring up the close button, * Initialize the second tab's contents.
* substituting in translated strings and requesting certificate details. * This is a 'oneShot' function, meaning it will only be invoked once,
* no matter how many times it is called. This is done for unit-testing
* purposes in case a test needs to initialize the tab before the timer
* fires.
*/ */
function initialize() { const initializeDetailTab = oneShot(function() {
decorate('tabbox', TabBox); initializeTree(assert($('hierarchy')), showCertificateFields);
initializeTree(assert($('cert-fields')), showCertificateFieldValue);
const args = JSON.parse(chrome.getVariableValue('dialogArguments')); createCertificateHierarchy(args.hierarchy);
getCertificateInfo(/** @type {!CertificateInfo} */ (args)); });
/** // The second tab's contents aren't visible on startup, so we can
* Initialize the second tab's contents. // shorten startup by not populating those controls until after we
* This is a 'oneShot' function, meaning it will only be invoked once, // have had a chance to draw the visible controls the first time.
* no matter how many times it is called. This is done for unit-testing // The value of 200ms is quick enough that the user couldn't open the
* purposes in case a test needs to initialize the tab before the timer // tab in that time but long enough to allow the first tab to draw on
* fires. // even the slowest machine.
*/ setTimeout(initializeDetailTab, 200);
const initializeDetailTab = oneShot(function() {
initializeTree(assert($('hierarchy')), showCertificateFields); $('tabbox').addEventListener('selectedChange', function f(e) {
initializeTree(assert($('cert-fields')), showCertificateFieldValue); $('tabbox').removeEventListener('selectedChange', f);
createCertificateHierarchy(args.hierarchy); initializeDetailTab();
}); }, true);
// The second tab's contents aren't visible on startup, so we can stripGtkAccessorKeys();
// shorten startup by not populating those controls until after we
// have had a chance to draw the visible controls the first time. $('export').onclick = exportCertificate;
// The value of 200ms is quick enough that the user couldn't open the }
// tab in that time but long enough to allow the first tab to draw on
// even the slowest machine. /**
setTimeout(initializeDetailTab, 200); * Decorate a function so that it can only be invoked once.
*/
$('tabbox').addEventListener('selectedChange', function f(e) { function oneShot(fn) {
$('tabbox').removeEventListener('selectedChange', f); let fired = false;
initializeDetailTab(); return function() {
}, true); if (fired) {
return;
stripGtkAccessorKeys();
$('export').onclick = exportCertificate;
}
/**
* Decorate a function so that it can only be invoked once.
*/
function oneShot(fn) {
let fired = false;
return function() {
if (fired) {
return;
}
fired = true;
fn();
};
}
/**
* Initialize a Tree object from a given element using the specified
* change handler.
* @param {!HTMLElement} tree The HTMLElement to style as a tree.
* @param {function()} handler Function to call when a node is selected.
*/
function initializeTree(tree, handler) {
decorate(tree, Tree);
tree['detail'] = {payload: {}, children: {}};
tree.addEventListener('change', handler);
}
/**
* The tab name strings in the languages file have accessor keys indicated
* by a preceding & sign. Strip these out for now.
* TODO(flackr) These accessor keys could be implemented with Javascript or
* translated strings could be added / modified to remove the & sign.
*/
function stripGtkAccessorKeys() {
// Copy all the tab labels into an array.
const nodes = Array.prototype.slice.call($('tabs').childNodes, 0);
nodes.push($('export'));
for (let i = 0; i < nodes.length; i++) {
nodes[i].textContent = nodes[i].textContent.replace('&', '');
} }
fired = true;
fn();
};
}
/**
* Initialize a Tree object from a given element using the specified
* change handler.
* @param {!HTMLElement} tree The HTMLElement to style as a tree.
* @param {function()} handler Function to call when a node is selected.
*/
function initializeTree(tree, handler) {
decorate(tree, Tree);
tree['detail'] = {payload: {}, children: {}};
tree.addEventListener('change', handler);
}
/**
* The tab name strings in the languages file have accessor keys indicated
* by a preceding & sign. Strip these out for now.
* TODO(flackr) These accessor keys could be implemented with Javascript or
* translated strings could be added / modified to remove the & sign.
*/
function stripGtkAccessorKeys() {
// Copy all the tab labels into an array.
const nodes = Array.prototype.slice.call($('tabs').childNodes, 0);
nodes.push($('export'));
for (let i = 0; i < nodes.length; i++) {
nodes[i].textContent = nodes[i].textContent.replace('&', '');
} }
}
/**
* Expand all nodes of the given tree object. /**
* @param {!Tree} tree The tree object to expand all nodes on. * Expand all nodes of the given tree object.
*/ * @param {!Tree} tree The tree object to expand all nodes on.
function revealTree(tree) { */
tree.expanded = true; function revealTree(tree) {
for (const key in tree['detail'].children) { tree.expanded = true;
revealTree(tree['detail'].children[key]); for (const key in tree['detail'].children) {
} revealTree(tree['detail'].children[key]);
} }
}
/**
* This function is called from certificate_viewer_ui.cc with the certificate /**
* information. Display all returned information to the user. * This function is called from certificate_viewer_ui.cc with the certificate
* @param {!CertificateInfo} certInfo Certificate information in named fields. * information. Display all returned information to the user.
*/ * @param {!CertificateInfo} certInfo Certificate information in named fields.
function getCertificateInfo(certInfo) { */
for (const key in certInfo.general) { function getCertificateInfo(certInfo) {
$(key).textContent = certInfo.general[key]; for (const key in certInfo.general) {
} $(key).textContent = certInfo.general[key];
} }
}
/**
* This function populates the certificate hierarchy. /**
* @param {Object} hierarchy A dictionary containing the hierarchy. * This function populates the certificate hierarchy.
*/ * @param {Object} hierarchy A dictionary containing the hierarchy.
function createCertificateHierarchy(hierarchy) { */
const treeItem = /** @type {!Tree} */ ($('hierarchy')); function createCertificateHierarchy(hierarchy) {
const root = constructTree(hierarchy[0]); const treeItem = /** @type {!Tree} */ ($('hierarchy'));
treeItem['detail'].children['root'] = root; const root = constructTree(hierarchy[0]);
treeItem.add(root); treeItem['detail'].children['root'] = root;
treeItem.add(root);
// Select the last item in the hierarchy (really we have a list here - each
// node has at most one child). This will reveal the parent nodes and // Select the last item in the hierarchy (really we have a list here - each
// populate the fields view. // node has at most one child). This will reveal the parent nodes and
let last = root; // populate the fields view.
while (last.detail.children && last.detail.children[0]) { let last = root;
last = last.detail.children[0]; while (last.detail.children && last.detail.children[0]) {
} last = last.detail.children[0];
last.selected = true;
}
/**
* Constructs a TreeItem corresponding to the passed in tree
* @param {Object} tree Dictionary describing the tree structure.
* @return {!TreeItem} Tree node corresponding to the input dictionary.
*/
function constructTree(tree) {
const treeItem = new TreeItem({
label: tree.label,
detail: {payload: tree.payload ? tree.payload : {}, children: {}}
});
if (tree.children) {
for (let i = 0; i < tree.children.length; i++) {
treeItem.add(
treeItem['detail'].children[i] = constructTree(tree.children[i]));
}
}
return treeItem;
} }
last.selected = true;
/** }
* Clear any previous certificate fields in the tree.
*/ /**
function clearCertificateFields() { * Constructs a TreeItem corresponding to the passed in tree
const treeItem = /** @type {!Tree} */ ($('cert-fields')); * @param {Object} tree Dictionary describing the tree structure.
for (const key in treeItem['detail'].children) { * @return {!TreeItem} Tree node corresponding to the input dictionary.
treeItem.remove(treeItem['detail'].children[key]); */
delete treeItem['detail'].children[key]; function constructTree(tree) {
const treeItem = new TreeItem({
label: tree.label,
detail: {payload: tree.payload ? tree.payload : {}, children: {}}
});
if (tree.children) {
for (let i = 0; i < tree.children.length; i++) {
treeItem.add(
treeItem['detail'].children[i] = constructTree(tree.children[i]));
} }
} }
return treeItem;
/** }
* Request certificate fields for the selected certificate in the hierarchy.
*/ /**
function showCertificateFields() { * Clear any previous certificate fields in the tree.
clearCertificateFields(); */
const item = $('hierarchy').selectedItem; function clearCertificateFields() {
if (item && item.detail.payload.index !== undefined) { const treeItem = /** @type {!Tree} */ ($('cert-fields'));
sendWithPromise('requestCertificateFields', item.detail.payload.index) for (const key in treeItem['detail'].children) {
.then(onCertificateFields); treeItem.remove(treeItem['detail'].children[key]);
} delete treeItem['detail'].children[key];
} }
}
/**
* Show the returned certificate fields for the selected certificate. /**
* @param {Object} certFields A dictionary containing the fields tree * Request certificate fields for the selected certificate in the hierarchy.
* structure. */
*/ function showCertificateFields() {
function onCertificateFields(certFields) { clearCertificateFields();
clearCertificateFields(); const item = $('hierarchy').selectedItem;
const treeItem = /** @type {!Tree} */ ($('cert-fields')); if (item && item.detail.payload.index !== undefined) {
treeItem.add( sendWithPromise('requestCertificateFields', item.detail.payload.index)
treeItem['detail'].children['root'] = constructTree(certFields[0])); .then(onCertificateFields);
revealTree(treeItem);
// Ensure the list is scrolled to the top by selecting the first item.
treeItem.children[0].selected = true;
document.body.dispatchEvent(
new CustomEvent('certificate-fields-updated-for-testing'));
} }
}
/**
* Show certificate field value for a selected certificate field. /**
*/ * Show the returned certificate fields for the selected certificate.
function showCertificateFieldValue() { * @param {Object} certFields A dictionary containing the fields tree
const item = $('cert-fields').selectedItem; * structure.
if (item && item.detail.payload.val) { */
$('cert-field-value').textContent = item.detail.payload.val; function onCertificateFields(certFields) {
} else { clearCertificateFields();
$('cert-field-value').textContent = ''; const treeItem = /** @type {!Tree} */ ($('cert-fields'));
} treeItem.add(
treeItem['detail'].children['root'] = constructTree(certFields[0]));
revealTree(treeItem);
// Ensure the list is scrolled to the top by selecting the first item.
treeItem.children[0].selected = true;
document.body.dispatchEvent(
new CustomEvent('certificate-fields-updated-for-testing'));
}
/**
* Show certificate field value for a selected certificate field.
*/
function showCertificateFieldValue() {
const item = $('cert-fields').selectedItem;
if (item && item.detail.payload.val) {
$('cert-field-value').textContent = item.detail.payload.val;
} else {
$('cert-field-value').textContent = '';
} }
}
/**
* Export the selected certificate. /**
*/ * Export the selected certificate.
function exportCertificate() { */
const item = $('hierarchy').selectedItem; function exportCertificate() {
if (item && item.detail.payload.index !== undefined) { const item = $('hierarchy').selectedItem;
chrome.send('exportCertificate', [item.detail.payload.index]); if (item && item.detail.payload.index !== undefined) {
} chrome.send('exportCertificate', [item.detail.payload.index]);
} }
}
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