Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ node_modules
dist
dist-ssr
/release
!/release/
/release/*
!/release/AUTO-UPDATE-GUIDE.md
!/release/AUTO-UPDATE-TEST-REPORT.md
!/release/RELEASE_NOTES.md
!/release/README.txt
!/release/VERSION.txt
*.local

# Editor directories and files
Expand Down
20 changes: 20 additions & 0 deletions electron/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ const { randomUUID } = require("crypto");
const { fileURLToPath } = require("url");
const { isSafeUrl, shouldBlockWebsiteNavigation } = require("./url-policy.cjs");
const { safeDownloadFilename } = require("./download-policy.cjs");
const { createUpdateManager } = require("./update-manager.cjs");

// Keep the existing browser-pro userData directory stable across the product rename.
const appDataDirectory = process.platform === "win32" && process.env.APPDATA
? process.env.APPDATA
: app.getPath("appData");
app.setPath("userData", path.join(appDataDirectory, "browser-pro"));
app.setName("DevFlow");
const { autoUpdater } = require("electron-updater");
if (process.platform === "win32") app.setAppUserModelId("com.devflow.browser");

const database = require("./database.cjs");
const { isValidIpcArguments } = require("./ipc-payload.cjs");

let mainWindow;
let updateManager = null;
let toolsWindow = null;
let toolsWindowReady = false;
let toolsWindowRequested = false;
Expand Down Expand Up @@ -106,6 +109,20 @@ function secureOn(channel, handler) {
});
}

updateManager = createUpdateManager({
app,
autoUpdater,
send: state => {
if (mainWindow && !mainWindow.isDestroyed() && !mainWindow.webContents.isDestroyed()) {
mainWindow.webContents.send("updates:state", state);
}
}
});
secureHandle("updates:get-version", () => ({ version: app.getVersion(), enabled: app.isPackaged }));
secureHandle("updates:check", () => updateManager.check());
secureHandle("updates:download", () => updateManager.download());
secureHandle("updates:install", () => updateManager.install());

function browserShortcutForInput(input) {
if (!input || input.type !== "keyDown") return null;
const key = String(input.key || "").toLowerCase();
Expand Down Expand Up @@ -3940,8 +3957,11 @@ app.whenReady().then(() => {
setupNetworkMonitoring();
setupDownloadManager();
createWindow();
updateManager.initialize();
});

app.on("before-quit", () => updateManager?.dispose());

app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});
Expand Down
10 changes: 10 additions & 0 deletions electron/preload.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const initialTheme = process.argv.find(argument => argument.startsWith("--devflo

contextBridge.exposeInMainWorld("browserAPI", {
initialTheme,
getAppVersion: () => ipcRenderer.invoke("updates:get-version"),
checkForUpdates: () => ipcRenderer.invoke("updates:check"),
downloadUpdate: () => ipcRenderer.invoke("updates:download"),
restartAndInstallUpdate: () => ipcRenderer.invoke("updates:install"),
onUpdateState: (callback) => {
if (typeof callback !== "function") return () => {};
const listener = (_, state) => callback(state);
ipcRenderer.on("updates:state", listener);
return () => ipcRenderer.removeListener("updates:state", listener);
},
navigate: (url) => ipcRenderer.invoke("browser:navigate", url),
back: () => ipcRenderer.invoke("browser:back"),
forward: () => ipcRenderer.invoke("browser:forward"),
Expand Down
102 changes: 102 additions & 0 deletions electron/update-manager.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const STARTUP_DELAY_MS = 30_000;
const CHECK_INTERVAL_MS = 4 * 60 * 60 * 1000;

function createUpdateManager({ app, autoUpdater, send, setTimeoutFn = setTimeout, setIntervalFn = setInterval }) {
let initialized = false;
let checkInFlight = false;
let downloadInFlight = false;
let available = false;
let downloaded = false;
let errorReported = false;
let startupTimer;
let periodicTimer;

function publish(status, details = {}) {
send({ status, version: details.version || null, percent: details.percent ?? null, transferred: details.transferred ?? null, total: details.total ?? null, message: details.message || null, phase: details.phase || null });
}

function reportError(message, phase) {
if (errorReported) return;
errorReported = true;
publish("error", { message, phase });
}

async function check() {
if (!initialized || checkInFlight) return { success: false, reason: "unavailable-or-in-progress" };
checkInFlight = true;
errorReported = false;
publish("checking");
try {
await autoUpdater.checkForUpdates();
return { success: true };
} catch {
reportError("Could not check for updates. Check your connection and try again.", "check");
return { success: false };
} finally {
checkInFlight = false;
}
}

function initialize() {
if (initialized || !app.isPackaged) return false;
initialized = true;
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = false;
autoUpdater.allowPrerelease = false;
autoUpdater.on("checking-for-update", () => publish("checking"));
autoUpdater.on("update-available", info => {
available = true;
downloaded = false;
publish("available", { version: info?.version });
});
autoUpdater.on("update-not-available", info => {
available = false;
publish("up-to-date", { version: info?.version });
});
autoUpdater.on("download-progress", progress => publish("downloading", progress));
autoUpdater.on("update-downloaded", info => {
downloaded = true;
publish("downloaded", { version: info?.version });
});
autoUpdater.on("error", _error => {
const isChecking = checkInFlight;
const message = isChecking
? "Could not check for updates. Check your connection and try again."
: "The update could not be downloaded. Check your connection and try again.";
reportError(message, isChecking ? "check" : "download");
});
startupTimer = setTimeoutFn(() => { void check(); }, STARTUP_DELAY_MS);
periodicTimer = setIntervalFn(() => { void check(); }, CHECK_INTERVAL_MS);
return true;
}

async function download() {
if (!initialized || !available || downloaded || downloadInFlight) return { success: false };
downloadInFlight = true;
errorReported = false;
try {
await autoUpdater.downloadUpdate();
return { success: true };
} catch {
reportError("The update could not be downloaded. Check your connection and try again.", "download");
return { success: false };
} finally {
downloadInFlight = false;
}
}

function install() {
if (!initialized || !downloaded) return { success: false };
autoUpdater.quitAndInstall();
return { success: true };
}

function dispose() {
if (startupTimer) clearTimeout(startupTimer);
if (periodicTimer) clearInterval(periodicTimer);
}

return { initialize, check, download, install, dispose };
}

module.exports = { createUpdateManager, STARTUP_DELAY_MS, CHECK_INTERVAL_MS };
Loading