summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorOskar Nyberg <oskar@mullvad.net>2023-04-14 17:57:47 +0200
committerOskar Nyberg <oskar@mullvad.net>2023-04-17 13:19:37 +0200
commit226cdcfc25a1106014527ef419133e6bdc2bff86 (patch)
tree3f4cd352d3ecc842fd94930013ca7f89175ba83b
parent290ad7672272f34529afcd80a79f56fc38b340f6 (diff)
downloadmullvadvpn-226cdcfc25a1106014527ef419133e6bdc2bff86.tar.xz
mullvadvpn-226cdcfc25a1106014527ef419133e6bdc2bff86.zip
Remove daemon connection observer when disconnecting during suspend
-rw-r--r--gui/src/main/daemon-rpc.ts27
-rw-r--r--gui/src/main/index.ts24
2 files changed, 23 insertions, 28 deletions
diff --git a/gui/src/main/daemon-rpc.ts b/gui/src/main/daemon-rpc.ts
index 966846fdf6..40794e765c 100644
--- a/gui/src/main/daemon-rpc.ts
+++ b/gui/src/main/daemon-rpc.ts
@@ -138,12 +138,11 @@ 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();
private reconnectionTimeout?: NodeJS.Timer;
- constructor() {
+ constructor(private connectionObserver?: ConnectionObserver) {
this.client = new ManagementServiceClient(
DAEMON_RPC_PATH,
grpc.credentials.createInsecure(),
@@ -155,7 +154,7 @@ export class DaemonRpc {
return this.isConnectedValue;
}
- public reopen() {
+ public reopen(connectionObserver?: ConnectionObserver) {
if (this.isClosed) {
this.isClosed = false;
this.client = new ManagementServiceClient(
@@ -163,6 +162,8 @@ export class DaemonRpc {
grpc.credentials.createInsecure(),
this.channelOptions(),
);
+
+ this.connectionObserver = connectionObserver;
}
}
@@ -176,7 +177,7 @@ export class DaemonRpc {
} else {
this.reconnectionTimeout = undefined;
this.isConnectedValue = true;
- this.connectionObservers.forEach((observer) => observer.onOpen());
+ this.connectionObserver?.onOpen();
this.setChannelCallback();
resolve();
}
@@ -193,24 +194,12 @@ export class DaemonRpc {
this.isClosed = true;
this.client.close();
+ this.connectionObserver = undefined;
if (this.reconnectionTimeout) {
clearTimeout(this.reconnectionTimeout);
}
}
- public addConnectionObserver(observer: ConnectionObserver) {
- this.connectionObservers.push(observer);
- // Call getConnectivityState(true) to start connecting if idle
- this.client.getChannel()?.getConnectivityState(true);
- }
-
- public removeConnectionObserver(observer: ConnectionObserver) {
- const index = this.connectionObservers.indexOf(observer);
- if (index !== -1) {
- this.connectionObservers.splice(index, 1);
- }
- }
-
public async getAccountData(accountToken: AccountToken): Promise<IAccountData> {
try {
const response = await this.callString<grpcTypes.AccountData>(
@@ -686,7 +675,7 @@ export class DaemonRpc {
const wasConnected = this.isConnectedValue;
this.isConnectedValue = false;
- this.connectionObservers.forEach((observer) => observer.onClose(wasConnected, error));
+ this.connectionObserver?.onClose(wasConnected, error);
}
private removeSubscription(id: number) {
@@ -739,7 +728,7 @@ export class DaemonRpc {
this.setChannelCallback(currentState);
} else if (!wasConnected && currentState === grpc.connectivityState.READY) {
this.isConnectedValue = true;
- this.connectionObservers.forEach((observer) => observer.onOpen());
+ this.connectionObserver?.onOpen();
this.setChannelCallback(currentState);
}
}
diff --git a/gui/src/main/index.ts b/gui/src/main/index.ts
index c30665ffc5..95668c5df4 100644
--- a/gui/src/main/index.ts
+++ b/gui/src/main/index.ts
@@ -79,13 +79,13 @@ class ApplicationMain
TunnelStateHandlerDelegate,
SettingsDelegate,
AccountDelegate {
- private daemonRpc = new DaemonRpc();
+ private daemonRpc: DaemonRpc;
private notificationController = new NotificationController(this);
- private version = new Version(this, this.daemonRpc, UPDATE_NOTIFICATION_DISABLED);
- private settings = new Settings(this, this.daemonRpc, this.version.currentVersion);
+ private version: Version;
+ private settings: Settings;
+ private account: Account;
private userInterface?: UserInterface;
- private account: Account = new Account(this, this.daemonRpc);
private tunnelState = new TunnelStateHandler(this);
private daemonEventListener?: SubscriptionListener<DaemonEvent>;
@@ -113,6 +113,16 @@ class ApplicationMain
private relayList?: IRelayListWithEndpointData;
+ public constructor() {
+ this.daemonRpc = new DaemonRpc(
+ new ConnectionObserver(this.onDaemonConnected, this.onDaemonDisconnected),
+ );
+
+ this.version = new Version(this, this.daemonRpc, UPDATE_NOTIFICATION_DISABLED);
+ this.settings = new Settings(this, this.daemonRpc, this.version.currentVersion);
+ this.account = new Account(this, this.daemonRpc);
+ }
+
public run() {
// Remove window animations to combat window flickering when opening window. Can be removed when
// this issue has been resolved: https://github.com/electron/electron/issues/12130
@@ -375,9 +385,6 @@ class ApplicationMain
this.updateCurrentLocale();
- this.daemonRpc.addConnectionObserver(
- new ConnectionObserver(this.onDaemonConnected, this.onDaemonDisconnected),
- );
this.connectToDaemon();
if (process.platform === 'darwin') {
@@ -459,8 +466,7 @@ class ApplicationMain
private onResume = () => {
log.info('Resume event received, connecting to daemon');
- this.daemonRpc.reopen();
- this.daemonRpc.addConnectionObserver(
+ this.daemonRpc.reopen(
new ConnectionObserver(this.onDaemonConnected, this.onDaemonDisconnected),
);
this.connectToDaemon();