Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | module.metadata = { |
michael@0 | 7 | "stability": "experimental" |
michael@0 | 8 | }; |
michael@0 | 9 | |
michael@0 | 10 | var { Cc, Ci } = require("chrome"); |
michael@0 | 11 | |
michael@0 | 12 | var appInfo = Cc["@mozilla.org/xre/app-info;1"] |
michael@0 | 13 | .getService(Ci.nsIXULAppInfo); |
michael@0 | 14 | var vc = Cc["@mozilla.org/xpcom/version-comparator;1"] |
michael@0 | 15 | .getService(Ci.nsIVersionComparator); |
michael@0 | 16 | |
michael@0 | 17 | var ID = exports.ID = appInfo.ID; |
michael@0 | 18 | var name = exports.name = appInfo.name; |
michael@0 | 19 | var version = exports.version = appInfo.version; |
michael@0 | 20 | var platformVersion = exports.platformVersion = appInfo.platformVersion; |
michael@0 | 21 | |
michael@0 | 22 | // The following mapping of application names to GUIDs was taken from: |
michael@0 | 23 | // |
michael@0 | 24 | // https://addons.mozilla.org/en-US/firefox/pages/appversions |
michael@0 | 25 | // |
michael@0 | 26 | // Using the GUID instead of the app's name is preferable because sometimes |
michael@0 | 27 | // re-branded versions of a product have different names: for instance, |
michael@0 | 28 | // Firefox, Minefield, Iceweasel, and Shiretoko all have the same |
michael@0 | 29 | // GUID. |
michael@0 | 30 | // This mapping is duplicated in `app-extensions/bootstrap.js`. They should keep |
michael@0 | 31 | // in sync, so if you change one, change the other too! |
michael@0 | 32 | |
michael@0 | 33 | var ids = exports.ids = { |
michael@0 | 34 | Firefox: "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}", |
michael@0 | 35 | Mozilla: "{86c18b42-e466-45a9-ae7a-9b95ba6f5640}", |
michael@0 | 36 | Sunbird: "{718e30fb-e89b-41dd-9da7-e25a45638b28}", |
michael@0 | 37 | SeaMonkey: "{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}", |
michael@0 | 38 | Fennec: "{aa3c5121-dab2-40e2-81ca-7ea25febc110}", |
michael@0 | 39 | Thunderbird: "{3550f703-e582-4d05-9a08-453d09bdfdc6}" |
michael@0 | 40 | }; |
michael@0 | 41 | |
michael@0 | 42 | function is(name) { |
michael@0 | 43 | if (!(name in ids)) |
michael@0 | 44 | throw new Error("Unkown Mozilla Application: " + name); |
michael@0 | 45 | return ID == ids[name]; |
michael@0 | 46 | }; |
michael@0 | 47 | exports.is = is; |
michael@0 | 48 | |
michael@0 | 49 | function isOneOf(names) { |
michael@0 | 50 | for (var i = 0; i < names.length; i++) |
michael@0 | 51 | if (is(names[i])) |
michael@0 | 52 | return true; |
michael@0 | 53 | return false; |
michael@0 | 54 | }; |
michael@0 | 55 | exports.isOneOf = isOneOf; |
michael@0 | 56 | |
michael@0 | 57 | /** |
michael@0 | 58 | * Use this to check whether the given version (e.g. xulApp.platformVersion) |
michael@0 | 59 | * is in the given range. Versions must be in version comparator-compatible |
michael@0 | 60 | * format. See MDC for details: |
michael@0 | 61 | * https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIVersionComparator |
michael@0 | 62 | */ |
michael@0 | 63 | var versionInRange = exports.versionInRange = |
michael@0 | 64 | function versionInRange(version, lowInclusive, highExclusive) { |
michael@0 | 65 | return (vc.compare(version, lowInclusive) >= 0) && |
michael@0 | 66 | (vc.compare(version, highExclusive) < 0); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | const reVersionRange = /^((?:<|>)?=?)?\s*((?:\d+[\S]*)|\*)(?:\s+((?:<|>)=?)?(\d+[\S]+))?$/; |
michael@0 | 70 | const reOnlyInifinity = /^[<>]?=?\s*[*x]$/; |
michael@0 | 71 | const reSubInfinity = /\.[*x]/g; |
michael@0 | 72 | const reHyphenRange = /^(\d+.*?)\s*-\s*(\d+.*?)$/; |
michael@0 | 73 | const reRangeSeparator = /\s*\|\|\s*/; |
michael@0 | 74 | |
michael@0 | 75 | const compares = { |
michael@0 | 76 | "=": function (c) { return c === 0 }, |
michael@0 | 77 | ">=": function (c) { return c >= 0 }, |
michael@0 | 78 | "<=": function (c) { return c <= 0}, |
michael@0 | 79 | "<": function (c) { return c < 0 }, |
michael@0 | 80 | ">": function (c) { return c > 0 } |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | function normalizeRange(range) { |
michael@0 | 84 | return range |
michael@0 | 85 | .replace(reOnlyInifinity, "") |
michael@0 | 86 | .replace(reSubInfinity, ".*") |
michael@0 | 87 | .replace(reHyphenRange, ">=$1 <=$2") |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * Compare the versions given, using the comparison operator provided. |
michael@0 | 92 | * Internal use only. |
michael@0 | 93 | * |
michael@0 | 94 | * @example |
michael@0 | 95 | * compareVersion("1.2", "<=", "1.*") // true |
michael@0 | 96 | * |
michael@0 | 97 | * @param {String} version |
michael@0 | 98 | * A version to compare |
michael@0 | 99 | * |
michael@0 | 100 | * @param {String} comparison |
michael@0 | 101 | * The comparison operator |
michael@0 | 102 | * |
michael@0 | 103 | * @param {String} compareVersion |
michael@0 | 104 | * A version to compare |
michael@0 | 105 | */ |
michael@0 | 106 | function compareVersion(version, comparison, compareVersion) { |
michael@0 | 107 | let hasWildcard = compareVersion.indexOf("*") !== -1; |
michael@0 | 108 | |
michael@0 | 109 | comparison = comparison || "="; |
michael@0 | 110 | |
michael@0 | 111 | if (hasWildcard) { |
michael@0 | 112 | switch (comparison) { |
michael@0 | 113 | case "=": |
michael@0 | 114 | let zeroVersion = compareVersion.replace(reSubInfinity, ".0"); |
michael@0 | 115 | return versionInRange(version, zeroVersion, compareVersion); |
michael@0 | 116 | case ">=": |
michael@0 | 117 | compareVersion = compareVersion.replace(reSubInfinity, ".0"); |
michael@0 | 118 | break; |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | let compare = compares[comparison]; |
michael@0 | 123 | |
michael@0 | 124 | return typeof compare === "function" && compare(vc.compare(version, compareVersion)); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Returns `true` if `version` satisfies the `versionRange` given. |
michael@0 | 129 | * If only an argument is passed, is used as `versionRange` and compared against |
michael@0 | 130 | * `xulApp.platformVersion`. |
michael@0 | 131 | * |
michael@0 | 132 | * `versionRange` is either a string which has one or more space-separated |
michael@0 | 133 | * descriptors, or a range like "fromVersion - toVersion". |
michael@0 | 134 | * Version range descriptors may be any of the following styles: |
michael@0 | 135 | * |
michael@0 | 136 | * - "version" Must match `version` exactly |
michael@0 | 137 | * - "=version" Same as just `version` |
michael@0 | 138 | * - ">version" Must be greater than `version` |
michael@0 | 139 | * - ">=version" Must be greater or equal than `version` |
michael@0 | 140 | * - "<version" Must be less than `version` |
michael@0 | 141 | * - "<=version" Must be less or equal than `version` |
michael@0 | 142 | * - "1.2.x" or "1.2.*" See 'X version ranges' below |
michael@0 | 143 | * - "*" or "" (just an empty string) Matches any version |
michael@0 | 144 | * - "version1 - version2" Same as ">=version1 <=version2" |
michael@0 | 145 | * - "range1 || range2" Passes if either `range1` or `range2` are satisfied |
michael@0 | 146 | * |
michael@0 | 147 | * For example, these are all valid: |
michael@0 | 148 | * - "1.0.0 - 2.9999.9999" |
michael@0 | 149 | * - ">=1.0.2 <2.1.2" |
michael@0 | 150 | * - ">1.0.2 <=2.3.4" |
michael@0 | 151 | * - "2.0.1" |
michael@0 | 152 | * - "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0" |
michael@0 | 153 | * - "2.x" (equivalent to "2.*") |
michael@0 | 154 | * - "1.2.x" (equivalent to "1.2.*" and ">=1.2.0 <1.3.0") |
michael@0 | 155 | */ |
michael@0 | 156 | function satisfiesVersion(version, versionRange) { |
michael@0 | 157 | if (arguments.length === 1) { |
michael@0 | 158 | versionRange = version; |
michael@0 | 159 | version = appInfo.version; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | let ranges = versionRange.trim().split(reRangeSeparator); |
michael@0 | 163 | |
michael@0 | 164 | return ranges.some(function(range) { |
michael@0 | 165 | range = normalizeRange(range); |
michael@0 | 166 | |
michael@0 | 167 | // No versions' range specified means that any version satisfies the |
michael@0 | 168 | // requirements. |
michael@0 | 169 | if (range === "") |
michael@0 | 170 | return true; |
michael@0 | 171 | |
michael@0 | 172 | let matches = range.match(reVersionRange); |
michael@0 | 173 | |
michael@0 | 174 | if (!matches) |
michael@0 | 175 | return false; |
michael@0 | 176 | |
michael@0 | 177 | let [, lowMod, lowVer, highMod, highVer] = matches; |
michael@0 | 178 | |
michael@0 | 179 | return compareVersion(version, lowMod, lowVer) && (highVer !== undefined |
michael@0 | 180 | ? compareVersion(version, highMod, highVer) |
michael@0 | 181 | : true); |
michael@0 | 182 | }); |
michael@0 | 183 | } |
michael@0 | 184 | exports.satisfiesVersion = satisfiesVersion; |