diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2022-08-11 15:00:29 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2022-08-22 08:34:37 +0200 |
| commit | 27754f1b721dd1d0b82febf3df61153567dc7567 (patch) | |
| tree | 37be43882ea2ac8a734ecd9cf894fa2e290bcb64 /gui/src | |
| parent | b5bfaa7bc50a7055ed1e562086ac3ed099e0d36a (diff) | |
| download | mullvadvpn-27754f1b721dd1d0b82febf3df61153567dc7567.tar.xz mullvadvpn-27754f1b721dd1d0b82febf3df61153567dc7567.zip | |
Move daemon connection state to daemon-rpc
Diffstat (limited to 'gui/src')
| -rw-r--r-- | gui/src/main/daemon-rpc.ts | 16 | ||||
| -rw-r--r-- | gui/src/main/index.ts | 37 |
2 files changed, 27 insertions, 26 deletions
diff --git a/gui/src/main/daemon-rpc.ts b/gui/src/main/daemon-rpc.ts index c80a49dc10..424c765d53 100644 --- a/gui/src/main/daemon-rpc.ts +++ b/gui/src/main/daemon-rpc.ts @@ -130,7 +130,7 @@ type CallFunctionArgument<T, R> = export class DaemonRpc { private client: ManagementServiceClient; - private isConnected = false; + private isConnectedValue = false; private connectionObservers: ConnectionObserver[] = []; private nextSubscriptionId = 0; private subscriptions: Map<number, grpc.ClientReadableStream<grpcTypes.DaemonEvent>> = new Map(); @@ -144,6 +144,10 @@ export class DaemonRpc { ); } + public get isConnected() { + return this.isConnectedValue; + } + public connect(): Promise<void> { return new Promise((resolve, reject) => { this.client.waitForReady(this.deadlineFromNow(), (error) => { @@ -153,7 +157,7 @@ export class DaemonRpc { reject(error); } else { this.reconnectionTimeout = undefined; - this.isConnected = true; + this.isConnectedValue = true; this.connectionObservers.forEach((observer) => observer.onOpen()); this.setChannelCallback(); resolve(); @@ -163,7 +167,7 @@ export class DaemonRpc { } public disconnect() { - this.isConnected = false; + this.isConnectedValue = false; for (const subscriptionId of this.subscriptions.keys()) { this.removeSubscription(subscriptionId); @@ -683,14 +687,14 @@ export class DaemonRpc { const wasConnected = this.isConnected; if (this.channelDisconnected(currentState)) { this.connectionObservers.forEach((observer) => observer.onClose()); - this.isConnected = false; + this.isConnectedValue = false; // Try and reconnect in case void this.connect().catch((error) => { log.error(`Failed to reconnect - ${error}`); }); this.setChannelCallback(currentState); } else if (!wasConnected && currentState === grpc.connectivityState.READY) { - this.isConnected = true; + this.isConnectedValue = true; this.connectionObservers.forEach((observer) => observer.onOpen()); this.setChannelCallback(currentState); } @@ -727,7 +731,7 @@ export class DaemonRpc { const lastState = this.client.getChannel().getConnectivityState(true); if (this.channelDisconnected(lastState)) { this.connectionObservers.forEach((observer) => observer.onClose()); - this.isConnected = false; + this.isConnectedValue = false; } if (!this.isConnected) { void this.connect().catch((error) => { diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts index dca6c87fb3..6d57df3b5f 100644 --- a/gui/src/main/index.ts +++ b/gui/src/main/index.ts @@ -135,7 +135,6 @@ class ApplicationMain { private daemonEventListener?: SubscriptionListener<DaemonEvent>; private reconnectBackoff = new ReconnectionBackoff(); private beforeFirstDaemonConnection = true; - private connectedToDaemon = false; private isPerformingPostUpgrade = false; private quitStage = AppQuitStage.unready; @@ -452,7 +451,7 @@ class ApplicationMain { if (this.stayConnectedOnQuit) { log.info('Not disconnecting tunnel on quit'); } else { - if (this.connectedToDaemon) { + if (this.daemonRpc.isConnected) { try { await this.daemonRpc.disconnectTunnel(); log.info('Disconnected the tunnel'); @@ -486,7 +485,7 @@ class ApplicationMain { this.windowController.window.closable = true; } - if (this.connectedToDaemon) { + if (this.daemonRpc.isConnected) { this.daemonRpc.disconnect(); } @@ -562,7 +561,7 @@ class ApplicationMain { await this.trayIconController.updateTheme(); this.setTrayContextMenu(); - this.trayIconController?.setTooltip(this.connectedToDaemon, this.tunnelState); + this.trayIconController?.setTooltip(this.daemonRpc.isConnected, this.tunnelState); if (process.platform === 'win32') { nativeTheme.on('updated', async () => { @@ -652,7 +651,6 @@ class ApplicationMain { private onDaemonConnected = async () => { const firstDaemonConnection = this.beforeFirstDaemonConnection; this.beforeFirstDaemonConnection = false; - this.connectedToDaemon = true; log.info('Connected to the daemon'); @@ -760,9 +758,9 @@ class ApplicationMain { // reset the reconnect backoff when connection established. this.reconnectBackoff.reset(); - // notify renderer, this.connectedToDaemon could have changed if the daemon disconnected again - // before this if-statement is reached. - if (this.windowController && this.connectedToDaemon) { + // notify renderer, this.daemonRpc.isConnected could have changed if the daemon disconnected + // again before this if-statement is reached. + if (this.windowController && this.daemonRpc.isConnected) { IpcMainEventChannel.daemon.notifyConnected(this.windowController.webContents); } @@ -782,7 +780,7 @@ class ApplicationMain { } // make sure we were connected before to distinguish between a failed attempt to reconnect and // connection loss. - const wasConnected = this.connectedToDaemon; + const wasConnected = this.daemonRpc.isConnected; // Reset the daemon event listener since it's going to be invalidated on disconnect this.daemonEventListener = undefined; @@ -790,12 +788,11 @@ class ApplicationMain { this.tunnelStateFallback = undefined; if (wasConnected) { - this.connectedToDaemon = false; // update the tray icon to indicate that the computer is not secure anymore this.updateTrayIcon({ state: 'disconnected' }, false); this.setTrayContextMenu(); - this.trayIconController?.setTooltip(this.connectedToDaemon, this.tunnelState); + this.trayIconController?.setTooltip(this.daemonRpc.isConnected, this.tunnelState); // notify renderer process if (this.windowController) { @@ -876,21 +873,21 @@ class ApplicationMain { } private connectTunnel = async (): Promise<void> => { - if (connectEnabled(this.connectedToDaemon, this.isLoggedIn(), this.tunnelState.state)) { + if (connectEnabled(this.daemonRpc.isConnected, this.isLoggedIn(), this.tunnelState.state)) { this.setOptimisticTunnelState('connecting'); await this.daemonRpc.connectTunnel(); } }; private reconnectTunnel = async (): Promise<void> => { - if (reconnectEnabled(this.connectedToDaemon, this.isLoggedIn(), this.tunnelState.state)) { + if (reconnectEnabled(this.daemonRpc.isConnected, this.isLoggedIn(), this.tunnelState.state)) { this.setOptimisticTunnelState('connecting'); await this.daemonRpc.reconnectTunnel(); } }; private disconnectTunnel = async (): Promise<void> => { - if (disconnectEnabled(this.connectedToDaemon, this.tunnelState.state)) { + if (disconnectEnabled(this.daemonRpc.isConnected, this.tunnelState.state)) { this.setOptimisticTunnelState('disconnecting'); await this.daemonRpc.disconnectTunnel(); } @@ -949,7 +946,7 @@ class ApplicationMain { this.updateTrayIcon(newState, this.settings.blockWhenDisconnected); this.setTrayContextMenu(); - this.trayIconController?.setTooltip(this.connectedToDaemon, this.tunnelState); + this.trayIconController?.setTooltip(this.daemonRpc.isConnected, this.tunnelState); this.notificationController.notifyTunnelState( newState, @@ -1282,7 +1279,7 @@ class ApplicationMain { private registerIpcListeners() { IpcMainEventChannel.state.handleGet(() => ({ - isConnected: this.connectedToDaemon, + isConnected: this.daemonRpc.isConnected, autoStart: getOpenAtLogin(), accountData: this.accountData, accountHistory: this.accountHistory, @@ -1599,7 +1596,7 @@ class ApplicationMain { } private updateAccountData() { - if (this.connectedToDaemon && this.isLoggedIn()) { + if (this.daemonRpc.isConnected && this.isLoggedIn()) { this.accountDataCache.fetch(this.getAccountToken()!); } } @@ -1711,7 +1708,7 @@ class ApplicationMain { }; this.setTrayContextMenu(); - this.trayIconController?.setTooltip(this.connectedToDaemon, this.tunnelState); + this.trayIconController?.setTooltip(this.daemonRpc.isConnected, this.tunnelState); } private blockPermissionRequests() { @@ -1985,7 +1982,7 @@ class ApplicationMain { // displayed on left click as well. this.tray?.on('right-click', () => this.trayIconController?.popUpContextMenu( - this.connectedToDaemon, + this.daemonRpc.isConnected, this.isLoggedIn(), this.tunnelState, ), @@ -2023,7 +2020,7 @@ class ApplicationMain { private setTrayContextMenu() { this.trayIconController?.setContextMenu( - this.connectedToDaemon, + this.daemonRpc.isConnected, this.isLoggedIn(), this.tunnelState, ); |
