/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import { html } from "chrome://global/content/vendor/lit.all.mjs"; import { MozLitElement } from "chrome://global/content/lit-utils.mjs"; // eslint-disable-next-line import/no-unassigned-import import "chrome://global/content/elements/moz-message-bar.mjs"; // eslint-disable-next-line import/no-unassigned-import import "chrome://browser/content/backup/password-validation-inputs.mjs"; import { ERRORS } from "chrome://browser/content/backup/backup-constants.mjs"; const ENABLE_ERROR_L10N_IDS = Object.freeze({ [ERRORS.FILE_SYSTEM_ERROR]: "turn-on-scheduled-backups-error-file-system", [ERRORS.INVALID_PASSWORD]: "backup-error-password-requirements", [ERRORS.UNKNOWN]: "backup-error-retry", }); /** * @param {number} errorCode Error code from backup-constants.mjs * @returns {string} Localization ID for error message */ function getEnableErrorL10nId(errorCode) { return ( ENABLE_ERROR_L10N_IDS[errorCode] ?? ENABLE_ERROR_L10N_IDS[ERRORS.UNKNOWN] ); } /** * The widget for showing available options when users want to turn on * scheduled backups. */ export default class TurnOnScheduledBackups extends MozLitElement { #placeholderIconURL = "chrome://global/skin/icons/page-portrait.svg"; static properties = { // passed in from parents defaultIconURL: { type: String, reflect: true }, defaultLabel: { type: String, reflect: true }, defaultPath: { type: String, reflect: true }, supportBaseLink: { type: String }, // internal state _newIconURL: { type: String, state: true }, _newLabel: { type: String, state: true }, _newPath: { type: String, state: true }, _showPasswordOptions: { type: Boolean, reflect: true, state: true }, _passwordsMatch: { type: Boolean, state: true }, _inputPassValue: { type: String, state: true }, // managed by BackupUIChild enableBackupErrorCode: { type: Number }, }; static get queries() { return { cancelButtonEl: "#backup-turn-on-scheduled-cancel-button", confirmButtonEl: "#backup-turn-on-scheduled-confirm-button", filePathButtonEl: "#backup-location-filepicker-button", filePathInputCustomEl: "#backup-location-filepicker-input-custom", filePathInputDefaultEl: "#backup-location-filepicker-input-default", passwordOptionsCheckboxEl: "#sensitive-data-checkbox-input", passwordOptionsExpandedEl: "#passwords", errorEl: "#enable-backup-encryption-error", }; } constructor() { super(); this.defaultIconURL = ""; this.defaultLabel = ""; this.defaultPath = ""; this._newIconURL = ""; this._newLabel = ""; this._newPath = ""; this._showPasswordOptions = false; this._passwordsMatch = false; this.enableBackupErrorCode = 0; } connectedCallback() { super.connectedCallback(); this.dispatchEvent( new CustomEvent("BackupUI:InitWidget", { bubbles: true }) ); // listen to events from BackupUIChild this.addEventListener("BackupUI:SelectNewFilepickerPath", this); // listen to events from this.addEventListener("ValidPasswordsDetected", this); this.addEventListener("InvalidPasswordsDetected", this); } handleEvent(event) { if (event.type == "BackupUI:SelectNewFilepickerPath") { let { path, filename, iconURL } = event.detail; this._newPath = path; this._newLabel = filename; this._newIconURL = iconURL; } else if (event.type == "ValidPasswordsDetected") { let { password } = event.detail; this._passwordsMatch = true; this._inputPassValue = password; } else if (event.type == "InvalidPasswordsDetected") { this._passwordsMatch = false; this._inputPassValue = ""; } } async handleChooseLocation() { this.dispatchEvent( new CustomEvent("BackupUI:ShowFilepicker", { bubbles: true, detail: { win: window.browsingContext, }, }) ); } close() { this.dispatchEvent( new CustomEvent("dialogCancel", { bubbles: true, composed: true, }) ); this.reset(); } handleConfirm() { let detail = { parentDirPath: this._newPath || this.defaultPath, }; if (this._showPasswordOptions && this._passwordsMatch) { detail.password = this._inputPassValue; } this.dispatchEvent( new CustomEvent("BackupUI:EnableScheduledBackups", { bubbles: true, detail, }) ); } handleTogglePasswordOptions() { this._showPasswordOptions = this.passwordOptionsCheckboxEl?.checked; this._passwordsMatch = false; } reset() { this._newPath = ""; this._newIconURL = ""; this._newLabel = ""; this._showPasswordOptions = false; this.passwordOptionsCheckboxEl.checked = false; this._passwordsMatch = false; this._inputPassValue = ""; this.enableBackupErrorCode = 0; if (this.passwordOptionsExpandedEl) { /** @type {import("./password-validation-inputs.mjs").default} */ const passwordElement = this.passwordOptionsExpandedEl; passwordElement.reset(); } } defaultFilePathInputTemplate() { let filename = this.defaultLabel; let iconURL = this.defaultIconURL || this.#placeholderIconURL; return html` `; } customFilePathInputTemplate() { let filename = this._newLabel; let iconURL = this._newIconURL || this.#placeholderIconURL; return html` `; } errorTemplate() { return html` `; } allOptionsTemplate() { return html`
${!this._newPath ? this.defaultFilePathInputTemplate() : this.customFilePathInputTemplate()}
${this._showPasswordOptions ? this.passwordsTemplate() : null}
`; } passwordsTemplate() { return html` `; } contentTemplate() { return html`

${this.allOptionsTemplate()} ${this.enableBackupErrorCode ? this.errorTemplate() : null}
`; } render() { return html` ${this.contentTemplate()} `; } } customElements.define("turn-on-scheduled-backups", TurnOnScheduledBackups);