/* 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, { GeneratedWidgetsProvider: "moz-src:///toolkit/components/widgets/GeneratedWidgetsProvider.sys.mjs", WidgetGenerator: "moz-src:///browser/components/widgets/WidgetGenerator.sys.mjs", }); /** * The codes of a WidgetCreatorError. */ export const CREATION_ERRORS = Object.freeze({ /** Another widget is already being created. */ BUSY: "busy", /** The generated widget can't be installed. */ INSTALL: "install", }); /** * An error from WidgetCreator, with a code from CREATION_ERRORS. */ export class WidgetCreatorError extends Error { name = "WidgetCreatorError"; /** * @param {string} message * @param {string} code - one of CREATION_ERRORS * @param {Error} [cause] - the error that led to this one */ constructor(message, code, cause) { super(message, { cause }); this.code = code; } } /** * Generates and installs one widget at a time. * * @fires _WidgetCreator#"WidgetCreator:Creating" * When generation starts. detail: { pending } * @fires _WidgetCreator#"WidgetCreator:Error" * When generation or installation fails for any reason besides an * AbortError. detail: { error, prompt, widgetId } */ class _WidgetCreator extends EventTarget { #pending = null; #prompt = null; #widgetId = null; /** * @type {?AbortController} The controller for the current generation request. */ #abortController = null; /** * The current create() promise, or null when idle. * * @type {?Promise} */ get pending() { return this.#pending; } /** @type {?string} The current widget prompt, or null when idle. */ get prompt() { return this.#prompt; } /** * @type {?string} The id of the widget the current request updates, or null. */ get widgetId() { return this.#widgetId; } /** * Generates a widget from the prompt and installs it as a new widget, or as * an update to the widget with widgetId. * * @param {object} options * @param {string} options.prompt - The user's description of the widget. * @param {?string} [options.widgetId] - The id of the generated widget to * update. * @param {AbortSignal} [options.signal] - Aborting it cancels the generation * step. * @returns {Promise} Resolves to the installed * or updated add-on, or rejects with a BUSY WidgetCreatorError while * another widget is being created. */ create({ prompt, widgetId = null, signal }) { if (this.#pending) { return Promise.reject( new WidgetCreatorError( "A widget is already being created", CREATION_ERRORS.BUSY ) ); } this.#abortController = new AbortController(); let signals = [this.#abortController.signal]; if (signal) { signals.push(signal); } this.#prompt = prompt; this.#widgetId = widgetId; this.#pending = this.#create({ prompt, widgetId, signal: AbortSignal.any(signals), }).then( addon => this.#finished(addon), error => this.#failed(error) ); this.dispatchEvent( new CustomEvent("WidgetCreator:Creating", { detail: { pending: this.#pending }, }) ); return this.#pending; } /** * Cancels the current widget's generation step. Does nothing when no widget * is being generated, including while the generated widget installs. */ cancel() { this.#abortController?.abort(); } /** * @param {object} options * @param {string} options.prompt - The user's description of the widget. * @param {?string} options.widgetId - The id of the generated widget to * update if present. * @param {AbortSignal} options.signal - Aborted by cancel() or the caller's * signal. * @returns {Promise} Resolves to the installed * or updated add-on. */ async #create({ prompt, widgetId, signal }) { let path = await lazy.WidgetGenerator.generate({ prompt, signal }); signal.throwIfAborted(); this.#abortController = null; try { return await (widgetId ? lazy.GeneratedWidgetsProvider.update(widgetId, path) : lazy.GeneratedWidgetsProvider.install(path)); } catch (error) { throw new WidgetCreatorError( "The widget couldn't be installed", CREATION_ERRORS.INSTALL, error ); } } /** * @param {GeneratedWidgetAddonWrapper} addon - The installed add-on. * @returns {GeneratedWidgetAddonWrapper} The same add-on. */ #finished(addon) { this.#clear(); return addon; } /** * @param {Error} error - The error from generation or installation. */ #failed(error) { let prompt = this.#prompt; let widgetId = this.#widgetId; this.#clear(); if (error.name != "AbortError") { this.dispatchEvent( new CustomEvent("WidgetCreator:Error", { detail: { error, prompt, widgetId }, }) ); } throw error; } #clear() { this.#pending = null; this.#prompt = null; this.#widgetId = null; this.#abortController = null; } } export const WidgetCreator = new _WidgetCreator();