diff --git a/www/common/sframe-boot.js b/www/common/sframe-boot.js index 127505ef1..34b25167e 100644 --- a/www/common/sframe-boot.js +++ b/www/common/sframe-boot.js @@ -1,10 +1,9 @@ // 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. window.addEventListener('message', function (msg) { - var data = msg.data; + var data = JSON.parse(msg.data); if (data.q !== 'INIT') { return; } - msg.source.postMessage({ txid: data.txid, res: 'OK' }, '*'); - if (data.requireConf) { require.config(data.requireConf); } + msg.source.postMessage({ txid: data.txid, content: 'OK' }, '*'); + if (data.content && data.content.requireConf) { require.config(data.content.requireConf); } require(['/common/sframe-boot2.js'], function () { }); -}); -console.log('boot'); \ No newline at end of file +}); \ No newline at end of file diff --git a/www/common/sframe-boot2.js b/www/common/sframe-boot2.js index ac0c060e9..0c98fb5d7 100644 --- a/www/common/sframe-boot2.js +++ b/www/common/sframe-boot2.js @@ -22,6 +22,5 @@ console.log('boot2'); window.__defineGetter__('localStorage', function () { return mkFakeStore(); }); window.__defineGetter__('sessionStorage', function () { return mkFakeStore(); }); - require([document.querySelector('script[data-bootload]').getAttribute('data-bootload')]); }); diff --git a/www/common/sframe-chainpad-netflux-outer.js b/www/common/sframe-chainpad-netflux-outer.js index 0da737c0f..c27be9021 100644 --- a/www/common/sframe-chainpad-netflux-outer.js +++ b/www/common/sframe-chainpad-netflux-outer.js @@ -27,45 +27,24 @@ define([ var unBencode = function (str) { return str.replace(/^\d+:/, ''); }; - module.exports.start = function (config) { - var websocketUrl = config.websocketURL; - var userName = config.userName; - var channel = config.channel; - var Crypto = config.crypto; - var validateKey = config.validateKey; - var readOnly = config.readOnly || false; - - // make sure configuration is defined - config = config || {}; + module.exports.start = function (conf) { + var websocketUrl = conf.websocketURL; + var userName = conf.userName; + var channel = conf.channel; + var Crypto = conf.crypto; + var validateKey = conf.validateKey; + var readOnly = conf.readOnly || false; + var websocketURL = conf.websocketURL; + var network = conf.network; + conf = undefined; var initializing = true; var toReturn = {}; var messagesHistory = []; var chainpadAdapter = {}; var realtime; - var network = config.network; 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) { // 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. @@ -73,10 +52,8 @@ define([ realtime.start(); - if(config.setMyID) { - config.setMyID({ - myID: wc.myID - }); + if(setMyID) { + setMyID({ myID: wc.myID }); } // Trigger onJoining with our own Cryptpad username to tell the toolbar that we are synced if (!readOnly) { diff --git a/www/common/sframe-channel.js b/www/common/sframe-channel.js new file mode 100644 index 000000000..4291f5108 --- /dev/null +++ b/www/common/sframe-channel.js @@ -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; +}); \ No newline at end of file diff --git a/www/common/sframe-ctrl.js b/www/common/sframe-ctrl.js index 536f2689e..ebae43985 100644 --- a/www/common/sframe-ctrl.js +++ b/www/common/sframe-ctrl.js @@ -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([ '/common/requireconfig.js' ], function (RequireConfig) { @@ -11,40 +12,64 @@ define([ 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'); } var txid = mkTxid(); var intr = setInterval(function () { - frame.contentWindow.postMessage({ + frame.contentWindow.postMessage(JSON.stringify({ txid: txid, - requireConf: RequireConfig, + content: { requireConf: RequireConfig }, q: 'INIT' - }, '*'); + }), '*'); }); window.addEventListener('message', function (msg) { - console.log('recv'); - console.log(msg.origin); - var data = msg.data; - if (data.txid !== txid) { return; } - clearInterval(intr); - iframe = frame; - cb(); + 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); + } }); }; - var query = module.exports.query = function (msg, cb) { + + module.exports.query = function (q, content, cb) { if (!iframe) { throw new Error('not yet initialized'); } var txid = mkTxid(); - queries[txid] = { - txid: txid, - timeout: setTimeout(function () { - delete queries[txid]; - console.log("Error") - }) + 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 + }), '*'); }; - var registerHandler = module.exports.registerHandler = function (queryType, handler) { + + module.exports.registerHandler = function (queryType, handler) { 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; diff --git a/www/common/sframe-protocol.js b/www/common/sframe-protocol.js new file mode 100644 index 000000000..56d32601a --- /dev/null +++ b/www/common/sframe-protocol.js @@ -0,0 +1,5 @@ +// This file defines all of the RPC calls +// The internal API is in sframe-channel.js +define({ + +}); \ No newline at end of file