forked from mirrors/gecko-dev
This removes `UrlbarProvider.pickResult()` and `blockResult()` in favor of handling picks and dismissals through `onEngagement()`. A number of providers use those two methods, so this revision touches a lot of files. Handling dismissals through `onEngagement()` means `UrlbarInput.pickResult()` can no longer tell whether a result is successfully dismissed, so it can't remove the result anymore. (Maybe `onEngagement()` could return some value indicating it dismissed the result, but I don't want to go down that road.) Instead, I split `UrlbarController.handleDeleteEntry()` into two methods: a public one that removes the result and notifies listeners, and a private one that handles dismissing the selected result internally in UrlbarController. Providers that have dismissable results should now implement `onEngagement()` and call `controller.removeResult()`. I made some other improvements to engagement handling. There's still room for more but this patch is big enough already. Other notable changes: Include the engaged result in engagement notifications so providers have easy access to it and can respond to clicks and dismissals more easily. That also lets us stop passing `selIndex` and `provider` to `engagementEvent.record()` since now it can compute those from the passed-in result. Add the concept of `isSessionOngoing` to engagement notifications so providers can tell whether an engagement ended the search session. Right now, providers like quick suggest that record a bunch of provider-specific legacy telemetry assume that `onEngagement()` ends the session, but that's no longer true. Unify result buttons and result menu commands by setting `element.dataset.command` on buttons (hopefully we can remove buttons soon, at least the ones that aren't tip buttons) Make sure we always notify providers on engagement even on dismissals or when skipping legacy telemetry Move dismissal of restyled search suggestions and history results from `UrlbarController.handleDeleteEntry()` to the Places provider Move dismissal of form history results from `UrlbarController.handleDeleteEntry()` to the search suggestions provider In the Places provider, remove the unused `_addSearchEngineMatch()` method. Also remove the code in the "searchengine" case that creates a non-search-history result. This code is unreached because the only time the provider creates a "searchengine" match it also sets `isSearchHistory` to true. In `UrlbarTestUtils.promiseAutocompleteResultPopup()`, change the default value of the `fireInputEvent` param from false to true. This is necessary because without a starting input event, the start event info in `engagementEvent` will be null, so when `engagementEvent.record()` is called at the end of the engagement, it will bail, and providers will not be notified of the engagement. IMO true is a better default value anyway because input events will typically be fired when the user performs a search. Differential Revision: https://phabricator.services.mozilla.com/D174941
183 lines
4.8 KiB
JavaScript
183 lines
4.8 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/. */
|
|
|
|
/**
|
|
* Provide unit converter.
|
|
*/
|
|
|
|
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
|
|
|
|
import { UnitConverterSimple } from "resource:///modules/UnitConverterSimple.sys.mjs";
|
|
import { UnitConverterTemperature } from "resource:///modules/UnitConverterTemperature.sys.mjs";
|
|
import { UnitConverterTimezone } from "resource:///modules/UnitConverterTimezone.sys.mjs";
|
|
import {
|
|
UrlbarProvider,
|
|
UrlbarUtils,
|
|
} from "resource:///modules/UrlbarUtils.sys.mjs";
|
|
|
|
const lazy = {};
|
|
|
|
ChromeUtils.defineESModuleGetters(lazy, {
|
|
UrlbarPrefs: "resource:///modules/UrlbarPrefs.sys.mjs",
|
|
UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
|
|
UrlbarView: "resource:///modules/UrlbarView.sys.mjs",
|
|
});
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(
|
|
lazy,
|
|
"ClipboardHelper",
|
|
"@mozilla.org/widget/clipboardhelper;1",
|
|
"nsIClipboardHelper"
|
|
);
|
|
|
|
const CONVERTERS = [
|
|
new UnitConverterSimple(),
|
|
new UnitConverterTemperature(),
|
|
new UnitConverterTimezone(),
|
|
];
|
|
|
|
const DYNAMIC_RESULT_TYPE = "unitConversion";
|
|
const VIEW_TEMPLATE = {
|
|
attributes: {
|
|
selectable: true,
|
|
},
|
|
children: [
|
|
{
|
|
name: "content",
|
|
tag: "span",
|
|
classList: ["urlbarView-no-wrap"],
|
|
children: [
|
|
{
|
|
name: "icon",
|
|
tag: "img",
|
|
classList: ["urlbarView-favicon"],
|
|
attributes: {
|
|
src: "chrome://global/skin/icons/edit-copy.svg",
|
|
},
|
|
},
|
|
{
|
|
name: "output",
|
|
tag: "strong",
|
|
},
|
|
{
|
|
name: "action",
|
|
tag: "span",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
/**
|
|
* Provide a feature that converts given units.
|
|
*/
|
|
class ProviderUnitConversion extends UrlbarProvider {
|
|
constructor() {
|
|
super();
|
|
lazy.UrlbarResult.addDynamicResultType(DYNAMIC_RESULT_TYPE);
|
|
lazy.UrlbarView.addDynamicViewTemplate(DYNAMIC_RESULT_TYPE, VIEW_TEMPLATE);
|
|
}
|
|
|
|
/**
|
|
* Returns the name of this provider.
|
|
*
|
|
* @returns {string} the name of this provider.
|
|
*/
|
|
get name() {
|
|
return "UnitConversion";
|
|
}
|
|
|
|
/**
|
|
* Returns the type of this provider.
|
|
*
|
|
* @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
|
|
*/
|
|
get type() {
|
|
return UrlbarUtils.PROVIDER_TYPE.PROFILE;
|
|
}
|
|
|
|
/**
|
|
* Whether the provider should be invoked for the given context. If this
|
|
* method returns false, the providers manager won't start a query with this
|
|
* provider, to save on resources.
|
|
*
|
|
* @param {UrlbarQueryContext} queryContext
|
|
* The query context object.
|
|
* @returns {boolean}
|
|
* Whether this provider should be invoked for the search.
|
|
*/
|
|
isActive({ searchString }) {
|
|
if (!lazy.UrlbarPrefs.get("unitConversion.enabled")) {
|
|
return false;
|
|
}
|
|
|
|
for (const converter of CONVERTERS) {
|
|
const result = converter.convert(searchString);
|
|
if (result) {
|
|
this._activeResult = result;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
this._activeResult = null;
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* This is called only for dynamic result types, when the urlbar view updates
|
|
* the view of one of the results of the provider. It should return an object
|
|
* describing the view update.
|
|
*
|
|
* @param {UrlbarResult} result The result whose view will be updated.
|
|
* @returns {object} An object describing the view update.
|
|
*/
|
|
getViewUpdate(result) {
|
|
return {
|
|
output: {
|
|
textContent: result.payload.output,
|
|
},
|
|
action: {
|
|
l10n: { id: "urlbar-result-action-copy-to-clipboard" },
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* This method is called by the providers manager when a query starts to fetch
|
|
* each extension provider's results. It fires the resultsRequested event.
|
|
*
|
|
* @param {UrlbarQueryContext} queryContext
|
|
* The query context object.
|
|
* @param {Function} addCallback
|
|
* The callback invoked by this method to add each result.
|
|
*/
|
|
startQuery(queryContext, addCallback) {
|
|
const result = new lazy.UrlbarResult(
|
|
UrlbarUtils.RESULT_TYPE.DYNAMIC,
|
|
UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
|
|
{
|
|
dynamicType: DYNAMIC_RESULT_TYPE,
|
|
output: this._activeResult,
|
|
input: queryContext.searchString,
|
|
}
|
|
);
|
|
result.suggestedIndex = lazy.UrlbarPrefs.get(
|
|
"unitConversion.suggestedIndex"
|
|
);
|
|
|
|
addCallback(this, result);
|
|
}
|
|
|
|
onEngagement(isPrivate, state, queryContext, details) {
|
|
let { result, element } = details;
|
|
if (result?.providerName == this.name) {
|
|
const { textContent } = element.querySelector(
|
|
".urlbarView-dynamic-unitConversion-output"
|
|
);
|
|
lazy.ClipboardHelper.copyString(textContent);
|
|
}
|
|
}
|
|
}
|
|
|
|
export const UrlbarProviderUnitConversion = new ProviderUnitConversion();
|