tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TaskbarTabsCmd.sys.mjs (3141B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 let lazy = {};
      6 
      7 ChromeUtils.defineESModuleGetters(lazy, {
      8  TaskbarTabs: "resource:///modules/taskbartabs/TaskbarTabs.sys.mjs",
      9  TaskbarTabsUtils: "resource:///modules/taskbartabs/TaskbarTabsUtils.sys.mjs",
     10 });
     11 
     12 ChromeUtils.defineLazyGetter(lazy, "logConsole", () => {
     13  return console.createInstance({
     14    prefix: "TaskbarTabs",
     15    maxLogLevel: "Warn",
     16  });
     17 });
     18 
     19 /**
     20 * A command line handler for Firefox shortcuts with the flag "-taskbar-tab",
     21 * which will trigger a Taskbar Tab window to be opened.
     22 */
     23 export class CommandLineHandler {
     24  static classID = Components.ID("{974fe39b-4584-4cb5-bf62-5c141aedc557}");
     25  static contractID = "@mozilla.org/browser/taskbar-tabs-clh;1";
     26 
     27  QueryInterface = ChromeUtils.generateQI([Ci.nsICommandLineHandler]);
     28 
     29  handle(aCmdLine) {
     30    if (!lazy.TaskbarTabsUtils.isEnabled()) {
     31      lazy.logConsole.info("Taskbar Tabs disabled, skipping.");
     32      return;
     33    }
     34 
     35    let id = aCmdLine.handleFlagWithParam("taskbar-tab", false);
     36    if (!id) {
     37      return;
     38    }
     39 
     40    let context = { id };
     41 
     42    // Handle the commands before entering an async context so that they're not
     43    // handled by other nsICommandLine handlers.
     44    let urlParam = aCmdLine.handleFlagWithParam("new-window", false);
     45    context.url = Services.io.newURI(urlParam);
     46 
     47    let containerParam = aCmdLine.handleFlagWithParam("container", false);
     48    if (containerParam !== null) {
     49      context.userContextId = Number(containerParam);
     50    }
     51 
     52    lazy.logConsole.info(
     53      `Handling command line invoation for Taskbar Tab ${id}`
     54    );
     55 
     56    // Prevent the default commandline handler from running.
     57    aCmdLine.preventDefault = true;
     58 
     59    // Retrieving Taskbar Tabs requires async operations. Prevent shutdown while
     60    // it loads context to open the window.
     61    Services.startup.enterLastWindowClosingSurvivalArea();
     62    launchTaskbarTab(context).finally(() => {
     63      Services.startup.exitLastWindowClosingSurvivalArea();
     64    });
     65  }
     66 }
     67 
     68 /**
     69 * Launches a new Taskbar Tab, recreating it if it didn't exist.
     70 *
     71 * @param {object} aContext - Command line retrieved flags and context.
     72 */
     73 async function launchTaskbarTab(aContext) {
     74  let taskbarTab;
     75  try {
     76    taskbarTab = await lazy.TaskbarTabs.getTaskbarTab(aContext.id);
     77 
     78    lazy.logConsole.debug(
     79      `Found Taskbar Tab matching the flag "-taskbar-tab ${aContext.id}"`
     80    );
     81  } catch (e) {
     82    lazy.logConsole.debug(
     83      `Taskbar Tab for ID ${aContext.id} doesn't exist, reconstructing it.`
     84    );
     85 
     86    if (!Object.hasOwn(aContext, "userContextId")) {
     87      lazy.logConsole.error(
     88        "Expected -container flag, but found none. Using the default container."
     89      );
     90 
     91      aContext.userContextId =
     92        Services.scriptSecurityManager.DEFAULT_USER_CONTEXT_ID;
     93    }
     94 
     95    ({ taskbarTab } = await lazy.TaskbarTabs.findOrCreateTaskbarTab(
     96      aContext.url,
     97      aContext.userContextId
     98    ));
     99  }
    100 
    101  await lazy.TaskbarTabs.openWindow(taskbarTab);
    102 }