summaryrefslogtreecommitdiffhomepage
path: root/app/lib
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2018-06-12 15:55:52 +0200
committerAndrej Mihajlov <and@mullvad.net>2018-06-12 15:55:52 +0200
commit55b5d390bc377b9ff6cdf5a0b1641ea2fae23719 (patch)
treefaeece30342d7f2688da64f74a1e983b284f04fa /app/lib
parentbb4304f397dc954d3aec6cb2139ca5ab9cc0bf06 (diff)
parent068ff217d75ec578aacb01fd441e23898919913b (diff)
downloadmullvadvpn-55b5d390bc377b9ff6cdf5a0b1641ea2fae23719.tar.xz
mullvadvpn-55b5d390bc377b9ff6cdf5a0b1641ea2fae23719.zip
Merge branch 'modernize-app'
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/jsonrpc-ws-ipc.js58
-rw-r--r--app/lib/problem-report.js71
-rw-r--r--app/lib/tray-icon-manager.js62
3 files changed, 47 insertions, 144 deletions
diff --git a/app/lib/jsonrpc-ws-ipc.js b/app/lib/jsonrpc-ws-ipc.js
index eb84607be1..ff73f4cc88 100644
--- a/app/lib/jsonrpc-ws-ipc.js
+++ b/app/lib/jsonrpc-ws-ipc.js
@@ -96,26 +96,22 @@ export default class Ipc {
this._closeConnectionHandler = handler;
}
- on(event: string, listener: (mixed) => void): Promise<*> {
- log.debug('Adding a listener to', event);
- return this.send(event + '_subscribe')
- .then((subscriptionId) => {
- if (typeof subscriptionId === 'string' || typeof subscriptionId === 'number') {
- this._subscriptions.set(subscriptionId, listener);
- } else {
- throw new InvalidReply(
- subscriptionId,
- 'The subscription id was not a string or a number',
- );
- }
- })
- .catch((e) => {
- log.error('Failed adding listener to', event, ':', e);
- });
+ async on(event: string, listener: (mixed) => void): Promise<*> {
+ log.silly(`Adding a listener to ${event}`);
+ try {
+ const subscriptionId = await this.send(`${event}_subscribe`);
+ if (typeof subscriptionId === 'string' || typeof subscriptionId === 'number') {
+ this._subscriptions.set(subscriptionId, listener);
+ } else {
+ throw new InvalidReply(subscriptionId, 'The subscription id was not a string or a number');
+ }
+ } catch (e) {
+ log.error(`Failed adding listener to ${event}: ${e.message}`);
+ }
}
send(action: string, data: mixed, timeout: number = DEFAULT_TIMEOUT_MILLIS): Promise<mixed> {
- return new Promise((resolve, reject) => {
+ return new Promise(async (resolve, reject) => {
const id = uuid.v4();
const params = this._prepareParams(data);
@@ -128,15 +124,14 @@ export default class Ipc {
message: jsonrpcMessage,
});
- this._getWebSocket()
- .then((ws) => {
- log.debug('Sending message', id, action);
- ws.send(jsonrpcMessage);
- })
- .catch((e) => {
- log.error('Failed sending RPC message "' + action + '":', e);
- reject(e);
- });
+ try {
+ const ws = await this._getWebSocket();
+ log.silly('Sending message', id, action);
+ ws.send(jsonrpcMessage);
+ } catch (e) {
+ log.error(`Failed sending RPC message "${action}": ${e.message}`);
+ reject(e);
+ }
});
}
@@ -159,7 +154,6 @@ export default class Ipc {
_getWebSocket(): Promise<WebSocket> {
return new Promise((resolve) => {
if (this._websocket && this._websocket.readyState === 1) {
- // Connected
resolve(this._websocket);
} else {
log.debug('Waiting for websocket to connect');
@@ -175,11 +169,11 @@ export default class Ipc {
this._unansweredRequests.delete(requestId);
if (!request) {
- log.debug(requestId, 'timed out but it seems to already have been answered');
+ log.warn(requestId, 'timed out but it seems to already have been answered');
return;
}
- log.debug(request.message, 'timed out');
+ log.warn(request.message, 'timed out');
request.reject(new TimeOutError(request.message));
}
@@ -199,7 +193,7 @@ export default class Ipc {
const listener = this._subscriptions.get(subscriptionId);
if (listener) {
- log.debug('Got notification', message.payload.method, message.payload.params.result);
+ log.silly('Got notification', message.payload.method, message.payload.params.result);
listener(message.payload.params.result);
} else {
log.warn('Got notification for', message.payload.method, 'but no one is listening for it');
@@ -216,7 +210,7 @@ export default class Ipc {
return;
}
- log.debug('Got answer to', id, message.type);
+ log.silly('Got answer to', id, message.type);
clearTimeout(request.timerId);
@@ -236,7 +230,7 @@ export default class Ipc {
this._websocket = this._websocketFactory(connectionString);
this._websocket.onopen = () => {
- log.debug('Websocket is connected');
+ log.info('Websocket is connected');
this._backoff.successfullyConnected();
while (this._onConnect.length > 0) {
diff --git a/app/lib/problem-report.js b/app/lib/problem-report.js
index 32acb32b93..df302bd908 100644
--- a/app/lib/problem-report.js
+++ b/app/lib/problem-report.js
@@ -1,71 +1,42 @@
// @flow
-import { resolveBin } from './proc';
-import { execFile } from 'child_process';
import { ipcRenderer } from 'electron';
-import { log } from './platform';
import uuid from 'uuid';
const collectProblemReport = (toRedact: Array<string>): Promise<string> => {
return new Promise((resolve, reject) => {
const requestId = uuid.v4();
- let responseListener: Function;
-
- const removeResponseListener = () => {
- ipcRenderer.removeListener('collect-logs-reply', responseListener);
- };
-
- // timeout after 10 seconds if no ipc response received
- const requestTimeout = setTimeout(() => {
- removeResponseListener();
- log.error('Timed out when collecting a problem report');
- reject(new Error('Timed out'));
- }, 10000);
-
- responseListener = (_event, id, error, reportPath) => {
- if (id !== requestId) {
- return;
- }
-
- clearTimeout(requestTimeout);
- removeResponseListener();
-
- if (error) {
- log.error(`Cannot collect a problem report: ${error.err}`);
- log.error(`Stdout: ${error.stdout}`);
- reject(error);
- } else {
- resolve(reportPath);
+ const responseListener = (_event, responseId, result) => {
+ if (responseId === requestId) {
+ ipcRenderer.removeListener('collect-logs-reply', responseListener);
+ if (result.success) {
+ resolve(result.reportPath);
+ } else {
+ reject(new Error(result.error));
+ }
}
};
- // add ipc response listener
ipcRenderer.on('collect-logs-reply', responseListener);
-
- // send ipc request
ipcRenderer.send('collect-logs', requestId, toRedact);
});
};
-const sendProblemReport = (email: string, message: string, savedReport: string) => {
- const args = ['send', '--email', email, '--message', message, '--report', savedReport];
-
- const binPath = resolveBin('problem-report');
-
+const sendProblemReport = (email: string, message: string, savedReport: string): Promise<void> => {
return new Promise((resolve, reject) => {
- execFile(binPath, args, { windowsHide: true }, (err, stdout, stderr) => {
- if (err) {
- reject({ err, stdout, stderr });
- } else {
- log.debug('Report sent');
- resolve();
+ const requestId = uuid.v4();
+ const responseListener = (_event, responseId, result) => {
+ if (requestId === responseId) {
+ ipcRenderer.removeListener('send-problem-report-reply', responseListener);
+ if (result.success) {
+ resolve();
+ } else {
+ reject(new Error(result.error));
+ }
}
- });
- }).catch((e) => {
- const { err, stdout } = e;
- log.error('Failed sending problem report', err);
- log.error(' stdout: ' + stdout);
+ };
- throw e;
+ ipcRenderer.on('send-problem-report-reply', responseListener);
+ ipcRenderer.send('send-problem-report', requestId, email, message, savedReport);
});
};
diff --git a/app/lib/tray-icon-manager.js b/app/lib/tray-icon-manager.js
deleted file mode 100644
index 915bd2c8b8..0000000000
--- a/app/lib/tray-icon-manager.js
+++ /dev/null
@@ -1,62 +0,0 @@
-// @flow
-import path from 'path';
-import KeyframeAnimation from './keyframe-animation';
-
-import type { Tray } from 'electron';
-
-export type TrayIconType = 'unsecured' | 'securing' | 'secured';
-
-export default class TrayIconManager {
- _animation: ?KeyframeAnimation;
- _iconType: TrayIconType;
-
- constructor(tray: Tray, initialType: TrayIconType) {
- const animation = this._createAnimation();
- animation.onFrame = (img) => tray.setImage(img);
- animation.reverse = this._isReverseAnimation(initialType);
- animation.play({ advanceTo: 'end' });
-
- this._animation = animation;
- this._iconType = initialType;
- }
-
- destroy() {
- if (this._animation) {
- this._animation.stop();
- this._animation = null;
- }
- }
-
- _createAnimation(): KeyframeAnimation {
- const basePath = path.join(path.resolve(__dirname, '..'), 'assets/images/menubar icons');
- const filePath = path.join(basePath, 'lock-{}.png');
- const animation = KeyframeAnimation.fromFilePattern(filePath, [1, 10]);
- animation.speed = 100;
- return animation;
- }
-
- _isReverseAnimation(type: TrayIconType): boolean {
- return type === 'unsecured';
- }
-
- get iconType(): TrayIconType {
- return this._iconType;
- }
-
- set iconType(type: TrayIconType) {
- if (this._iconType === type || !this._animation) {
- return;
- }
-
- const animation = this._animation;
- if (type === 'secured') {
- animation.reverse = true;
- animation.play({ beginFromCurrentState: true, startFrame: 8, endFrame: 9 });
- } else {
- animation.reverse = this._isReverseAnimation(type);
- animation.play({ beginFromCurrentState: true });
- }
-
- this._iconType = type;
- }
-}