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

View File

@@ -10,6 +10,10 @@ ChromeUtils.defineESModuleGetters(this, {
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_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_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
const PREF_FEED_TOPSITES = "feeds.topsites";
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() {
let feed = new AdsFeed();
let tiles = mockedTileData;
@@ -205,10 +262,7 @@ add_task(async function test_fetchData_noOHTTP() {
sandbox.stub(feed, "Date").returns({ now: () => 123 });
// Simulate OHTTP being disabled
Services.prefs.setBoolPref(
"browser.newtabpage.activity-stream.unifiedAds.ohttp.enabled",
false
);
Services.prefs.setBoolPref(PREF_UNIFIED_ADS_OHTTP_ENABLED, false);
const supportedAdTypes = { tiles: true };
await feed.fetchData(supportedAdTypes);
@@ -221,51 +275,38 @@ add_task(async function test_fetchData_OHTTP() {
const sandbox = sinon.createSandbox();
const feed = getAdsFeedForTest();
const mockConfig = { config: "mocked" };
const mockResponse = new Response(
JSON.stringify({
tile1: [
{
block_key: "foo",
name: "bar",
url: "https://test.com",
image_url: "image.png",
callbacks: { click: "click", impression: "impression" },
},
],
})
Services.prefs.setBoolPref(PREF_UNIFIED_ADS_OHTTP_ENABLED, true);
Services.prefs.setStringPref(
PREF_UNIFIED_ADS_OHTTP_RELAY_URL,
"https://relay.test"
);
Services.prefs.setStringPref(
PREF_UNIFIED_ADS_OHTTP_CONFIG_URL,
"https://config.test"
);
const mockConfig = { config: "mocked" };
sandbox
.stub(AdsFeed.prototype, "PersistentCache")
.returns({ get: () => {}, set: () => {} });
sandbox.stub(feed, "Date").returns({ now: () => 123 });
const ohttpStub = {
getOHTTPConfig: sandbox.stub().resolves(mockConfig),
ohttpRequest: sandbox.stub().resolves(mockResponse),
};
sandbox.stub(ObliviousHTTP, "getOHTTPConfig").resolves(mockConfig);
sandbox.stub(ObliviousHTTP, "ohttpRequest").resolves({
status: 200,
json: () => {
return Promise.resolve(mockedFetchTileData);
},
});
feed.ObliviousHTTP = ohttpStub;
const result = await feed.fetchData({ tiles: true, spocs: false });
Services.prefs.setBoolPref(
"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"
);
info("AdsFeed: fetchData() should fetch via OHTTP when enabled");
const result = await feed.fetchData({ tiles: true });
Assert.ok(ohttpStub.getOHTTPConfig.calledOnce);
Assert.ok(ohttpStub.ohttpRequest.calledOnce);
Assert.deepEqual(result.tiles[0].id, "foo");
Assert.ok(ObliviousHTTP.getOHTTPConfig.calledOnce);
Assert.ok(ObliviousHTTP.ohttpRequest.calledOnce);
Assert.deepEqual(result.tiles[0].id, "test1");
sandbox.restore();
});