pull/2/head
Raymond Hill 7 years ago
commit 6d85d53868
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

@ -73,59 +73,77 @@ vAPI.shutdown = (function() {
/******************************************************************************/ /******************************************************************************/
var messagingConnector = function(response) { vAPI.messaging = {
if ( !response ) { listeners: new Set(),
return; pending: new Map(),
} requestId: 1,
connected: false,
var channels = vAPI.messaging.channels;
var channel, listener;
if ( response.broadcast && !response.channelName ) { setup: function() {
for ( channel in channels ) { this.addListener(this.builtinListener);
if ( channels.hasOwnProperty(channel) === false ) { if ( this.toggleListenerCallback === null ) {
continue; this.toggleListenerCallback = this.toggleListener.bind(this);
} }
listener = channels[channel].listener; window.addEventListener('pagehide', this.toggleListenerCallback, true);
if ( typeof listener === 'function' ) { window.addEventListener('pageshow', this.toggleListenerCallback, true);
listener(response.msg); },
shutdown: function() {
if ( this.toggleListenerCallback !== null ) {
window.removeEventListener('pagehide', this.toggleListenerCallback, true);
window.removeEventListener('pageshow', this.toggleListenerCallback, true);
} }
this.removeAllListeners();
//service pending callbacks
var pending = this.pending;
this.pending.clear();
for ( var callback of pending.values() ) {
if ( typeof callback === 'function' ) {
callback(null);
} }
return;
} }
},
if ( response.requestId ) { connect: function() {
listener = vAPI.messaging.listeners[response.requestId]; if ( !this.connected ) {
delete vAPI.messaging.listeners[response.requestId]; if ( this.messageListenerCallback === null ) {
delete response.requestId; this.messageListenerCallback = this.messageListener.bind(this);
} }
addMessageListener(this.messageListenerCallback);
if ( !listener ) { this.connected = true;
channel = channels[response.channelName];
listener = channel && channel.listener;
} }
},
if ( typeof listener === 'function' ) { disconnect: function() {
listener(response.msg); if ( this.connected ) {
removeMessageListener();
this.connected = false;
} }
}; },
/******************************************************************************/
vAPI.messaging = { messageListener: function(msg) {
channels: {}, var details = JSON.parse(msg);
listeners: {}, if ( !details ) {
requestId: 1, return;
}
setup: function() { if ( details.broadcast ) {
this.connector = function(msg) { this.sendToListeners(details.msg);
messagingConnector(JSON.parse(msg)); return;
}; }
addMessageListener(this.connector); if ( details.requestId ) {
var listener = this.pending.get(details.requestId);
if ( listener !== undefined ) {
this.pending.delete(details.requestId);
listener(details.msg);
return;
}
}
},
messageListenerCallback: null,
this.channels.vAPI = { builtinListener: function(msg) {
listener: function(msg) {
if ( typeof msg.cmd === 'string' && msg.cmd === 'injectScript' ) { if ( typeof msg.cmd === 'string' && msg.cmd === 'injectScript' ) {
var details = msg.details; var details = msg.details;
if ( !details.allFrames && window !== window.top ) { if ( !details.allFrames && window !== window.top ) {
@ -133,72 +151,61 @@ vAPI.messaging = {
} }
self.injectScript(details.file); self.injectScript(details.file);
} }
}
};
},
close: function() {
if ( !this.connector ) {
return;
}
removeMessageListener();
this.connector = null;
this.channels = {};
this.listeners = {};
}, },
channel: function(channelName, callback) { send: function(channelName, message, callback) {
if ( !channelName ) { this.connect()
return;
}
this.channels[channelName] = {
channelName: channelName,
listener: typeof callback === 'function' ? callback : null,
send: function(message, callback) {
if ( !vAPI.messaging.connector ) {
vAPI.messaging.setup();
}
message = { message = {
channelName: self._sandboxId_ + '|' + this.channelName, channelName: self._sandboxId_ + '|' + channelName,
msg: message msg: message
}; };
if ( callback ) { if ( callback ) {
message.requestId = vAPI.messaging.requestId++; message.requestId = this.requestId++;
vAPI.messaging.listeners[message.requestId] = callback; this.pending.set(message.requestId, callback);
} }
sendAsyncMessage('umatrix:background', message); sendAsyncMessage('umatrix:background', message);
}, },
close: function() {
delete vAPI.messaging.channels[this.channelName];
}
};
return this.channels[channelName];
},
toggleListener: function({type, persisted}) { toggleListener: function({type, persisted}) {
if ( !vAPI.messaging.connector ) { if ( type === 'pagehide' && !persisted ) {
vAPI.shutdown.exec();
this.shutdown();
return; return;
} }
if ( type === 'pagehide' ) { if ( type === 'pagehide' ) {
removeMessageListener(); this.disconnect();
return; } else /* if ( type === 'pageshow' ) */ {
this.connect();
} }
},
toggleListenerCallback: null,
if ( persisted ) { sendToListeners: function(msg) {
addMessageListener(vAPI.messaging.connector); for ( var listener of this.listeners ) {
listener(msg);
} }
},
addListener: function(listener) {
this.listeners.add(listener);
this.connect()
},
removeListener: function(listener) {
this.listeners.delete(listener);
},
removeAllListeners: function() {
this.disconnect();
this.listeners.clear();;
} }
}; };
window.addEventListener('pagehide', vAPI.messaging.toggleListener, true); vAPI.messaging.setup()
window.addEventListener('pageshow', vAPI.messaging.toggleListener, true);
/******************************************************************************/ /******************************************************************************/
@ -206,7 +213,9 @@ window.addEventListener('pageshow', vAPI.messaging.toggleListener, true);
// we are not a top window (because element picker can still // we are not a top window (because element picker can still
// be injected in top window). // be injected in top window).
if ( window !== window.top ) { if ( window !== window.top ) {
// Can anything be done? vAPI.shutdown.add(function() {
vAPI = null;
});
} }
/******************************************************************************/ /******************************************************************************/

Loading…
Cancel
Save