2 const {app, ipcMain, pwrt} = require('electron');
3 const IPC_MESSAGE = require('./ipc_message');
4 const WAS_EVENT = require('./was_event');
5 const WebApplication = require('./web_application');
6 const events = require('./events');
7 const runtime_debug = require('debug')('RUNTIME');
8 const try_debug = require('debug')('TRY');
11 constructor(options) {
12 this.webApplication = null;
13 this.quitting = false;
14 this.handleWasEvents();
15 this.handleIpcMessages();
16 this.isLaunched = false;
19 app.on('before-quit', function(event) {
20 runtime_debug('before-quit');
21 _this.quitting = true;
23 app.on('will-quit', function(event) {
24 runtime_debug('will-quit');
26 app.on('quit', function(event) {
27 return runtime_debug('quit');
29 app.on('browser-window-blur', function() {
30 return runtime_debug('browser-window-blur');
32 app.on('browser-window-focus', function() {
33 return runtime_debug('browser-window-focus');
35 app.on('browser-window-created', function() {
36 return runtime_debug('browser-window-created');
38 app.on('gpu-process-crashed', function() {
39 return runtime_debug('gpu-process-crashed');
41 app.on('window-all-closed', function(event) {
42 return runtime_debug('window-all-closed');
44 app.on('will-finish-launching', function(event) {
45 runtime_debug('will-finish-launching');
46 if (pwrt.isElectronLaunch()) {
47 console.log("Electron App launch");
48 let filePath = pwrt.getPath();
49 let pkgJson = require(filePath.substr(7, filePath.length - 12));
50 let mainJsPath = filePath.substr(7, filePath.length - 19) +
51 (pkgJson.main || 'index.js');
53 const Module = require('module');
54 Module._load(mainJsPath, Module, true);
57 app.on('ready', function(event) {
58 runtime_debug('ready');
59 if (pwrt.isElectronLaunch()) {
62 this.webApplication = new WebApplication(options);
65 onPause(web_window_id) {
66 runtime_debug('onPause : ' + web_window_id);
68 return this.webApplication.suspend(web_window_id);
70 try_debug('catch e : ', e.message);
73 onResume(web_window_id) {
74 runtime_debug('onResume : ' + web_window_id);
76 return this.webApplication.resume(web_window_id);
78 try_debug('catch e : ', e.message);
82 onLanguageChanged() {}
86 events.on(WAS_EVENT.WEBAPPLICATION.RESUME, (sender, id) => {
87 runtime_debug('handleWasMessages: focus ' + id);
88 return _this.onResume(id);
90 events.on(WAS_EVENT.WEBAPPLICATION.SUSPEND, (sender, id) => {
91 return _this.onPause(id);
96 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_FINISH_LOAD, (sender, id) => {
97 runtime_debug('handleWasMessages: did-finish-load ' + id);
98 return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FINISH_LOAD, id);
100 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_FAIL_LOAD, function(sender, id, params) {
101 runtime_debug('handleWasMessages: did-fail-load ' + id);
102 runtime_debug(' params:' + JSON.stringify(params));
103 return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FAIL_LOAD, id, params);
105 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_FRAME_FINISH_LOAD, function(sender, id, params) {
106 runtime_debug('handleWasMessages: did-frame-finish-load ' + id);
107 runtime_debug(' params:' + JSON.stringify(params));
108 return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FRAME_FINISH_LOAD, id, params);
110 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_START_LOADING, (sender, id) => {
111 runtime_debug('handleWasMessages: did-start-loading ' + id);
112 return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_START_LOADING, id);
114 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_STOP_LOADING, (sender, id) => {
115 runtime_debug('handleWasMessages: did-stop-loading ' + id);
116 return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_STOP_LOADING, id);
118 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_GET_RESPONSE_DETAILS, function(sender, id, params) {
119 runtime_debug('handleWasMessages: did-get-response-details ' + id);
120 runtime_debug(' params:' + JSON.stringify(params));
121 return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_GET_RESPONSE_DETAILS, id, params);
123 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DID_GET_REDIRECT_REQUEST, function(sender, id, params) {
124 runtime_debug('handleWasMessages: did-get-redirect-request ' + id);
125 runtime_debug(' params:' + JSON.stringify(params));
126 return app.emit(IPC_MESSAGE.WEBCONTENTS.DID_GET_REDIRECT_REQUEST, id, params);
128 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DOM_READY, (sender, id) => {
129 runtime_debug('handleWasMessages: dom-ready ' + id);
130 return app.emit(IPC_MESSAGE.WEBCONTENTS.DOM_READY, id);
132 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.PAGE_FAVICON_UPDATED, function(sender, id, params) {
133 runtime_debug('handleWasMessages: page-favicon-updated ' + id);
134 runtime_debug(' params:' + JSON.stringify(params));
135 return app.emit(IPC_MESSAGE.WEBCONTENTS.PAGE_FAVICON_UPDATED, id, params);
137 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.NEW_WINDOW, function(sender, id, params) {
138 runtime_debug('handleWasMessages: new-window ' + id);
139 runtime_debug(' params:' + JSON.stringify(params));
140 return app.emit(IPC_MESSAGE.WEBCONTENTS.NEW_WINDOW, id, params);
142 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.CRASHED, (sender, id) => {
143 runtime_debug('handleWasMessages: crashed ' + id);
144 return app.emit(IPC_MESSAGE.WEBCONTENTS.CRASHED, id);
146 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.PLUGIN_CRASHED, function(sender, id, params) {
147 runtime_debug('handleWasMessages: plugin-crashed ' + id);
148 runtime_debug(' params:' + JSON.stringify(params));
149 return app.emit(IPC_MESSAGE.WEBCONTENTS.PLUGIN_CRASHED, id, params);
151 ipcMain.on(IPC_MESSAGE.WEBCONTENTS.DESTROYED, (sender, id) => {
152 runtime_debug('handleWasMessages: destroyed ' + id);
153 return app.emit(IPC_MESSAGE.WEBCONTENTS.DESTROYED, id);
157 module.exports = Runtime;