ext-normandyAddonStudy.js (2410B)
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 file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { AddonStudies } = ChromeUtils.importESModule( 8 "resource://normandy/lib/AddonStudies.sys.mjs" 9 ); 10 const { ClientID } = ChromeUtils.importESModule( 11 "resource://gre/modules/ClientID.sys.mjs" 12 ); 13 14 ChromeUtils.defineESModuleGetters(this, { 15 AddonManager: "resource://gre/modules/AddonManager.sys.mjs", 16 }); 17 18 this.normandyAddonStudy = class extends ExtensionAPI { 19 getAPI(context) { 20 let { extension } = context; 21 22 return { 23 normandyAddonStudy: { 24 /** 25 * Returns a study object for the current study. 26 * 27 * @returns {Study} 28 */ 29 async getStudy() { 30 const studies = await AddonStudies.getAll(); 31 return studies.find(study => study.addonId === extension.id); 32 }, 33 34 /** 35 * Marks the study as ended and then uninstalls the addon. 36 * 37 * @param {string} reason Why the study is ending 38 */ 39 async endStudy(reason) { 40 const study = await this.getStudy(); 41 42 // Mark the study as ended 43 await AddonStudies.markAsEnded(study, reason); 44 45 // Uninstall the addon 46 const addon = await AddonManager.getAddonByID(study.addonId); 47 if (addon) { 48 await addon.uninstall(); 49 } 50 }, 51 52 /** 53 * Returns an object with metadata about the client which may 54 * be required for constructing survey URLs. 55 * 56 * @returns {object} 57 */ 58 async getClientMetadata() { 59 return { 60 updateChannel: Services.appinfo.defaultUpdateChannel, 61 fxVersion: Services.appinfo.version, 62 clientID: await ClientID.getClientID(), 63 }; 64 }, 65 66 onUnenroll: new EventManager({ 67 context, 68 name: "normandyAddonStudy.onUnenroll", 69 register: fire => { 70 const listener = async reason => { 71 await fire.async(reason); 72 }; 73 74 AddonStudies.addUnenrollListener(extension.id, listener); 75 76 return () => { 77 AddonStudies.removeUnenrollListener(extension.id, listener); 78 }; 79 }, 80 }).api(), 81 }, 82 }; 83 } 84 };