diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2023-03-10 17:31:23 +0100 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2023-03-10 17:31:23 +0100 |
| commit | 67f13cc44286432274c320360529ed28f6358a37 (patch) | |
| tree | aba4873436bc4cab25bf08125430ac98d96c02cc /gui/src/main | |
| parent | 8be326a45b53dae1e16bd62a57ac257ed1e9cf93 (diff) | |
| parent | 25fa6c63766f6da254ebc6d252214244ca116e0e (diff) | |
| download | mullvadvpn-67f13cc44286432274c320360529ed28f6358a37.tar.xz mullvadvpn-67f13cc44286432274c320360529ed28f6358a37.zip | |
Merge branch 'disconnect-daemon-on-suspen-DES-11'
Diffstat (limited to 'gui/src/main')
| -rw-r--r-- | gui/src/main/daemon-rpc.ts | 13 | ||||
| -rw-r--r-- | gui/src/main/index.ts | 26 |
2 files changed, 38 insertions, 1 deletions
diff --git a/gui/src/main/daemon-rpc.ts b/gui/src/main/daemon-rpc.ts index 5d24870908..966846fdf6 100644 --- a/gui/src/main/daemon-rpc.ts +++ b/gui/src/main/daemon-rpc.ts @@ -137,6 +137,7 @@ type CallFunctionArgument<T, R> = export class DaemonRpc { private client: ManagementServiceClient; private isConnectedValue = false; + private isClosed = false; private connectionObservers: ConnectionObserver[] = []; private nextSubscriptionId = 0; private subscriptions: Map<number, grpc.ClientReadableStream<grpcTypes.DaemonEvent>> = new Map(); @@ -154,6 +155,17 @@ export class DaemonRpc { return this.isConnectedValue; } + public reopen() { + if (this.isClosed) { + this.isClosed = false; + this.client = new ManagementServiceClient( + DAEMON_RPC_PATH, + grpc.credentials.createInsecure(), + this.channelOptions(), + ); + } + } + public connect(): Promise<void> { return new Promise((resolve, reject) => { this.client.waitForReady(this.deadlineFromNow(), (error) => { @@ -179,6 +191,7 @@ export class DaemonRpc { this.removeSubscription(subscriptionId); } + this.isClosed = true; this.client.close(); if (this.reconnectionTimeout) { clearTimeout(this.reconnectionTimeout); diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index 7181f863bf..1e8214a277 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -1,5 +1,5 @@ import { exec, execFile } from 'child_process'; -import { app, nativeTheme, session, shell, systemPreferences } from 'electron'; +import { app, nativeTheme, powerMonitor, session, shell, systemPreferences } from 'electron'; import fs from 'fs'; import * as path from 'path'; import util from 'util'; @@ -181,6 +181,9 @@ class ApplicationMain log.info('quit received'); this.onQuit(); }); + + powerMonitor.on('suspend', this.onSuspend); + powerMonitor.on('resume', this.onResume); } public async performPostUpgradeCheck(): Promise<void> { @@ -442,6 +445,27 @@ class ApplicationMain ); }; + private onSuspend = () => { + log.info('Suspend event received, disconnecting from daemon'); + if (this.daemonEventListener) { + this.daemonRpc.unsubscribeDaemonEventListener(this.daemonEventListener); + } + + const wasConnected = this.daemonRpc.isConnected; + IpcMainEventChannel.navigation.notifyReset?.(); + this.daemonRpc.disconnect(); + this.onDaemonDisconnected(wasConnected); + }; + + private onResume = () => { + log.info('Resume event received, connecting to daemon'); + this.daemonRpc.reopen(); + this.daemonRpc.addConnectionObserver( + new ConnectionObserver(this.onDaemonConnected, this.onDaemonDisconnected), + ); + this.connectToDaemon(); + }; + private onDaemonConnected = async () => { const firstDaemonConnection = this.beforeFirstDaemonConnection; this.beforeFirstDaemonConnection = false; |
