cryptpad/www/common/sframe-channel.js

167 lines
6.5 KiB
JavaScript
Raw Normal View History

2017-08-09 12:45:39 +00:00
// This file provides the API for the channel for talking to and from the sandbox iframe.
define([
'/common/sframe-protocol.js',
'/common/common-util.js'
], function (SFrameProtocol, Util) {
2017-08-07 15:23:28 +00:00
var mkTxid = function () {
return Math.random().toString(16).replace('0.', '') + Math.random().toString(16).replace('0.', '');
};
var create = function (ow, cb, isSandbox, sendData) {
2017-08-09 15:37:55 +00:00
var otherWindow;
var evReady = Util.mkEvent(true);
2017-08-09 15:37:55 +00:00
var handlers = {};
var queries = {};
// list of handlers which are registered from the other side...
var insideHandlers = [];
var callWhenRegistered = {};
var chan = {};
2017-08-10 16:31:44 +00:00
// Send a query. channel.query('Q_SOMETHING', { args: "whatever" }, function (reply) { ... });
2018-07-19 15:51:38 +00:00
chan.query = function (q, content, cb, opts) {
2017-08-09 15:37:55 +00:00
if (!otherWindow) { throw new Error('not yet initialized'); }
if (!SFrameProtocol[q]) {
throw new Error('please only make queries are defined in sframe-protocol.js');
}
2018-07-19 15:51:38 +00:00
opts = opts || {};
2017-08-09 15:37:55 +00:00
var txid = mkTxid();
2018-07-19 15:51:38 +00:00
var to = opts.timeout || 30000;
2017-08-09 15:37:55 +00:00
var timeout = setTimeout(function () {
delete queries[txid];
console.log("Timeout making query " + q);
2018-07-19 15:51:38 +00:00
}, to);
2017-08-09 15:37:55 +00:00
queries[txid] = function (data, msg) {
clearTimeout(timeout);
delete queries[txid];
cb(undefined, data.content, msg);
};
evReady.reg(function () {
otherWindow.postMessage(JSON.stringify({
txid: txid,
content: content,
q: q
}), '*');
});
2017-08-09 15:37:55 +00:00
};
2017-08-10 16:31:44 +00:00
// Fire an event. channel.event('EV_SOMETHING', { args: "whatever" });
2017-08-09 15:37:55 +00:00
var event = chan.event = function (e, content) {
if (!SFrameProtocol[e]) {
throw new Error('please only fire events that are defined in sframe-protocol.js');
}
if (e.indexOf('EV_') !== 0) {
throw new Error('please only use events (starting with EV_) for event messages');
}
evReady.reg(function () {
otherWindow.postMessage(JSON.stringify({ content: content, q: e }), '*');
});
2017-08-09 15:37:55 +00:00
};
2017-08-10 16:31:44 +00:00
// Be notified on query or event. channel.on('EV_SOMETHING', function (args, reply) { ... });
// If the type is a query, your handler will be invoked with a reply function that takes
// one argument (the content to reply with).
2017-08-10 12:49:21 +00:00
chan.on = function (queryType, handler, quiet) {
2017-08-09 15:37:55 +00:00
if (!SFrameProtocol[queryType]) {
throw new Error('please only register handlers which are defined in sframe-protocol.js');
}
2017-08-10 12:49:21 +00:00
(handlers[queryType] = handlers[queryType] || []).push(function (data, msg) {
2017-08-09 15:37:55 +00:00
handler(data.content, function (replyContent) {
2017-08-10 16:31:44 +00:00
if (queryType.indexOf('Q_') !== 0) { throw new Error("replies to events are invalid"); }
2017-08-09 15:37:55 +00:00
msg.source.postMessage(JSON.stringify({
txid: data.txid,
content: replyContent
}), '*');
}, msg);
2017-08-10 12:49:21 +00:00
});
if (!quiet) {
event('EV_REGISTER_HANDLER', queryType);
}
2017-08-09 15:37:55 +00:00
};
2017-08-10 16:31:44 +00:00
// If a particular handler is registered, call the callback immediately, otherwise it will be called
// when that handler is first registered.
// channel.whenReg('Q_SOMETHING', function () { ...query Q_SOMETHING?... });
chan.whenReg = function (queryType, cb, always) {
2017-08-09 15:37:55 +00:00
if (!SFrameProtocol[queryType]) {
throw new Error('please only register handlers which are defined in sframe-protocol.js');
}
2017-08-10 16:31:44 +00:00
var reg = always;
2017-08-09 15:37:55 +00:00
if (insideHandlers.indexOf(queryType) > -1) {
2017-08-10 16:31:44 +00:00
cb();
2017-08-09 15:37:55 +00:00
} else {
2017-08-10 16:31:44 +00:00
reg = true;
}
if (reg) {
(callWhenRegistered[queryType] = callWhenRegistered[queryType] || []).push(cb);
2017-08-09 15:37:55 +00:00
}
};
2017-08-10 16:31:44 +00:00
// Same as whenReg except it will invoke every time there is another registration, not just once.
chan.onReg = function (queryType, cb) { chan.whenReg(queryType, cb, true); };
chan.on('EV_REGISTER_HANDLER', function (content) {
if (callWhenRegistered[content]) {
callWhenRegistered[content].forEach(function (f) { f(); });
delete callWhenRegistered[content];
2017-08-09 15:37:55 +00:00
}
2017-08-10 16:31:44 +00:00
insideHandlers.push(content);
});
chan.whenReg('EV_REGISTER_HANDLER', evReady.fire);
2017-08-09 15:37:55 +00:00
// Make sure both iframes are ready
var isReady =false;
chan.onReady = function (h) {
if (isReady) {
return void h();
}
if (typeof(h) !== "function") { return; }
chan.on('EV_RPC_READY', function () { isReady = true; h(); });
};
chan.ready = function () {
chan.whenReg('EV_RPC_READY', function () {
chan.event('EV_RPC_READY');
});
};
2017-08-09 12:45:39 +00:00
var txid;
window.addEventListener('message', function (msg) {
var data = JSON.parse(msg.data);
if (ow !== msg.source) {
return;
//console.log("DROP Message from unexpected source");
//console.log(msg);
2017-08-09 12:45:39 +00:00
} else if (!otherWindow) {
otherWindow = ow;
sendData = sendData || {};
sendData.txid = data.txid;
ow.postMessage(JSON.stringify(sendData), '*');
2017-08-09 15:37:55 +00:00
cb(chan);
2017-08-09 12:45:39 +00:00
} else if (typeof(data.q) === 'string' && handlers[data.q]) {
2017-08-10 12:49:21 +00:00
handlers[data.q].forEach(function (f) {
f(data || JSON.parse(msg.data), msg);
data = undefined;
});
2017-08-09 12:45:39 +00:00
} else if (typeof(data.q) === 'undefined' && queries[data.txid]) {
2017-08-21 09:08:27 +00:00
queries[data.txid](data, msg);
2017-08-09 12:45:39 +00:00
} else if (data.txid === txid) {
// stray message from init
return;
} else {
console.log("DROP Unhandled message");
console.log(msg);
}
});
2017-09-06 08:56:27 +00:00
if (isSandbox) {
2017-08-09 12:45:39 +00:00
// we're in the sandbox
otherWindow = ow;
evReady.fire();
2017-08-09 15:37:55 +00:00
cb(chan);
2017-08-09 12:45:39 +00:00
}
};
2017-08-09 15:37:55 +00:00
return { create: create };
2017-08-09 12:45:39 +00:00
});