Files
tubestation/browser/components/urlbar/ActionsProviderTabGroups.sys.mjs
Stephen Thompson f2e07044ae Bug 1938424 - metrics event when tabs added to tab group r=dao,jswinarton,tabbrowser-reviewers,extension-reviewers,robwu
When using the tab context menu or drag-dropping tabs to put them into a group, record a metric for the number of tabs added to the group.

Data Science asked us to launch with this metric disabled so that they could control it using server knobs. They expect a high volume of events, so they expect to only enable this metric for some proportion of users.

I converted the existing `TabMove` event derived from `UIEvent` being fired when tabs change their tab index in the tab strip. `UIEvent` doesn't allow for attaching additional context/detail to the event. `TabMove` is now a `CustomEvent` that provides more context about the moved tab and it fires in more cases -- it's possible for the tab index not to change despite the tab having "moved" into/out of a tab group.

This approach would not capture tab movements that occur across multiple frames/event loop iterations.

Differential Revision: https://phabricator.services.mozilla.com/D244616
2025-04-11 13:52:22 +00:00

117 lines
3.4 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import {
ActionsProvider,
ActionsResult,
} from "resource:///modules/ActionsProvider.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.sys.mjs",
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
SessionStore: "resource:///modules/sessionstore/SessionStore.sys.mjs",
TabMetrics: "moz-src:///browser/components/tabbrowser/TabMetrics.sys.mjs",
});
const MIN_SEARCH_PREF = "tabGroups.minSearchLength";
/**
* A provider that matches the urlbar input to built in actions.
*/
class ProviderTabGroups extends ActionsProvider {
get name() {
return "ActionsProviderTabGroups";
}
isActive(queryContext) {
return (
Services.prefs.getBoolPref("browser.tabs.groups.enabled") &&
!queryContext.searchMode &&
queryContext.trimmedSearchString.length < 50 &&
queryContext.trimmedSearchString.length >=
lazy.UrlbarPrefs.get(MIN_SEARCH_PREF)
);
}
async queryActions(queryContext) {
let window = lazy.BrowserWindowTracker.getTopWindow();
if (!window) {
// We're likely running xpcshell tests if this happens in automation.
if (!Cu.isInAutomation) {
console.error("Couldn't find a browser window.");
}
return null;
}
let input = queryContext.trimmedLowerCaseSearchString;
let results = [];
let i = 0;
for (let group of window.gBrowser.getAllTabGroups()) {
if (group.label.toLowerCase().startsWith(input)) {
results.push(
this.#makeResult({
key: `tabgroup-${i++}`,
l10nId: "urlbar-result-action-switch-to-tabgroup",
l10nArgs: { group: group.label },
onPick: (_queryContext, _controller) => {
this.#switchToGroup(group);
},
color: group.color,
})
);
}
}
for (let savedGroup of lazy.SessionStore.getSavedTabGroups()) {
if (savedGroup.name.toLowerCase().startsWith(input)) {
results.push(
this.#makeResult({
key: `tabgroup-${i++}`,
l10nId: "urlbar-result-action-open-saved-tabgroup",
l10nArgs: { group: savedGroup.name },
onPick: (_queryContext, _controller) => {
let group = lazy.SessionStore.openSavedTabGroup(
savedGroup.id,
window,
{
source: lazy.TabMetrics.METRIC_SOURCE.SUGGEST,
}
);
this.#switchToGroup(group);
},
color: savedGroup.color,
})
);
}
}
return results;
}
#makeResult({ key, l10nId, l10nArgs, onPick, color }) {
return new ActionsResult({
key,
l10nId,
l10nArgs,
onPick,
icon: "chrome://browser/skin/tabbrowser/tab-groups.svg",
dataset: {
style: {
"--tab-group-color": `var(--tab-group-color-${color})`,
"--tab-group-color-invert": `var(--tab-group-color-${color}-invert)`,
"--tab-group-color-pale": `var(--tab-group-color-${color}-pale)`,
},
},
});
}
#switchToGroup(group) {
group.select();
group.ownerGlobal.focus();
}
}
export var ActionsProviderTabGroups = new ProviderTabGroups();