2022-02-22 15:39:11 +01:00
|
|
|
import { dialog, shell } from "electron";
|
2018-05-04 20:16:23 +02:00
|
|
|
import log from "electron-log";
|
|
|
|
import { autoUpdater } from "electron-updater";
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
2022-02-22 15:39:11 +01:00
|
|
|
|
|
|
|
import { isAppImage, isDev, isMacAppStore, isWindowsPortable, isWindowsStore } from "./utils";
|
2018-05-04 20:16:23 +02:00
|
|
|
import { WindowMain } from "./window.main";
|
|
|
|
|
|
|
|
const UpdaterCheckInitalDelay = 5 * 1000; // 5 seconds
|
|
|
|
const UpdaterCheckInterval = 12 * 60 * 60 * 1000; // 12 hours
|
|
|
|
|
|
|
|
export class UpdaterMain {
|
|
|
|
private doingUpdateCheck = false;
|
|
|
|
private doingUpdateCheckWithFeedback = false;
|
|
|
|
private canUpdate = false;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
constructor(
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private windowMain: WindowMain,
|
|
|
|
private gitHubProject: string,
|
|
|
|
private onCheckingForUpdate: () => void = null,
|
2018-11-27 14:31:08 +01:00
|
|
|
private onReset: () => void = null,
|
|
|
|
private onUpdateDownloaded: () => void = null,
|
2018-08-25 14:30:06 +02:00
|
|
|
private projectName: string
|
2021-12-16 13:36:21 +01:00
|
|
|
) {
|
2018-08-25 14:30:06 +02:00
|
|
|
autoUpdater.logger = log;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-08-25 14:30:06 +02:00
|
|
|
const linuxCanUpdate = process.platform === "linux" && isAppImage();
|
2018-05-04 20:16:23 +02:00
|
|
|
const windowsCanUpdate =
|
|
|
|
process.platform === "win32" && !isWindowsStore() && !isWindowsPortable();
|
|
|
|
const macCanUpdate = process.platform === "darwin" && !isMacAppStore();
|
|
|
|
this.canUpdate =
|
2020-01-27 15:46:42 +01:00
|
|
|
process.env.ELECTRON_NO_UPDATER !== "1" &&
|
|
|
|
(linuxCanUpdate || windowsCanUpdate || macCanUpdate);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
async init() {
|
2020-01-27 15:46:42 +01:00
|
|
|
global.setTimeout(async () => await this.checkForUpdate(), UpdaterCheckInitalDelay);
|
2018-11-27 14:31:08 +01:00
|
|
|
global.setInterval(async () => await this.checkForUpdate(), UpdaterCheckInterval);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
autoUpdater.on("checking-for-update", () => {
|
|
|
|
if (this.onCheckingForUpdate != null) {
|
|
|
|
this.onCheckingForUpdate();
|
|
|
|
}
|
|
|
|
this.doingUpdateCheck = true;
|
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
autoUpdater.on("update-available", async () => {
|
|
|
|
if (this.doingUpdateCheckWithFeedback) {
|
|
|
|
if (this.windowMain.win == null) {
|
|
|
|
this.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:46:42 +01:00
|
|
|
const result = await dialog.showMessageBox(this.windowMain.win, {
|
2018-05-04 20:16:23 +02:00
|
|
|
type: "info",
|
2018-11-27 14:31:08 +01:00
|
|
|
title:
|
|
|
|
this.i18nService.t(this.projectName) + " - " + this.i18nService.t("updateAvailable"),
|
2018-05-04 20:16:23 +02:00
|
|
|
message: this.i18nService.t("updateAvailable"),
|
|
|
|
detail: this.i18nService.t("updateAvailableDesc"),
|
|
|
|
buttons: [this.i18nService.t("yes"), this.i18nService.t("no")],
|
|
|
|
cancelId: 1,
|
|
|
|
defaultId: 0,
|
|
|
|
noLink: true,
|
|
|
|
});
|
|
|
|
|
2020-01-27 15:46:42 +01:00
|
|
|
if (result.response === 0) {
|
2021-02-04 16:49:23 +01:00
|
|
|
autoUpdater.downloadUpdate();
|
|
|
|
} else {
|
2018-05-04 20:16:23 +02:00
|
|
|
this.reset();
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
autoUpdater.on("update-not-available", () => {
|
2018-05-04 20:16:23 +02:00
|
|
|
if (this.doingUpdateCheckWithFeedback && this.windowMain.win != null) {
|
|
|
|
dialog.showMessageBox(this.windowMain.win, {
|
|
|
|
message: this.i18nService.t("noUpdatesAvailable"),
|
|
|
|
buttons: [this.i18nService.t("ok")],
|
|
|
|
defaultId: 0,
|
|
|
|
noLink: true,
|
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
this.reset();
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
autoUpdater.on("update-downloaded", async (info) => {
|
2018-05-04 20:16:23 +02:00
|
|
|
if (this.onUpdateDownloaded != null) {
|
|
|
|
this.onUpdateDownloaded();
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
if (this.windowMain.win == null) {
|
2021-12-16 13:36:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:46:42 +01:00
|
|
|
const result = await dialog.showMessageBox(this.windowMain.win, {
|
2018-05-04 20:16:23 +02:00
|
|
|
type: "info",
|
2018-11-27 14:31:08 +01:00
|
|
|
title: this.i18nService.t(this.projectName) + " - " + this.i18nService.t("restartToUpdate"),
|
2018-05-04 20:16:23 +02:00
|
|
|
message: this.i18nService.t("restartToUpdate"),
|
|
|
|
detail: this.i18nService.t("restartToUpdateDesc", info.version),
|
|
|
|
buttons: [this.i18nService.t("restart"), this.i18nService.t("later")],
|
|
|
|
cancelId: 1,
|
|
|
|
defaultId: 0,
|
|
|
|
noLink: true,
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2020-01-27 15:46:42 +01:00
|
|
|
if (result.response === 0) {
|
2018-05-04 20:16:23 +02:00
|
|
|
autoUpdater.quitAndInstall(false, true);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
autoUpdater.on("error", (error) => {
|
|
|
|
if (this.doingUpdateCheckWithFeedback) {
|
|
|
|
dialog.showErrorBox(
|
|
|
|
this.i18nService.t("updateError"),
|
|
|
|
error == null ? this.i18nService.t("unknown") : (error.stack || error).toString()
|
2021-12-16 13:36:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
this.reset();
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:11 +01:00
|
|
|
async checkForUpdate(withFeedback = false) {
|
2018-05-04 20:16:23 +02:00
|
|
|
if (this.doingUpdateCheck || isDev()) {
|
2021-12-16 13:36:21 +01:00
|
|
|
return;
|
2018-05-04 20:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.canUpdate) {
|
|
|
|
if (withFeedback) {
|
|
|
|
shell.openExternal("https://github.com/bitwarden/" + this.gitHubProject + "/releases");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.doingUpdateCheckWithFeedback = withFeedback;
|
|
|
|
if (withFeedback) {
|
|
|
|
autoUpdater.autoDownload = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
await autoUpdater.checkForUpdates();
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:16:23 +02:00
|
|
|
private reset() {
|
|
|
|
if (this.onReset != null) {
|
|
|
|
this.onReset();
|
|
|
|
}
|
|
|
|
autoUpdater.autoDownload = true;
|
|
|
|
this.doingUpdateCheck = false;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-05-04 20:16:23 +02:00
|
|
|
}
|