2016-11-28 11:42:58 +01:00
|
|
|
/**
|
|
|
|
* Waiter to handle events related to the window object.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @constructor
|
2017-03-23 18:52:20 +01:00
|
|
|
* @param {App} app - The main view object for CyberChef.
|
2016-11-28 11:42:58 +01:00
|
|
|
*/
|
2017-04-13 19:08:50 +02:00
|
|
|
const WindowWaiter = function(app) {
|
2016-11-28 11:42:58 +01:00
|
|
|
this.app = app;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for window resize events.
|
|
|
|
* Resets the layout of CyberChef's panes after 200ms (so that continuous resizing doesn't cause
|
|
|
|
* continuous resetting).
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
WindowWaiter.prototype.windowResize = function() {
|
|
|
|
clearTimeout(this.resetLayoutTimeout);
|
|
|
|
this.resetLayoutTimeout = setTimeout(this.app.resetLayout.bind(this.app), 200);
|
2016-11-28 11:42:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for window blur events.
|
|
|
|
* Saves the current time so that we can calculate how long the window was unfocussed for when
|
|
|
|
* focus is returned.
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
WindowWaiter.prototype.windowBlur = function() {
|
|
|
|
this.windowBlurTime = new Date().getTime();
|
2016-11-28 11:42:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for window focus events.
|
|
|
|
*
|
|
|
|
* When a browser tab is unfocused and the browser has to run lots of dynamic content in other
|
|
|
|
* tabs, it swaps out the memory for that tab.
|
|
|
|
* If the CyberChef tab has been unfocused for more than a minute, we run a silent bake which will
|
|
|
|
* force the browser to load and cache all the relevant JavaScript code needed to do a real bake.
|
|
|
|
* This will stop baking taking a long time when the CyberChef browser tab has been unfocused for
|
|
|
|
* a long time and the browser has swapped out all its memory.
|
|
|
|
*/
|
2017-01-31 19:24:56 +01:00
|
|
|
WindowWaiter.prototype.windowFocus = function() {
|
2017-04-13 19:08:50 +02:00
|
|
|
const unfocusedTime = new Date().getTime() - this.windowBlurTime;
|
2017-01-31 19:24:56 +01:00
|
|
|
if (unfocusedTime > 60000) {
|
|
|
|
this.app.silentBake();
|
2016-11-28 11:42:58 +01:00
|
|
|
}
|
|
|
|
};
|
2017-03-23 18:52:20 +01:00
|
|
|
|
|
|
|
export default WindowWaiter;
|