michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "experimental" michael@0: }; michael@0: michael@0: var { Cc, Ci } = require("chrome"); michael@0: michael@0: var appInfo = Cc["@mozilla.org/xre/app-info;1"] michael@0: .getService(Ci.nsIXULAppInfo); michael@0: var vc = Cc["@mozilla.org/xpcom/version-comparator;1"] michael@0: .getService(Ci.nsIVersionComparator); michael@0: michael@0: var ID = exports.ID = appInfo.ID; michael@0: var name = exports.name = appInfo.name; michael@0: var version = exports.version = appInfo.version; michael@0: var platformVersion = exports.platformVersion = appInfo.platformVersion; michael@0: michael@0: // The following mapping of application names to GUIDs was taken from: michael@0: // michael@0: // https://addons.mozilla.org/en-US/firefox/pages/appversions michael@0: // michael@0: // Using the GUID instead of the app's name is preferable because sometimes michael@0: // re-branded versions of a product have different names: for instance, michael@0: // Firefox, Minefield, Iceweasel, and Shiretoko all have the same michael@0: // GUID. michael@0: // This mapping is duplicated in `app-extensions/bootstrap.js`. They should keep michael@0: // in sync, so if you change one, change the other too! michael@0: michael@0: var ids = exports.ids = { michael@0: Firefox: "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}", michael@0: Mozilla: "{86c18b42-e466-45a9-ae7a-9b95ba6f5640}", michael@0: Sunbird: "{718e30fb-e89b-41dd-9da7-e25a45638b28}", michael@0: SeaMonkey: "{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}", michael@0: Fennec: "{aa3c5121-dab2-40e2-81ca-7ea25febc110}", michael@0: Thunderbird: "{3550f703-e582-4d05-9a08-453d09bdfdc6}" michael@0: }; michael@0: michael@0: function is(name) { michael@0: if (!(name in ids)) michael@0: throw new Error("Unkown Mozilla Application: " + name); michael@0: return ID == ids[name]; michael@0: }; michael@0: exports.is = is; michael@0: michael@0: function isOneOf(names) { michael@0: for (var i = 0; i < names.length; i++) michael@0: if (is(names[i])) michael@0: return true; michael@0: return false; michael@0: }; michael@0: exports.isOneOf = isOneOf; michael@0: michael@0: /** michael@0: * Use this to check whether the given version (e.g. xulApp.platformVersion) michael@0: * is in the given range. Versions must be in version comparator-compatible michael@0: * format. See MDC for details: michael@0: * https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIVersionComparator michael@0: */ michael@0: var versionInRange = exports.versionInRange = michael@0: function versionInRange(version, lowInclusive, highExclusive) { michael@0: return (vc.compare(version, lowInclusive) >= 0) && michael@0: (vc.compare(version, highExclusive) < 0); michael@0: } michael@0: michael@0: const reVersionRange = /^((?:<|>)?=?)?\s*((?:\d+[\S]*)|\*)(?:\s+((?:<|>)=?)?(\d+[\S]+))?$/; michael@0: const reOnlyInifinity = /^[<>]?=?\s*[*x]$/; michael@0: const reSubInfinity = /\.[*x]/g; michael@0: const reHyphenRange = /^(\d+.*?)\s*-\s*(\d+.*?)$/; michael@0: const reRangeSeparator = /\s*\|\|\s*/; michael@0: michael@0: const compares = { michael@0: "=": function (c) { return c === 0 }, michael@0: ">=": function (c) { return c >= 0 }, michael@0: "<=": function (c) { return c <= 0}, michael@0: "<": function (c) { return c < 0 }, michael@0: ">": function (c) { return c > 0 } michael@0: } michael@0: michael@0: function normalizeRange(range) { michael@0: return range michael@0: .replace(reOnlyInifinity, "") michael@0: .replace(reSubInfinity, ".*") michael@0: .replace(reHyphenRange, ">=$1 <=$2") michael@0: } michael@0: michael@0: /** michael@0: * Compare the versions given, using the comparison operator provided. michael@0: * Internal use only. michael@0: * michael@0: * @example michael@0: * compareVersion("1.2", "<=", "1.*") // true michael@0: * michael@0: * @param {String} version michael@0: * A version to compare michael@0: * michael@0: * @param {String} comparison michael@0: * The comparison operator michael@0: * michael@0: * @param {String} compareVersion michael@0: * A version to compare michael@0: */ michael@0: function compareVersion(version, comparison, compareVersion) { michael@0: let hasWildcard = compareVersion.indexOf("*") !== -1; michael@0: michael@0: comparison = comparison || "="; michael@0: michael@0: if (hasWildcard) { michael@0: switch (comparison) { michael@0: case "=": michael@0: let zeroVersion = compareVersion.replace(reSubInfinity, ".0"); michael@0: return versionInRange(version, zeroVersion, compareVersion); michael@0: case ">=": michael@0: compareVersion = compareVersion.replace(reSubInfinity, ".0"); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: let compare = compares[comparison]; michael@0: michael@0: return typeof compare === "function" && compare(vc.compare(version, compareVersion)); michael@0: } michael@0: michael@0: /** michael@0: * Returns `true` if `version` satisfies the `versionRange` given. michael@0: * If only an argument is passed, is used as `versionRange` and compared against michael@0: * `xulApp.platformVersion`. michael@0: * michael@0: * `versionRange` is either a string which has one or more space-separated michael@0: * descriptors, or a range like "fromVersion - toVersion". michael@0: * Version range descriptors may be any of the following styles: michael@0: * michael@0: * - "version" Must match `version` exactly michael@0: * - "=version" Same as just `version` michael@0: * - ">version" Must be greater than `version` michael@0: * - ">=version" Must be greater or equal than `version` michael@0: * - "=version1 <=version2" michael@0: * - "range1 || range2" Passes if either `range1` or `range2` are satisfied michael@0: * michael@0: * For example, these are all valid: michael@0: * - "1.0.0 - 2.9999.9999" michael@0: * - ">=1.0.2 <2.1.2" michael@0: * - ">1.0.2 <=2.3.4" michael@0: * - "2.0.1" michael@0: * - "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0" michael@0: * - "2.x" (equivalent to "2.*") michael@0: * - "1.2.x" (equivalent to "1.2.*" and ">=1.2.0 <1.3.0") michael@0: */ michael@0: function satisfiesVersion(version, versionRange) { michael@0: if (arguments.length === 1) { michael@0: versionRange = version; michael@0: version = appInfo.version; michael@0: } michael@0: michael@0: let ranges = versionRange.trim().split(reRangeSeparator); michael@0: michael@0: return ranges.some(function(range) { michael@0: range = normalizeRange(range); michael@0: michael@0: // No versions' range specified means that any version satisfies the michael@0: // requirements. michael@0: if (range === "") michael@0: return true; michael@0: michael@0: let matches = range.match(reVersionRange); michael@0: michael@0: if (!matches) michael@0: return false; michael@0: michael@0: let [, lowMod, lowVer, highMod, highVer] = matches; michael@0: michael@0: return compareVersion(version, lowMod, lowVer) && (highVer !== undefined michael@0: ? compareVersion(version, highMod, highVer) michael@0: : true); michael@0: }); michael@0: } michael@0: exports.satisfiesVersion = satisfiesVersion;