Skip to content

Commit

Permalink
Use CustomEvent instead of postMessage
Browse files Browse the repository at this point in the history
to avoid conflicts with websites
  • Loading branch information
brendan-duncan committed May 26, 2024
1 parent 679ca1a commit 2e451d3
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion extensions/chrome/content_script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/chrome/webgpu_inspector.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion extensions/chrome/webgpu_inspector_worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/firefox/content_script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/firefox/webgpu_inspector.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion extensions/firefox/webgpu_inspector_worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions src/extension/content_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const port = new MessagePort("webgpu-inspector-page", 0, (message) => {
}

if (action === PanelActions.RequestTexture || action === PanelActions.CompileShader || action === PanelActions.RevertShader) {
window.postMessage(message, "*");
window.dispatchEvent(new CustomEvent("__WebGPUInspector", { detail: message }))
return;
}

Expand All @@ -36,8 +36,9 @@ const port = new MessagePort("webgpu-inspector-page", 0, (message) => {
inspectMessage = messageString;
} else {
sessionStorage.setItem(webgpuInspectorCaptureFrameKey, messageString);
window.postMessage({ __webgpuInspector: true, __webgpuInspectorPanel: true, action: PanelActions.Capture,
data: messageString }, "*");
const message = { __webgpuInspector: true, __webgpuInspectorPanel: true, action: PanelActions.Capture,
data: messageString };
window.dispatchEvent(new CustomEvent("__WebGPUInspector", { detail: message }))
}
}

Expand All @@ -59,11 +60,8 @@ window.addEventListener('pageshow', (event) => {
});

// Listen for messages from the page
window.addEventListener('message', (event) => {
if (event.source !== window) {
return;
}
const message = event.data;
window.addEventListener("__WebGPUInspector", (event) => {
const message = event.detail;
if (typeof message !== 'object' || message === null) {
return;
}
Expand Down Expand Up @@ -119,7 +117,6 @@ if (recordMessage) {
removeUnusedResources: 1,
messageRecording: 1
});

}

port.postMessage({action: "PageLoaded"});
25 changes: 17 additions & 8 deletions src/webgpu_inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export let webgpuInspector = null;
const _document = self.document;
const _sessionStorage = self.sessionStorage;
const _postMessage = self.postMessage;
const _dispatchEvent = self.dispatchEvent;

const webgpuInspectorCaptureFrameKey = "WEBGPU_INSPECTOR_CAPTURE_FRAME";

Expand Down Expand Up @@ -181,18 +182,16 @@ export let webgpuInspector = null;
};

// Listen for messages from the content-script.
_self.addEventListener("message", (event) => {
if (_window && event.source !== _window) {
return;
}
const message = event.data;

function eventCallback(event) {
const message = event.detail || event.data;
if (typeof message !== "object" || !message.__webgpuInspector) {
return;
}

if (message.action === Actions.DeltaTime) {
if (message.__webgpuInspectorWorker) {
this._updateFrameRate(message.deltaTime);
self._updateFrameRate(message.deltaTime);
}
} else if (message.action === PanelActions.RequestTexture) {
const textureId = message.id;
Expand All @@ -212,7 +211,13 @@ export let webgpuInspector = null;
self._captureData = message.data;
}
}
});
}

if (!_window) {
_self.addEventListener("message", eventCallback);
} else {
_self.addEventListener("__WebGPUInspector", eventCallback);
}
}

captureWorker(canvas) {
Expand All @@ -231,7 +236,11 @@ export let webgpuInspector = null;
message.__webgpuInspector = true;
message.__webgpuInspectorPage = true;
message.__webgpuInspectorWorker = !_window;
_postMessage(message);
if (!_window) {
_postMessage(message);
} else {
_dispatchEvent(new CustomEvent("__WebGPUInspector", { detail: message }));
}
}

_updateCanvasAttachment(attachment) {
Expand Down
10 changes: 5 additions & 5 deletions src/webgpu_inspector_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ export function webgpuInspectorWorker(worker) {

worker.addEventListener("message", (event) => {
if (event.data.__webgpuInspector) {
window.postMessage(event.data, "*");
window.dispatchEvent(new CustomEvent("__WebGPUInspector", { detail: event.data }));
}
});

window.addEventListener("message", (event) => {
window.addEventListener("__WebGPUInspector", (event) => {
// Forward messages from the page to the worker, if the worker hasn't been terminated,
// the message is from the inspector, and the message is not from the worker.
if (worker.__webgpuInspector && event.data.__webgpuInspector &&
!event.data.__webgpuInspectorPage) {
worker.postMessage(event.data);
if (worker.__webgpuInspector && event.detail.__webgpuInspector &&
!event.detail.__webgpuInspectorPage) {
worker.postMessage(event.detail);
}
});
}
1 change: 1 addition & 0 deletions test/webgpu_worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { webgpuInspector } from 'https://cdn.jsdelivr.net/gh/brendan-duncan/[email protected]/src/webgpu_inspector.js';
//import { webgpuInspector } from '../src/webgpu_inspector.js';

self.addEventListener('message', (ev) => {
switch (ev.data.type) {
Expand Down
Loading

0 comments on commit 2e451d3

Please sign in to comment.