summaryrefslogtreecommitdiffstats
path: root/browser/components/backup/actors
diff options
context:
space:
mode:
Diffstat (limited to 'browser/components/backup/actors')
-rw-r--r--browser/components/backup/actors/BackupUIChild.sys.mjs54
-rw-r--r--browser/components/backup/actors/BackupUIParent.sys.mjs82
2 files changed, 136 insertions, 0 deletions
diff --git a/browser/components/backup/actors/BackupUIChild.sys.mjs b/browser/components/backup/actors/BackupUIChild.sys.mjs
new file mode 100644
index 0000000000..25d013fa8e
--- /dev/null
+++ b/browser/components/backup/actors/BackupUIChild.sys.mjs
@@ -0,0 +1,54 @@
+/* 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 BackupUI:InitWidget custom events fired by widgets that want to
+ * register with BackupUIChild. Firing this event 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.
+ *
+ * @param {Event} event
+ * The BackupUI:InitWidget custom event that the widget fired.
+ */
+ handleEvent(event) {
+ if (event.type == "BackupUI:InitWidget") {
+ this.#inittedWidgets.add(event.target);
+ this.sendAsyncMessage("RequestState");
+ }
+ }
+
+ /**
+ * 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;
+ }
+ }
+ }
+ }
+}
diff --git a/browser/components/backup/actors/BackupUIParent.sys.mjs b/browser/components/backup/actors/BackupUIParent.sys.mjs
new file mode 100644
index 0000000000..e4d0f3aace
--- /dev/null
+++ b/browser/components/backup/actors/BackupUIParent.sys.mjs
@@ -0,0 +1,82 @@
+/* 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.
+ */
+ receiveMessage(message) {
+ if (message.name == "RequestState") {
+ this.sendState();
+ }
+ }
+
+ /**
+ * Sends the StateUpdate message to the BackupUIChild, along with the most
+ * recent state object from BackupService.
+ */
+ sendState() {
+ this.sendAsyncMessage("StateUpdate", { state: this.#bs.state });
+ }
+}