This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.js
150 lines (122 loc) · 5.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
const electron = require('electron');
const fs = require('fs');
const path = require('path');
const url = require('url');
const util = require('util');
const addMenu = require('./menu');
const { cli } = require('./cli');
const doesParityExist = require('./operations/doesParityExist');
const fetchParity = require('./operations/fetchParity');
const handleError = require('./operations/handleError');
const messages = require('./messages');
const { killParity } = require('./operations/runParity');
const { getLocalDappsPath } = require('./utils/paths');
const { name: appName } = require('../package.json');
const { app, BrowserWindow, ipcMain, session } = electron;
const fsExists = util.promisify(fs.stat); // eslint-disable-line
const fsMkdir = util.promisify(fs.mkdir);
let mainWindow;
function createWindow () {
// Will send these variables to renderers via IPC
global.dirName = __dirname;
global.wsInterface = cli.wsInterface;
global.wsPort = cli.wsPort;
mainWindow = new BrowserWindow({
height: 800,
width: 1200
});
const localDappsPath = getLocalDappsPath();
fsExists(localDappsPath)
.catch(() => fsMkdir(localDappsPath));
doesParityExist()
.catch(() => fetchParity(mainWindow)) // Install parity if not present
.catch(handleError); // Errors should be handled before, this is really just in case
if (cli.uiDev === true) {
// Opens http://127.0.0.1:3000 in --ui-dev mode
mainWindow.loadURL('http://127.0.0.1:3000');
mainWindow.webContents.openDevTools();
} else {
// Opens file:///path/to/.build/index.html in prod mode
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, '..', '.build', 'index.html'),
protocol: 'file:',
slashes: true
})
);
}
// Listen to messages from renderer process
ipcMain.on('asynchronous-message', messages);
// Add application menu
addMenu(mainWindow);
// WS calls have Origin `file://` by default, which is not trusted.
// We override Origin header on all WS connections with an authorized one.
session.defaultSession.webRequest.onBeforeSendHeaders({
urls: ['ws://*/*', 'wss://*/*']
}, (details, callback) => {
details.requestHeaders.Origin = `parity://${mainWindow.id}.ui.parity`;
callback({ requestHeaders: details.requestHeaders });
});
// Do not accept all kind of web permissions (camera, location...)
// https://electronjs.org/docs/tutorial/security#4-handle-session-permission-requests-from-remote-content
session.defaultSession
.setPermissionRequestHandler((webContents, permission, callback) => {
if (!webContents.getURL().startsWith('file:')) {
// Denies the permissions request for all non-file://. Currently all
// network dapps are loaded on http://127.0.0.1:8545, so they won't
// have any permissions.
return callback(false);
}
// All others loaded on file:// (shell, builtin, local) can have those
// permissions.
return callback(true);
});
// Verify WebView Options Before Creation
// https://electronjs.org/docs/tutorial/security#12-verify-webview-options-before-creation
mainWindow.webContents.on('will-attach-webview', (event, webPreferences, params) => {
// Strip away inline preload scripts, ours is at preloadURL
delete webPreferences.preload;
// TODO Verify that the location of webPreferences.preloadURL is:
// `file://path/to/app.asar/.build/preload.js`
// Disable Node.js integration
webPreferences.nodeIntegration = false;
webPreferences.contextIsolation = true;
});
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
killParity();
app.quit();
}
});
// Make sure parity stops when UI stops
app.on('before-quit', killParity);
app.on('will-quit', killParity);
app.on('quit', killParity);
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
// userData value is derived from the Electron app name by default. However,
// Electron doesn't know the app name defined in package.json because we
// execute Electron directly on a file. Running Electron on a folder (either
// .build/ or electron/) doesn't solve the issue because the package.json
// is located in the parent directory.
app.setPath('userData', path.join(app.getPath('appData'), appName));