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