Bug 639970 - Error: "document is null" / "Weave is not defined" in syncUI.js. r=Neil a=Callek
authorJens Hatlak <jh@junetz.de>
Wed, 03 Aug 2011 22:36:53 +0200
changeset 8327 c22150b0c74a2bd63092a6dd8601c269bbf83bbe
parent 8326 851aad78b9c6ceffc9386d396521d4f785e0a90b
child 8328 b72d03c050aedd220ed10fd4436d098e16a5d143
push idunknown
push userunknown
push dateunknown
reviewersNeil, Callek
bugs639970, 666330
Bug 639970 - Error: "document is null" / "Weave is not defined" in syncUI.js. r=Neil a=Callek This ports bug 666330 and fixes Sync issues with windows other than the initial one.
suite/common/sync/syncUI.js
--- a/suite/common/sync/syncUI.js
+++ b/suite/common/sync/syncUI.js
@@ -35,49 +35,63 @@
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 let gSyncUI = {
+  _obs: ["weave:service:sync:start",
+         "weave:service:sync:finish",
+         "weave:service:sync:error",
+         "weave:service:sync:delayed",
+         "weave:service:quota:remaining",
+         "weave:service:setup-complete",
+         "weave:service:login:start",
+         "weave:service:login:finish",
+         "weave:service:login:error",
+         "weave:service:logout:finish",
+         "weave:service:start-over",
+         "weave:notification:added"],
+
+  _unloaded: false,
+
   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);
 
     // This will be the first notification fired during init.
     // We can set up everything else later.
     Services.obs.addObserver(this, "weave:service:ready", true);
 
+    // The above notification is only sent once per session, i.e.
+    // it only works for the window that triggered the init.
+    if (Weave.Status.ready)
+      this.initUI();
+
     // Remove the observer if the window is closed before the observer
     // was triggered.
     window.addEventListener("unload", function SUI_unload() {
+      gSyncUI._unloaded = true;
       window.removeEventListener("unload", SUI_unload, false);
       Services.obs.removeObserver(gSyncUI, "weave:service:ready");
+
+      if (Weave.Status.ready) {
+        gSyncUI._obs.forEach(function(topic) {
+          Services.obs.removeObserver(gSyncUI, topic);
+        });
+      }
     }, false);
   },
 
   initUI: function SUI_initUI() {
-    let obs = ["weave:service:sync:start",
-               "weave:service:sync:finish",
-               "weave:service:sync:error",
-               "weave:service:sync:delayed",
-               "weave:service:quota:remaining",
-               "weave:service:setup-complete",
-               "weave:service:login:start",
-               "weave:service:login:finish",
-               "weave:service:login:error",
-               "weave:service:logout:finish",
-               "weave:service:start-over",
-               "weave:notification:added"];
-
-    obs.forEach(function(topic) {
+    this._obs.forEach(function(topic) {
       Services.obs.addObserver(this, topic, true);
     }, this);
 
     // Find the alltabs-popup
     let popup = document.getElementById("alltabs-popup");
     if (popup) {
       let self = this;
       popup.addEventListener("popupshowing", function() {
@@ -391,16 +405,19 @@ let gSyncUI = {
       this._wasDelayed = false;
     }
 
     this.updateUI();
     this._updateLastSyncTime();
   },
 
   observe: function SUI_observe(subject, topic, data) {
+    if (this._unloaded)
+      throw "SyncUI observer called after unload: " + topic;
+
     switch (topic) {
       case "weave:service:sync:start":
         this.onActivityStart();
         break;
       case "weave:service:sync:finish":
         this.onSyncFinish();
         break;
       case "weave:service:sync:error":