This commit is contained in:
gorhill 2014-08-28 11:08:51 -04:00
parent fe2d761908
commit c8e288fdf6
4 changed files with 153 additions and 86 deletions

View File

@ -30,7 +30,6 @@
var uBlockMessaging = (function(name){ var uBlockMessaging = (function(name){
var port = null; var port = null;
var dangling = false;
var requestId = 1; var requestId = 1;
var requestIdToCallbackMap = {}; var requestIdToCallbackMap = {};
var listenCallback = null; var listenCallback = null;
@ -50,9 +49,10 @@ var uBlockMessaging = (function(name){
if ( !callback ) { if ( !callback ) {
return; return;
} }
callback(details.msg); // Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[details.id]; delete requestIdToCallbackMap[details.id];
checkDisconnect(); callback(details.msg);
}; };
var start = function(name) { var start = function(name) {
@ -67,20 +67,30 @@ var uBlockMessaging = (function(name){
) )
}); });
port.onMessage.addListener(onPortMessage); port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) { // https://github.com/gorhill/uBlock/issues/193
start(name); port.onDisconnect.addListener(stop);
} };
var stop = function() { var stop = function() {
listenCallback = null; listenCallback = null;
dangling = true; port.disconnect();
checkDisconnect(); port = null;
flushCallbacks();
}; };
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
var ask = function(msg, callback) { var ask = function(msg, callback) {
if ( !callback ) { if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
tell(msg); tell(msg);
return; return;
} }
@ -90,22 +100,30 @@ var uBlockMessaging = (function(name){
}; };
var tell = function(msg) { var tell = function(msg) {
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg }); port.postMessage({ id: 0, msg: msg });
}
}; };
var listen = function(callback) { var listen = function(callback) {
listenCallback = callback; listenCallback = callback;
}; };
var checkDisconnect = function() { var flushCallbacks = function() {
if ( !dangling ) { var callback;
return; for ( id in requestIdToCallbackMap ) {
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
} }
if ( Object.keys(requestIdToCallbackMap).length ) { callback = requestIdToCallbackMap[id];
return; if ( !callback ) {
continue;
}
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[id];
callback();
} }
port.disconnect();
port = null;
}; };
return { return {

View File

@ -38,9 +38,8 @@
// https://github.com/gorhill/httpswitchboard/issues/345 // https://github.com/gorhill/httpswitchboard/issues/345
var messaging = (function(name){ var uBlockMessaging = (function(name){
var port = null; var port = null;
var dangling = false;
var requestId = 1; var requestId = 1;
var requestIdToCallbackMap = {}; var requestIdToCallbackMap = {};
var listenCallback = null; var listenCallback = null;
@ -60,9 +59,10 @@ var messaging = (function(name){
if ( !callback ) { if ( !callback ) {
return; return;
} }
callback(details.msg); // Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[details.id]; delete requestIdToCallbackMap[details.id];
checkDisconnect(); callback(details.msg);
}; };
var start = function(name) { var start = function(name) {
@ -77,20 +77,30 @@ var messaging = (function(name){
) )
}); });
port.onMessage.addListener(onPortMessage); port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) { // https://github.com/gorhill/uBlock/issues/193
start(name); port.onDisconnect.addListener(stop);
} };
var stop = function() { var stop = function() {
listenCallback = null; listenCallback = null;
dangling = true; port.disconnect();
checkDisconnect(); port = null;
flushCallbacks();
}; };
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
var ask = function(msg, callback) { var ask = function(msg, callback) {
if ( !callback ) { if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
tell(msg); tell(msg);
return; return;
} }
@ -100,22 +110,30 @@ var messaging = (function(name){
}; };
var tell = function(msg) { var tell = function(msg) {
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg }); port.postMessage({ id: 0, msg: msg });
}
}; };
var listen = function(callback) { var listen = function(callback) {
listenCallback = callback; listenCallback = callback;
}; };
var checkDisconnect = function() { var flushCallbacks = function() {
if ( !dangling ) { var callback;
return; for ( id in requestIdToCallbackMap ) {
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
} }
if ( Object.keys(requestIdToCallbackMap).length ) { callback = requestIdToCallbackMap[id];
return; if ( !callback ) {
continue;
}
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[id];
callback();
} }
port.disconnect();
port = null;
}; };
return { return {
@ -189,6 +207,9 @@ var netFilters = function(details) {
}; };
var filteringHandler = function(details) { var filteringHandler = function(details) {
// The port will never be used again at this point, disconnecting allows
// the browser to flush this script from memory.
uBlockMessaging.stop();
if ( !details ) { if ( !details ) {
return; return;
} }
@ -213,7 +234,7 @@ var hideElements = function(selectors) {
} }
}; };
messaging.ask( uBlockMessaging.ask(
{ {
what: 'retrieveDomainCosmeticSelectors', what: 'retrieveDomainCosmeticSelectors',
pageURL: window.location.href, pageURL: window.location.href,
@ -225,14 +246,6 @@ messaging.ask(
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
// The port will never be used again at this point, disconnecting allows
// the browser to flush this script from memory.
messaging.stop();
/******************************************************************************/
/******************************************************************************/
})(); })();
/******************************************************************************/ /******************************************************************************/

View File

@ -117,7 +117,6 @@
var messaging = (function(name){ var messaging = (function(name){
var port = null; var port = null;
var dangling = false;
var requestId = 1; var requestId = 1;
var requestIdToCallbackMap = {}; var requestIdToCallbackMap = {};
var listenCallback = null; var listenCallback = null;
@ -137,9 +136,10 @@ var messaging = (function(name){
if ( !callback ) { if ( !callback ) {
return; return;
} }
callback(details.msg); // Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[details.id]; delete requestIdToCallbackMap[details.id];
checkDisconnect(); callback(details.msg);
}; };
var start = function(name) { var start = function(name) {
@ -154,20 +154,30 @@ var messaging = (function(name){
) )
}); });
port.onMessage.addListener(onPortMessage); port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) { // https://github.com/gorhill/uBlock/issues/193
start(name); port.onDisconnect.addListener(stop);
} };
var stop = function() { var stop = function() {
listenCallback = null; listenCallback = null;
dangling = true; port.disconnect();
checkDisconnect(); port = null;
flushCallbacks();
}; };
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
var ask = function(msg, callback) { var ask = function(msg, callback) {
if ( !callback ) { if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
tell(msg); tell(msg);
return; return;
} }
@ -177,22 +187,30 @@ var messaging = (function(name){
}; };
var tell = function(msg) { var tell = function(msg) {
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg }); port.postMessage({ id: 0, msg: msg });
}
}; };
var listen = function(callback) { var listen = function(callback) {
listenCallback = callback; listenCallback = callback;
}; };
var checkDisconnect = function() { var flushCallbacks = function() {
if ( !dangling ) { var callback;
return; for ( id in requestIdToCallbackMap ) {
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
} }
if ( Object.keys(requestIdToCallbackMap).length ) { callback = requestIdToCallbackMap[id];
return; if ( !callback ) {
continue;
}
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[id];
callback();
} }
port.disconnect();
port = null;
}; };
return { return {

View File

@ -42,7 +42,6 @@
var messaging = (function(name){ var messaging = (function(name){
var port = null; var port = null;
var dangling = false;
var requestId = 1; var requestId = 1;
var requestIdToCallbackMap = {}; var requestIdToCallbackMap = {};
var listenCallback = null; var listenCallback = null;
@ -62,9 +61,10 @@ var messaging = (function(name){
if ( !callback ) { if ( !callback ) {
return; return;
} }
callback(details.msg); // Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[details.id]; delete requestIdToCallbackMap[details.id];
checkDisconnect(); callback(details.msg);
}; };
var start = function(name) { var start = function(name) {
@ -79,20 +79,30 @@ var messaging = (function(name){
) )
}); });
port.onMessage.addListener(onPortMessage); port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) { // https://github.com/gorhill/uBlock/issues/193
start(name); port.onDisconnect.addListener(stop);
} };
var stop = function() { var stop = function() {
listenCallback = null; listenCallback = null;
dangling = true; port.disconnect();
checkDisconnect(); port = null;
flushCallbacks();
}; };
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
var ask = function(msg, callback) { var ask = function(msg, callback) {
if ( !callback ) { if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
tell(msg); tell(msg);
return; return;
} }
@ -102,22 +112,30 @@ var messaging = (function(name){
}; };
var tell = function(msg) { var tell = function(msg) {
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg }); port.postMessage({ id: 0, msg: msg });
}
}; };
var listen = function(callback) { var listen = function(callback) {
listenCallback = callback; listenCallback = callback;
}; };
var checkDisconnect = function() { var flushCallbacks = function() {
if ( !dangling ) { var callback;
return; for ( id in requestIdToCallbackMap ) {
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
} }
if ( Object.keys(requestIdToCallbackMap).length ) { callback = requestIdToCallbackMap[id];
return; if ( !callback ) {
continue;
}
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[id];
callback();
} }
port.disconnect();
port = null;
}; };
return { return {