forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextensions-impl.js
627 lines (568 loc) · 17.9 KB
/
extensions-impl.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/**
* 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 {
copyElementToChildWindow,
stubElementIfNotKnown,
} from '../custom-element';
import {cssText} from '../../build/css';
import {dev, rethrowAsync} from '../log';
import {getMode} from '../mode';
import {fromClass, setParentWindow} from '../service';
import installCustomElements from
'document-register-element/build/document-register-element.node';
import {install as installDocContains} from '../polyfills/document-contains';
import {installImg} from '../../builtins/amp-img';
import {installPixel} from '../../builtins/amp-pixel';
import {installStyles} from '../style-installer';
import {installVideo} from '../../builtins/amp-video';
import {urls} from '../config';
const TAG = 'extensions';
const UNKNOWN_EXTENSION = '_UNKNOWN_';
/**
* The structure that contains the declaration of a custom element.
*
* @typedef {{
* implementationClass:
* function(new:../base-element.BaseElement, !Element),
* css: (?string|undefined),
* }}
*/
let ExtensionElementDef;
/**
* The structure that contains the resources declared by an extension.
* Currently only limitted to elements.
*
* @typedef {{
* elements: !Object<string, !ExtensionElementDef>,
* }}
*/
let ExtensionDef;
/**
* Internal structure that maintains the state of an extension through loading.
*
* @typedef {{
* extension: !ExtensionDef,
* docFactories: !Array<function(!./ampdoc-impl.AmpDoc)>,
* shadowRootFactories: !Array<function(!ShadowRoot)>,
* promise: (!Promise<!ExtensionDef>|undefined),
* resolve: (function(!ExtensionDef)|undefined),
* reject: (function(!Error)|undefined),
* loaded: (boolean|undefined),
* error: (!Error|undefined),
* scriptPresent: (boolean|undefined),
* }}
* @private
*/
let ExtensionHolderDef;
/**
* Install extensions service.
* @param {!Window} window
* @return {!Extensions}
* @restricted
*/
export function installExtensionsService(window) {
return fromClass(window, 'extensions', Extensions);
}
/**
* Register and process the specified extension. The factory is called
* immediately, which in turn is expected to register elements, templates,
* services and document factories.
* @param {!Extensions} extensions
* @param {string} extensionId
* @param {function(!Object)} factory
* @param {!Object} arg
* @restricted
*/
export function registerExtension(extensions, extensionId, factory, arg) {
extensions.registerExtension_(extensionId, factory, arg);
}
/**
* Apply all registered factories to the specified ampdoc.
* @param {!Extensions} extensions
* @param {!./ampdoc-impl.AmpDoc} ampdoc
* @param {!Array<string>} extensionIds
* @return {!Promise}
* @restricted
*/
export function installExtensionsInShadowDoc(extensions, ampdoc, extensionIds) {
return extensions.installExtensionsInShadowDoc_(ampdoc, extensionIds);
}
/**
* Add an element to the extension currently being registered. This is a
* restricted method and it's allowed to be called only during the overall
* extension registration.
* @param {!Extensions} extensions
* @param {string} name
* @param {function(new:../base-element.BaseElement, !Element)}
* implementationClass
* @param {?string|undefined} css
* @restricted
*/
export function addElementToExtension(
extensions, name, implementationClass, css) {
extensions.addElement_(name, implementationClass, css);
}
/**
* Add a ampdoc factory to the extension currently being registered. This is a
* restricted method and it's allowed to be called only during the overall
* extension registration.
* @param {!Extensions} extensions
* @param {function(!./ampdoc-impl.AmpDoc)} factory
* @param {string=} opt_forName
* @restricted
*/
export function addDocFactoryToExtension(extensions, factory, opt_forName) {
extensions.addDocFactory_(factory, opt_forName);
}
/**
* Add a shadow-root factory to the extension currently being registered. This
* is a restricted method and it's allowed to be called only during the overall
* extension registration.
* @param {!Extensions} extensions
* @param {function(!ShadowRoot)} factory
* @param {string=} opt_forName
* @restricted
*/
export function addShadowRootFactoryToExtension(extensions, factory,
opt_forName) {
extensions.addShadowRootFactory_(factory, opt_forName);
}
/**
* The services that manages extensions in the runtime.
* @visibleForTesting
*/
export class Extensions {
/**
* @param {!Window} win
*/
constructor(win) {
/** @const {!Window} */
this.win = win;
/** @private @const {!Object<string, !ExtensionHolderDef>} */
this.extensions_ = {};
/** @private {?string} */
this.currentExtensionId_ = null;
}
/**
* Registers a new extension. This method is called by the extension's script
* itself when it's loaded using the regular `AMP.push()` callback.
* @param {string} extensionId
* @param {function(!Object)} factory
* @param {!Object} arg
* @private
* @restricted
*/
registerExtension_(extensionId, factory, arg) {
const holder = this.getExtensionHolder_(extensionId);
try {
this.currentExtensionId_ = extensionId;
factory(arg);
if (getMode().localDev || getMode().test) {
if (Object.freeze) {
const m = holder.extension;
m.elements = Object.freeze(m.elements);
holder.extension = Object.freeze(m);
}
}
holder.loaded = true;
if (holder.resolve) {
holder.resolve(holder.extension);
}
} catch (e) {
holder.error = e;
if (holder.reject) {
holder.reject(e);
}
throw e;
} finally {
this.currentExtensionId_ = null;
}
}
/**
* Waits for the previously included extension to complete
* loading/registration.
* @param {string} extensionId
* @return {!Promise<!ExtensionDef>}
*/
waitForExtension(extensionId) {
return this.waitFor_(this.getExtensionHolder_(extensionId));
}
/**
* Returns the promise that will be resolved when the extension has been
* loaded. If necessary, adds the extension script to the page.
* @param {string} extensionId
* @param {boolean=} stubElement
* @return {!Promise<!ExtensionDef>}
*/
loadExtension(extensionId, stubElement = true) {
if (extensionId == 'amp-embed') {
extensionId = 'amp-ad';
}
const holder = this.getExtensionHolder_(extensionId);
this.insertExtensionScriptIfNeeded_(extensionId, holder, stubElement);
return this.waitFor_(holder);
}
/**
* Returns the promise that will be resolved with the extension element's
* class when the extension has been loaded. If necessary, adds the extension
* script to the page.
* @param {string} elementName
* @return {!Promise<function(new:../base-element.BaseElement, !Element)>}
*/
loadElementClass(elementName) {
return this.loadExtension(elementName).then(extension => {
const element = dev().assert(extension.elements[elementName],
'Element not found: %s', elementName);
return element.implementationClass;
});
}
/**
* Registers the element implementation with the current extension.
* @param {string} name
* @param {!Function} implementationClass
* @param {?string|undefined} css
* @private
* @restricted
*/
addElement_(name, implementationClass, css) {
const holder = this.getCurrentExtensionHolder_(name);
holder.extension.elements[name] = {implementationClass, css};
}
/**
* Registers an ampdoc factory.
* @param {function(!./ampdoc-impl.AmpDoc)} factory
* @param {string=} opt_forName
* @private
* @restricted
*/
addDocFactory_(factory, opt_forName) {
const holder = this.getCurrentExtensionHolder_(opt_forName);
holder.docFactories.push(factory);
}
/**
* Registers a shadow-root factory.
* @param {function(!ShadowRoot)} factory
* @param {string=} opt_forName
* @private
* @restricted
*/
addShadowRootFactory_(factory, opt_forName) {
const holder = this.getCurrentExtensionHolder_(opt_forName);
holder.shadowRootFactories.push(factory);
}
/**
* Installs all ampdoc factories previously registered with
* `addDocFactory_`.
* @param {!./ampdoc-impl.AmpDoc} ampdoc
* @param {!Array<string>} extensionIds
* @return {!Promise}
* @private
* @restricted
*/
installExtensionsInShadowDoc_(ampdoc, extensionIds) {
const promises = [];
extensionIds.forEach(extensionId => {
const holder = this.getExtensionHolder_(extensionId);
promises.push(this.waitFor_(holder).then(() => {
holder.shadowRootFactories.forEach(factory => {
try {
factory(/** @type {!ShadowRoot} */ (ampdoc.getRootNode()));
} catch (e) {
rethrowAsync('ShadowRoot factory failed: ', e, extensionId);
}
});
holder.docFactories.forEach(factory => {
try {
factory(ampdoc);
} catch (e) {
rethrowAsync('Doc factory failed: ', e, extensionId);
}
});
}));
});
return Promise.all(promises);
}
/**
* Installs all shadow-root factories previously registered with
* `addShadowRootFactory_`.
* @param {!ShadowRoot} shadowRoot
* @param {!Array<string>} extensionIds
* @return {!Promise}
* @restricted
*/
installFactoriesInShadowRoot(shadowRoot, extensionIds) {
const promises = [];
extensionIds.forEach(extensionId => {
const holder = this.getExtensionHolder_(extensionId);
promises.push(this.waitFor_(holder).then(() => {
holder.shadowRootFactories.forEach(factory => {
try {
factory(/** @type {!ShadowRoot} */ (shadowRoot));
} catch (e) {
rethrowAsync('ShadowRoot factory failed: ', e, extensionId);
}
});
}));
});
return Promise.all(promises);
}
/**
* Install extensions in the child window (friendly iframe). The pre-install
* callback, if specified, is executed after polyfills have been configured
* but before the first extension is installed.
* @param {!Window} childWin
* @param {!Array<string>} extensionIds
* @param {function(!Window)=} opt_preinstallCallback
* @return {!Promise}
* @restricted
*/
installExtensionsInChildWindow(childWin, extensionIds,
opt_preinstallCallback) {
const topWin = this.win;
const parentWin = childWin.frameElement.ownerDocument.defaultView;
setParentWindow(childWin, parentWin);
// Install necessary polyfills.
installPolyfillsInChildWindow(childWin);
// Install runtime styles.
installStyles(childWin.document, cssText, () => {},
/* opt_isRuntimeCss */ true, /* opt_ext */ 'amp-runtime');
// Run pre-install callback.
if (opt_preinstallCallback) {
opt_preinstallCallback(childWin);
}
// Install built-ins.
copyBuiltinElementsToChildWindow(childWin);
const promises = [];
extensionIds.forEach(extensionId => {
// This will extend automatic upgrade of custom elements from top
// window to the child window.
stubElementIfNotKnown(topWin, extensionId);
copyElementToChildWindow(childWin, extensionId);
// Install CSS.
const promise = this.loadExtension(extensionId).then(extension => {
const elementDef = extension.elements[extensionId];
if (elementDef && elementDef.css) {
installStyles(childWin.document, elementDef.css, () => {},
/* isRuntime */ false, extensionId);
}
});
promises.push(promise);
});
return Promise.all(promises);
}
/**
* Creates or returns an existing extension holder.
* @param {string} extensionId
* @return {!ExtensionHolderDef}
* @private
*/
getExtensionHolder_(extensionId) {
let holder = this.extensions_[extensionId];
if (!holder) {
const extension = {
elements: {},
};
holder = /** @type {ExtensionHolderDef} */ ({
extension,
docFactories: [],
shadowRootFactories: [],
promise: undefined,
resolve: undefined,
reject: undefined,
loaded: undefined,
error: undefined,
scriptPresent: undefined,
});
this.extensions_[extensionId] = holder;
}
return holder;
}
/**
* Returns the holder for the extension currently being registered.
* @param {string=} opt_forName Used for logging only.
* @return {!ExtensionHolderDef}
* @private
*/
getCurrentExtensionHolder_(opt_forName) {
if (!this.currentExtensionId_ && !getMode().test) {
dev().error(TAG, 'unknown extension for ', opt_forName);
}
return this.getExtensionHolder_(
this.currentExtensionId_ || UNKNOWN_EXTENSION);
}
/**
* Creates or returns an existing promise that will yield as soon as the
* extension has been loaded.
* @param {!ExtensionHolderDef} holder
* @return {!Promise<!ExtensionDef>}
* @private
*/
waitFor_(holder) {
if (!holder.promise) {
if (holder.loaded) {
holder.promise = Promise.resolve(holder.extension);
} else if (holder.error) {
holder.promise = Promise.reject(holder.error);
} else {
holder.promise = new Promise((resolve, reject) => {
holder.resolve = resolve;
holder.reject = reject;
});
}
}
return holder.promise;
}
/**
* Ensures that the script has already been injected in the page.
* @param {string} extensionId
* @param {!ExtensionHolderDef} holder
* @param {boolean} stubElement
* @private
*/
insertExtensionScriptIfNeeded_(extensionId, holder, stubElement) {
if (this.isExtensionScriptRequired_(extensionId, holder)) {
const scriptElement = this.createExtensionScript_(extensionId);
this.win.document.head.appendChild(scriptElement);
holder.scriptPresent = true;
if (stubElement) {
stubElementIfNotKnown(this.win, extensionId);
}
}
}
/**
* Determine the need to add amp extension script to document.
* @param {string} extensionId
* @param {!ExtensionHolderDef} holder
* @return {boolean}
* @private
*/
isExtensionScriptRequired_(extensionId, holder) {
if (holder.loaded || holder.error) {
return false;
}
if (holder.scriptPresent === undefined) {
const scriptInHead = this.win.document.head.querySelector(
`[custom-element="${extensionId}"]`);
holder.scriptPresent = !!scriptInHead;
}
return !holder.scriptPresent;
}
/**
* Create the missing amp extension HTML script element.
* @param {string} extensionId
* @return {!Element} Script object
* @private
*/
createExtensionScript_(extensionId) {
const scriptElement = this.win.document.createElement('script');
scriptElement.async = true;
scriptElement.setAttribute('custom-element', extensionId);
scriptElement.setAttribute('data-script', extensionId);
const loc = this.win.location;
const useCompiledJs = shouldUseCompiledJs();
const scriptSrc = calculateExtensionScriptUrl(loc, extensionId,
getMode().localDev, getMode().test, useCompiledJs);
scriptElement.src = scriptSrc;
return scriptElement;
}
}
/**
* Calculate the base url for any scripts.
* @param {!Location} location The window's location
* @param {boolean=} isLocalDev
* @param {boolean=} isTest
* @return {string}
*/
export function calculateScriptBaseUrl(location, isLocalDev, isTest) {
if (isLocalDev) {
if (isTest || isMax(location) || isMin(location)) {
return `${location.protocol}//${location.host}/dist`;
}
}
return urls.cdn;
}
/**
* Calculate script url for amp-ad.
* @param {!Location} location The window's location
* @param {string} extensionId
* @param {boolean=} isLocalDev
* @param {boolean=} isTest
* @param {boolean=} isUsingCompiledJs
* @return {string}
*/
export function calculateExtensionScriptUrl(location, extensionId, isLocalDev,
isTest, isUsingCompiledJs) {
const base = calculateScriptBaseUrl(location, isLocalDev, isTest);
if (isLocalDev) {
if ((isTest && !isUsingCompiledJs) || isMax(location)) {
return `${base}/v0/${extensionId}-0.1.max.js`;
}
return `${base}/v0/${extensionId}-0.1.js`;
}
return `${base}/rtv/${getMode().rtvVersion}/v0/${extensionId}-0.1.js`;
}
/**
* Is this path to a max (unminified) version?
* @param {!Location} location
* @return {boolean}
*/
function isMax(location) {
const path = location.pathname;
return path.indexOf('.max') >= 0 || path.substr(0, 5) == '/max/';
}
/**
* Is this path to a minified version?
* @param {!Location} location
* @return {boolean}
*/
function isMin(location) {
const path = location.pathname;
return path.indexOf('.min') >= 0 || path.substr(0, 5) == '/min/';
}
/**
* @return {boolean}
*/
function shouldUseCompiledJs() {
return getMode().test && self.ampTestRuntimeConfig &&
self.ampTestRuntimeConfig.useCompiledJs;
}
/**
* Install builtins.
* @param {!Window} win
* @restricted
*/
export function installBuiltinElements(win) {
installImg(win);
installPixel(win);
installVideo(win);
}
/**
* Copy builtins to a child window.
* @param {!Window} childWin
*/
function copyBuiltinElementsToChildWindow(childWin) {
copyElementToChildWindow(childWin, 'amp-img');
copyElementToChildWindow(childWin, 'amp-pixel');
copyElementToChildWindow(childWin, 'amp-video');
}
/**
* Install polyfills in the child window (friendly iframe).
* @param {!Window} childWin
*/
function installPolyfillsInChildWindow(childWin) {
installDocContains(childWin);
installCustomElements(childWin);
}