From dd5408585d346f5aa16f7224a6a5cae6505f7ee3 Mon Sep 17 00:00:00 2001 From: Jan Kiszka Date: Tue, 7 Jan 2025 07:54:49 +0100 Subject: [PATCH 1/3] Avoid redundant function call in waitFor The loop will only stop if f() returned true. Therefore, we can predict its return code and don't need to call it once more. Signed-off-by: Jan Kiszka --- background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/background.js b/background.js index 3f0e2b3..4bbb86c 100644 --- a/background.js +++ b/background.js @@ -44,7 +44,7 @@ async function sleep(ms) { } async function waitFor(f) { while (!f()) await sleep(200); - return f(); + return true; } /* From b32e89b13c047bc3af39f6a4eca734e7dec059aa Mon Sep 17 00:00:00 2001 From: Jan Kiszka Date: Tue, 7 Jan 2025 08:08:16 +0100 Subject: [PATCH 2/3] Establish timeout while waiting for native messaging host This also catches problems of the native messaging host. If it stalls, the browser will stall as well. 10 seconds should be enough time for it to react. Fail waitFor if that limit is reached and handle the failure at the caller sites. Signed-off-by: Jan Kiszka --- background.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/background.js b/background.js index 4bbb86c..4480174 100644 --- a/background.js +++ b/background.js @@ -43,7 +43,14 @@ async function sleep(ms) { return new Promise((r) => setTimeout(r, ms)); } async function waitFor(f) { - while (!f()) await sleep(200); + let retries = 50; + while (!f() && --retries > 0) { + await sleep(200); + } + if (retries <= 0) { + ssoLogError("timeout while waiting for native messaging host"); + return false; + } return true; } @@ -163,13 +170,15 @@ function notify_state_change() { async function load_accounts() { port_native.postMessage({ command: "getAccounts" }); - await waitFor(() => { + let success = await waitFor(() => { if (accounts.queried) { return true; } return false; }); - if (accounts.registered.length == 0) { + if (!success) { + return; + } else if (accounts.registered.length == 0) { ssoLog("no accounts registered"); return; } @@ -185,10 +194,12 @@ async function load_accounts() { command: "acquireTokenSilently", account: accounts.active, }); - await waitFor(() => { + let success = await waitFor(() => { return graph_api_token !== null; }); - if ("error" in graph_api_token) { + if (!success) { + return; + } else if ("error" in graph_api_token) { ssoLog( "couldn't aquire API token for avatar: " + graph_api_token.error, @@ -244,12 +255,15 @@ async function get_or_request_prt(ssoUrl) { account: accounts.active, ssoUrl: ssoUrl, }); - await waitFor(() => { + let success = await waitFor(() => { if (prt_sso_cookie.hasData) { return true; } return false; }); + if (!success) { + return { error: "timeout while waiting for native messaging host" }; + } prt_sso_cookie.hasData = false; const data = prt_sso_cookie.data; if ("error" in data) { From 5005cb98ee220e0710e26d303fc172718b05410a Mon Sep 17 00:00:00 2001 From: Jan Kiszka Date: Tue, 7 Jan 2025 08:41:59 +0100 Subject: [PATCH 3/3] Do not signal account availability if the broker is offline Signaling availability makes no sense if the broker is offline as we cannot request or update the PRT in that case. Signed-off-by: Jan Kiszka --- background.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/background.js b/background.js index 4480174..5207173 100644 --- a/background.js +++ b/background.js @@ -65,7 +65,7 @@ function is_operational() { * Update the UI according to the current state */ function update_ui() { - if (state_active && accounts.active) { + if (broker_online && state_active && accounts.active) { chrome.action.enable(); let imgdata = accounts.active.avatar_imgdata; if (imgdata) { @@ -373,10 +373,13 @@ async function on_message_native(response) { if (host_versions.native === null) { await load_accounts(); port_native.postMessage({ command: "getVersion" }); + } else { + update_ui(); } } else { ssoLog("lost connection to broker"); broker_online = false; + update_ui(); } } else { ssoLog("unknown command: " + response.command);