mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
code review re. sandbox shutdown
This commit is contained in:
parent
352ef808f1
commit
35ef80f3aa
@ -211,11 +211,12 @@ const contentObserver = {
|
|||||||
return this.ACCEPT;
|
return this.ACCEPT;
|
||||||
},
|
},
|
||||||
|
|
||||||
initContentScripts: function(win, sandbox) {
|
initContentScripts: function(win, create) {
|
||||||
let messager = getMessageManager(win);
|
let messager = getMessageManager(win);
|
||||||
let sandboxId = hostName + ':sb:' + this.uniqueSandboxId++;
|
let sandboxId = hostName + ':sb:' + this.uniqueSandboxId++;
|
||||||
|
let sandbox;
|
||||||
|
|
||||||
if ( sandbox ) {
|
if ( create ) {
|
||||||
let sandboxName = [
|
let sandboxName = [
|
||||||
win.location.href.slice(0, 100),
|
win.location.href.slice(0, 100),
|
||||||
win.document.title.slice(0, 100)
|
win.document.title.slice(0, 100)
|
||||||
@ -236,6 +237,23 @@ const contentObserver = {
|
|||||||
// I've seen this happens, need to investigate why.
|
// I've seen this happens, need to investigate why.
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// The goal is to have content scripts removed from web pages. This
|
||||||
|
// helps remove traces of uBlock from memory when disabling/removing
|
||||||
|
// the addon.
|
||||||
|
// For example, this takes care of:
|
||||||
|
// https://github.com/gorhill/uBlock/commit/ea4faff383789053f423498c1f1165c403fde7c7#commitcomment-11964137
|
||||||
|
// > "gets the whole selected tab flashing"
|
||||||
|
sandbox.outerShutdown = function() {
|
||||||
|
sandbox.removeMessageListener();
|
||||||
|
sandbox.addMessageListener =
|
||||||
|
sandbox.injectScript =
|
||||||
|
sandbox.removeMessageListener =
|
||||||
|
sandbox.sendAsyncMessage =
|
||||||
|
sandbox.outerShutdown = function(){};
|
||||||
|
sandbox.vAPI = {};
|
||||||
|
messager = null;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
sandbox = win;
|
sandbox = win;
|
||||||
@ -283,22 +301,6 @@ const contentObserver = {
|
|||||||
sandbox._messageListener_ = null;
|
sandbox._messageListener_ = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// The goal is to have content scripts removed from web pages. This
|
|
||||||
// helps remove traces of uBlock from memory when disabling/removing
|
|
||||||
// the addon.
|
|
||||||
// For example, this takes care of:
|
|
||||||
// https://github.com/gorhill/uBlock/commit/ea4faff383789053f423498c1f1165c403fde7c7#commitcomment-11964137
|
|
||||||
// > "gets the whole selected tab flashing"
|
|
||||||
sandbox.shutdownSandbox = function() {
|
|
||||||
sandbox.removeMessageListener();
|
|
||||||
sandbox.addMessageListener =
|
|
||||||
sandbox.injectScript =
|
|
||||||
sandbox.removeMessageListener =
|
|
||||||
sandbox.sendAsyncMessage =
|
|
||||||
sandbox.shutdownSandbox = null;
|
|
||||||
messager = null;
|
|
||||||
};
|
|
||||||
|
|
||||||
return sandbox;
|
return sandbox;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
Home: https://github.com/gorhill/uBlock
|
Home: https://github.com/gorhill/uBlock
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* global addMessageListener, removeMessageListener, sendAsyncMessage, shutdownSandbox */
|
/* global addMessageListener, removeMessageListener, sendAsyncMessage, outerShutdown */
|
||||||
|
|
||||||
// For non background pages
|
// For non background pages
|
||||||
|
|
||||||
@ -78,27 +78,24 @@ vAPI.messaging = {
|
|||||||
|
|
||||||
builtinChannelHandler: function(msg) {
|
builtinChannelHandler: function(msg) {
|
||||||
if ( msg.cmd === 'injectScript' ) {
|
if ( msg.cmd === 'injectScript' ) {
|
||||||
|
// injectScript is not always present.
|
||||||
|
// - See contentObserver.initContentScripts in frameModule.js
|
||||||
|
if ( typeof self.injectScript !== 'function' ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
var details = msg.details;
|
var details = msg.details;
|
||||||
if ( !details.allFrames && window !== window.top ) {
|
if ( !details.allFrames && window !== window.top ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// TODO: investigate why this happens, and if this happens
|
self.injectScript(details.file);
|
||||||
// legitimately (content scripts not injected I suspect, so
|
|
||||||
// that would make this legitimate).
|
|
||||||
// Case: open popup UI from icon in uBlock's logger
|
|
||||||
if ( typeof self.injectScript === 'function' ) {
|
|
||||||
self.injectScript(details.file);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( msg.cmd === 'shutdownSandbox' ) {
|
if ( msg.cmd === 'shutdownSandbox' ) {
|
||||||
vAPI.shutdown.exec();
|
vAPI.shutdown.exec();
|
||||||
vAPI.messaging.close();
|
vAPI.messaging.shutdown();
|
||||||
window.removeEventListener('pagehide', vAPI.messaging.toggleListener, true);
|
if ( typeof self.outerShutdown === 'function' ) {
|
||||||
window.removeEventListener('pageshow', vAPI.messaging.toggleListener, true);
|
outerShutdown();
|
||||||
vAPI.messaging = null;
|
}
|
||||||
vAPI = {};
|
|
||||||
shutdownSandbox();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -109,6 +106,13 @@ vAPI.messaging = {
|
|||||||
window.addEventListener('pageshow', this.toggleListener, true);
|
window.addEventListener('pageshow', this.toggleListener, true);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
shutdown: function() {
|
||||||
|
this.close();
|
||||||
|
window.removeEventListener('pagehide', this.toggleListener, true);
|
||||||
|
window.removeEventListener('pageshow', this.toggleListener, true);
|
||||||
|
vAPI.messaging = null;
|
||||||
|
},
|
||||||
|
|
||||||
close: function() {
|
close: function() {
|
||||||
window.removeEventListener('pagehide', this.toggleListener, true);
|
window.removeEventListener('pagehide', this.toggleListener, true);
|
||||||
window.removeEventListener('pageshow', this.toggleListener, true);
|
window.removeEventListener('pageshow', this.toggleListener, true);
|
||||||
@ -146,18 +150,24 @@ vAPI.messaging = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
toggleListener: function({type, persisted}) {
|
toggleListener: function({type, persisted}) {
|
||||||
if ( !vAPI.messaging ) {
|
var me = vAPI.messaging;
|
||||||
|
if ( !me ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( type === 'pagehide' ) {
|
if ( type === 'pagehide' ) {
|
||||||
vAPI.messaging.disconnect();
|
if ( !persisted ) {
|
||||||
|
vAPI.shutdown.exec();
|
||||||
|
vAPI.messaging.shutdown();
|
||||||
|
if ( typeof self.outerShutdown === 'function' ) {
|
||||||
|
outerShutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
me.disconnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( persisted ) {
|
me.connect();
|
||||||
vAPI.messaging.connect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user