Bug 1462725 - Add eligibility check; r=rhelmer
On startup, ends the study if auto private browsing mode is enabled.
I did not add a pref observer for this, since when the pref is changed, the changes won't take effect until the next startup anyway.
MozReview-Commit-ID: Do5BM6QopSX
--- a/browser/modules/SavantShieldStudy.jsm
+++ b/browser/modules/SavantShieldStudy.jsm
@@ -21,66 +21,82 @@ XPCOMUtils.defineLazyGetter(this, "log",
};
return new ConsoleAPI(consoleOptions);
});
class SavantShieldStudyClass {
constructor() {
this.STUDY_PREF = "shield.savant.enabled";
this.STUDY_TELEMETRY_CATEGORY = "savant";
+ this.ALWAYS_PRIVATE_BROWSING_PREF = "browser.privatebrowsing.autostart";
}
init() {
this.TelemetryEvents = new TelemetryEvents(this.STUDY_TELEMETRY_CATEGORY);
- // TODO: implement eligibility (#13)
- const isEligible = true;
- if (!isEligible) {
- this.endStudy("ineligible");
- return;
- }
-
// TODO check expiration, add study duration override pref
// check the pref in case Normandy flipped it on before we could add the pref listener
this.shouldCollect = Services.prefs.getBoolPref(this.STUDY_PREF);
if (this.shouldCollect) {
- this.TelemetryEvents.enableCollection();
+ this.startupStudy();
}
Services.prefs.addObserver(this.STUDY_PREF, this);
}
observe(subject, topic, data) {
if (topic === "nsPref:changed" && data === this.STUDY_PREF) {
// toggle state of the pref
this.shouldCollect = !this.shouldCollect;
if (this.shouldCollect) {
- this.TelemetryEvents.enableCollection();
+ this.startupStudy();
} else {
// The pref has been turned off
- this.endStudy("expired");
+ this.endStudy("study-disabled");
}
}
}
+ startupStudy() {
+ if (!this.isEligible()) {
+ this.endStudy("ineligible");
+ return;
+ }
+
+ this.TelemetryEvents.enableCollection();
+ }
+
+ isEligible() {
+ const isAlwaysPrivateBrowsing = Services.prefs.getBoolPref(this.ALWAYS_PRIVATE_BROWSING_PREF);
+ if (isAlwaysPrivateBrowsing) {
+ return false;
+ }
+
+ return true;
+ }
+
+
sendEvent(method, object, value, extra) {
this.TelemetryEvents.sendEvent(method, object, value, extra);
}
endStudy(reason) {
+ log.debug(`Ending the study due to reason: ${ reason }`);
this.TelemetryEvents.disableCollection();
// Services.telemetry.recordEvent(this.STUDY_TELEMETRY_CATEGORY, "end_study", reason);
this.uninit();
+ // Study pref needs to persist between restarts, so only reset on endStudy
+ Services.prefs.clearUserPref(this.STUDY_PREF);
}
// Called on every Firefox shutdown and endStudy
uninit() {
// TODO: clear study expiration override pref and remove its listener
+ Services.prefs.removeObserver(this.ALWAYS_PRIVATE_BROWSING_PREF, this);
Services.prefs.removeObserver(this.STUDY_PREF, this);
- Services.prefs.clearUserPref(this.STUDY_PREF);
Services.prefs.clearUserPref(PREF_LOG_LEVEL);
}
}
const SavantShieldStudy = new SavantShieldStudyClass();
// references:
// - https://hg.mozilla.org/mozilla-central/file/tip/toolkit/components/normandy/lib/TelemetryEvents.jsm