Bug 1608150 [wpt PR 21112] - Add missing space in `./wpt lint` command line docs...
[gecko.git] / toolkit / modules / GMPUtils.jsm
blobf0075eabc86f2898df6d923782af2d6404316049
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/. */
5 "use strict";
7 var EXPORTED_SYMBOLS = [
8   "GMP_PLUGIN_IDS",
9   "GMPPrefs",
10   "GMPUtils",
11   "OPEN_H264_ID",
12   "WIDEVINE_ID",
15 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
16 const { AppConstants } = ChromeUtils.import(
17   "resource://gre/modules/AppConstants.jsm"
19 const { UpdateUtils } = ChromeUtils.import(
20   "resource://gre/modules/UpdateUtils.jsm"
23 // GMP IDs
24 const OPEN_H264_ID = "gmp-gmpopenh264";
25 const WIDEVINE_ID = "gmp-widevinecdm";
26 const GMP_PLUGIN_IDS = [OPEN_H264_ID, WIDEVINE_ID];
28 var GMPUtils = {
29   /**
30    * Checks whether or not a given plugin is hidden. Hidden plugins are neither
31    * downloaded nor displayed in the addons manager.
32    * @param   aPlugin
33    *          The plugin to check.
34    */
35   isPluginHidden(aPlugin) {
36     if (this._is32bitModeMacOS()) {
37       // GMPs are hidden on MacOS when running in 32 bit mode.
38       // See bug 1291537.
39       return true;
40     }
42     if (!this._isPluginSupported(aPlugin) || !this._isPluginVisible(aPlugin)) {
43       return true;
44     }
46     if (!aPlugin.isEME) {
47       return false;
48     }
50     if (!GMPPrefs.getBool(GMPPrefs.KEY_EME_ENABLED, true)) {
51       return true;
52     }
54     return false;
55   },
57   /**
58    * Checks whether or not a given plugin is supported by the current OS.
59    * @param   aPlugin
60    *          The plugin to check.
61    */
62   _isPluginSupported(aPlugin) {
63     if (this._isPluginForceSupported(aPlugin)) {
64       return true;
65     }
66     if (aPlugin.id == WIDEVINE_ID) {
67       // The Widevine plugin is available for Windows versions Vista and later,
68       // Mac OSX, and Linux.
69       return (
70         AppConstants.platform == "win" ||
71         AppConstants.platform == "macosx" ||
72         AppConstants.platform == "linux"
73       );
74     }
76     return true;
77   },
79   _is32bitModeMacOS() {
80     if (AppConstants.platform != "macosx") {
81       return false;
82     }
83     return Services.appinfo.XPCOMABI.split("-")[0] == "x86";
84   },
86   /**
87    * Checks whether or not a given plugin is visible in the addons manager
88    * UI and the "enable DRM" notification box. This can be used to test
89    * plugins that aren't yet turned on in the mozconfig.
90    * @param   aPlugin
91    *          The plugin to check.
92    */
93   _isPluginVisible(aPlugin) {
94     return GMPPrefs.getBool(GMPPrefs.KEY_PLUGIN_VISIBLE, false, aPlugin.id);
95   },
97   /**
98    * Checks whether or not a given plugin is forced-supported. This is used
99    * in automated tests to override the checks that prevent GMPs running on an
100    * unsupported platform.
101    * @param   aPlugin
102    *          The plugin to check.
103    */
104   _isPluginForceSupported(aPlugin) {
105     return GMPPrefs.getBool(
106       GMPPrefs.KEY_PLUGIN_FORCE_SUPPORTED,
107       false,
108       aPlugin.id
109     );
110   },
112   _isWindowsOnARM64() {
113     return AppConstants.platform == "win" && UpdateUtils.ABI.match(/aarch64/);
114   },
116   _expectedABI(aPlugin) {
117     let defaultABI = UpdateUtils.ABI;
118     if (aPlugin.id == WIDEVINE_ID && this._isWindowsOnARM64()) {
119       // On Windows on aarch64, we need the x86 plugin,
120       // as there's no native aarch64 plugins yet.
121       defaultABI = defaultABI.replace(/aarch64/g, "x86");
122     }
123     return defaultABI;
124   },
128  * Manages preferences for GMP addons
129  */
130 var GMPPrefs = {
131   KEY_EME_ENABLED: "media.eme.enabled",
132   KEY_PLUGIN_ENABLED: "media.{0}.enabled",
133   KEY_PLUGIN_LAST_UPDATE: "media.{0}.lastUpdate",
134   KEY_PLUGIN_VERSION: "media.{0}.version",
135   KEY_PLUGIN_AUTOUPDATE: "media.{0}.autoupdate",
136   KEY_PLUGIN_VISIBLE: "media.{0}.visible",
137   KEY_PLUGIN_ABI: "media.{0}.abi",
138   KEY_PLUGIN_FORCE_SUPPORTED: "media.{0}.forceSupported",
139   KEY_URL: "media.gmp-manager.url",
140   KEY_URL_OVERRIDE: "media.gmp-manager.url.override",
141   KEY_CERT_CHECKATTRS: "media.gmp-manager.cert.checkAttributes",
142   KEY_CERT_REQUIREBUILTIN: "media.gmp-manager.cert.requireBuiltIn",
143   KEY_UPDATE_LAST_CHECK: "media.gmp-manager.lastCheck",
144   KEY_SECONDS_BETWEEN_CHECKS: "media.gmp-manager.secondsBetweenChecks",
145   KEY_UPDATE_ENABLED: "media.gmp-manager.updateEnabled",
146   KEY_APP_DISTRIBUTION: "distribution.id",
147   KEY_APP_DISTRIBUTION_VERSION: "distribution.version",
148   KEY_BUILDID: "media.gmp-manager.buildID",
149   KEY_CERTS_BRANCH: "media.gmp-manager.certs.",
150   KEY_PROVIDER_ENABLED: "media.gmp-provider.enabled",
151   KEY_LOG_BASE: "media.gmp.log.",
152   KEY_LOGGING_LEVEL: "media.gmp.log.level",
153   KEY_LOGGING_DUMP: "media.gmp.log.dump",
155   /**
156    * Obtains the specified string preference in relation to the specified plugin.
157    * @param aKey The preference key value to use.
158    * @param aDefaultValue The default value if no preference exists.
159    * @param aPlugin The plugin to scope the preference to.
160    * @return The obtained preference value, or the defaultValue if none exists.
161    */
162   getString(aKey, aDefaultValue, aPlugin) {
163     if (
164       aKey === this.KEY_APP_DISTRIBUTION ||
165       aKey === this.KEY_APP_DISTRIBUTION_VERSION
166     ) {
167       return Services.prefs.getDefaultBranch(null).getCharPref(aKey, "default");
168     }
169     return Services.prefs.getStringPref(
170       this.getPrefKey(aKey, aPlugin),
171       aDefaultValue
172     );
173   },
175   /**
176    * Obtains the specified int preference in relation to the specified plugin.
177    * @param aKey The preference key value to use.
178    * @param aDefaultValue The default value if no preference exists.
179    * @param aPlugin The plugin to scope the preference to.
180    * @return The obtained preference value, or the defaultValue if none exists.
181    */
182   getInt(aKey, aDefaultValue, aPlugin) {
183     return Services.prefs.getIntPref(
184       this.getPrefKey(aKey, aPlugin),
185       aDefaultValue
186     );
187   },
189   /**
190    * Obtains the specified bool preference in relation to the specified plugin.
191    * @param aKey The preference key value to use.
192    * @param aDefaultValue The default value if no preference exists.
193    * @param aPlugin The plugin to scope the preference to.
194    * @return The obtained preference value, or the defaultValue if none exists.
195    */
196   getBool(aKey, aDefaultValue, aPlugin) {
197     return Services.prefs.getBoolPref(
198       this.getPrefKey(aKey, aPlugin),
199       aDefaultValue
200     );
201   },
203   /**
204    * Sets the specified string preference in relation to the specified plugin.
205    * @param aKey The preference key value to use.
206    * @param aVal The value to set.
207    * @param aPlugin The plugin to scope the preference to.
208    */
209   setString(aKey, aVal, aPlugin) {
210     Services.prefs.setStringPref(this.getPrefKey(aKey, aPlugin), aVal);
211   },
213   /**
214    * Sets the specified bool preference in relation to the specified plugin.
215    * @param aKey The preference key value to use.
216    * @param aVal The value to set.
217    * @param aPlugin The plugin to scope the preference to.
218    */
219   setBool(aKey, aVal, aPlugin) {
220     Services.prefs.setBoolPref(this.getPrefKey(aKey, aPlugin), aVal);
221   },
223   /**
224    * Sets the specified int preference in relation to the specified plugin.
225    * @param aKey The preference key value to use.
226    * @param aVal The value to set.
227    * @param aPlugin The plugin to scope the preference to.
228    */
229   setInt(aKey, aVal, aPlugin) {
230     Services.prefs.setIntPref(this.getPrefKey(aKey, aPlugin), aVal);
231   },
233   /**
234    * Checks whether or not the specified preference is set in relation to the
235    * specified plugin.
236    * @param aKey The preference key value to use.
237    * @param aPlugin The plugin to scope the preference to.
238    * @return true if the preference is set, false otherwise.
239    */
240   isSet(aKey, aPlugin) {
241     return Services.prefs.prefHasUserValue(this.getPrefKey(aKey, aPlugin));
242   },
244   /**
245    * Resets the specified preference in relation to the specified plugin to its
246    * default.
247    * @param aKey The preference key value to use.
248    * @param aPlugin The plugin to scope the preference to.
249    */
250   reset(aKey, aPlugin) {
251     Services.prefs.clearUserPref(this.getPrefKey(aKey, aPlugin));
252   },
254   /**
255    * Scopes the specified preference key to the specified plugin.
256    * @param aKey The preference key value to use.
257    * @param aPlugin The plugin to scope the preference to.
258    * @return A preference key scoped to the specified plugin.
259    */
260   getPrefKey(aKey, aPlugin) {
261     return aKey.replace("{0}", aPlugin || "");
262   },