Skip to content

Commit d98f2e3

Browse files
author
Dima Voytenko
authored
Remove visibilityState from viewer (ampproject#24566)
* Remove visibilityState from viewer * working on chunks * clarify chunk support * tests * test fixes * cleanup
1 parent fdc1901 commit d98f2e3

File tree

104 files changed

+553
-902
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+553
-902
lines changed

ads/google/a4a/test/test-utils.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function setupForAdTesting(fixture) {
6666
// functions.
6767
function noopMethods(
6868
impl,
69-
doc,
69+
ampdoc,
7070
sandbox,
7171
pageLayoutBox = {
7272
top: 11,
@@ -81,7 +81,7 @@ function noopMethods(
8181
impl.element.build = noop;
8282
impl.element.getPlaceholder = noop;
8383
impl.element.createPlaceholder = noop;
84-
sandbox.stub(impl, 'getAmpDoc').returns(doc);
84+
sandbox.stub(impl, 'getAmpDoc').returns(ampdoc);
8585
sandbox.stub(impl, 'getPageLayoutBox').returns(pageLayoutBox);
8686
}
8787

@@ -331,7 +331,7 @@ describe('Google A4A utils', () => {
331331
'height': '50',
332332
});
333333
const impl = new MockA4AImpl(elem);
334-
noopMethods(impl, doc, sandbox);
334+
noopMethods(impl, fixture.ampdoc, sandbox);
335335
return fixture.addElement(elem).then(() => {
336336
return googleAdUrl(impl, '', 0, [], []).then(url1 => {
337337
expect(url1).to.match(/ady=11/);
@@ -352,7 +352,7 @@ describe('Google A4A utils', () => {
352352
'height': '50',
353353
});
354354
const impl = new MockA4AImpl(elem);
355-
noopMethods(impl, doc, sandbox);
355+
noopMethods(impl, fixture.ampdoc, sandbox);
356356
const getRect = () => {
357357
return {'width': 100, 'height': 200};
358358
};
@@ -385,7 +385,7 @@ describe('Google A4A utils', () => {
385385
'data-experiment-id': '123,456',
386386
});
387387
const impl = new MockA4AImpl(elem);
388-
noopMethods(impl, doc, sandbox);
388+
noopMethods(impl, fixture.ampdoc, sandbox);
389389
return fixture.addElement(elem).then(() => {
390390
return googleAdUrl(impl, '', 0, {}, ['789', '098']).then(url1 => {
391391
expect(url1).to.match(/eid=123%2C456%2C789%2C098/);
@@ -405,7 +405,7 @@ describe('Google A4A utils', () => {
405405
'height': '50',
406406
});
407407
const impl = new MockA4AImpl(elem);
408-
noopMethods(impl, doc, sandbox);
408+
noopMethods(impl, fixture.ampdoc, sandbox);
409409
impl.win.AMP_CONFIG = {type: 'production'};
410410
impl.win.location.hash = 'foo,deid=123456,654321,bar';
411411
return fixture.addElement(elem).then(() => {
@@ -427,7 +427,7 @@ describe('Google A4A utils', () => {
427427
'height': '50',
428428
});
429429
const impl = new MockA4AImpl(elem);
430-
noopMethods(impl, doc, sandbox);
430+
noopMethods(impl, fixture.ampdoc, sandbox);
431431
impl.win.gaGlobal = {cid: 'foo', hid: 'bar'};
432432
return fixture.addElement(elem).then(() => {
433433
return googleAdUrl(impl, '', 0, [], []).then(url => {
@@ -449,7 +449,7 @@ describe('Google A4A utils', () => {
449449
'height': '50',
450450
});
451451
const impl = new MockA4AImpl(elem);
452-
noopMethods(impl, doc, sandbox);
452+
noopMethods(impl, fixture.ampdoc, sandbox);
453453
const createElementStub = sandbox.stub(
454454
impl.win.document,
455455
'createElement'
@@ -478,7 +478,7 @@ describe('Google A4A utils', () => {
478478
'height': '50',
479479
});
480480
const impl = new MockA4AImpl(elem);
481-
noopMethods(impl, doc, sandbox);
481+
noopMethods(impl, fixture.ampdoc, sandbox);
482482
const createElementStub = sandbox.stub(
483483
impl.win.document,
484484
'createElement'
@@ -505,7 +505,7 @@ describe('Google A4A utils', () => {
505505
'height': '50',
506506
});
507507
const impl = new MockA4AImpl(elem);
508-
noopMethods(impl, doc, sandbox);
508+
noopMethods(impl, fixture.ampdoc, sandbox);
509509
impl.win.SVGElement = undefined;
510510
const createElementStub = sandbox.stub(
511511
impl.win.document,
@@ -535,7 +535,7 @@ describe('Google A4A utils', () => {
535535
'height': '50',
536536
});
537537
const impl = new MockA4AImpl(elem);
538-
noopMethods(impl, doc, sandbox);
538+
noopMethods(impl, fixture.ampdoc, sandbox);
539539
sandbox
540540
.stub(Services.viewerForDoc(impl.getAmpDoc()), 'getReferrerUrl')
541541
.returns(new Promise(() => {}));
@@ -564,7 +564,7 @@ describe('Google A4A utils', () => {
564564
doc.win = fixture.win;
565565
const elem = createElementWithAttributes(doc, 'amp-a4a', {});
566566
const impl = new MockA4AImpl(elem);
567-
noopMethods(impl, doc, sandbox);
567+
noopMethods(impl, fixture.ampdoc, sandbox);
568568
return fixture.addElement(elem).then(() => {
569569
return googleAdUrl(impl, '', Date.now(), [], []).then(url => {
570570
expect(url).to.match(/[&?]bdt=[1-9][0-9]*[&$]/);
@@ -580,7 +580,7 @@ describe('Google A4A utils', () => {
580580
doc.win = fixture.win;
581581
const elem = createElementWithAttributes(doc, 'amp-a4a', {});
582582
const impl = new MockA4AImpl(elem);
583-
noopMethods(impl, doc, sandbox, {
583+
noopMethods(impl, fixture.ampdoc, sandbox, {
584584
top: 0,
585585
left: 0,
586586
right: 0,
@@ -902,6 +902,7 @@ describe('Google A4A utils', () => {
902902
describe('variables for amp-analytics', () => {
903903
let a4a;
904904
let sandbox;
905+
let ampdoc;
905906

906907
beforeEach(() => {
907908
sandbox = sinon.sandbox;
@@ -913,7 +914,8 @@ describe('Google A4A utils', () => {
913914
'type': 'adsense',
914915
'data-amp-slot-index': '4',
915916
});
916-
element.getAmpDoc = () => fixture.doc;
917+
ampdoc = fixture.ampdoc;
918+
element.getAmpDoc = () => ampdoc;
917919
a4a = new MockA4AImpl(element);
918920
});
919921
});
@@ -957,9 +959,7 @@ describe('Google A4A utils', () => {
957959
});
958960

959961
it('should include viewer lastVisibleTime', () => {
960-
const getLastVisibleTime = () => 300;
961-
const viewerStub = sandbox.stub(Services, 'viewerForDoc');
962-
viewerStub.returns({getLastVisibleTime});
962+
sandbox.stub(ampdoc, 'getLastVisibleTime').returns(300);
963963

964964
const vars = getCsiAmpAnalyticsVariables('trigger', a4a, null);
965965
expect(vars['viewerLastVisibleTime']).to.be.a('number');

ads/google/a4a/traffic-experiments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const SINGLE_PASS_EXPERIMENT_IDS = {
5555
*/
5656
export function extractUrlExperimentId(win, element) {
5757
const expParam =
58-
Services.viewerForDoc(element).getParam('exp') ||
58+
Services.ampdoc(element).getParam('exp') ||
5959
parseQueryString(win.location.search)['exp'];
6060
if (!expParam) {
6161
return null;

ads/google/a4a/utils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export function googlePageParameters(a4a, startTime) {
299299
const viewport = Services.viewportForDoc(ampDoc);
300300
const viewportRect = viewport.getRect();
301301
const viewportSize = viewport.getSize();
302-
const visibilityState = Services.viewerForDoc(ampDoc).getVisibilityState();
302+
const visibilityState = ampDoc.getVisibilityState();
303303
return {
304304
'is_amp': a4a.isXhrAllowed()
305305
? AmpAdImplementation.AMP_AD_XHR_TO_IFRAME_OR_AMP
@@ -626,12 +626,11 @@ export function getCsiAmpAnalyticsConfig() {
626626
export function getCsiAmpAnalyticsVariables(analyticsTrigger, a4a, qqid) {
627627
const {win} = a4a;
628628
const ampdoc = a4a.getAmpDoc();
629-
const viewer = Services.viewerForDoc(ampdoc);
630629
const navStart = getNavigationTiming(win, 'navigationStart');
631630
const vars = /** @type {!JsonObject} */ ({
632631
'correlator': getCorrelator(win, ampdoc),
633632
'slotId': a4a.element.getAttribute('data-amp-slot-index'),
634-
'viewerLastVisibleTime': viewer.getLastVisibleTime() - navStart,
633+
'viewerLastVisibleTime': ampdoc.getLastVisibleTime() - navStart,
635634
});
636635
if (qqid) {
637636
vars['qqid'] = qqid;

builtins/amp-pixel.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ export class AmpPixel extends BaseElement {
6565
return;
6666
}
6767
// Trigger, but only when visible.
68-
const viewer = Services.viewerForDoc(this.getAmpDoc());
69-
viewer.whenFirstVisible().then(this.trigger_.bind(this));
68+
this.getAmpDoc()
69+
.whenFirstVisible()
70+
.then(this.trigger_.bind(this));
7071
}
7172

7273
/**

extensions/amp-a4a/0.1/amp-a4a.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export class AmpA4A extends AMP.BaseElement {
387387
this.uiHandler = new AMP.AmpAdUIHandler(this);
388388

389389
const verifier = signatureVerifierFor(this.win);
390-
this.keysetPromise_ = Services.viewerForDoc(this.getAmpDoc())
390+
this.keysetPromise_ = this.getAmpDoc()
391391
.whenFirstVisible()
392392
.then(() => {
393393
this.getSigningServiceNames().forEach(signingServiceName => {
@@ -632,7 +632,7 @@ export class AmpA4A extends AMP.BaseElement {
632632
// - Rendering fails => return false
633633
// - Chain cancelled => don't return; drop error
634634
// - Uncaught error otherwise => don't return; percolate error up
635-
this.adPromise_ = Services.viewerForDoc(this.getAmpDoc())
635+
this.adPromise_ = this.getAmpDoc()
636636
.whenFirstVisible()
637637
.then(() => {
638638
checkStillCurrent();
@@ -997,7 +997,7 @@ export class AmpA4A extends AMP.BaseElement {
997997
this.isRelayoutNeededFlag = true;
998998
this.getResource().layoutCanceled();
999999
// Only Require relayout after page visible
1000-
Services.viewerForDoc(this.getAmpDoc())
1000+
this.getAmpDoc()
10011001
.whenNextVisible()
10021002
.then(() => {
10031003
Services.ownersForDoc(this.getAmpDoc())./*OK*/ requireLayout(

extensions/amp-a4a/0.1/amp-ad-network-base.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import {FailureType, RecoveryModeType} from './amp-ad-type-defs';
18-
import {Services} from '../../../src/services';
1918
import {dev, devAssert} from '../../../src/log';
2019
import {isLayoutSizeDefined} from '../../../src/layout';
2120
import {map} from '../../../src/utils/object';
@@ -150,7 +149,7 @@ export class AmpAdNetworkBase extends AMP.BaseElement {
150149
* @private
151150
*/
152151
sendRequest_() {
153-
this.adResponsePromise_ = Services.viewerForDoc(this.getAmpDoc())
152+
this.adResponsePromise_ = this.getAmpDoc()
154153
.whenFirstVisible()
155154
.then(() => {
156155
const url = this.getRequestUrl();

extensions/amp-a4a/0.1/test/test-amp-a4a.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,7 @@ describe('amp-a4a', () => {
25442544
const a4aElement = createA4aElement(fixture.doc);
25452545
const a4a = new MockA4AImpl(a4aElement);
25462546
a4a.adPromise_ = Promise.resolve();
2547-
a4a.getAmpDoc = () => a4a.win.document;
2547+
a4a.getAmpDoc = () => fixture.ampdoc;
25482548
a4a.getResource = () => {
25492549
return {
25502550
layoutCanceled: () => {},
@@ -2583,7 +2583,7 @@ describe('amp-a4a', () => {
25832583
const a4aElement = createA4aElement(fixture.doc);
25842584
const a4a = new MockA4AImpl(a4aElement);
25852585
a4a.adPromise_ = Promise.resolve();
2586-
a4a.getAmpDoc = () => a4a.win.document;
2586+
a4a.getAmpDoc = () => fixture.ampdoc;
25872587
a4a.getResource = () => {
25882588
return {
25892589
layoutCanceled: () => {},
@@ -2622,7 +2622,7 @@ describe('amp-a4a', () => {
26222622
const a4aElement = createA4aElement(fixture.doc);
26232623
const a4a = new MockA4AImpl(a4aElement);
26242624
a4a.adPromise_ = null;
2625-
a4a.getAmpDoc = () => a4a.win.document;
2625+
a4a.getAmpDoc = () => fixture.ampdoc;
26262626
a4a.getResource = () => {
26272627
return {
26282628
layoutCanceled: () => {},

extensions/amp-access/0.1/amp-access-server-jwt.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ export class AccessServerJwtAdapter {
8989
/** @private @const */
9090
this.clientAdapter_ = new AccessClientAdapter(ampdoc, configJson, context);
9191

92-
/** @private @const {!../../../src/service/viewer-interface.ViewerInterface} */
93-
this.viewer_ = Services.viewerForDoc(ampdoc);
94-
9592
/** @const @private {!../../../src/service/xhr-impl.Xhr} */
9693
this.xhr_ = Services.xhrFor(ampdoc.win);
9794

@@ -116,7 +113,7 @@ export class AccessServerJwtAdapter {
116113
this.isProxyOrigin_ = isProxyOrigin(ampdoc.win.location) || isInExperiment;
117114

118115
const serviceUrlOverride = isInExperiment
119-
? this.viewer_.getParam('serverAccessService')
116+
? ampdoc.getParam('serverAccessService')
120117
: null;
121118

122119
/** @private @const {string} */

extensions/amp-access/0.1/amp-access-server.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ export class AccessServerAdapter {
7272
/** @private @const */
7373
this.clientAdapter_ = new AccessClientAdapter(ampdoc, configJson, context);
7474

75-
/** @private @const {!../../../src/service/viewer-interface.ViewerInterface} */
76-
this.viewer_ = Services.viewerForDoc(ampdoc);
77-
7875
/** @const @protected {!../../../src/service/xhr-impl.Xhr} */
7976
this.xhr_ = Services.xhrFor(ampdoc.win);
8077

@@ -99,7 +96,7 @@ export class AccessServerAdapter {
9996
this.isProxyOrigin_ = isProxyOrigin(ampdoc.win.location) || isInExperiment;
10097

10198
const serviceUrlOverride = isInExperiment
102-
? this.viewer_.getParam('serverAccessService')
99+
? ampdoc.getParam('serverAccessService')
103100
: null;
104101

105102
/** @private @const {string} */

extensions/amp-access/0.1/amp-access.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export class AccessService {
383383
runAuthorization_(opt_disableFallback) {
384384
this.toggleTopClass_('amp-access-loading', true);
385385

386-
const authorizations = this.viewer_.whenFirstVisible().then(() => {
386+
const authorizations = this.ampdoc.whenFirstVisible().then(() => {
387387
return Promise.all(
388388
this.sources_.map(source => this.runOneAuthorization_(source))
389389
);
@@ -559,11 +559,11 @@ export class AccessService {
559559
}
560560
this.reportViewPromise_ = null;
561561
this.ampdoc.whenReady().then(() => {
562-
if (this.viewer_.isVisible()) {
562+
if (this.ampdoc.isVisible()) {
563563
this.reportWhenViewed_(timeToView);
564564
}
565-
this.viewer_.onVisibilityChanged(() => {
566-
if (this.viewer_.isVisible()) {
565+
this.ampdoc.onVisibilityChanged(() => {
566+
if (this.ampdoc.isVisible()) {
567567
this.reportWhenViewed_(timeToView);
568568
}
569569
});
@@ -624,8 +624,8 @@ export class AccessService {
624624
return new Promise((resolve, reject) => {
625625
// 1. Document becomes invisible again: cancel.
626626
unlistenSet.push(
627-
this.viewer_.onVisibilityChanged(() => {
628-
if (!this.viewer_.isVisible()) {
627+
this.ampdoc.onVisibilityChanged(() => {
628+
if (!this.ampdoc.isVisible()) {
629629
reject(cancellation());
630630
}
631631
})

0 commit comments

Comments
 (0)