diff --git a/toolkit/components/telemetry/pings/UninstallPing.jsm b/toolkit/components/telemetry/pings/UninstallPing.jsm index e18b0e04077b..8dfd9001b5ab 100644 --- a/toolkit/components/telemetry/pings/UninstallPing.jsm +++ b/toolkit/components/telemetry/pings/UninstallPing.jsm @@ -5,7 +5,15 @@ "use strict"; -const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { XPCOMUtils } = ChromeUtils.import( + "resource://gre/modules/XPCOMUtils.jsm" +); + +XPCOMUtils.defineLazyModuleGetters(this, { + Services: "resource://gre/modules/Services.jsm", + WindowsInstallsInfo: + "resource://gre/modules/components-utils/WindowsInstallsInfo.jsm", +}); var EXPORTED_SYMBOLS = ["UninstallPing"]; @@ -24,81 +32,10 @@ var UninstallPing = { * Count other installs of this app, based on the values in the TaskBarIDs registry key. * */ - getOtherInstallsCount(rootKey) { - // This is somewhat more complicated than just counting the number of values on the key: - // 1) the same install can be listed in both HKCU and HKLM - // 2) the current path may not be listed - // - // The strategy is to add all paths to a Set (to deduplicate) and use that size. - - // Add the names of the values under `rootKey\subKey` to `set`, until the set has `maxCount`. - // All strings are lower cased first, as Windows paths are not case-sensitive. - function collectValues(rootKey, wowFlag, subKey, set, maxCount) { - if (set.size >= maxCount) { - return; - } - - const key = Cc["@mozilla.org/windows-registry-key;1"].createInstance( - Ci.nsIWindowsRegKey - ); - - try { - key.open(rootKey, subKey, key.ACCESS_READ | wowFlag); - } catch (_e) { - // The key may not exist, ignore. - // (nsWindowsRegKey::Open doesn't provide detailed error codes) - return; - } - const valueCount = key.valueCount; - - try { - for (let i = 0; i < valueCount && set.size < maxCount; ++i) { - set.add(key.getValueName(i).toLowerCase()); - } - } finally { - key.close(); - } - } - - const subKeyName = `Software\\Mozilla\\${Services.appinfo.name}\\TaskBarIDs`; - - const paths = new Set(); - - // The current install path may not have a value in TaskBarIDs. It is simpler to - // pre-add it and always include it in the total, than to check for it everywhere and - // sometimes include it. - paths.add(Services.dirsvc.get("GreBinD", Ci.nsIFile).path.toLowerCase()); - - const initialPathsCount = paths.size; - const maxPathsCount = initialPathsCount + this.MAX_OTHER_INSTALLS; - - // First collect from HKLM for both 32-bit and 64-bit installs regardless of the architecture - // of the current application. - for (const wowFlag of [ - Ci.nsIWindowsRegKey.WOW64_32, - Ci.nsIWindowsRegKey.WOW64_64, - ]) { - collectValues( - Ci.nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE, - wowFlag, - subKeyName, - paths, - maxPathsCount - ); - } - - // Then collect from HKCU. - // HKCU\Software is shared between 32 and 64 so nothing special is needed for WOW64, - // ref https://docs.microsoft.com/en-us/windows/win32/winprog64/shared-registry-keys - collectValues( - Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, - 0 /* wowFlag */, - subKeyName, - paths, - maxPathsCount - ); - - // Subtract our pre-added path, which is not an "other" install. - return paths.size - initialPathsCount; + getOtherInstallsCount() { + return WindowsInstallsInfo.getInstallPaths( + this.MAX_OTHER_INSTALLS, + new Set([Services.dirsvc.get("GreBinD", Ci.nsIFile).path]) + ).size; }, }; diff --git a/toolkit/components/utils/WindowsInstallsInfo.jsm b/toolkit/components/utils/WindowsInstallsInfo.jsm new file mode 100644 index 000000000000..558700911050 --- /dev/null +++ b/toolkit/components/utils/WindowsInstallsInfo.jsm @@ -0,0 +1,101 @@ +/* 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/. */ + +"use strict"; + +var EXPORTED_SYMBOLS = ["WindowsInstallsInfo"]; + +ChromeUtils.defineModuleGetter( + this, + "Services", + "resource://gre/modules/Services.jsm" +); + +var WindowsInstallsInfo = { + /** + * Retrieve install paths of this app, based on the values in the TaskBarIDs registry key. + * + * Installs from unarchived packages do not have a TaskBarID registry key and + * therefore won't appear in the result. + * + * @param {Number} [limit] Optional, maximum number of installation paths to count. + Defaults to 1024. + * @param {Set} [exclude] Optional, an Set of paths to exclude from the count. + * @returns {Set} Set of install paths, lower cased. + */ + getInstallPaths(limit = 1024, exclude = new Set()) { + // This is somewhat more complicated than just collecting all values because + // the same install can be listed in both HKCU and HKLM. The strategy is to + // add all paths to a Set to deduplicate. + + const lcExclude = new Set(); + exclude.forEach(p => lcExclude.add(p.toLowerCase())); + + // Add the names of the values under `rootKey\subKey` to `set`. + // All strings are lower cased first, as Windows paths are not case-sensitive. + function collectValues(rootKey, wowFlag, subKey, set) { + const key = Cc["@mozilla.org/windows-registry-key;1"].createInstance( + Ci.nsIWindowsRegKey + ); + + try { + key.open(rootKey, subKey, key.ACCESS_READ | wowFlag); + } catch (_e) { + // The key may not exist, ignore. + // (nsWindowsRegKey::Open doesn't provide detailed error codes) + return; + } + const valueCount = key.valueCount; + + try { + for (let i = 0; i < valueCount; ++i) { + const path = key.getValueName(i).toLowerCase(); + if (!lcExclude.has(path)) { + set.add(path); + } + if (set.size >= limit) { + break; + } + } + } finally { + key.close(); + } + } + + const subKeyName = `Software\\Mozilla\\${Services.appinfo.name}\\TaskBarIDs`; + + const paths = new Set(); + + // First collect from HKLM for both 32-bit and 64-bit installs regardless of the architecture + // of the current application. + for (const wowFlag of [ + Ci.nsIWindowsRegKey.WOW64_32, + Ci.nsIWindowsRegKey.WOW64_64, + ]) { + collectValues( + Ci.nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE, + wowFlag, + subKeyName, + paths + ); + if (paths.size >= limit) { + break; + } + } + + if (paths.size < limit) { + // Then collect from HKCU. + // HKCU\Software is shared between 32 and 64 so nothing special is needed for WOW64, + // ref https://docs.microsoft.com/en-us/windows/win32/winprog64/shared-registry-keys + collectValues( + Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, + 0 /* wowFlag */, + subKeyName, + paths + ); + } + + return paths; + }, +}; diff --git a/toolkit/components/utils/moz.build b/toolkit/components/utils/moz.build index a25f9f88efd3..d2028de02a57 100644 --- a/toolkit/components/utils/moz.build +++ b/toolkit/components/utils/moz.build @@ -18,6 +18,7 @@ EXTRA_JS_MODULES["components-utils"] = [ "mozjexl.js", "PreferenceFilters.jsm", "Sampling.jsm", + "WindowsInstallsInfo.jsm", "WindowsVersionInfo.jsm", ]