Bug 1358648 part 1 - Clean-up some Weave imports. r?markh draft
authorEdouard Oger <eoger@fastmail.com>
Tue, 02 May 2017 18:27:49 -0400
changeset 571619 c7e4fbfdae788cd8489c1ebff756d78a86654ff8
parent 571618 a748acbebbde373a88868dc02910fb2bc5e6a023
child 571620 12e90c0e480e87428a54cd2c9b7b8ba48ee29191
push id56865
push userbmo:eoger@fastmail.com
push dateWed, 03 May 2017 01:41:16 +0000
reviewersmarkh
bugs1358648
milestone55.0a1
Bug 1358648 part 1 - Clean-up some Weave imports. r?markh MozReview-Commit-ID: 2NK4Mew65UT
services/common/utils.js
services/sync/Weave.js
services/sync/modules/engines.js
services/sync/modules/telemetry.js
services/sync/modules/util.js
--- a/services/common/utils.js
+++ b/services/common/utils.js
@@ -1,21 +1,21 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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/. */
 
 var {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 this.EXPORTED_SYMBOLS = ["CommonUtils"];
 
-Cu.import("resource://gre/modules/Promise.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/osfile.jsm")
 Cu.import("resource://gre/modules/Log.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "OS",
+                                  "resource://gre/modules/osfile.jsm");
 
 this.CommonUtils = {
   /*
    * Set manipulation methods. These should be lifted into toolkit, or added to
    * `Set` itself.
    */
 
   /**
@@ -137,20 +137,20 @@ this.CommonUtils = {
 
   /**
    * Return a promise resolving on some later tick.
    *
    * This a wrapper around Promise.resolve() that prevents stack
    * accumulation and prevents callers from accidentally relying on
    * same-tick promise resolution.
    */
-  laterTickResolvingPromise(value, prototype) {
-    let deferred = Promise.defer(prototype);
-    this.nextTick(deferred.resolve.bind(deferred, value));
-    return deferred.promise;
+  laterTickResolvingPromise(value) {
+    return new Promise(resolve => {
+      this.nextTick(() => resolve(value));
+    });
   },
 
   /**
    * Return a timer that is scheduled to call the callback after waiting the
    * provided time or as soon as possible. The timer will be set as a property
    * of the provided object with the given timer name.
    */
   namedTimer: function namedTimer(callback, wait, thisObj, name) {
--- a/services/sync/Weave.js
+++ b/services/sync/Weave.js
@@ -3,19 +3,21 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 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/Promise.jsm");
-Cu.import("resource://services-sync/util.js");
+XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
+                                  "resource://gre/modules/FileUtils.jsm");
+XPCOMUtils.defineLazyGetter(this, "Utils", () => {
+  return Cu.import("resource://services-sync/util.js", {}).Utils;
+});
 
 const SYNC_PREFS_BRANCH = "services.sync.";
 
 
 /**
  * Sync's XPCOM service.
  *
  * It is named "Weave" for historical reasons.
@@ -77,24 +79,24 @@ WeaveService.prototype = {
     // Side-effect of accessing the service is that it is instantiated.
     Weave.Service;
   },
 
   whenLoaded() {
     if (this.ready) {
       return Promise.resolve();
     }
-    let deferred = Promise.defer();
-
-    Services.obs.addObserver(function onReady() {
-      Services.obs.removeObserver(onReady, "weave:service:ready");
-      deferred.resolve();
-    }, "weave:service:ready");
+    let onReadyPromise = new Promise(resolve => {
+      Services.obs.addObserver(function onReady() {
+        Services.obs.removeObserver(onReady, "weave:service:ready");
+        resolve();
+      }, "weave:service:ready");
+    });
     this.ensureLoaded();
-    return deferred.promise;
+    return onReadyPromise;
   },
 
   /**
    * Whether Sync appears to be enabled.
    *
    * This returns true if we have an associated FxA account
    *
    * It does *not* perform a robust check to see if the client is working.
--- a/services/sync/modules/engines.js
+++ b/services/sync/modules/engines.js
@@ -11,25 +11,26 @@ this.EXPORTED_SYMBOLS = [
   "Changeset"
 ];
 
 var {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
 
 Cu.import("resource://services-common/async.js");
 Cu.import("resource://gre/modules/JSONFile.jsm");
 Cu.import("resource://gre/modules/Log.jsm");
-Cu.import("resource://gre/modules/osfile.jsm");
 Cu.import("resource://services-common/observers.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/record.js");
 Cu.import("resource://services-sync/resource.js");
 Cu.import("resource://services-sync/util.js");
 
 XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts",
   "resource://gre/modules/FxAccounts.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "OS",
+                                  "resource://gre/modules/osfile.jsm");
 
 /*
  * Trackers are associated with a single engine and deal with
  * listening for changes to their particular data type.
  *
  * There are two things they keep track of:
  * 1) A score, indicating how urgently the engine wants to sync
  * 2) A list of IDs for all the changed items that need to be synced
--- a/services/sync/modules/telemetry.js
+++ b/services/sync/modules/telemetry.js
@@ -12,23 +12,24 @@ Cu.import("resource://services-sync/brow
 Cu.import("resource://services-sync/main.js");
 Cu.import("resource://services-sync/status.js");
 Cu.import("resource://services-sync/util.js");
 Cu.import("resource://services-sync/resource.js");
 Cu.import("resource://services-common/observers.js");
 Cu.import("resource://services-common/async.js");
 Cu.import("resource://gre/modules/Log.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/osfile.jsm", this);
 
 let constants = {};
 Cu.import("resource://services-sync/constants.js", constants);
 
 XPCOMUtils.defineLazyModuleGetter(this, "TelemetryController",
                               "resource://gre/modules/TelemetryController.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "OS",
+                                  "resource://gre/modules/osfile.jsm");
 
 XPCOMUtils.defineLazyServiceGetter(this, "Telemetry",
                                    "@mozilla.org/base/telemetry;1",
                                    "nsITelemetry");
 
 const log = Log.repository.getLogger("Sync.Telemetry");
 
 const TOPICS = [
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -9,18 +9,18 @@ var {classes: Cc, interfaces: Ci, result
 Cu.import("resource://services-common/observers.js");
 Cu.import("resource://services-common/utils.js");
 Cu.import("resource://services-common/async.js", this);
 Cu.import("resource://services-crypto/utils.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/Services.jsm", this);
 Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
-Cu.import("resource://gre/modules/osfile.jsm", this);
-Cu.import("resource://gre/modules/Task.jsm", this);
+XPCOMUtils.defineLazyModuleGetter(this, "OS",
+                                  "resource://gre/modules/osfile.jsm");
 
 // FxAccountsCommon.js doesn't use a "namespace", so create one here.
 XPCOMUtils.defineLazyGetter(this, "FxAccountsCommon", function() {
   let FxAccountsCommon = {};
   Cu.import("resource://gre/modules/FxAccountsCommon.js", FxAccountsCommon);
   return FxAccountsCommon;
 });
 
@@ -339,41 +339,41 @@ this.Utils = {
    *        <profile>/<filePath>.json. i.e. Do not specify the ".json"
    *        extension.
    * @param that
    *        Object to use for logging and "this" for callback.
    * @param callback
    *        Function to process json object as its first argument. If the file
    *        could not be loaded, the first argument will be undefined.
    */
-  jsonLoad: Task.async(function*(filePath, that, callback) {
+  async jsonLoad(filePath, that, callback) {
     let path = Utils.jsonFilePath(filePath);
 
     if (that._log) {
       that._log.trace("Loading json from disk: " + filePath);
     }
 
     let json;
 
     try {
-      json = yield CommonUtils.readJSON(path);
+      json = await CommonUtils.readJSON(path);
     } catch (e) {
       if (e instanceof OS.File.Error && e.becauseNoSuchFile) {
         // Ignore non-existent files, but explicitly return null.
         json = null;
       } else if (that._log) {
           that._log.debug("Failed to load json", e);
         }
     }
 
     if (callback) {
       callback.call(that, json);
     }
     return json;
-  }),
+  },
 
   /**
    * Save a json-able object to disk in the profile directory.
    *
    * @param filePath
    *        JSON file path save to <filePath>.json
    * @param that
    *        Object to use for logging and "this" for callback
@@ -381,40 +381,40 @@ this.Utils = {
    *        Function to provide json-able object to save. If this isn't a
    *        function, it'll be used as the object to make a json string.
    * @param callback
    *        Function called when the write has been performed. Optional.
    *        The first argument will be a Components.results error
    *        constant on error or null if no error was encountered (and
    *        the file saved successfully).
    */
-  jsonSave: Task.async(function*(filePath, that, obj, callback) {
+  async jsonSave(filePath, that, obj, callback) {
     let path = OS.Path.join(OS.Constants.Path.profileDir, "weave",
                             ...(filePath + ".json").split("/"));
     let dir = OS.Path.dirname(path);
     let error = null;
 
     try {
-      yield OS.File.makeDir(dir, { from: OS.Constants.Path.profileDir });
+      await OS.File.makeDir(dir, { from: OS.Constants.Path.profileDir });
 
       if (that._log) {
         that._log.trace("Saving json to disk: " + path);
       }
 
       let json = typeof obj == "function" ? obj.call(that) : obj;
 
-      yield CommonUtils.writeJSON(json, path);
+      await CommonUtils.writeJSON(json, path);
     } catch (e) {
       error = e
     }
 
     if (typeof callback == "function") {
       callback.call(that, error);
     }
-  }),
+  },
 
   /**
    * Move a json file in the profile directory. Will fail if a file exists at the
    * destination.
    *
    * @returns a promise that resolves to undefined on success, or rejects on failure
    *
    * @param aFrom