diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2019-02-14 12:24:33 +0100 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2019-02-14 15:37:02 +0100 |
| commit | bcfda1c26cb9ac3093b0981feb0c65a23238708f (patch) | |
| tree | 19f26d65a574cc478a4ac32b21b03c9ccb403d1b | |
| parent | 895bf56ff846ca7ff6f05a5824018e57898d03ea (diff) | |
| download | mullvadvpn-bcfda1c26cb9ac3093b0981feb0c65a23238708f.tar.xz mullvadvpn-bcfda1c26cb9ac3093b0981feb0c65a23238708f.zip | |
Update account history on account change
| -rw-r--r-- | gui/packages/desktop/src/main/index.ts | 41 | ||||
| -rw-r--r-- | gui/packages/desktop/src/renderer/app.tsx | 24 | ||||
| -rw-r--r-- | gui/packages/desktop/src/shared/ipc-event-channel.ts | 45 |
3 files changed, 69 insertions, 41 deletions
diff --git a/gui/packages/desktop/src/main/index.ts b/gui/packages/desktop/src/main/index.ts index 173be30819..bd56ed0423 100644 --- a/gui/packages/desktop/src/main/index.ts +++ b/gui/packages/desktop/src/main/index.ts @@ -60,6 +60,7 @@ class ApplicationMain { private oldLogFilePath?: string; private quitStage = AppQuitStage.unready; + private accountHistory: AccountToken[] = []; private tunnelState: TunnelStateTransition = { state: 'disconnected' }; private settings: ISettings = { accountToken: undefined, @@ -351,6 +352,15 @@ class ApplicationMain { return this.recoverFromBootstrapError(error); } + // fetch account history + try { + this.setAccountHistory(await this.daemonRpc.getAccountHistory()); + } catch (error) { + log.error(`Failed to fetch the account history: ${error.message}`); + + return this.recoverFromBootstrapError(error); + } + // fetch the tunnel state try { this.setTunnelState(await this.daemonRpc.getState()); @@ -490,6 +500,14 @@ class ApplicationMain { ]); } + private setAccountHistory(accountHistory: AccountToken[]) { + this.accountHistory = accountHistory; + + if (this.windowController) { + IpcMainEventChannel.accountHistory.notify(this.windowController.webContents, accountHistory); + } + } + private setTunnelState(newState: TunnelStateTransition) { this.tunnelState = newState; this.updateTrayIcon(newState, this.settings.blockWhenDisconnected); @@ -505,9 +523,15 @@ class ApplicationMain { } private setSettings(newSettings: ISettings) { + const oldSettings = this.settings; this.settings = newSettings; + this.updateTrayIcon(this.tunnelState, newSettings.blockWhenDisconnected); + if (oldSettings.accountToken !== newSettings.accountToken) { + this.updateAccountHistory(); + } + if (this.windowController) { IpcMainEventChannel.settings.notify(this.windowController.webContents, newSettings); } @@ -748,6 +772,7 @@ class ApplicationMain { IpcMainEventChannel.state.handleGet(() => ({ isConnected: this.connectedToDaemon, autoStart: getOpenAtLogin(), + accountHistory: this.accountHistory, tunnelState: this.tunnelState, settings: this.settings, location: this.location, @@ -800,10 +825,10 @@ class ApplicationMain { this.daemonRpc.getAccountData(token), ); - IpcMainEventChannel.accountHistory.handleGet(() => this.daemonRpc.getAccountHistory()); - IpcMainEventChannel.accountHistory.handleRemoveItem((token: AccountToken) => - this.daemonRpc.removeAccountFromHistory(token), - ); + IpcMainEventChannel.accountHistory.handleRemoveItem(async (token: AccountToken) => { + await this.daemonRpc.removeAccountFromHistory(token); + this.updateAccountHistory(); + }); ipcMain.on('show-window', () => { const windowController = this.windowController; @@ -883,6 +908,14 @@ class ApplicationMain { ); } + private async updateAccountHistory(): Promise<void> { + try { + this.setAccountHistory(await this.daemonRpc.getAccountHistory()); + } catch (error) { + log.error(`Failed to fetch the account history: ${error.message}`); + } + } + private updateDaemonsAutoConnect() { const daemonAutoConnect = this.guiSettings.autoConnect && getOpenAtLogin(); if (daemonAutoConnect !== this.settings.autoConnect) { diff --git a/gui/packages/desktop/src/renderer/app.tsx b/gui/packages/desktop/src/renderer/app.tsx index 688aaf2eb0..fe4047187f 100644 --- a/gui/packages/desktop/src/renderer/app.tsx +++ b/gui/packages/desktop/src/renderer/app.tsx @@ -99,6 +99,10 @@ export default class AppRenderer { this.onDaemonDisconnected(errorMessage ? new Error(errorMessage) : undefined); }); + IpcRendererEventChannel.accountHistory.listen((newAccountHistory: AccountToken[]) => { + this.setAccountHistory(newAccountHistory); + }); + IpcRendererEventChannel.tunnel.listen((newState: TunnelStateTransition) => { this.setTunnelState(newState); }); @@ -141,6 +145,7 @@ export default class AppRenderer { this.settings = initialState.settings; this.guiSettings = initialState.guiSettings; + this.setAccountHistory(initialState.accountHistory); this.setTunnelState(initialState.tunnelState); this.setSettings(initialState.settings); @@ -258,8 +263,7 @@ export default class AppRenderer { } public async removeAccountFromHistory(accountToken: AccountToken): Promise<void> { - await IpcRendererEventChannel.accountHistory.removeItem(accountToken); - await this.fetchAccountHistory(); + return IpcRendererEventChannel.accountHistory.removeItem(accountToken); } public async setAllowLan(allowLan: boolean) { @@ -362,21 +366,9 @@ export default class AppRenderer { } } - private async fetchAccountHistory(): Promise<void> { - const accountHistory = await IpcRendererEventChannel.accountHistory.get(); - - this.reduxActions.account.updateAccountHistory(accountHistory); - } - private async onDaemonConnected() { this.connectedToDaemon = true; - try { - await this.fetchAccountHistory(); - } catch (error) { - log.error(`Cannot fetch the account history: ${error.message}`); - } - if (this.settings.accountToken) { this.memoryHistory.replace('/connect'); @@ -424,6 +416,10 @@ export default class AppRenderer { } } + private setAccountHistory(accountHistory: AccountToken[]) { + this.reduxActions.account.updateAccountHistory(accountHistory); + } + private setTunnelState(tunnelState: TunnelStateTransition) { const actions = this.reduxActions; diff --git a/gui/packages/desktop/src/shared/ipc-event-channel.ts b/gui/packages/desktop/src/shared/ipc-event-channel.ts index c8cb1bf4c8..02480d8150 100644 --- a/gui/packages/desktop/src/shared/ipc-event-channel.ts +++ b/gui/packages/desktop/src/shared/ipc-event-channel.ts @@ -17,6 +17,7 @@ import { export interface IAppStateSnapshot { isConnected: boolean; autoStart: boolean; + accountHistory: AccountToken[]; tunnelState: TunnelStateTransition; settings: ISettings; location?: ILocation; @@ -38,17 +39,17 @@ interface IReceiver<T> { listen(fn: (value: T) => void): void; } -interface ITunnelMethods { +interface ITunnelMethods extends IReceiver<TunnelStateTransition> { connect(): Promise<void>; disconnect(): Promise<void>; } -interface ITunnelHandlers { +interface ITunnelHandlers extends ISender<TunnelStateTransition> { handleConnect(fn: () => Promise<void>): void; handleDisconnect(fn: () => Promise<void>): void; } -interface ISettingsMethods { +interface ISettingsMethods extends IReceiver<ISettings> { setAllowLan(allowLan: boolean): Promise<void>; setEnableIpv6(enableIpv6: boolean): Promise<void>; setBlockWhenDisconnected(block: boolean): Promise<void>; @@ -56,7 +57,7 @@ interface ISettingsMethods { updateRelaySettings(update: RelaySettingsUpdate): Promise<void>; } -interface ISettingsHandlers { +interface ISettingsHandlers extends ISender<ISettings> { handleAllowLan(fn: (allowLan: boolean) => Promise<void>): void; handleEnableIpv6(fn: (enableIpv6: boolean) => Promise<void>): void; handleBlockWhenDisconnected(fn: (block: boolean) => Promise<void>): void; @@ -64,13 +65,13 @@ interface ISettingsHandlers { handleUpdateRelaySettings(fn: (update: RelaySettingsUpdate) => Promise<void>): void; } -interface IGuiSettingsMethods { +interface IGuiSettingsMethods extends IReceiver<IGuiSettingsState> { setAutoConnect(autoConnect: boolean): void; setStartMinimized(startMinimized: boolean): void; setMonochromaticIcon(monochromaticIcon: boolean): void; } -interface IGuiSettingsHandlers { +interface IGuiSettingsHandlers extends ISender<IGuiSettingsState> { handleAutoConnect(fn: (autoConnect: boolean) => void): void; handleStartMinimized(fn: (startMinimized: boolean) => void): void; handleMonochromaticIcon(fn: (monochromaticIcon: boolean) => void): void; @@ -88,21 +89,19 @@ interface IAccountMethods { getData(token: AccountToken): Promise<IAccountData>; } -interface IAccountHistoryHandlers { - handleGet(fn: () => Promise<AccountToken[]>): void; +interface IAccountHistoryHandlers extends ISender<AccountToken[]> { handleRemoveItem(fn: (token: AccountToken) => Promise<void>): void; } -interface IAccountHistoryMethods { - get(): Promise<AccountToken[]>; +interface IAccountHistoryMethods extends IReceiver<AccountToken[]> { removeItem(token: AccountToken): Promise<void>; } -interface IAutoStartMethods { +interface IAutoStartMethods extends IReceiver<boolean> { set(autoStart: boolean): Promise<void>; } -interface IAutoStartHandlers { +interface IAutoStartHandlers extends ISender<boolean> { handleSet(fn: (value: boolean) => Promise<void>): void; } @@ -134,7 +133,7 @@ const SET_START_MINIMIZED = 'set-start-minimized'; const GET_APP_STATE = 'get-app-state'; -const GET_ACCOUNT_HISTORY = 'get-account-history'; +const ACCOUNT_HISTORY_CHANGED = 'account-history-changed'; const REMOVE_ACCOUNT_HISTORY_ITEM = 'remove-account-history-item'; const SET_ACCOUNT = 'set-account'; @@ -165,13 +164,13 @@ export class IpcRendererEventChannel { listen: listen(DAEMON_DISCONNECTED), }; - public static tunnel: IReceiver<TunnelStateTransition> & ITunnelMethods = { + public static tunnel: ITunnelMethods = { listen: listen(TUNNEL_STATE_CHANGED), connect: requestSender(CONNECT_TUNNEL), disconnect: requestSender(DISCONNECT_TUNNEL), }; - public static settings: IReceiver<ISettings> & ISettingsMethods = { + public static settings: ISettingsMethods = { listen: listen(SETTINGS_CHANGED), setAllowLan: requestSender(SET_ALLOW_LAN), setEnableIpv6: requestSender(SET_ENABLE_IPV6), @@ -196,14 +195,14 @@ export class IpcRendererEventChannel { listen: listen(UPGRADE_VERSION_CHANGED), }; - public static guiSettings: IReceiver<IGuiSettingsState> & IGuiSettingsMethods = { + public static guiSettings: IGuiSettingsMethods = { listen: listen(GUI_SETTINGS_CHANGED), setAutoConnect: set(SET_AUTO_CONNECT), setMonochromaticIcon: set(SET_MONOCHROMATIC_ICON), setStartMinimized: set(SET_START_MINIMIZED), }; - public static autoStart: IReceiver<boolean> & IAutoStartMethods = { + public static autoStart: IAutoStartMethods = { listen: listen(AUTO_START_CHANGED), set: requestSender(SET_AUTO_START), }; @@ -215,7 +214,7 @@ export class IpcRendererEventChannel { }; public static accountHistory: IAccountHistoryMethods = { - get: requestSender(GET_ACCOUNT_HISTORY), + listen: listen(ACCOUNT_HISTORY_CHANGED), removeItem: requestSender(REMOVE_ACCOUNT_HISTORY_ITEM), }; } @@ -237,7 +236,7 @@ export class IpcMainEventChannel { notify: sender(DAEMON_DISCONNECTED), }; - public static tunnel: ISender<TunnelStateTransition> & ITunnelHandlers = { + public static tunnel: ITunnelHandlers = { notify: sender(TUNNEL_STATE_CHANGED), handleConnect: requestHandler(CONNECT_TUNNEL), handleDisconnect: requestHandler(DISCONNECT_TUNNEL), @@ -247,7 +246,7 @@ export class IpcMainEventChannel { notify: sender(LOCATION_CHANGED), }; - public static settings: ISender<ISettings> & ISettingsHandlers = { + public static settings: ISettingsHandlers = { notify: sender(SETTINGS_CHANGED), handleAllowLan: requestHandler(SET_ALLOW_LAN), handleEnableIpv6: requestHandler(SET_ENABLE_IPV6), @@ -268,14 +267,14 @@ export class IpcMainEventChannel { notify: sender(UPGRADE_VERSION_CHANGED), }; - public static guiSettings: ISender<IGuiSettingsState> & IGuiSettingsHandlers = { + public static guiSettings: IGuiSettingsHandlers = { notify: sender(GUI_SETTINGS_CHANGED), handleAutoConnect: handler(SET_AUTO_CONNECT), handleMonochromaticIcon: handler(SET_MONOCHROMATIC_ICON), handleStartMinimized: handler(SET_START_MINIMIZED), }; - public static autoStart: ISender<boolean> & IAutoStartHandlers = { + public static autoStart: IAutoStartHandlers = { notify: sender<boolean>(AUTO_START_CHANGED), handleSet: requestHandler(SET_AUTO_START), }; @@ -287,7 +286,7 @@ export class IpcMainEventChannel { }; public static accountHistory: IAccountHistoryHandlers = { - handleGet: requestHandler(GET_ACCOUNT_HISTORY), + notify: sender<AccountToken[]>(ACCOUNT_HISTORY_CHANGED), handleRemoveItem: requestHandler(REMOVE_ACCOUNT_HISTORY_ITEM), }; } |
