Bug 1407366 - Part 2: Rearrange RFPHelper for expansion r=johannh draft
authorTom Ritter <tom@mozilla.com>
Wed, 24 Apr 2019 09:17:53 -0500
changeset 1980227 d929994f46ebb041a623d98aecdeac98266db6c9
parent 1965449 744a475c948ee8c987d43a6348deca5e9a4a5a61
child 1980228 4b538f3e992bec812dcaaf5aca551ea7656580ff
push id356427
push usertritter@mozilla.com
push dateFri, 03 May 2019 16:48:12 +0000
treeherdertry@e57dbbcb5e7e [default view] [failures only]
reviewersjohannh
bugs1407366
milestone60.6.2
Bug 1407366 - Part 2: Rearrange RFPHelper for expansion r=johannh This patch rearranges RFPHelper.jsm to make it more clear what parts of the file are responsible for what feature.
toolkit/components/resistfingerprinting/RFPHelper.jsm
--- a/toolkit/components/resistfingerprinting/RFPHelper.jsm
+++ b/toolkit/components/resistfingerprinting/RFPHelper.jsm
@@ -9,92 +9,101 @@ var EXPORTED_SYMBOLS = ["RFPHelper"];
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 const kPrefResistFingerprinting = "privacy.resistFingerprinting";
 const kPrefSpoofEnglish = "privacy.spoof_english";
 const kTopicHttpOnModifyRequest = "http-on-modify-request";
 
 class _RFPHelper {
+  // ============================================================================
+  // Setup
+  // ============================================================================
   constructor() {
     this._initialized = false;
   }
 
   init() {
     if (this._initialized) {
       return;
     }
     this._initialized = true;
 
+    // Add unconditional observers
     Services.prefs.addObserver(kPrefResistFingerprinting, this);
+    // Add RFP observers if the pref is enabled
     this._handleResistFingerprintingChanged();
   }
 
   uninit() {
     if (!this._initialized) {
       return;
     }
     this._initialized = false;
 
+    // Remove unconditional observers
     Services.prefs.removeObserver(kPrefResistFingerprinting, this);
-    this._removeObservers();
+    // Remove the RFP observers, swallowing exceptions if they weren't present
+    this._removeRFPObservers();
   }
 
   observe(subject, topic, data) {
     switch (topic) {
       case "nsPref:changed":
         this._handlePrefChanged(data);
         break;
       case kTopicHttpOnModifyRequest:
         this._handleHttpOnModifyRequest(subject, data);
         break;
       default:
         break;
     }
   }
 
-  _removeObservers() {
-    try {
-      Services.pref.removeObserver(kPrefSpoofEnglish, this);
-    } catch (e) {
-      // do nothing
-    }
-    try {
-      Services.obs.removeObserver(this, kTopicHttpOnModifyRequest);
-    } catch (e) {
-      // do nothing
-    }
-  }
-
-  _shouldPromptForLanguagePref() {
-    return (Services.locale.getAppLocaleAsLangTag().substr(0, 2) !== "en")
-      && (Services.prefs.getIntPref(kPrefSpoofEnglish) === 0);
-  }
-
   _handlePrefChanged(data) {
     switch (data) {
       case kPrefResistFingerprinting:
         this._handleResistFingerprintingChanged();
         break;
       case kPrefSpoofEnglish:
         this._handleSpoofEnglishChanged();
         break;
       default:
         break;
     }
   }
 
+  // ============================================================================
+  // Language Prompt
+  // ============================================================================
+  _addRFPObservers() {
+    Services.prefs.addObserver(kPrefSpoofEnglish, this);
+    if (this._shouldPromptForLanguagePref()) {
+      Services.obs.addObserver(this, kTopicHttpOnModifyRequest);
+    }
+  }
+
+  _removeRFPObservers() {
+    try {
+      Services.pref.removeObserver(kPrefSpoofEnglish, this);
+    } catch (e) {
+      // do nothing
+    }
+    try {
+      Services.obs.removeObserver(this, kTopicHttpOnModifyRequest);
+    } catch (e) {
+      // do nothing
+    }
+  }
+
   _handleResistFingerprintingChanged() {
     if (Services.prefs.getBoolPref(kPrefResistFingerprinting)) {
-      Services.prefs.addObserver(kPrefSpoofEnglish, this);
-      if (this._shouldPromptForLanguagePref()) {
-        Services.obs.addObserver(this, kTopicHttpOnModifyRequest);
-      }
+      this._addRFPObservers();
     } else {
-      this._removeObservers();
+      this._removeRFPObservers();
       Services.prefs.setIntPref(kPrefSpoofEnglish, 0);
     }
   }
 
   _handleSpoofEnglishChanged() {
     switch (Services.prefs.getIntPref(kPrefSpoofEnglish)) {
       case 0: // will prompt
         // This should only happen when turning privacy.resistFingerprinting off.
@@ -111,16 +120,21 @@ class _RFPHelper {
         Services.prefs.setCharPref("intl.accept_languages", "en-US, en");
         Services.prefs.setBoolPref("javascript.use_us_english_locale", true);
         break;
       default:
         break;
     }
   }
 
+  _shouldPromptForLanguagePref() {
+    return (Services.locale.getAppLocaleAsLangTag().substr(0, 2) !== "en")
+      && (Services.prefs.getIntPref(kPrefSpoofEnglish) === 0);
+  }
+
   _handleHttpOnModifyRequest(subject, data) {
     // If we are loading an HTTP page from content, show the
     // "request English language web pages?" prompt.
     let httpChannel;
     try {
       httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
     } catch (e) {
       return;