ontext so that they're not
    // handled by other nsICommandLine handlers.
    let urlParam = aCmdLine.handleFlagWithParam("new-window", false);
    context.url = Services.io.newURI(urlParam);

    let containerParam = aCmdLine.handleFlagWithParam("container", false);
    if (containerParam !== null) {
      context.userContextId = Number(containerParam);
    }

    lazy.logConsole.info(
      `Handling command line invoation for Taskbar Tab ${id}`
    );

    // Prevent the default commandline handler from running.
    aCmdLine.preventDefault = true;

    // Retrieving Taskbar Tabs requires async operations. Prevent shutdown while
    // it loads context to open the window.
    Services.startup.enterLastWindowClosingSurvivalArea();
    launchTaskbarTab(context).finally(() => {
      Services.startup.exitLastWindowClosingSurvivalArea();
    });
  }
}

/**
 * Launches a new Taskbar Tab, recreating it if it didn't exist.
 *
 * @param {object} aContext - Command line retrieved flags and context.
 */
async function launchTaskbarTab(aContext) {
  let taskbarTab;
  try {
    taskbarTab = await lazy.TaskbarTabs.getTaskbarTab(aContext.id);

    lazy.logConsole.debug(
      `Found Taskbar Tab matching the flag "-taskbar-tab ${aContext.id}"`
    );
  } catch (e) {
    lazy.logConsole.debug(
      `Taskbar Tab for ID ${aContext.id} doesn't exist, reconstructing it.`
    );

    if (!Object.hasOwn(aContext, "userContextId")) {
      lazy.logConsole.error(
        "Expected -container flag, but found none. Using the default container."
      );

      aContext.userContextId =
        Services.scriptSecurityManager.DEFAULT_USER_CONTEXT_ID;
    }

    ({ taskbarTab } = await lazy.TaskbarTabs.findOrCreateTaskbarTab(
      aContext.url,
      aContext.userContextId
    ));
  }

  await lazy.TaskbarTabs.openWindow(taskbarTab);
}
PK