Skip to content

Commit

Permalink
Fix iframe __tcfapi arguments (prebid#6058)
Browse files Browse the repository at this point in the history
* Fix __tcfapi declaration in iframe

* Add tests for cmp declaration in iframe
  • Loading branch information
polo2ro authored Dec 7, 2020
1 parent dbd0d84 commit 93f393a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
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

0 comments on commit 93f393a

Please sign in to comment.