Bug 836702 Port front end sync lazy init changes from bug 836120 r=IanN
authorNeil Rashbrook <neil@parkwaycc.co.uk>
Tue, 12 Feb 2013 19:18:05 +0000
changeset 11942 13c3733f053f99a2071e2f10cabdb0f5637c26e7
parent 11941 db8918e7990795c2259c6f6cfe08fa8dbee0ad68
child 11943 d9f8f39bf0fac7cda35c5c5e01213f955e5a33a4
push id8890
push userneil@parkwaycc.co.uk
push dateTue, 12 Feb 2013 19:22:31 +0000
treeherdercomm-central@918a7b8058ee [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersIanN
bugs836702, 836120
Bug 836702 Port front end sync lazy init changes from bug 836120 r=IanN
suite/common/pref/pref-sync.js
suite/common/sync/syncUI.js
--- a/suite/common/pref/pref-sync.js
+++ b/suite/common/pref/pref-sync.js
@@ -23,40 +23,45 @@ let gSyncPane = {
 
   needsUpdate: function () {
     this.page = PAGE_NEEDS_UPDATE;
     let label = document.getElementById("loginError");
     label.value = Weave.Utils.getErrorString(Weave.Status.login);
     label.className = "error";
   },
 
-  init: function () {
-    let topics = [
-      "weave:service:login:error",
-      "weave:service:login:finish",
-      "weave:service:start-over",
-      "weave:service:setup-complete",
-      "weave:service:logout:finish"];
+  topics: [ "weave:service:ready",
+            "weave:service:login:error",
+            "weave:service:login:finish",
+            "weave:service:start-over",
+            "weave:service:setup-complete",
+            "weave:service:logout:finish"],
 
-    // Add the observers now and remove them on unload
-    //XXXzpao This should use Services.obs.* but Weave's Obs does nice handling
-    //        of `this`. Fix in a followup. (bug 583347)
-    topics.forEach(function(topic) {
-      Weave.Svc.Obs.add(topic, this.updateWeavePrefs, this);
-    }, this);
-    window.addEventListener("unload", function() {
-      topics.forEach(function (topic) {
-        Weave.Svc.Obs.remove(topic, this.updateWeavePrefs, this);
-      }, gSyncPane);
-    }, false);
+  init: function () {
+    for (var topic of this.topics)
+      Services.obs.addObserver(this, topic, false);
+
+    window.addEventListener("unload", this);
 
-    this.updateWeavePrefs();
+    var xps = Components.classes["@mozilla.org/weave/service;1"]
+                        .getService().wrappedJSObject;
+    if (xps.ready)
+      this.observe(null, "weave:service:ready", null);
+    else
+      xps.ensureLoaded();
   },
 
-  updateWeavePrefs: function () {
+  handleEvent: function (aEvent) {
+    window.removeEventListener("unload", this);
+
+    for (var topic of this.topics)
+      Services.obs.removeObserver(this, topic);
+  },
+
+  observe: function (aSubject, aTopic, aData) {
     if (Weave.Status.service == Weave.CLIENT_NOT_CONFIGURED ||
         Weave.Svc.Prefs.get("firstSync", "") == "notReady") {
       this.page = PAGE_NO_ACCOUNT;
     } else if (Weave.Status.login == Weave.LOGIN_FAILED_INVALID_PASSPHRASE ||
                Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED) {
       this.needsUpdate();
     } else {
       this.page = PAGE_HAS_ACCOUNT;
--- a/suite/common/sync/syncUI.js
+++ b/suite/common/sync/syncUI.js
@@ -22,17 +22,18 @@ let gSyncUI = {
   init: function SUI_init() {
     // Update the Tools menu according to whether Sync is set up or not.
     let taskPopup = document.getElementById("taskPopup");
     if (taskPopup)
       taskPopup.addEventListener("popupshowing", this.updateUI.bind(this), false);
 
     // Proceed to set up the UI if Sync has already started up.
     // Otherwise we'll do it when Sync is firing up.
-    if (Weave.Status.ready) {
+    if (Components.classes["@mozilla.org/weave/service;1"]
+                  .getService().wrappedJSObject.ready) {
       this.initUI();
       return;
     }
     
     Services.obs.addObserver(this, "weave:service:ready", true);
 
     // Remove the observer if the window is closed before the observer
     // was triggered.