tor-browser

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

UrlbarProviderPrivateSearch.sys.mjs (3698B)


      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 returning a private search entry.
      7 */
      8 
      9 import {
     10  SkippableTimer,
     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  UrlbarTokenizer:
     22    "moz-src:///browser/components/urlbar/UrlbarTokenizer.sys.mjs",
     23 });
     24 
     25 /**
     26 * Class used to create the provider.
     27 */
     28 export class UrlbarProviderPrivateSearch extends UrlbarProvider {
     29  constructor() {
     30    super();
     31  }
     32 
     33  /**
     34   * @returns {Values<typeof UrlbarUtils.PROVIDER_TYPE>}
     35   */
     36  get type() {
     37    return UrlbarUtils.PROVIDER_TYPE.PROFILE;
     38  }
     39 
     40  /**
     41   * Whether this provider should be invoked for the given context.
     42   * If this method returns false, the providers manager won't start a query
     43   * with this provider, to save on resources.
     44   *
     45   * @param {UrlbarQueryContext} queryContext The query context object
     46   */
     47  async isActive(queryContext) {
     48    return (
     49      lazy.UrlbarSearchUtils.separatePrivateDefaultUIEnabled &&
     50      !queryContext.isPrivate &&
     51      !!queryContext.tokens.length
     52    );
     53  }
     54 
     55  /**
     56   * Starts querying.
     57   *
     58   * @param {UrlbarQueryContext} queryContext
     59   * @param {(provider: UrlbarProvider, result: UrlbarResult) => void} addCallback
     60   *   Callback invoked by the provider to add a new result.
     61   */
     62  async startQuery(queryContext, addCallback) {
     63    let searchString = queryContext.trimmedSearchString;
     64    if (
     65      queryContext.tokens.some(
     66        t => t.type == lazy.UrlbarTokenizer.TYPE.RESTRICT_SEARCH
     67      )
     68    ) {
     69      if (queryContext.tokens.length == 1) {
     70        // There's only the restriction token, bail out.
     71        return;
     72      }
     73      // Remove the restriction char from the search string.
     74      searchString = queryContext.tokens
     75        .filter(t => t.type != lazy.UrlbarTokenizer.TYPE.RESTRICT_SEARCH)
     76        .map(t => t.value)
     77        .join(" ");
     78    }
     79 
     80    let instance = this.queryInstance;
     81 
     82    let engine = queryContext.searchMode?.engineName
     83      ? Services.search.getEngineByName(queryContext.searchMode.engineName)
     84      : await Services.search.getDefaultPrivate();
     85    let isPrivateEngine =
     86      lazy.UrlbarSearchUtils.separatePrivateDefault &&
     87      engine != (await Services.search.getDefault());
     88    this.logger.info(`isPrivateEngine: ${isPrivateEngine}`);
     89 
     90    // This is a delay added before returning results, to avoid flicker.
     91    // Our result must appear only when all results are searches, but if search
     92    // results arrive first, then the muxer would insert our result and then
     93    // immediately remove it when non-search results arrive.
     94    await new SkippableTimer({
     95      name: "ProviderPrivateSearch",
     96      time: 100,
     97      logger: this.logger,
     98    }).promise;
     99 
    100    let icon = await engine.getIconURL();
    101    if (instance != this.queryInstance) {
    102      return;
    103    }
    104 
    105    let result = new lazy.UrlbarResult({
    106      type: UrlbarUtils.RESULT_TYPE.SEARCH,
    107      source: UrlbarUtils.RESULT_SOURCE.SEARCH,
    108      suggestedIndex: 1,
    109      payload: {
    110        engine: engine.name,
    111        query: searchString,
    112        title: searchString,
    113        icon,
    114        inPrivateWindow: true,
    115        isPrivateEngine,
    116      },
    117      highlights: {
    118        engine: UrlbarUtils.HIGHLIGHT.TYPED,
    119      },
    120    });
    121    addCallback(this, result);
    122  }
    123 }