瀏覽代碼

Add `messageToApp` message

This lets apps communicate with each other, via Puter.

We probably want to limit this in some way, but for now, all apps are
allowed to send messages to any other apps.

The message is:
- `targetAppInstanceID`: The instance ID to send the message to
- `targetAppOrigin`: targetOrigin passed to postMessage(), in case we
  want to restrict which URL can receive the message
- `contents`: The message to send to the target
Sam Atkins 1 年之前
父節點
當前提交
c78927d5db
共有 1 個文件被更改,包括 26 次插入1 次删除
  1. 26 1
      src/IPC.js

+ 26 - 1
src/IPC.js

@@ -74,10 +74,14 @@ window.addEventListener('message', async (event) => {
         return;
     }
 
+    const iframe_for_app_instance = (instanceID) => {
+        return $(`.window[data-element_uuid="${instanceID}"]`).find('.window-app-iframe').get(0)
+    };
+
     const $el_parent_window = $(`.window[data-element_uuid="${event.data.appInstanceID}"]`);
     const parent_window_id = $el_parent_window.attr('data-id');
     const $el_parent_disable_mask = $el_parent_window.find('.window-disable-mask');
-    const target_iframe = $(`.window[data-element_uuid="${event.data.appInstanceID}"]`).find('.window-app-iframe').get(0);
+    const target_iframe = iframe_for_app_instance(event.data.appInstanceID);
     const msg_id = event.data.uuid;
     const app_name = $(target_iframe).attr('data-app');
     const app_uuid = $el_parent_window.attr('data-app_uuid');
@@ -1054,6 +1058,27 @@ window.addEventListener('message', async (event) => {
             }
         }
     }
+    //--------------------------------------------------------
+    // messageToApp
+    //--------------------------------------------------------
+    else if (event.data.msg === 'messageToApp') {
+        const { appInstanceID, targetAppInstanceID, targetAppOrigin, contents } = event.data;
+        // TODO: Determine if we should allow the message
+        // TODO: Track message traffic between apps
+
+        // pass on the message
+        const target_iframe = iframe_for_app_instance(targetAppInstanceID);
+        if (!target_iframe) {
+            console.error('Failed to send message to non-existent app', event);
+            return;
+        }
+        target_iframe.contentWindow.postMessage({
+            msg: 'messageToApp',
+            appInstanceID,
+            targetAppInstanceID,
+            contents,
+        }, targetAppOrigin);
+    }
 
     //--------------------------------------------------------
     // exit