Files
tubestation/browser/components/backup/actors/BackupUIChild.sys.mjs
kpatenio f5e604a1bf Bug 1895943 - Implement file picker and save location for backups. r=backup-reviewers,mconley,fluent-reviewers,flod,firefox-desktop-core-reviewers
1. Allows for selecting a custom path or the default (Documents) path for saving backups. The selection is passed from the "turn-on-scheduled-backups" dialog to "BackupService".
2. After pressing the "Choose" button in the dialog, a filepicker will appear so that a folder can be selected.
3. Once the dialog is confirmed, the absolute path is saved to a pref called "browser.backup.location" and saved in the service state.

Other changes:
- Added the Documents folder as the default save location
- Added an onUpdate function for "browser.backup.location" that passes the updated BackupService state to registered widgets (backup settings section, dialogs)
- Added Storybook entries and tests for the newly updated input and filepicker

Figma: https://www.figma.com/design/vNbX4c0ws0L1qr0mxpKvsW/Fx-Backup?node-id=147-4568&t=tILUMKfg8c6Ed1Ul-0 (turn on backup dialog)

Differential Revision: https://phabricator.services.mozilla.com/D210850
2024-06-10 21:41:40 +00:00

84 lines
2.8 KiB
JavaScript

/* 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/. */
/**
* A JSWindowActor that is responsible for marshalling information between
* the BackupService singleton and any registered UI widgets that need to
* represent data from that service. Any UI widgets that want to receive
* state updates from BackupService should emit a BackupUI:InitWidget
* event in a document that this actor pair is registered for.
*/
export class BackupUIChild extends JSWindowActorChild {
#inittedWidgets = new WeakSet();
/**
* Handles custom events fired by widgets that want to register with
* BackupUIChild.
*
* @param {Event} event
* The custom event that the widget fired.
*/
async handleEvent(event) {
/**
* BackupUI:InitWidget sends a message to the parent to request the BackupService state
* which will result in a `backupServiceState` property of the widget to be set when that
* state is received. Subsequent state updates will also cause that state property to
* be set.
*/
if (event.type == "BackupUI:InitWidget") {
this.#inittedWidgets.add(event.target);
this.sendAsyncMessage("RequestState");
} else if (event.type == "BackupUI:ToggleScheduledBackups") {
this.sendAsyncMessage("ToggleScheduledBackups", event.detail);
} else if (event.type == "BackupUI:ShowFilepicker") {
let targetNodeName = event.target.nodeName;
let { path, filename, iconURL } = await this.sendQuery("ShowFilepicker", {
win: event.detail?.win,
});
let widgets = ChromeUtils.nondeterministicGetWeakSetKeys(
this.#inittedWidgets
);
for (let widget of widgets) {
if (widget.isConnected && widget.nodeName == targetNodeName) {
widget.dispatchEvent(
new CustomEvent("BackupUI:SelectNewFilepickerPath", {
bubbles: true,
detail: {
path,
filename,
iconURL,
},
})
);
break;
}
}
}
}
/**
* Handles messages sent by BackupUIParent.
*
* @param {ReceiveMessageArgument} message
* The message received from the BackupUIParent.
*/
receiveMessage(message) {
if (message.name == "StateUpdate") {
let widgets = ChromeUtils.nondeterministicGetWeakSetKeys(
this.#inittedWidgets
);
for (let widget of widgets) {
if (widget.isConnected) {
// Note: we might need to switch to using Cu.cloneInto here in the
// event that these widgets are embedded in a non-parent-process
// context, like in an onboarding card.
widget.backupServiceState = message.data.state;
}
}
}
}
}