1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-17 16:02:33 +01:00
uBlock/js/messaging-client.js

142 lines
4.5 KiB
JavaScript
Raw Normal View History

2014-06-24 00:42:43 +02:00
/*******************************************************************************
µBlock - a Chromium browser extension to block requests.
Copyright (C) 2014 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
// This is the reference client-side implementation of µBlock's messaging
// infrastructure. The "server"-side implementation is in messaging.js.
// The client-side implementation creates a port in order to connect to
// µBlock's background page. With this port we can "ask", "tell" or "announce":
//
// "ask": send a request and expect an answer using a callback.
// "tell": send a request with no expectation of an answer.
// "announce": send a request to be relayed to all connections -- no answer
// expected.
//
// The tricky part in this implementation is to ensure all the requests are
// uniquely identified, so that the background-page can keep track of these
// until it is ready to send back an answer, which will be tagged with the
// same id. The uniqueness must be true for all ports which connect to the
// background page at any given time.
//
// Currently using Math.random() to generate this id... I don't know about the
// implementation of Math.random(), but as long as I have a good expectation
// of uniqueness, it's ok, we are not dealing with critical stuff here.
2014-09-14 23:14:58 +02:00
/* global chrome */
2014-06-24 00:42:43 +02:00
var messaging = (function(name){
var port = null;
var requestId = 1;
var requestIdToCallbackMap = {};
var listenCallback = null;
var onPortMessage = function(details) {
if ( typeof details.id !== 'number' ) {
return;
}
// Announcement?
if ( details.id < 0 ) {
if ( listenCallback ) {
listenCallback(details.msg);
}
return;
}
var callback = requestIdToCallbackMap[details.id];
if ( !callback ) {
return;
}
2014-08-28 17:08:51 +02:00
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
2014-06-24 00:42:43 +02:00
delete requestIdToCallbackMap[details.id];
2014-08-28 17:08:51 +02:00
callback(details.msg);
2014-06-24 00:42:43 +02:00
};
var start = function(name) {
2014-09-14 22:20:40 +02:00
port = chrome.runtime.connect({ name: name });
2014-06-24 00:42:43 +02:00
port.onMessage.addListener(onPortMessage);
2014-08-28 17:08:51 +02:00
// https://github.com/gorhill/uBlock/issues/193
port.onDisconnect.addListener(stop);
};
2014-06-24 00:42:43 +02:00
var stop = function() {
listenCallback = null;
2014-08-28 17:08:51 +02:00
port.disconnect();
port = null;
flushCallbacks();
2014-06-24 00:42:43 +02:00
};
2014-08-28 17:08:51 +02:00
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
2014-06-24 00:42:43 +02:00
var ask = function(msg, callback) {
2014-08-28 17:08:51 +02:00
if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
2014-06-24 00:42:43 +02:00
tell(msg);
return;
}
var id = requestId++;
port.postMessage({ id: id, msg: msg });
requestIdToCallbackMap[id] = callback;
};
var tell = function(msg) {
2014-08-28 17:08:51 +02:00
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg });
}
2014-06-24 00:42:43 +02:00
};
var listen = function(callback) {
listenCallback = callback;
};
2014-08-28 17:08:51 +02:00
var flushCallbacks = function() {
var callback;
2014-09-14 22:20:40 +02:00
for ( var id in requestIdToCallbackMap ) {
2014-08-28 17:08:51 +02:00
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
}
callback = requestIdToCallbackMap[id];
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();
2014-06-24 00:42:43 +02:00
}
};
return {
start: start,
stop: stop,
ask: ask,
tell: tell,
listen: listen
};
})();