1 import { IpcMainEvent, ipcMain, shell } from "electron";
2 import { setReleaseCategory } from "../store/settingsStore";
3 import { cachedLatestVersion } from "../update";
5 IPCInboxClientUpdateMessage,
6 IPCInboxGetInfoMessage,
7 IPCInboxGetUserInfoMessage,
8 } from "@proton/shared/lib/desktop/desktopTypes";
9 import { clearStorage } from "../utils/helpers";
10 import { ipcLogger } from "../utils/log";
11 import { getColorScheme, getTheme, isEqualTheme, setTheme } from "../utils/themes";
18 } from "../utils/view/viewManagement";
19 import { DESKTOP_FEATURES } from "./ipcConstants";
20 import { handleIPCBadge, resetBadge, showNotification } from "./notification";
21 import { setInstallSourceReported, getInstallSource } from "../store/installInfoStore";
22 import { getESUserChoice, setESUserChoice } from "../store/userSettingsStore";
23 import { checkDefaultMailto, getDefaultMailto, setDefaultMailtoTelemetryReported } from "../utils/protocol/default";
24 import { getAllAppVersions, storeAppVersion } from "../utils/appVersions";
25 import metrics from "../utils/metrics";
26 import telemetry from "../utils/telemetry";
28 function isValidClientUpdateMessage(message: unknown): message is IPCInboxClientUpdateMessage {
29 return Boolean(message && typeof message === "object" && "type" in message && "payload" in message);
32 export const handleIPCCalls = () => {
33 ipcMain.on("hasFeature", (event: IpcMainEvent, message: keyof typeof DESKTOP_FEATURES) => {
34 event.returnValue = !!DESKTOP_FEATURES[message];
37 ipcMain.on("getUserInfo", (event: IpcMainEvent, message: IPCInboxGetUserInfoMessage["type"], userID: string) => {
40 event.returnValue = getESUserChoice(userID);
43 ipcLogger.error(`Invalid getUserInfo message: ${message}`);
48 ipcMain.on("getInfo", (event: IpcMainEvent, message: IPCInboxGetInfoMessage["type"]) => {
51 event.returnValue = getTheme();
54 event.returnValue = cachedLatestVersion;
56 case "installSource": {
57 const installSource = getInstallSource();
58 event.returnValue = installSource;
59 setInstallSourceReported();
62 case "defaultMailto": {
63 event.returnValue = getDefaultMailto();
67 event.returnValue = telemetry.getDailyStats();
71 event.returnValue = getColorScheme();
73 case "getAllAppVersions":
74 event.returnValue = getAllAppVersions();
77 ipcLogger.error(`Invalid getInfo message: ${message}`);
82 ipcMain.on("clientUpdate", (_e, message: unknown) => {
83 if (!isValidClientUpdateMessage(message)) {
84 ipcLogger.error(`Invalid clientUpdate message: ${message}`);
88 const { type, payload } = message;
91 case "updateNotification":
92 handleIPCBadge(payload);
96 telemetry.userLogin();
101 telemetry.userLogout();
104 clearStorage(true, 500);
107 case "oauthPopupOpened": {
108 const enabled = payload === "oauthPopupStarted";
109 global.oauthProcess = enabled;
110 ipcLogger.debug("oauthProcess", enabled ? "enabled" : "disabled");
113 case "subscriptionModalOpened": {
114 const enabled = payload === "subscriptionModalStarted";
115 global.subscriptionProcess = enabled;
116 ipcLogger.debug("subscriptionProcess", enabled ? "enabled" : "disabled");
120 shell.openExternal(payload);
129 case "showNotification":
130 showNotification(payload);
136 if (!isEqualTheme(getTheme(), payload)) {
142 case "earlyAccess": {
143 setReleaseCategory(payload);
146 case "checkDefaultMailtoAndSignal": {
147 checkDefaultMailto();
148 const defaultMailto = getDefaultMailto();
150 getMailView()?.webContents?.send("hostUpdate", {
151 type: "defaultMailtoChecked",
152 payload: defaultMailto,
156 case "defaultMailtoTelemetryReported": {
157 setDefaultMailtoTelemetryReported(payload);
160 case "checkDailyStatsAndSignal": {
161 telemetry.checkDailyStats();
162 const dailyStatsReport = telemetry.getDailyStatsReport();
164 getMailView()?.webContents?.send("hostUpdate", {
165 type: "dailyStatsChecked",
166 payload: dailyStatsReport,
170 case "dailyStatsReported": {
171 telemetry.dailyStatsReported(payload);
174 case "setESUserChoice": {
175 setESUserChoice(payload.userID, payload.userChoice);
178 case "storeAppVersion":
179 storeAppVersion(payload);
182 throw new Error("Crash bandicoot");
183 case "metricsListenerChanged":
184 if (payload === "ready") {
185 metrics.listenerReady();
187 if (payload === "removed") {
188 metrics.listenerRemoved();
192 ipcLogger.error(`unknown message type: ${type}`);