diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2018-11-19 15:43:50 +0100 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2018-11-20 10:49:40 +0100 |
| commit | eae5f1e410c6cd5e6f3ca533ac9fe14f69f9da56 (patch) | |
| tree | c41d74057c67e86b4cbb9e60f76d734be5344fe2 /gui | |
| parent | 95347a14761726976f86ebac5fa798b9e19b9243 (diff) | |
| download | mullvadvpn-eae5f1e410c6cd5e6f3ca533ac9fe14f69f9da56.tar.xz mullvadvpn-eae5f1e410c6cd5e6f3ca533ac9fe14f69f9da56.zip | |
Move location management to the main process
Diffstat (limited to 'gui')
| -rw-r--r-- | gui/packages/desktop/src/main/index.js | 28 | ||||
| -rw-r--r-- | gui/packages/desktop/src/renderer/app.js | 131 |
2 files changed, 87 insertions, 72 deletions
diff --git a/gui/packages/desktop/src/main/index.js b/gui/packages/desktop/src/main/index.js index 3c1d76459e..e7f56fce97 100644 --- a/gui/packages/desktop/src/main/index.js +++ b/gui/packages/desktop/src/main/index.js @@ -23,6 +23,7 @@ import { } from './daemon-rpc'; import type { AppVersionInfo, + Location, RelayList, Settings, TunnelState, @@ -66,6 +67,7 @@ const ApplicationMain = { _tunnelState: defaultTunnelStateTransition(), _settings: defaultSettings(), + _location: (null: ?Location), _relays: ({ countries: [] }: RelayList), _relaysInterval: (null: ?IntervalID), @@ -436,17 +438,16 @@ const ApplicationMain = { }, _setTunnelState(newState: TunnelStateTransition) { - const windowController = this._windowController; - this._tunnelState = newState; this._updateTrayIcon(newState.state); + this._updateLocation(newState.state); if (!this._shouldSuppressNotifications()) { this._notificationController.notifyTunnelState(newState); } - if (windowController) { - windowController.send('tunnel-state-changed', newState); + if (this._windowController) { + this._windowController.send('tunnel-state-changed', newState); } }, @@ -458,6 +459,14 @@ const ApplicationMain = { } }, + _setLocation(newLocation: Location) { + this._location = newLocation; + + if (this._windowController) { + this._windowController.send('location-changed', newLocation); + } + }, + _setRelays(newRelayList: RelayList) { this._relays = newRelayList; @@ -592,6 +601,16 @@ const ApplicationMain = { return this._windowController && this._windowController.isVisible(); }, + async _updateLocation(tunnelState: TunnelState) { + if (['connected', 'connecting', 'disconnected'].includes(tunnelState)) { + try { + this._setLocation(await this._daemonRpc.getLocation()); + } catch (error) { + log.error(`Failed to update the location: ${error.message}`); + } + } + }, + _updateTrayIcon(tunnelState: TunnelState) { const iconTypes: { [TunnelState]: TrayIconType } = { connected: 'secured', @@ -642,6 +661,7 @@ const ApplicationMain = { this._connectedToDaemon, this._tunnelState, this._settings, + this._location, this._relays, this._currentVersion, this._upgradeVersion, diff --git a/gui/packages/desktop/src/renderer/app.js b/gui/packages/desktop/src/renderer/app.js index 1c456fd7d5..93f70dc14c 100644 --- a/gui/packages/desktop/src/renderer/app.js +++ b/gui/packages/desktop/src/renderer/app.js @@ -27,13 +27,13 @@ import type { CurrentAppVersionInfo, AppUpgradeInfo } from '../main'; import type { AccountToken, - Settings, - TunnelStateTransition, + AccountData, + Location, RelayList, RelaySettingsUpdate, RelaySettings, - TunnelState, - AccountData, + Settings, + TunnelStateTransition, } from './lib/daemon-rpc-proxy'; import DaemonRpcProxy, { @@ -111,6 +111,10 @@ export default class AppRenderer { this._setSettings(newSettings); }); + ipcRenderer.on('location-changed', (_event: Event, location: Location) => { + this._setLocation(location); + }); + ipcRenderer.on('relays-changed', (_event: Event, newRelays: RelayList) => { this._setRelays(newRelays); }); @@ -134,12 +138,18 @@ export default class AppRenderer { isConnected: boolean, tunnelState: TunnelStateTransition, settings: Settings, + location: ?Location, relays: RelayList, currentVersion: CurrentAppVersionInfo, upgradeVersion: AppUpgradeInfo, ) => { this._setTunnelState(tunnelState); this._setSettings(settings); + + if (location) { + this._setLocation(location); + } + this._setRelays(relays); this._setCurrentVersion(currentVersion); this._setUpgradeVersion(upgradeVersion); @@ -323,28 +333,6 @@ export default class AppRenderer { actions.account.updateAccountHistory(accountHistory); } - _setRelays(relayList: RelayList) { - const locations = relayList.countries.map((country) => ({ - name: country.name, - code: country.code, - hasActiveRelays: country.cities.some((city) => city.relays.length > 0), - cities: country.cities.map((city) => ({ - name: city.name, - code: city.code, - latitude: city.latitude, - longitude: city.longitude, - hasActiveRelays: city.relays.length > 0, - relays: city.relays, - })), - })); - - this._reduxActions.settings.updateRelayLocations(locations); - } - - async _fetchLocation() { - this._reduxActions.connection.newLocation(await this._daemonRpc.getLocation()); - } - async setAllowLan(allowLan: boolean) { const actions = this._reduxActions; await this._daemonRpc.setAllowLan(allowLan); @@ -444,12 +432,37 @@ export default class AppRenderer { } _setTunnelState(tunnelState: TunnelStateTransition) { + const actions = this._reduxActions; + log.debug(`Tunnel state: ${tunnelState.state}`); this._tunnelState = tunnelState; - this._updateConnectionStatus(tunnelState); - this._updateUserLocation(tunnelState.state); + switch (tunnelState.state) { + case 'connecting': + actions.connection.connecting(tunnelState.details); + break; + + case 'connected': + actions.connection.connected(tunnelState.details); + break; + + case 'disconnecting': + actions.connection.disconnecting(tunnelState.details); + break; + + case 'disconnected': + actions.connection.disconnected(); + break; + + case 'blocked': + actions.connection.blocked(tunnelState.details); + break; + + default: + log.error(`Unexpected TunnelState: ${(tunnelState.state: empty)}`); + break; + } } _setSettings(newSettings: Settings) { @@ -465,52 +478,34 @@ export default class AppRenderer { this._setRelaySettings(newSettings.relaySettings); } - _setCurrentVersion(versionInfo: CurrentAppVersionInfo) { - this._reduxActions.version.updateVersion(versionInfo.gui, versionInfo.isConsistent); + _setLocation(location: Location) { + this._reduxActions.connection.newLocation(location); } - _setUpgradeVersion(upgradeVersion: AppUpgradeInfo) { - this._reduxActions.version.updateLatest(upgradeVersion); - } + _setRelays(relayList: RelayList) { + const locations = relayList.countries.map((country) => ({ + name: country.name, + code: country.code, + hasActiveRelays: country.cities.some((city) => city.relays.length > 0), + cities: country.cities.map((city) => ({ + name: city.name, + code: city.code, + latitude: city.latitude, + longitude: city.longitude, + hasActiveRelays: city.relays.length > 0, + relays: city.relays, + })), + })); - async _updateUserLocation(tunnelState: TunnelState) { - if (['connected', 'connecting', 'disconnected'].includes(tunnelState)) { - try { - await this._fetchLocation(); - } catch (error) { - log.error(`Failed to update the location: ${error.message}`); - } - } + this._reduxActions.settings.updateRelayLocations(locations); } - _updateConnectionStatus(stateTransition: TunnelStateTransition) { - const actions = this._reduxActions; - - switch (stateTransition.state) { - case 'connecting': - actions.connection.connecting(stateTransition.details); - break; - - case 'connected': - actions.connection.connected(stateTransition.details); - break; - - case 'disconnecting': - actions.connection.disconnecting(stateTransition.details); - break; - - case 'disconnected': - actions.connection.disconnected(); - break; - - case 'blocked': - actions.connection.blocked(stateTransition.details); - break; + _setCurrentVersion(versionInfo: CurrentAppVersionInfo) { + this._reduxActions.version.updateVersion(versionInfo.gui, versionInfo.isConsistent); + } - default: - log.error(`Unexpected TunnelStateTransition: ${(stateTransition.state: empty)}`); - break; - } + _setUpgradeVersion(upgradeVersion: AppUpgradeInfo) { + this._reduxActions.version.updateLatest(upgradeVersion); } } |
