Use the same code for the different frontend channels
This commit is contained in:
parent
1e672c4a6b
commit
41a1148c17
@ -8,22 +8,30 @@ define([
|
|||||||
return Math.random().toString(16).replace('0.', '') + Math.random().toString(16).replace('0.', '');
|
return Math.random().toString(16).replace('0.', '') + Math.random().toString(16).replace('0.', '');
|
||||||
};
|
};
|
||||||
|
|
||||||
var create = function (onMsg, postMsg, cb, isWorker) {
|
var create = function (onMsg, postMsg, cb) {
|
||||||
var chanLoaded;
|
var chanLoaded;
|
||||||
var waitingData;
|
var waitingData = [];
|
||||||
if (!isWorker) {
|
|
||||||
chanLoaded = false;
|
|
||||||
waitingData = [];
|
|
||||||
onMsg.reg(function (data) {
|
|
||||||
if (chanLoaded) { return; }
|
|
||||||
waitingData.push(data);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var evReady = Util.mkEvent(true);
|
var evReady = Util.mkEvent(true);
|
||||||
|
|
||||||
|
onMsg.reg(function (msg) {
|
||||||
|
if (chanLoaded) { return; }
|
||||||
|
var data = msg.data;
|
||||||
|
if (data === '_READY') {
|
||||||
|
postMsg('_READY');
|
||||||
|
chanLoaded = true;
|
||||||
|
evReady.fire();
|
||||||
|
waitingData.forEach(function (d) {
|
||||||
|
onMsg.fire(d);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
waitingData.push(data);
|
||||||
|
});
|
||||||
|
|
||||||
var handlers = {};
|
var handlers = {};
|
||||||
var queries = {};
|
var queries = {};
|
||||||
|
var acks = {};
|
||||||
|
|
||||||
// list of handlers which are registered from the other side...
|
// list of handlers which are registered from the other side...
|
||||||
var insideHandlers = [];
|
var insideHandlers = [];
|
||||||
@ -32,16 +40,24 @@ define([
|
|||||||
var chan = {};
|
var chan = {};
|
||||||
|
|
||||||
// Send a query. channel.query('Q_SOMETHING', { args: "whatever" }, function (reply) { ... });
|
// Send a query. channel.query('Q_SOMETHING', { args: "whatever" }, function (reply) { ... });
|
||||||
|
// We have a timeout for receiving an ACK, but unlimited time for receiving an answer to the query
|
||||||
chan.query = function (q, content, cb, opts) {
|
chan.query = function (q, content, cb, opts) {
|
||||||
var txid = mkTxid();
|
var txid = mkTxid();
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
var to = opts.timeout || 30000;
|
var to = opts.timeout || 30000;
|
||||||
var timeout = setTimeout(function () {
|
var timeout = setTimeout(function () {
|
||||||
delete queries[txid];
|
delete queries[txid];
|
||||||
//console.log("Timeout making query " + q);
|
cb('TIMEOUT');
|
||||||
}, to);
|
}, to);
|
||||||
queries[txid] = function (data, msg) {
|
acks[txid] = function (err) {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
|
delete acks[txid];
|
||||||
|
if (err) {
|
||||||
|
delete queries[txid];
|
||||||
|
cb('UNHANDLED');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
queries[txid] = function (data, msg) {
|
||||||
delete queries[txid];
|
delete queries[txid];
|
||||||
cb(undefined, data.content, msg);
|
cb(undefined, data.content, msg);
|
||||||
};
|
};
|
||||||
@ -103,10 +119,10 @@ define([
|
|||||||
}
|
}
|
||||||
insideHandlers.push(content);
|
insideHandlers.push(content);
|
||||||
});
|
});
|
||||||
chan.whenReg('EV_REGISTER_HANDLER', evReady.fire);
|
//chan.whenReg('EV_REGISTER_HANDLER', evReady.fire);
|
||||||
|
|
||||||
// Make sure both iframes are ready
|
// Make sure both iframes are ready
|
||||||
var isReady =false;
|
var isReady = false;
|
||||||
chan.onReady = function (h) {
|
chan.onReady = function (h) {
|
||||||
if (isReady) {
|
if (isReady) {
|
||||||
return void h();
|
return void h();
|
||||||
@ -121,29 +137,43 @@ define([
|
|||||||
};
|
};
|
||||||
|
|
||||||
onMsg.reg(function (msg) {
|
onMsg.reg(function (msg) {
|
||||||
|
if (!chanLoaded) { return; }
|
||||||
|
if (!msg.data || msg.data === '_READY') { return; }
|
||||||
var data = JSON.parse(msg.data);
|
var data = JSON.parse(msg.data);
|
||||||
if (typeof(data.q) === 'string' && handlers[data.q]) {
|
if (typeof(data.ack) !== "undefined") {
|
||||||
handlers[data.q].forEach(function (f) {
|
if (acks[data.txid]) { acks[data.txid](!data.ack); }
|
||||||
f(data || JSON.parse(msg.data), msg);
|
} else if (typeof(data.q) === 'string') {
|
||||||
data = undefined;
|
if (handlers[data.q]) {
|
||||||
});
|
// If this is a "query", send an ack
|
||||||
|
if (data.txid) {
|
||||||
|
postMsg(JSON.stringify({
|
||||||
|
txid: data.txid,
|
||||||
|
ack: true
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
handlers[data.q].forEach(function (f) {
|
||||||
|
f(data || JSON.parse(msg.data), msg);
|
||||||
|
data = undefined;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if (data.txid) {
|
||||||
|
postMsg(JSON.stringify({
|
||||||
|
txid: data.txid,
|
||||||
|
ack: false
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (typeof(data.q) === 'undefined' && queries[data.txid]) {
|
} else if (typeof(data.q) === 'undefined' && queries[data.txid]) {
|
||||||
queries[data.txid](data, msg);
|
queries[data.txid](data, msg);
|
||||||
} else {
|
} else {
|
||||||
console.log("DROP Unhandled message");
|
/*console.log("DROP Unhandled message");
|
||||||
console.log(msg.data, isWorker);
|
console.log(msg.data, window);
|
||||||
console.log(msg);
|
console.log(msg);*/
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (isWorker) {
|
|
||||||
evReady.fire();
|
postMsg('_READY');
|
||||||
} else {
|
|
||||||
chanLoaded = true;
|
|
||||||
waitingData.forEach(function (d) {
|
|
||||||
onMsg.fire(d);
|
|
||||||
});
|
|
||||||
waitingData = [];
|
|
||||||
}
|
|
||||||
cb(chan);
|
cb(chan);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -36,7 +36,7 @@ define([
|
|||||||
'/common/cryptpad-common.js',
|
'/common/cryptpad-common.js',
|
||||||
'/bower_components/chainpad-crypto/crypto.js',
|
'/bower_components/chainpad-crypto/crypto.js',
|
||||||
'/common/cryptget.js',
|
'/common/cryptget.js',
|
||||||
'/common/sframe-channel.js',
|
'/common/outer/worker-channel.js',
|
||||||
'/filepicker/main.js',
|
'/filepicker/main.js',
|
||||||
'/common/common-messaging.js',
|
'/common/common-messaging.js',
|
||||||
'/common/common-notifier.js',
|
'/common/common-notifier.js',
|
||||||
@ -89,9 +89,36 @@ define([
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
SFrameChannel.create($('#sbox-iframe')[0].contentWindow, waitFor(function (sfc) {
|
// The inner iframe tries to get some data from us every ms (cache, store...).
|
||||||
sframeChan = sfc;
|
// It will send a "READY" message and wait for our answer with the correct txid.
|
||||||
}), false, { cache: cache, localStore: localStore, language: Cryptpad.getLanguage() });
|
// First, we have to answer to this message, otherwise we're going to block
|
||||||
|
// sframe-boot.js. Then we can start the channel.
|
||||||
|
var msgEv = _Util.mkEvent();
|
||||||
|
var iframe = $('#sbox-iframe')[0].contentWindow;
|
||||||
|
var iframeReady = false;
|
||||||
|
var postMsg = function (data) {
|
||||||
|
iframe.postMessage(data, '*');
|
||||||
|
};
|
||||||
|
var whenReady = waitFor(function (msg) {
|
||||||
|
if (msg.source !== iframe) { return; }
|
||||||
|
var data = JSON.parse(msg.data);
|
||||||
|
if (!data.txid) { return; }
|
||||||
|
// Remove the listener once we've received the READY message
|
||||||
|
window.removeEventListener('message', whenReady);
|
||||||
|
// Answer with the requested data
|
||||||
|
postMsg(JSON.stringify({ txid: data.txid, cache: cache, localStore: localStore, language: Cryptpad.getLanguage() }));
|
||||||
|
|
||||||
|
// Then start the channel
|
||||||
|
window.addEventListener('message', function (msg) {
|
||||||
|
if (msg.source !== iframe) { return; }
|
||||||
|
msgEv.fire(msg);
|
||||||
|
});
|
||||||
|
SFrameChannel.create(msgEv, postMsg, waitFor(function (sfc) {
|
||||||
|
sframeChan = sfc;
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
window.addEventListener('message', whenReady);
|
||||||
|
|
||||||
Cryptpad.loading.onDriveEvent.reg(function (data) {
|
Cryptpad.loading.onDriveEvent.reg(function (data) {
|
||||||
if (sframeChan) { sframeChan.event('EV_LOADING_INFO', data); }
|
if (sframeChan) { sframeChan.event('EV_LOADING_INFO', data); }
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,7 +3,7 @@ define([
|
|||||||
'/bower_components/nthen/index.js',
|
'/bower_components/nthen/index.js',
|
||||||
'/customize/messages.js',
|
'/customize/messages.js',
|
||||||
'/common/sframe-chainpad-netflux-inner.js',
|
'/common/sframe-chainpad-netflux-inner.js',
|
||||||
'/common/sframe-channel.js',
|
'/common/outer/worker-channel.js',
|
||||||
'/common/sframe-common-title.js',
|
'/common/sframe-common-title.js',
|
||||||
'/common/common-ui-elements.js',
|
'/common/common-ui-elements.js',
|
||||||
'/common/sframe-common-history.js',
|
'/common/sframe-common-history.js',
|
||||||
@ -481,8 +481,16 @@ define([
|
|||||||
window.CryptPad_sframe_common = true;
|
window.CryptPad_sframe_common = true;
|
||||||
|
|
||||||
nThen(function (waitFor) {
|
nThen(function (waitFor) {
|
||||||
SFrameChannel.create(window.parent, waitFor(function (sfc) { ctx.sframeChan = sfc; }), true);
|
var msgEv = Util.mkEvent();
|
||||||
// CpNfInner.start() should be here....
|
var iframe = window.parent;
|
||||||
|
window.addEventListener('message', function (msg) {
|
||||||
|
if (msg.source !== iframe) { return; }
|
||||||
|
msgEv.fire(msg);
|
||||||
|
});
|
||||||
|
var postMsg = function (data) {
|
||||||
|
iframe.postMessage(data, '*');
|
||||||
|
};
|
||||||
|
SFrameChannel.create(msgEv, postMsg, waitFor(function (sfc) { ctx.sframeChan = sfc; }));
|
||||||
}).nThen(function (waitFor) {
|
}).nThen(function (waitFor) {
|
||||||
localForage.clear();
|
localForage.clear();
|
||||||
Language.applyTranslation();
|
Language.applyTranslation();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user