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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '//resources/cr_elements/cr_icon_button/cr_icon_button.js';
import '//resources/cr_elements/cr_button/cr_button.js';
import '//resources/cr_elements/cr_input/cr_input.js';
import '/strings.m.js';
import type {CrButtonElement} from '//resources/cr_elements/cr_button/cr_button.js';
import type {CrInputElement} from '//resources/cr_elements/cr_input/cr_input.js';
import {I18nMixinLit} from '//resources/cr_elements/i18n_mixin_lit.js';
import {assert} from '//resources/js/assert.js';
import {CrLitElement} from '//resources/lit/v3_0/lit.rollup.js';
import {CertViewerBrowserProxyImpl} from './browser_proxy.js';
import type {CertMetadataChangeResult, ConstraintChangeResult} from './browser_proxy.js';
import {getCss} from './modifications_panel.css.js';
import {getHtml} from './modifications_panel.html.js';
const ModificationsPanelElementBase = I18nMixinLit(CrLitElement);
export interface ModificationsPanelElement {
$: {
addConstraintSection: HTMLElement,
addConstraintInput: CrInputElement,
addConstraintButton: CrButtonElement,
constraintListSection: HTMLElement,
constraintDeleteError: HTMLElement,
trustStateSelect: HTMLSelectElement,
trustStateSelectError: HTMLElement,
};
}
export class ModificationsPanelElement extends ModificationsPanelElementBase {
static get is() {
return 'modifications-panel';
}
static override get styles() {
return getCss();
}
override render() {
return getHtml.bind(this)();
}
static override get properties() {
return {
constraints: {type: Array},
trustStateValue: {type: String},
isEditable: {type: Boolean},
editControlsEnabled: {type: Boolean},
addConstraintErrorMessage: {type: String},
deleteConstraintErrorMessage: {type: String},
trustStateErrorMessage: {type: String},
};
}
accessor constraints: string[] = [];
accessor trustStateValue: string = '0';
accessor isEditable: boolean = false;
protected accessor editControlsEnabled: boolean = true;
protected accessor addConstraintErrorMessage: string = '';
protected accessor deleteConstraintErrorMessage: string = '';
protected accessor trustStateErrorMessage: string = '';
// Clear all error messages in this element.
private clearErrorMessages() {
this.deleteConstraintErrorMessage = '';
this.addConstraintErrorMessage = '';
this.trustStateErrorMessage = '';
}
protected onDeleteConstraintClick_(e: Event) {
this.clearErrorMessages();
assert(e.target);
const constraintToDeleteIndex =
Number((e.target as HTMLElement).dataset['index']);
if (this.constraints[constraintToDeleteIndex]) {
// Disable editing so we only have one change outstanding at any one time.
this.editControlsEnabled = false;
CertViewerBrowserProxyImpl.getInstance()
.deleteConstraint(this.constraints[constraintToDeleteIndex])
.then(this.onDeleteConstraintFinished_.bind(this));
} else {
this.deleteConstraintErrorMessage =
this.i18n('deleteConstraintErrorMessage');
}
}
private onDeleteConstraintFinished_(result: ConstraintChangeResult) {
if (result.status.success) {
assert(result.constraints);
this.constraints = result.constraints;
} else {
if (result.status.errorMessage !== undefined) {
this.deleteConstraintErrorMessage = result.status.errorMessage;
} else {
this.deleteConstraintErrorMessage =
this.i18n('deleteConstraintErrorMessage');
}
}
this.editControlsEnabled = true;
}
protected onAddConstraintClick_() {
// If no input, assume this is a misclick.
const trimmedInput = this.$.addConstraintInput.value.trim();
if (trimmedInput.length === 0) {
return;
}
// Disable editing so we only have one change outstanding at any one time.
this.editControlsEnabled = false;
this.clearErrorMessages();
CertViewerBrowserProxyImpl.getInstance()
.addConstraint(trimmedInput)
.then(this.onAddConstraintFinished_.bind(this));
}
private onAddConstraintFinished_(result: ConstraintChangeResult) {
if (result.status.success) {
assert(result.constraints);
this.constraints = result.constraints;
// Only clear input on successful add.
this.$.addConstraintInput.value = '';
} else {
if (result.status.errorMessage !== undefined) {
this.addConstraintErrorMessage = result.status.errorMessage;
} else {
this.addConstraintErrorMessage = this.i18n('addConstraintErrorMessage');
}
}
this.editControlsEnabled = true;
}
protected onTrustStateChange_() {
// Disable editing so we only have one change outstanding at any one time.
this.editControlsEnabled = false;
this.clearErrorMessages();
CertViewerBrowserProxyImpl.getInstance()
.updateTrustState(Number(this.$.trustStateSelect.value))
.then(this.onTrustStateChangeFinished_.bind(this));
}
private async onTrustStateChangeFinished_(result: CertMetadataChangeResult) {
if (result.success) {
// Update state to the new trust value.
this.trustStateValue = this.$.trustStateSelect.value;
} else {
// Restore UI to the old trust value.
this.$.trustStateSelect.value = this.trustStateValue;
if (result.errorMessage !== undefined) {
this.trustStateErrorMessage = result.errorMessage;
} else {
this.trustStateErrorMessage = this.i18n('trustStateErrorMessage');
}
}
this.editControlsEnabled = true;
// Can't focus disabled elements; wait for update to complete first.
await this.updateComplete;
this.$.trustStateSelect.focus();
}
protected onConstraintKeyPress_(event: KeyboardEvent): void {
if (event.key !== 'Enter') {
return;
}
event.stopPropagation();
this.onAddConstraintClick_();
}
}
declare global {
interface HTMLElementTagNameMap {
'modifications-panel': ModificationsPanelElement;
}
}
customElements.define(ModificationsPanelElement.is, ModificationsPanelElement);
|