UrlbarProviderAliasEngines.sys.mjs (2452B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * This module exports a provider that offers engines with aliases as heuristic 7 * results. 8 */ 9 10 import { 11 UrlbarProvider, 12 UrlbarUtils, 13 } from "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs"; 14 15 const lazy = {}; 16 17 ChromeUtils.defineESModuleGetters(lazy, { 18 UrlbarResult: "moz-src:///browser/components/urlbar/UrlbarResult.sys.mjs", 19 UrlbarSearchUtils: 20 "moz-src:///browser/components/urlbar/UrlbarSearchUtils.sys.mjs", 21 }); 22 23 /** 24 * Class used to create the provider. 25 */ 26 export class UrlbarProviderAliasEngines extends UrlbarProvider { 27 /** 28 * @returns {Values<typeof UrlbarUtils.PROVIDER_TYPE>} 29 */ 30 get type() { 31 return UrlbarUtils.PROVIDER_TYPE.HEURISTIC; 32 } 33 34 /** 35 * Whether this provider should be invoked for the given context. 36 * If this method returns false, the providers manager won't start a query 37 * with this provider, to save on resources. 38 * 39 * @param {UrlbarQueryContext} queryContext The query context object 40 */ 41 async isActive(queryContext) { 42 return ( 43 (!queryContext.restrictSource || 44 queryContext.restrictSource == UrlbarUtils.RESULT_SOURCE.SEARCH) && 45 !queryContext.searchMode && 46 !!queryContext.tokens.length 47 ); 48 } 49 50 /** 51 * Starts querying. 52 * 53 * @param {UrlbarQueryContext} queryContext 54 * @param {(provider: UrlbarProvider, result: UrlbarResult) => void} addCallback 55 * Callback invoked by the provider to add a new result. 56 */ 57 async startQuery(queryContext, addCallback) { 58 let instance = this.queryInstance; 59 let alias = queryContext.tokens[0]?.value; 60 let engine = await lazy.UrlbarSearchUtils.engineForAlias( 61 alias, 62 queryContext.searchString 63 ); 64 let icon = await engine?.getIconURL(); 65 if (!engine || instance != this.queryInstance) { 66 return; 67 } 68 let query = UrlbarUtils.substringAfter( 69 queryContext.searchString, 70 alias 71 ).trimStart(); 72 let result = new lazy.UrlbarResult({ 73 type: UrlbarUtils.RESULT_TYPE.SEARCH, 74 source: UrlbarUtils.RESULT_SOURCE.SEARCH, 75 heuristic: true, 76 payload: { 77 engine: engine.name, 78 keyword: alias, 79 query, 80 title: query, 81 icon, 82 }, 83 }); 84 addCallback(this, result); 85 } 86 }