class WebApplication {
constructor(options) {
- let winopt = this.getBrowserWindowOption(options);
- console.log('opt: ' + JSON.stringify(winopt));
- this.mainWindow = new BrowserWindow(winopt);
- if (options.devMode) {
- this.mainWindow.webContents.openDevTools({
- detached: true
- });
- }
- console.log('mainWindow id : ' + this.mainWindow.id);
+ this.initialize(options);
+ this.createMainWindow(options);
+ }
+ initialize(options) {
this.pendingID = 0;
this.pendingCallbacks = new Map();
- this.handleEvents(winopt);
+ this.windowList = [];
this.firstRendered = false;
this.backgroundSupport = wrt.getBackgroundSupport();
this.multitaskingSupport = wrt.getMultitaskingSupport();
this.preloadState = 'none';
}
this.accessiblePath = wrt.getAccessiblePath();
- console.log(this.accessiblePath);
+
+ let self = this;
+ app.on('browser-window-created', function(event, window) {
+ if (self.windowList.length > 0)
+ self.windowList[self.windowList.length - 1].hide();
+ self.windowList.push(window);
+ console.log(`window created : #${self.windowList.length}`);
+
+ window.on('closed', function() {
+ console.log(`window closed : #${self.windowList.length}`);
+ let index = self.windowList.indexOf(window);
+ self.windowList.splice(index, 1);
+ if (index === self.windowList.length && self.windowList.length > 0)
+ self.windowList[self.windowList.length - 1].show();
+ });
+ });
+ app.on('web-contents-created', function(event, webContents) {
+ webContents.on('crashed', function() {
+ console.error('webContents crashed');
+ app.exit(100);
+ });
+ webContents.session.setPermissionRequestHandler(function(webContents, permission, callback) {
+ console.log(`handlePermissionRequests for ${permission}`);
+ if (permission === 'notifications') {
+ const id = ++self.pendingID;
+ console.log(`Raising a notification permission request with id: ${id}`);
+ self.pendingCallbacks.set(id, callback);
+ wrt.handleNotificationPermissionRequest(id, webContents);
+ } else if (permission === 'media') {
+ const id = ++self.pendingID;
+ console.log(`Raising a media permission request with id: ${id}`);
+ self.pendingCallbacks.set(id, callback);
+ wrt.handleMediaPermissionRequest(id, webContents);
+ } else {
+ /* electron by default allows permission for all if no request handler
+ is there; so granting permission only temporarily to not have any
+ side effects */
+ callback(true);
+ }
+ });
+ });
+ app.on('certificate-error', function(event, webContents, url, error, certificate, callback) {
+ console.log('A certificate error has occurred');
+ event.preventDefault();
+ if (certificate.data) {
+ const id = ++self.pendingID;
+ console.log(`Raising a certificate error response with id: ${id}`);
+ self.pendingCallbacks.set(id, callback);
+ wrt.handleCertificateError(id, webContents, certificate.data, url);
+ } else {
+ console.log('Certificate could not be opened');
+ callback(false);
+ }
+ });
+ app.on('login', function(event, webContents, request, authInfo, callback) {
+ console.log('Login info is required');
+ event.preventDefault();
+ const id = ++self.pendingID;
+ console.log(`Raising a login info request with id: ${id}`);
+ self.pendingCallbacks.set(id, callback);
+ wrt.handleAuthRequest(id, webContents);
+ });
if (this.accessiblePath) {
+ console.log('accessiblePath : ' + this.accessiblePath);
protocol.interceptFileProtocol('file', (request, callback) => {
const url = require('url');
let access_path, parsed_info = url.parse(request.url);
access_path = parsed_info.host + parsed_info.pathname;
console.log("check path: " + access_path);
- for (let p in this.accessiblePath) {
- if (access_path.startsWith(this.accessiblePath[p])) {
+ for (let p in self.accessiblePath) {
+ if (access_path.startsWith(self.accessiblePath[p])) {
callback(access_path);
return;
}
console.log(error);
});
}
+ wrt.on('permission-response', function(event, id, result) {
+ console.log(`permission-response for ${id} is ${result}`);
+ let callback = self.pendingCallbacks.get(id);
+ if (typeof callback === 'function') {
+ console.log('calling permission response callback');
+ callback(result);
+ self.pendingCallbacks.delete(id);
+ }
+ });
+ wrt.on('auth-response', function(event, id, submit, user, passwd) {
+ let callback = self.pendingCallbacks.get(id);
+ if (typeof callback === 'function') {
+ console.log('calling auth response callback');
+ if (submit) {
+ callback(user, passwd);
+ } else {
+ callback();
+ }
+ self.pendingCallbacks.delete(id);
+ }
+ });
}
getBrowserWindowOption(options) {
return {
}
};
}
- handleEvents(options) {
+ createMainWindow(options) {
+ let winopt = this.getBrowserWindowOption(options);
+ this.mainWindow = new BrowserWindow(winopt);
+ if (options.devMode) {
+ this.mainWindow.webContents.openDevTools({
+ detached: true
+ });
+ }
let self = this;
- this.mainWindow.on('ready-to-show', function() {
- if (self.firstRendered)
- return;
+ this.mainWindow.once('ready-to-show', function() {
console.log('mainWindow ready-to-show');
wrt.hideSplashScreen(1);
self.firstRendered = true;
}
self.show();
});
- this.mainWindow.on('closed', function() {
- console.log('mainWindow closed!');
- self.mainWindow = null;
- });
- this.mainWindow.webContents.on('crashed', function() {
- console.error('webContents crashed');
- app.exit(100);
- });
this.mainWindow.webContents.on('did-start-loading', function() {
console.log('webContents did-start-loading');
});
self.suspend();
}
});
- wrt.on('permission-response', function(event, id, result) {
- console.log(`permission-response for ${id} is ${result}`);
- let callback = self.pendingCallbacks.get(id);
- if (typeof callback === 'function') {
- console.log('calling permission response callback');
- callback(result);
- self.pendingCallbacks.delete(id);
- }
- });
- wrt.on('auth-response', function(event, id, submit, user, passwd) {
- let callback = self.pendingCallbacks.get(id);
- if (typeof callback === 'function') {
- console.log('calling auth response callback');
- if (submit) {
- callback(user, passwd);
- } else {
- callback();
- }
- self.pendingCallbacks.delete(id);
- }
- });
- app.on('certificate-error', function(event, webContents, url, error, certificate, callback) {
- console.log('A certificate error has occurred');
- event.preventDefault();
- if (certificate.data) {
- const id = ++self.pendingID;
- console.log(`Raising a certificate error response with id: ${id}`);
- self.pendingCallbacks.set(id, callback);
- wrt.handleCertificateError(id, webContents, certificate.data, url);
- } else {
- console.log('Certificate could not be opened');
- callback(false);
- }
- });
- function handlePermissionRequests(webContents, permission, callback) {
- console.log(`handlePermissionRequests for ${permission}`);
- if (permission === 'notifications') {
- const id = ++self.pendingID;
- console.log(`Raising a notification permission request with id: ${id}`);
- self.pendingCallbacks.set(id, callback);
- wrt.handleNotificationPermissionRequest(id, webContents);
- } else if (permission === 'media') {
- const id = ++self.pendingID;
- console.log(`Raising a media permission request with id: ${id}`);
- self.pendingCallbacks.set(id, callback);
- wrt.handleMediaPermissionRequest(id, webContents);
- } else {
- /* electron by default allows permission for all if no request handler
- is there; so granting permission only temporarily to not have any
- side effects */
- callback(true);
- }
- }
- this.mainWindow.webContents.session.setPermissionRequestHandler(handlePermissionRequests);
- app.on('web-contents-created', function(event, webContents) {
- const ses = webContents.session;
- ses.setPermissionRequestHandler(handlePermissionRequests);
- });
- app.on('login', function(event, webContents, request, authInfo, callback) {
- console.log('Login info is required');
- event.preventDefault();
- const id = ++self.pendingID;
- console.log(`Raising a login info request with id: ${id}`);
- self.pendingCallbacks.set(id, callback);
- wrt.handleAuthRequest(id, webContents);
- });
}
suspend() {
console.log('WebApplication : suspend');
let windows = BrowserWindow.getAllWindows();
- windows.forEach((window) => window.hide());
if (!this.multitaskingSupport) {
console.log('multitasking is not supported; quitting app')
app.quit();
} else if (!this.backgroundSupport) {
windows.forEach((window) => window.setEnabled(false));
}
+ this.windowList[this.windowList.length - 1].hide();
}
resume() {
console.log('WebApplication : resume');
return;
}
BrowserWindow.getAllWindows().forEach((window) => {
- window.show();
if (!this.backgroundSupport)
window.setEnabled(true);
});
+ this.windowList[this.windowList.length - 1].show();
}
sendAppControlEvent() {
const kAppControlEventScript =