forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsent.js
117 lines (108 loc) · 3.17 KB
/
consent.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
/**
* 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';
import {user} from './log';
/**
* Returns a promise that resolve when all consent state the policy wait
* for resolve. Or if consent service is not available.
* @param {!Element|!ShadowRoot} element
* @param {string=} policyId
* @return {!Promise<?CONSENT_POLICY_STATE>}
*/
export function getConsentPolicyState(element, policyId = 'default') {
return Services.consentPolicyServiceForDocOrNull(element).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 {!Element|!ShadowRoot} element
* @param {string} policyId
* @return {!Promise<?Object>}
*/
export function getConsentPolicySharedData(element, policyId) {
return Services.consentPolicyServiceForDocOrNull(element).then(
consentPolicy => {
if (!consentPolicy) {
return null;
}
return consentPolicy.getMergedSharedData(
/** @type {string} */ (policyId)
);
}
);
}
/**
* TODO(zhouyx): Combine with getConsentPolicyState and return a consentInfo
* object.
* @param {!Element|!ShadowRoot} element
* @param {string} policyId
* @return {!Promise<string>}
*/
export function getConsentPolicyInfo(element, policyId) {
// Return the stored consent string.
return Services.consentPolicyServiceForDocOrNull(element).then(
consentPolicy => {
if (!consentPolicy) {
return null;
}
return consentPolicy.getConsentStringInfo(
/** @type {string} */ (policyId)
);
}
);
}
/**
* Determine if an element needs to be blocked by consent based on metaTags.
* @param {*} element
* @return {boolean}
*/
export function shouldBlockOnConsentByMeta(element) {
const ampdoc = element.getAmpDoc();
let content = Services.documentInfoForDoc(ampdoc).metaTags[
'amp-consent-blocking'
];
if (!content) {
return false;
}
// validator enforce uniqueness of <meta name='amp-consent-blocking'>
// content will not be an array.
if (typeof content !== 'string') {
user().error(
'CONSENT',
'Invalid amp-consent-blocking value, ignore meta tag'
);
return false;
}
// Handles whitespace
content = content
.toUpperCase()
.replace(/\s/g, '')
.split(',');
if (content.includes(element.tagName)) {
return true;
}
return false;
}