Skip to content

Commit

Permalink
Discovery Bid Adapter : fix window.top bug (#11511)
Browse files Browse the repository at this point in the history
* Discovery Bid Adapter: Fix window.top bug

* Discovery Bid Adapter : fix window.top bug

---------

Co-authored-by: lvhuixin <[email protected]>
  • Loading branch information
lhxx121 and lvhuixin authored Jun 13, 2024
1 parent e8be790 commit 7ded2b7
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 22 deletions.
45 changes: 24 additions & 21 deletions modules/discoveryBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as utils from '../src/utils.js';
import { getStorageManager } from '../src/storageManager.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, NATIVE } from '../src/mediaTypes.js';
import { getHLen, getHC, getDM } from '../src/fpd/navigator.js';

/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
Expand Down Expand Up @@ -455,11 +456,31 @@ function getParam(validBidRequests, bidderRequest) {
const referer = utils.deepAccess(bidderRequest, 'refererInfo.ref');
const firstPartyData = bidderRequest.ortb2;
const tpData = utils.deepAccess(bidderRequest, 'ortb2.user.data') || undefined;
const topWindow = window.top;
const title = getPageTitle();
const desc = getPageDescription();
const keywords = getPageKeywords();

let ext = {};
try {
ext = {
eids,
firstPartyData,
ssppid: storage.getCookie(COOKIE_KEY_SSPPID) || undefined,
pmguid: getPmgUID(),
tpData,
utm: storage.getCookie(UTM_KEY),
page: {
title: title ? title.slice(0, 100) : undefined,
desc: desc ? desc.slice(0, 300) : undefined,
keywords: keywords ? keywords.slice(0, 100) : undefined,
hLen: getHLen(),
},
device: {
nbw: getConnectionDownLink(),
hc: getHC(),
dm: getDM()
}
}
} catch (error) {}
try {
buildUTMTagData(page);
} catch (error) { }
Expand All @@ -480,25 +501,7 @@ function getParam(validBidRequests, bidderRequest) {
ua: navigator.userAgent,
language: /en/.test(navigator.language) ? 'en' : navigator.language,
},
ext: {
eids,
firstPartyData,
ssppid: storage.getCookie(COOKIE_KEY_SSPPID) || undefined,
pmguid: getPmgUID(),
tpData,
utm: storage.getCookie(UTM_KEY),
page: {
title: title ? title.slice(0, 100) : undefined,
desc: desc ? desc.slice(0, 300) : undefined,
keywords: keywords ? keywords.slice(0, 100) : undefined,
hLen: topWindow.history?.length || undefined,
},
device: {
nbw: getConnectionDownLink(),
hc: topWindow.navigator?.hardwareConcurrency || undefined,
dm: topWindow.navigator?.deviceMemory || undefined,
}
},
ext,
user: {
buyeruid: storage.getCookie(COOKIE_KEY_MGUID) || undefined,
id: sharedid || pubcid,
Expand Down
29 changes: 29 additions & 0 deletions src/fpd/navigator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function getHLen(win = window) {
let hLen;
try {
hLen = win.top.history.length;
} catch (error) {
hLen = undefined;
}
return hLen;
}

export function getHC(win = window) {
let hc;
try {
hc = win.top.navigator.hardwareConcurrency;
} catch (error) {
hc = undefined;
}
return hc;
}

export function getDM(win = window) {
let dm;
try {
dm = win.top.navigator.deviceMemory;
} catch (error) {
dm = undefined;
}
return dm;
};
72 changes: 71 additions & 1 deletion test/spec/modules/discoveryBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
THIRD_PARTY_COOKIE_ORIGIN,
COOKIE_KEY_MGUID,
getCurrentTimeToUTCString,
buildUTMTagData
buildUTMTagData,
} from 'modules/discoveryBidAdapter.js';
import * as utils from 'src/utils.js';
import { getHLen, getHC, getDM } from '../../../src/fpd/navigator.js';

describe('discovery:BidAdapterTests', function () {
let sandbox;
Expand Down Expand Up @@ -633,5 +634,74 @@ describe('discovery Bid Adapter Tests', function () {
expect(storage.setCookie.notCalled).to.be.true;
});
});
describe('getHLen', () => {
it('should return the correct length of history when accessible', () => {
const mockWindow = {
top: {
history: {
length: 3
}
}
};
const result = getHLen(mockWindow);
expect(result).to.equal(3);
});

it('should return undefined when accessing win.top.history.length throws an error', () => {
const mockWindow = {
get top() {
throw new Error('Access denied');
}
};
const result = getHLen(mockWindow);
expect(result).be.undefined;
});
});

describe('getHC', () => {
it('should return the correct value of hardwareConcurrency when accessible', () => {
const mockWindow = {
top: {
navigator: {
hardwareConcurrency: 4
}
}
};
const result = getHC(mockWindow);
expect(result).to.equal(4);
});
it('should return undefined when accessing win.top.navigator.hardwareConcurrency throws an error', () => {
const mockWindow = {
get top() {
throw new Error('Access denied');
}
};
const result = getHC(mockWindow);
expect(result).be.undefined;
});
});

describe('getDM', () => {
it('should return the correct value of deviceMemory when accessible', () => {
const mockWindow = {
top: {
navigator: {
deviceMemory: 4
}
}
};
const result = getDM(mockWindow);
expect(result).to.equal(4);
});
it('should return undefined when accessing win.top.navigator.deviceMemory throws an error', () => {
const mockWindow = {
get top() {
throw new Error('Access denied');
}
};
const result = getDM(mockWindow);
expect(result).be.undefined;
});
});
});
});

0 comments on commit 7ded2b7

Please sign in to comment.