[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor-launcher/master] Remove all observers when dialogs are closed.



commit 9c0ef4f67d5ec18e17e8a3e789a198819c90c7ce
Author: Kathy Brade <brade@xxxxxxxxxxxxxxxxx>
Date:   Thu Aug 7 14:14:57 2014 -0400

    Remove all observers when dialogs are closed.
---
 src/chrome/content/network-settings-wizard.xul |    3 +-
 src/chrome/content/network-settings.js         |   64 +++++++++++++++++++-----
 src/chrome/content/network-settings.xul        |    3 +-
 3 files changed, 56 insertions(+), 14 deletions(-)

diff --git a/src/chrome/content/network-settings-wizard.xul b/src/chrome/content/network-settings-wizard.xul
index da81ced..6a3b8fc 100644
--- a/src/chrome/content/network-settings-wizard.xul
+++ b/src/chrome/content/network-settings-wizard.xul
@@ -21,7 +21,8 @@
         buttonlabelextra2="&torsettings.copyLog;"
         onwizardfinish="return applySettings();"
         onwizardcancel="return onCancel();"
-        onload="initDialog();">
+        onload="initDialog();"
+        onunload="deinitDialog();">
 
   <script type="application/x-javascript"
           src="chrome://torlauncher/content/network-settings.js"/>
diff --git a/src/chrome/content/network-settings.js b/src/chrome/content/network-settings.js
index bdfb5c1..fdd565e 100644
--- a/src/chrome/content/network-settings.js
+++ b/src/chrome/content/network-settings.js
@@ -62,6 +62,7 @@ var gObsService = null;
 var gIsInitialBootstrap = false;
 var gIsBootstrapComplete = false;
 var gRestoreAfterHelpPanelID = null;
+var gActiveTopics = [];  // Topics for which an observer is currently installed.
 
 
 function initDialog()
@@ -156,10 +157,10 @@ function initDialog()
 
   initDefaultBridgeTypeMenu();
 
-  gObsService.addObserver(gObserver, kTorBootstrapErrorTopic, false);
-  gObsService.addObserver(gObserver, kTorLogHasWarnOrErrTopic, false);
-  gObsService.addObserver(gObserver, kTorProcessExitedTopic, false);
-  gObsService.addObserver(gObserver, kTorOpenProgressTopic, false);
+  addObserver(kTorBootstrapErrorTopic);
+  addObserver(kTorLogHasWarnOrErrTopic);
+  addObserver(kTorProcessExitedTopic);
+  addObserver(kTorOpenProgressTopic);
 
   var status = gTorProcessService.TorProcessStatus;
   if (TorLauncherUtil.shouldStartAndOwnTor &&
@@ -169,8 +170,8 @@ function initDialog()
       showErrorMessage(true, null);
     else
       showStartingTorPanel();
-    gObsService.addObserver(gObserver, kTorProcessReadyTopic, false);
-    gObsService.addObserver(gObserver, kTorProcessDidNotStartTopic, false);
+    addObserver(kTorProcessReadyTopic);
+    addObserver(kTorProcessDidNotStartTopic);
   }
   else
   {
@@ -199,6 +200,12 @@ function initDialog()
 }
 
 
+function deinitDialog()
+{
+  removeAllObservers();
+}
+
+
 // For now, we assume that the wizard buttons are the widest portion.
 // TODO: return a value for the settings dialog (non-wizard case).
 function maxWidthOfContent()
@@ -329,7 +336,8 @@ var gObserver = {
 
     if (kTorProcessReadyTopic == aTopic)
     {
-      gObsService.removeObserver(gObserver, kTorProcessReadyTopic);
+      removeObserver(kTorProcessReadyTopic);
+      removeObserver(kTorProcessDidNotStartTopic);
       var haveWizard = (getWizard() != null);
       showPanel();
       if (haveWizard)
@@ -341,12 +349,13 @@ var gObserver = {
     }
     else if (kTorProcessDidNotStartTopic == aTopic)
     {
-      gObsService.removeObserver(gObserver, kTorProcessDidNotStartTopic);
+      removeObserver(kTorProcessReadyTopic);
+      removeObserver(kTorProcessDidNotStartTopic);
       showErrorMessage(false, aData);
     }
     else if (kTorProcessExitedTopic == aTopic)
     {
-      gObsService.removeObserver(gObserver, kTorProcessExitedTopic);
+      removeObserver(kTorProcessExitedTopic);
       showErrorMessage(true, null);
     }
     else if (kTorOpenProgressTopic == aTopic)
@@ -357,6 +366,37 @@ var gObserver = {
 };
 
 
+// addObserver() will not add two observers for the same topic.
+function addObserver(aTopic)
+{
+  if (gActiveTopics.indexOf(aTopic) < 0)
+  {
+    gObsService.addObserver(gObserver, aTopic, false);
+    gActiveTopics.push(aTopic);
+  }
+}
+
+
+function removeObserver(aTopic)
+{
+  let idx = gActiveTopics.indexOf(aTopic);
+  if (idx >= 0)
+  {
+    gObsService.removeObserver(gObserver, aTopic);
+    gActiveTopics.splice(idx, 1);
+  }
+}
+
+
+function removeAllObservers()
+{
+  for (let i = gActiveTopics.length - 1; i >= 0; --i)
+    gObsService.removeObserver(gObserver, gActiveTopics[i]);
+
+  gActiveTopics = [];
+}
+
+
 function readTorSettings()
 {
   TorLauncherLogger.log(2, "readTorSettings " +
@@ -641,9 +681,9 @@ function onProxyTypeChange()
 function onRestartTor()
 {
   // Re-add these observers in case they have been removed.
-  gObsService.addObserver(gObserver, kTorProcessReadyTopic, false);
-  gObsService.addObserver(gObserver, kTorProcessDidNotStartTopic, false);
-  gObsService.addObserver(gObserver, kTorProcessExitedTopic, false);
+  addObserver(kTorProcessReadyTopic);
+  addObserver(kTorProcessDidNotStartTopic);
+  addObserver(kTorProcessExitedTopic);
 
   gTorProcessService._startTor();
   gTorProcessService._controlTor();
diff --git a/src/chrome/content/network-settings.xul b/src/chrome/content/network-settings.xul
index 1fcd4a7..937db71 100644
--- a/src/chrome/content/network-settings.xul
+++ b/src/chrome/content/network-settings.xul
@@ -24,7 +24,8 @@
         ondialogcancel="return onCancel();"
         ondialogextra2="onCopyLog();"
         ondialoghelp="onOpenHelp();"
-        onload="initDialog();">
+        onload="initDialog();"
+        onunload="deinitDialog();">
 
   <script type="application/x-javascript"
           src="chrome://torlauncher/content/network-settings.js"/>

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits