1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-07-01 04:20:36 +12:00
uMatrix/platform/chromium/vapi-client.js

186 lines
5.1 KiB
JavaScript
Raw Normal View History

2014-11-25 05:49:11 +13:00
/*******************************************************************************
µBlock - a browser extension to block requests.
2014-11-25 05:49:11 +13:00
Copyright (C) 2014 The µBlock authors
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
*/
// For non background pages
/******************************************************************************/
(function(self) {
2014-11-25 05:49:11 +13:00
'use strict';
/******************************************************************************/
2015-01-15 11:45:55 +13:00
var vAPI = self.vAPI = self.vAPI || {};
2015-01-02 15:14:53 +13:00
var chrome = self.chrome;
2015-04-07 13:26:05 +12:00
// https://github.com/chrisaljoudi/uBlock/issues/456
// Already injected?
2015-01-02 15:14:53 +13:00
if ( vAPI.vapiClientInjected ) {
//console.debug('vapi-client.js already injected: skipping.');
return;
}
vAPI.vapiClientInjected = true;
2015-04-12 09:15:57 +12:00
vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) +
Math.random().toString(36).slice(2);
2014-11-25 11:20:21 +13:00
vAPI.chrome = true;
2014-11-25 05:49:11 +13:00
/******************************************************************************/
2015-04-08 11:34:22 +12:00
vAPI.shutdown = (function() {
var jobs = [];
var add = function(job) {
jobs.push(job);
};
var exec = function() {
//console.debug('Shutting down...');
var job;
while ( job = jobs.pop() ) {
job();
}
};
return {
add: add,
exec: exec
};
})();
/******************************************************************************/
2014-11-25 05:49:11 +13:00
var messagingConnector = function(response) {
if ( !response ) {
return;
}
var channels = vAPI.messaging.channels;
var channel, listener;
2014-12-29 09:26:06 +13:00
if ( response.broadcast === true && !response.channelName ) {
2014-11-25 05:49:11 +13:00
for ( channel in channels ) {
if ( channels.hasOwnProperty(channel) === false ) {
continue;
}
listener = channels[channel].listener;
if ( typeof listener === 'function' ) {
listener(response.msg);
}
}
return;
}
if ( response.requestId ) {
listener = vAPI.messaging.listeners[response.requestId];
delete vAPI.messaging.listeners[response.requestId];
delete response.requestId;
}
if ( !listener ) {
2014-12-29 09:26:06 +13:00
channel = channels[response.channelName];
2014-11-25 05:49:11 +13:00
listener = channel && channel.listener;
}
if ( typeof listener === 'function' ) {
listener(response.msg);
}
};
/******************************************************************************/
2014-11-25 11:20:21 +13:00
vAPI.messaging = {
2014-11-25 05:49:11 +13:00
port: null,
channels: {},
listeners: {},
requestId: 1,
setup: function() {
this.port = chrome.runtime.connect({name: vAPI.sessionId});
2014-11-25 05:49:11 +13:00
this.port.onMessage.addListener(messagingConnector);
},
close: function() {
if ( this.port === null ) {
return;
}
this.port.disconnect();
this.port.onMessage.removeListener(messagingConnector);
this.port = null;
this.channels = {};
this.listeners = {};
},
channel: function(channelName, callback) {
if ( !channelName ) {
return;
}
this.channels[channelName] = {
2014-12-29 09:26:06 +13:00
channelName: channelName,
2014-11-25 05:49:11 +13:00
listener: typeof callback === 'function' ? callback : null,
send: function(message, callback) {
if ( vAPI.messaging.port === null ) {
vAPI.messaging.setup();
}
message = {
2014-12-29 09:26:06 +13:00
channelName: this.channelName,
2014-11-25 05:49:11 +13:00
msg: message
};
if ( callback ) {
message.requestId = vAPI.messaging.requestId++;
vAPI.messaging.listeners[message.requestId] = callback;
}
vAPI.messaging.port.postMessage(message);
},
close: function() {
2014-12-29 09:26:06 +13:00
delete vAPI.messaging.channels[this.channelName];
if ( Object.keys(vAPI.messaging.channels).length === 0 ) {
vAPI.messaging.close();
}
2014-11-25 05:49:11 +13:00
}
};
return this.channels[channelName];
}
};
/******************************************************************************/
2015-04-08 11:34:22 +12:00
// No need to have vAPI client linger around after shutdown if
// we are not a top window (because element picker can still
// be injected in top window).
if ( window !== window.top ) {
vAPI.shutdown.add(function() {
vAPI = null;
});
}
2014-11-25 05:49:11 +13:00
/******************************************************************************/
})(this);
2014-11-25 05:49:11 +13:00
/******************************************************************************/