Skip to content

Commit c575ae2

Browse files
mkendall07jsnellbaker
authored andcommitted
fix a bug when the iframe locator is not present on page (#4637)
* fix a bug when the iframe locator is not present on page * clean up
1 parent 01be180 commit c575ae2

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

modules/consentManagementUsp.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,26 @@ function lookupUspConsent(uspSuccess, uspError, hookConfig) {
6363
// - use the USPAPI locator code to see if USP's located in the current window or an ancestor window. This works in friendly or cross domain iframes
6464
// - if USPAPI is not found, the iframe function will call the uspError exit callback to abort the rest of the USPAPI workflow
6565
// - try to call the __uspapi() function directly, otherwise use the postMessage() api
66-
6766
// find the CMP frame/window
68-
let f = window;
69-
let uspapiFrame;
70-
while (!uspapiFrame) {
71-
try {
72-
if (f.frames['__uspapiLocator']) uspapiFrame = f;
73-
} catch (e) { }
74-
if (f === window.top) break;
75-
f = f.parent;
76-
}
77-
78-
if (!uspapiFrame) {
79-
return uspError('USP CMP not found.', hookConfig);
80-
}
8167

8268
try {
8369
// try to call __uspapi directly
84-
uspapiFrame.__uspapi('getUSPData', USPAPI_VERSION, callbackHandler.consentDataCallback);
70+
window.__uspapi('getUSPData', USPAPI_VERSION, callbackHandler.consentDataCallback);
8571
} catch (e) {
8672
// must not have been accessible, try using postMessage() api
73+
let f = window;
74+
let uspapiFrame;
75+
while (!uspapiFrame) {
76+
try {
77+
if (f.frames['__uspapiLocator']) uspapiFrame = f;
78+
} catch (e) { }
79+
if (f === window.top) break;
80+
f = f.parent;
81+
}
82+
83+
if (!uspapiFrame) {
84+
return uspError('USP CMP not found.', hookConfig);
85+
}
8786
callUspApiWhileInIframe('getUSPData', uspapiFrame, callbackHandler.consentDataCallback);
8887
}
8988

test/spec/modules/consentManagementUsp_spec.js

+42
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,48 @@ describe('consentManagement', function () {
245245
}
246246
});
247247

248+
describe('test without iframe locater', function() {
249+
let uspapiStub = sinon.stub();
250+
251+
beforeEach(function () {
252+
didHookReturn = false;
253+
sinon.stub(utils, 'logError');
254+
sinon.stub(utils, 'logWarn');
255+
window.__uspapi = function() {};
256+
});
257+
258+
afterEach(function () {
259+
config.resetConfig();
260+
$$PREBID_GLOBAL$$.requestBids.removeAll();
261+
uspapiStub.restore();
262+
utils.logError.restore();
263+
utils.logWarn.restore();
264+
delete window.__uspapi;
265+
resetConsentData();
266+
});
267+
268+
it('Workflow for normal page withoout iframe locater', function() {
269+
let testConsentData = {
270+
uspString: '1NY'
271+
};
272+
273+
uspapiStub = sinon.stub(window, '__uspapi').callsFake((...args) => {
274+
args[2](testConsentData, true);
275+
});
276+
277+
setConsentConfig(goodConfig);
278+
requestBidsHook(() => { didHookReturn = true; }, {});
279+
280+
let consent = uspDataHandler.getConsentData();
281+
282+
sinon.assert.notCalled(utils.logWarn);
283+
sinon.assert.notCalled(utils.logError);
284+
285+
expect(didHookReturn).to.be.true;
286+
expect(consent).to.equal(testConsentData.uspString);
287+
});
288+
});
289+
248290
describe('USPAPI workflow for normal pages:', function () {
249291
let uspapiStub = sinon.stub();
250292
let ifr = null;

0 commit comments

Comments
 (0)