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

[tor-commits] [pluggable-transports/snowflake-webext] 04/04: Change long timers to alarms



This is an automated email from the git hooks/post-receive script.

cohosh pushed a commit to branch i29
in repository pluggable-transports/snowflake-webext.

commit cb68d6ee34c93727b7afbf1b81821a81a6a13807
Author: Cecylia Bocovich <cohosh@xxxxxxxxxxxxxx>
AuthorDate: Tue Nov 1 14:21:24 2022 -0400

    Change long timers to alarms
    
    With manifest v3, our background page is not persistent. Longer timers
    set with setTimeout and setInterval are replaced with alarms and an
    alarm listener.
---
 config.js                 |  4 ++--
 init-badge.js             | 19 +++++++++++++++++++
 init-webext.js            | 21 ++++++++++++++++++++-
 package-lock.json         |  2 +-
 package.json              |  2 +-
 snowflake.js              |  7 +++----
 ui.js                     |  6 +-----
 webext/manifest_base.json |  8 +++++---
 8 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/config.js b/config.js
index 8f42176..3eaaf63 100644
--- a/config.js
+++ b/config.js
@@ -29,8 +29,8 @@ Config.prototype.slowestBrokerPollInterval = 6 * 60 * 60.0 * 1000; //1 poll ever
 Config.prototype.pollAdjustment = 100.0 * 1000;
 Config.prototype.fastBrokerPollInterval = 30 * 1000; //1 poll every 30 seconds
 
-// Recheck our NAT type once every 2 days
-Config.prototype.natCheckInterval = 2 * 24 * 60 * 60 * 1000;
+// Recheck our NAT type once every 2 days (note: this value is given in minutes)
+Config.prototype.natCheckInterval = 2 * 24 * 60;
 
 // Timeout after sending answer before datachannel is opened
 Config.prototype.datachannelTimeout = 20 * 1000;
diff --git a/init-badge.js b/init-badge.js
index a4305a2..9f9d46c 100644
--- a/init-badge.js
+++ b/init-badge.js
@@ -149,6 +149,25 @@ var
   /** @type {() => void} */
   tryProbe;
 
+// Listeners for alarms
+browser.alarms.onAlarm.addListener((alarmInfo) => {
+  switch(alarmInfo.name){
+    case "poll":
+      snowflake.beginWebRTC();
+      break;
+    case "NAT":
+      ui.checkNAT();
+      break;
+    case "stats":
+      ui.stats.unshift(0);
+      ui.stats.splice(24);
+      ui.postActive();
+      break;
+    default:
+      console.log("Unknown alarm name: ", alarmInfo.name);
+  }
+});
+
 (function() {
 
   snowflake = null;
diff --git a/init-webext.js b/init-webext.js
index a1126eb..9e0bfbd 100644
--- a/init-webext.js
+++ b/init-webext.js
@@ -65,7 +65,7 @@ class WebExtUI extends UI {
   initNATType() {
     this.natType = "unknown";
     this.checkNAT();
-    setInterval(() => {this.checkNAT();}, config.natCheckInterval);
+    browser.alarms.create("NAT", { "periodInMinutes": config.natCheckInterval } );
   }
 
   tryProbe() {
@@ -235,6 +235,25 @@ var
   /** @type {boolean} */
   silenceNotifications;
 
+// Listeners for alarms
+browser.alarms.onAlarm.addListener((alarmInfo) => {
+  switch(alarmInfo.name){
+    case "poll":
+      snowflake.beginWebRTC();
+      break;
+    case "NAT":
+      ui.checkNAT();
+      break;
+    case "stats":
+      ui.stats.unshift(0);
+      ui.stats.splice(24);
+      ui.postActive();
+      break;
+    default:
+      console.log("Unknown alarm name: ", alarmInfo.name);
+  }
+});
+
 (function () {
 
   silenceNotifications = false;
diff --git a/package-lock.json b/package-lock.json
index e90c7f0..fb4aa4f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "snowflake-pt",
-  "version": "0.6.0",
+  "version": "0.6.3",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
diff --git a/package.json b/package.json
index fda82f9..1cfe3d9 100644
--- a/package.json
+++ b/package.json
@@ -33,4 +33,4 @@
     "ws": "^5.2.3",
     "xmlhttprequest": "^1.8.0"
   }
-}
\ No newline at end of file
+}
diff --git a/snowflake.js b/snowflake.js
index abb4c11..ed11c32 100644
--- a/snowflake.js
+++ b/snowflake.js
@@ -53,10 +53,9 @@ class Snowflake {
    * process. `pollBroker` automatically arranges signalling.
    */
   beginWebRTC() {
+    console.log("polling broker..");
     this.pollBroker();
-    this.pollTimeoutId = setTimeout((() => {
-      this.beginWebRTC();
-    }), this.pollInterval);
+    browser.alarms.create("poll", {"when": Date.now()+this.pollInterval } );
   }
 
   /**
@@ -205,7 +204,7 @@ class Snowflake {
   /** Stop all proxypairs. */
   disable() {
     log('Disabling Snowflake.');
-    clearTimeout(this.pollTimeoutId);
+    browser.alarms.clear("pollTimeout");
     while (this.proxyPairs.length > 0) {
       this.proxyPairs.pop().close();
     }
diff --git a/ui.js b/ui.js
index 1af9b38..0f7e67e 100644
--- a/ui.js
+++ b/ui.js
@@ -10,11 +10,7 @@ class UI {
 
   initStats() {
     this.stats = [0];
-    setInterval((() => {
-      this.stats.unshift(0);
-      this.stats.splice(24);
-      this.postActive();
-    }), 60 * 60 * 1000);
+    browser.alarms.create("stats", { "periodInMinutes" : 60 });
   }
 
   setStatus() {}
diff --git a/webext/manifest_base.json b/webext/manifest_base.json
index d9080fb..2b7d258 100644
--- a/webext/manifest_base.json
+++ b/webext/manifest_base.json
@@ -11,7 +11,8 @@
   "background": {
     "scripts": [
       "snowflake.js"
-    ]
+    ],
+    "persistent": false
   },
   "action": {
     "default_icon": {
@@ -22,6 +23,7 @@
     "default_popup": "embed.html"
   },
   "permissions": [
-    "storage"
+    "storage",
+    "alarms"
   ]
-}
\ No newline at end of file
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits