Bug 1968982 - Update AdsFeed OHTTP request to use lazy method a=dmeehan

Original Revision: https://phabricator.services.mozilla.com/D251587

Differential Revision: https://phabricator.services.mozilla.com/D251738
This commit is contained in:
Maxx Crawford
2025-05-29 17:44:55 +00:00
committed by dmeehan@mozilla.com
parent cff24df1eb
commit c9db33e322
2 changed files with 88 additions and 41 deletions

View File

@@ -255,6 +255,10 @@ export class AdsFeed {
} }
let fetchPromise; let fetchPromise;
const controller = new AbortController();
const { signal } = controller;
const options = { const options = {
method: "POST", method: "POST",
headers, headers,
@@ -265,11 +269,13 @@ export class AdsFeed {
count: counts[index], count: counts[index],
})), })),
blocks: blockedSponsors.split(","), blocks: blockedSponsors.split(","),
credentials: "omit",
}), }),
signal,
}; };
if (marsOhttpEnabled && ohttpConfigURL && ohttpRelayURL) { if (marsOhttpEnabled && ohttpConfigURL && ohttpRelayURL) {
let config = await this.ObliviousHTTP.getOHTTPConfig(ohttpConfigURL); let config = await lazy.ObliviousHTTP.getOHTTPConfig(ohttpConfigURL);
if (!config) { if (!config) {
console.error( console.error(
new Error( new Error(
@@ -278,7 +284,7 @@ export class AdsFeed {
); );
return null; return null;
} }
fetchPromise = this.ObliviousHTTP.ohttpRequest( fetchPromise = lazy.ObliviousHTTP.ohttpRequest(
ohttpRelayURL, ohttpRelayURL,
config, config,
fetchUrl, fetchUrl,

View File

@@ -10,6 +10,10 @@ ChromeUtils.defineESModuleGetters(this, {
sinon: "resource://testing-common/Sinon.sys.mjs", sinon: "resource://testing-common/Sinon.sys.mjs",
}); });
const { ObliviousHTTP } = ChromeUtils.importESModule(
"resource://gre/modules/ObliviousHTTP.sys.mjs"
);
const PREF_UNIFIED_ADS_ADSFEED_ENABLED = "unifiedAds.adsFeed.enabled"; const PREF_UNIFIED_ADS_ADSFEED_ENABLED = "unifiedAds.adsFeed.enabled";
const PREF_UNIFIED_ADS_ADSFEED_TILES_ENABLED = const PREF_UNIFIED_ADS_ADSFEED_TILES_ENABLED =
"unifiedAds.adsFeed.tiles.enabled"; "unifiedAds.adsFeed.tiles.enabled";
@@ -19,6 +23,14 @@ const PREF_UNIFIED_ADS_PLACEMENTS = "discoverystream.placements.tiles";
const PREF_UNIFIED_ADS_COUNTS = "discoverystream.placements.tiles.counts"; const PREF_UNIFIED_ADS_COUNTS = "discoverystream.placements.tiles.counts";
const PREF_UNIFIED_ADS_BLOCKED_LIST = "unifiedAds.blockedAds"; const PREF_UNIFIED_ADS_BLOCKED_LIST = "unifiedAds.blockedAds";
// Note: Full pref path required by Services.prefs.setBoolPref
const PREF_UNIFIED_ADS_OHTTP_ENABLED =
"browser.newtabpage.activity-stream.unifiedAds.ohttp.enabled";
const PREF_UNIFIED_ADS_OHTTP_RELAY_URL =
"browser.newtabpage.activity-stream.discoverystream.ohttp.relayURL";
const PREF_UNIFIED_ADS_OHTTP_CONFIG_URL =
"browser.newtabpage.activity-stream.discoverystream.ohttp.configURL";
// Primary pref that is toggled when enabling top site sponsored tiles // Primary pref that is toggled when enabling top site sponsored tiles
const PREF_FEED_TOPSITES = "feeds.topsites"; const PREF_FEED_TOPSITES = "feeds.topsites";
const PREF_SHOW_SPONSORED_TOPSITES = "showSponsoredTopSites"; const PREF_SHOW_SPONSORED_TOPSITES = "showSponsoredTopSites";
@@ -45,6 +57,51 @@ const mockedTileData = [
}, },
]; ];
const mockedFetchTileData = {
newtab_tile_1: [
{
format: "tile",
url: "https://www.test1.com",
callbacks: {
click: "https://www.test1-click.com",
impression: "https://www.test1-impression.com",
report: "https://www.test1-report.com",
},
image_url: "images/test1-com.png",
name: "test1",
block_key: "test1",
},
],
newtab_tile_2: [
{
format: "tile",
url: "https://www.test2.com",
callbacks: {
click: "https://www.test2-click.com",
impression: "https://www.test2-impression.com",
report: "https://www.test2-report.com",
},
image_url: "images/test2-com.png",
name: "test2",
block_key: "test2",
},
],
newtab_tile_3: [
{
format: "tile",
url: "https://www.test3.com",
callbacks: {
click: "https://www.test3-click.com",
impression: "https://www.test3-impression.com",
report: "https://www.test3-report.com",
},
image_url: "images/test3-com.png",
name: "test3",
block_key: "test3",
},
],
};
function getAdsFeedForTest() { function getAdsFeedForTest() {
let feed = new AdsFeed(); let feed = new AdsFeed();
let tiles = mockedTileData; let tiles = mockedTileData;
@@ -205,10 +262,7 @@ add_task(async function test_fetchData_noOHTTP() {
sandbox.stub(feed, "Date").returns({ now: () => 123 }); sandbox.stub(feed, "Date").returns({ now: () => 123 });
// Simulate OHTTP being disabled // Simulate OHTTP being disabled
Services.prefs.setBoolPref( Services.prefs.setBoolPref(PREF_UNIFIED_ADS_OHTTP_ENABLED, false);
"browser.newtabpage.activity-stream.unifiedAds.ohttp.enabled",
false
);
const supportedAdTypes = { tiles: true }; const supportedAdTypes = { tiles: true };
await feed.fetchData(supportedAdTypes); await feed.fetchData(supportedAdTypes);
@@ -221,51 +275,38 @@ add_task(async function test_fetchData_OHTTP() {
const sandbox = sinon.createSandbox(); const sandbox = sinon.createSandbox();
const feed = getAdsFeedForTest(); const feed = getAdsFeedForTest();
const mockConfig = { config: "mocked" }; Services.prefs.setBoolPref(PREF_UNIFIED_ADS_OHTTP_ENABLED, true);
const mockResponse = new Response( Services.prefs.setStringPref(
JSON.stringify({ PREF_UNIFIED_ADS_OHTTP_RELAY_URL,
tile1: [ "https://relay.test"
{
block_key: "foo",
name: "bar",
url: "https://test.com",
image_url: "image.png",
callbacks: { click: "click", impression: "impression" },
},
],
})
); );
Services.prefs.setStringPref(
PREF_UNIFIED_ADS_OHTTP_CONFIG_URL,
"https://config.test"
);
const mockConfig = { config: "mocked" };
sandbox sandbox
.stub(AdsFeed.prototype, "PersistentCache") .stub(AdsFeed.prototype, "PersistentCache")
.returns({ get: () => {}, set: () => {} }); .returns({ get: () => {}, set: () => {} });
sandbox.stub(feed, "Date").returns({ now: () => 123 }); sandbox.stub(feed, "Date").returns({ now: () => 123 });
const ohttpStub = { sandbox.stub(ObliviousHTTP, "getOHTTPConfig").resolves(mockConfig);
getOHTTPConfig: sandbox.stub().resolves(mockConfig), sandbox.stub(ObliviousHTTP, "ohttpRequest").resolves({
ohttpRequest: sandbox.stub().resolves(mockResponse), status: 200,
}; json: () => {
return Promise.resolve(mockedFetchTileData);
},
});
feed.ObliviousHTTP = ohttpStub; const result = await feed.fetchData({ tiles: true, spocs: false });
Services.prefs.setBoolPref( info("AdsFeed: fetchData() should fetch via OHTTP when enabled");
"browser.newtabpage.activity-stream.unifiedAds.ohttp.enabled",
true
);
Services.prefs.setStringPref(
"browser.newtabpage.activity-stream.discoverystream.ohttp.relayURL",
"https://relay.test"
);
Services.prefs.setStringPref(
"browser.newtabpage.activity-stream.discoverystream.ohttp.configURL",
"https://config.test"
);
const result = await feed.fetchData({ tiles: true }); Assert.ok(ObliviousHTTP.getOHTTPConfig.calledOnce);
Assert.ok(ObliviousHTTP.ohttpRequest.calledOnce);
Assert.ok(ohttpStub.getOHTTPConfig.calledOnce); Assert.deepEqual(result.tiles[0].id, "test1");
Assert.ok(ohttpStub.ohttpRequest.calledOnce);
Assert.deepEqual(result.tiles[0].id, "foo");
sandbox.restore(); sandbox.restore();
}); });