1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-13 10:24:20 +01:00
bitwarden-browser/apps/desktop/src/main/updater.main.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

139 lines
4.2 KiB
TypeScript
Raw Normal View History

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
2022-06-14 17:10:53 +02:00
import { I18nService } from "@bitwarden/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 UpdaterCheckInitialDelay = 5 * 1000; // 5 seconds
2018-05-04 20:16:23 +02:00
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
[PM-469] [PM-1325] [PS-1165] [PS-1257] Small refactorings/improvements on the desktop app main (#4704) * Only pass necessary service to power-monitor PowerMonitorMain only requires the messagingService instead of a full reference to Main * Remove never changing constructor params Window.main has a defaultWidth and defaultHeight that never change, so they do not need to get passed in from outside hideTitleBar is always true, so there is no need to make it a param * Remove projectName from updater This is likely another relict from sharing this previously with dircetory-connector and is not needed anymore * Only pass necessary service to MenuMain MenuMain only needs service references instead of a full reference to Main * Refactor biometrics service Create BiometricsService that takes care of loading the platformspecifc services, hiding the implementation details Make it clearer which dependencies are needed by a specific biometrics-service (compile-error vs runtime-error) Add unit tests Isolate biometrics import/exports with a barrel file * Fix #3148 recordActivity was only getting called when user-activity in the main window is recognized When using biometrics to unlock, the Windows Hello/TouchID prompt would be focused and no input would be recognised. LastActive would have an old value and the vault would get locked * Improve reloading with biometrics * Mock import of desktop-native * Add mock for "@bitwarden/desktop-native-linux-x64-musl" * Revert "Add mock for "@bitwarden/desktop-native-linux-x64-musl"" This reverts commit 69771b94bf34ba9af9d23370b7d92ff8a20ec92e. * mock the exports of desktop-native * Pass process.platform inot BiometricsService
2023-03-10 21:32:26 +01:00
constructor(private i18nService: I18nService, private windowMain: WindowMain) {
autoUpdater.logger = log;
2021-12-16 13:36:21 +01: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() {
global.setTimeout(async () => await this.checkForUpdate(), UpdaterCheckInitialDelay);
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", () => {
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",
[PM-469] [PM-1325] [PS-1165] [PS-1257] Small refactorings/improvements on the desktop app main (#4704) * Only pass necessary service to power-monitor PowerMonitorMain only requires the messagingService instead of a full reference to Main * Remove never changing constructor params Window.main has a defaultWidth and defaultHeight that never change, so they do not need to get passed in from outside hideTitleBar is always true, so there is no need to make it a param * Remove projectName from updater This is likely another relict from sharing this previously with dircetory-connector and is not needed anymore * Only pass necessary service to MenuMain MenuMain only needs service references instead of a full reference to Main * Refactor biometrics service Create BiometricsService that takes care of loading the platformspecifc services, hiding the implementation details Make it clearer which dependencies are needed by a specific biometrics-service (compile-error vs runtime-error) Add unit tests Isolate biometrics import/exports with a barrel file * Fix #3148 recordActivity was only getting called when user-activity in the main window is recognized When using biometrics to unlock, the Windows Hello/TouchID prompt would be focused and no input would be recognised. LastActive would have an old value and the vault would get locked * Improve reloading with biometrics * Mock import of desktop-native * Add mock for "@bitwarden/desktop-native-linux-x64-musl" * Revert "Add mock for "@bitwarden/desktop-native-linux-x64-musl"" This reverts commit 69771b94bf34ba9af9d23370b7d92ff8a20ec92e. * mock the exports of desktop-native * Pass process.platform inot BiometricsService
2023-03-10 21:32:26 +01:00
title: this.i18nService.t("bitwarden") + " - " + 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) {
autoUpdater.downloadUpdate();
} else {
2018-05-04 20:16:23 +02:00
this.reset();
2021-12-16 13:36:21 +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
});
autoUpdater.on("update-downloaded", async (info) => {
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",
[PM-469] [PM-1325] [PS-1165] [PS-1257] Small refactorings/improvements on the desktop app main (#4704) * Only pass necessary service to power-monitor PowerMonitorMain only requires the messagingService instead of a full reference to Main * Remove never changing constructor params Window.main has a defaultWidth and defaultHeight that never change, so they do not need to get passed in from outside hideTitleBar is always true, so there is no need to make it a param * Remove projectName from updater This is likely another relict from sharing this previously with dircetory-connector and is not needed anymore * Only pass necessary service to MenuMain MenuMain only needs service references instead of a full reference to Main * Refactor biometrics service Create BiometricsService that takes care of loading the platformspecifc services, hiding the implementation details Make it clearer which dependencies are needed by a specific biometrics-service (compile-error vs runtime-error) Add unit tests Isolate biometrics import/exports with a barrel file * Fix #3148 recordActivity was only getting called when user-activity in the main window is recognized When using biometrics to unlock, the Windows Hello/TouchID prompt would be focused and no input would be recognised. LastActive would have an old value and the vault would get locked * Improve reloading with biometrics * Mock import of desktop-native * Add mock for "@bitwarden/desktop-native-linux-x64-musl" * Revert "Add mock for "@bitwarden/desktop-native-linux-x64-musl"" This reverts commit 69771b94bf34ba9af9d23370b7d92ff8a20ec92e. * mock the exports of desktop-native * Pass process.platform inot BiometricsService
2023-03-10 21:32:26 +01:00
title: this.i18nService.t("bitwarden") + " - " + 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) {
// Quit and install have a different window logic, setting `isQuitting` just to be safe.
this.windowMain.isQuitting = true;
2022-03-25 10:13:50 +01:00
autoUpdater.quitAndInstall(true, 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/clients/releases");
2018-05-04 20:16:23 +02:00
}
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() {
autoUpdater.autoDownload = true;
this.doingUpdateCheck = false;
2021-12-16 13:36:21 +01:00
}
2018-05-04 20:16:23 +02:00
}