uBlock/platform/chromium/vapi-client.js

470 lines
15 KiB
JavaScript
Raw Normal View History

/*******************************************************************************
2016-03-06 08:51:06 -07:00
uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2018 The uBlock Origin 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 page
'use strict';
/******************************************************************************/
// https://github.com/chrisaljoudi/uBlock/issues/456
// Skip if already injected.
// >>>>>>>> start of HUGE-IF-BLOCK
if ( typeof vAPI === 'object' && !vAPI.clientScript ) {
2016-01-02 17:33:32 -07:00
/******************************************************************************/
/******************************************************************************/
vAPI.clientScript = true;
vAPI.randomToken = function() {
return String.fromCharCode(Date.now() % 26 + 97) +
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
};
vAPI.sessionId = vAPI.randomToken();
vAPI.chrome = true;
vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self);
/******************************************************************************/
vAPI.shutdown = {
jobs: [],
add: function(job) {
this.jobs.push(job);
},
exec: function() {
2015-04-07 17:34:22 -06:00
var job;
while ( (job = this.jobs.pop()) ) {
2015-04-07 17:34:22 -06:00
job();
}
},
remove: function(job) {
var pos;
while ( (pos = this.jobs.indexOf(job)) !== -1 ) {
this.jobs.splice(pos, 1);
}
}
};
2015-04-07 17:34:22 -06:00
/******************************************************************************/
vAPI.messaging = {
port: null,
2016-03-06 08:51:06 -07:00
portTimer: null,
portTimerDelay: 10000,
channels: new Map(),
connections: new Map(),
pending: new Map(),
auxProcessId: 1,
2016-03-06 08:51:06 -07:00
shuttingDown: false,
Connection: function(handler, details) {
var messaging = vAPI.messaging;
this.messaging = messaging;
this.handler = handler;
this.id = details.id;
this.to = details.to;
this.toToken = details.toToken;
this.from = details.from;
this.fromToken = details.fromToken;
this.checkBound = this.check.bind(this);
this.checkTimer = undefined;
// On Firefox it appears ports are not automatically disconnected when
// navigating to another page.
if ( messaging.Connection.pagehide !== undefined ) { return; }
messaging.Connection.pagehide = function() {
for ( var connection of this.connections.values() ) {
connection.disconnect();
connection.handler(connection.toDetails('connectionBroken'));
}
}.bind(messaging);
window.addEventListener('pagehide', messaging.Connection.pagehide);
},
2016-03-06 08:51:06 -07:00
shutdown: function() {
this.shuttingDown = true;
this.destroyPort();
},
2015-06-25 22:08:41 -06:00
2016-03-06 08:51:06 -07:00
disconnectListener: function() {
this.port = null;
vAPI.shutdown.exec();
},
disconnectListenerBound: null,
2016-03-06 08:51:06 -07:00
messageListener: function(details) {
if ( !details ) { return; }
2016-03-06 08:51:06 -07:00
// Sent to all channels
if ( details.broadcast ) {
for ( var channelName of this.channels.keys() ) {
2016-03-06 08:51:06 -07:00
this.sendToChannelListeners(channelName, details.msg);
}
return;
}
// Response to specific message previously sent
var listener;
2016-03-06 08:51:06 -07:00
if ( details.auxProcessId ) {
listener = this.pending.get(details.auxProcessId);
if ( listener !== undefined ) {
this.pending.delete(details.auxProcessId);
2016-03-06 08:51:06 -07:00
listener(details.msg);
return;
}
}
if ( details.channelName !== 'vapi' ) { return; }
// Internal handler
var connection;
switch ( details.msg.what ) {
case 'connectionAccepted':
case 'connectionBroken':
case 'connectionCheck':
case 'connectionMessage':
case 'connectionRefused':
connection = this.connections.get(details.msg.id);
if ( connection === undefined ) { return; }
connection.receive(details.msg);
break;
case 'connectionRequested':
var listeners = this.channels.get(details.msg.to);
if ( listeners === undefined ) { return; }
var port = this.getPort();
if ( port === null ) { return; }
for ( listener of listeners ) {
if ( listener(details.msg) !== true ) { continue; }
details.msg.what = 'connectionAccepted';
details.msg.toToken = port.name;
connection = new this.Connection(listener, details.msg);
this.connections.set(connection.id, connection);
break;
}
if ( details.msg.what !== 'connectionAccepted' ) {
details.msg.what = 'connectionRefused';
}
port.postMessage(details);
break;
default:
break;
2015-07-07 09:03:26 -06:00
}
},
2016-03-06 08:51:06 -07:00
messageListenerCallback: null,
portPoller: function() {
this.portTimer = null;
2017-10-21 11:43:46 -06:00
if (
this.port !== null &&
this.channels.size === 0 &&
this.connections.size === 0 &&
this.pending.size === 0
2017-10-21 11:43:46 -06:00
) {
return this.destroyPort();
2016-03-06 08:51:06 -07:00
}
this.portTimer = vAPI.setTimeout(this.portPollerBound, this.portTimerDelay);
2017-10-21 11:43:46 -06:00
this.portTimerDelay = Math.min(this.portTimerDelay * 2, 60 * 60 * 1000);
2016-03-06 08:51:06 -07:00
},
portPollerBound: null,
2015-06-25 22:08:41 -06:00
2016-03-06 08:51:06 -07:00
destroyPort: function() {
if ( this.portTimer !== null ) {
clearTimeout(this.portTimer);
this.portTimer = null;
}
2015-07-07 09:03:26 -06:00
var port = this.port;
if ( port !== null ) {
port.disconnect();
2016-03-06 08:51:06 -07:00
port.onMessage.removeListener(this.messageListenerCallback);
port.onDisconnect.removeListener(this.disconnectListenerBound);
this.port = null;
}
this.channels.clear();
if ( this.connections.size !== 0 ) {
for ( var connection of this.connections.values() ) {
connection.receive({ what: 'connectionBroken' });
}
this.connections.clear();
2016-03-06 08:51:06 -07:00
}
2015-07-07 09:03:26 -06:00
// service pending callbacks
if ( this.pending.size !== 0 ) {
var pending = this.pending;
this.pending = new Map();
for ( var callback of pending.values() ) {
2016-03-06 08:51:06 -07:00
if ( typeof callback === 'function' ) {
callback(null);
2015-07-07 09:03:26 -06:00
}
}
}
},
2015-06-28 15:42:08 -06:00
2016-03-06 08:51:06 -07:00
createPort: function() {
if ( this.shuttingDown ) { return null; }
2016-03-06 08:51:06 -07:00
if ( this.messageListenerCallback === null ) {
this.messageListenerCallback = this.messageListener.bind(this);
this.disconnectListenerBound = this.disconnectListener.bind(this);
this.portPollerBound = this.portPoller.bind(this);
}
2016-03-06 08:51:06 -07:00
try {
2016-03-15 09:17:56 -06:00
this.port = chrome.runtime.connect({name: vAPI.sessionId}) || null;
2016-03-06 08:51:06 -07:00
} catch (ex) {
2016-03-15 09:17:56 -06:00
this.port = null;
}
if ( this.port !== null ) {
this.port.onMessage.addListener(this.messageListenerCallback);
this.port.onDisconnect.addListener(this.disconnectListenerBound);
this.portTimerDelay = 10000;
if ( this.portTimer === null ) {
this.portTimer = vAPI.setTimeout(
this.portPollerBound,
this.portTimerDelay
);
}
2015-06-25 22:08:41 -06:00
}
2016-03-15 09:17:56 -06:00
return this.port;
2016-03-06 08:51:06 -07:00
},
getPort: function() {
2016-03-06 08:51:06 -07:00
return this.port !== null ? this.port : this.createPort();
},
2015-06-25 22:08:41 -06:00
2016-03-06 08:51:06 -07:00
send: function(channelName, message, callback) {
// 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 ( this.pending.size > 25 ) {
2016-03-06 08:51:06 -07:00
vAPI.shutdown.exec();
2015-06-25 22:08:41 -06:00
}
var port = this.getPort();
2016-03-06 08:51:06 -07:00
if ( port === null ) {
if ( typeof callback === 'function' ) { callback(); }
2015-07-07 09:03:26 -06:00
return;
}
2016-03-06 08:51:06 -07:00
var auxProcessId;
if ( callback ) {
auxProcessId = this.auxProcessId++;
this.pending.set(auxProcessId, callback);
2016-03-06 08:51:06 -07:00
}
port.postMessage({
channelName: channelName,
auxProcessId: auxProcessId,
msg: message
});
},
connectTo: function(from, to, handler) {
var port = this.getPort();
if ( port === null ) { return; }
var connection = new this.Connection(handler, {
id: from + '-' + to + '-' + vAPI.sessionId,
to: to,
from: from,
fromToken: port.name
});
this.connections.set(connection.id, connection);
port.postMessage({
channelName: 'vapi',
msg: {
what: 'connectionRequested',
id: connection.id,
from: from,
fromToken: port.name,
to: to
}
});
return connection.id;
2016-03-06 08:51:06 -07:00
},
disconnectFrom: function(connectionId) {
var connection = this.connections.get(connectionId);
if ( connection === undefined ) { return; }
connection.disconnect();
},
sendTo: function(connectionId, payload) {
var connection = this.connections.get(connectionId);
if ( connection === undefined ) { return; }
connection.send(payload);
},
addChannelListener: function(channelName, listener) {
var listeners = this.channels.get(channelName);
2016-03-06 08:51:06 -07:00
if ( listeners === undefined ) {
this.channels.set(channelName, [ listener ]);
} else if ( listeners.indexOf(listener) === -1 ) {
listeners.push(listener);
2016-03-06 08:51:06 -07:00
}
this.getPort();
},
removeChannelListener: function(channelName, listener) {
var listeners = this.channels.get(channelName);
if ( listeners === undefined ) { return; }
var pos = listeners.indexOf(listener);
if ( pos === -1 ) { return; }
2016-03-06 08:51:06 -07:00
listeners.splice(pos, 1);
if ( listeners.length === 0 ) {
this.channels.delete(channelName);
2016-03-06 08:51:06 -07:00
}
},
2016-03-06 08:51:06 -07:00
removeAllChannelListeners: function(channelName) {
this.channels.delete(channelName);
2016-03-06 08:51:06 -07:00
},
2015-06-28 15:42:08 -06:00
2016-03-06 08:51:06 -07:00
sendToChannelListeners: function(channelName, msg) {
var listeners = this.channels.get(channelName);
2017-10-21 11:43:46 -06:00
if ( listeners === undefined ) { return; }
listeners = listeners.slice(0);
2016-03-06 08:51:06 -07:00
var response;
2017-10-21 11:43:46 -06:00
for ( var listener of listeners ) {
response = listener(msg);
if ( response !== undefined ) { break; }
2015-06-25 22:08:41 -06:00
}
2016-03-06 08:51:06 -07:00
return response;
}
};
2016-03-06 08:51:06 -07:00
/******************************************************************************/
vAPI.messaging.Connection.prototype = {
toDetails: function(what, payload) {
return {
what: what,
id: this.id,
from: this.from,
fromToken: this.fromToken,
to: this.to,
toToken: this.toToken,
payload: payload
};
},
disconnect: function() {
if ( this.checkTimer !== undefined ) {
clearTimeout(this.checkTimer);
this.checkTimer = undefined;
}
this.messaging.connections.delete(this.id);
var port = this.messaging.getPort();
if ( port === null ) { return; }
port.postMessage({
channelName: 'vapi',
msg: this.toDetails('connectionBroken')
});
},
checkAsync: function() {
if ( this.checkTimer !== undefined ) {
clearTimeout(this.checkTimer);
}
this.checkTimer = vAPI.setTimeout(this.checkBound, 499);
},
check: function() {
this.checkTimer = undefined;
if ( this.messaging.connections.has(this.id) === false ) { return; }
var port = this.messaging.getPort();
if ( port === null ) { return; }
port.postMessage({
channelName: 'vapi',
msg: this.toDetails('connectionCheck')
});
this.checkAsync();
},
receive: function(details) {
switch ( details.what ) {
case 'connectionAccepted':
this.toToken = details.toToken;
this.handler(details);
this.checkAsync();
break;
case 'connectionBroken':
this.messaging.connections.delete(this.id);
this.handler(details);
break;
case 'connectionMessage':
this.handler(details);
this.checkAsync();
break;
case 'connectionCheck':
var port = this.messaging.getPort();
if ( port === null ) { return; }
if ( this.messaging.connections.has(this.id) ) {
this.checkAsync();
} else {
details.what = 'connectionBroken';
port.postMessage({ channelName: 'vapi', msg: details });
}
break;
case 'connectionRefused':
this.messaging.connections.delete(this.id);
this.handler(details);
break;
}
},
send: function(payload) {
var port = this.messaging.getPort();
if ( port === null ) { return; }
port.postMessage({
channelName: 'vapi',
msg: this.toDetails('connectionMessage', payload)
});
}
};
/******************************************************************************/
2016-03-06 08:51:06 -07:00
vAPI.shutdown.add(function() {
vAPI.messaging.shutdown();
window.vAPI = undefined;
2016-03-06 08:51:06 -07:00
});
// https://www.youtube.com/watch?v=rT5zCHn0tsg
// https://www.youtube.com/watch?v=E-jS4e3zacI
/******************************************************************************/
/******************************************************************************/
}
// <<<<<<<< end of HUGE-IF-BLOCK
/*******************************************************************************
DO NOT:
- Remove the following code
- Add code beyond the following code
Reason:
- https://github.com/gorhill/uBlock/pull/3721
- uBO never uses the return value from injected content scripts
**/
void 0;