/* 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, ifDefined } from "chrome://global/content/vendor/lit.all.mjs"; import { MozLitElement } from "chrome://global/content/lit-utils.mjs"; /** * The widget for allowing users to select and restore from a * a backup file. */ export default class RestoreFromBackup extends MozLitElement { #placeholderFileIconURL = "chrome://global/skin/icons/page-portrait.svg"; static properties = { backupFilePath: { type: String }, backupFileToRestore: { type: String, reflect: true }, backupFileInfo: { type: Object }, _fileIconURL: { type: String }, }; static get queries() { return { filePicker: "#backup-filepicker-input", passwordInput: "#backup-password-input", cancelButtonEl: "#restore-from-backup-cancel-button", confirmButtonEl: "#restore-from-backup-confirm-button", chooseButtonEl: "#backup-filepicker-button", }; } constructor() { super(); this._fileIconURL = ""; } /** * Dispatches the BackupUI:InitWidget custom event upon being attached to the * DOM, which registers with BackupUIChild for BackupService state updates. */ connectedCallback() { super.connectedCallback(); this.dispatchEvent( new CustomEvent("BackupUI:InitWidget", { bubbles: true }) ); if (this.backupFileToRestore && !this.backupFileInfo) { this.getBackupFileInfo(); } this.addEventListener("BackupUI:SelectNewFilepickerPath", this); } handleEvent(event) { if (event.type == "BackupUI:SelectNewFilepickerPath") { let { path, iconURL } = event.detail; this.backupFileToRestore = path; this._fileIconURL = iconURL; } } willUpdate(changedProperties) { if (changedProperties.has("backupFileToRestore")) { this.getBackupFileInfo(); } } async handleChooseBackupFile() { this.dispatchEvent( new CustomEvent("BackupUI:ShowFilepicker", { bubbles: true, detail: { win: window.browsingContext, filter: "filterHTML", displayDirectoryPath: this.backupFileToRestore, }, }) ); } getBackupFileInfo() { let backupFile = this.backupFileToRestore; if (!backupFile) { return; } this.dispatchEvent( new CustomEvent("getBackupFileInfo", { bubbles: true, composed: true, detail: { backupFile, }, }) ); } handleCancel() { this.dispatchEvent( new CustomEvent("dialogCancel", { bubbles: true, composed: true, }) ); } handleConfirm() { let backupFile = this.backupFileToRestore; if (!backupFile) { return; } let backupPassword = this.passwordInput?.value; this.dispatchEvent( new CustomEvent("restoreFromBackupConfirm", { bubbles: true, composed: true, detail: { backupFile, backupPassword, }, }) ); } controlsTemplate() { let iconURL = this.backupFileToRestore && (this._fileIconURL || this.#placeholderFileIconURL); return html`
`; } passwordEntryTemplate() { return html`