Fix timers after suspend/resume/pause

We use timers to decide when to query and delete expired messages or
when to perform signed key rotations.

Internally, timers are counters that get updated when the CPU ticks, so
if the CPU sleeps, the timer will stop counting, and start again after
it wakes up, ignoring the intervening passage of wall clock time.

To fix this, without having to query the database or other potentially
high overhead operations too often, use an interval to frequently check
the wall clock time. If time jumps forward, trigger a global event so
other listeners can update their possibly-inaccurate timers.

https://stackoverflow.com/questions/6346849/what-happens-to-settimeout-when-the-computer-goes-to-sleep
https://stackoverflow.com/questions/4079115/can-any-desktop-browsers-detect-when-the-computer-resumes-from-sleep

// FREEBIE
This commit is contained in:
lilia 2017-03-01 13:11:40 -08:00
parent 31b9311f62
commit 25ee61d3cb
5 changed files with 37 additions and 1 deletions

View File

@ -541,6 +541,7 @@
<script type='text/javascript' src='js/views/identicon_svg_view.js'></script>
<script type='text/javascript' src='js/views/settings_view.js'></script>
<script type='text/javascript' src='js/wall_clock_listener.js'></script>
<script type='text/javascript' src='js/rotate_signed_prekey_listener.js'></script>
<script type='text/javascript' src='js/background.js'></script>
</head>

View File

@ -87,8 +87,9 @@
openInbox();
}
WallClockListener.init();
RotateSignedPreKeyListener.init();
ExpiringMessagesListener.update();
ExpiringMessagesListener.init();
});
window.getSyncRequest = function() {

View File

@ -37,6 +37,10 @@
}
window.ExpiringMessagesListener = {
init: function() {
checkExpiringMessages();
window.events.on('timetravel', checkExpiringMessages);
},
update: checkExpiringMessages
};

View File

@ -53,6 +53,11 @@
scheduleNextRotation();
setTimeoutForNextRun();
});
window.events.on('timetravel', function() {
if (Whisper.Registration.isDone()) {
setTimeoutForNextRun();
}
});
}
};
}());

25
js/wall_clock_listener.js Normal file
View File

@ -0,0 +1,25 @@
/*
* vim: ts=4:sw=4:expandtab
*/
;(function () {
'use strict';
var lastTime;
var interval = 1000;
function checkTime() {
var currentTime = Date.now();
if (currentTime > (lastTime + interval * 2)) {
console.log('time travel detected!');
window.events.trigger('timetravel');
}
lastTime = currentTime;
}
window.WallClockListener = {
init: function() {
lastTime = Date.now();
setInterval(checkTime, 1000);
}
};
}());