Skip to content

Commit 5a4c3d8

Browse files
author
Micajuine Ho
committed
no caching for now
1 parent 38b8176 commit 5a4c3d8

File tree

9 files changed

+175
-339
lines changed

9 files changed

+175
-339
lines changed

extensions/amp-consent/0.1/amp-consent.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ export class AmpConsent extends AMP.BaseElement {
420420
*/
421421
init_() {
422422
this.passSharedData_();
423+
this.setGdprApplies();
423424
this.syncRemoteConsentState_();
424425

425426
this.getConsentRequiredPromise_()
@@ -482,6 +483,28 @@ export class AmpConsent extends AMP.BaseElement {
482483
this.consentStateManager_.setConsentInstanceSharedData(sharedDataPromise);
483484
}
484485

486+
/**
487+
* Create and set gdprApplies promise form consent manager.
488+
* Default value to `consentRequired`, if no `gdprApplies`
489+
* value is provided.
490+
*
491+
*/
492+
setGdprApplies() {
493+
const responsePromise = this.getConsentRemote_();
494+
const gdprAppliesPromise = responsePromise.then((response) => {
495+
if (
496+
!response ||
497+
response['gdprApplies'] === undefined ||
498+
typeof response['gdprApplies'] !== 'boolean'
499+
) {
500+
return this.getConsentRequiredPromise_();
501+
}
502+
return response['gdprApplies'];
503+
});
504+
505+
this.consentStateManager_.setConsentInstanceGdprApplies(gdprAppliesPromise);
506+
}
507+
485508
/**
486509
* Clear cache for server side decision and then sync.
487510
*/
@@ -504,34 +527,24 @@ export class AmpConsent extends AMP.BaseElement {
504527
) {
505528
this.updateCacheIfNotNull_(
506529
response['consentStateValue'],
507-
response['consentString'] || undefined,
508-
response['gdprApplies'] !== undefined
509-
? response['gdprApplies']
510-
: !!response['consentRequired']
530+
response['consentString'] || undefined
511531
);
512532
}
513533
});
514534
}
515535

516536
/**
517537
* Sync with local storage if consentRequired is true.
518-
* Treat all three values as a grouping, only update cache
519-
* if responseStateValue is not null
520538
* @param {string=} responseStateValue
521539
* @param {string=} responseConsentString
522-
* @param {boolean=} responseGdprApplies
523540
*/
524-
updateCacheIfNotNull_(
525-
responseStateValue,
526-
responseConsentString,
527-
responseGdprApplies
528-
) {
541+
updateCacheIfNotNull_(responseStateValue, responseConsentString) {
529542
const consentStateValue = convertEnumValueToState(responseStateValue);
543+
// consentStateValue and consentString are treated as a pair that will update together
530544
if (consentStateValue !== null) {
531545
this.consentStateManager_.updateConsentInstanceState(
532546
consentStateValue,
533-
responseConsentString,
534-
responseGdprApplies
547+
responseConsentString
535548
);
536549
}
537550
}

extensions/amp-consent/0.1/consent-info.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ import {isEnumValue, isObject} from '../../../src/types';
2323
* STATE: Set when user accept or reject consent.
2424
* STRING: Set when a consent string is used to store more granular consent info
2525
* on vendors.
26-
* GDPR: Set when the gdprApplies value of TCF v2 is sent from vendors.
2726
* DITRYBIT: Set when the stored consent info need to be revoked next time.
2827
* @enum {string}
2928
*/
3029
export const STORAGE_KEY = {
3130
STATE: 's',
3231
STRING: 'r',
33-
GDPR: 'g',
3432
IS_DIRTY: 'd',
3533
};
3634

@@ -52,7 +50,6 @@ export const CONSENT_ITEM_STATE = {
5250
* @typedef {{
5351
* consentState: CONSENT_ITEM_STATE,
5452
* consentString: (string|undefined),
55-
* gdprApplies: (boolean|undefined)
5653
* isDirty: (boolean|undefined),
5754
* }}
5855
*/
@@ -68,7 +65,6 @@ export function getStoredConsentInfo(value) {
6865
return constructConsentInfo(
6966
CONSENT_ITEM_STATE.UNKNOWN,
7067
undefined,
71-
undefined,
7268
undefined
7369
);
7470
}
@@ -84,9 +80,6 @@ export function getStoredConsentInfo(value) {
8480
return constructConsentInfo(
8581
consentState,
8682
value[STORAGE_KEY.STRING],
87-
value[STORAGE_KEY.GDPR] !== undefined
88-
? value[STORAGE_KEY.GDPR] === 1
89-
: undefined,
9083
value[STORAGE_KEY.IS_DIRTY] && value[STORAGE_KEY.IS_DIRTY] === 1
9184
);
9285
}
@@ -148,11 +141,6 @@ export function composeStoreValue(consentInfo) {
148141
obj[STORAGE_KEY.STRING] = consentInfo['consentString'];
149142
}
150143

151-
// g:(0|1|undefined)
152-
if (consentInfo['gdprApplies'] !== undefined) {
153-
obj[STORAGE_KEY.GDPR] = consentInfo['gdprApplies'] ? 1 : 0;
154-
}
155-
156144
if (consentInfo['isDirty'] === true) {
157145
obj[STORAGE_KEY.IS_DIRTY] = 1;
158146
}
@@ -197,14 +185,13 @@ export function isConsentInfoStoredValueSame(infoA, infoB, opt_isDirty) {
197185
calculateLegacyStateValue(infoB['consentState']);
198186
const stringEqual =
199187
(infoA['consentString'] || '') === (infoB['consentString'] || '');
200-
const gdprAppliesEqual = infoA['gdprApplies'] === infoB['gdprApplies'];
201188
let isDirtyEqual;
202189
if (opt_isDirty) {
203190
isDirtyEqual = !!infoA['isDirty'] === !!opt_isDirty;
204191
} else {
205192
isDirtyEqual = !!infoA['isDirty'] === !!infoB['isDirty'];
206193
}
207-
return stateEqual && stringEqual && gdprAppliesEqual && isDirtyEqual;
194+
return stateEqual && stringEqual && isDirtyEqual;
208195
}
209196
return false;
210197
}
@@ -216,27 +203,24 @@ export function isConsentInfoStoredValueSame(infoA, infoB, opt_isDirty) {
216203
*/
217204
function getLegacyStoredConsentInfo(value) {
218205
const state = convertValueToState(value);
219-
return constructConsentInfo(state, undefined, undefined, undefined);
206+
return constructConsentInfo(state, undefined, undefined);
220207
}
221208

222209
/**
223210
* Construct the consentInfo object from values
224211
* @param {CONSENT_ITEM_STATE} consentState
225212
* @param {string=} opt_consentString
226-
* @param {boolean=} opt_gdprApplies
227213
* @param {boolean=} opt_isDirty
228214
* @return {!ConsentInfoDef}
229215
*/
230216
export function constructConsentInfo(
231217
consentState,
232218
opt_consentString,
233-
opt_gdprApplies,
234219
opt_isDirty
235220
) {
236221
return {
237222
'consentState': consentState,
238223
'consentString': opt_consentString,
239-
'gdprApplies': opt_gdprApplies,
240224
'isDirty': opt_isDirty,
241225
};
242226
}

extensions/amp-consent/0.1/consent-policy-manager.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ export class ConsentPolicyManager {
7676

7777
/** @private {?string} */
7878
this.consentString_ = null;
79-
80-
/** @private {?string} */
81-
this.gdprApplies_ = null;
8279
}
8380

8481
/**
@@ -182,14 +179,9 @@ export class ConsentPolicyManager {
182179
consentStateChangeHandler_(info) {
183180
const state = info['consentState'];
184181
const consentStr = info['consentString'];
185-
const gdprApplies = info['gdprApplies'];
186-
const {
187-
consentString_: prevConsentStr,
188-
gdprApplies_: prevGdprApplies,
189-
} = this;
182+
const {consentString_: prevConsentStr} = this;
190183

191184
this.consentString_ = consentStr;
192-
this.gdprApplies_ = gdprApplies;
193185
if (state === CONSENT_ITEM_STATE.UNKNOWN) {
194186
// consent state has not been resolved yet.
195187
return;
@@ -208,9 +200,8 @@ export class ConsentPolicyManager {
208200
if (this.consentState_ === null) {
209201
this.consentState_ = CONSENT_ITEM_STATE.UNKNOWN;
210202
}
211-
// consentString and gdprApplies doesn't change with dismiss action
203+
// consentString doesn't change with dismiss action
212204
this.consentString_ = prevConsentStr;
213-
this.gdprApplies_ = prevGdprApplies;
214205
} else {
215206
this.consentState_ = state;
216207
}
@@ -279,26 +270,28 @@ export class ConsentPolicyManager {
279270
}
280271

281272
/**
282-
* Get the consent string value of a policy. Return a promise that resolves
283-
* when the policy resolves.
273+
* Get gdprApplies value of a policy.
274+
*
284275
* @param {string} policyId
285-
* @return {!Promise<?string>}
276+
* @return {!Promise<Object>}
286277
*/
287-
getConsentStringInfo(policyId) {
288-
return this.whenPolicyResolved(policyId).then(() => {
289-
return this.consentString_;
290-
});
278+
getGdprAppliesInfo(policyId) {
279+
return this.whenPolicyResolved(policyId)
280+
.then(() => this.ConsentStateManagerPromise_)
281+
.then((manager) => {
282+
return manager.getConsentInstanceGdprApplies();
283+
});
291284
}
292285

293286
/**
294-
* Get the gdprApplies value of a policy. Return a promise that resolves
287+
* Get the consent string value of a policy. Return a promise that resolves
295288
* when the policy resolves.
296289
* @param {string} policyId
297-
* @return {!Promise<?boolean>}
290+
* @return {!Promise<?string>}
298291
*/
299-
getGdprAppliesInfo(policyId) {
292+
getConsentStringInfo(policyId) {
300293
return this.whenPolicyResolved(policyId).then(() => {
301-
return this.gdprApplies_;
294+
return this.consentString_;
302295
});
303296
}
304297

extensions/amp-consent/0.1/consent-state-manager.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,16 @@ export class ConsentStateManager {
9494
*
9595
* @param {CONSENT_ITEM_STATE} state
9696
* @param {string=} consentStr
97-
* @param {boolean=} gdprApplies
9897
*/
99-
updateConsentInstanceState(state, consentStr, gdprApplies) {
98+
updateConsentInstanceState(state, consentStr) {
10099
if (!this.instance_) {
101100
dev().error(TAG, 'instance not registered');
102101
return;
103102
}
104-
this.instance_.update(state, consentStr, gdprApplies, false);
103+
this.instance_.update(state, consentStr, false);
105104

106105
if (this.consentChangeHandler_) {
107-
this.consentChangeHandler_(
108-
constructConsentInfo(state, consentStr, gdprApplies)
109-
);
106+
this.consentChangeHandler_(constructConsentInfo(state, consentStr));
110107
}
111108
}
112109

@@ -165,6 +162,17 @@ export class ConsentStateManager {
165162
this.instance_.sharedDataPromise = sharedDataPromise;
166163
}
167164

165+
/**
166+
* Sets a promise which resolves to a shareData object that is to be returned
167+
* from the remote endpoint.
168+
*
169+
* @param {!Promise<boolean>} gdprAppliesPromise
170+
*/
171+
setConsentInstanceGdprApplies(gdprAppliesPromise) {
172+
devAssert(this.instance_, '%s: cannot find the instance', TAG);
173+
this.instance_.gdprAppliesPromise = gdprAppliesPromise;
174+
}
175+
168176
/**
169177
* Sets the dirty bit so current consent info won't be used for
170178
* decision making on next visit
@@ -184,6 +192,16 @@ export class ConsentStateManager {
184192
return this.instance_.sharedDataPromise;
185193
}
186194

195+
/**
196+
* Returns a promise that resolves to a gdprApplies value
197+
*
198+
* @return {?Promise<?Object>}
199+
*/
200+
getConsentInstanceGdprApplies() {
201+
devAssert(this.instance_, '%s: cannot find the instance', TAG);
202+
return this.instance_.gdprAppliesPromise;
203+
}
204+
187205
/**
188206
* Returns a promise that's resolved when consent instance is ready.
189207
* @return {*} TODO(#23582): Specify return type
@@ -230,6 +248,9 @@ export class ConsentInstance {
230248
/** @public {?Promise<Object>} */
231249
this.sharedDataPromise = null;
232250

251+
/** @public {?Promise<boolean>} */
252+
this.gdprAppliesPromise = null;
253+
233254
/** @private {Promise<!../../../src/service/storage-impl.Storage>} */
234255
this.storagePromise_ = Services.storageForDoc(ampdoc);
235256

@@ -267,37 +288,28 @@ export class ConsentInstance {
267288
// No need to update with dirtyBit
268289
return;
269290
}
270-
this.update(
271-
info['consentState'],
272-
info['consentString'],
273-
info['gdprApplies'],
274-
true
275-
);
291+
this.update(info['consentState'], info['consentString'], true);
276292
});
277293
}
278294

279295
/**
280296
* Update the local consent state list
281297
* @param {!CONSENT_ITEM_STATE} state
282298
* @param {string=} consentString
283-
* @param {boolean=} gdprApplies
284299
* @param {boolean=} opt_systemUpdate
285300
*/
286-
update(state, consentString, gdprApplies, opt_systemUpdate) {
301+
update(state, consentString, opt_systemUpdate) {
287302
const localState =
288303
this.localConsentInfo_ && this.localConsentInfo_['consentState'];
289304
const calculatedState = recalculateConsentStateValue(state, localState);
290305

291306
if (state === CONSENT_ITEM_STATE.DISMISSED) {
292307
const localConsentStr =
293308
this.localConsentInfo_ && this.localConsentInfo_['consentString'];
294-
const localGdprApplies =
295-
this.localConsentInfo_ && this.localConsentInfo_['gdprApplies'];
296309
// If state is dismissed, use the old consent string.
297310
this.localConsentInfo_ = constructConsentInfo(
298311
calculatedState,
299-
localConsentStr,
300-
localGdprApplies
312+
localConsentStr
301313
);
302314
return;
303315
}
@@ -309,23 +321,20 @@ export class ConsentInstance {
309321
this.localConsentInfo_ = constructConsentInfo(
310322
calculatedState,
311323
consentString,
312-
gdprApplies,
313324
true
314325
);
315326
} else {
316327
// Any user update makes the current state valid, thus remove dirtyBit
317328
// from localConsentInfo_
318329
this.localConsentInfo_ = constructConsentInfo(
319330
calculatedState,
320-
consentString,
321-
gdprApplies
331+
consentString
322332
);
323333
}
324334

325335
const newConsentInfo = constructConsentInfo(
326336
calculatedState,
327337
consentString,
328-
gdprApplies,
329338
this.hasDirtyBitNext_
330339
);
331340

@@ -476,9 +485,6 @@ export class ConsentInstance {
476485
if (consentInfo['consentString']) {
477486
request['consentString'] = consentInfo['consentString'];
478487
}
479-
if (consentInfo['gdprApplies'] !== undefined) {
480-
request['gdprApplies'] = consentInfo['gdprApplies'];
481-
}
482488
const init = {
483489
credentials: 'include',
484490
method: 'POST',

0 commit comments

Comments
 (0)