Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 9dd2357

Browse files
committed
Bug 1942222 - Hook up the AMP-suggestion-matching strategy and add a Nimbus variable for it. r=daisuke
Depends on D233983, D234787 Differential Revision: https://phabricator.services.mozilla.com/D234637
1 parent 7135a4d commit 9dd2357

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

browser/app/profile/firefox.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,13 @@ pref("browser.urlbar.quicksuggest.impressionCaps.sponsoredEnabled", false);
583583
// characters than this threshold.
584584
pref("browser.urlbar.quicksuggest.ampTopPickCharThreshold", 0);
585585

586+
// The matching strategy for AMP suggestions. Zero is the usual default
587+
// exact-keyword strategy. Other values are the integers defined on
588+
// `AmpMatchingStrategy` in `RustSuggest.sys.mjs` (corresponding to the
589+
// `AmpMatchingStrategy` enum in the Rust component coerced to a 1-based integer
590+
// value).
591+
pref("browser.urlbar.quicksuggest.ampMatchingStrategy", 0);
592+
586593
// Comma-separated list of Suggest exposure suggestion types to enable.
587594
pref("browser.urlbar.quicksuggest.exposureSuggestionTypes", "");
588595

browser/components/urlbar/private/AmpSuggestions.sys.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SuggestProvider } from "resource:///modules/urlbar/private/SuggestFeatu
77
const lazy = {};
88

99
ChromeUtils.defineESModuleGetters(lazy, {
10+
AmpMatchingStrategy: "resource://gre/modules/RustSuggest.sys.mjs",
1011
CONTEXTUAL_SERVICES_PING_TYPES:
1112
"resource:///modules/PartnerLinkAttribution.sys.mjs",
1213
QuickSuggest: "resource:///modules/QuickSuggest.sys.mjs",
@@ -46,6 +47,25 @@ export class AmpSuggestions extends SuggestProvider {
4647
return "Amp";
4748
}
4849

50+
get rustProviderConstraints() {
51+
let intValue = lazy.UrlbarPrefs.get("ampMatchingStrategy");
52+
if (!intValue) {
53+
// If the value is zero or otherwise falsey, use the usual default
54+
// exact-keyword strategy by returning null here.
55+
return null;
56+
}
57+
if (!Object.values(lazy.AmpMatchingStrategy).includes(intValue)) {
58+
this.logger.error(
59+
"Unknown AmpMatchingStrategy value, using default strategy",
60+
{ intValue }
61+
);
62+
return null;
63+
}
64+
return {
65+
ampAlternativeMatching: intValue,
66+
};
67+
}
68+
4969
isSuggestionSponsored() {
5070
return true;
5171
}

browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
"use strict";
99

10+
ChromeUtils.defineESModuleGetters(this, {
11+
AmpMatchingStrategy: "resource://gre/modules/RustSuggest.sys.mjs",
12+
SuggestionProvider: "resource://gre/modules/RustSuggest.sys.mjs",
13+
});
14+
1015
const SPONSORED_SEARCH_STRING = "amp";
1116
const NONSPONSORED_SEARCH_STRING = "wikipedia";
1217
const SPONSORED_AND_NONSPONSORED_SEARCH_STRING = "sponsored and non-sponsored";
@@ -1868,3 +1873,124 @@ add_task(async function ampTopPickCharThreshold_zero() {
18681873

18691874
UrlbarPrefs.clear("quicksuggest.ampTopPickCharThreshold");
18701875
});
1876+
1877+
// Tests `ampMatchingStrategy`.
1878+
add_task(async function ampMatchingStrategy() {
1879+
UrlbarPrefs.set("suggest.quicksuggest.nonsponsored", false);
1880+
UrlbarPrefs.set("suggest.quicksuggest.sponsored", true);
1881+
await QuickSuggestTestUtils.forceSync();
1882+
1883+
// Test each strategy in `AmpMatchingStrategy`. There are only a few.
1884+
for (let [key, value] of Object.entries(AmpMatchingStrategy)) {
1885+
await doAmpMatchingStrategyTest({ key, value });
1886+
1887+
// Reset back to the default strategy just to make sure that works.
1888+
await doAmpMatchingStrategyTest({
1889+
key: "(default)",
1890+
value: 0,
1891+
});
1892+
}
1893+
1894+
// Test an invalid strategy integer value. The default strategy should
1895+
// actually be used. First we need to set a valid non-default strategy.
1896+
await doAmpMatchingStrategyTest({
1897+
key: "FTS_AGAINST_TITLE",
1898+
value: AmpMatchingStrategy.FTS_AGAINST_TITLE,
1899+
});
1900+
await doAmpMatchingStrategyTest({
1901+
key: "(invalid)",
1902+
value: 99,
1903+
expectedStrategy: 0,
1904+
});
1905+
1906+
Services.prefs.clearUserPref(
1907+
"browser.urlbar.quicksuggest.ampMatchingStrategy"
1908+
);
1909+
await QuickSuggestTestUtils.forceSync();
1910+
});
1911+
1912+
async function doAmpMatchingStrategyTest({
1913+
key,
1914+
value,
1915+
expectedStrategy = value,
1916+
}) {
1917+
info("Doing ampMatchingStrategy test: " + JSON.stringify({ key, value }));
1918+
1919+
let sandbox = sinon.createSandbox();
1920+
let ingestSpy = sandbox.spy(QuickSuggest.rustBackend._test_store, "ingest");
1921+
1922+
// Set the strategy. It should trigger ingest. (Assuming it's different from
1923+
// the current strategy. If it's not, ingest won't happen.)
1924+
Services.prefs.setIntPref(
1925+
"browser.urlbar.quicksuggest.ampMatchingStrategy",
1926+
value
1927+
);
1928+
1929+
let ingestCall = await TestUtils.waitForCondition(() => {
1930+
return ingestSpy.getCalls().find(call => {
1931+
let ingestConstraints = call.args[0];
1932+
return ingestConstraints?.providers[0] == SuggestionProvider.AMP;
1933+
});
1934+
}, "Waiting for ingest() to be called with Amp provider");
1935+
1936+
// Check the provider constraints in the ingest constraints.
1937+
let { providerConstraints } = ingestCall.args[0];
1938+
if (!expectedStrategy) {
1939+
Assert.ok(
1940+
!providerConstraints,
1941+
"ingest() should not have been called with provider constraints"
1942+
);
1943+
} else {
1944+
Assert.ok(
1945+
providerConstraints,
1946+
"ingest() should have been called with provider constraints"
1947+
);
1948+
Assert.strictEqual(
1949+
providerConstraints.ampAlternativeMatching,
1950+
expectedStrategy,
1951+
"ampAlternativeMatching should have been set"
1952+
);
1953+
}
1954+
1955+
// Now do a query to make sure it also uses the correct provider constraints.
1956+
// No need to use `check_results()`. We only need to trigger a query, and
1957+
// checking the right results unnecessarily complicates things.
1958+
let querySpy = sandbox.spy(
1959+
QuickSuggest.rustBackend._test_store,
1960+
"queryWithMetrics"
1961+
);
1962+
1963+
let controller = UrlbarTestUtils.newMockController();
1964+
await controller.startQuery(
1965+
createContext(SPONSORED_SEARCH_STRING, {
1966+
providers: [UrlbarProviderQuickSuggest.name],
1967+
isPrivate: false,
1968+
})
1969+
);
1970+
1971+
let queryCalls = querySpy.getCalls();
1972+
Assert.equal(queryCalls.length, 1, "query() should have been called once");
1973+
1974+
let query = queryCalls[0].args[0];
1975+
Assert.ok(query, "query() should have been called with a query object");
1976+
Assert.ok(
1977+
query.providerConstraints,
1978+
"query() should have been called with provider constraints"
1979+
);
1980+
1981+
if (!expectedStrategy) {
1982+
Assert.strictEqual(
1983+
query.providerConstraints.ampAlternativeMatching,
1984+
null,
1985+
"ampAlternativeMatching should not have been set on query provider constraints"
1986+
);
1987+
} else {
1988+
Assert.strictEqual(
1989+
query.providerConstraints.ampAlternativeMatching,
1990+
expectedStrategy,
1991+
"ampAlternativeMatching should have been set on query provider constraints"
1992+
);
1993+
}
1994+
1995+
sandbox.restore();
1996+
}

toolkit/components/nimbus/FeatureManifest.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ urlbar:
232232
will be able to click the "Show less frequently" command for addon
233233
suggestions. If undefined or zero, the user will be able to click the
234234
command without any limit.
235+
ampMatchingStrategy:
236+
type: int
237+
fallbackPref: browser.urlbar.quicksuggest.ampMatchingStrategy
238+
description: >-
239+
The matching strategy for AMP suggestions. Leave undefined or set to
240+
zero to use the usual default exact-keyword strategy. Otherwise set to
241+
an integer value defined on `AmpMatchingStrategy` in
242+
`RustSuggest.sys.mjs` (corresponding to the `AmpMatchingStrategy` enum
243+
in the Rust component coerced to a 1-based integer value).
235244
autoFillAdaptiveHistoryEnabled:
236245
type: boolean
237246
fallbackPref: browser.urlbar.autoFill.adaptiveHistory.enabled

0 commit comments

Comments
 (0)