Skip to content

Instantly share code, notes, and snippets.

@aminomancer
Last active February 24, 2023 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aminomancer/dbdf556f86103238e1f272b52292b5b1 to your computer and use it in GitHub Desktop.
Save aminomancer/dbdf556f86103238e1f272b52292b5b1 to your computer and use it in GitHub Desktop.
ASRouter listens to enrollment changes
--- a/browser/components/newtab/lib/ASRouter.jsm
+++ b/browser/components/newtab/lib/ASRouter.jsm
@@ -98,7 +98,7 @@ const JEXL_PROVIDER_CACHE = new Set(["snippets"]);
// To observe the app locale change notification.
const TOPIC_INTL_LOCALE_CHANGED = "intl:app-locales-changed";
-const TOPIC_EXPERIMENT_FORCE_ENROLLED = "nimbus:force-enroll";
+const NIMBUS_TOPICS = ["nimbus:enroll", "nimbus:unenroll"];
// To observe the pref that controls if ASRouter should use the remote Fluent files for l10n.
const USE_REMOTE_L10N_PREF =
"browser.newtabpage.activity-stream.asrouter.useRemoteL10n";
@@ -627,7 +627,7 @@ class _ASRouter {
this.isUnblockedMessage = this.isUnblockedMessage.bind(this);
this.unblockAll = this.unblockAll.bind(this);
this.forceWNPanel = this.forceWNPanel.bind(this);
- this._onExperimentForceEnrolled = this._onExperimentForceEnrolled.bind(
+ this._onExperimentEnrollmentChanged = this._onExperimentEnrollmentChanged.bind(
this
);
this.forcePBWindow = this.forcePBWindow.bind(this);
@@ -1041,10 +1041,9 @@ class _ASRouter {
lazy.SpecialMessageActions.blockMessageById = this.blockMessageById;
Services.obs.addObserver(this._onLocaleChanged, TOPIC_INTL_LOCALE_CHANGED);
- Services.obs.addObserver(
- this._onExperimentForceEnrolled,
- TOPIC_EXPERIMENT_FORCE_ENROLLED
- );
+ for (const topic of NIMBUS_TOPICS) {
+ Services.obs.addObserver(this._onExperimentEnrollmentChanged, topic);
+ }
Services.prefs.addObserver(USE_REMOTE_L10N_PREF, this);
// sets .initialized to true and resolves .waitForInitialized promise
this._finishInitializing();
@@ -1074,10 +1073,9 @@ class _ASRouter {
this._onLocaleChanged,
TOPIC_INTL_LOCALE_CHANGED
);
- Services.obs.removeObserver(
- this._onExperimentForceEnrolled,
- TOPIC_EXPERIMENT_FORCE_ENROLLED
- );
+ for (const topic of NIMBUS_TOPICS) {
+ Services.obs.removeObserver(this._onExperimentEnrollmentChanged, topic);
+ }
Services.prefs.removeObserver(USE_REMOTE_L10N_PREF, this);
// If we added any CFR recommendations, they need to be removed
CFRPageActions.clearRecommendations();
@@ -1987,7 +1985,7 @@ class _ASRouter {
await lazy.ToolbarPanelHub._hideToolbarButton(win);
}
- async _onExperimentForceEnrolled(subject, topic, slug) {
+ async _onExperimentEnrollmentChanged(subject, topic, slug) {
const experimentProvider = this.state.providers.find(
p => p.id === "messaging-experiments"
);
--- a/toolkit/components/nimbus/lib/ExperimentManager.jsm
+++ b/toolkit/components/nimbus/lib/ExperimentManager.jsm
@@ -402,6 +402,7 @@ class _ExperimentManager {
this.setExperimentActive(experiment);
}
this.sendEnrollmentTelemetry(experiment);
+ Services.obs.notifyObservers(null, "nimbus:enroll", slug);
this._setEnrollmentPrefs(prefsToSet);
this._updatePrefObservers(experiment);
@@ -452,8 +453,6 @@ class _ExperimentManager {
{ force: true }
);
- Services.obs.notifyObservers(null, "nimbus:force-enroll", slug);
-
return enrollment;
}
@@ -480,6 +479,7 @@ class _ExperimentManager {
if (!branch) {
// Our branch has been removed. Unenroll.
this.unenroll(recipe.slug, "branch-removed");
+ Services.obs.notifyObservers(null, "nimbus:unenroll", recipe.slug);
}
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment