/
integration.js
446 lines (419 loc) · 14.7 KB
/
integration.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* Copyright 2015 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.
*/
/**
* @fileoverview Registers all known ad network factories and then executes
* one of them.
*
* This files gets minified and published to
* https://3p.ampproject.net/$version/f.js
*/
import './polyfills';
import {installEmbedStateListener} from './environment';
import {a9} from '../ads/a9';
import {adform} from '../ads/adform';
import {adreactor} from '../ads/adreactor';
import {adsense} from '../ads/adsense';
import {adtech} from '../ads/adtech';
import {plista} from '../ads/plista';
import {criteo} from '../ads/criteo';
import {doubleclick} from '../ads/doubleclick';
import {dotandads} from '../ads/dotandads';
import {endsWith} from '../src/string';
import {facebook} from './facebook';
import {flite} from '../ads/flite';
import {manageWin} from './environment';
import {mediaimpact} from '../ads/mediaimpact';
import {nonSensitiveDataPostMessage, listenParent} from './messaging';
import {twitter} from './twitter';
import {yieldmo} from '../ads/yieldmo';
import {computeInMasterFrame, register, run} from '../src/3p';
import {parseUrl, getSourceUrl} from '../src/url';
import {assert} from '../src/asserts';
import {taboola} from '../ads/taboola';
import {smartadserver} from '../ads/smartadserver';
import {revcontent} from '../ads/revcontent';
import {openadstream} from '../ads/openadstream';
import {triplelift} from '../ads/triplelift';
import {teads} from '../ads/teads';
/**
* Whether the embed type may be used with amp-embed tag.
* @const {!Object<string: boolean>}
*/
const AMP_EMBED_ALLOWED = {
taboola: true,
};
register('a9', a9);
register('adform', adform);
register('adreactor', adreactor);
register('adsense', adsense);
register('adtech', adtech);
register('plista', plista);
register('criteo', criteo);
register('doubleclick', doubleclick);
register('flite', flite);
register('taboola', taboola);
register('dotandads', dotandads);
register('yieldmo', yieldmo);
register('_ping_', function(win, data) {
win.document.getElementById('c').textContent = data.ping;
});
register('twitter', twitter);
register('facebook', facebook);
register('smartadserver', smartadserver);
register('mediaimpact', mediaimpact);
register('revcontent', revcontent);
register('openadstream', openadstream);
register('triplelift', triplelift);
register('teads', teads);
// For backward compat, we always allow these types without the iframe
// opting in.
const defaultAllowedTypesInCustomFrame = [
// Entries must be reasonably safe and not allow overriding the injected
// JS URL.
// Each custom iframe can override this through the second argument to
// draw3p. See amp-ad docs.
'facebook',
'twitter',
'doubleclick',
'_ping_',
];
/**
* Visible for testing.
* Draws a 3p embed to the window. Expects the data to include the 3p type.
* @param {!Window} win
* @param {!Object} data
* @param {function(!Object, function(!Object))|undefined} configCallback
* Optional callback that allows user code to manipulate the incoming
* configuration. See
* https://github.com/ampproject/amphtml/issues/1210 for some context
* on this.
*/
export function draw3p(win, data, configCallback) {
const type = data.type;
assert(win.context.location.originValidated != null,
'Origin should have been validated');
assert(isTagNameAllowed(data.type, win.context.tagName),
'Embed type %s not allowed with tag %s', data.type, win.context.tagName);
if (configCallback) {
configCallback(data, data => {
assert(data, 'Expected configuration to be passed as first argument');
run(type, win, data);
});
} else {
run(type, win, data);
}
};
/**
* Returns the "master frame" for all widgets of a given type.
* This frame should be used to e.g. fetch scripts that can
* be reused across frames.
* @param {string} type
* @return {!Window}
*/
function masterSelection(type) {
// The master has a special name.
const masterName = 'frame_' + type + '_master';
let master;
try {
// Try to get the master from the parent. If it does not
// exist yet we get a security exception that we catch
// and ignore.
master = window.parent.frames[masterName];
} catch (expected) {
/* ignore */
}
if (!master) {
// No master yet, rename ourselves to be master. Yaihh.
window.name = masterName;
master = window;
}
return master;
}
/**
* Draws an embed, optionally synchronously, to the DOM.
* @param {function(!Object, function(!Object))} opt_configCallback If provided
* will be invoked with two arguments:
* 1. The configuration parameters supplied to this embed.
* 2. A callback that MUST be called for rendering to proceed. It takes
* no arguments. Configuration is expected to be modified in-place.
* @param {!Array<string>=} opt_allowed3pTypes List of advertising network
* types you expect.
* @param {!Array<string>=} opt_allowedEmbeddingOrigins List of domain suffixes
* that are allowed to embed this frame.
*/
window.draw3p = function(opt_configCallback, opt_allowed3pTypes,
opt_allowedEmbeddingOrigins) {
try {
ensureFramed(window);
const data = parseFragment(location.hash);
window.context = data._context;
window.context.location = parseUrl(data._context.location.href);
validateParentOrigin(window, window.context.location);
validateAllowedTypes(window, data.type, opt_allowed3pTypes);
if (opt_allowedEmbeddingOrigins) {
validateAllowedEmbeddingOrigins(window, opt_allowedEmbeddingOrigins);
}
window.context.master = masterSelection(data.type);
window.context.isMaster = window.context.master == window;
window.context.data = data;
window.context.noContentAvailable = triggerNoContentAvailable;
window.context.requestResize = triggerResizeRequest;
if (data.type === 'facebook' || data.type === 'twitter') {
// Only make this available to selected embeds until the
// generic solution is available.
window.context.updateDimensions = triggerDimensions;
}
// This only actually works for ads.
window.context.observeIntersection = observeIntersection;
window.context.onResizeSuccess = onResizeSuccess;
window.context.onResizeDenied = onResizeDenied;
window.context.reportRenderedEntityIdentifier =
reportRenderedEntityIdentifier;
window.context.computeInMasterFrame = computeInMasterFrame;
delete data._context;
manageWin(window);
installEmbedStateListener();
draw3p(window, data, opt_configCallback);
updateVisibilityState(window);
nonSensitiveDataPostMessage('render-start');
} catch (e) {
lightweightErrorReport(e);
throw e;
}
};
function triggerNoContentAvailable() {
nonSensitiveDataPostMessage('no-content');
}
function triggerDimensions(width, height) {
nonSensitiveDataPostMessage('embed-size', {
width: width,
height: height,
});
}
function triggerResizeRequest(width, height) {
nonSensitiveDataPostMessage('embed-size', {
width: width,
height: height,
});
}
/**
* Registers a callback for intersections of this iframe with the current
* viewport.
* The passed in array has entries that aim to be compatible with
* the IntersectionObserver spec callback.
* http://rawgit.com/slightlyoff/IntersectionObserver/master/index.html#callbackdef-intersectionobservercallback
* @param {function(!Array<IntersectionObserverEntry>)} observerCallback
* @returns {!function} A function which removes the event listener that
* observes for intersection messages.
*/
function observeIntersection(observerCallback) {
// Send request to received records.
nonSensitiveDataPostMessage('send-intersections');
return listenParent(window, 'intersection', data => {
observerCallback(data.changes);
});
}
/**
* Listens for events via postMessage and updates `context.hidden` based on
* it and forwards the event to a custom event called `amp:visibilitychange`.
* @param {!Window} global
*/
function updateVisibilityState(global) {
listenParent(window, 'embed-state', function(data) {
global.context.hidden = data.pageHidden;
const event = global.document.createEvent('Event');
event.data = {
hidden: data.pageHidden,
};
event.initEvent('amp:visibilitychange', true, true);
global.dispatchEvent(event);
});
}
/**
* Registers a callback for communicating when a resize request succeeds.
* @param {function(number)} observerCallback
* @returns {!function} A function which removes the event listener that
* observes for resize status messages.
*/
function onResizeSuccess(observerCallback) {
return listenParent(window, 'embed-size-changed', data => {
observerCallback(data.requestedHeight);
});
}
/**
* Registers a callback for communicating when a resize request is denied.
* @param {function(number)} observerCallback
* @returns {!function} A function which removes the event listener that
* observes for resize status messages.
*/
function onResizeDenied(observerCallback) {
return listenParent(window, 'embed-size-denied', data => {
observerCallback(data.requestedHeight);
});
}
/**
* Reports the "entity" that was rendered to this frame to the parent for
* reporting purposes.
* The entityId MUST NOT contain user data or personal identifiable
* information. One example for an acceptable data item would be the
* creative id of an ad, while the user's location would not be
* acceptable.
* @param {string} entityId See comment above for content.
*/
function reportRenderedEntityIdentifier(entityId) {
assert(typeof entityId == 'string',
'entityId should be a string %s', entityId);
nonSensitiveDataPostMessage('entity-id', {
id: entityId,
});
}
/**
* Throws if the current frame's parent origin is not equal to
* the claimed origin.
* For browsers that don't support ancestorOrigins it adds
* `originValidated = false` to the location object.
* @param {!Window} window
* @param {!Location} parentLocation
* @visibleForTesting
*/
export function validateParentOrigin(window, parentLocation) {
const ancestors = window.location.ancestorOrigins;
// Currently only webkit and blink based browsers support
// ancestorOrigins. In that case we proceed but mark the origin
// as non-validated.
if (!ancestors || !ancestors.length) {
parentLocation.originValidated = false;
return;
}
assert(ancestors[0] == parentLocation.origin,
'Parent origin mismatch: %s, %s',
ancestors[0], parentLocation.origin);
parentLocation.originValidated = true;
}
/**
* Check that this iframe intended this particular ad type to run.
* @param {!Window} window
* @param {string} type 3p type
* @param {!Array<string>|undefined} allowedTypes May be undefined.
* @visiblefortesting
*/
export function validateAllowedTypes(window, type, allowedTypes) {
// Everything allowed in default iframe.
if (window.location.hostname == '3p.ampproject.net') {
return;
}
if (/^d-\d+\.ampproject\.net$/.test(window.location.hostname)) {
return;
}
if (window.location.hostname == 'ads.localhost') {
return;
}
if (defaultAllowedTypesInCustomFrame.indexOf(type) != -1) {
return;
}
assert(allowedTypes && allowedTypes.indexOf(type) != -1,
'Non-whitelisted 3p type for custom iframe: ' + type);
}
/**
* Check that parent host name was whitelisted.
* @param {!Window} window
* @param {!Array<string>} allowedHostnames Suffixes of allowed host names.
* @visiblefortesting
*/
export function validateAllowedEmbeddingOrigins(window, allowedHostnames) {
if (!window.document.referrer) {
throw new Error('Referrer expected: ' + window.location.href);
}
const ancestors = window.location.ancestorOrigins;
// We prefer the unforgable ancestorOrigins, but referrer is better than
// nothing.
const ancestor = ancestors ? ancestors[0] : window.document.referrer;
let hostname = parseUrl(ancestor).hostname;
const onDefault = hostname == 'cdn.ampproject.org';
if (onDefault) {
// If we are on the cache domain, parse the source hostname from
// the referrer. The referrer is used because it should be
// trustable.
hostname = parseUrl(getSourceUrl(window.document.referrer)).hostname;
}
for (let i = 0; i < allowedHostnames.length; i++) {
// Either the hostname is exactly as whitelisted…
if (allowedHostnames[i] == hostname) {
return;
}
// Or it ends in .$hostname (aka is a sub domain of the whitelisted domain.
if (endsWith(hostname, '.' + allowedHostnames[i])) {
return;
}
}
throw new Error('Invalid embedding hostname: ' + hostname + ' not in '
+ allowedHostnames);
}
/**
* Throws if this window is a top level window.
* @param {!Window} window
* @visiblefortesting
*/
export function ensureFramed(window) {
if (window == window.parent) {
throw new Error('Must be framed: ' + window.location.href);
}
}
/**
* Expects the fragment to contain JSON.
* @param {string} fragment Value of location.fragment
* @return {!JSONObject}
* @visibleForTesting
*/
export function parseFragment(fragment) {
let json = fragment.substr(1);
// Some browser, notably Firefox produce an encoded version of the fragment
// while most don't. Since we know how the string should start, this is easy
// to detect.
if (json.indexOf('{%22') == 0) {
json = decodeURIComponent(json);
}
return json ? JSON.parse(json) : {};
}
/**
* Not all types of embeds are allowed to be used with all tag names on the
* AMP side. This function checks whether the current usage is permissible.
* @param {string} type
* @param {string|undefined} tagName The tagName that was used to embed this
* 3p-frame.
* @return {boolean}
*/
export function isTagNameAllowed(type, tagName) {
if (tagName == 'AMP-EMBED') {
return !!AMP_EMBED_ALLOWED[type];
}
return true;
}
/**
* Reports an error to the server. Must only be called once per page.
* Not for use in event handlers.
*
* We don't use the default error in error.js handler because it has
* too many deps for this small JS binary.
*
* @param {!Error} e
*/
function lightweightErrorReport(e) {
new Image().src = 'https://amp-error-reporting.appspot.com/r' +
'?3p=1&v=' + encodeURIComponent('$internalRuntimeVersion$') +
'&m=' + encodeURIComponent(e.message) +
'&r=' + encodeURIComponent(document.referrer);
}