|
| 1 | + |
| 2 | +import { CONFIG, CONSTANTS } from '../conf'; |
| 3 | +import { util, promise, isSameDomain, isOpener, isSameTopWindow, getUserAgent } from '../lib'; |
| 4 | +import { global } from '../global'; |
| 5 | +import { receiveMessage } from '../drivers'; |
| 6 | + |
| 7 | +export function needsBridgeForBrowser() { |
| 8 | + |
| 9 | + if (getUserAgent(window).match(/MSIE|trident|edge/i)) { |
| 10 | + return true; |
| 11 | + } |
| 12 | + |
| 13 | + if (!CONFIG.ALLOW_POSTMESSAGE_POPUP) { |
| 14 | + return true; |
| 15 | + } |
| 16 | + |
| 17 | + return false; |
| 18 | +} |
| 19 | + |
| 20 | +export function needsBridgeForWin(win) { |
| 21 | + |
| 22 | + if (win && isSameTopWindow(window, win)) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + if (win && isSameDomain(win)) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + return true; |
| 31 | +} |
| 32 | + |
| 33 | +export function needsBridgeForDomain(domain) { |
| 34 | + |
| 35 | + if (domain && util.getDomain() === util.getDomainFromUrl(domain)) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +export function needsBridge({ win, domain }) { |
| 43 | + return needsBridgeForBrowser() && needsBridgeForWin(win) && needsBridgeForDomain(domain); |
| 44 | +} |
| 45 | + |
| 46 | +export function getBridgeName(domain) { |
| 47 | + |
| 48 | + domain = domain || util.getDomainFromUrl(domain); |
| 49 | + |
| 50 | + let sanitizedDomain = domain.replace(/[^a-zA-Z0-9]+/g, '_'); |
| 51 | + |
| 52 | + let id = `${CONSTANTS.BRIDGE_NAME_PREFIX}_${sanitizedDomain}`; |
| 53 | + |
| 54 | + return id; |
| 55 | +} |
| 56 | + |
| 57 | +export function isBridge() { |
| 58 | + return window.name && window.name === getBridgeName(util.getDomain()); |
| 59 | +} |
| 60 | + |
| 61 | +export let documentBodyReady = new promise.Promise(resolve => { |
| 62 | + |
| 63 | + if (window.document && window.document.body) { |
| 64 | + return resolve(window.document.body); |
| 65 | + } |
| 66 | + |
| 67 | + let interval = setInterval(() => { |
| 68 | + if (window.document && window.document.body) { |
| 69 | + clearInterval(interval); |
| 70 | + return resolve(window.document.body); |
| 71 | + } |
| 72 | + }, 10); |
| 73 | +}); |
| 74 | + |
| 75 | +global.remoteWindows = global.remoteWindows || []; |
| 76 | + |
| 77 | +export function registerRemoteWindow(win, timeout = CONFIG.BRIDGE_TIMEOUT) { |
| 78 | + let sendMessagePromise = new promise.Promise(); |
| 79 | + global.clean.push(global.remoteWindows, { win, sendMessagePromise }); |
| 80 | +} |
| 81 | + |
| 82 | +export function findRemoteWindow(win) { |
| 83 | + for (let i = 0; i < global.remoteWindows.length; i++) { |
| 84 | + if (global.remoteWindows[i].win === win) { |
| 85 | + return global.remoteWindows[i]; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +export function registerRemoteSendMessage(win, domain, sendMessage) { |
| 91 | + |
| 92 | + let remoteWindow = findRemoteWindow(win); |
| 93 | + |
| 94 | + if (!remoteWindow) { |
| 95 | + throw new Error(`Window not found to register sendMessage to`); |
| 96 | + } |
| 97 | + |
| 98 | + let sendMessageWrapper = (remoteWin, message, remoteDomain) => { |
| 99 | + |
| 100 | + if (remoteWin !== win) { |
| 101 | + throw new Error(`Remote window does not match window`); |
| 102 | + } |
| 103 | + |
| 104 | + if (remoteDomain !== `*` && remoteDomain !== domain) { |
| 105 | + throw new Error(`Remote domain ${remoteDomain} does not match domain ${domain}`); |
| 106 | + } |
| 107 | + |
| 108 | + sendMessage(message); |
| 109 | + }; |
| 110 | + |
| 111 | + remoteWindow.sendMessagePromise.resolve(sendMessageWrapper); |
| 112 | + remoteWindow.sendMessagePromise = promise.Promise.resolve(sendMessageWrapper); |
| 113 | +} |
| 114 | + |
| 115 | +export function rejectRemoteSendMessage(win, err) { |
| 116 | + |
| 117 | + let remoteWindow = findRemoteWindow(win); |
| 118 | + |
| 119 | + if (!remoteWindow) { |
| 120 | + throw new Error(`Window not found on which to reject sendMessage`); |
| 121 | + } |
| 122 | + |
| 123 | + return remoteWindow.sendMessagePromise.asyncReject(err); |
| 124 | +} |
| 125 | + |
| 126 | +export function sendBridgeMessage(win, message, domain) { |
| 127 | + |
| 128 | + let messagingChild = isOpener(window, win); |
| 129 | + let messagingParent = isOpener(win, window); |
| 130 | + |
| 131 | + if (!messagingChild && !messagingParent) { |
| 132 | + throw new Error(`Can only send messages to and from parent and popup windows`); |
| 133 | + } |
| 134 | + |
| 135 | + let remoteWindow = findRemoteWindow(win); |
| 136 | + |
| 137 | + if (!remoteWindow) { |
| 138 | + throw new Error(`Window not found to send message to`); |
| 139 | + } |
| 140 | + |
| 141 | + return remoteWindow.sendMessagePromise.then(sendMessage => { |
| 142 | + return sendMessage(win, message, domain); |
| 143 | + }); |
| 144 | +} |
| 145 | + |
| 146 | +global.receiveMessage = function(event) { |
| 147 | + return receiveMessage(event); |
| 148 | +}; |
0 commit comments