/* 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, { AddonManager: "resource://gre/modules/AddonManager.sys.mjs", GeneratedWidgetsProvider: "moz-src:///toolkit/components/widgets/GeneratedWidgetsProvider.sys.mjs", GENERATION_ERRORS: "moz-src:///browser/components/widgets/WidgetGenerator.sys.mjs", WidgetCreator: "moz-src:///browser/components/widgets/WidgetCreator.sys.mjs", WidgetCreatorError: "moz-src:///browser/components/widgets/WidgetCreator.sys.mjs", WidgetGeneratorError: "moz-src:///browser/components/widgets/WidgetGenerator.sys.mjs", WidgetManager: "moz-src:///toolkit/components/widgets/WidgetManager.sys.mjs", }); const ORDER_PREF = "browser.widgets.studio.order"; /** * The prompt, error code and widget id of a failed widget creation request. * * @fires CreationFailure#update * When set() or clear() is called. */ class CreationFailure extends EventTarget { /** @type {?{prompt: string, error: string, widgetId: ?string}} */ value = null; /** * @param {?{prompt: string, error: string, widgetId: ?string}} value - The * failed request's prompt, error code and the id of the widget it updated, * or null. */ set(value) { this.value = value; this.dispatchEvent(new Event("update")); } clear() { this.set(null); } } /** * Keeps a host element in sync with WidgetCreator and the WidgetManager, * re-rendering it whenever a widget creation in any window starts or settles * and whenever a generated widget changes. */ export class WidgetsController { /** @type {CreationFailure} */ static lastFailure = new CreationFailure(); /** * Aborted on disconnect to remove the event listeners and stop pending * callbacks from updating the host. * * @type {?AbortController} */ #listeners = null; /** @type {Array} registered generated widgets in the saved order */ #widgets = []; /** * @param {ReactiveControllerHost} host - the element to update */ constructor(host) { this.host = host; host.addController(this); } /** * @type {boolean} True while a widget is being generated or installed. */ get generating() { return !!lazy.WidgetCreator.pending; } /** * @type {?string} The active request's prompt, or null when idle. */ get prompt() { return lazy.WidgetCreator.prompt; } /** * @type {?string} The id of the widget the active request updates, or null. */ get widgetId() { return lazy.WidgetCreator.widgetId; } /** * @type {?{prompt: string, error: string, widgetId: ?string}} The last * failed request's prompt, error code and the id of the widget it updated, * or null. */ get failed() { return WidgetsController.lastFailure.value; } /** * The ids of the generated widgets in the order they are listed, saved in * ORDER_PREF. * * @type {Array} */ get order() { return Services.prefs .getStringPref(ORDER_PREF, "") .split(",") .filter(Boolean); } /** * @param {Array} ids - the widget ids in the order to list them */ set order(ids) { Services.prefs.setStringPref(ORDER_PREF, ids.join(",")); } /** * All generated widgets. * * @type {Array} */ get all() { return this.#widgets; } /** * @type {Array} Enabled generated widgets. */ get enabled() { return this.all.filter(widget => widget.enabled); } /** * @type {Array} Disabled generated widgets. */ get disabled() { return this.all.filter(widget => !widget.enabled); } /** * Generates a widget and installs it as a new widget, or as an update to the * widget with widgetId. * * @param {string} prompt - The user's description of the widget. * @param {?string} [widgetId] - The id of the generated widget to update. * @returns {Promise} The id of the installed or updated widget, or * null when creation failed or was canceled. */ async create(prompt, widgetId = null) { WidgetsController.lastFailure.clear(); try { let addon = await lazy.WidgetCreator.create({ prompt, widgetId }); return addon.id; } catch (error) { if (error.name == "AbortError") { return null; } console.error(error); WidgetsController.lastFailure.set({ prompt, error: error instanceof lazy.WidgetGeneratorError || error instanceof lazy.WidgetCreatorError ? error.code : lazy.GENERATION_ERRORS.SERVICE, widgetId, }); return null; } } /** * Cancels generation even if another window started it. Once installation * starts, cancel() has no effect. */ cancel() { lazy.WidgetCreator.cancel(); } /** * Clears lastFailure. */ dismiss() { WidgetsController.lastFailure.clear(); } /** * Uninstalls a generated widget. * * @param {string} id - The id of the generated widget. * @returns {Promise} Resolves once the widget is uninstalled. */ delete(id) { return lazy.GeneratedWidgetsProvider.uninstall(id); } handleEvent(event) { this.#watch(event.detail.pending); } /** * Updates the connected host when the request starts and when it settles. * * @param {Promise} pending - The promise * returned by WidgetCreator.create(). */ #watch(pending) { let { signal } = this.#listeners; let update = () => { if (signal.aborted) { return; } this.host.requestUpdate(); }; update(); pending.then(update, update); } /** * Reads the generated widgets, appends the ones missing from the saved * order to it, sorts them by that order and updates the host. */ #update = () => { let widgets = lazy.WidgetManager.getWidgets().filter( widget => widget.generated ); let order = new Set(this.order); let registered = new Set(widgets.map(widget => widget.id)); let ids = order.union(registered); if (ids.size > order.size) { this.order = [...ids]; } let index = new Map([...ids].map((id, i) => [id, i])); this.#widgets = widgets.sort((a, b) => index.get(a.id) - index.get(b.id)); this.host.requestUpdate(); }; /** * Removes the ids of widgets that are no longer installed from the saved * order. */ async #prune() { if (Services.appinfo.inSafeMode) { return; } let { signal } = this.#listeners; let addons = await lazy.AddonManager.getAddonsByTypes(["generatedwidget"]); if (signal.aborted) { return; } let installed = new Set(addons.map(addon => addon.id)); let { order } = this; let kept = order.filter( id => installed.has(id) || lazy.WidgetManager.getWidgetByID(id) ); if (kept.length < order.length) { this.order = kept; } } hostConnected() { this.#listeners = new AbortController(); let { signal } = this.#listeners; WidgetsController.lastFailure.addEventListener( "update", () => this.host.requestUpdate(), { signal } ); lazy.WidgetCreator.addEventListener("WidgetCreator:Creating", this, { signal, }); this.host.documentGlobal.addEventListener( "unload", () => this.hostDisconnected(), { signal } ); lazy.WidgetManager.on("change", this.#update); Services.prefs.addObserver(ORDER_PREF, this.#update); this.#update(); this.#prune().catch(console.error); let { pending } = lazy.WidgetCreator; if (pending) { this.#watch(pending); } } hostDisconnected() { lazy.WidgetManager.off("change", this.#update); Services.prefs.removeObserver(ORDER_PREF, this.#update); this.#listeners?.abort(); this.#listeners = null; } }