Bug 1646149 - Switch about:pioneer from AMO API to Remote Settings r=leplatrem
authorRobert Helmer <rhelmer@mozilla.com>
Thu, 25 Jun 2020 17:23:06 +0000
changeset 537653 e89aaae327560aa0334d90a7b1a87f1eb9269f45
parent 537652 6727729203e9412620066073e60e526d3c6b30f7
child 537654 917028c5ad2c1f1a01923d85467a19f21c6b6df8
push id37545
push usernerli@mozilla.com
push dateSat, 27 Jun 2020 09:38:32 +0000
treeherdermozilla-central@0a4b3f99d2d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersleplatrem
bugs1646149
milestone79.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1646149 - Switch about:pioneer from AMO API to Remote Settings r=leplatrem Differential Revision: https://phabricator.services.mozilla.com/D80978
browser/components/pioneer/content/pioneer.js
browser/components/pioneer/test/browser/browser_pioneer_ui.js
--- a/browser/components/pioneer/content/pioneer.js
+++ b/browser/components/pioneer/content/pioneer.js
@@ -19,30 +19,30 @@ const { AddonManager } = ChromeUtils.imp
   "resource://gre/modules/AddonManager.jsm"
 );
 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 const { XPCOMUtils } = ChromeUtils.import(
   "resource://gre/modules/XPCOMUtils.jsm"
 );
 
-ChromeUtils.defineModuleGetter(
-  this,
-  "AddonRepository",
-  "resource://gre/modules/addons/AddonRepository.jsm"
+const { RemoteSettings } = ChromeUtils.import(
+  "resource://services-settings/remote-settings.js"
 );
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   FileUtils: "resource://gre/modules/FileUtils.jsm",
 });
 
 const PREF_PIONEER_ID = "toolkit.telemetry.pioneerId";
 
-// For the pilot program (Fx78), only allow a single add-on ID.
-const STUDY_ADDON_WHITELIST = ["pioneer-v2-example@mozilla.org"];
+/**
+ * This is the Remote Settings key that we use to get the list of available studies.
+ */
+const STUDY_ADDON_COLLECTION_KEY = "pioneer-study-addons";
 
 function showEnrollmentStatus() {
   const pioneerId = Services.prefs.getStringPref(PREF_PIONEER_ID, null);
 
   const enrollmentButton = document.getElementById("enrollment-button");
 
   document.l10n.setAttributes(
     enrollmentButton,
@@ -79,17 +79,17 @@ async function showAvailableStudies(cach
 
     const studyName = document.createElement("h3");
     studyName.setAttribute("class", "card-name");
     studyName.textContent = cachedAddon.name;
     studyBody.appendChild(studyName);
 
     const studyCreator = document.createElement("span");
     studyCreator.setAttribute("class", "card-creator");
-    studyCreator.textContent = cachedAddon.creator;
+    studyCreator.textContent = cachedAddon.creator.name;
     studyBody.appendChild(studyCreator);
 
     const actions = document.createElement("div");
     actions.classList.add("card-actions");
     study.appendChild(actions);
 
     const joinBtn = document.createElement("button");
     joinBtn.setAttribute("id", `${studyAddonId}-join-button`);
@@ -139,26 +139,26 @@ async function showAvailableStudies(cach
       }
 
       if (addon) {
         await addon.uninstall();
       } else {
         joinBtn.disabled = true;
         await install.install();
       }
-      updateStudy(studyAddonId);
+      await updateStudy(studyAddonId);
     }
 
     enrollStudyBtn.addEventListener("input", toggleEnrolled);
     joinBtn.addEventListener("click", toggleEnrolled);
 
     const availableStudies = document.getElementById("available-studies");
     availableStudies.appendChild(study);
 
-    updateStudy(studyAddonId);
+    await updateStudy(studyAddonId);
   }
 
   const availableStudies = document.getElementById("header-available-studies");
   document.l10n.setAttributes(availableStudies, "pioneer-available-studies");
 }
 
 async function updateStudy(studyAddonId) {
   let addon;
@@ -203,78 +203,79 @@ async function updateStudy(studyAddonId)
 function generateUUID() {
   let str = Cc["@mozilla.org/uuid-generator;1"]
     .getService(Ci.nsIUUIDGenerator)
     .generateUUID()
     .toString();
   return str.substring(1, str.length - 1);
 }
 
-function setup() {
+async function setup(cachedAddons) {
   document
     .getElementById("enrollment-button")
     .addEventListener("click", async event => {
       const pioneerId = Services.prefs.getStringPref(PREF_PIONEER_ID, null);
 
       if (pioneerId) {
         Services.prefs.clearUserPref(PREF_PIONEER_ID);
+        for (const cachedAddon of cachedAddons) {
+          const addon = await AddonManager.getAddonByID(cachedAddon.id);
+          if (addon) {
+            await addon.uninstall();
+          }
 
-        for (const studyAddonId of STUDY_ADDON_WHITELIST) {
-          const study = document.getElementById(studyAddonId);
+          const study = document.getElementById(cachedAddon.id);
           if (study) {
-            const addon = await AddonManager.getAddonByID(studyAddonId);
-            if (addon) {
-              await addon.uninstall();
-            }
-            updateStudy(studyAddonId);
+            await updateStudy(cachedAddon.id);
           }
         }
       } else {
         let uuid = generateUUID();
         Services.prefs.setStringPref(PREF_PIONEER_ID, uuid);
-
-        for (const studyAddonId of STUDY_ADDON_WHITELIST) {
-          const study = document.getElementById(studyAddonId);
+        for (const addon of cachedAddons) {
+          const study = document.getElementById(addon.id);
           if (study) {
-            updateStudy(studyAddonId);
+            await updateStudy(addon.id);
           }
         }
       }
-      await showEnrollmentStatus();
+      showEnrollmentStatus();
     });
 
-  const onAddonEvent = addon => {
-    if (STUDY_ADDON_WHITELIST.includes(addon.id)) {
-      updateStudy(addon.id);
+  const onAddonEvent = async addon => {
+    for (const cachedAddon in cachedAddons) {
+      if (cachedAddon.id == addon.id) {
+        await updateStudy(addon.id);
+      }
     }
   };
   const addonsListener = {
     onEnabled: onAddonEvent,
     onDisabled: onAddonEvent,
     onInstalled: onAddonEvent,
     onUninstalled: onAddonEvent,
   };
   AddonManager.addAddonListener(addonsListener);
 
   window.addEventListener("unload", event => {
     AddonManager.removeAddonListener(addonsListener);
   });
 }
 
 document.addEventListener("DOMContentLoaded", async domEvent => {
-  setup();
   showEnrollmentStatus();
 
   let cachedAddons;
   if (Cu.isInAutomation) {
     let testCachedAddons = Services.prefs.getStringPref(
       "toolkit.pioneer.testCachedAddons",
       null
     );
     if (testCachedAddons) {
       cachedAddons = JSON.parse(testCachedAddons);
     }
   } else {
-    cachedAddons = await AddonRepository.cacheAddons(STUDY_ADDON_WHITELIST);
+    cachedAddons = await RemoteSettings(STUDY_ADDON_COLLECTION_KEY).get();
   }
 
-  showAvailableStudies(cachedAddons);
+  await setup(cachedAddons);
+  await showAvailableStudies(cachedAddons);
 });
--- a/browser/components/pioneer/test/browser/browser_pioneer_ui.js
+++ b/browser/components/pioneer/test/browser/browser_pioneer_ui.js
@@ -10,17 +10,17 @@
 
 const PREF_PIONEER_ID = "toolkit.telemetry.pioneerId";
 
 const PREF_CACHED_ADDONS = "toolkit.pioneer.testCachedAddons";
 const PREF_TEST_ADDON_INSTALLED = "toolkit.pioneer.testAddonInstalled";
 
 const CACHED_ADDONS = [
   {
-    id: "pioneer-v2-example@mozilla.org",
+    id: "pioneer-v2-example@pioneer.mozilla.org",
     icons: {
       "32":
         "https://localhost/user-media/addon_icons/2644/2644632-32.png?modified=4a64e2bc",
       "64":
         "https://localhost/user-media/addon_icons/2644/2644632-64.png?modified=4a64e2bc",
       "128":
         "https://localhost/user-media/addon_icons/2644/2644632-128.png?modified=4a64e2bc",
     },
@@ -79,16 +79,18 @@ add_task(async function() {
   ok(
     beforeToolbarButton.hidden,
     "before enrollment, Pioneer toolbar button is hidden."
   );
 
   const enrollmentButton = content.document.getElementById("enrollment-button");
   enrollmentButton.click();
 
+  await waitForAnimationFrame();
+
   const pioneerEnrolled = Services.prefs.getStringPref(PREF_PIONEER_ID, null);
   ok(pioneerEnrolled, "after enrollment, Pioneer pref is set.");
 
   const enrolledToolbarButton = document.getElementById("pioneer-button");
   ok(
     !enrolledToolbarButton.hidden,
     "after enrollment, Pioneer toolbar button is not hidden."
   );