1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00
uBlock/platform/chromium/vapi-client.js

334 lines
9.5 KiB
JavaScript
Raw Normal View History

/*******************************************************************************
µBlock - a browser extension to block requests.
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
*/
/* global HTMLDocument, XMLDocument */
// For non background pages
/******************************************************************************/
(function(self) {
'use strict';
/******************************************************************************/
// https://github.com/chrisaljoudi/uBlock/issues/464
if ( document instanceof HTMLDocument === false ) {
// https://github.com/chrisaljoudi/uBlock/issues/1528
// A XMLDocument can be a valid HTML document.
if (
document instanceof XMLDocument === false ||
document.createElement('div') instanceof HTMLDivElement === false
) {
return;
}
}
2016-01-03 01:33:32 +01:00
// https://github.com/gorhill/uBlock/issues/1124
// Looks like `contentType` is on track to be standardized:
// https://dom.spec.whatwg.org/#concept-document-content-type
2016-01-11 07:38:29 +01:00
if ( (document.contentType || '').lastIndexOf('image/', 0) === 0 ) {
2016-01-03 01:33:32 +01:00
return;
}
/******************************************************************************/
2015-01-14 23:45:55 +01:00
var vAPI = self.vAPI = self.vAPI || {};
2015-01-02 03:14:53 +01:00
var chrome = self.chrome;
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/456
// Already injected?
if ( vAPI.sessionId ) {
return;
}
2014-11-24 23:20:21 +01:00
2015-07-07 17:03:26 +02:00
vAPI.sessionId = String.fromCharCode(Date.now() % 26 + 97) +
Math.random().toString(36).slice(2);
2014-11-24 23:20:21 +01:00
vAPI.chrome = true;
/******************************************************************************/
vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self);
/******************************************************************************/
2015-04-08 01:34:22 +02:00
vAPI.shutdown = (function() {
var jobs = [];
var add = function(job) {
jobs.push(job);
};
var exec = function() {
var job;
2016-01-03 01:33:32 +01:00
while ( (job = jobs.pop()) ) {
2015-04-08 01:34:22 +02:00
job();
}
};
return {
add: add,
exec: exec
};
})();
/******************************************************************************/
vAPI.messaging = {
port: null,
channels: {},
pending: {},
2015-07-07 17:03:26 +02:00
pendingCount: 0,
auxProcessId: 1,
2015-06-26 06:08:41 +02:00
onDisconnect: function() {
this.port = null;
this.close();
vAPI.shutdown.exec();
},
setup: function() {
2015-07-07 17:03:26 +02:00
try {
this.port = chrome.runtime.connect({name: vAPI.sessionId}) || null;
2015-07-07 17:03:26 +02:00
} catch (ex) {
}
if ( this.port === null ) {
vAPI.shutdown.exec();
return false;
}
this.port.onMessage.addListener(messagingConnector);
this.port.onDisconnect.addListener(this.onDisconnect.bind(this));
2015-07-07 17:03:26 +02:00
return true;
},
2015-06-26 06:08:41 +02:00
close: function() {
2015-07-07 17:03:26 +02:00
var port = this.port;
if ( port !== null ) {
port.disconnect();
port.onMessage.removeListener(messagingConnector);
port.onDisconnect.removeListener(this.onDisconnect);
this.port = null;
}
this.channels = {};
2015-07-07 17:03:26 +02:00
// service pending callbacks
var pending = this.pending, listener;
this.pending = {};
2015-07-07 17:03:26 +02:00
this.pendingCount = 0;
for ( var auxId in pending ) {
if ( this.pending.hasOwnProperty(auxId) ) {
listener = pending[auxId];
if ( typeof listener === 'function' ) {
listener(null);
}
}
}
},
2015-06-28 23:42:08 +02:00
channel: function(channelName, callback) {
if ( !channelName ) {
return;
}
var channel = this.channels[channelName];
if ( channel instanceof MessagingChannel ) {
channel.addListener(callback);
channel.refCount += 1;
} else {
channel = this.channels[channelName] = new MessagingChannel(channelName, callback);
}
return channel;
2015-06-26 06:08:41 +02:00
}
};
/******************************************************************************/
var messagingConnector = function(details) {
if ( !details ) {
return;
}
2015-06-26 06:08:41 +02:00
var messaging = vAPI.messaging;
var channels = messaging.channels;
var channel;
2015-06-26 06:08:41 +02:00
// Sent to all channels
if ( details.broadcast === true && !details.channelName ) {
2014-11-24 16:40:05 +01:00
for ( channel in channels ) {
2015-06-26 06:08:41 +02:00
if ( channels[channel] instanceof MessagingChannel === false ) {
2014-11-24 16:40:05 +01:00
continue;
}
channels[channel].sendToListeners(details.msg);
}
return;
}
2015-06-26 06:08:41 +02:00
// Response to specific message previously sent
if ( details.auxProcessId ) {
var listener = messaging.pending[details.auxProcessId];
delete messaging.pending[details.auxProcessId];
delete details.auxProcessId; // TODO: why?
2015-06-26 06:08:41 +02:00
if ( listener ) {
2015-07-07 17:03:26 +02:00
messaging.pendingCount -= 1;
listener(details.msg);
2015-06-26 06:08:41 +02:00
return;
}
}
// Sent to a specific channel
var response;
channel = channels[details.channelName];
2015-06-26 06:08:41 +02:00
if ( channel instanceof MessagingChannel ) {
response = channel.sendToListeners(details.msg);
}
// Respond back if required
if ( details.mainProcessId !== undefined ) {
messaging.port.postMessage({
mainProcessId: details.mainProcessId,
msg: response
});
2015-06-26 06:08:41 +02:00
}
};
/******************************************************************************/
2015-07-07 17:03:26 +02:00
var MessagingChannel = function(name, listener) {
2015-06-26 06:08:41 +02:00
this.channelName = name;
2015-07-07 17:03:26 +02:00
this.listeners = typeof listener === 'function' ? [listener] : [];
2015-06-26 06:08:41 +02:00
this.refCount = 1;
2015-07-07 17:03:26 +02:00
if ( typeof listener === 'function' ) {
2015-06-26 06:08:41 +02:00
var messaging = vAPI.messaging;
if ( messaging.port === null ) {
messaging.setup();
}
}
};
MessagingChannel.prototype.send = function(message, callback) {
this.sendTo(message, undefined, undefined, callback);
};
MessagingChannel.prototype.sendTo = function(message, toTabId, toChannel, callback) {
2015-06-26 06:08:41 +02:00
var messaging = vAPI.messaging;
2015-07-07 17:03:26 +02:00
// Too large a gap between the last request and the last response means
// the main process is no longer reachable: memory leaks and bad
// performance become a risk -- especially for long-lived, dynamic
// pages. Guard against this.
if ( messaging.pendingCount > 25 ) {
2015-07-13 01:33:12 +02:00
//console.error('uBlock> Sigh. Main process is sulking. Will try to patch things up.');
2015-07-07 17:03:26 +02:00
messaging.close();
}
2015-06-26 06:08:41 +02:00
if ( messaging.port === null ) {
2015-07-07 17:03:26 +02:00
if ( messaging.setup() === false ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
2015-06-26 06:08:41 +02:00
}
var auxProcessId;
2015-06-26 06:08:41 +02:00
if ( callback ) {
auxProcessId = messaging.auxProcessId++;
messaging.pending[auxProcessId] = callback;
2015-07-07 17:03:26 +02:00
messaging.pendingCount += 1;
2015-06-26 06:08:41 +02:00
}
messaging.port.postMessage({
channelName: this.channelName,
auxProcessId: auxProcessId,
toTabId: toTabId,
toChannel: toChannel,
2015-06-26 06:08:41 +02:00
msg: message
});
};
MessagingChannel.prototype.close = function() {
this.refCount -= 1;
if ( this.refCount !== 0 ) {
return;
}
var messaging = vAPI.messaging;
delete messaging.channels[this.channelName];
if ( Object.keys(messaging.channels).length === 0 ) {
messaging.close();
}
2015-06-26 06:08:41 +02:00
};
2015-06-26 06:08:41 +02:00
MessagingChannel.prototype.addListener = function(callback) {
if ( typeof callback !== 'function' ) {
return;
}
if ( this.listeners.indexOf(callback) !== -1 ) {
throw new Error('Duplicate listener.');
}
this.listeners.push(callback);
2015-06-26 06:08:41 +02:00
var messaging = vAPI.messaging;
if ( messaging.port === null ) {
messaging.setup();
}
2015-06-26 06:08:41 +02:00
};
2015-06-26 06:08:41 +02:00
MessagingChannel.prototype.removeListener = function(callback) {
if ( typeof callback !== 'function' ) {
return;
}
var pos = this.listeners.indexOf(callback);
if ( pos === -1 ) {
throw new Error('Listener not found.');
}
this.listeners.splice(pos, 1);
};
2015-06-28 23:42:08 +02:00
MessagingChannel.prototype.removeAllListeners = function() {
this.listeners = [];
2015-06-28 23:42:08 +02:00
};
MessagingChannel.prototype.sendToListeners = function(msg) {
var response;
var listeners = this.listeners;
for ( var i = 0, n = listeners.length; i < n; i++ ) {
response = listeners[i](msg);
if ( response !== undefined ) {
break;
2015-06-26 06:08:41 +02:00
}
}
return response;
};
// https://www.youtube.com/watch?v=rT5zCHn0tsg
// https://www.youtube.com/watch?v=E-jS4e3zacI
/******************************************************************************/
// 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;
});
}
/******************************************************************************/
})(this);
/******************************************************************************/