connectSandbox: function (receiver, oasis) { var adapter = this; Logger.log("Sandbox listening for initialization message"); function initializeOasisSandbox(event) { if (!event.data.isOasisInitialization) { return; } removeEventListener(receiver, 'message', initializeOasisSandbox); oasis.configuration.eventCallback(function () { Logger.log("Sandbox received initialization message."); oasis.configuration.oasisURL = event.data.oasisURL; adapter.loadScripts(event.data.base, event.data.scriptURLs); oasis.connectCapabilities(event.data.capabilities, event.ports); adapter.didConnect(); }); } addEventListener(receiver, 'message', initializeOasisSandbox); adapter.oasisLoaded(); },
sandbox.oasis.configuration.eventCallback(function () { if( event.data !== sandbox.adapter.oasisLoadedMessage ) {return;} removeEventListener(worker, 'message', worker.loadHandler); Logger.log("worker sandbox initialized"); resolve(sandbox); });
terminateSandbox: function(sandbox) { var el = sandbox.el; if (el.parentNode) { Logger.log("Terminating sandbox ", sandbox.el.name); el.parentNode.removeChild(el); } sandbox.el = null; },
oasis.configuration.eventCallback(function () { Logger.log("Sandbox received initialization message."); oasis.configuration.oasisURL = event.data.oasisURL; adapter.loadScripts(event.data.base, event.data.scriptURLs); oasis.connectCapabilities(event.data.capabilities, event.ports); adapter.didConnect(); });
connectSandbox: function (receiver, oasis) { var adapter = this; Logger.log("Sandbox listening for initialization message"); function initializeOasisSandbox(event) { if (!event.data.isOasisInitialization) { return; } removeEventListener(receiver, 'message', initializeOasisSandbox); adapter.initializeOasisSandbox(event, oasis); } addEventListener(receiver, 'message', initializeOasisSandbox); adapter.oasisLoaded(oasis); },
terminateSandbox: function(sandbox) { var el = sandbox.el; sandbox.terminated = true; if (el.loadHandler) { // no load handler for HTML sandboxes removeEventListener(el, 'load', el.loadHandler); } removeEventListener(window, 'message', el.initializationHandler); removeEventListener(window, 'message', el.oasisLoadHandler); if (el.parentNode) { Logger.log("Terminating sandbox ", sandbox.el.name); el.parentNode.removeChild(el); } sandbox.el = null; },
iframe.oasisLoadHandler = function (event) { if( event.data !== sandbox.adapter.oasisLoadedMessage ) {return;} try { // verify this message came from the expected sandbox; try/catch // because ie8 will disallow reading contentWindow in the case of // another sandbox's message if( event.source !== iframe.contentWindow ) {return;} } catch(e) { return; } Logger.log("container: iframe sandbox has loaded Oasis"); if (verifyCurrentSandboxOrigin(sandbox, event)) { sandbox.createAndTransferCapabilities(); } if (sandbox.options.reconnect === "none") { removeEventListener(window, 'message', iframe.oasisLoadHandler); } };
initializeSandbox: function(sandbox) { var options = sandbox.options, iframe = document.createElement('iframe'); if(sandbox.oasis.configuration.sandboxed !== false) { var sandboxAttributes = ['allow-scripts']; if( sandbox.oasis.configuration.allowSameOrigin ) { sandboxAttributes.push('allow-same-origin'); } if( options && options.sandbox && options.sandbox.popups ) { sandboxAttributes.push('allow-popups'); } iframe.sandbox = sandboxAttributes.join(' '); } iframe.name = sandbox.options.url + '?uuid=' + UUID.generate(); iframe.seamless = true; // rendering-specific code if (options.width) { iframe.width = options.width; } else if (options.height) { iframe.height = options.height; } // Error handling inside the iFrame iframe.errorHandler = function(event) { if(!event.data.sandboxException) {return;} try { // verify this message came from the expected sandbox; try/catch // because ie8 will disallow reading contentWindow in the case of // another sandbox's message if( event.source !== iframe.contentWindow ) {return;} } catch(e) { return; } sandbox.onerror( event.data.sandboxException ); }; addEventListener(window, 'message', iframe.errorHandler); verifySandbox( sandbox.oasis, sandbox.options.url ); iframe.src = sandbox.options.url; Logger.log('Initializing sandbox ' + iframe.name); // Promise that sandbox has loaded and capabilities connected at least once. // This does not mean that the sandbox will be loaded & connected in the // face of reconnects (eg pages that navigate) sandbox._waitForLoadDeferred().resolve(new RSVP.Promise( function(resolve, reject) { iframe.initializationHandler = function (event) { if( event.data !== sandbox.adapter.sandboxInitializedMessage ) {return;} try { // verify this message came from the expected sandbox; try/catch // because ie8 will disallow reading contentWindow in the case of // another sandbox's message if( event.source !== iframe.contentWindow ) {return;} } catch(e) { return; } removeEventListener(window, 'message', iframe.initializationHandler); sandbox.oasis.configuration.eventCallback(function () { Logger.log("container: iframe sandbox has initialized (capabilities connected)"); resolve(sandbox); }); }; addEventListener(window, 'message', iframe.initializationHandler); })); sandbox.el = iframe; iframe.oasisLoadHandler = function (event) { if( event.data !== sandbox.adapter.oasisLoadedMessage ) {return;} try { // verify this message came from the expected sandbox; try/catch // because ie8 will disallow reading contentWindow in the case of // another sandbox's message if( event.source !== iframe.contentWindow ) {return;} } catch(e) { return; } Logger.log("container: iframe sandbox has loaded Oasis"); if (verifyCurrentSandboxOrigin(sandbox, event)) { sandbox.createAndTransferCapabilities(); } if (sandbox.options.reconnect === "none") { removeEventListener(window, 'message', iframe.oasisLoadHandler); } }; addEventListener(window, 'message', iframe.oasisLoadHandler); },
sandbox.oasis.configuration.eventCallback(function () { Logger.log("container: iframe sandbox has initialized (capabilities connected)"); resolve(sandbox); });
function applySandboxJS(sandboxFn) { Logger.log("sandbox: inline sandbox initialized"); sandboxFn(oasis); return oasis.sandbox; }
initializeSandbox: function(sandbox) { var options = sandbox.options, iframe = document.createElement('iframe'), oasisURL = this.oasisURL(sandbox), sandboxAttributes = ['allow-scripts']; if( sandbox.oasis.configuration.allowSameOrigin ) { sandboxAttributes.push('allow-same-origin'); } iframe.name = sandbox.options.url + '?uuid=' + UUID.generate(); iframe.sandbox = sandboxAttributes.join(' '); iframe.seamless = true; // rendering-specific code if (options.width) { iframe.width = options.width; } else if (options.height) { iframe.height = options.height; } // Error handling inside the iFrame iframe.errorHandler = function(event) { if(!event.data.sandboxException) {return;} try { // verify this message came from the expected sandbox; try/catch // because ie8 will disallow reading contentWindow in the case of // another sandbox's message if( event.source !== iframe.contentWindow ) {return;} } catch(e) { return; } sandbox.onerror( event.data.sandboxException ); }; addEventListener(window, 'message', iframe.errorHandler); switch (sandbox.type) { case 'js': verifySandbox( sandbox.oasis, oasisURL ); this._setupIFrameBootstrap(iframe, sandbox, oasisURL); break; case 'html': verifySandbox( sandbox.oasis, sandbox.options.url ); iframe.src = sandbox.options.url; break; default: assert(false, "IFrame Adapter only supports sandbox types `js` and `html`, not `" + sandbox.type + "`"); } Logger.log('Initializing sandbox ' + iframe.name); // Promise that sandbox is loaded and capabilities are connected sandbox._waitForLoadDeferred().resolve(new RSVP.Promise( function(resolve, reject) { iframe.initializationHandler = function (event) { if( event.data !== sandbox.adapter.sandboxInitializedMessage ) {return;} try { // verify this message came from the expected sandbox; try/catch // because ie8 will disallow reading contentWindow in the case of // another sandbox's message if( event.source !== iframe.contentWindow ) {return;} } catch(e) { return; } removeEventListener(window, 'message', iframe.initializationHandler); sandbox.oasis.configuration.eventCallback(function () { Logger.log("container: iframe sandbox has initialized (capabilities connected)"); resolve(sandbox); }); }; addEventListener(window, 'message', iframe.initializationHandler); })); sandbox.el = iframe; // Promise that sandbox is loaded; capabilities not connected return new RSVP.Promise(function (resolve, reject) { iframe.oasisLoadHandler = function (event) { if( event.data !== sandbox.adapter.oasisLoadedMessage ) {return;} try { // verify this message came from the expected sandbox; try/catch // because ie8 will disallow reading contentWindow in the case of // another sandbox's message if( event.source !== iframe.contentWindow ) {return;} } catch(e) { return; } removeEventListener(window, 'message', iframe.oasisLoadHandler); sandbox.oasis.configuration.eventCallback(function () { Logger.log("container: iframe sandbox has loaded Oasis"); resolve(sandbox); }); }; addEventListener(window, 'message', iframe.oasisLoadHandler); }); },
sandbox.oasis.configuration.eventCallback(function () { Logger.log("iframe loading oasis"); iframe.contentWindow.location.href = oasisURL; });
sandbox.oasis.configuration.eventCallback(function () { Logger.log("container: iframe sandbox has loaded Oasis"); resolve(sandbox); });