diff --git a/CHANGELOG.md b/CHANGELOG.md
index 065327c33..cd7d61048 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,7 @@
- Pasting a security key file now explains how to use it instead of failing to decode it
- Plugins can add a panel that sits beside a host tab, like a terminal. You can open, resize and move it
- The desktop app can link to a server that requires a client certificate (mutual TLS). It uses a PFX or P12 file for sync and the certificate in your system store for the app window
+- Plugins can show a web page in a native view in the desktop app, for sites that do not load in an iframe
### Changed
diff --git a/electron/embedded-view.cjs b/electron/embedded-view.cjs
new file mode 100644
index 000000000..c6967fa4d
--- /dev/null
+++ b/electron/embedded-view.cjs
@@ -0,0 +1,165 @@
+const { isolatedUrl } = require("./isolated-window.cjs");
+
+function cleanBounds(bounds) {
+ const value = (n) => Math.max(0, Math.round(Number(n) || 0));
+ return {
+ x: value(bounds?.x),
+ y: value(bounds?.y),
+ width: value(bounds?.width),
+ height: value(bounds?.height),
+ };
+}
+
+/**
+ * Native web views drawn inside the main window, for pages that do not work
+ * in a sandboxed iframe. Each one is a WebContentsView in its own
+ * non-persistent session, with the same limits as an isolated window: no
+ * permissions, no downloads, http(s) and ws(s) only. The renderer owns the
+ * placement and sends the bounds of the element the view should cover.
+ */
+function createEmbeddedViews({ WebContentsView, session, getMainWindow }) {
+ const views = new Map();
+
+ function destroy(id) {
+ const entry = views.get(id);
+ if (!entry) return;
+ views.delete(id);
+ const main = getMainWindow();
+ if (main && !main.isDestroyed()) {
+ try {
+ main.contentView.removeChildView(entry.view);
+ } catch {
+ /* already detached */
+ }
+ }
+ if (!entry.view.webContents.isDestroyed()) entry.view.webContents.close();
+ void Promise.allSettled([
+ entry.session.clearStorageData(),
+ entry.session.clearCache(),
+ entry.session.closeAllConnections(),
+ ]);
+ }
+
+ function destroyAll() {
+ for (const id of [...views.keys()]) destroy(id);
+ }
+
+ function open(options = {}) {
+ const main = getMainWindow();
+ if (!main) throw new Error("No window to attach a view to");
+ const id = String(options.id || "");
+ if (!id) throw new Error("A view needs an id");
+ const url = isolatedUrl(options.url);
+ const existing = views.get(id);
+ if (existing) {
+ if (
+ existing.url === url.href &&
+ existing.ignoreCert === !!options.ignoreCert
+ ) {
+ setBounds(id, options.bounds);
+ setVisible(id, options.visible !== false);
+ return { success: true };
+ }
+ destroy(id);
+ }
+
+ const isolated = session.fromPartition(
+ `embedded-view-${id}-${Date.now()}`,
+ {
+ cache: false,
+ },
+ );
+ isolated.setPermissionRequestHandler((_c, _p, callback) => callback(false));
+ isolated.setPermissionCheckHandler(() => false);
+ isolated.on("will-download", (event) => event.preventDefault());
+ isolated.webRequest.onBeforeRequest((details, callback) => {
+ const protocol = new URL(details.url).protocol;
+ callback({
+ cancel: !["http:", "https:", "ws:", "wss:"].includes(protocol),
+ });
+ });
+
+ const view = new WebContentsView({
+ webPreferences: {
+ session: isolated,
+ sandbox: true,
+ contextIsolation: true,
+ nodeIntegration: false,
+ webviewTag: false,
+ webSecurity: true,
+ },
+ });
+ const canNavigate = (target) => {
+ try {
+ return target === "about:blank" || !!isolatedUrl(target);
+ } catch {
+ return false;
+ }
+ };
+ for (const name of ["will-navigate", "will-redirect"]) {
+ view.webContents.on(name, (event, target) => {
+ if (!canNavigate(target)) event.preventDefault();
+ });
+ }
+ view.webContents.setWindowOpenHandler(({ url: target }) =>
+ canNavigate(target)
+ ? {
+ action: "allow",
+ overrideBrowserWindowOptions: {
+ webPreferences: { session: isolated, sandbox: true },
+ },
+ }
+ : { action: "deny" },
+ );
+
+ views.set(id, {
+ view,
+ session: isolated,
+ url: url.href,
+ origin: url.origin,
+ ignoreCert: !!options.ignoreCert,
+ });
+ main.contentView.addChildView(view);
+ setBounds(id, options.bounds);
+ setVisible(id, options.visible !== false);
+ view.webContents.loadURL(url.href).catch(() => {});
+ return { success: true };
+ }
+
+ function setBounds(id, bounds) {
+ const entry = views.get(id);
+ if (entry) entry.view.setBounds(cleanBounds(bounds));
+ }
+
+ function setVisible(id, visible) {
+ const entry = views.get(id);
+ if (entry) entry.view.setVisible(!!visible);
+ }
+
+ function handleCertificateError(event, contents, url, callback) {
+ for (const entry of views.values()) {
+ if (entry.view.webContents !== contents) continue;
+ event.preventDefault();
+ let allowed = false;
+ try {
+ allowed = entry.ignoreCert && new URL(url).origin === entry.origin;
+ } catch {
+ /* malformed URL is refused */
+ }
+ callback(allowed);
+ return true;
+ }
+ return false;
+ }
+
+ return {
+ open,
+ setBounds,
+ setVisible,
+ destroy,
+ destroyAll,
+ handleCertificateError,
+ };
+}
+
+module.exports = { createEmbeddedViews, cleanBounds };
diff --git a/electron/main.cjs b/electron/main.cjs
index 80fb7930a..34c744719 100644
--- a/electron/main.cjs
+++ b/electron/main.cjs
@@ -1,6 +1,7 @@
const {
app,
BrowserWindow,
+ WebContentsView,
shell,
ipcMain,
dialog,
@@ -497,12 +498,38 @@ app.commandLine.appendSwitch("--enable-features=NetworkService");
let mainWindow = null;
const { createIsolatedWindows } = require("./isolated-window.cjs");
+const { createEmbeddedViews } = require("./embedded-view.cjs");
const isolatedWindows = createIsolatedWindows({
BrowserWindow,
session,
getMainWindow: () => mainWindow,
});
+const embeddedViews = createEmbeddedViews({
+ WebContentsView,
+ session,
+ getMainWindow: () => mainWindow,
+});
+
+// Only the app's own page may place native views, never a frame inside it.
+function fromMainFrame(event) {
+ return !!mainWindow && event.senderFrame === mainWindow.webContents.mainFrame;
+}
+
+ipcMain.handle("embedded-view-open", (event, options) => {
+ if (!fromMainFrame(event)) throw new Error("Not allowed");
+ return embeddedViews.open(options);
+});
+ipcMain.handle("embedded-view-bounds", (event, id, bounds) => {
+ if (fromMainFrame(event)) embeddedViews.setBounds(String(id), bounds);
+});
+ipcMain.handle("embedded-view-visible", (event, id, visible) => {
+ if (fromMainFrame(event)) embeddedViews.setVisible(String(id), visible);
+});
+ipcMain.handle("embedded-view-close", (event, id) => {
+ if (fromMainFrame(event)) embeddedViews.destroy(String(id));
+});
+
// Requests from the embedded backend over the fork IPC channel (the other
// direction from the "shutdown" message main already sends it). A plugin's
// ctx.desktop.openIsolatedWindow is the one caller today.
@@ -731,6 +758,10 @@ app.on(
isolatedWindows.handleCertificateError(event, _webContents, url, callback)
)
return;
+ if (
+ embeddedViews.handleCertificateError(event, _webContents, url, callback)
+ )
+ return;
if (isWebEndpointCertificateAllowed(url)) {
event.preventDefault();
logToFile("Allowed invalid certificate for configured web endpoint", {
@@ -1508,7 +1539,14 @@ function createWindow() {
}
});
+ mainWindow.webContents.on("did-start-navigation", (details) => {
+ if (details.isMainFrame && !details.isSameDocument) {
+ embeddedViews.destroyAll();
+ }
+ });
+
mainWindow.on("closed", () => {
+ embeddedViews.destroyAll();
mainWindow = null;
});
diff --git a/electron/preload.js b/electron/preload.js
index afef50c22..8bc56bc56 100644
--- a/electron/preload.js
+++ b/electron/preload.js
@@ -80,6 +80,16 @@ contextBridge.exposeInMainWorld("electronAPI", {
ipcRenderer.invoke("start-drag-to-desktop", dragData),
cleanupTempFile: (tempId) => ipcRenderer.invoke("cleanup-temp-file", tempId),
+ // Native web views placed over an element of the page (WebContentsView).
+ embeddedView: {
+ open: (options) => ipcRenderer.invoke("embedded-view-open", options),
+ setBounds: (id, bounds) =>
+ ipcRenderer.invoke("embedded-view-bounds", id, bounds),
+ setVisible: (id, visible) =>
+ ipcRenderer.invoke("embedded-view-visible", id, visible),
+ close: (id) => ipcRenderer.invoke("embedded-view-close", id),
+ },
+
invoke: invokeAllowed,
});
diff --git a/packages/plugin-sdk/CHANGELOG.md b/packages/plugin-sdk/CHANGELOG.md
index 2bd566464..4c76081be 100644
--- a/packages/plugin-sdk/CHANGELOG.md
+++ b/packages/plugin-sdk/CHANGELOG.md
@@ -7,6 +7,7 @@ Plugin API 1.9.0.
### Added
- `app.registerCompanion` and `app.toggleCompanion`, so a plugin can add a panel beside any host tab. The user opens, resizes and moves it, and the tab keeps running
+- `NativeView` in `@termix-ssh/plugin-sdk/ui`, which shows a web page in a native view in the desktop app, for sites that do not work in an iframe
## 1.0.16
diff --git a/packages/plugin-sdk/PLUGIN-GUIDE.md b/packages/plugin-sdk/PLUGIN-GUIDE.md
index 5553f7d24..9341f28d7 100644
--- a/packages/plugin-sdk/PLUGIN-GUIDE.md
+++ b/packages/plugin-sdk/PLUGIN-GUIDE.md
@@ -631,6 +631,9 @@ Build UI from `@termix-ssh/plugin-sdk/ui` so it matches Termix and follows the t
exports with props is `ui.api.txt` in the Termix repo
(https://github.com/Termix-SSH/Termix/blob/main/packages/plugin-sdk/ui.api.txt).
+- Web pages that break in an iframe: `NativeView` (props `url`, `ignoreCert`, `hidden`, `fallback`) shows the
+ page in a native view in the desktop app. It fills its element, hides while a dialog or menu is open and renders
+ `fallback` in a browser.
- Square: panels, cards and containers have no rounded corners. Small controls may stay rounded.
- Flat: borders, not shadows. Use theme tokens like `border-border`, `bg-background`,
`text-muted-foreground`, `text-destructive`. One accent, `text-accent-brand`. Don't add colors.
diff --git a/packages/plugin-sdk/ui.api.txt b/packages/plugin-sdk/ui.api.txt
index 62fec6f18..a2e578d7c 100644
--- a/packages/plugin-sdk/ui.api.txt
+++ b/packages/plugin-sdk/ui.api.txt
@@ -196,6 +196,8 @@ ManagerSearch (value): ({ value, onChange, placeholder, count, extra, }: { value
Meter (value): ({ percent, className, }: { percent: number; className?: string; }) => Element
MetricCard (value): ({ title, icon, action, children, bodyClassName, scroll, scrollMax, }: { title: string; icon: ReactNode; action?: ReactNode; children: ReactNode; bodyClassName?: string; scroll?: boolean; scrollMax?: number; }) => Element
MiniStat (value): ({ caption, value, valueClassName, className, }: { caption: ReactNode; value: ReactNode; valueClassName?: string; className?: string; }) => Element
+NativeView (value): ({ url, ignoreCert, hidden, fallback, className, style, }: NativeViewProps) => Element
+NativeViewProps (type): NativeViewProps
NumberField (value): ({ label, value, onChange, placeholder, hint, error, min, max, disabled, className, }: { label: string; value: number; onChange: (value: number) => void; placeholder?: string; hint?: ReactNode; error?: string; min?: number; max?: number; disabled?: boolean; className?: string; }) => Element
OPEN_PLUGINS_EVENT (value): "termix:open-plugins"
OpenTabRecord (type): OpenTabRecord
diff --git a/scripts/check-sdk-ui-exports.cjs b/scripts/check-sdk-ui-exports.cjs
index bd2a540cb..bf020c3ef 100644
--- a/scripts/check-sdk-ui-exports.cjs
+++ b/scripts/check-sdk-ui-exports.cjs
@@ -217,6 +217,8 @@ const ALLOWED = [
"Meter",
"MetricCard",
"MiniStat",
+ "NativeView",
+ "NativeViewProps",
"NumberField",
"OPEN_PLUGINS_EVENT",
"OpenTabRecord",
diff --git a/src/backend/tests/electron/embedded-view.test.ts b/src/backend/tests/electron/embedded-view.test.ts
new file mode 100644
index 000000000..69bf03210
--- /dev/null
+++ b/src/backend/tests/electron/embedded-view.test.ts
@@ -0,0 +1,164 @@
+import { createRequire } from "node:module";
+import { EventEmitter } from "node:events";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const { createEmbeddedViews, cleanBounds } = createRequire(import.meta.url)(
+ "../../../../electron/embedded-view.cjs",
+);
+
+const views: FakeView[] = [];
+
+class FakeView {
+ webContents = Object.assign(new EventEmitter(), {
+ setWindowOpenHandler: vi.fn(),
+ loadURL: vi.fn().mockResolvedValue(undefined),
+ isDestroyed: () => false,
+ close: vi.fn(),
+ });
+ setBounds = vi.fn();
+ setVisible = vi.fn();
+ constructor(public options: { webPreferences: Record