/* 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/. */
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
import { BrowserUtils } from "resource://gre/modules/BrowserUtils.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const APPLE_COPY_LINK = "com.apple.share.CopyLink.invite";
let lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
QRCodeGenerator:
"moz-src:///browser/components/qrcode/QRCodeGenerator.sys.mjs",
});
XPCOMUtils.defineLazyServiceGetters(lazy, {
WindowsUIUtils: ["@mozilla.org/windows-ui-utils;1", Ci.nsIWindowsUIUtils],
});
// Use a non-caching getter so tests can swap out the service via MockRegistrar.
Object.defineProperty(lazy, "MacSharingService", {
get() {
return Cc["@mozilla.org/widget/macsharingservice;1"].getService(
Ci.nsIMacSharingService
);
},
});
class SharingUtilsCls {
/**
* Ensures a "Share" submenu exists in a given menu, creating it if necessary.
*
* @param {MozBrowser} contextBrowser
* The browser of the right-clicked (context) tab.
* @param {MozBrowser[]|null} browsers
* All selected browsers in tab strip order. Pass null for single URL sharing.
* @param {Element} insertAfterEl
* The menu item after which the share item is inserted.
*/
ensureShareMenu(contextBrowser, browsers, insertAfterEl) {
if (!Services.prefs.getBoolPref("browser.menu.share_url.allow", true)) {
return;
}
let hasShareableURL =
browsers !== null
? browsers.some(b => BrowserUtils.getShareableURL(b.currentURI))
: !!BrowserUtils.getShareableURL(contextBrowser.currentURI);
let shareMenu;
let oldElement = insertAfterEl.nextElementSibling;
if (oldElement?.matches(".share-tab-url-item")) {
shareMenu = oldElement;
} else {
shareMenu = this.#createShareMenu(insertAfterEl);
}
shareMenu.contextBrowserToShare = Cu.getWeakReference(contextBrowser);
shareMenu.browsersToShare =
browsers?.map(b => Cu.getWeakReference(b)) ?? null;
if (AppConstants.platform != "macosx") {
// On macOS, we keep the item visible and handle enable/disable
// inside the menupopup.
shareMenu.hidden = !hasShareableURL;
}
}
/**
* Creates and inserts the "Share"