-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
amp-access.js
733 lines (646 loc) · 21.1 KB
/
amp-access.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/**
* 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.
*/
import {AccessSource, AccessType} from './amp-access-source';
import {AccessVars} from './access-vars';
import {AmpEvents} from '../../../src/amp-events';
import {CSS} from '../../../build/amp-access-0.1.css';
import {Observable} from '../../../src/observable';
import {Services} from '../../../src/services';
import {cancellation} from '../../../src/error';
import {dev, user} from '../../../src/log';
import {dict} from '../../../src/utils/object';
import {evaluateAccessExpr} from './access-expr';
import {getSourceOrigin} from '../../../src/url';
import {getValueForExpr, tryParseJson} from '../../../src/json';
import {installStylesForDoc} from '../../../src/style-installer';
import {isArray} from '../../../src/types';
import {listenOnce} from '../../../src/event-helper';
import {startsWith} from '../../../src/string';
import {triggerAnalyticsEvent} from '../../../src/analytics';
/** @const */
const TAG = 'amp-access';
/** @const {number} */
const VIEW_TIMEOUT = 2000;
/** @const {string} */
const TEMPLATE_PROP = '__AMP_ACCESS__TEMPLATE';
/**
* AccessService implements the complete lifecycle of the AMP Access system.
* @implements {AccessVars}
*/
export class AccessService {
/**
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
*/
constructor(ampdoc) {
/** @const */
this.ampdoc = ampdoc;
// Install styles.
installStylesForDoc(ampdoc, CSS, () => {}, false, TAG);
const accessElement = ampdoc.getElementById('amp-access');
/** @private {boolean} */
this.enabled_ = !!accessElement;
if (!this.enabled_) {
return;
}
/** @const @private {!Element} */
this.accessElement_ = dev().assertElement(accessElement);
/** @const @private {string} */
this.pubOrigin_ = getSourceOrigin(ampdoc.win.location);
/** @const @private {!../../../src/service/timer-impl.Timer} */
this.timer_ = Services.timerFor(ampdoc.win);
/** @private @const {!../../../src/service/vsync-impl.Vsync} */
this.vsync_ = Services.vsyncFor(ampdoc.win);
// TODO(dvoytenko, #3742): This will refer to the ampdoc once AccessService
// is migrated to ampdoc as well.
/** @private @const {!Promise<!../../../src/service/cid-impl.Cid>} */
this.cid_ = Services.cidForDoc(ampdoc);
/** @private @const {!../../../src/service/viewer-impl.Viewer} */
this.viewer_ = Services.viewerForDoc(ampdoc);
/** @private @const {!../../../src/service/viewport/viewport-impl.Viewport} */
this.viewport_ = Services.viewportForDoc(ampdoc);
/** @private @const {!../../../src/service/template-impl.Templates} */
this.templates_ = Services.templatesFor(ampdoc.win);
/** @private @const {!../../../src/service/resources-impl.Resources} */
this.resources_ = Services.resourcesForDoc(ampdoc);
/** @private @const {?../../../src/service/performance-impl.Performance} */
this.performance_ = Services.performanceForOrNull(ampdoc.win);
/** @private {?Promise<string>} */
this.readerIdPromise_ = null;
/** @const */
this.sources_ = this.parseConfig_();
const promises = this.sources_.map(source => source.whenFirstAuthorized());
/** @private {boolean} */
this.firstAuthorizationsCompleted_ = false;
/**
* Track most recent requests and block reporting and refreshes if
* outstanding. Future optimizations may choose to take action as soon
* as a single request completes. These complete even on failure.
* @private {!Promise}
*/
this.lastAuthorizationPromises_ = Promise.all(promises);
/** @private {?Promise} */
this.reportViewPromise_ = null;
/** @private @const {!Observable} */
this.applyAuthorizationsObservable_ = new Observable();
// This will fire after the first received authorization, even if
// there are multiple sources.
this.lastAuthorizationPromises_.then(() => {
this.firstAuthorizationsCompleted_ = true;
this.analyticsEvent_('access-authorization-received');
if (this.performance_) {
this.performance_.tick('aaa');
this.performance_.tickSinceVisible('aaav');
this.performance_.flush();
}
});
// Re-authorize newly added sections.
ampdoc.getRootNode().addEventListener(AmpEvents.DOM_UPDATE,
this.onDomUpdate_.bind(this));
}
/** @override from AccessVars */
getAccessReaderId() {
if (!this.enabled_) {
return null;
}
return this.getReaderId_();
}
/**
* @return {!Promise<string>}
* @private
*/
getReaderId_() {
if (!this.readerIdPromise_) {
// No consent - an essential part of the access system.
const consent = Promise.resolve();
this.readerIdPromise_ = this.cid_.then(cid => {
return cid.get({scope: 'amp-access', createCookieIfNotPresent: true},
consent);
});
}
return this.readerIdPromise_;
}
/**
* @return {boolean}
*/
areFirstAuthorizationsCompleted() {
return this.firstAuthorizationsCompleted_;
}
/**
* Registers a callback to be triggered when the document gets (re)authorized.
* @param {!Function} callback
*/
onApplyAuthorizations(callback) {
this.applyAuthorizationsObservable_.add(callback);
}
/**
* @return {!Array<!AccessSource>}
* @private
*/
parseConfig_() {
const rawContent = tryParseJson(this.accessElement_.textContent, e => {
throw user().createError('Failed to parse "amp-access" JSON: ' + e);
});
const configMap = {};
if (isArray(rawContent)) {
const contentArray = rawContent;
for (let i = 0; i < contentArray['length']; i++) {
const namespace = contentArray[i]['namespace'];
user().assert(!!namespace, 'Namespace required');
user().assert(!configMap[namespace],
'Namespace already used: ' + namespace);
configMap[namespace] = contentArray[i];
}
} else {
configMap[rawContent['namespace'] || ''] = rawContent;
}
const readerIdFn = this.getReaderId_.bind(this);
const scheduleViewFn = this.scheduleView_.bind(this);
const onReauthorizeFn = this.onReauthorize_.bind(this);
return Object.keys(configMap).map(key =>
new AccessSource(this.ampdoc, configMap[key], readerIdFn, scheduleViewFn,
onReauthorizeFn, this.accessElement_)
);
}
/**
* @param {!Event} event
* @private
*/
onDomUpdate_(event) {
// Only re-authorize sections if authorization already fired, otherwise
// just wait and existing callback will cover new sections.
if (this.firstAuthorizationsCompleted_) {
// Guard against anything else in flight.
return this.lastAuthorizationPromises_.then(() => {
const target = dev().assertElement(event.target);
const responses = this.combinedResponses();
this.applyAuthorizationToRoot_(target, responses);
});
}
}
/**
* @param {string} name
* @return {!AccessSource}
*/
getVendorSource(name) {
for (let i = 0; i < this.sources_.length; i++) {
const source = this.sources_[i];
if (source.getType() == AccessType.VENDOR) {
const vendorAdapter =
/** @type {!./amp-access-vendor.AccessVendorAdapter} */ (
source.getAdapter()
);
if (vendorAdapter.getVendorName() == name) {
return source;
}
}
}
user().assert(false,
'Access vendor "%s" can only be used for "type=vendor", but none found',
name);
// Should not happen, just to appease type checking.
throw new Error();
}
/**
* @return {boolean}
*/
isEnabled() {
return this.enabled_;
}
/**
* @return {!Element}
* @private
*/
getRootElement_() {
const root = this.ampdoc.getRootNode();
return dev().assertElement(root.documentElement || root.body || root);
}
/**
* @param {string} eventType
* @private
*/
analyticsEvent_(eventType) {
triggerAnalyticsEvent(this.getRootElement_(), eventType);
}
/**
* @return {!AccessService}
* @private
* @restricted
*/
start_() {
if (!this.enabled_) {
user().info(TAG, 'Access is disabled - no "id=amp-access" element');
return this;
}
this.startInternal_();
return this;
}
/** @private */
startInternal_() {
// TODO(dvoytenko, #3742): This will refer to the ampdoc once AccessService
// is migrated to ampdoc as well.
Services.actionServiceForDoc(this.ampdoc).installActionHandler(
this.accessElement_, this.handleAction_.bind(this));
for (let i = 0; i < this.sources_.length; i++) {
this.sources_[i].start();
}
// Run authorization as soon as visible.
this.runAuthorization_();
// Wait for the "view" signal.
this.scheduleView_(VIEW_TIMEOUT);
// Listen to amp-access broadcasts from other pages.
this.listenToBroadcasts_();
}
/** @private */
listenToBroadcasts_() {
this.viewer_.onBroadcast(message => {
if (message['type'] == 'amp-access-reauthorize' &&
message['origin'] == this.pubOrigin_) {
this.runAuthorization_();
}
});
}
/**
* @param {!Promise} authorization
* @private
*/
onReauthorize_(authorization) {
this.broadcastReauthorize_();
authorization.then(() => {
// If nothing has happened, initial render will cover this change.
if (this.firstAuthorizationsCompleted_) {
// Guard against anything else in flight.
this.lastAuthorizationPromises_.then(() => {
this.ampdoc.whenReady().then(() => {
const root = this.ampdoc.getRootNode();
const responses = this.combinedResponses();
return this.applyAuthorizationToRoot_(root, responses);
});
});
}
});
}
/** @private */
broadcastReauthorize_() {
this.viewer_.broadcast(dict({
'type': 'amp-access-reauthorize',
'origin': this.pubOrigin_,
}));
}
/**
* Returns the promise that resolves when all authorization work has
* completed, including authorization endpoint call and UI update.
* Note that this promise never fails.
* @param {boolean=} opt_disableFallback
* @return {!Promise}
* @private
*/
runAuthorization_(opt_disableFallback) {
this.toggleTopClass_('amp-access-loading', true);
const authorizations = this.viewer_.whenFirstVisible().then(() => {
return Promise.all(
this.sources_.map(source => this.runOneAuthorization_(source)));
});
const rendered = authorizations.then(() => {
this.toggleTopClass_('amp-access-loading', false);
return this.ampdoc.whenReady().then(() => {
const root = this.ampdoc.getRootNode();
const responses = this.combinedResponses();
return this.applyAuthorizationToRoot_(root, responses);
});
});
this.lastAuthorizationPromises_ = rendered;
return rendered;
}
/**
* Make a single authorization call.
* @param {AccessSource} source
* @return {Promise}
* @private
*/
runOneAuthorization_(source) {
return source.runAuthorization()
.catch(() => {
this.toggleTopClass_('amp-access-error', true);
});
}
/** @override from AccessVars */
getAuthdataField(field) {
if (!this.enabled_) {
return null;
}
return this.lastAuthorizationPromises_.then(() => {
const responses = this.combinedResponses();
const v = getValueForExpr(responses, field);
return v !== undefined ? v : null;
});
}
/**
* @param {!Document|!ShadowRoot|!Element} root
* @param {!JsonObject} response
* @return {!Promise}
* @private
*/
applyAuthorizationToRoot_(root, response) {
const elements = root.querySelectorAll('[amp-access]');
const promises = [];
for (let i = 0; i < elements.length; i++) {
promises.push(this.applyAuthorizationToElement_(elements[i], response));
}
return Promise.all(promises).then(() => {
this.applyAuthorizationsObservable_.fire();
});
}
/**
* @param {!Element} element
* @param {!JsonObject} response
* @return {!Promise}
* @private
*/
applyAuthorizationToElement_(element, response) {
const expr = element.getAttribute('amp-access');
const on = evaluateAccessExpr(expr, response);
let renderPromise = null;
if (on) {
renderPromise = this.renderTemplates_(element, response);
}
if (renderPromise) {
return renderPromise.then(() =>
this.applyAuthorizationAttrs_(element, on));
}
return this.applyAuthorizationAttrs_(element, on);
}
/**
* @param {!Element} element
* @param {boolean} on
* @return {!Promise}
* @private
*/
applyAuthorizationAttrs_(element, on) {
const wasOn = !element.hasAttribute('amp-access-hide');
if (on == wasOn) {
return Promise.resolve();
}
return this.resources_.mutateElement(element, () => {
if (on) {
element.removeAttribute('amp-access-hide');
} else {
element.setAttribute('amp-access-hide', '');
}
});
}
/**
* Discovers and renders templates.
* @param {!Element} element
* @param {!JsonObject} response
* @return {?Promise}
* @private
*/
renderTemplates_(element, response) {
const promises = [];
const templateElements = element.querySelectorAll('[amp-access-template]');
if (templateElements.length > 0) {
for (let i = 0; i < templateElements.length; i++) {
const p = this.renderTemplate_(element, templateElements[i], response)
.catch(error => {
// Ignore the error.
dev().error(TAG, 'Template failed: ', error,
templateElements[i], element);
});
promises.push(p);
}
}
return promises.length > 0 ? Promise.all(promises) : null;
}
/**
* @param {!Element} element
* @param {!Element} templateOrPrev
* @param {!JsonObject} response
* @return {!Promise}
* @private
*/
renderTemplate_(element, templateOrPrev, response) {
let template = templateOrPrev;
let prev = null;
if (template.tagName != 'TEMPLATE') {
prev = template;
template = prev[TEMPLATE_PROP];
}
if (!template) {
return Promise.reject(new Error('template not found'));
}
const rendered = this.templates_.renderTemplate(template, response);
return rendered.then(element => {
return this.vsync_.mutatePromise(() => {
element.setAttribute('amp-access-template', '');
element[TEMPLATE_PROP] = template;
if (template.parentElement) {
template.parentElement.replaceChild(element, template);
} else if (prev && prev.parentElement) {
prev.parentElement.replaceChild(element, prev);
}
});
});
}
/**
* @param {time} timeToView
* @private
*/
scheduleView_(timeToView) {
if (!this.sources_.some(s => s.getAdapter().isPingbackEnabled())) {
return;
}
this.reportViewPromise_ = null;
this.ampdoc.whenReady().then(() => {
if (this.viewer_.isVisible()) {
this.reportWhenViewed_(timeToView);
}
this.viewer_.onVisibilityChanged(() => {
if (this.viewer_.isVisible()) {
this.reportWhenViewed_(timeToView);
}
});
});
}
/**
* @param {time} timeToView
* @return {!Promise}
* @private
*/
reportWhenViewed_(timeToView) {
if (this.reportViewPromise_) {
return this.reportViewPromise_;
}
dev().fine(TAG, 'start view monitoring');
this.reportViewPromise_ = this.whenViewed_(timeToView)
.then(() => {
// Wait for the most recent authorization flow to complete.
return this.lastAuthorizationPromises_;
})
.then(() => {
// Report the analytics event.
this.analyticsEvent_('access-viewed');
return this.reportViewToServer_();
})
.catch(reason => {
// Ignore - view has been canceled.
dev().fine(TAG, 'view cancelled:', reason);
this.reportViewPromise_ = null;
throw reason;
});
// Support pre-rendering with metering by possibly hiding content
// after view is recorded.
this.reportViewPromise_.then(this.broadcastReauthorize_.bind(this));
return this.reportViewPromise_;
}
/**
* The promise will be resolved when a view of this document has occurred. It
* will be rejected if the current impression should not be counted as a view.
* @param {time} timeToView Pass the value of 0 when this method is called
* as the result of the user action.
* @return {!Promise}
* @private
*/
whenViewed_(timeToView) {
if (timeToView == 0) {
// Immediate view has been registered. This will happen when this method
// is called as the result of the user action.
return Promise.resolve();
}
// Viewing kick off: document is visible.
const unlistenSet = [];
return new Promise((resolve, reject) => {
// 1. Document becomes invisible again: cancel.
unlistenSet.push(this.viewer_.onVisibilityChanged(() => {
if (!this.viewer_.isVisible()) {
reject(cancellation());
}
}));
// 2. After a few seconds: register a view.
const timeoutId = this.timer_.delay(resolve, timeToView);
unlistenSet.push(() => this.timer_.cancel(timeoutId));
// 3. If scrolled: register a view.
unlistenSet.push(this.viewport_.onScroll(resolve));
// 4. Tap: register a view.
unlistenSet.push(listenOnce(this.ampdoc.getRootNode(),
'click', resolve));
}).then(() => {
unlistenSet.forEach(unlisten => unlisten());
}, reason => {
unlistenSet.forEach(unlisten => unlisten());
throw reason;
});
}
/**
* @return {!Promise}
* @private
*/
reportViewToServer_() {
const promises = [];
for (let i = 0; i < this.sources_.length; i++) {
if (this.sources_[i].getAdapter().isPingbackEnabled()) {
promises.push(this.sources_[i].reportViewToServer());
}
}
return Promise.all(promises);
}
/**
* @param {string} className
* @param {boolean} on
* @private
*/
toggleTopClass_(className, on) {
this.vsync_.mutate(() => {
this.getRootElement_().classList.toggle(className, on);
});
}
/**
* @param {!../../../src/service/action-impl.ActionInvocation} invocation
* @return {?Promise}
* @private
*/
handleAction_(invocation) {
if (invocation.method == 'login') {
if (invocation.event) {
invocation.event.preventDefault();
}
this.loginWithType_('');
} else if (startsWith(invocation.method, 'login-')) {
if (invocation.event) {
invocation.event.preventDefault();
}
this.loginWithType_(invocation.method.substring('login-'.length));
}
return null;
}
/**
* Expose the underlying AccessSource for use by laterpay.
* @param {number} index
* @return {!AccessSource}
*/
getSource(index) {
user().assert(index >= 0 && index < this.sources_.length,
'Invalid index: %d', index);
return this.sources_[index];
}
/**
* Runs the login flow using one of the predefined urls in the amp-access
* config
*
* @private
* @param {string} type Type of login defined in the config
* @return {!Promise}
*/
loginWithType_(type) {
const splitPoint = type.indexOf('-');
const singleSource = this.sources_.length == 1;
// Try to find a matching namespace
const namespace = (splitPoint > -1) ? type.substring(0, splitPoint) : type;
const match = this.sources_.filter(s => s.getNamespace() == namespace);
if (match.length) {
// Matching namespace found
const remaining = (splitPoint > -1) ? type.substring(splitPoint + 1) : '';
return match[0].loginWithType(remaining);
}
// If there is only one source, process as standalone
user().assert(singleSource, 'Login must match namespace: %s', namespace);
return this.sources_[0].loginWithType(type);
}
/**
* Either combine namespaced responses or just return the single one.
*
* @return {!JsonObject}
*/
combinedResponses() {
if (this.sources_.length == 1 && !this.sources_[0].getNamespace()) {
return /** @type {!JsonObject} */ (this.sources_[0].getAuthResponse() ||
{});
}
const combined = /** @type {!JsonObject} */ ({});
this.sources_.forEach(source =>
combined[source.getNamespace()] = source.getAuthResponse());
return combined;
}
}
// Register the extension services.
AMP.extension(TAG, '0.1', function(AMP) {
AMP.registerServiceForDoc('access', function(ampdoc) {
return new AccessService(ampdoc).start_();
});
});
/** @package Visible for testing only. */
export function getAccessVarsClassForTesting() {
return AccessVars;
}