Files
tubestation/browser/components/backup/actors/BackupUIParent.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

122 lines
3.4 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/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
BackupService: "resource:///modules/backup/BackupService.sys.mjs",
});
/**
* 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.
*/
export class BackupUIParent extends JSWindowActorParent {
/**
* A reference to the BackupService singleton instance.
*
* @type {BackupService}
*/
#bs;
/**
* Create a BackupUIParent instance. If a BackupUIParent is instantiated
* before BrowserGlue has a chance to initialize the BackupService, this
* constructor will cause it to initialize first.
*/
constructor() {
super();
// We use init() rather than get(), since it's possible to load
// about:preferences before the service has had a chance to init itself
// via BrowserGlue.
this.#bs = lazy.BackupService.init();
}
/**
* Called once the BackupUIParent/BackupUIChild pair have been connected.
*/
actorCreated() {
this.#bs.addEventListener("BackupService:StateUpdate", this);
}
/**
* Called once the BackupUIParent/BackupUIChild pair have been disconnected.
*/
didDestroy() {
this.#bs.removeEventListener("BackupService:StateUpdate", this);
}
/**
* Handles events fired by the BackupService.
*
* @param {Event} event
* The event that the BackupService emitted.
*/
handleEvent(event) {
if (event.type == "BackupService:StateUpdate") {
this.sendState();
}
}
/**
* Handles messages sent by BackupUIChild.
*
* @param {ReceiveMessageArgument} message
* The message received from the BackupUIChild.
*/
async receiveMessage(message) {
if (message.name == "RequestState") {
this.sendState();
} else if (message.name == "ToggleScheduledBackups") {
let { isScheduledBackupsEnabled, parentDirPath } = message.data;
if (isScheduledBackupsEnabled && parentDirPath) {
this.#bs.setParentDirPath(parentDirPath);
}
this.#bs.setScheduledBackups(isScheduledBackupsEnabled);
return true;
/**
* TODO: (Bug 1900125) we should create a backup at the specified dir path once we turn on
* scheduled backups. The backup folder in the chosen directory should contain
* the archive file, which we create using BackupService.createArchive implemented in
* Bug 1897498.
*/
} else if (message.name == "ShowFilepicker") {
let { win } = message.data;
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.init(win, "", Ci.nsIFilePicker.modeGetFolder);
let result = await new Promise(resolve => fp.open(resolve));
if (result === Ci.nsIFilePicker.returnCancel) {
return false;
}
let path = fp.file.path;
let iconURL = this.#bs.getIconFromFilePath(path);
let filename = PathUtils.filename(path);
return {
path,
filename,
iconURL,
};
}
return null;
}
/**
* Sends the StateUpdate message to the BackupUIChild, along with the most
* recent state object from BackupService.
*/
sendState() {
this.sendAsyncMessage("StateUpdate", { state: this.#bs.state });
}
}