tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SuggestBackendMerino.sys.mjs (2232B)


      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 import { SuggestBackend } from "moz-src:///browser/components/urlbar/private/SuggestFeature.sys.mjs";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineESModuleGetters(lazy, {
     10  MerinoClient: "moz-src:///browser/components/urlbar/MerinoClient.sys.mjs",
     11 });
     12 
     13 /**
     14 * @import {MerinoClient} from "moz-src:///browser/components/urlbar/MerinoClient.sys.mjs"
     15 */
     16 
     17 /**
     18 * The Suggest Merino backend. This backend is enabled when online Suggest is
     19 * available to the user and enabled.
     20 */
     21 export class SuggestBackendMerino extends SuggestBackend {
     22  get enablingPreferences() {
     23    return ["quickSuggestOnlineAvailable", "quicksuggest.online.enabled"];
     24  }
     25 
     26  /**
     27   * @returns {MerinoClient}
     28   *   The Merino client. The client is created lazily and isn't kept around
     29   *   when the backend is disabled, so this may return null.
     30   */
     31  get client() {
     32    return this.#client;
     33  }
     34 
     35  async enable(enabled) {
     36    if (!enabled) {
     37      this.#client = null;
     38    }
     39  }
     40 
     41  async query(searchString, { queryContext }) {
     42    if (!queryContext.allowRemoteResults()) {
     43      return [];
     44    }
     45 
     46    this.logger.debug("Handling query", { searchString });
     47 
     48    if (!this.#client) {
     49      this.#client = new lazy.MerinoClient(this.name, { allowOhttp: true });
     50    }
     51 
     52    let suggestions = await this.#client.fetch({
     53      query: searchString,
     54    });
     55 
     56    this.logger.debug("Got suggestions", suggestions);
     57 
     58    return suggestions;
     59  }
     60 
     61  cancelQuery() {
     62    // Cancel the Merino timeout timer so it doesn't fire and record a timeout.
     63    // If it's already canceled or has fired, this is a no-op.
     64    this.#client?.cancelTimeoutTimer();
     65 
     66    // Don't abort the Merino fetch if one is ongoing. By design we allow
     67    // fetches to finish so we can record their latency.
     68  }
     69 
     70  onSearchSessionEnd(_queryContext, _controller, _details) {
     71    // Reset the Merino session ID when a session ends. By design for the user's
     72    // privacy, we don't keep it around between engagements.
     73    this.#client?.resetSession();
     74  }
     75 
     76  // `MerinoClient`
     77  #client = null;
     78 }