-
Notifications
You must be signed in to change notification settings - Fork 93
/
parent.js
233 lines (173 loc) · 7.27 KB
/
parent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* @flow */
import { ZalgoPromise } from 'zalgo-promise/src';
import { getDomain, getFrameByName, isWindowClosed, getDomainFromUrl, normalizeMockUrl, type CrossDomainWindowType } from 'cross-domain-utils/src';
import { BRIDGE_TIMEOUT, MESSAGE_NAME } from '../conf';
import { awaitWindowHello } from '../lib';
import { windowStore, globalStore } from '../global';
import type { OnType, SendType, ReceiveMessageType } from '../types';
import { getBridgeName, documentBodyReady, registerRemoteSendMessage, registerRemoteWindow } from './common';
export function listenForOpenTunnel({ on, send, receiveMessage } : { on : OnType, send : SendType, receiveMessage : ReceiveMessageType }) {
const popupWindowsByName = globalStore('popupWindowsByName');
on(MESSAGE_NAME.OPEN_TUNNEL, ({ source, origin, data }) => {
const bridgePromise = globalStore('bridges').get(origin);
if (!bridgePromise) {
throw new Error(`Can not find bridge promise for domain ${ origin }`);
}
return bridgePromise.then(bridge => {
if (source !== bridge) {
throw new Error(`Message source does not matched registered bridge for domain ${ origin }`);
}
if (!data.name) {
throw new Error(`Register window expected to be passed window name`);
}
if (!data.sendMessage) {
throw new Error(`Register window expected to be passed sendMessage method`);
}
if (!popupWindowsByName.has(data.name)) {
throw new Error(`Window with name ${ data.name } does not exist, or was not opened by this window`);
}
if (!popupWindowsByName.get(data.name).domain) {
throw new Error(`We do not have a registered domain for window ${ data.name }`);
}
if (popupWindowsByName.get(data.name).domain !== origin) {
throw new Error(`Message origin ${ origin } does not matched registered window origin ${ popupWindowsByName.get(data.name).domain }`);
}
registerRemoteSendMessage(popupWindowsByName.get(data.name).win, origin, data.sendMessage);
return {
sendMessage(message) {
if (!window || window.closed) {
return;
}
const winDetails = popupWindowsByName.get(data.name);
if (!winDetails) {
return;
}
try {
receiveMessage({
data: message,
origin: winDetails.domain,
source: winDetails.win
}, { on, send });
} catch (err) {
ZalgoPromise.reject(err);
}
}
};
});
});
}
function openBridgeFrame(name : string, url : string) : HTMLIFrameElement {
const iframe = document.createElement(`iframe`);
iframe.setAttribute(`name`, name);
iframe.setAttribute(`id`, name);
iframe.setAttribute(`style`, `display: none; margin: 0; padding: 0; border: 0px none; overflow: hidden;`);
iframe.setAttribute(`frameborder`, `0`);
iframe.setAttribute(`border`, `0`);
iframe.setAttribute(`scrolling`, `no`);
iframe.setAttribute(`allowTransparency`, `true`);
iframe.setAttribute(`tabindex`, `-1`);
iframe.setAttribute(`hidden`, `true`);
iframe.setAttribute(`title`, ``);
iframe.setAttribute(`role`, `presentation`);
iframe.src = url;
return iframe;
}
export function hasBridge(url : string, domain : string) : boolean {
const bridges = globalStore('bridges');
return bridges.has(domain || getDomainFromUrl(url));
}
export function openBridge(url : string, domain : string) : ZalgoPromise<CrossDomainWindowType> {
const bridges = globalStore('bridges');
const bridgeFrames = globalStore('bridgeFrames');
domain = domain || getDomainFromUrl(url);
return bridges.getOrSet(domain, () => ZalgoPromise.try(() => {
if (getDomain() === domain) {
throw new Error(`Can not open bridge on the same domain as current domain: ${ domain }`);
}
const name = getBridgeName(domain);
const frame = getFrameByName(window, name);
if (frame) {
throw new Error(`Frame with name ${ name } already exists on page`);
}
const iframe = openBridgeFrame(name, url);
bridgeFrames.set(domain, iframe);
return documentBodyReady.then(body => {
body.appendChild(iframe);
const bridge = iframe.contentWindow;
return new ZalgoPromise((resolve, reject) => {
iframe.addEventListener('load', resolve);
iframe.addEventListener('error', reject);
}).then(() => {
return awaitWindowHello(bridge, BRIDGE_TIMEOUT, `Bridge ${ url }`);
}).then(() => {
return bridge;
});
});
}));
}
type WinDetails = {|
win : CrossDomainWindowType,
domain? : ?string,
name? : ?string
|};
export function linkWindow({ win, name, domain } : WinDetails) : WinDetails {
const popupWindowsByName = globalStore('popupWindowsByName');
const popupWindowsByWin = windowStore('popupWindowsByWin');
for (const winName of popupWindowsByName.keys()) {
// $FlowFixMe
const details = popupWindowsByName.get(winName);
if (!details || isWindowClosed(details.win)) {
popupWindowsByName.del(winName);
}
}
if (isWindowClosed(win)) {
return { win, name, domain };
}
const details = popupWindowsByWin.getOrSet(win, () : WinDetails => {
if (!name) {
return { win };
}
return popupWindowsByName.getOrSet(name, () : WinDetails => {
return { win, name };
});
});
if (details.win && details.win !== win) {
throw new Error(`Different window already linked for window: ${ name || 'undefined' }`);
}
if (name) {
details.name = name;
popupWindowsByName.set(name, details);
}
if (domain) {
details.domain = domain;
registerRemoteWindow(win);
}
popupWindowsByWin.set(win, details);
return details;
}
export function linkUrl(win : CrossDomainWindowType, url : string) {
linkWindow({ win, domain: getDomainFromUrl(url) });
}
export function listenForWindowOpen() {
const windowOpen = window.open;
window.open = function windowOpenWrapper(url : string, name : string, options : string, last : mixed) : mixed {
const win = windowOpen.call(this, normalizeMockUrl(url), name, options, last);
if (!win) {
return win;
}
linkWindow({ win, name, domain: url ? getDomainFromUrl(url) : null });
return win;
};
}
export function destroyBridges() {
const bridges = globalStore('bridges');
const bridgeFrames = globalStore('bridgeFrames');
for (const domain of bridgeFrames.keys()) {
const frame = bridgeFrames.get(domain);
if (frame && frame.parentNode) {
frame.parentNode.removeChild(frame);
}
}
bridgeFrames.reset();
bridges.reset();
}