Skip to content

armgit / ente

Public Imported from GitHub
0

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

init

Manav Rathi4ca40085c1e1
diff --git a/desktop/src/main/services/ml-util-test.ts b/desktop/src/main/services/ml-util-test.ts
index 18293c2a20..9ed2f3a46d 100644
--- a/desktop/src/main/services/ml-util-test.ts
+++ b/desktop/src/main/services/ml-util-test.ts
@@ -1,4 +1,6 @@
-//
+// See [Note: Using Electron APIs in UtilityProcess] about what we can and
+// cannot import.
+
import { ensure, wait } from "../utils/common";
/**
@@ -17,6 +19,11 @@ import { ensure, wait } from "../utils/common";
* transport the logs over to the main process.
*/
const log = {
+ /**
+ * Unlike the real {@link log.error}, this accepts only the first string
+ * argument, not the second optional error one.
+ */
+ errorString: (s: string) => mainProcess("log.errorString", s),
info: (...ms: unknown[]) => mainProcess("log.info", ms),
/**
* Unlike the real {@link log.debug}, this is (a) eagerly evaluated, and (b)
@@ -37,6 +44,8 @@ log.debugString(
);
process.parentPort.once("message", (e) => {
+ parseInitData(e.data);
+
const port = ensure(e.ports[0]);
port.on("message", (event) => {
void handleMessageFromRenderer(event.data).then((response) => {
@@ -46,6 +55,29 @@ process.parentPort.once("message", (e) => {
port.start();
});
+/**
+ * We cannot access Electron's {@link app} object within a utility process, so
+ * we pass the value of `app.getPath("userData")` during initialization, and it
+ * can be subsequently retrieved from here.
+ */
+let _userDataPath: string | undefined;
+
+/** Equivalent to app.getPath("userData") */
+const userDataPath = () => ensure(_userDataPath);
+
+const parseInitData = (data: unknown) => {
+ if (
+ data &&
+ typeof data == "object" &&
+ "userDataPath" in data &&
+ typeof data.userDataPath == "string"
+ ) {
+ _userDataPath = data.userDataPath;
+ } else {
+ log.errorString("Unparseable initialization data");
+ }
+};
+
/**
* Our hand-rolled IPC handler and router - the Node.js utility process end.
*
@@ -67,7 +99,7 @@ const handleMessageFromRenderer = async (m: unknown) => {
};
const foo = async (a: string) => {
- log.info("got message foo with argument", a);
+ log.info("got message foo with argument", a, userDataPath());
await wait(0);
return a.length;
};
diff --git a/desktop/src/main/services/ml.ts b/desktop/src/main/services/ml.ts
index 5de35b4b28..6e975d3910 100644
--- a/desktop/src/main/services/ml.ts
+++ b/desktop/src/main/services/ml.ts
@@ -7,7 +7,7 @@ import {
type BrowserWindow,
type UtilityProcess,
} from "electron";
-import { utilityProcess } from "electron/main";
+import { app, utilityProcess } from "electron/main";
import path from "node:path";
import log from "../log";
@@ -68,7 +68,8 @@ export const createMLWorker = (window: BrowserWindow) => {
const { port1, port2 } = new MessageChannelMain();
const child = utilityProcess.fork(path.join(__dirname, "ml-util-test.js"));
- child.postMessage(undefined, [port1]);
+ const userDataPath = app.getPath("userData");
+ child.postMessage({ userDataPath }, [port1]);
window.webContents.postMessage("createMLWorker/port", undefined, [port2]);
@@ -92,6 +93,9 @@ export const createMLWorker = (window: BrowserWindow) => {
*
* For the other cases,
*
+ * - Additional parameters to the utility process are passed alongwith the
+ * initial message where we provide it the message port.
+ *
* - When we need to communicate from the utility process to the main process,
* we use the `parentPort` in the utility process.
*/
@@ -101,6 +105,12 @@ const handleMLWorkerRequests = (child: UtilityProcess) => {
if (m && typeof m == "object" && "method" in m && "param" in m) {
const p = m.param;
switch (m.method) {
+ case "log.errorString":
+ if (typeof p == "string") {
+ log.error(`${logTag} ${p}`);
+ return;
+ }
+ break;
case "log.info":
if (Array.isArray(p)) {
// Need to cast from any[] to unknown[]
@@ -114,7 +124,6 @@ const handleMLWorkerRequests = (child: UtilityProcess) => {
return;
}
break;
-
default:
break;
}