wip
This commit is contained in:
parent
65dfd99171
commit
35a55a15ed
@ -1,10 +1,9 @@
|
|||||||
// Stage 0, this gets cached which means we can't change it. boot2-sframe.js is changable.
|
// Stage 0, this gets cached which means we can't change it. boot2-sframe.js is changable.
|
||||||
// Note that this file is meant to be executed only inside of a sandbox iframe.
|
// Note that this file is meant to be executed only inside of a sandbox iframe.
|
||||||
window.addEventListener('message', function (msg) {
|
window.addEventListener('message', function (msg) {
|
||||||
var data = msg.data;
|
var data = JSON.parse(msg.data);
|
||||||
if (data.q !== 'INIT') { return; }
|
if (data.q !== 'INIT') { return; }
|
||||||
msg.source.postMessage({ txid: data.txid, res: 'OK' }, '*');
|
msg.source.postMessage({ txid: data.txid, content: 'OK' }, '*');
|
||||||
if (data.requireConf) { require.config(data.requireConf); }
|
if (data.content && data.content.requireConf) { require.config(data.content.requireConf); }
|
||||||
require(['/common/sframe-boot2.js'], function () { });
|
require(['/common/sframe-boot2.js'], function () { });
|
||||||
});
|
});
|
||||||
console.log('boot');
|
|
||||||
@ -22,6 +22,5 @@ console.log('boot2');
|
|||||||
window.__defineGetter__('localStorage', function () { return mkFakeStore(); });
|
window.__defineGetter__('localStorage', function () { return mkFakeStore(); });
|
||||||
window.__defineGetter__('sessionStorage', function () { return mkFakeStore(); });
|
window.__defineGetter__('sessionStorage', function () { return mkFakeStore(); });
|
||||||
|
|
||||||
|
|
||||||
require([document.querySelector('script[data-bootload]').getAttribute('data-bootload')]);
|
require([document.querySelector('script[data-bootload]').getAttribute('data-bootload')]);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -27,45 +27,24 @@ define([
|
|||||||
|
|
||||||
var unBencode = function (str) { return str.replace(/^\d+:/, ''); };
|
var unBencode = function (str) { return str.replace(/^\d+:/, ''); };
|
||||||
|
|
||||||
module.exports.start = function (config) {
|
module.exports.start = function (conf) {
|
||||||
var websocketUrl = config.websocketURL;
|
var websocketUrl = conf.websocketURL;
|
||||||
var userName = config.userName;
|
var userName = conf.userName;
|
||||||
var channel = config.channel;
|
var channel = conf.channel;
|
||||||
var Crypto = config.crypto;
|
var Crypto = conf.crypto;
|
||||||
var validateKey = config.validateKey;
|
var validateKey = conf.validateKey;
|
||||||
var readOnly = config.readOnly || false;
|
var readOnly = conf.readOnly || false;
|
||||||
|
var websocketURL = conf.websocketURL;
|
||||||
// make sure configuration is defined
|
var network = conf.network;
|
||||||
config = config || {};
|
conf = undefined;
|
||||||
|
|
||||||
var initializing = true;
|
var initializing = true;
|
||||||
var toReturn = {};
|
var toReturn = {};
|
||||||
var messagesHistory = [];
|
var messagesHistory = [];
|
||||||
var chainpadAdapter = {};
|
var chainpadAdapter = {};
|
||||||
var realtime;
|
var realtime;
|
||||||
var network = config.network;
|
|
||||||
var lastKnownHash;
|
var lastKnownHash;
|
||||||
|
|
||||||
var userList = {
|
|
||||||
change : [],
|
|
||||||
onChange : function(newData) {
|
|
||||||
userList.change.forEach(function (el) {
|
|
||||||
el(newData);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
users: []
|
|
||||||
};
|
|
||||||
|
|
||||||
var onJoining = function(peer) {
|
|
||||||
if(peer.length !== 32) { return; }
|
|
||||||
var list = userList.users;
|
|
||||||
var index = list.indexOf(peer);
|
|
||||||
if(index === -1) {
|
|
||||||
userList.users.push(peer);
|
|
||||||
}
|
|
||||||
userList.onChange();
|
|
||||||
};
|
|
||||||
|
|
||||||
var onReady = function(wc, network) {
|
var onReady = function(wc, network) {
|
||||||
// Trigger onReady only if not ready yet. This is important because the history keeper sends a direct
|
// Trigger onReady only if not ready yet. This is important because the history keeper sends a direct
|
||||||
// message through "network" when it is synced, and it triggers onReady for each channel joined.
|
// message through "network" when it is synced, and it triggers onReady for each channel joined.
|
||||||
@ -73,10 +52,8 @@ define([
|
|||||||
|
|
||||||
realtime.start();
|
realtime.start();
|
||||||
|
|
||||||
if(config.setMyID) {
|
if(setMyID) {
|
||||||
config.setMyID({
|
setMyID({ myID: wc.myID });
|
||||||
myID: wc.myID
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
// Trigger onJoining with our own Cryptpad username to tell the toolbar that we are synced
|
// Trigger onJoining with our own Cryptpad username to tell the toolbar that we are synced
|
||||||
if (!readOnly) {
|
if (!readOnly) {
|
||||||
|
|||||||
46
www/common/sframe-channel.js
Normal file
46
www/common/sframe-channel.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// This file provides the internal API for talking from inside of the sandbox iframe
|
||||||
|
// The external API is in sframe-ctrl.js
|
||||||
|
define([], function () {
|
||||||
|
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.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,4 +1,5 @@
|
|||||||
// This file provides the external API for launching the sandboxed iframe.
|
// This file provides the external API for launching and talking to the sandboxed iframe.
|
||||||
|
// The internal API is in sframe-channel.js
|
||||||
define([
|
define([
|
||||||
'/common/requireconfig.js'
|
'/common/requireconfig.js'
|
||||||
], function (RequireConfig) {
|
], function (RequireConfig) {
|
||||||
@ -11,40 +12,64 @@ 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 init = module.exports.init = function (frame, cb) {
|
module.exports.init = function (frame, cb) {
|
||||||
if (iframe) { throw new Error('already initialized'); }
|
if (iframe) { throw new Error('already initialized'); }
|
||||||
var txid = mkTxid();
|
var txid = mkTxid();
|
||||||
var intr = setInterval(function () {
|
var intr = setInterval(function () {
|
||||||
frame.contentWindow.postMessage({
|
frame.contentWindow.postMessage(JSON.stringify({
|
||||||
txid: txid,
|
txid: txid,
|
||||||
requireConf: RequireConfig,
|
content: { requireConf: RequireConfig },
|
||||||
q: 'INIT'
|
q: 'INIT'
|
||||||
}, '*');
|
}), '*');
|
||||||
});
|
});
|
||||||
window.addEventListener('message', function (msg) {
|
window.addEventListener('message', function (msg) {
|
||||||
console.log('recv');
|
var data = JSON.parse(msg.data);
|
||||||
console.log(msg.origin);
|
if (!iframe) {
|
||||||
var data = msg.data;
|
if (data.txid !== txid) { return; }
|
||||||
if (data.txid !== txid) { return; }
|
clearInterval(intr);
|
||||||
clearInterval(intr);
|
iframe = frame;
|
||||||
iframe = frame;
|
cb();
|
||||||
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
var query = module.exports.query = function (msg, cb) {
|
|
||||||
|
module.exports.query = function (q, content, cb) {
|
||||||
if (!iframe) { throw new Error('not yet initialized'); }
|
if (!iframe) { throw new Error('not yet initialized'); }
|
||||||
var txid = mkTxid();
|
var txid = mkTxid();
|
||||||
queries[txid] = {
|
var timeout = setTimeout(function () {
|
||||||
txid: txid,
|
delete queries[txid];
|
||||||
timeout: setTimeout(function () {
|
cb("Timeout making query " + q);
|
||||||
delete queries[txid];
|
});
|
||||||
console.log("Error")
|
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
|
||||||
|
}), '*');
|
||||||
};
|
};
|
||||||
var registerHandler = module.exports.registerHandler = function (queryType, handler) {
|
|
||||||
|
module.exports.registerHandler = function (queryType, handler) {
|
||||||
if (typeof(handlers[queryType]) !== 'undefined') { throw new Error('already registered'); }
|
if (typeof(handlers[queryType]) !== 'undefined') { throw new Error('already registered'); }
|
||||||
handlers[queryType] = handler;
|
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;
|
return module.exports;
|
||||||
|
|||||||
5
www/common/sframe-protocol.js
Normal file
5
www/common/sframe-protocol.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// This file defines all of the RPC calls
|
||||||
|
// The internal API is in sframe-channel.js
|
||||||
|
define({
|
||||||
|
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user