wip
parent
a612f02be2
commit
4b25ab80d6
@ -1,76 +0,0 @@
|
|||||||
// This file provides the external API for launching and talking to the sandboxed iframe.
|
|
||||||
// The internal API is in sframe-channel.js
|
|
||||||
define([
|
|
||||||
'/common/requireconfig.js'
|
|
||||||
], function (RequireConfig) {
|
|
||||||
var iframe;
|
|
||||||
var handlers = {};
|
|
||||||
var queries = {};
|
|
||||||
var module = { exports: {} };
|
|
||||||
|
|
||||||
var mkTxid = function () {
|
|
||||||
return Math.random().toString(16).replace('0.', '') + Math.random().toString(16).replace('0.', '');
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.init = function (frame, cb) {
|
|
||||||
if (iframe) { throw new Error('already initialized'); }
|
|
||||||
var txid = mkTxid();
|
|
||||||
var intr = setInterval(function () {
|
|
||||||
frame.contentWindow.postMessage(JSON.stringify({
|
|
||||||
txid: txid,
|
|
||||||
content: { requireConf: RequireConfig },
|
|
||||||
q: 'INIT'
|
|
||||||
}), '*');
|
|
||||||
});
|
|
||||||
window.addEventListener('message', function (msg) {
|
|
||||||
var data = JSON.parse(msg.data);
|
|
||||||
if (!iframe) {
|
|
||||||
if (data.txid !== txid) { return; }
|
|
||||||
clearInterval(intr);
|
|
||||||
iframe = frame;
|
|
||||||
cb();
|
|
||||||
} else if (typeof(data.q) === 'string' && handlers[data.q]) {
|
|
||||||
handlers[data.q](data, msg);
|
|
||||||
} else if (typeof(data.q) === 'undefined' && queries[data.txid]) {
|
|
||||||
queries[data.txid](data, msg);
|
|
||||||
} else {
|
|
||||||
console.log("Unhandled message");
|
|
||||||
console.log(msg);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.query = function (q, content, cb) {
|
|
||||||
if (!iframe) { throw new Error('not yet initialized'); }
|
|
||||||
var txid = mkTxid();
|
|
||||||
var timeout = setTimeout(function () {
|
|
||||||
delete queries[txid];
|
|
||||||
cb("Timeout making query " + q);
|
|
||||||
});
|
|
||||||
queries[txid] = function (data, msg) {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
delete queries[txid];
|
|
||||||
cb(undefined, data.content, msg);
|
|
||||||
};
|
|
||||||
iframe.contentWindow.postMessage(JSON.stringify({
|
|
||||||
txid: txid,
|
|
||||||
content: content,
|
|
||||||
q: q
|
|
||||||
}), '*');
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.registerHandler = function (queryType, handler) {
|
|
||||||
if (typeof(handlers[queryType]) !== 'undefined') { throw new Error('already registered'); }
|
|
||||||
handlers[queryType] = function (msg) {
|
|
||||||
var data = JSON.parse(msg.data);
|
|
||||||
handler(data.content, function (replyContent) {
|
|
||||||
msg.source.postMessage(JSON.stringify({
|
|
||||||
txid: data.txid,
|
|
||||||
content: replyContent
|
|
||||||
}), '*');
|
|
||||||
}, msg);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
return module.exports;
|
|
||||||
});
|
|
@ -1,5 +1,33 @@
|
|||||||
// This file defines all of the RPC calls
|
// This file defines all of the RPC calls which are used between the inner and outer iframe.
|
||||||
// The internal API is in sframe-channel.js
|
// Define *querys* (which expect a response) using Q_<query name>
|
||||||
|
// Define *events* (which expect no response) using EV_<event name>
|
||||||
|
// Please document the queries and events you create, and please please avoid making generic
|
||||||
|
// "do stuff" events/queries which are used for many different things because it makes the
|
||||||
|
// protocol unclear.
|
||||||
define({
|
define({
|
||||||
|
// When the iframe first launches, this query is sent repeatedly by the controller
|
||||||
|
// to wait for it to awake and give it the requirejs config to use.
|
||||||
|
'Q_INIT': true,
|
||||||
|
|
||||||
|
// When either the outside or inside registers a query handler, this is sent.
|
||||||
|
'EV_REGISTER_HANDLER': true,
|
||||||
|
|
||||||
|
// Realtime events called from the outside.
|
||||||
|
// When someone joins the pad, argument is a string with their netflux id.
|
||||||
|
'EV_RT_JOIN': true,
|
||||||
|
// When someone leaves the pad, argument is a string with their netflux id.
|
||||||
|
'EV_RT_LEAVE': true,
|
||||||
|
// When you have been disconnected, no arguments.
|
||||||
|
'EV_RT_DISCONNECT': true,
|
||||||
|
// When you have connected, argument is an object with myID: string, members: list, readOnly: boolean.
|
||||||
|
'EV_RT_CONNECT': true,
|
||||||
|
// Called after the history is finished synchronizing, no arguments.
|
||||||
|
'EV_RT_READY': true,
|
||||||
|
// Called from both outside and inside, argument is a (string) chainpad message.
|
||||||
|
'Q_RT_MESSAGE': true,
|
||||||
|
|
||||||
|
// Called from the outside, this informs the inside whenever the user's data has been changed.
|
||||||
|
// The argument is the object representing the content of the user profile minus the netfluxID
|
||||||
|
// which changes per-reconnect.
|
||||||
|
'EV_USERDATA_UPDATE': true
|
||||||
});
|
});
|
File diff suppressed because it is too large
Load Diff
@ -1,13 +1,39 @@
|
|||||||
|
|
||||||
define([
|
define([
|
||||||
'/common/sframe-ctrl.js',
|
'/common/sframe-channel.js',
|
||||||
'jquery'
|
'jquery',
|
||||||
], function (SFrameCtrl, $) {
|
'/common/sframe-chainpad-netflux-outer.js',
|
||||||
|
'/bower_components/nthen/index.js',
|
||||||
|
'/common/cryptpad-common.js',
|
||||||
|
'/bower_components/chainpad-crypto/crypto.js'
|
||||||
|
], function (SFrameChannel, $, CpNfOuter, nThen, Cryptpad, Crypto) {
|
||||||
console.log('xxx');
|
console.log('xxx');
|
||||||
$(function () {
|
nThen(function (waitFor) {
|
||||||
console.log('go');
|
$(waitFor());
|
||||||
SFrameCtrl.init($('#sbox-iframe')[0], function () {
|
}).nThen(function (waitFor) {
|
||||||
console.log('\n\ndone\n\n');
|
SFrameChannel.init($('#sbox-iframe')[0].contentWindow, waitFor(function () {
|
||||||
|
console.log('sframe initialized');
|
||||||
|
}));
|
||||||
|
Cryptpad.ready(waitFor());
|
||||||
|
}).nThen(function (waitFor) {
|
||||||
|
Cryptpad.onError(function (info) {
|
||||||
|
console.log('error');
|
||||||
|
console.log(info);
|
||||||
|
if (info && info.type === "store") {
|
||||||
|
//onConnectError();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).nThen(function (waitFor) {
|
||||||
|
var secret = Cryptpad.getSecrets();
|
||||||
|
var readOnly = secret.keys && !secret.keys.editKeyStr;
|
||||||
|
if (!secret.keys) { secret.keys = secret.key; }
|
||||||
|
|
||||||
|
var outer = CpNfOuter.start({
|
||||||
|
channel: secret.channel,
|
||||||
|
network: Cryptpad.getNetwork(),
|
||||||
|
validateKey: secret.keys.validateKey || undefined,
|
||||||
|
readOnly: readOnly,
|
||||||
|
crypto: Crypto.createEncryptor(secret.keys),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
Reference in New Issue