Skip to content
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

Fix iframe __tcfapi arguments #6058

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions modules/consentManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,51 @@ function lookupIabConsent(cmpSuccess, cmpError, hookConfig) {
function callCmpWhileInIframe(commandName, cmpFrame, moduleCallback) {
let apiName = (cmpVersion === 2) ? '__tcfapi' : '__cmp';

let callId = Math.random() + '';
let callName = `${apiName}Call`;

/* Setup up a __cmp function to do the postMessage and stash the callback.
This function behaves (from the caller's perspective identicially to the in-frame __cmp call */
window[apiName] = function (cmd, arg, callback) {
let callId = Math.random() + '';
let callName = `${apiName}Call`;
let msg = {
[callName]: {
command: cmd,
parameter: arg,
callId: callId
}
};
if (cmpVersion !== 1) msg[callName].version = cmpVersion;
if (cmpVersion === 2) {
window[apiName] = function (cmd, cmpVersion, callback, arg) {
let msg = {
[callName]: {
command: cmd,
version: cmpVersion,
parameter: arg,
callId: callId
}
};

cmpCallbacks[callId] = callback;
cmpFrame.postMessage(msg, '*');
}
cmpCallbacks[callId] = callback;
cmpFrame.postMessage(msg, '*');
}

/** when we get the return message, call the stashed callback */
window.addEventListener('message', readPostMessageResponse, false);
/** when we get the return message, call the stashed callback */
window.addEventListener('message', readPostMessageResponse, false);

// call CMP
window[apiName](commandName, undefined, moduleCallback);
// call CMP
window[apiName](commandName, cmpVersion, moduleCallback);
} else {
window[apiName] = function (cmd, arg, callback) {
let msg = {
[callName]: {
command: cmd,
parameter: arg,
callId: callId
}
};

cmpCallbacks[callId] = callback;
cmpFrame.postMessage(msg, '*');
}

/** when we get the return message, call the stashed callback */
window.addEventListener('message', readPostMessageResponse, false);

// call CMP
window[apiName](commandName, undefined, moduleCallback);
}

function readPostMessageResponse(event) {
let cmpDataPkgName = `${apiName}Return`;
Expand Down
18 changes: 18 additions & 0 deletions test/spec/modules/consentManagement_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,15 @@ describe('consentManagement', function () {
// from CMP window postMessage listener.
testIFramedPage('with/JSON response', false, 'encoded_consent_data_via_post_message', 1);
testIFramedPage('with/String response', true, 'encoded_consent_data_via_post_message', 1);

it('should contain correct V1 CMP definition', (done) => {
setConsentConfig(goodConfigWithAllowAuction);
requestBidsHook(() => {
const nbArguments = window.__cmp.toString().split('\n')[0].split(', ').length;
expect(nbArguments).to.equal(3);
done();
}, {});
});
});

describe('v2 CMP workflow for iframe pages:', function () {
Expand All @@ -562,6 +571,15 @@ describe('consentManagement', function () {

testIFramedPage('with/JSON response', false, 'abc12345234', 2);
testIFramedPage('with/String response', true, 'abc12345234', 2);

it('should contain correct v2 CMP definition', (done) => {
setConsentConfig(goodConfigWithAllowAuction);
requestBidsHook(() => {
const nbArguments = window.__tcfapi.toString().split('\n')[0].split(', ').length;
expect(nbArguments).to.equal(4);
done();
}, {});
});
});
});

Expand Down