Skip to content

Commit

Permalink
Cleanup WindowManager
Browse files Browse the repository at this point in the history
This commit adds some cleanup for the window manager.

The change theme handler has been commented out until #350 is addressed.
  • Loading branch information
pr1sm committed Feb 22, 2019
1 parent 2743a51 commit 52c8323
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
4 changes: 4 additions & 0 deletions packages/frontend/lib/_electron/captchaWindowManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ class CaptchaWindowManager {
* Close all captcha windows
*/
closeAllCaptchaWindows() {
// Do nothing if we don't have any captcha windows
if (this._captchaWindows.length === 0) {
return;
}
console.log('[DEBUG]: Closing all captcha windows...');
this._captchaWindows.forEach(win => {
win.close();
Expand Down
90 changes: 47 additions & 43 deletions packages/frontend/lib/_electron/windowManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class WindowManager {
context.ipc.on(IPCKeys.RequestSendMessage, this._onRequestSendMessage.bind(this));
context.ipc.on(IPCKeys.RequestGetWindowIDs, this._onRequestGetWindowIDs.bind(this));
context.ipc.on(IPCKeys.RequestCloseWindow, this._onRequestWindowClose.bind(this));
context.ipc.on(IPCKeys.ChangeTheme, this.onRequestChangeTheme.bind(this));
// TODO: Add this back in #350 (https://github.com/walmat/nebula/issues/350)
// context.ipc.on(IPCKeys.ChangeTheme, this.onRequestChangeTheme.bind(this));
}

/**
Expand Down Expand Up @@ -143,40 +144,35 @@ class WindowManager {
if (this._aboutDialog) {
return this._aboutDialog;
}
w = await createAboutWindow();
w = createAboutWindow();
this._aboutDialog = w;
break;
}
case 'auth': {
if (this._auth) {
return this._auth;
}
w = await createAuthWindow();
w = createAuthWindow();
this._auth = w;
break;
}
case 'main': {
if (this._main) {
return this._main;
}
w = await createMainWindow();
w = createMainWindow();
this._main = w;
this._context.taskLauncher.start();
break;
}
default:
break;
}

if (tag !== 'captcha') {
w.loadURL(urls.get(tag));
}

// Make sure window was created before adding event listeners
if (w) {
this.addWindowEventListeners(w);
default: {
// throw error if unsupported tag was passed
throw new Error('Window Tag is Unsupported!');
}
}

w.loadURL(urls.get(tag));
this.addWindowEventListeners(w);
return w;
}
return this.transitionToDeauthedState();
Expand Down Expand Up @@ -218,18 +214,21 @@ class WindowManager {
* @param {BrowserWindow} win reference to the window being closed
*/
handleClose(win) {
// Store the winId in the upper scope so we don't throw an exception when
// Trying to access win (which could already be destroyed)
const winId = win.id;
return () => {
if (nebulaEnv.isDevelopment()) {
console.log(`Window was closed, id = ${win.id}`);
console.log(`Window was closed, id = ${winId}`);
}
this._windows.delete(win.id);
this._notifyUpdateWindowIDs(win.id);
this._windows.delete(winId);
this._notifyUpdateWindowIDs(winId);

if (this._aboutDialog && win.id === this._aboutDialog.id) {
if (this._aboutDialog && winId === this._aboutDialog.id) {
this._aboutDialog = null;
} else if (this._main && win.id === this._main.id) {
} else if (this._main && winId === this._main.id) {
this._main = null;
} else if (this._auth && win.id === this._auth.id) {
} else if (this._auth && winId === this._auth.id) {
this._auth = null;
}
};
Expand Down Expand Up @@ -306,9 +305,14 @@ class WindowManager {
this._captchaWindowManager.spawnCaptchaWindow(opts);
ev.sender.send(IPCKeys.FinishCreateNewWindow);
} else {
const createdWindow = this.createNewWindow(tag);
ev.sender.send(IPCKeys.FinishCreateNewWindow);
this._notifyUpdateWindowIDs(createdWindow.id);
try {
const createdWindow = this.createNewWindow(tag);
ev.sender.send(IPCKeys.FinishCreateNewWindow);
this._notifyUpdateWindowIDs(createdWindow.id);
} catch (err) {
console.log('[ERROR]: %s', err.message);
ev.sender.send(IPCKeys.FinishCreateNewWindow);
}
}
}

Expand Down Expand Up @@ -361,35 +365,35 @@ class WindowManager {

/**
* Start Harvesting Captchas for a specific task
* // TODO This should be moved to CaptchaWindowManager when issue #97 gets tackled
* // https://github.com/walmat/nebula/issues/97
*
* Forward call to Captcha Window Manager
*/
async onRequestStartHarvestingCaptcha(runnerId, siteKey) {
startHarvestingCaptcha(runnerId, siteKey) {
this._captchaWindowManager.startHarvesting(runnerId, siteKey);
}

/**
* Stop Harvesting Captchas for a specific task
* // TODO This should be moved to CaptchaWindowManager when issue #97 gets tackled
* // https://github.com/walmat/nebula/issues/97
*
* Forward call to Captcha Window Manager
*/
onRequestStopHarvestingCaptcha(runnerId, siteKey) {
stopHarvestingCaptcha(runnerId, siteKey) {
this._captchaWindowManager.stopHarvesting(runnerId, siteKey);
}

onRequestChangeTheme(_, opts) {
const { backgroundColor } = opts;

this._captchas.forEach((__, windowId) => {
const win = this._windows.get(windowId);
// #350 (https://github.com/walmat/nebula/issues/350)
/**
* I've tried:
* 1. win.setBackgroundColor(backgroundColor);
* 2. win.webContents.browserWindowOptions.backgroundColor = backgroundColor;
*/
});
}
// // TODO: Add this back in #350 (https://github.com/walmat/nebula/issues/350)
// onRequestChangeTheme(_, opts) {
// const { backgroundColor } = opts;
// // TODO: Use captcha window manager in this case...
// this._captchas.forEach((__, windowId) => {
// const win = this._windows.get(windowId);
// /**
// * I've tried:
// * 1. win.setBackgroundColor(backgroundColor);
// * 2. win.webContents.browserWindowOptions.backgroundColor = backgroundColor;
// */
// });
// }
}

module.exports = WindowManager;
7 changes: 5 additions & 2 deletions packages/frontend/lib/task/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ class TaskLauncher {
runnerId,
siteKey = '6LeoeSkTAAAAAA9rkZs5oS82l69OEYjKRZAiKdaF',
) {
await this._context.windowManager.onRequestStartHarvestingCaptcha(runnerId, siteKey);
// If this is the first harvest event, start harvesting
if (this._captchaSemaphore === 0) {
await this._context.windowManager.startHarvestingCaptcha(runnerId, siteKey);
}
this._captchaSemaphore += 1;
}

Expand All @@ -255,7 +258,7 @@ class TaskLauncher {
// Captcha Harvest Requesters will drop to 0
// Drop the semaphore and stop harvesting
this._captchaSemaphore -= 1;
this._context.windowManager.onRequestStopHarvestingCaptcha(runnerId, siteKey);
this._context.windowManager.stopHarvestingCaptcha(runnerId, siteKey);
} else if (this._captchaSemaphore > 0) {
// There are still Captcha Harvest Requesters
// Drop the semaphore, but continue harvesting
Expand Down

0 comments on commit 52c8323

Please sign in to comment.