Skip to content

Commit

Permalink
Move service getters to static class functions (#10460)
Browse files Browse the repository at this point in the history
* move service getters into new Services class

* fix lint

* fix rename error

* fix errors after analytics refactor
  • Loading branch information
William Chou authored Jul 17, 2017
1 parent 69977c6 commit 3b8c24b
Show file tree
Hide file tree
Showing 222 changed files with 1,300 additions and 1,490 deletions.
10 changes: 4 additions & 6 deletions ads/google/a4a/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import {dev} from '../../../src/log';
import {dict} from '../../../src/utils/object';
import {serializeQueryString} from '../../../src/url';
import {getTimingDataSync} from '../../../src/service/variable-source';
import {urlReplacementsForDoc} from '../../../src/services';
import {viewerForDoc} from '../../../src/services';
import {Services} from '../../../src/services';
import {CommonSignals} from '../../../src/common-signals';
import {analyticsForDoc} from '../../../src/services';

/**
* This module provides a fairly crude form of performance monitoring (or
Expand Down Expand Up @@ -174,10 +172,10 @@ export class GoogleAdLifecycleReporter extends BaseLifecycleReporter {
* @private {!../../../src/service/url-replacements-impl.UrlReplacements}
* @const
*/
this.urlReplacer_ = urlReplacementsForDoc(element);
this.urlReplacer_ = Services.urlReplacementsForDoc(element);

/** @const @private {!../../../src/service/viewer-impl.Viewer} */
this.viewer_ = viewerForDoc(element);
this.viewer_ = Services.viewerForDoc(element);
}

/**
Expand Down Expand Up @@ -275,7 +273,7 @@ export class GoogleAdLifecycleReporter extends BaseLifecycleReporter {
* @override
*/
addPingsForVisibility(element) {
analyticsForDoc(element, true).then(analytics => {
Services.analyticsForDoc(element, true).then(analytics => {
const signals = element.signals();
const readyPromise = Promise.race([
signals.whenSignal(CommonSignals.INI_LOAD),
Expand Down
6 changes: 3 additions & 3 deletions ads/google/a4a/test/test-google-ads-a4a-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {ampdocServiceFor} from '../../../../src/services';
import {Services} from '../../../../src/services';
import {installDocService} from '../../../../src/service/ampdoc-impl';
import {
googleAdsIsA4AEnabled,
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('a4a_config', () => {
};
win.document.defaultView = win;
installDocService(win, /* isSingleDoc */ true);
const ampdoc = ampdocServiceFor(win).getAmpDoc();
const ampdoc = Services.ampdocServiceFor(win).getAmpDoc();
events = {};
installDocumentStateService(win);
installPlatformService(win);
Expand Down Expand Up @@ -340,7 +340,7 @@ describe('a4a_config hash param parsing', () => {
};
win.document.defaultView = win;
installDocService(win, /* isSingleDoc */ true);
ampdoc = ampdocServiceFor(win).getAmpDoc();
ampdoc = Services.ampdocServiceFor(win).getAmpDoc();
events = {};
installPlatformService(win);
installDocumentStateService(win);
Expand Down
4 changes: 2 additions & 2 deletions ads/google/a4a/test/test-performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
BaseLifecycleReporter,
} from '../performance';
import {createIframePromise} from '../../../../testing/iframe';
import {viewerForDoc} from '../../../../src/services';
import {Services} from '../../../../src/services';
import * as sinon from 'sinon';

/**
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('GoogleAdLifecycleReporter', () => {
iframe = createIframePromise(false).then(iframeFixture => {
const win = iframeFixture.win;
const doc = iframeFixture.doc;
const viewer = viewerForDoc(doc);
const viewer = Services.viewerForDoc(doc);
const elem = doc.createElement('div');
doc.body.appendChild(elem);
const reporter = new GoogleAdLifecycleReporter(win, elem, 42);
Expand Down
4 changes: 2 additions & 2 deletions ads/google/a4a/test/test-traffic-experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {ampdocServiceFor} from '../../../../src/services';
import {Services} from '../../../../src/services';
import {installDocService} from '../../../../src/service/ampdoc-impl';
import {
addExperimentIdToElement,
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('all-traffic-experiments-tests', () => {
};
win.document.defaultView = win;
installDocService(win, /* isSingleDoc */ true);
const ampdoc = ampdocServiceFor(win).getAmpDoc();
const ampdoc = Services.ampdocServiceFor(win).getAmpDoc();
events = {};
installDocumentStateService(win);
installPlatformService(win);
Expand Down
9 changes: 3 additions & 6 deletions ads/google/a4a/traffic-experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ import {
randomlySelectUnsetExperiments,
} from '../../../src/experiments';
import {dev} from '../../../src/log';
import {
viewerForDoc,
performanceForOrNull,
} from '../../../src/services';
import {Services} from '../../../src/services';
import {parseQueryString} from '../../../src/url';

/** @typedef {{
Expand Down Expand Up @@ -115,7 +112,7 @@ export function googleAdsIsA4AEnabled(win, element, experimentName,
const selectedBranch = getExperimentBranch(win, experimentName);
if (selectedBranch) {
addExperimentIdToElement(selectedBranch, element);
const perf = performanceForOrNull(win);
const perf = Services.performanceForOrNull(win);
if (perf) {
perf.addEnabledExperiment(experimentName + '-' + selectedBranch);
}
Expand Down Expand Up @@ -146,7 +143,7 @@ export function googleAdsIsA4AEnabled(win, element, experimentName,
* @return {?string} experiment extracted from page url.
*/
export function extractUrlExperimentId(win, element) {
const expParam = viewerForDoc(element).getParam('exp') ||
const expParam = Services.viewerForDoc(element).getParam('exp') ||
parseQueryString(win.location.search)['exp'];
if (!expParam) {
return null;
Expand Down
18 changes: 7 additions & 11 deletions ads/google/a4a/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@
* limitations under the License.
*/

import {Services} from '../../../src/services';
import {buildUrl} from './url-builder';
import {makeCorrelator} from '../correlator';
import {isCanary} from '../../../src/experiments';
import {getOrCreateAdCid} from '../../../src/ad-cid';
import {documentInfoForDoc} from '../../../src/services';
import {dev} from '../../../src/log';
import {dict} from '../../../src/utils/object';
import {getMode} from '../../../src/mode';
import {isProxyOrigin, parseUrl} from '../../../src/url';
import {parseJson} from '../../../src/json';
import {
resourcesForDoc,
viewerForDoc,
viewportForDoc,
} from '../../../src/services';
import {domFingerprint} from '../../../src/utils/dom-fingerprint';
import {
isExperimentOn,
Expand Down Expand Up @@ -177,7 +172,7 @@ export function googleBlockParameters(a4a, opt_experimentIds) {
* @return {!Promise<!Object<string,!Array<!Promise<!../../../src/base-element.BaseElement>>>>}
*/
export function groupAmpAdsByType(win, type, groupFn) {
return resourcesForDoc(win.document).getMeasuredResources(win,
return Services.resourcesForDoc(win.document).getMeasuredResources(win,
r => r.element.tagName == 'AMP-AD' &&
r.element.getAttribute('type') == type)
.then(resources => {
Expand All @@ -198,15 +193,15 @@ export function groupAmpAdsByType(win, type, groupFn) {
* @return {!Promise<!Object<string,null|number|string>>}
*/
export function googlePageParameters(win, doc, startTime, output = 'html') {
const referrerPromise = viewerForDoc(doc).getReferrerUrl();
const referrerPromise = Services.viewerForDoc(doc).getReferrerUrl();
return getOrCreateAdCid(doc, 'AMP_ECID_GOOGLE', '_ga')
.then(clientId => referrerPromise.then(referrer => {
const documentInfo = documentInfoForDoc(doc);
const documentInfo = Services.documentInfoForDoc(doc);
// Read by GPT for GA/GPT integration.
win.gaGlobal = win.gaGlobal ||
{cid: clientId, hid: documentInfo.pageViewId};
const screen = win.screen;
const viewport = viewportForDoc(doc);
const viewport = Services.viewportForDoc(doc);
const viewportRect = viewport.getRect();
const viewportSize = viewport.getSize();
return {
Expand Down Expand Up @@ -368,7 +363,8 @@ function elapsedTimeWithCeiling(time, start) {
export function getCorrelator(win, opt_cid, opt_nodeOrDoc) {
if (!win.ampAdPageCorrelator) {
win.ampAdPageCorrelator = makeCorrelator(
opt_cid, documentInfoForDoc(opt_nodeOrDoc || win.document).pageViewId);
opt_cid,
Services.documentInfoForDoc(opt_nodeOrDoc || win.document).pageViewId);
}
return win.ampAdPageCorrelator;
}
Expand Down
10 changes: 4 additions & 6 deletions builtins/amp-pixel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import {BaseElement} from '../src/base-element';
import {dev, user} from '../src/log';
import {dict} from '../src/utils/object';
import {registerElement} from '../src/custom-element';
import {timerFor} from '../src/services';
import {urlReplacementsForDoc} from '../src/services';
import {viewerForDoc} from '../src/services';
import {Services} from '../src/services';
import {createElementWithAttributes} from '../src/dom';

const TAG = 'amp-pixel';
Expand Down Expand Up @@ -61,7 +59,7 @@ export class AmpPixel extends BaseElement {
+ ' Only "no-referrer" is supported');
}
// Trigger, but only when visible.
const viewer = viewerForDoc(this.getAmpDoc());
const viewer = Services.viewerForDoc(this.getAmpDoc());
viewer.whenFirstVisible().then(this.trigger_.bind(this));
}

Expand All @@ -77,12 +75,12 @@ export class AmpPixel extends BaseElement {
}
// Delay(1) provides a rudimentary "idle" signal.
// TODO(dvoytenko): use an improved idle signal when available.
this.triggerPromise_ = timerFor(this.win).promise(1).then(() => {
this.triggerPromise_ = Services.timerFor(this.win).promise(1).then(() => {
const src = this.element.getAttribute('src');
if (!src) {
return;
}
return urlReplacementsForDoc(this.element)
return Services.urlReplacementsForDoc(this.element)
.expandAsync(this.assertSource_(src))
.then(src => {
const pixel = this.referrerPolicy_
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-3q-player/0.1/amp-3q-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import {isObject} from '../../../src/types';
import {listen, getData} from '../../../src/event-helper';
import {VideoEvents} from '../../../src/video-interface';
import {videoManagerForDoc} from '../../../src/services';
import {Services} from '../../../src/services';

/**
* @implements {../../../src/video-interface.VideoInterface}
Expand Down Expand Up @@ -71,7 +71,7 @@ class Amp3QPlayer extends AMP.BaseElement {
});

installVideoManagerForDoc(this.element);
videoManagerForDoc(this.element).register(this);
Services.videoManagerForDoc(this.element).register(this);
}

/** @override */
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-3q-player/0.1/test/test-amp-3q-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import '../amp-3q-player';
import {listenOncePromise} from '../../../../src/event-helper';
import {adopt} from '../../../../src/runtime';
import {timerFor} from '../../../../src/services';
import {Services} from '../../../../src/services';
import {VideoEvents} from '../../../../src/video-interface';
import * as sinon from 'sinon';

Expand All @@ -30,7 +30,7 @@ adopt(window);
describe('amp-3q-player', function() {
this.timeout(10000);
let sandbox;
const timer = timerFor(window);
const timer = Services.timerFor(window);

beforeEach(() => {
sandbox = sinon.sandbox.create();
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-a4a/0.1/a4a-variable-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {urlReplacementsForDoc} from '../../../src/services';
import {Services} from '../../../src/services';
import {
VariableSource,
getNavigationData,
Expand Down Expand Up @@ -76,7 +76,7 @@ export class A4AVariableSource extends VariableSource {
constructor(ampdoc, embedWin) {
super();
/** @private {VariableSource} global variable source for fallback. */
this.globalVariableSource_ = urlReplacementsForDoc(ampdoc)
this.globalVariableSource_ = Services.urlReplacementsForDoc(ampdoc)
.getVariableSource();

/** @private {!Window} */
Expand Down
23 changes: 10 additions & 13 deletions extensions/amp-a4a/0.1/amp-a4a.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import {Services} from '../../../src/services';
import {
verifyHashVersion,
LegacySignatureVerifier,
Expand Down Expand Up @@ -43,10 +44,7 @@ import {isArray, isObject, isEnumValue} from '../../../src/types';
import {some} from '../../../src/utils/promise';
import {base64UrlDecodeToBytes} from '../../../src/utils/base64';
import {utf8Decode} from '../../../src/utils/bytes';
import {viewerForDoc} from '../../../src/services';
import {xhrFor} from '../../../src/services';
import {endsWith} from '../../../src/string';
import {platformFor} from '../../../src/services';
import {isExperimentOn} from '../../../src/experiments';
import {setStyle} from '../../../src/style';
import {assertHttpsUrl} from '../../../src/url';
Expand All @@ -59,7 +57,6 @@ import {
import {
installUrlReplacementsForEmbed,
} from '../../../src/service/url-replacements-impl';
import {extensionsFor} from '../../../src/services';
import {A4AVariableSource} from './a4a-variable-source';
// TODO(tdrl): Temporary. Remove when we migrate to using amp-analytics.
import {getTimingDataAsync} from '../../../src/service/variable-source';
Expand Down Expand Up @@ -289,7 +286,7 @@ export class AmpA4A extends AMP.BaseElement {
* @private {?XORIGIN_MODE}
*/
this.experimentalNonAmpCreativeRenderMethod_ =
platformFor(this.win).isIos() ? XORIGIN_MODE.SAFEFRAME : null;
Services.platformFor(this.win).isIos() ? XORIGIN_MODE.SAFEFRAME : null;

/**
* Gets a notion of current time, in ms. The value is not necessarily
Expand Down Expand Up @@ -572,7 +569,7 @@ export class AmpA4A extends AMP.BaseElement {
// - Rendering fails => return false
// - Chain cancelled => don't return; drop error
// - Uncaught error otherwise => don't return; percolate error up
this.adPromise_ = viewerForDoc(this.getAmpDoc()).whenFirstVisible()
this.adPromise_ = Services.viewerForDoc(this.getAmpDoc()).whenFirstVisible()
.then(() => {
checkStillCurrent();
// See if experiment that delays request until slot is within
Expand Down Expand Up @@ -739,7 +736,7 @@ export class AmpA4A extends AMP.BaseElement {
this.updatePriority(0);
// Load any extensions; do not wait on their promises as this
// is just to prefetch.
const extensions = extensionsFor(this.win);
const extensions = Services.extensionsFor(this.win);
creativeMetaDataDef.customElementExtensions.forEach(
extensionId => extensions.loadExtension(extensionId));
return creativeMetaDataDef;
Expand Down Expand Up @@ -1018,7 +1015,7 @@ export class AmpA4A extends AMP.BaseElement {
this.isVerifiedAmpCreative_ = false;
this.fromResumeCallback = false;
this.experimentalNonAmpCreativeRenderMethod_ =
platformFor(this.win).isIos() ? XORIGIN_MODE.SAFEFRAME : null;
Services.platformFor(this.win).isIos() ? XORIGIN_MODE.SAFEFRAME : null;
if (this.xOriginIframeHandler_) {
this.xOriginIframeHandler_.freeXOriginIframe();
this.xOriginIframeHandler_ = null;
Expand Down Expand Up @@ -1176,7 +1173,7 @@ export class AmpA4A extends AMP.BaseElement {
method: 'GET',
credentials: 'include',
};
return xhrFor(this.win)
return Services.xhrFor(this.win)
.fetch(adUrl, xhrInit)
.catch(unusedReason => {
// If an error occurs, let the ad be rendered via iframe after delay.
Expand Down Expand Up @@ -1214,8 +1211,8 @@ export class AmpA4A extends AMP.BaseElement {
const currServiceName = serviceName;
if (url) {
// Delay request until document is not in a prerender state.
return viewerForDoc(this.getAmpDoc()).whenFirstVisible()
.then(() => xhrFor(this.win).fetchJson(url, {
return Services.viewerForDoc(this.getAmpDoc()).whenFirstVisible()
.then(() => Services.xhrFor(this.win).fetchJson(url, {
mode: 'cors',
method: 'GET',
// Set ampCors false so that __amp_source_origin is not
Expand Down Expand Up @@ -1444,7 +1441,7 @@ export class AmpA4A extends AMP.BaseElement {
renderViaCachedContentIframe_(adUrl) {
this.protectedEmitLifecycleEvent_('renderCrossDomainStart');
return this.iframeRenderHelper_(dict({
'src': xhrFor(this.win).getCorsUrl(this.win, adUrl),
'src': Services.xhrFor(this.win).getCorsUrl(this.win, adUrl),
'name': JSON.stringify(
getContextMetadata(this.win, this.element, this.sentinel)),
}));
Expand Down Expand Up @@ -1599,7 +1596,7 @@ export class AmpA4A extends AMP.BaseElement {
}
iframeWin.document.documentElement.addEventListener('click', event => {
handleClick(event, url => {
viewerForDoc(this.getAmpDoc()).navigateTo(url, 'a4a');
Services.viewerForDoc(this.getAmpDoc()).navigateTo(url, 'a4a');
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-a4a/0.1/legacy-signature-verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {cryptoFor} from '../../../src/services';
import {Services} from '../../../src/services';
import {base64UrlDecodeToBytes} from '../../../src/utils/base64';

/**
Expand All @@ -36,7 +36,7 @@ export class LegacySignatureVerifier {
/** @param {!Window} win */
constructor(win) {
/** @private @const {!../../../src/service/crypto-impl.Crypto} */
this.crypto_ = cryptoFor(win);
this.crypto_ = Services.cryptoFor(win);
}

/**
Expand Down
Loading

0 comments on commit 3b8c24b

Please sign in to comment.