1 /** 2 *@namespace Sfdc.canvas.xd 3 *@name Sfdc.canvas.xd 4 */ 5 (function ($$, window) { 6 7 "use strict"; 8 9 var module = (function() { 10 11 var internalCallback; 12 13 /** 14 * @lends Sfdc.canvas.xd 15 */ 16 17 /** 18 * @name Sfdc.canvas.xd#post 19 * @function 20 * @description Pass a message to the target url 21 * @param {String} message The message to send 22 * @param {String} target_url Specifies what the origin of the target must be for the event to be dispatched. 23 * @param {String} [target] The window that is the message's target. Defaults to the parent of the current window. 24 */ 25 function postMessage(message, target_url, target) { 26 27 // If target url was not supplied (client may have lost it), we could default to '*', 28 // However there are security implications here as other canvas apps could receive this 29 // canvas apps oauth token. 30 if ($$.isNil(target_url)) { 31 throw "ERROR: target_url was not supplied on postMessage"; 32 } 33 var otherWindow = $$.stripUrl(target_url); 34 35 target = target || parent; // default to parent 36 if (window.postMessage) { 37 // the browser supports window.postMessage, so call it with a targetOrigin 38 // set appropriately, based on the target_url parameter. 39 message = Sfdc.JSON.stringify(message); 40 target.postMessage(message, otherWindow); 41 } 42 } 43 44 /** 45 * @name Sfdc.canvas.xd#receive 46 * @function Runs the callback function when the message event is received. 47 * @param {Function} callback Function to run when the message event is received 48 if the event origin is acceptable. 49 * @param {String} source_origin The origin of the desired events 50 */ 51 function receiveMessage(callback, source_origin) { 52 53 // browser supports window.postMessage (if not not supported for pilot - removed per securities request) 54 if (window.postMessage) { 55 // bind the callback to the actual event associated with window.postMessage 56 if (callback) { 57 internalCallback = function(e) { 58 59 var data, r; 60 if (typeof source_origin === 'string' && e.origin !== source_origin) { 61 return false; 62 } 63 if ($$.isFunction(source_origin)) { 64 r = source_origin(e.origin, e.data); 65 if (r === false) { 66 return false; 67 } 68 } 69 data = Sfdc.JSON.parse(e.data); 70 callback(data, r); 71 72 }; 73 } 74 if (window.addEventListener) { 75 window.addEventListener('message', internalCallback, false); 76 } else { 77 window.attachEvent('onmessage', internalCallback); 78 } 79 } 80 } 81 82 /** 83 * @name Sfdc.canvas.xd#remove 84 * @function 85 * @description Removes the message event listener 86 * @public 87 */ 88 function removeListener() { 89 90 // browser supports window.postMessage 91 if (window.postMessage) { 92 if (window.removeEventListener) { 93 window.removeEventListener('message', internalCallback, false); 94 } else { 95 window.detachEvent('onmessage', internalCallback); 96 } 97 } 98 } 99 100 return { 101 post : postMessage, 102 receive : receiveMessage, 103 remove : removeListener 104 }; 105 }()); 106 107 $$.module('Sfdc.canvas.xd', module); 108 109 }(Sfdc.canvas, this));