Code review: ensure vAPI.shutdown.exec is called from root context only

This prevents uncaught errors in content scripts when uBO's main
process is terminated (i.e. disabled, updated).
This commit is contained in:
Raymond Hill 2019-01-28 16:16:52 -05:00
parent a6ebcc85be
commit 920eee88be
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 23 additions and 14 deletions

View File

@ -53,10 +53,15 @@ vAPI.shutdown = {
this.jobs.push(job);
},
exec: function() {
let job;
while ( (job = this.jobs.pop()) ) {
job();
// Shutdown asynchronously, to ensure shutdown jobs are called from
// the top context.
self.requestIdleCallback(( ) => {
const jobs = this.jobs.slice();
this.jobs.length = 0;
while ( jobs.length !== 0 ) {
(jobs.pop())();
}
});
},
remove: function(job) {
let pos;
@ -238,7 +243,12 @@ vAPI.messaging = {
} catch (ex) {
this.port = null;
}
if ( this.port !== null ) {
// Not having a valid port at this point means the main process is
// not available: no point keeping the content scripts alive.
if ( this.port === null ) {
vAPI.shutdown.exec();
return null;
}
this.port.onMessage.addListener(this.messageListenerCallback);
this.port.onDisconnect.addListener(this.disconnectListenerBound);
this.portTimerDelay = 10000;
@ -248,7 +258,6 @@ vAPI.messaging = {
this.portTimerDelay
);
}
}
return this.port;
},