From 21c64e827bc96dfbf1f314ef8fdc63671f8a9410 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 9 Apr 2024 15:05:48 +0100 Subject: [PATCH] Add closeApp message Sending a 'closeApp' message allows an app to close a target app, if it has permission to do so. Currently, permission is granted if the requesting app is the parent of the target app, or has godmode set. --- src/IPC.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/IPC.js b/src/IPC.js index 92b0855a0..54c53e4fc 100644 --- a/src/IPC.js +++ b/src/IPC.js @@ -1100,6 +1100,43 @@ window.addEventListener('message', async (event) => { contents, }, targetAppOrigin); } + //-------------------------------------------------------- + // closeApp + //-------------------------------------------------------- + else if (event.data.msg === 'closeApp') { + const { appInstanceID, targetAppInstanceID } = event.data; + + const target_window = window_for_app_instance(targetAppInstanceID); + if (!target_window) { + console.warn(`Failed to close non-existent app ${targetAppInstanceID}`); + return; + } + + // Check permissions + const allowed = (() => { + // Parents can close their children + if (target_window.dataset['parent_instance_id']) { + console.log(`⚠️ Allowing app ${appInstanceID} to close child app ${targetAppInstanceID}`); + return true; + } + + // God-mode apps can close anything + const app_info = await get_apps(app_name); + if (app_info.godmode === 1) { + console.log(`⚠️ Allowing GODMODE app ${appInstanceID} to close app ${targetAppInstanceID}`); + return true; + } + + // TODO: What other situations should we allow? + return false; + })(); + + if (allowed) { + $(target_window).close(); + } else { + console.warn(`⚠️ App ${appInstanceID} is not permitted to close app ${targetAppInstanceID}`); + } + } //-------------------------------------------------------- // exit