Bug 1357517 - Remove Preferences.jsm usage from some toolkit modules. r=MattN
authorMarco Castelluccio <mcastelluccio@mozilla.com>
Sat, 29 Jul 2017 00:55:58 +0200
changeset 420590 e05ac296ea1472fd8f2ec7f2347e25348275e76c
parent 420589 65105a4351d19736d45d6dbef38acd2e9313d7db
child 420591 11eaa60784b8886d85db57d07404c4285c1b7a98
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1357517
milestone56.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 1357517 - Remove Preferences.jsm usage from some toolkit modules. r=MattN
toolkit/modules/ClientID.jsm
toolkit/modules/GMPInstallManager.jsm
toolkit/modules/Locale.jsm
toolkit/modules/tests/xpcshell/test_client_id.js
--- a/toolkit/modules/ClientID.jsm
+++ b/toolkit/modules/ClientID.jsm
@@ -4,17 +4,17 @@
 
 "use strict";
 
 this.EXPORTED_SYMBOLS = ["ClientID"];
 
 const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/Preferences.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Log.jsm");
 
 const LOGGER_NAME = "Toolkit.Telemetry";
 const LOGGER_PREFIX = "ClientID::";
 
 XPCOMUtils.defineLazyModuleGetter(this, "CommonUtils",
                                   "resource://services-common/utils.js");
 XPCOMUtils.defineLazyModuleGetter(this, "OS",
@@ -173,23 +173,28 @@ var ClientIDImpl = {
    */
   getCachedClientID() {
     if (this._clientID) {
       // Already loaded the client id from disk.
       return this._clientID;
     }
 
     // Not yet loaded, return the cached client id if we have one.
-    let id = Preferences.get(PREF_CACHED_CLIENTID, null);
+    if (Services.prefs.getPrefType(PREF_CACHED_CLIENTID) != Ci.nsIPrefBranch.PREF_STRING) {
+      this._log.error("getCachedClientID - invalid client id type in preferences, resetting");
+      Services.prefs.clearUserPref(PREF_CACHED_CLIENTID);
+    }
+
+    let id = Services.prefs.getStringPref(PREF_CACHED_CLIENTID, null);
     if (id === null) {
       return null;
     }
     if (!isValidClientID(id)) {
       this._log.error("getCachedClientID - invalid client id in preferences, resetting", id);
-      Preferences.reset(PREF_CACHED_CLIENTID);
+      Services.prefs.clearUserPref(PREF_CACHED_CLIENTID);
       return null;
     }
     return id;
   },
 
   /*
    * Resets the provider. This is for testing only.
    */
@@ -209,17 +214,17 @@ var ClientIDImpl = {
    */
   updateClientID(id) {
     if (!isValidClientID(id)) {
       this._log.error("updateClientID - invalid client ID", id);
       return false;
     }
 
     this._clientID = id;
-    Preferences.set(PREF_CACHED_CLIENTID, this._clientID);
+    Services.prefs.setStringPref(PREF_CACHED_CLIENTID, this._clientID);
     return true;
   },
 
   /**
    * A helper for getting access to telemetry logger.
    */
   get _log() {
     if (!this._logger) {
--- a/toolkit/modules/GMPInstallManager.jsm
+++ b/toolkit/modules/GMPInstallManager.jsm
@@ -17,17 +17,16 @@ var GMPInstallFailureReason = {
   GMP_DISABLED: 3,
   GMP_UPDATE_DISABLED: 4,
 };
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/FileUtils.jsm");
 Cu.import("resource://gre/modules/PromiseUtils.jsm");
-Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/Log.jsm");
 Cu.import("resource://gre/modules/osfile.jsm");
 Cu.import("resource://gre/modules/GMPUtils.jsm");
 Cu.import("resource://gre/modules/addons/ProductAddonChecker.jsm");
 
 this.EXPORTED_SYMBOLS = ["GMPInstallManager", "GMPExtractor", "GMPDownloader",
                          "GMPAddon"];
 
--- a/toolkit/modules/Locale.jsm
+++ b/toolkit/modules/Locale.jsm
@@ -2,17 +2,16 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 this.EXPORTED_SYMBOLS = ["Locale"];
 
 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
 
 Cu.import("resource://gre/modules/Services.jsm");
-Cu.import("resource://gre/modules/Preferences.jsm");
 
 this.Locale = {
   /**
    * Gets the currently selected locale for display.
    * @return  the selected locale or "en-US" if none is selected
    */
   getLocale() {
     return Services.locale.getRequestedLocale() || "en-US";
--- a/toolkit/modules/tests/xpcshell/test_client_id.js
+++ b/toolkit/modules/tests/xpcshell/test_client_id.js
@@ -4,29 +4,35 @@
 "use strict";
 
 var {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
 Cu.import("resource://gre/modules/ClientID.jsm");
 Cu.import("resource://services-common/utils.js");
 Cu.import("resource://gre/modules/osfile.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/Preferences.jsm");
 
 function run_test() {
   do_get_profile();
   run_next_test();
 }
 
 add_task(async function() {
   const drsPath = OS.Path.join(OS.Constants.Path.profileDir, "datareporting", "state.json");
   const fhrDir  = OS.Path.join(OS.Constants.Path.profileDir, "healthreport");
   const fhrPath = OS.Path.join(fhrDir, "state.json");
   const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
-  const invalidIDs = [-1, 0.5, "INVALID-UUID", true, "", "3d1e1560-682a-4043-8cf2-aaaaaaaaaaaZ"];
+  const invalidIDs = [
+    [-1, "setIntPref"],
+    [0.5, "setIntPref"],
+    ["INVALID-UUID", "setStringPref"],
+    [true, "setBoolPref"],
+    ["", "setStringPref"],
+    ["3d1e1560-682a-4043-8cf2-aaaaaaaaaaaZ", "setStringPref"],
+  ];
   const PREF_CACHED_CLIENTID = "toolkit.telemetry.cachedClientID";
 
   await OS.File.makeDir(fhrDir);
 
   // Check that we are importing the FHR client ID.
   let clientID = CommonUtils.generateUUID();
   await CommonUtils.writeJSON({clientID}, fhrPath);
   Assert.equal(clientID, await ClientID.getClientID());
@@ -68,27 +74,29 @@ add_task(async function() {
   await ClientID._reset();
   await OS.File.remove(fhrPath);
   await OS.File.writeAtomic(drsPath, "abcd", {encoding: "utf-8", tmpPath: drsPath + ".tmp"});
   clientID = await ClientID.getClientID();
   Assert.equal(typeof(clientID), "string");
   Assert.ok(uuidRegex.test(clientID));
 
   // If both the FHR and DSR data are broken, we should end up with a new client ID.
-  for (let invalidID of invalidIDs) {
+  for (let [invalidID, ] of invalidIDs) {
     await ClientID._reset();
     await CommonUtils.writeJSON({clientID: invalidID}, fhrPath);
     await CommonUtils.writeJSON({clientID: invalidID}, drsPath);
     clientID = await ClientID.getClientID();
     Assert.equal(typeof(clientID), "string");
     Assert.ok(uuidRegex.test(clientID));
   }
 
   // Assure that cached IDs are being checked for validity.
-  for (let invalidID of invalidIDs) {
+  for (let [invalidID, prefFunc] of invalidIDs) {
     await ClientID._reset();
-    Preferences.set(PREF_CACHED_CLIENTID, invalidID);
+    Services.prefs[prefFunc](PREF_CACHED_CLIENTID, invalidID);
     let cachedID = ClientID.getCachedClientID();
     Assert.strictEqual(cachedID, null, "ClientID should ignore invalid cached IDs");
-    let prefID = Preferences.get(PREF_CACHED_CLIENTID, null);
-    Assert.strictEqual(prefID, null, "ClientID should reset invalid cached IDs");
+    Assert.ok(!Services.prefs.prefHasUserValue(PREF_CACHED_CLIENTID),
+              "ClientID should reset invalid cached IDs");
+    Assert.ok(Services.prefs.getPrefType(PREF_CACHED_CLIENTID) == Ci.nsIPrefBranch.PREF_INVALID,
+              "ClientID should reset invalid cached IDs");
   }
 });