Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Generisize

Manav Rathi92a9b348363c
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts
index 24043fe10d..b74b19ec6c 100644
--- a/desktop/src/main/ipc.ts
+++ b/desktop/src/main/ipc.ts
@@ -16,6 +16,7 @@ import type {
FFmpegCommand,
FolderWatch,
PendingUploads,
+ UtilityProcessType,
ZipItem,
} from "../types/ipc";
import { logToDisk } from "./log";
@@ -47,7 +48,6 @@ import {
} from "./services/fs";
import { convertToJPEG, generateImageThumbnail } from "./services/image";
import { logout } from "./services/logout";
-import { createMLUtilityProcess } from "./services/ml";
import {
lastShownChangelogVersion,
masterKeyB64,
@@ -70,6 +70,7 @@ import {
watchUpdateIgnoredFiles,
watchUpdateSyncedFiles,
} from "./services/watch";
+import { triggerCreateUtilityProcess } from "./services/workers";
/**
* Listen for IPC events sent/invoked by the renderer process, and route them to
@@ -233,10 +234,10 @@ export const attachIPCHandlers = () => {
* the main window to do their thing.
*/
export const attachMainWindowIPCHandlers = (mainWindow: BrowserWindow) => {
- // - ML
+ // - Utility processes
- ipcMain.on("createMLUtilityProcess", () =>
- createMLUtilityProcess(mainWindow),
+ ipcMain.on("triggerCreateUtilityProcess", (_, type: UtilityProcessType) =>
+ triggerCreateUtilityProcess(type, mainWindow),
);
};
diff --git a/desktop/src/main/services/ml.ts b/desktop/src/main/services/workers.ts
similarity index 85%
rename from desktop/src/main/services/ml.ts
rename to desktop/src/main/services/workers.ts
index 263a6cfe63..cb6923bc62 100644
--- a/desktop/src/main/services/ml.ts
+++ b/desktop/src/main/services/workers.ts
@@ -1,5 +1,6 @@
/**
- * @file ML related functionality. This code runs in the main process.
+ * @file This main process code and interface for dealing with the various
+ * utility processes that we create.
*/
import {
@@ -9,6 +10,7 @@ import {
} from "electron";
import { app, utilityProcess } from "electron/main";
import path from "node:path";
+import type { UtilityProcessType } from "../../types/ipc";
import log, { processUtilityProcessLogMessage } from "../log";
/** The active ML utility process, if any. */
@@ -36,7 +38,7 @@ let _child: UtilityProcess | undefined;
* does not forward events to the renderer, causing the UI to jitter.
*
* The solution for this is to spawn an Electron UtilityProcess, which we can
- * think of a regular Node.js child process. This frees up the Node.js main
+ * think of a regular Node.js child process. This frees up the Node.js main
* process, and would remove the jitter.
* https://www.electronjs.org/docs/latest/tutorial/process-model
*
@@ -70,7 +72,19 @@ let _child: UtilityProcess | undefined;
* The RPC protocol is handled using comlink on both ends. The port itself needs
* to be relayed using `postMessage`.
*/
-export const createMLUtilityProcess = (window: BrowserWindow) => {
+export const triggerCreateUtilityProcess = (
+ type: UtilityProcessType,
+ window: BrowserWindow,
+) => {
+ switch (type) {
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+ case "ml":
+ triggerCreateMLUtilityProcess(window);
+ break;
+ }
+};
+
+export const triggerCreateMLUtilityProcess = (window: BrowserWindow) => {
if (_child) {
log.debug(() => "Terminating previous ML utility process");
_child.kill();
@@ -83,9 +97,7 @@ export const createMLUtilityProcess = (window: BrowserWindow) => {
const userDataPath = app.getPath("userData");
child.postMessage({ userDataPath }, [port1]);
- window.webContents.postMessage("createMLUtilityProcess/port", undefined, [
- port2,
- ]);
+ window.webContents.postMessage("utilityProcessPort/ml", undefined, [port2]);
handleMessagesFromUtilityProcess(child);
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index 60a9c6074b..20ace5ab8c 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -69,6 +69,7 @@ import type {
FFmpegCommand,
FolderWatch,
PendingUploads,
+ UtilityProcessType,
ZipItem,
} from "./types/ipc";
@@ -215,18 +216,19 @@ const ffmpegExec = (
outputFileExtension,
);
-// - ML
+// - Utility processes
-const createMLUtilityProcess = () => {
+const triggerCreateUtilityProcess = (type: UtilityProcessType) => {
+ const portEvent = `utilityProcessPort/${type}`;
const l = (event: IpcRendererEvent) => {
void windowLoaded.then(() => {
// "*"" is the origin to send to.
- window.postMessage("createMLUtilityProcess/port", "*", event.ports);
- ipcRenderer.off("createMLUtilityProcess/port", l);
+ window.postMessage(portEvent, "*", event.ports);
+ ipcRenderer.off(portEvent, l);
});
};
- ipcRenderer.on("createMLUtilityProcess/port", l);
- ipcRenderer.send("createMLUtilityProcess");
+ ipcRenderer.on(portEvent, l);
+ ipcRenderer.send("triggerCreateUtilityProcess", type);
};
// - Watch
@@ -393,7 +395,7 @@ contextBridge.exposeInMainWorld("electron", {
// - ML
- createMLUtilityProcess,
+ triggerCreateUtilityProcess,
// - Watch
diff --git a/desktop/src/types/ipc.ts b/desktop/src/types/ipc.ts
index 7701836217..db1d009f8b 100644
--- a/desktop/src/types/ipc.ts
+++ b/desktop/src/types/ipc.ts
@@ -5,6 +5,8 @@
* See [Note: types.ts <-> preload.ts <-> ipc.ts]
*/
+export type UtilityProcessType = "ml";
+
export interface AppUpdate {
autoUpdatable: boolean;
version: string;
diff --git a/web/packages/base/types/ipc.ts b/web/packages/base/types/ipc.ts
index d68a7d8434..25f5ce2126 100644
--- a/web/packages/base/types/ipc.ts
+++ b/web/packages/base/types/ipc.ts
@@ -373,21 +373,27 @@ export interface Electron {
// - ML
/**
- * Create a new ML utility process, terminating the older ones (if any).
+ * Trigger the creation of a new utility process of the given {@link type},
+ * terminating the older ones (if any).
*
* This creates a new Node.js utility process, and sets things up so that we
* can communicate directly with that utility process using a
- * {@link MessagePort} that gets posted using "createMLUtilityProcess/port".
+ * {@link MessagePort} that gets posted on the "utilityProcessPort/<type>"
+ * channel.
*
- * At the other end of that port will be an object that conforms to the
- * {@link ElectronMLWorker} interface.
+ * The code running in the utility process is determined by the specific
+ * value of {@link type}. Thus, att the other end of that port will be an
+ * object that conforms to:
+ *
+ * - {@link ElectronMLWorker} interface, when type is "ml".
*
* For more details about the IPC flow, see: [Note: ML IPC].
*
* Note: For simplicity of implementation, we assume that there is at most
- * one outstanding call to {@link createMLUtilityProcess}.
+ * one outstanding call to {@link triggerCreateUtilityProcess} for a given
+ * {@link type}.
*/
- createMLUtilityProcess: () => void;
+ triggerCreateUtilityProcess: (type: UtilityProcessType) => void;
// - Watch
@@ -590,10 +596,12 @@ export interface Electron {
clearPendingUploads: () => Promise<void>;
}
+export type UtilityProcessType = "ml";
+
/**
- * The shape of the object exposed by the Node.js ML worker process on the
- * message port that the web layer obtains by doing
- * {@link createMLUtilityProcess}.
+ * The shape of the object exposed by the Node.js utility process listening on
+ * the other side message port that the web layer obtains by doing
+ * {@link triggerCreateUtilityProcess} with type "ml".
*/
export interface ElectronMLWorker {
/**
diff --git a/web/packages/gallery/utils/native-worker.ts b/web/packages/gallery/utils/native-worker.ts
new file mode 100644
index 0000000000..e168ac4bbe
--- /dev/null
+++ b/web/packages/gallery/utils/native-worker.ts
@@ -0,0 +1,33 @@
+import type { Electron, UtilityProcessType } from "ente-base/types/ipc";
+
+/**
+ * Obtain a port from the Node.js layer that can be used to communicate with the
+ * native utility process of type {@link type}.
+ */
+export const createUtilityProcess = (
+ electron: Electron,
+ type: UtilityProcessType,
+): Promise<MessagePort> => {
+ // The main process will do its thing, and send back the port it created to
+ // us by sending an message on the "utilityProcessPort/<type>" channel via
+ // the postMessage API. This roundabout way is needed because MessagePorts
+ // cannot be transferred via the usual send/invoke pattern.
+
+ const portEvent = `utilityProcessPort/${type}`;
+
+ const port = new Promise<MessagePort>((resolve) => {
+ const l = ({ source, data, ports }: MessageEvent) => {
+ // The source check verifies that the message is coming from our own
+ // preload script. The data is the message that was posted.
+ if (source == window && data == portEvent) {
+ window.removeEventListener("message", l);
+ resolve(ports[0]!);
+ }
+ };
+ window.addEventListener("message", l);
+ });
+
+ electron.triggerCreateUtilityProcess(type);
+
+ return port;
+};
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 70a4f57b79..f97d9d91bd 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -8,9 +8,9 @@ import { blobCache } from "ente-base/blob-cache";
import { ensureElectron } from "ente-base/electron";
import log from "ente-base/log";
import { masterKeyFromSession } from "ente-base/session";
-import type { Electron } from "ente-base/types/ipc";
import { ComlinkWorker } from "ente-base/worker/comlink-worker";
import { type ProcessableUploadItem } from "ente-gallery/services/upload";
+import { createUtilityProcess } from "ente-gallery/utils/native-worker";
import type { EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { throttled } from "ente-utils/promise";
@@ -135,7 +135,7 @@ const createComlinkWorker = async () => {
const delegate = { workerDidUpdateStatus, workerDidUnawaitedIndex };
// Obtain a message port from the Electron layer.
- const messagePort = await createMLUtilityProcess(electron);
+ const messagePort = await createUtilityProcess(electron, "ml");
const cw = new ComlinkWorker<typeof MLWorker>(
"ML",
@@ -166,33 +166,6 @@ export const terminateMLWorker = async () => {
}
};
-/**
- * Obtain a port from the Node.js layer that can be used to communicate with the
- * ML utility process.
- */
-const createMLUtilityProcess = (electron: Electron): Promise<MessagePort> => {
- // The main process will do its thing, and send back the port it created to
- // us by sending an message on the "createMLUtilityProcess/port" channel via
- // the postMessage API. This roundabout way is needed because MessagePorts
- // cannot be transferred via the usual send/invoke pattern.
-
- const port = new Promise<MessagePort>((resolve) => {
- const l = ({ source, data, ports }: MessageEvent) => {
- // The source check verifies that the message is coming from our own
- // preload script. The data is the message that was posted.
- if (source == window && data == "createMLUtilityProcess/port") {
- window.removeEventListener("message", l);
- resolve(ports[0]!);
- }
- };
- window.addEventListener("message", l);
- });
-
- electron.createMLUtilityProcess();
-
- return port;
-};
-
/**
* Return true if the current client supports ML.
*