managed-empty-main.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { app, BrowserWindow, ipcMain, protocol, webContents } from "electron";
  2. import assert from "node:assert/strict";
  3. import { readFile, writeFile } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import {
  6. installPluginSandboxHost,
  7. PLUGIN_SANDBOX_PRIVILEGED_SCHEME,
  8. } from "../../src/main/pluginSandbox/index.js";
  9. const [root, profile] = process.argv.slice(2) as [string, string];
  10. app.setPath("userData", profile);
  11. protocol.registerSchemesAsPrivileged([PLUGIN_SANDBOX_PRIVILEGED_SCHEME]);
  12. app
  13. .whenReady()
  14. .then(async () => {
  15. const { appIdentity } = JSON.parse(
  16. await readFile(join(root, "managed-storage-identity.json"), "utf8"),
  17. );
  18. const host = installPluginSandboxHost({
  19. rendererDir: root,
  20. aliasDir: root,
  21. preloadPath: join(root, "guest.cjs"),
  22. });
  23. const win = new BrowserWindow({
  24. show: false,
  25. webPreferences: {
  26. preload: join(root, "preload.cjs"),
  27. webviewTag: true,
  28. backgroundThrottling: false,
  29. },
  30. });
  31. const handle = host.registerFromHost({
  32. instance: {
  33. runtimeId: "storage-probe",
  34. generation: 1,
  35. token: crypto.randomUUID(),
  36. appIdentity,
  37. },
  38. ownerWebContentsId: win.webContents.id,
  39. workspacePath: "/fixture",
  40. sessionId: "fixture",
  41. pluginId: "fixture",
  42. serverName: "fixture",
  43. scopeId: "fixture",
  44. html: await readFile(join(root, "widget.html"), "utf8"),
  45. });
  46. win.webContents.on("will-attach-webview", (event, webPreferences, params) => {
  47. if (
  48. !host.configureGuest({ webPreferences, params, ownerWebContentsId: win.webContents.id }).ok
  49. )
  50. event.preventDefault();
  51. });
  52. win.webContents.on("did-attach-webview", (_event, guest) =>
  53. host.attachGuest({ guest, hostWebContents: win.webContents, sandboxId: handle.sandboxId }),
  54. );
  55. ipcMain.handle("fixture-config", () => null);
  56. ipcMain.handle("fixture-handle", () => handle);
  57. await win.loadFile(join(root, "host.html"));
  58. await win.webContents.executeJavaScript("window.harnessReady");
  59. const guest = webContents.getAllWebContents().find((w) => w.getType() === "webview")!;
  60. const frame = guest.mainFrame.frames.find((f) => f.url.includes("/instance/"))!;
  61. await frame.executeJavaScript("window.fixtureReady");
  62. const empty = await frame.executeJavaScript(
  63. "indexedDB.databases().then(dbs=>({local:localStorage.length,databases:dbs.length,widget:window.readWidget()}))",
  64. );
  65. assert.deepEqual(empty, { local: 0, databases: 0, widget: null });
  66. await writeFile(join(root, "managed-storage-empty.json"), JSON.stringify(empty));
  67. process.stdout.write(
  68. "PASS new Electron process confirms multi-window clearing left LS/IDB empty and widgetState absent\n",
  69. );
  70. win.destroy();
  71. app.exit(0);
  72. })
  73. .catch((error) => {
  74. process.stderr.write(String(error.stack ?? error));
  75. app.exit(1);
  76. });