Bug 1770870 - Exclude history URLs with the AMP query parameter from appearing in urlbar results. r=daisuke,nanj

This modifies the muxer to discard history results whose URLs were originally
sponsored according to the same URL search param we used for top sites in
bug 1768529. I used the newtab pref from that bug. I thought about making a
urlbar-specific version, but there's no reason to complicate it.

The test is very similar to [the test I added for the other bug](https://searchfox.org/mozilla-central/rev/301e9545863b14ff85aa110fabd4274da4593f9d/toolkit/modules/tests/xpcshell/test_NewTabUtils.js#1209).

Differential Revision: https://phabricator.services.mozilla.com/D147395
This commit is contained in:
Drew Willcoxon
2022-05-26 16:08:34 +00:00
parent 31e4efa801
commit e9d847f8d8
3 changed files with 130 additions and 0 deletions

View File

@@ -885,6 +885,31 @@ class MuxerUnifiedComplete extends UrlbarMuxer {
return false;
}
// Discard history results whose URLs were originally sponsored. We use the
// presence of a partner's URL search param to detect these. The param is
// defined in the pref below, which is also used for the newtab page.
if (
result.source == UrlbarUtils.RESULT_SOURCE.HISTORY &&
result.type == UrlbarUtils.RESULT_TYPE.URL
) {
let param = Services.prefs.getCharPref(
"browser.newtabpage.activity-stream.hideTopSitesWithSearchParam"
);
if (param) {
let [key, value] = param.split("=");
let searchParams;
try {
({ searchParams } = new URL(result.payload.url));
} catch (error) {}
if (
(value === undefined && searchParams?.has(key)) ||
(value !== undefined && searchParams?.getAll(key).includes(value))
) {
return false;
}
}
}
// Heuristic results must always be the first result. If this result is a
// heuristic but we've already added results, discard it. Normally this
// should never happen because the standard result groups are set up so

View File

@@ -0,0 +1,104 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// This tests the muxer functionality that hides URLs in history that were
// originally sponsored.
"use strict";
add_task(async function test() {
// Disable search suggestions to avoid hitting the network.
UrlbarPrefs.set("suggest.searches", false);
let engine = await Services.search.getDefault();
let pref = "browser.newtabpage.activity-stream.hideTopSitesWithSearchParam";
// This maps URL search params to objects describing whether a URL with those
// params is expected to appear in the search results. Each inner object maps
// from a value of the pref to whether the URL is expected to appear given the
// pref value.
let tests = {
"": {
"": true,
test: true,
"test=": true,
"test=hide": true,
nomatch: true,
"nomatch=": true,
"nomatch=hide": true,
},
test: {
"": true,
test: false,
"test=": false,
"test=hide": true,
nomatch: true,
"nomatch=": true,
"nomatch=hide": true,
},
"test=hide": {
"": true,
test: false,
"test=": true,
"test=hide": false,
nomatch: true,
"nomatch=": true,
"nomatch=hide": true,
},
"test=foo&test=hide": {
"": true,
test: false,
"test=": true,
"test=hide": false,
nomatch: true,
"nomatch=": true,
"nomatch=hide": true,
},
};
for (let [urlParams, expected] of Object.entries(tests)) {
for (let [prefValue, shouldAppear] of Object.entries(expected)) {
info(
"Running test: " +
JSON.stringify({ urlParams, prefValue, shouldAppear })
);
// Add a visit to a URL with search params `urlParams`.
let url = new URL("http://example.com/");
url.search = urlParams;
await PlacesTestUtils.addVisits(url);
// Set the pref to `prefValue`.
Services.prefs.setCharPref(pref, prefValue);
// Set up the context and expected results. If `shouldAppear` is true, a
// visit result for the URL should appear.
let context = createContext("ample", { isPrivate: false });
let expectedResults = [
makeSearchResult(context, {
heuristic: true,
engineName: engine.name,
engineIconUri: engine.iconURI?.spec,
}),
];
if (shouldAppear) {
expectedResults.push(
makeVisitResult(context, {
uri: url.toString(),
title: "test visit for " + url,
})
);
}
// Do a search and check the results.
await check_results({
context,
matches: expectedResults,
});
await PlacesUtils.history.clear();
}
}
Services.prefs.clearUserPref(pref);
});

View File

@@ -30,6 +30,7 @@ support-files =
[test_escaping_escapeSelf.js]
[test_frecency.js]
[test_heuristic_cancel.js]
[test_hideSponsoredHistory.js]
[test_keywords.js]
skip-if = os == 'linux' # bug 1474616
[test_l10nCache.js]