cryptpad/www/secureiframe/main.js

198 lines
8.3 KiB
JavaScript
Raw Normal View History

2019-05-29 17:00:20 +00:00
// Load #1, load as little as possible because we are in a race to get the loading screen up.
define([
'/components/nthen/index.js',
2019-05-29 17:00:20 +00:00
'/api/config',
'jquery',
'/common/requireconfig.js',
2020-11-04 10:22:31 +00:00
'/customize/messages.js',
], function (nThen, ApiConfig, $, RequireConfig, Messages) {
2019-05-29 17:00:20 +00:00
var requireConfig = RequireConfig();
var ready = false;
2019-05-29 17:00:20 +00:00
var create = function (config) {
// Loaded in load #2
var sframeChan;
nThen(function (waitFor) {
$(waitFor());
}).nThen(function (waitFor) {
2020-11-04 10:22:31 +00:00
var lang = Messages._languageUsed;
var themeKey = 'CRYPTPAD_STORE|colortheme';
2019-05-29 17:00:20 +00:00
var req = {
cfg: requireConfig,
req: [ '/common/loading.js' ],
2020-11-04 10:22:31 +00:00
pfx: window.location.origin,
theme: localStorage[themeKey],
themeOS: localStorage[themeKey+'_default'],
2020-11-04 10:22:31 +00:00
lang: lang
2019-05-29 17:00:20 +00:00
};
window.rc = requireConfig;
window.apiconf = ApiConfig;
2023-02-01 17:02:03 +00:00
// FIXME extra sandboxing features are temporarily disabled as I suspect this is the cause of a regression in Safari
$('#sbox-secure-iframe')/*.attr('sandbox', 'allow-scripts allow-popups allow-modals')*/.attr('src',
2020-04-07 12:27:44 +00:00
ApiConfig.httpSafeOrigin + '/secureiframe/inner.html?' + requireConfig.urlArgs +
2019-05-29 17:00:20 +00:00
'#' + encodeURIComponent(JSON.stringify(req)));
// This is a cheap trick to avoid loading sframe-channel in parallel with the
// loading screen setup.
var done = waitFor();
var onMsg = function (msg) {
var data = typeof(msg.data) === "object" ? msg.data : JSON.parse(msg.data);
2019-05-29 17:00:20 +00:00
if (data.q !== 'READY') { return; }
window.removeEventListener('message', onMsg);
var _done = done;
done = function () { };
_done();
};
window.addEventListener('message', onMsg);
}).nThen(function (/*waitFor*/) {
var Cryptpad = config.modules.Cryptpad;
var Utils = config.modules.Utils;
nThen(function (waitFor) {
// The inner iframe tries to get some data from us every ms (cache, store...).
// It will send a "READY" message and wait for our answer with the correct txid.
// 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 = Utils.Util.mkEvent();
2020-04-07 12:27:44 +00:00
var iframe = $('#sbox-secure-iframe')[0].contentWindow;
2019-05-29 17:00:20 +00:00
var postMsg = function (data) {
iframe.postMessage(data, ApiConfig.httpSafeOrigin);
2019-05-29 17:00:20 +00:00
};
var w = waitFor();
var whenReady = 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, language: Cryptpad.getLanguage(), localStore: window.localStore, cache: window.cpCache }));
2019-05-29 17:00:20 +00:00
// Then start the channel
window.addEventListener('message', function (msg) {
if (msg.source !== iframe) { return; }
msgEv.fire(msg);
});
config.modules.SFrameChannel.create(msgEv, postMsg, waitFor(function (sfc) {
sframeChan = sfc;
}));
w();
};
window.addEventListener('message', whenReady);
}).nThen(function () {
var isTemplate = config.data.isTemplate;
2019-05-29 17:00:20 +00:00
var updateMeta = function () {
//console.log('EV_METADATA_UPDATE');
var metaObj;
nThen(function (waitFor) {
Cryptpad.getMetadata(waitFor(function (err, n) {
if (err) {
waitFor.abort();
return void console.log(err);
}
2019-05-29 17:00:20 +00:00
metaObj = n;
}));
}).nThen(function (/*waitFor*/) {
metaObj.doc = {};
var additionalPriv = {
2020-04-17 13:56:17 +00:00
app: config.data.app,
2019-08-27 09:37:39 +00:00
fileHost: ApiConfig.fileHost,
2020-02-28 14:48:50 +00:00
loggedIn: Utils.LocalStore.isLoggedIn(),
2019-05-29 17:00:20 +00:00
origin: window.location.origin,
pathname: window.location.pathname,
feedbackAllowed: Utils.Feedback.state,
channel: config.data.channel,
2019-05-29 17:00:20 +00:00
hashes: config.data.hashes,
password: config.data.password,
propChannels: config.data.getPropChannels(),
isTemplate: isTemplate,
file: config.data.file,
2023-07-13 12:12:47 +00:00
devMode: localStorage.CryptPad_dev === '1',
2021-01-19 15:58:56 +00:00
secureIframe: true,
2019-05-29 17:00:20 +00:00
};
for (var k in additionalPriv) { metaObj.priv[k] = additionalPriv[k]; }
sframeChan.event('EV_METADATA_UPDATE', metaObj);
});
};
Cryptpad.onMetadataChanged(updateMeta);
sframeChan.onReg('EV_METADATA_UPDATE', updateMeta);
config.addCommonRpc(sframeChan, true);
2019-05-29 17:00:20 +00:00
Cryptpad.padRpc.onMetadataEvent.reg(function (data) {
sframeChan.event('EV_RT_METADATA', data);
});
sframeChan.on('EV_CACHE_PUT', function (x) {
Object.keys(x).forEach(function (k) {
try {
localStorage['CRYPTPAD_CACHE|' + k] = x[k];
} catch (err) {
console.error(err);
}
});
});
sframeChan.on('EV_LOCALSTORE_PUT', function (x) {
Object.keys(x).forEach(function (k) {
if (typeof(x[k]) === "undefined") {
delete localStorage['CRYPTPAD_STORE|' + k];
return;
}
try {
localStorage['CRYPTPAD_STORE|' + k] = x[k];
} catch (err) {
console.error(err);
}
});
});
2019-05-29 17:00:20 +00:00
sframeChan.on('Q_GET_FILES_LIST', function (types, cb) {
Cryptpad.getSecureFilesList(types, function (err, data) {
cb({
error: err,
data: data
});
});
});
2020-04-07 12:27:44 +00:00
sframeChan.on('EV_SECURE_ACTION', function (data) {
config.onAction(data);
});
sframeChan.on('Q_UPLOAD_FILE', function (data, cb) {
config.onFileUpload(sframeChan, data, cb);
2019-05-29 17:00:20 +00:00
});
2020-04-07 12:27:44 +00:00
sframeChan.on('EV_SECURE_IFRAME_CLOSE', function () {
config.onClose();
2019-05-29 17:00:20 +00:00
});
sframeChan.onReady(function () {
if (ready === true) { return; }
if (typeof ready === "function") {
ready();
}
ready = true;
});
2019-05-29 17:00:20 +00:00
});
});
var refresh = function (data, cb) {
if (!ready) {
ready = function () {
refresh(data, cb);
};
return;
}
2020-04-07 12:27:44 +00:00
sframeChan.event('EV_REFRESH', data);
cb();
2019-05-29 17:00:20 +00:00
};
return {
refresh: refresh
};
};
return {
create: create
};
});