Source code

Revision control

Copy as Markdown

Other Tools

/* 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 { BANDWIDTH } from "chrome://browser/content/ipprotection/ipprotection-constants.mjs";
const countryDisplayNames = new Intl.DisplayNames(undefined, {
type: "region",
});
/**
* Returns the localized country name for a given country code.
*
* @param {string} code
* @returns {string|null}
*/
export function countryName(code) {
try {
return countryDisplayNames.of(code) ?? null;
} catch (_) {
return null;
}
}
/**
* Formats remaining bandwidth bytes into a rounded value with a unit indicator.
*
* @param {number} remainingBytes - Remaining bandwidth in bytes.
* @param {string} [locale] - BCP 47 locale tag; defaults to the runtime locale.
* @returns {{ value: number|string, useGB: boolean }}
* `value` is the remaining amount: a locale-formatted string rounded to 1
* decimal place (when >= 1 GB) or a floored integer in MB (when < 1 GB).
* `useGB` indicates whether the value is in GB (true) or MB (false).
*/
export function formatRemainingBandwidth(remainingBytes, locale = undefined) {
const remainingGB = remainingBytes / BANDWIDTH.BYTES_IN_GB;
if (remainingGB < 1) {
return {
value: Math.floor(remainingBytes / BANDWIDTH.BYTES_IN_MB),
useGB: false,
};
}
return {
value: new Intl.NumberFormat(locale, {
maximumFractionDigits: 1,
}).format(remainingGB),
useGB: true,
};
}
/**
* Returns a principal reflecting the user-visible URL for
* the provided browser, rather than the loaded document's principal.
* This especially matters for pages rendered through Firefox's built-in
* viewers (e.g. PDF, JSON), where the content document principal is a
* `resource://pdf.js` or `resource://devtools` origin even though the
* URL bar still shows the underlying http(s) URL. Using the URL bar
* URI ensures the IP Protection panel and toolbar button operate
* against the site the user actually navigated to.
*
* @param {object} gBrowser
* The window's gBrowser.
* @returns {nsIPrincipal|null}
* The principal for the current page, or null if it can't be derived.
*/
export function getSitePrincipal(gBrowser) {
let uri = gBrowser?.currentURI;
if (!uri) {
return null;
}
let originAttributes =
gBrowser.selectedBrowser?.browsingContext?.originAttributes ?? {};
return Services.scriptSecurityManager.createContentPrincipal(
uri,
originAttributes
);
}