Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Develop into master #1516

Merged
merged 14 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix webview ids (#1513)
* updated electron to 1.4.10

* fixed webview ids and mist freezing

* removes sockets immediately after disconnecting, so it can create new ones next time

* reset sockets were not properly done

* fix es lint errors
  • Loading branch information
frozeman authored Dec 14, 2016
commit 28f09bed7d832d3650917c1ba2dbf2dfcd629caa
5 changes: 5 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ settings:
import/core-modules: ## don't lint for these missing packages in package.json
- electron ## 'electron' is only needed as devDependency / global installation


rules:
# "off" or 0 - turn the rule off
# "warn" or 1 - turn the rule on as a warning (doesn’t affect exit code)
# "error" or 2 - turn the rule on as an error (exit code is 1 when triggered)
indent:
- 2 ## error
- 4 ## number of spaces
no-console: 0 ## allowed for chrome dev-console
no-underscore-dangle: 0

globals:
i18n: true # don't warn about missing 'i18n' declaration
Expand Down
4 changes: 1 addition & 3 deletions interface/client/mistAPIBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ mistAPIBackend = function (event) {
// console.trace('mistAPIBackend event', event);

if (event.channel === 'setWebviewId') {
Tabs.update(template.data._id, { $set: {
webviewId: webview.getId(),
} });
Tabs.update(template.data._id, { $set: { webviewId: webview.getWebContents().id }});
}

// Send TEST DATA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ Template['popupWindows_sendTransactionConfirmation'].onCreated(function(){
});

Template['popupWindows_sendTransactionConfirmation'].onRendered(function(){
this.$('input[type="password"]').focus();
var template = this;

Meteor.setTimeout(function(){
template.$('input[type="password"]').focus();
}, 200);
});

Template['popupWindows_sendTransactionConfirmation'].helpers({
Expand Down
5 changes: 5 additions & 0 deletions interface/client/templates/webviewEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ webviewChangeUrl = function(tabId, e){

console.log(e.type, tabId, url);

if(e.type === 'did-navigate') {
// destroy socket when navigating away
ipc.send('ipcProvider-destroy', this.getWebContents().id);
}

// make sure to not store error pages in history
if(!url || url.indexOf('mist/errorPages/') !== -1)
return;
Expand Down
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const timesync = require('os-timesync');
const dbSync = require('./modules/dbSync.js');
const i18n = require('./modules/i18n.js');
const logger = require('./modules/utils/logger');
const Sockets = require('./modules/sockets');
const Sockets = require('./modules/socketManager');
const Windows = require('./modules/windows');
const ClientBinaryManager = require('./modules/clientBinaryManager');
const UpdateChecker = require('./modules/updateChecker');
Expand Down
2 changes: 1 addition & 1 deletion modules/ethereumNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Settings = require('./settings');
const log = require('./utils/logger').create('EthereumNode');
const logRotate = require('log-rotate');
const EventEmitter = require('events').EventEmitter;
const Sockets = require('./sockets');
const Sockets = require('./socketManager');
const ClientBinaryManager = require('./clientBinaryManager');

const DEFAULT_NODE_TYPE = 'geth';
Expand Down
77 changes: 45 additions & 32 deletions modules/ipc/dechunker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,64 @@ The dechunker module gets IPC buffers and tries to decode them.

const _ = require('underscore');

let lastChunk = null;
let lastChunkTimeout = null;

/**
The dechunker module gets IPC buffers and tries to decode them.

@method dechunker
*/
module.exports = function (data, callback) {
data = data.toString();
module.exports = class Dechunker {
constructor() {
this.lastChunk = null;
this.lastChunkTimeout = null;
}

// DE-CHUNKER
const dechunkedData = data
.replace(/\}[\n\r]?\{/g, '}|--|{') // }{
.replace(/\}\][\n\r]?\[\{/g, '}]|--|[{') // }][{
.replace(/\}[\n\r]?\[\{/g, '}|--|[{') // }[{
.replace(/\}\][\n\r]?\{/g, '}]|--|{') // }]{
.split('|--|');
dechunk(data, callback) {
data = data.toString();

// DE-CHUNKER
const dechunkedData = data
.replace(/\][\n\r]?/g, ']') // ]
.replace(/\}[\n\r]?/g, '}') // }
.replace(/\}[\n\r]?\{/g, '}|--|{') // }{
.replace(/\}\][\n\r]?\[\{/g, '}]|--|[{') // }][{
.replace(/\}[\n\r]?\[\{/g, '}|--|[{') // }[{
.replace(/\}\][\n\r]?\{/g, '}]|--|{') // }]{
.split('|--|');

_.each(dechunkedData, (data) => {
// prepend the last chunk
if (lastChunk)
{ data = lastChunk + data; }

let result = data;
// if it couldn't be split, return error
if (!_.isArray(dechunkedData)) {
return callback(`Couldn't split data: ${data}`);
}

try {
result = JSON.parse(result);
} catch (e) {
lastChunk = data;
return _.each(dechunkedData, (data) => {
// prepend the last chunk
if (this.lastChunk)
{ data = this.lastChunk + data; }

// start timeout to cancel all requests
clearTimeout(lastChunkTimeout);
lastChunkTimeout = setTimeout(() => {
callback(`Couldn't decode data: ${data}`);
}, 1000 * 15);
let result = data;

return;
}
try {
result = JSON.parse(result);
} catch (e) {
this.lastChunk = data;

// start timeout to cancel all requests
clearTimeout(this.lastChunkTimeout);
this.lastChunkTimeout = setTimeout(() => {
callback(`Couldn't decode data: ${data}`);
}, 1000 * 15);

return;
}

// cancel timeout and set chunk to null
clearTimeout(lastChunkTimeout);
lastChunk = null;
// cancel timeout and set chunk to null
clearTimeout(this.lastChunkTimeout);
this.lastChunkTimeout = null;
this.lastChunk = null;

callback(null, result);
});
callback(null, result);
});
}
};
30 changes: 15 additions & 15 deletions modules/ipc/ipcProviderBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const fs = require('fs');
const path = require('path');

const log = require('../utils/logger').create('ipcProviderBackend');
const Sockets = require('../sockets');
const Sockets = require('../socketManager');
const Settings = require('../settings');
const ethereumNode = require('../ethereumNode');
const Windows = require('../windows');
Expand Down Expand Up @@ -70,7 +70,7 @@ class IpcProviderBackend {
*/
_getOrCreateConnection(event) {
const owner = event.sender;
const ownerId = owner.getId();
const ownerId = owner.id;

let socket;

Expand All @@ -79,7 +79,7 @@ class IpcProviderBackend {
if (this._connections[ownerId]) {
socket = this._connections[ownerId].socket;
} else {
log.debug(`Get/create socket connection, id=${ownerId}`);
log.debug(`Create new socket connection, id=${ownerId}`);

socket = Sockets.get(ownerId, Settings.rpcMode);
}
Expand All @@ -99,11 +99,13 @@ class IpcProviderBackend {
log.debug(`Destroy socket connection due to event: ${ev}, id=${ownerId}`);

socket.destroy().finally(() => {
delete this._connections[ownerId];

if(!owner.isDestroyed())
owner.send(`ipcProvider-${ev}`, JSON.stringify(data));
});

delete this._connections[ownerId];
Sockets.remove(ownerId);
});
});

Expand Down Expand Up @@ -182,19 +184,17 @@ class IpcProviderBackend {
* Handle IPC call to destroy a connection.
*/
_destroyConnection(event) {
const ownerId = event.sender.getId();
const ownerId = event.sender.id;

return Q.try(() => {
if (this._connections[ownerId]) {
log.debug('Destroy socket connection', ownerId);
if (this._connections[ownerId]) {
log.debug('Destroy socket connection', ownerId);

this._connections[ownerId].owner.send('ipcProvider-setWritable', false);
this._connections[ownerId].owner.send('ipcProvider-setWritable', false);

return this._connections[ownerId].socket.destroy().finally(() => {
delete this._connections[ownerId];
});
}
});
this._connections[ownerId].socket.destroy();
delete this._connections[ownerId];
Sockets.remove(ownerId);
}
}


Expand Down Expand Up @@ -237,7 +237,7 @@ class IpcProviderBackend {
* @param {String} payload request payload.
*/
_sendRequest(isSync, event, payload) {
const ownerId = event.sender.getId();
const ownerId = event.sender.id;

log.trace('sendRequest', isSync ? 'sync' : 'async', ownerId, payload);

Expand Down
2 changes: 1 addition & 1 deletion modules/ipc/methods/eth_sendTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = class extends BaseProcessor {
});

ipc.once('backendAction_unlockedAccountAndSentTransaction', (ev, err, result) => {
if (Windows.getById(ev.sender.getId()) === modalWindow
if (Windows.getById(ev.sender.id) === modalWindow
&& !modalWindow.isClosed)
{
if (err || !result) {
Expand Down
12 changes: 6 additions & 6 deletions modules/ipcCommunicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ ipc.on('backendAction_openExternalUrl', (e, url) => {
});

ipc.on('backendAction_closePopupWindow', (e) => {
const windowId = e.sender.getId();
const windowId = e.sender.id;
const senderWindow = Windows.getById(windowId);

if (senderWindow) {
senderWindow.close();
}
});
ipc.on('backendAction_setWindowSize', (e, width, height) => {
const windowId = e.sender.getId();
const windowId = e.sender.id;
const senderWindow = Windows.getById(windowId);

if (senderWindow) {
Expand All @@ -59,7 +59,7 @@ ipc.on('backendAction_setWindowSize', (e, width, height) => {
});

ipc.on('backendAction_windowCallback', (e, value1, value2, value3) => {
const windowId = e.sender.getId();
const windowId = e.sender.id;
const senderWindow = Windows.getById(windowId);

if(senderWindow.callback) {
Expand All @@ -68,7 +68,7 @@ ipc.on('backendAction_windowCallback', (e, value1, value2, value3) => {
});

ipc.on('backendAction_windowMessageToOwner', (e, error, value) => {
const windowId = e.sender.getId();
const windowId = e.sender.id;
const senderWindow = Windows.getById(windowId);

if (senderWindow.ownerId) {
Expand Down Expand Up @@ -163,7 +163,7 @@ ipc.on('backendAction_importPresaleFile', (e, path, pw) => {

const createAccountPopup = (e) => {
Windows.createPopup('requestAccount', {
ownerId: e.sender.getId(),
ownerId: e.sender.id,
electronOptions: {
width: 400,
height: 230,
Expand All @@ -180,7 +180,7 @@ ipc.on('mistAPI_requestAccount', (e) => {
createAccountPopup(e);
} else { // Mist
Windows.createPopup('connectAccount', {
ownerId: e.sender.getId(),
ownerId: e.sender.id,
electronOptions: {
width: 460,
height: 497,
Expand Down
42 changes: 25 additions & 17 deletions modules/sockets.js → modules/socketManager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const _ = global._;
const Q = require('bluebird');
const EventEmitter = require('events').EventEmitter;
const log = require('./utils/logger').create('Sockets');
const net = require('net');
const dechunker = require('./ipc/dechunker.js');

const Web3IpcSocket = require('./sockets/web3Ipc');
const Web3HttpSocket = require('./sockets/web3Http');
Expand All @@ -17,6 +14,27 @@ class SocketManager {
this._sockets = {};
}

/**
* Get socket with given id, creating it if it does not exist.
*
* @return {Socket}
*/
create(id, type) {
log.debug(`Create socket, id=${id}, type=${type}`);

switch (type) {
case 'ipc':
this._sockets[id] = new Web3IpcSocket(this, id);
break;
case 'http':
this._sockets[id] = new Web3HttpSocket(this, id);
break;
default:
throw new Error(`Unrecognized socket type: ${type}`);
}

return this._sockets[id];
}

/**
* Get socket with given id, creating it if it does not exist.
Expand All @@ -25,18 +43,7 @@ class SocketManager {
*/
get(id, type) {
if (!this._sockets[id]) {
log.debug(`Create socket, id=${id}, type=${type}`);

switch (type) {
case 'ipc':
this._sockets[id] = new Web3IpcSocket(this, id);
break;
case 'http':
this._sockets[id] = new Web3HttpSocket(this, id);
break;
default:
throw new Error(`Unrecognized socket type: ${type}`);
}
this.create(id, type);
}

return this._sockets[id];
Expand All @@ -49,7 +56,8 @@ class SocketManager {
destroyAll() {
log.info('Destroy all sockets');

return Q.all(_.map(this._sockets, (s) => {
return Q.all(_.map(this._sockets, (s, id) => {
this.remove(id);
return s.destroy();
}));
}
Expand All @@ -59,7 +67,7 @@ class SocketManager {
*
* Usually called by `Socket` instances when they're destroyed.
*/
_remove(id) {
remove(id) {
log.debug(`Remove socket, id=${id}`);

delete this._sockets[id];
Expand Down
Loading