Skip to content
Open
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
10 changes: 5 additions & 5 deletions 3p/3p.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,18 @@ export function validateSrcContains(string, src) {
* done. The first argument is the result.
*/
export function computeInMasterFrame(global, taskId, work, cb) {
const {master} = global.context;
let tasks = master.__ampMasterTasks;
const {primary} = global.context;
let tasks = primary.__ampMasterTasks;
if (!tasks) {
tasks = master.__ampMasterTasks = {};
tasks = primary.__ampMasterTasks = {};
}
let cbs = tasks[taskId];
if (!tasks[taskId]) {
cbs = tasks[taskId] = [];
}
cbs.push(cb);
if (!global.context.isMaster) {
return; // Only do work in master.
if (!global.context.isPrimary) {
return; // Only do work in primary frame.
}
work((result) => {
for (let i = 0; i < cbs.length; i++) {
Expand Down
25 changes: 18 additions & 7 deletions 3p/ampcontext-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,34 @@ export class IntegrationAmpContext extends AbstractAmpContext {
}

/** @return {!Window} */
get master() {
return this.master_();
get primary() {
return this.primary_();
}

/** @return {!Window} */
master_() {
primary_() {
return masterSelection(this.win_, dev().assertString(this.embedType_));
}

/** @return {boolean} */
get isMaster() {
return this.isMaster_();
get isPrimary() {
return this.isPrimary_();
}

/** @return {boolean} */
isPrimary_() {
return this.primary == this.win_;
}

// Backwards compatibility aliases
/** @return {!Window} */
get master() {
return this.primary_();
}

/** @return {boolean} */
isMaster_() {
return this.master == this.win_;
get isMaster() {
return this.isPrimary_();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions ads/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,11 @@ documentation with the new behaviors on user's consent choices. You can refer to

#### JS reuse across iframes

To allow ads to bundle HTTP requests across multiple ad units on the same page the object `window.context.master` will contain the window object of the iframe being elected master iframe for the current page. The `window.context.isMaster` property is `true` when the current frame is the master frame.
To allow ads to bundle HTTP requests across multiple ad units on the same page the object `window.context.primary` will contain the window object of the iframe being elected primary iframe for the current page. The `window.context.isPrimary` property is `true` when the current frame is the primary frame.

The `computeInMasterFrame` function is designed to make it easy to perform a task only in the master frame and provide the result to all frames. It is also available to custom ad iframes as `window.context.computeInMasterFrame`. See [3p.js](https://github.com/ampproject/amphtml/blob/main/3p/3p.js) for function signature.
The `computeInMasterFrame` function is designed to make it easy to perform a task only in the primary frame and provide the result to all frames. It is also available to custom ad iframes as `window.context.computeInMasterFrame`. See [3p.js](https://github.com/ampproject/amphtml/blob/main/3p/3p.js) for function signature.

**Note:** For backwards compatibility, `window.context.master` and `window.context.isMaster` are still available as aliases to the new properties, but we recommend using the new `primary` and `isPrimary` properties going forward.

#### Preconnect and prefetch

Expand Down
24 changes: 12 additions & 12 deletions ads/vendors/appnexus.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ export function appnexus(global, data) {
function appnexusAst(global, data) {
validateData(data, ['adUnits']);
let apntag;
if (context.isMaster) {
// in case we are in the master iframe, we load AST
context.master.apntag = context.master.apntag || {};
context.master.apntag.anq = context.master.apntag.anq || [];
apntag = context.master.apntag;
if (context.isPrimary) {
// in case we are in the primary iframe, we load AST
context.primary.apntag = context.primary.apntag || {};
context.primary.apntag.anq = context.primary.apntag.anq || [];
apntag = context.primary.apntag;

context.master.adUnitTargetIds = context.master.adUnitTargetIds || [];
context.primary.adUnitTargetIds = context.primary.adUnitTargetIds || [];

context.master.adUnitTargetIds = data.adUnits.map(
context.primary.adUnitTargetIds = data.adUnits.map(
(adUnit) => adUnit.targetId
);

Expand Down Expand Up @@ -97,21 +97,21 @@ function appnexusAst(global, data) {
}

if (!apntag) {
apntag = context.master.apntag;
apntag = context.primary.apntag;
//preserve a global reference
/** @type {{showTag: function(string, Object)}} global.apntag */
global.apntag = context.master.apntag;
global.apntag = context.primary.apntag;
}

if (!context.isMaster && data.adUnits) {
if (!context.isPrimary && data.adUnits) {
const newAddUnits = data.adUnits.filter((adUnit) => {
return context.master.adUnitTargetIds.indexOf(adUnit.targetId) === -1;
return context.primary.adUnitTargetIds.indexOf(adUnit.targetId) === -1;
});
if (newAddUnits.length) {
apntag.anq.push(() => {
/** @type {!Array} */ (newAddUnits).forEach((adUnit) => {
apntag.defineTag(adUnit);
context.master.adUnitTargetIds.push(adUnit.targetId);
context.primary.adUnitTargetIds.push(adUnit.targetId);
});
apntag.loadTags();
});
Expand Down
8 changes: 4 additions & 4 deletions ads/vendors/clickio.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function clickio(global, data) {
const clickioGlobal = global[CLICKIO_GLOBAL_NAME];

// base config
clickioGlobal.ampMode = context.isMaster ? 1 : 2;
clickioGlobal.ampMode = context.isPrimary ? 1 : 2;
clickioGlobal.pageUrl = global.context.location.href;
clickioGlobal.sendPageUrl = true;

Expand Down Expand Up @@ -61,8 +61,8 @@ export function clickio(global, data) {
'https://s.clickiocdn.com/t/' + data.siteId + '/360_amp.js'
);

// load consent module for master
if (context.isMaster) {
// load consent module for primary
if (context.isPrimary) {
// consent module
if (
context.initialConsentMetadata !== null &&
Expand All @@ -86,7 +86,7 @@ export function clickio(global, data) {

dev().info(
CLICKIO_LOG_TAG,
'clickio: running (' + (context.isMaster ? 'master' : 'slave') + ')'
'clickio: running (' + (context.isPrimary ? 'primary' : 'secondary') + ')'
);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion ads/vendors/imedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {computeInMasterFrame, loadScript, validateData} from '#3p/3p';
export function imedia(global, data) {
validateData(data, ['id', 'positions']);
const positions = JSON.parse(data.positions);
const mW = context.isMaster ? global : context.master;
const mW = context.isPrimary ? global : context.primary;

// create parent element
const parentElement = document.createElement('div');
Expand Down
6 changes: 3 additions & 3 deletions ads/vendors/kargo.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export function kargo(global, data) {
if (success) {
const w = options.source_window;

// Add reference to Kargo api to this window if it's not the Master window
if (!w.context.isMaster) {
w.Kargo = w.context.master.Kargo;
// Add reference to Kargo api to this window if it's not the Primary window
if (!w.context.isPrimary) {
w.Kargo = w.context.primary.Kargo;
}

w.Kargo.getAd(data.slot, options);
Expand Down
4 changes: 2 additions & 2 deletions ads/vendors/medianet.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function loadHBTag(global, data, publisherUrl, referrerUrl) {
}
loadMNETAd.alreadyCalled = true;

global.advBidxc = global.context.master.advBidxc;
global.advBidxc = global.context.primary.advBidxc;
if (global.advBidxc && typeof global.advBidxc.renderAmpAd === 'function') {
global.addEventListener('message', (event) => {
global.advBidxc.renderAmpAd(event, global);
Expand All @@ -171,7 +171,7 @@ function loadHBTag(global, data, publisherUrl, referrerUrl) {
* Handler for mnet.
*/
function mnetHBHandle() {
global.advBidxc = global.context.master.advBidxc;
global.advBidxc = global.context.primary.advBidxc;
if (
global.advBidxc &&
typeof global.advBidxc.registerAmpSlot === 'function'
Expand Down
8 changes: 4 additions & 4 deletions ads/vendors/pubmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export function pubmine(global, data) {
if (isMaster) {
initMasterFrame(data, global);
}
const master = isMaster ? global : global.context.master;
master['__ATA']['cmd']['push'](function () {
master['__ATA']['insertStyles'](global);
master['__ATA']['initSlot'](slotId, slotConfig);
const primary = isMaster ? global : global.context.primary;
primary['__ATA']['cmd']['push'](function () {
primary['__ATA']['insertStyles'](global);
primary['__ATA']['initSlot'](slotId, slotConfig);
});
}
10 changes: 5 additions & 5 deletions ads/vendors/springAds.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {parseJson} from '#core/types/object/json';
*/

const initSlotList = (context) => {
context.master.availableSlots = context.master.availableSlots || {};
context.primary.availableSlots = context.primary.availableSlots || {};
};

const registerSlot = (slot) => {
context.master.availableSlots[slot.slotName] = slot;
context.primary.availableSlots[slot.slotName] = slot;
};

// eslint-disable-next-line require-jsdoc
Expand All @@ -27,8 +27,8 @@ export function springAds(global, data) {
if (data.adssetup) {
const adSSetup = parseJson(data.adssetup);
adSSetup['isAMP'] = !0;
adSSetup['availableSlots'] = context.master.availableSlots;
context.master.adSSetup = global.adSSetup = adSSetup;
adSSetup['availableSlots'] = context.primary.availableSlots;
context.primary.adSSetup = global.adSSetup = adSSetup;
const sitename = adSSetup['publisher'].match(/(.*)\..*/)[1];
loadScript(
global,
Expand All @@ -41,7 +41,7 @@ export function springAds(global, data) {
context,
slotName: data['adslot'],
});
const adlib = window.ASCDP || context.master.ASCDP || '';
const adlib = window.ASCDP || context.primary.ASCDP || '';
adlib && adlib.adS.renderAd(data.adslot);
}
}
2 changes: 1 addition & 1 deletion ads/vendors/ssp.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function ssp(global, data) {
}

// This is super important. Without this any variables on context are not shared
const mW = global.context.isMaster ? global : global.context.master;
const mW = global.context.isPrimary ? global : global.context.primary;

// create parent element
const parentElement = document.createElement('div');
Expand Down
4 changes: 2 additions & 2 deletions ads/vendors/swoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export function swoop(global, data) {
},
(success) => {
if (success) {
if (!global.context.isMaster) {
global.context.master.Swoop.announcePlace(global, data);
if (!global.context.isPrimary) {
global.context.primary.Swoop.announcePlace(global, data);
}
} else {
global.context.noContentAvailable();
Expand Down
2 changes: 1 addition & 1 deletion ads/vendors/yieldbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function yieldbot(global, data) {

global.yieldbot.psn(data.psn);
global.yieldbot.enableAsync();
if (window.context.isMaster) {
if (window.context.isPrimary) {
global.yieldbot.defineSlot(data.ybSlot, {sizes: dimensions});
global.yieldbot.go();
} else {
Expand Down
2 changes: 1 addition & 1 deletion ads/vendors/yieldpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function yieldpro(global, data) {
},
(success) => {
if (success) {
global.showadAMPAdapter = global.context.master.showadAMPAdapter;
global.showadAMPAdapter = global.context.primary.showadAMPAdapter;
global.showadAMPAdapter.registerSlot(data, global);
} else {
throw new Error('Yieldpro AdTag failed to load');
Expand Down
2 changes: 2 additions & 0 deletions build-system/externs/amp.extern.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ window.context.initialLayoutRect;
window.context.initialIntersection;
window.context.sourceUrl;
window.context.experimentToggles;
window.context.primary;
window.context.isPrimary;
window.context.master;
window.context.isMaster;
window.context.ampcontextVersion;
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test-amp-ad-3p.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describes.sandboxed('amp-ad 3P', {}, () => {
expect(initialIntersection.boundingClientRect).to.deep.equal(
layoutRectLtwh(0, platform.isIos() ? 1001 : 1000, 300, 250)
);
expect(context.isMaster).to.exist;
expect(context.isPrimary).to.exist;
expect(context.computeInMasterFrame).to.exist;
expect(context.location).to.deep.include({
hash: '',
Expand Down
6 changes: 3 additions & 3 deletions test/unit/ads/test-pubmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ describes.fakeWin('pubmine', {}, (env) => {
cmd: [],
};
win.context = {
isMaster: false,
master: win,
isPrimary: false,
primary: win,
};
pubmine(win, mockData);
expect(win.context.master.__ATA.cmd).to.have.length(1);
expect(win.context.primary.__ATA.cmd).to.have.length(1);
expect(getPubmineScriptElement()).to.be.null;
expect(getSlotElement()).to.be.ok;
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ads/test-ssp.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describes.fakeWin('amp-ad-ssp', {}, (env) => {
sandbox.stub(_3p, 'loadScript').callsFake((window, url, cb) => {
// Mock script adding global object
window.sssp = sssp;
window.context.master.ssp = sssp;
window.context.primary.ssp = sssp;
window.ssp = sssp;

cb();
Expand Down
16 changes: 8 additions & 8 deletions test/unit/test-3p.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,26 @@ describes.sandboxed('3p', {}, (env) => {
const taskId = 'exampleId';
const master = {
context: {
isMaster: true,
isPrimary: true,
},
};
master.context.master = master;
master.context.primary = master;
const slave0 = {
context: {
isMaster: false,
master,
isPrimary: false,
primary: master,
},
};
const slave1 = {
context: {
isMaster: false,
master,
isPrimary: false,
primary: master,
},
};
const slave2 = {
context: {
isMaster: false,
master,
isPrimary: false,
primary: master,
},
};
let done;
Expand Down