forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-helpers.js
664 lines (616 loc) · 18.2 KB
/
service-helpers.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/**
* 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 Registration and getter functions for AMP services.
*
* Invariant: Service getters never return null for registered services.
*/
import {Deferred} from './core/data-structures/promise';
import {toWin} from './core/window';
import {dev, devAssert} from './log';
/**
* Holds info about a service.
* - obj: Actual service implementation when available.
* - promise: Promise for the obj.
* - resolve: Function to resolve the promise with the object.
* - context: Argument for ctor, either a window or an ampdoc.
* - ctor: Function that constructs and returns the service.
* @typedef {{
* obj: (?Object),
* promise: (?Promise),
* resolve: (?function(!Object)),
* reject: (?function((*))),
* context: (?Window|?./service/ampdoc-impl.AmpDoc),
* ctor: (function(new:Object, !Window)|
* function(new:Object, !./service/ampdoc-impl.AmpDoc)),
* }}
*/
let ServiceHolderDef;
/**
* This interface provides a `dispose` method that will be called by
* runtime when a service needs to be disposed of.
* @interface
*/
export class Disposable {
/**
* Instructs the service to release any resources it might be holding. Can
* be called only once in the lifecycle of a service.
*/
dispose() {}
}
/**
* Installs a service override on amp-doc level.
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @param {string} id
* @param {!Object} service The service.
*/
export function installServiceInEmbedDoc(ampdoc, id, service) {
registerServiceInternal(
getAmpdocServiceHolder(ampdoc),
ampdoc,
id,
function () {
return service;
},
/* override */ true
);
}
/**
* Installs a service override in the scope of an embedded window.
* @param {!Window} embedWin
* @param {string} id
* @param {function(new:Object, !Window)} constructor
*/
export function registerServiceBuilderInEmbedWin(embedWin, id, constructor) {
registerServiceInternal(
embedWin,
embedWin,
id,
constructor,
/* override */ true
);
}
/**
* Registers a service given a class to be used as implementation.
* @param {!Window} win
* @param {string} id of the service.
* @param {function(new:Object, !Window)} constructor
* @param {boolean=} opt_instantiate Whether to immediately create the service
*/
export function registerServiceBuilder(win, id, constructor, opt_instantiate) {
win = getTopWindow(win);
registerServiceInternal(win, win, id, constructor);
if (opt_instantiate) {
getServiceInternal(win, id);
}
}
/**
* Returns a service and registers it given a class to be used as
* implementation.
* @param {!Node|!./service/ampdoc-impl.AmpDoc} nodeOrDoc
* @param {string} id of the service.
* @param {function(new:Object, !./service/ampdoc-impl.AmpDoc)} constructor
* @param {boolean=} opt_instantiate Whether to immediately create the service
*/
export function registerServiceBuilderForDoc(
nodeOrDoc,
id,
constructor,
opt_instantiate
) {
const ampdoc = getAmpdoc(nodeOrDoc);
const holder = getAmpdocServiceHolder(ampdoc);
registerServiceInternal(holder, ampdoc, id, constructor);
if (opt_instantiate) {
getServiceInternal(holder, id);
}
}
/**
* Reject a service promise.
* @param {!Node|!./service/ampdoc-impl.AmpDoc} nodeOrDoc
* @param {string} id
* @param {*} error
*/
export function rejectServicePromiseForDoc(nodeOrDoc, id, error) {
const ampdoc = getAmpdoc(nodeOrDoc);
const holder = getAmpdocServiceHolder(ampdoc);
rejectServicePromiseInternal(holder, id, error);
}
/**
* Returns a service for the given id and window (a per-window singleton). Users
* should typically wrap this as a special purpose function (e.g.
* `Services.vsyncFor(win)`) for type safety and because the factory should not
* be passed around.
* @param {!Window} win
* @param {string} id of the service.
* @template T
* @return {T}
*/
export function getService(win, id) {
win = getTopWindow(win);
return getServiceInternal(win, id);
}
/**
* Returns a service for the given id and window (a per-window singleton). But
* it looks in the immediate window scope, not the top-level window.
* @param {!Window} win
* @param {string} id of the service.
* @template T
* @return {T}
*/
export function getServiceInEmbedWin(win, id) {
return getServiceInternal(win, id);
}
/**
* Returns a promise for a service for the given id and window. Also expects an
* element that has the actual implementation. The promise resolves when the
* implementation loaded. Users should typically wrap this as a special purpose
* function (e.g. `Services.vsyncFor(win)`) for type safety and because the
* factory should not be passed around.
* @param {!Window} win
* @param {string} id of the service.
* @return {!Promise<!Object>}
*/
export function getServicePromise(win, id) {
return getServicePromiseInternal(win, id);
}
/**
* Returns a service or null with the given id.
* @param {!Window} win
* @param {string} id
* @return {?Object} The service.
*/
export function getExistingServiceOrNull(win, id) {
win = getTopWindow(win);
if (isServiceRegistered(win, id)) {
return getServiceInternal(win, id);
} else {
return null;
}
}
/**
* Like getServicePromise but returns null if the service was never registered.
* @param {!Window} win
* @param {string} id
* @return {?Promise<!Object>}
*/
export function getServicePromiseOrNull(win, id) {
return getServicePromiseOrNullInternal(win, id);
}
/**
* Returns a service for the given id and ampdoc (a per-ampdoc singleton).
* Expects service `id` to be registered.
* @param {!Element|!ShadowRoot|!./service/ampdoc-impl.AmpDoc} elementOrAmpDoc
* @param {string} id
* @return {T}
* @template T
*/
export function getServiceForDoc(elementOrAmpDoc, id) {
const ampdoc = getAmpdoc(elementOrAmpDoc);
const holder = getAmpdocServiceHolder(ampdoc);
return getServiceInternal(holder, id);
}
/**
* Returns a service for the given id and ampdoc (a per-ampdoc singleton).
* If service `id` is not registered, returns null.
* @param {!Element|!ShadowRoot|!./service/ampdoc-impl.AmpDoc} elementOrAmpDoc
* @param {string} id
* @return {?Object}
*/
export function getServiceForDocOrNull(elementOrAmpDoc, id) {
const ampdoc = getAmpdoc(elementOrAmpDoc);
const holder = getAmpdocServiceHolder(ampdoc);
if (isServiceRegistered(holder, id)) {
return getServiceInternal(holder, id);
} else {
return null;
}
}
/**
* Returns a promise for a service for the given id and ampdoc. Also expects
* a service that has the actual implementation. The promise resolves when
* the implementation loaded.
* @param {!Element|!ShadowRoot|!./service/ampdoc-impl.AmpDoc} elementOrAmpDoc
* @param {string} id
* @return {!Promise<!Object>}
*/
export function getServicePromiseForDoc(elementOrAmpDoc, id) {
return getServicePromiseInternal(getAmpdocServiceHolder(elementOrAmpDoc), id);
}
/**
* Like getServicePromiseForDoc but returns null if the service was never
* registered for this ampdoc.
* @param {!Element|!ShadowRoot|!./service/ampdoc-impl.AmpDoc} elementOrAmpDoc
* @param {string} id
* @return {?Promise<!Object>}
*/
export function getServicePromiseOrNullForDoc(elementOrAmpDoc, id) {
return getServicePromiseOrNullInternal(
getAmpdocServiceHolder(elementOrAmpDoc),
id
);
}
/**
* Set the parent and top windows on a child window (friendly iframe).
* @param {!Window} win
* @param {!Window} parentWin
*/
export function setParentWindow(win, parentWin) {
win.__AMP_PARENT = parentWin;
win.__AMP_TOP = getTopWindow(parentWin);
}
/**
* Returns the parent window for a child window (friendly iframe).
* @param {!Window} win
* @return {!Window}
*/
export function getParentWindow(win) {
return win.__AMP_PARENT || win;
}
/**
* Returns the top window where AMP Runtime is installed for a child window
* (friendly iframe).
* @param {!Window} win
* @return {!Window}
*/
export function getTopWindow(win) {
return win.__AMP_TOP || (win.__AMP_TOP = win);
}
/**
* Returns the parent "friendly" iframe if the node belongs to a child window.
* @param {!Node} node
* @param {!Window=} opt_topWin
* @return {?HTMLIFrameElement}
*/
export function getParentWindowFrameElement(node, opt_topWin) {
const childWin = (node.ownerDocument || node).defaultView;
const topWin = opt_topWin || getTopWindow(childWin);
if (childWin && childWin != topWin && getTopWindow(childWin) == topWin) {
try {
return /** @type {?HTMLIFrameElement} */ (childWin.frameElement);
} catch (e) {
// Ignore the error.
}
}
return null;
}
/**
* @param {!Node|!./service/ampdoc-impl.AmpDoc} nodeOrDoc
* @return {!./service/ampdoc-impl.AmpDoc}
*/
export function getAmpdoc(nodeOrDoc) {
if (nodeOrDoc.nodeType) {
const win = toWin(
/** @type {!Document} */ (nodeOrDoc.ownerDocument || nodeOrDoc)
.defaultView
);
return getAmpdocService(win).getAmpDoc(/** @type {!Node} */ (nodeOrDoc));
}
return /** @type {!./service/ampdoc-impl.AmpDoc} */ (nodeOrDoc);
}
/**
* @param {!Node|!./service/ampdoc-impl.AmpDoc} nodeOrDoc
* @return {!./service/ampdoc-impl.AmpDoc|!Window}
*/
function getAmpdocServiceHolder(nodeOrDoc) {
const ampdoc = getAmpdoc(nodeOrDoc);
return ampdoc.isSingleDoc() ? ampdoc.win : ampdoc;
}
/**
* This is essentially a duplicate of `ampdoc.js`, but necessary to avoid
* circular dependencies.
* @param {!Window} win
* @return {!./service/ampdoc-impl.AmpDocService}
*/
function getAmpdocService(win) {
return /** @type {!./service/ampdoc-impl.AmpDocService} */ (
getService(win, 'ampdoc')
);
}
/**
* Get service `id` from `holder`. Assumes the service
* has already been registered.
* @param {!Object} holder Object holding the service instance.
* @param {string} id of the service.
* @return {Object}
*/
function getServiceInternal(holder, id) {
devAssert(
isServiceRegistered(holder, id),
`Expected service ${id} to be registered`
);
const services = getServices(holder);
const s = services[id];
if (!s.obj) {
devAssert(s.ctor, `Service ${id} registered without ctor nor impl.`);
devAssert(s.context, `Service ${id} registered without context.`);
s.obj = new s.ctor(s.context);
devAssert(s.obj, `Service ${id} constructed to null.`);
s.context = null;
// The service may have been requested already, in which case we have a
// pending promise we need to fulfill.
if (s.resolve) {
s.resolve(s.obj);
}
}
return s.obj;
}
/**
* @param {!Object} holder Object holding the service instance.
* @param {!Window|!./service/ampdoc-impl.AmpDoc} context Win or AmpDoc.
* @param {string} id of the service.
* @param {?function(new:Object, !Window)|?function(new:Object, !./service/ampdoc-impl.AmpDoc)} ctor Constructor function to new the service. Called with context.
* @param {boolean=} opt_override
* @param {boolean=} opt_sharedInstance
*/
function registerServiceInternal(
holder,
context,
id,
ctor,
opt_override,
opt_sharedInstance
) {
const services = getServices(holder);
let s = services[id];
if (!s) {
s = services[id] = {
obj: null,
promise: null,
resolve: null,
reject: null,
context: null,
ctor: null,
sharedInstance: opt_sharedInstance || false,
};
}
if (!opt_override && s.ctor) {
// Service already registered.
return;
}
s.ctor = ctor;
s.context = context;
s.sharedInstance = opt_sharedInstance || false;
// The service may have been requested already, in which case there is a
// pending promise that needs to fulfilled.
if (s.resolve) {
// getServiceInternal will resolve the promise.
getServiceInternal(holder, id);
}
}
/**
* @param {!Object} holder
* @param {string} id of the service.
* @return {!Promise<!Object>}
*/
function getServicePromiseInternal(holder, id) {
const cached = getServicePromiseOrNullInternal(holder, id);
if (cached) {
return cached;
}
// Service is not registered.
// TODO(@cramforce): Add a check that if the element is eventually registered
// that the service is actually provided and this promise resolves.
const services = getServices(holder);
services[id] = emptyServiceHolderWithPromise();
return /** @type {!Promise<!Object>} */ (services[id].promise);
}
/**
* @param {!Object} holder
* @param {string} id of the service.
* @param {*} error
*/
function rejectServicePromiseInternal(holder, id, error) {
const services = getServices(holder);
const s = services[id];
if (s) {
if (s.reject) {
s.reject(error);
}
return;
}
services[id] = emptyServiceHolderWithPromise();
services[id].reject(error);
}
/**
* Returns a promise for service `id` if the service has been registered
* on `holder`.
* @param {!Object} holder
* @param {string} id of the service.
* @return {?Promise<!Object>}
*/
function getServicePromiseOrNullInternal(holder, id) {
const services = getServices(holder);
const s = services[id];
if (s) {
if (s.promise) {
return s.promise;
} else {
// Instantiate service if not already instantiated.
getServiceInternal(holder, id);
return (s.promise = Promise.resolve(/** @type {!Object} */ (s.obj)));
}
}
return null;
}
/**
* Returns the object that holds the services registered in a holder.
* @param {!Object} holder
* @return {!Object<string,!ServiceHolderDef>}
*/
function getServices(holder) {
let services = holder.__AMP_SERVICES;
if (!services) {
services = holder.__AMP_SERVICES = {};
}
return services;
}
/**
* Whether the specified service implements `Disposable` interface.
* @param {!Object} service
* @return {boolean}
*/
export function isDisposable(service) {
return typeof service.dispose == 'function';
}
/**
* Asserts that the specified service implements `Disposable` interface and
* typecasts the instance to `Disposable`.
* @param {!Object} service
* @return {!Disposable}
*/
export function assertDisposable(service) {
devAssert(isDisposable(service), 'required to implement Disposable');
return /** @type {!Disposable} */ (service);
}
/**
* Disposes all disposable (implements `Disposable` interface) services in
* ampdoc scope.
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
*/
export function disposeServicesForDoc(ampdoc) {
disposeServicesInternal(ampdoc);
}
/**
* Disposes all disposable (implements `Disposable` interface) services in
* embed scope.
* @param {!Window} embedWin
*/
export function disposeServicesForEmbed(embedWin) {
disposeServicesInternal(embedWin);
}
/**
* @param {!Object} holder Object holding the service instances.
*/
function disposeServicesInternal(holder) {
const services = getServices(holder);
for (const id in services) {
if (!Object.prototype.hasOwnProperty.call(services, id)) {
continue;
}
const serviceHolder = services[id];
if (serviceHolder.sharedInstance) {
continue;
}
if (serviceHolder.obj) {
disposeServiceInternal(id, serviceHolder.obj);
} else if (serviceHolder.promise) {
serviceHolder.promise.then((instance) =>
disposeServiceInternal(id, instance)
);
}
}
}
/**
* @param {string} id
* @param {!Object} service
*/
function disposeServiceInternal(id, service) {
if (!isDisposable(service)) {
return;
}
try {
assertDisposable(service).dispose();
} catch (e) {
// Ensure that a failure to dispose a service does not disrupt other
// services.
dev().error('SERVICE', 'failed to dispose service', id, e);
}
}
/**
* This adopts the service **instance** from the parent.
*
* This function is dangerous! Sharing an instance means data can leak to and
* from a child ampdoc.
*
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @param {string} id
*/
export function adoptServiceForEmbedDoc(ampdoc, id) {
const service = getServiceInternal(
getAmpdocServiceHolder(devAssert(ampdoc.getParent())),
id
);
registerServiceInternal(
getAmpdocServiceHolder(ampdoc),
ampdoc,
id,
function () {
return service;
},
/* override */ false,
/* sharedInstance */ true
);
}
/**
* This adopts the service **factory** from the parent.
*
* This function is safer than sharing the service instance, since each ampdoc
* will create its own instance of the factory (and each instance will have its
* own instance data). Note that static data is still shared, so it's not 100%
* foolproof.
*
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @param {string} id
*/
export function adoptServiceFactoryForEmbedDoc(ampdoc, id) {
const parentHolder = getAmpdocServiceHolder(devAssert(ampdoc.getParent()));
devAssert(
isServiceRegistered(parentHolder, id),
`Expected service ${id} to be registered`
);
const service = getServices(parentHolder)[id];
registerServiceInternal(
getAmpdocServiceHolder(ampdoc),
ampdoc,
id,
devAssert(service.ctor)
);
}
/**
* Resets a single service, so it gets recreated on next getService invocation.
* @param {!Object} holder
* @param {string} id of the service.
*/
export function resetServiceForTesting(holder, id) {
if (holder.__AMP_SERVICES) {
holder.__AMP_SERVICES[id] = null;
}
}
/**
* @param {!Object} holder Object holding the service instance.
* @param {string} id of the service.
* @return {boolean}
*/
function isServiceRegistered(holder, id) {
const service = holder.__AMP_SERVICES && holder.__AMP_SERVICES[id];
// All registered services must have a constructor.
return !!(service && service.ctor);
}
/** @return {!ServiceHolderDef} */
function emptyServiceHolderWithPromise() {
const deferred = new Deferred();
const {promise, reject, resolve} = deferred;
promise.catch(() => {}); // avoid uncaught exception when service gets rejected
return {
obj: null,
promise,
resolve,
reject,
context: null,
ctor: null,
};
}