1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '/strings.m.js';
import 'chrome://resources/cr_elements/cr_tab_box/cr_tab_box.js';
import 'chrome://resources/cr_elements/cr_tree/cr_tree.js';
import 'chrome://resources/cr_elements/cr_tree/cr_tree_item.js';
import './browser_proxy.js';
import './modifications_panel.js';
import type {CrTreeElement} from 'chrome://resources/cr_elements/cr_tree/cr_tree.js';
import type {CrTreeItemElement} from 'chrome://resources/cr_elements/cr_tree/cr_tree_item.js';
import {assert} from 'chrome://resources/js/assert.js';
import {sendWithPromise} from 'chrome://resources/js/cr.js';
interface TreeInfo {
payload?: object;
children?: TreeInfo[];
label: string;
}
export enum CertificateTrust {
// LINT.IfChange(CertificateTrustType)
CERTIFICATE_TRUST_DISTRUSTED = 0,
CERTIFICATE_TRUST_UNSPECIFIED = 1,
CERTIFICATE_TRUST_TRUSTED = 2,
// LINT.ThenChange(//chrome/browser/ui/webui/certificate_viewer/certificate_viewer_webui.cc:CertificateTrustType)
}
interface CertificateMetadata {
trust: CertificateTrust;
constraints?: string[];
isEditable: boolean;
}
interface CertificateInfo {
general: {[key: string]: string};
hierarchy: TreeInfo[];
isError: boolean;
certMetadata?: CertificateMetadata;
}
export interface TreeItemDetail {
payload: {
val?: string,
index?: number,
};
children: {[key: string|number]: CrTreeItemElement};
}
/**
* Initialize the certificate viewer dialog by wiring up the close button,
* substituting in translated strings and requesting certificate details.
*/
function initialize() {
const tabBox = document.querySelector('cr-tab-box');
assert(tabBox);
tabBox.hidden = false;
const args =
JSON.parse(chrome.getVariableValue('dialogArguments')) as CertificateInfo;
getCertificateInfo(args);
getCertificateMetadata(args);
/**
* Initialize the second tab's contents.
* 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.
*/
const initializeDetailTab = oneShot(function() {
const hierarchy = document.querySelector<CrTreeElement>('#hierarchy');
assert(hierarchy);
initializeTree(hierarchy, showCertificateFields);
const certFields = document.querySelector<CrTreeElement>('#cert-fields');
assert(certFields);
initializeTree(certFields, showCertificateFieldValue);
createCertificateHierarchy(args.hierarchy);
});
// The second tab's contents aren't visible on startup, so we can
// shorten startup by not populating those controls until after we
// have had a chance to draw the visible controls the first time.
// 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);
tabBox.addEventListener('selected-index-change', function f() {
tabBox.removeEventListener('selected-index-change', f);
initializeDetailTab();
}, true);
stripGtkAccessorKeys();
const exportButton = document.querySelector<HTMLElement>('#export');
assert(exportButton);
exportButton.onclick = exportCertificate;
}
function getCertificateMetadata(certInfo: CertificateInfo) {
const modificationsTab =
document.querySelector<HTMLElement>('#modifications-tab');
assert(modificationsTab);
if (certInfo.certMetadata === undefined) {
// Remove the modifications tab if it is not needed. This prevents it from
// being selectable by keyboard navigation despite being hidden.
// This is a bit of a hack since cr_tab_box doesn't properly handle hidden
// or disabled attributes. Once the cert viewer is migrated to use the
// newer webui components, solve this in a better way. (See
// crbug.com/405143561 for more background.)
modificationsTab.remove();
const modifications = document.querySelector<HTMLElement>('#modifications');
assert(modifications);
modifications.remove();
return;
}
modificationsTab.hidden = false;
const modificationsPanel = document.querySelector('modifications-panel');
assert(modificationsPanel);
modificationsPanel.trustStateValue = certInfo.certMetadata.trust.toString();
if (certInfo.certMetadata.isEditable) {
modificationsPanel.isEditable = true;
}
if (certInfo.certMetadata.constraints !== undefined) {
modificationsPanel.constraints = certInfo.certMetadata.constraints;
}
}
/**
* Decorate a function so that it can only be invoked once.
*/
function oneShot(fn: () => void) {
let fired = false;
return function() {
if (fired) {
return;
}
fired = true;
fn();
};
}
/**
* Initialize a Tree object from a given element using the specified
* change handler.
* tree The HTMLElement to style as a tree.
* handler Function to call when a node is selected.
*/
function initializeTree(tree: CrTreeElement, handler: () => void) {
tree.detail = {payload: {}, children: {}};
tree.addEventListener('cr-tree-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 tabs = document.querySelectorAll('div[slot=\'tab\']');
const nodes = Array.prototype.slice.call(tabs, 0);
const exportButton = document.querySelector('#export');
nodes.push(exportButton);
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 The tree object to expand all nodes on.
*/
function revealTree(tree: CrTreeElement|CrTreeItemElement) {
tree.expanded = true;
const detail = tree.detail as TreeItemDetail;
for (const key in detail.children) {
revealTree(detail.children[key]!);
}
}
/**
* This function is called from certificate_viewer_ui.cc with the certificate
* information. Display all returned information to the user.
* @param certInfo Certificate information in named fields.
*/
function getCertificateInfo(certInfo: CertificateInfo) {
const generalError = document.querySelector<HTMLElement>('#general-error');
assert(generalError);
generalError.hidden = !certInfo.isError;
const generalFields = document.querySelector<HTMLElement>('#general-fields');
assert(generalFields);
generalFields.hidden = certInfo.isError;
for (const key in certInfo.general) {
const el = document.querySelector<HTMLElement>(`#${key}`);
assert(el);
el.textContent = certInfo.general[key]!;
}
}
/**
* This function populates the certificate hierarchy.
* @param hierarchy A dictionary containing the hierarchy.
*/
function createCertificateHierarchy(hierarchy: TreeInfo[]) {
const tree = document.querySelector<CrTreeElement>('#hierarchy');
assert(tree);
const root = constructTree(hierarchy[0]!);
(tree.detail as TreeItemDetail).children['root'] = root;
tree.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
// populate the fields view.
let last: CrTreeItemElement = root;
while ((last.detail as TreeItemDetail).children &&
(last.detail as TreeItemDetail).children[0]) {
last = (last.detail as TreeItemDetail).children[0]!;
}
tree.selectedItem = last;
}
/**
* Constructs a TreeItem corresponding to the passed in tree
* @param tree Dictionary describing the tree structure.
* @return Tree node corresponding to the input dictionary.
*/
function constructTree(tree: TreeInfo): CrTreeItemElement {
const treeItem = document.createElement('cr-tree-item');
treeItem.label = tree.label;
treeItem.detail = {payload: tree.payload ? tree.payload : {}, children: {}};
treeItem.setExtraAriaLabel(
(treeItem.detail as TreeItemDetail).payload.val || '');
if (tree.children) {
for (let i = 0; i < tree.children.length; i++) {
const child = constructTree(tree.children[i]!);
treeItem.add(child);
(treeItem.detail as TreeItemDetail).children[i] = child;
}
}
return treeItem;
}
/**
* Clear any previous certificate fields in the tree.
*/
function clearCertificateFields() {
const treeItem = document.querySelector<CrTreeElement>('#cert-fields');
assert(treeItem);
const detail = treeItem.detail as TreeItemDetail;
for (const key in detail.children) {
treeItem.removeTreeItem(detail.children[key]!);
delete detail.children[key];
}
}
/**
* Request certificate fields for the selected certificate in the hierarchy.
*/
function showCertificateFields() {
clearCertificateFields();
const hierarchy = document.querySelector<CrTreeElement>('#hierarchy');
assert(hierarchy);
const item = hierarchy.selectedItem;
if (item && (item.detail as TreeItemDetail).payload.index !== undefined) {
sendWithPromise(
'requestCertificateFields',
(item.detail as TreeItemDetail).payload.index)
.then(onCertificateFields);
}
}
/**
* Show the returned certificate fields for the selected certificate.
* @param certFields A dictionary containing the fields tree structure.
*/
function onCertificateFields(certFields: TreeInfo[]) {
clearCertificateFields();
const tree = document.querySelector<CrTreeElement>('#cert-fields');
assert(tree);
const root = constructTree(certFields[0]!);
(tree.detail as TreeItemDetail).children['root'] = root;
tree.add(root);
revealTree(tree);
// Ensure the list is scrolled to the top by selecting the first item.
tree.selectedItem = tree.items[0]!;
document.body.dispatchEvent(
new CustomEvent('certificate-fields-updated-for-testing'));
}
/**
* Show certificate field value for a selected certificate field.
*/
function showCertificateFieldValue() {
const certFields = document.querySelector<CrTreeElement>('#cert-fields');
assert(certFields);
const item = certFields.selectedItem;
const fieldValue = document.querySelector<HTMLElement>('#cert-field-value');
assert(fieldValue);
if (item && (item.detail as TreeItemDetail).payload.val) {
fieldValue.textContent = (item.detail as TreeItemDetail).payload.val || '';
} else {
fieldValue.textContent = '';
}
}
/**
* Export the selected certificate.
*/
function exportCertificate() {
const hierarchy = document.querySelector<CrTreeElement>('#hierarchy');
assert(hierarchy);
const item = hierarchy.selectedItem;
if (item && (item.detail as TreeItemDetail).payload.index !== undefined) {
chrome.send(
'exportCertificate', [(item.detail as TreeItemDetail).payload.index]);
}
}
document.addEventListener('DOMContentLoaded', initialize);
|