From 66e74efb0cb412377affb14bc9ff7d9206a1654a Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 5 Jul 2023 09:34:56 +0300 Subject: [PATCH] Fix persisting Shared Worker when logging out with multiple tabs --- www/common/cryptpad-common.js | 3 +++ www/common/outer/noworker.js | 4 ++++ www/common/outer/sharedworker.js | 5 +++++ www/common/sframe-common-outer.js | 6 +++++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 2947ee8a4..b2c98faf9 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -387,6 +387,9 @@ define([ cb(); }); }; + common.stopWorker = function () { + postMessage('STOPWORKER', null, function () {}); + }; common.logoutFromAll = function (cb) { var token = Math.floor(Math.random()*Number.MAX_SAFE_INTEGER); localStorage.setItem(Constants.tokenKey, token); diff --git a/www/common/outer/noworker.js b/www/common/outer/noworker.js index a4a44d1b9..5a3025958 100644 --- a/www/common/outer/noworker.js +++ b/www/common/outer/noworker.js @@ -19,6 +19,7 @@ define([ if (q === 'CONNECT') { return; } if (q === 'JOIN_PAD') { return; } if (q === 'SEND_PAD_MSG') { return; } + if (q === 'STOPWORKER') { return; } chan.on(q, function (data, cb) { try { Rpc.queries[q](clientId, data, cb); @@ -29,6 +30,9 @@ define([ } }); }); + chan.on('STOPWORKER', function (data, cb) { + cb(); + }); chan.on('CONNECT', function (cfg, cb) { // load Store here, with cfg, and pass a "query" (chan.query) // cId is a clientId used in ServiceWorker or SharedWorker diff --git a/www/common/outer/sharedworker.js b/www/common/outer/sharedworker.js index e256f065f..2e3545a71 100644 --- a/www/common/outer/sharedworker.js +++ b/www/common/outer/sharedworker.js @@ -53,6 +53,7 @@ var init = function (client, cb) { if (q === 'CONNECT') { return; } if (q === 'JOIN_PAD') { return; } if (q === 'SEND_PAD_MSG') { return; } + if (q === 'STOPWORKER') { return; } chan.on(q, function (data, cb) { try { Rpc.queries[q](clientId, data, cb); @@ -71,6 +72,10 @@ var init = function (client, cb) { } }); }); + chan.on('STOPWORKER', function (data, cb) { + console.error('ICI'); + self.close(); + }); chan.on('CONNECT', function (cfg, cb) { debug('SharedW connecting to store...'); /* diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 23e776218..1231bc133 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -1319,11 +1319,15 @@ define([ }); sframeChan.on('Q_LOGOUT', function (data, cb) { + Cryptpad.stopWorker(); Utils.LocalStore.logout(cb); }); sframeChan.on('Q_LOGOUT_EVERYWHERE', function (data, cb) { - Cryptpad.logoutFromAll(Utils.Util.bake(Utils.LocalStore.logout, cb)); + Cryptpad.logoutFromAll(Utils.Util.bake(Utils.LocalStore.logout, function () { + Cryptpad.stopWorker(); + cb(); + })); }); sframeChan.on('EV_NOTIFY', function (data) {