forked from ampproject/amphtml
/
amp-ad-xorigin-iframe-handler.js
309 lines (278 loc) · 9.53 KB
/
amp-ad-xorigin-iframe-handler.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {removeElement} from '../../../src/dom';
import {
SubscriptionApi,
listenFor,
listenForOncePromise,
postMessageToWindows,
} from '../../../src/iframe-helper';
import {IntersectionObserver} from '../../../src/intersection-observer';
import {viewerForDoc} from '../../../src/viewer';
import {dev, user} from '../../../src/log';
import {timerFor} from '../../../src/timer';
import {AdDisplayState} from './amp-ad-ui';
const TIMEOUT_VALUE = 10000;
export class AmpAdXOriginIframeHandler {
/**
* @param {!./amp-ad-3p-impl.AmpAd3PImpl|!../../amp-a4a/0.1/amp-a4a.AmpA4A} baseInstance
*/
constructor(baseInstance) {
/** @private */
this.baseInstance_ = baseInstance;
/** @private {!Element} */
this.element_ = baseInstance.element;
/** @private {?AMP.AmpAdUIHandler} */
this.uiHandler_ = baseInstance.uiHandler;
/** {?Element} iframe instance */
this.iframe = null;
/** @private {?IntersectionObserver} */
this.intersectionObserver_ = null;
/** @private {SubscriptionApi} */
this.embedStateApi_ = null;
/** @private {!Array<!Function>} functions to unregister listeners */
this.unlisteners_ = [];
/** @private @const {!../../../src/service/viewer-impl.Viewer} */
this.viewer_ = viewerForDoc(this.baseInstance_.getAmpDoc());
/** @private {?Promise} */
this.adResponsePromise_ = null;
}
/**
* Sets up listeners and iframe state for iframe containing ad creative.
* @param {!Element} iframe
* @param {boolean=} opt_isA4A when true do not listen to ad response
* @return {!Promise} awaiting render complete promise
* @suppress {checkTypes} // TODO(tdrl): Temporary, for lifecycleReporter.
*/
init(iframe, opt_isA4A) {
dev().assert(
!this.iframe, 'multiple invocations of init without destroy!');
this.iframe = iframe;
this.iframe.setAttribute('scrolling', 'no');
this.baseInstance_.applyFillContent(this.iframe);
this.intersectionObserver_ = new IntersectionObserver(
this.baseInstance_, this.iframe, true);
this.embedStateApi_ = new SubscriptionApi(
this.iframe, 'send-embed-state', true,
() => this.sendEmbedInfo_(this.baseInstance_.isInViewport()));
// Triggered by context.reportRenderedEntityIdentifier(…) inside the ad
// iframe.
listenForOncePromise(this.iframe, 'entity-id', true)
.then(info => {
this.element_.creativeId = info.data.id;
});
// Install iframe resize API.
this.unlisteners_.push(listenFor(this.iframe, 'embed-size',
(data, source, origin) => {
this.updateSize_(data.height, data.width, source, origin);
}, true, true));
this.unlisteners_.push(this.viewer_.onVisibilityChanged(() => {
this.sendEmbedInfo_(this.baseInstance_.isInViewport());
}));
if (opt_isA4A) {
// A4A writes creative frame directly to page therefore does not expect
// post message to unset visibility hidden
this.element_.appendChild(this.iframe);
return Promise.resolve();
}
// Install API that listen to ad response
if (this.baseInstance_.config
&& this.baseInstance_.config.renderStartImplemented) {
// If support render-start, create a race between render-start no-content
this.adResponsePromise_ = listenForOncePromise(this.iframe,
['render-start', 'no-content'], true).then(info => {
const data = info.data;
if (data.type == 'render-start') {
this.renderStart_(info);
} else {
this.noContent_();
}
});
} else {
// If NOT support render-start, listen to bootstrap-loaded no-content
// respectively
this.adResponsePromise_ = listenForOncePromise(this.iframe,
'bootstrap-loaded', true);
listenForOncePromise(this.iframe, 'no-content', true)
.then(() => this.noContent_());
}
// Set iframe initially hidden which will be removed on load event +
// post message.
this.iframe.style.visibility = 'hidden';
this.element_.appendChild(this.iframe);
return timerFor(this.baseInstance_.win).timeoutPromise(TIMEOUT_VALUE,
this.adResponsePromise_,
'timeout waiting for ad response').catch(e => {
this.noContent_();
user().warn('AMP-AD', e);
}).then(() => {
if (this.iframe) {
this.iframe.style.visibility = '';
}
});
}
/**
* callback functon on receiving render-start
* @param {!Object} info
* @private
*/
renderStart_(info) {
const data = info.data;
this.uiHandler_.setDisplayState(AdDisplayState.LOADED_RENDER_START);
this.updateSize_(data.height, data.width,
info.source, info.origin);
if (this.baseInstance_.lifecycleReporter) {
this.baseInstance_.lifecycleReporter.sendPing(
'renderCrossDomainStart');
}
}
/**
* Cleans up the listeners on the cross domain ad iframe.
* And free the iframe resource
* @param {boolean=} opt_keep
*/
freeXOriginIframe(opt_keep) {
this.cleanup_();
// If ask to keep the iframe.
// Use in the case of no-content and iframe is a master iframe.
if (opt_keep) {
return;
}
if (this.iframe) {
removeElement(this.iframe);
this.iframe = null;
}
}
/**
* Cleans up listeners on the ad, and apply the default UI for ad.
* @private
*/
noContent_() {
if (!this.iframe) {
// unlayout already called
return;
}
this.freeXOriginIframe(this.iframe.name.indexOf('_master') >= 0);
this.uiHandler_.setDisplayState(AdDisplayState.LOADED_NO_CONTENT);
}
/**
* Cleans up listeners on the ad iframe.
* @private
*/
cleanup_() {
this.unlisteners_.forEach(unlistener => unlistener());
this.unlisteners_.length = 0;
if (this.embedStateApi_) {
this.embedStateApi_.destroy();
this.embedStateApi_ = null;
}
if (this.intersectionObserver_) {
this.intersectionObserver_.destroy();
this.intersectionObserver_ = null;
}
}
/**
* Updates the element's dimensions to accommodate the iframe's
* requested dimensions. Notifies the window that request the resize
* of success or failure.
* @param {number|string|undefined} height
* @param {number|string|undefined} width
* @param {!Window} source
* @param {string} origin
* @private
*/
updateSize_(height, width, source, origin) {
// Calculate new width and height of the container to include the padding.
// If padding is negative, just use the requested width and height directly.
let newHeight, newWidth;
height = parseInt(height, 10);
if (!isNaN(height)) {
newHeight = Math.max(this.element_./*OK*/offsetHeight +
height - this.iframe./*OK*/offsetHeight, height);
}
width = parseInt(width, 10);
if (!isNaN(width)) {
newWidth = Math.max(this.element_./*OK*/offsetWidth +
width - this.iframe./*OK*/offsetWidth, width);
}
if (newHeight !== undefined || newWidth !== undefined) {
this.baseInstance_.attemptChangeSize(newHeight, newWidth).then(() => {
this.sendEmbedSizeResponse_(
true /* success */, newWidth, newHeight, source, origin);
}, () => this.sendEmbedSizeResponse_(
false /* success */, newWidth, newHeight, source, origin));
}
}
/**
* Sends a response to the window which requested a resize.
* @param {boolean} success
* @param {number} requestedWidth
* @param {number} requestedHeight
* @param {!Window} source
* @param {string} origin
* @private
*/
sendEmbedSizeResponse_(success, requestedWidth, requestedHeight, source,
origin) {
// The iframe may have been removed by the time we resize.
if (!this.iframe) {
return;
}
postMessageToWindows(
this.iframe,
[{win: source, origin}],
success ? 'embed-size-changed' : 'embed-size-denied',
{requestedWidth, requestedHeight},
true);
}
/**
* @param {boolean} inViewport
* @private
*/
sendEmbedInfo_(inViewport) {
if (!this.embedStateApi_) {
return;
}
this.embedStateApi_.send('embed-state', {
inViewport,
pageHidden: !this.viewer_.isVisible(),
});
}
/**
* See BaseElement method.
* @param {boolean} inViewport
*/
viewportCallback(inViewport) {
if (this.intersectionObserver_) {
this.intersectionObserver_.onViewportCallback(inViewport);
}
this.sendEmbedInfo_(inViewport);
}
/**
* See BaseElement method.
*/
onLayoutMeasure() {
// When the framework has the need to remeasure us, our position might
// have changed. Send an intersection record if needed. This does nothing
// if we aren't currently in view.
if (this.intersectionObserver_) {
this.intersectionObserver_.fire();
}
}
}
// Make the class available to other late loaded amp-ad implementations
// without them having to depend on it directly.
AMP.AmpAdXOriginIframeHandler = AmpAdXOriginIframeHandler;