Newer
Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
import { FetherWindowInstance } from "./types";
import parityElectron, {
getParityPath,
fetchParity,
} from "@parity/electron";
import electron from "electron";
import getRemainingArgs from "commander-remaining-args";
import path from "path";
import url from "url";
import addMenu from "./menu";
import cli from "./cli";
import handleError from "./utils/handleError";
import messages from "./messages";
import { parity } from "../../package.json";
import Pino from "./utils/pino";
import { productName } from "../../electron-builder.json";
import staticPath from "./utils/staticPath";
const { app, BrowserWindow, ipcMain, session } = electron;
// Disable gpu acceleration on linux
// https://github.com/parity-js/fether/issues/85
if (!["darwin", "win32"].includes(process.platform)) {
app.disableHardwareAcceleration();
}
class FetherWindow implements FetherWindowInstance {
private fetherWindow;
create(): void {
if (hasCalledInitFetherWindow) {
throw new Error("Unable to initialise Fether window more than once");
}
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
pino.info(`Starting ${productName}...`);
this.fetherWindow = new BrowserWindow({
height: 640,
resizable: false,
width: 360
});
// Set options for @parity/electron
parityElectron({
logger: namespace => log => Pino({ name: namespace }).info(log)
});
// Look if Parity is installed
getParityPath()
.catch(() =>
// Install parity if not present
fetchParity(this.fetherWindow, {
onProgress: progress =>
// Notify the renderers on download progress
this.fetherWindow.webContents.send(
"parity-download-progress",
progress
),
parityChannel: parity.channel
})
)
.then(async () => {
// Run parity when installed
// Don't run parity if the user ran fether with --no-run-parity
if (!cli.runParity) {
return;
}
if (
await isParityRunning({
wsInterface: cli.wsInterface,
wsPort: cli.wsPort
})
) {
return;
}
return runParity({
flags: [
...getRemainingArgs(cli),
"--light",
"--chain",
cli.chain,
"--ws-interface",
cli.wsInterface,
"--ws-port",
cli.wsPort
],
onParityError: err =>
handleError(err, "An error occured with Parity.")
});
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
151
.then(() => {
// Notify the renderers
this.fetherWindow.webContents.send("parity-running", true);
global.isParityRunning = true; // Send this variable to renderes via IPC
})
.catch(handleError);
// Globals for fether-react parityStore
global.wsInterface = cli.wsInterface;
global.wsPort = cli.wsPort;
// Opens file:///path/to/build/index.html in prod mode, or whatever is
// passed to ELECTRON_START_URL
this.fetherWindow.loadURL(
process.env.ELECTRON_START_URL ||
url.format({
pathname: path.join(staticPath, "build", "index.html"),
protocol: "file:",
slashes: true
})
);
// Listen to messages from renderer process
ipcMain.on("asynchronous-message", (...args) =>
messages(this.fetherWindow, ...args)
);
// Add application menu
addMenu(this.fetherWindow);
// 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) => {
if (!this.fetherWindow) {
// There might be a split second where the user closes the app, so
// this.fetherWindow is null, but there is still a network request done.
return;
}
details.requestHeaders.Origin = `parity://${
this.fetherWindow.id
}.ui.parity`;
callback({ requestHeaders: details.requestHeaders }); // eslint-disable-line
// Open external links in browser
this.fetherWindow.webContents.on("new-window", function(event, url) {
event.preventDefault();
electron.shell.openExternal(url);
});
this.fetherWindow.on("closed", () => {
this.fetherWindow = null;
});
}
const fetherWindowInstance = new FetherWindow();
fetherWindowInstance.create();
app.on("ready", fetherWindowInstance.create);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
});
// 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 (this.fetherWindow === null) {
fetherWindowInstance.create();