Skip to content

Commit

Permalink
Separate consent state enum file (#15567)
Browse files Browse the repository at this point in the history
* seperate file

* import

* add consent.js

* dep-check-fix

* fix test
  • Loading branch information
zhouyx authored May 26, 2018
1 parent 3aedf60 commit 8fdc155
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 52 deletions.
5 changes: 3 additions & 2 deletions ads/google/adsense.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import {ADSENSE_RSPV_WHITELISTED_HEIGHT} from './utils';
import {CONSENT_POLICY_STATE} from '../../src/consent-state';
import {camelCaseToDash} from '../../src/string';
import {setStyles} from '../../src/style';
import {user} from '../../src/log';
Expand Down Expand Up @@ -72,12 +73,12 @@ export function adsense(global, data) {
});
const initializer = {};
switch (global.context.initialConsentState) {
case 4: // CONSENT_POLICY_STATE.UNKNOWN
case CONSENT_POLICY_STATE.UNKNOWN:
if (data['npaOnUnknownConsent'] != 'true') {
// Unknown w/o NPA results in no ad request.
return;
}
case 2: // CONSENT_POLICY_STATE.INSUFFICIENT
case CONSENT_POLICY_STATE.INSUFFICIENT:
(global.adsbygoogle = global.adsbygoogle || [])
['requestNonPersonalizedAds'] = true;
break;
Expand Down
5 changes: 3 additions & 2 deletions ads/google/imaVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import {CONSENT_POLICY_STATE} from '../../src/consent-state';
import {ImaPlayerData} from './ima-player-data';
import {camelCaseToTitleCase, px, setStyle, setStyles} from '../../src/style';
import {isObject} from '../../src/types';
Expand Down Expand Up @@ -571,11 +572,11 @@ function onBigPlayTouchMove() {
export function requestAds() {
adsRequested = true;
adRequestFailed = false;
if (consentState == 4) { // UNKNOWN
if (consentState == CONSENT_POLICY_STATE.UNKNOWN) {
// We're unaware of the user's consent state - do not request ads.
imaLoadAllowed = false;
return;
} else if (consentState == 2) { // INSUFFICIENT
} else if (consentState == CONSENT_POLICY_STATE.INSUFFICIENT) {
// User has provided consent state but has not consented to personalized
// ads.
adsRequest.adTagUrl += '&npa=1';
Expand Down
2 changes: 2 additions & 0 deletions build-system/dep-check-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ exports.rules = [
'3p/**->src/3p-frame-messaging.js',
'3p/**->src/observable.js',
'3p/**->src/amp-events.js',
'3p/**->src/consent-state.js',
'3p/polyfills.js->src/polyfills/math-sign.js',
'3p/polyfills.js->src/polyfills/object-assign.js',
'3p/messaging.js->src/event-helper.js',
Expand All @@ -129,6 +130,7 @@ exports.rules = [
'ads/**->src/types.js',
'ads/**->src/string.js',
'ads/**->src/style.js',
'ads/**->src/consent-state.js',
'ads/google/adsense-amp-auto-ads.js->src/experiments.js',
'ads/google/doubleclick.js->src/experiments.js',
// ads/google/a4a doesn't contain 3P ad code and should probably move
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-a4a/0.1/amp-a4a.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {A4AVariableSource} from './a4a-variable-source';
import {
CONSENT_POLICY_STATE, // eslint-disable-line no-unused-vars
getConsentPolicyState,
} from '../../../src/consent-state';
import {Layout, isLayoutSizeDefined} from '../../../src/layout';
import {LayoutPriority} from '../../../src/layout';
Expand All @@ -43,6 +42,7 @@ import {
} from '../../amp-ad/0.1/concurrent-load';
import {getBinaryType} from '../../../src/experiments';
import {getBinaryTypeNumericalCode} from '../../../ads/google/a4a/utils';
import {getConsentPolicyState} from '../../../src/consent';
import {getContextMetadata} from '../../../src/iframe-attributes';
import {getMode} from '../../../src/mode';
import {tryResolve} from '../../../src/utils/promise';
Expand Down
6 changes: 4 additions & 2 deletions extensions/amp-ad/0.1/amp-ad-3p-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {AmpAdUIHandler} from './amp-ad-ui';
import {AmpAdXOriginIframeHandler} from './amp-ad-xorigin-iframe-handler';
import {
CONSENT_POLICY_STATE, // eslint-disable-line no-unused-vars
getConsentPolicySharedData,
getConsentPolicyState,
} from '../../../src/consent-state';
import {
Layout, // eslint-disable-line no-unused-vars
Expand All @@ -40,6 +38,10 @@ import {
incrementLoadingAds,
is3pThrottled,
} from './concurrent-load';
import {
getConsentPolicySharedData,
getConsentPolicyState,
} from '../../../src/consent';
import {getIframe} from '../../../src/3p-frame';
import {
googleLifecycleReporterFactory,
Expand Down
13 changes: 9 additions & 4 deletions extensions/amp-ad/0.1/test/test-amp-ad-3p-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
import '../../../amp-ad/0.1/amp-ad';
import '../../../amp-sticky-ad/1.0/amp-sticky-ad';
import * as adCid from '../../../../src/ad-cid';
import * as consent from '../../../../src/consent-state';
import * as consent from '../../../../src/consent';
import * as lolex from 'lolex';
import {AmpAd3PImpl} from '../amp-ad-3p-impl';
import {CONSENT_POLICY_STATE} from '../../../../src/consent-state';
import {LayoutPriority} from '../../../../src/layout';
import {adConfig} from '../../../../ads/_config';
import {createElementWithAttributes} from '../../../../src/dom';
import {macroTask} from '../../../../testing/yield';
import {stubService} from '../../../../testing/test-helper';
import {user} from '../../../../src/log';

function createAmpAd(win, attachToAmpdoc = false, ampdoc) {
const ampAdElement = createElementWithAttributes(win.document, 'amp-ad', {
Expand Down Expand Up @@ -140,7 +142,7 @@ describes.realWin('amp-ad-3p-impl', {
it('should propagate consent state to ad iframe', () => {
ad3p.element.setAttribute('data-block-on-consent', '');
sandbox.stub(consent, 'getConsentPolicyState')
.resolves(consent.CONSENT_POLICY_STATE.SUFFICIENT);
.resolves(CONSENT_POLICY_STATE.SUFFICIENT);
sandbox.stub(consent, 'getConsentPolicySharedData')
.resolves({a: 1, b: 2});

Expand All @@ -151,7 +153,7 @@ describes.realWin('amp-ad-3p-impl', {
expect(data).to.be.ok;
expect(data._context).to.be.ok;
expect(data._context.initialConsentState)
.to.equal(consent.CONSENT_POLICY_STATE.SUFFICIENT);
.to.equal(CONSENT_POLICY_STATE.SUFFICIENT);
expect(data._context.consentSharedData)
.to.deep.equal({a: 1, b: 2});
});
Expand Down Expand Up @@ -237,6 +239,7 @@ describes.realWin('amp-ad-3p-impl', {
win.document.head.appendChild(meta);
ad3p.config.remoteHTMLDisabled = true;
ad3p.onLayoutMeasure();
sandbox.stub(user(), 'error');
return ad3p.layoutCallback().then(() => {
expect(win.document.querySelector('iframe[src="' +
'http://ads.localhost:9876/dist.3p/current/frame.max.html"]'))
Expand Down Expand Up @@ -300,7 +303,9 @@ describes.realWin('amp-ad-3p-impl', {
win.document.head.appendChild(meta);
ad3p.config.remoteHTMLDisabled = true;
ad3p.buildCallback();
ad3p.preconnectCallback();
allowConsoleError(() => {
ad3p.preconnectCallback();
});
return whenFirstVisible.then(() => {
expect(Array.from(win.document.querySelectorAll('link[rel=preload]'))
.some(link => link.href ==
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-ima-video/0.1/amp-ima-video.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
removeElement,
} from '../../../src/dom';
import {dict} from '../../../src/utils/object';
import {getConsentPolicyState} from '../../../src/consent-state';
import {getConsentPolicyState} from '../../../src/consent';
import {
getData,
listen,
Expand Down
40 changes: 1 addition & 39 deletions src/consent-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Services} from './services';
// This file will be imported by 3P scripts.

/**
* Possible consent policy state to proceed with.
Expand All @@ -27,41 +27,3 @@ export const CONSENT_POLICY_STATE = {
UNKNOWN_NOT_REQUIRED: 3,
UNKNOWN: 4,
};

// TODO(@zhouyx): Move following functions to a different file

/**
* Returns a promise that resolve when all consent state the policy wait
* for resolve. Or if consent service is not available.
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @param {string} policyId
* @return {!Promise<?CONSENT_POLICY_STATE>}
*/
export function getConsentPolicyState(ampdoc, policyId) {
return Services.consentPolicyServiceForDocOrNull(ampdoc)
.then(consentPolicy => {
if (!consentPolicy) {
return null;
}
return consentPolicy.whenPolicyResolved(
/** @type {string} */ (policyId));
});
}

/**
* Returns a promise that resolves to a sharedData retrieved from consent
* remote endpoint.
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @param {string} policyId
* @return {!Promise<?Object>}
*/
export function getConsentPolicySharedData(ampdoc, policyId) {
return Services.consentPolicyServiceForDocOrNull(ampdoc)
.then(consentPolicy => {
if (!consentPolicy) {
return null;
}
return consentPolicy.getMergedSharedData(
/** @type {string} */ (policyId));
});
}
56 changes: 56 additions & 0 deletions src/consent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright 2018 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 {
CONSENT_POLICY_STATE, // eslint-disable-line no-unused-vars
} from './consent-state';
import {Services} from './services';

/**
* Returns a promise that resolve when all consent state the policy wait
* for resolve. Or if consent service is not available.
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @param {string} policyId
* @return {!Promise<?CONSENT_POLICY_STATE>}
*/
export function getConsentPolicyState(ampdoc, policyId) {
return Services.consentPolicyServiceForDocOrNull(ampdoc)
.then(consentPolicy => {
if (!consentPolicy) {
return null;
}
return consentPolicy.whenPolicyResolved(
/** @type {string} */ (policyId));
});
}

/**
* Returns a promise that resolves to a sharedData retrieved from consent
* remote endpoint.
* @param {!./service/ampdoc-impl.AmpDoc} ampdoc
* @param {string} policyId
* @return {!Promise<?Object>}
*/
export function getConsentPolicySharedData(ampdoc, policyId) {
return Services.consentPolicyServiceForDocOrNull(ampdoc)
.then(consentPolicy => {
if (!consentPolicy) {
return null;
}
return consentPolicy.getMergedSharedData(
/** @type {string} */ (policyId));
});
}
1 change: 0 additions & 1 deletion src/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import * as dom from './dom';
import {AmpEvents} from './amp-events';

import {CommonSignals} from './common-signals';
import {ElementStub} from './element-stub';
import {
Expand Down

0 comments on commit 8fdc155

Please sign in to comment.