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

[tor-commits] [snowflake/master] Reorder enable checks



commit d4aa9ad2b3abd73c13e32a847d5a0247b4364e81
Author: Arlo Breault <arlolra@xxxxxxxxx>
Date:   Thu Sep 26 12:07:24 2019 -0400

    Reorder enable checks
    
    First check that it is enabled before doing feature testing.
    
    This will be useful in the badge so that probing only happens if it is
    enabled.
---
 proxy/init-webext.js | 63 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 26 deletions(-)

diff --git a/proxy/init-webext.js b/proxy/init-webext.js
index f6c0d0f..6789ffd 100644
--- a/proxy/init-webext.js
+++ b/proxy/init-webext.js
@@ -26,30 +26,40 @@ class WebExtUI extends UI {
   }
 
   initToggle() {
-    if (!Util.hasWebRTC()) {
-      this.missingFeature = 'popupWebRTCOff';
-      this.setEnabled(false);
-      return;
-    }
-    WS.probeWebsocket(config.relayAddr)
-    .then(
-      () => {
-        chrome.storage.local.get("snowflake-enabled", (result) => {
-          let enabled = this.enabled;
-          if (result['snowflake-enabled'] !== void 0) {
-            enabled = result['snowflake-enabled'];
-          } else {
-            log("Toggle state not yet saved");
-          }
-          this.setEnabled(enabled);
-        });
-      },
-      () => {
-        log('Could not connect to bridge.');
-        this.missingFeature = 'popupBridgeUnreachable';
+    // First, check if we have our status stored
+    (new Promise((resolve) => {
+      chrome.storage.local.get(["snowflake-enabled"], resolve);
+    }))
+    .then((result) => {
+      let enabled = this.enabled;
+      if (result['snowflake-enabled'] !== void 0) {
+        enabled = result['snowflake-enabled'];
+      } else {
+        log("Toggle state not yet saved");
+      }
+      // If it isn't enabled, stop
+      if (!enabled) {
+        this.setEnabled(enabled);
+        return;
+      }
+      // Otherwise, do feature checks
+      if (!Util.hasWebRTC()) {
+        this.missingFeature = 'popupWebRTCOff';
         this.setEnabled(false);
+        return;
       }
-    );
+      WS.probeWebsocket(config.relayAddr)
+      .then(
+        () => {
+          this.setEnabled(true);
+        },
+        () => {
+          log('Could not connect to bridge.');
+          this.missingFeature = 'popupBridgeUnreachable';
+          this.setEnabled(false);
+        }
+      );
+    });
   }
 
   postActive() {
@@ -73,11 +83,12 @@ class WebExtUI extends UI {
   }
 
   onMessage(m) {
-    this.setEnabled(m.enabled);
-    chrome.storage.local.set({
-      "snowflake-enabled": this.enabled
-    }, function() {
+    (new Promise((resolve) => {
+      chrome.storage.local.set({ "snowflake-enabled": m.enabled }, resolve);
+    }))
+    .then(() => {
       log("Stored toggle state");
+      this.initToggle();
     });
   }
 



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