Bug 1462725 - Add eligibility check; r=rhelmer draft
authorBianca Danforth <bdanforth@mozilla.com>
Wed, 23 May 2018 07:25:20 -0700
changeset 803323 f26a1112e2cadb3d0ec016cef2fd375e2dcdf5dd
parent 803322 6320edb2aa39fa104c02147896a090fa556a7419
child 803324 d8f6dcf3105968a3cc2381b28fd277c207e446e1
push id112069
push userbdanforth@mozilla.com
push dateSun, 03 Jun 2018 04:54:10 +0000
reviewersrhelmer
bugs1462725
milestone62.0a1
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: DqABLEai8zx
browser/modules/SavantShieldStudy.jsm
--- a/browser/modules/SavantShieldStudy.jsm
+++ b/browser/modules/SavantShieldStudy.jsm
@@ -25,68 +25,84 @@ 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";
   }
 
   async init() {
     this.TELEMETRY_CLIENT_ID = await ClientID.getClientID();
     this.TelemetryEvents = new TelemetryEvents(this.STUDY_TELEMETRY_CATEGORY,
                                               this.TELEMETRY_CLIENT_ID);
 
-    // 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);
   }
 
   async getFlowID(str) {
     return this.TelemetryEvents.getFlowID(str);
   }
 }