-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Prebid core: add support for asynchronous access to consent data #8071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import {isStr, timestamp} from './utils.js'; | ||
|
||
export class ConsentHandler { | ||
#enabled; | ||
#data; | ||
#promise; | ||
#resolve; | ||
#ready; | ||
generatedTime; | ||
|
||
constructor() { | ||
this.reset(); | ||
} | ||
|
||
/** | ||
* reset this handler (mainly for tests) | ||
*/ | ||
reset() { | ||
this.#promise = new Promise((resolve) => { | ||
this.#resolve = (data) => { | ||
this.#ready = true; | ||
this.#data = data; | ||
resolve(data); | ||
}; | ||
}); | ||
this.#enabled = false; | ||
this.#data = null; | ||
this.#ready = false; | ||
this.generatedTime = null; | ||
} | ||
|
||
/** | ||
* Enable this consent handler. This should be called by the relevant consent management module | ||
* on initialization. | ||
*/ | ||
enable() { | ||
this.#enabled = true; | ||
} | ||
|
||
/** | ||
* @returns {boolean} true if the related consent management module is enabled. | ||
*/ | ||
get enabled() { | ||
return this.#enabled; | ||
} | ||
|
||
/** | ||
* @returns {boolean} true if consent data has been resolved (it may be `null` if the resolution failed). | ||
*/ | ||
get ready() { | ||
return this.#ready; | ||
} | ||
|
||
/** | ||
* @returns a promise than resolves to the consent data, or null if no consent data is available | ||
*/ | ||
get promise() { | ||
if (!this.#enabled) { | ||
this.#resolve(null); | ||
} | ||
return this.#promise; | ||
} | ||
|
||
setConsentData(data, time = timestamp()) { | ||
this.generatedTime = time; | ||
this.#resolve(data); | ||
} | ||
|
||
getConsentData() { | ||
return this.#data; | ||
} | ||
} | ||
|
||
export class UspConsentHandler extends ConsentHandler { | ||
getConsentMeta() { | ||
const consentData = this.getConsentData(); | ||
if (consentData && this.generatedTime) { | ||
return { | ||
usp: consentData, | ||
generatedAt: this.generatedTime | ||
}; | ||
} | ||
} | ||
} | ||
|
||
export class GdprConsentHandler extends ConsentHandler { | ||
getConsentMeta() { | ||
const consentData = this.getConsentData(); | ||
if (consentData && consentData.vendorData && this.generatedTime) { | ||
return { | ||
gdprApplies: consentData.gdprApplies, | ||
consentStringSize: (isStr(consentData.vendorData.tcString)) ? consentData.vendorData.tcString.length : 0, | ||
generatedAt: this.generatedTime, | ||
apiVersion: consentData.apiVersion | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {ConsentHandler} from '../../../../src/consentHandler.js'; | ||
|
||
describe('Consent data handler', () => { | ||
let handler; | ||
beforeEach(() => { | ||
handler = new ConsentHandler(); | ||
}) | ||
|
||
it('should be disabled, return null data on init', () => { | ||
expect(handler.enabled).to.be.false; | ||
expect(handler.getConsentData()).to.equal(null); | ||
}) | ||
|
||
it('should resolve promise to null when disabled', () => { | ||
return handler.promise.then((data) => { | ||
expect(data).to.equal(null); | ||
}); | ||
}); | ||
|
||
it('should return data after setConsentData', () => { | ||
const data = {consent: 'string'}; | ||
handler.enable(); | ||
handler.setConsentData(data); | ||
expect(handler.getConsentData()).to.equal(data); | ||
}); | ||
|
||
it('should resolve .promise to data after setConsentData', (done) => { | ||
let actual = null; | ||
const data = {consent: 'string'}; | ||
handler.enable(); | ||
handler.promise.then((d) => actual = d); | ||
setTimeout(() => { | ||
expect(actual).to.equal(null); | ||
handler.setConsentData(data); | ||
setTimeout(() => { | ||
expect(actual).to.equal(data); | ||
done(); | ||
}) | ||
}) | ||
}); | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would rejecting the promise suit better here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so (but I'm not that sure). My reasoning is that unless you make it so that
data
is nevernull
when the promise resolves, it's just asking the caller to add some complexity, because they will have to check fornull
and treat it as an error anyways. I am not against making everynull
(including, for example, USP timeouts) a rejection, but it also makes sense to me that you would want to have exactly whatgetConsentData
returns, just asynchronously.If someone is interested in whether the module is installed, they could look at
.enabled
.