Commit 90c94237 authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame 🎃
Browse files

Bug 1993166 - Improve origin attributes on opensearch. r=Standard8,urlbar-reviewers

parent 07a11864
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -286,10 +286,22 @@ class ProviderContextualSearch extends ActionsProvider {
    let { type, engine } = this.#resultEngine;

    if (type == OPEN_SEARCH_ENGINE) {
      let originAttributes;
      try {
        let currentURI = Services.io.newURI(queryContext.currentPage);
        originAttributes = {
          firstPartyDomain: Services.eTLD.getSchemelessSite(currentURI),
        };
      } catch {}
      let openSearchEngineData = await lazy.loadAndParseOpenSearchEngine(
        Services.io.newURI(engine.uri)
        Services.io.newURI(engine.uri),
        null,
        originAttributes
      );
      engine = new lazy.OpenSearchEngine({ engineData: openSearchEngineData });
      engine = new lazy.OpenSearchEngine({
        engineData: openSearchEngineData,
        originAttributes,
      });
    }

    this.#performSearch(
+16 −8
Original line number Diff line number Diff line
@@ -56,6 +56,8 @@ export class OpenSearchEngine extends SearchEngine {
   * @param {string} [options.faviconURL]
   *   The website favicon, to be used if the engine data hasn't specified an
   *   icon.
   * @param {object} [options.originAttributes]
   *   The origin attributes to use to download additional resources.
   */
  constructor(options = {}) {
    super({
@@ -68,7 +70,10 @@ export class OpenSearchEngine extends SearchEngine {
    });

    if (options.faviconURL) {
      this._setIcon(options.faviconURL, undefined, false).catch(e =>
      this._setIcon(options.faviconURL, {
        override: false,
        originAttributes: options.originAttributes,
      }).catch(e =>
        lazy.logConsole.error(
          `Error while setting icon for search engine ${options.engineData.name}:`,
          e.message
@@ -77,7 +82,7 @@ export class OpenSearchEngine extends SearchEngine {
    }

    if (options.engineData) {
      this.#setEngineData(options.engineData);
      this.#setEngineData(options.engineData, options.originAttributes);

      // As this is a new engine, we must set the verification hash for the load
      // path set in the constructor.
@@ -189,8 +194,10 @@ export class OpenSearchEngine extends SearchEngine {
   *
   * @param {OpenSearchProperties} data
   *   The OpenSearch data.
   * @param {object} originAttributes
   *   The origin attributes for any additional downloads
   */
  #setEngineData(data) {
  #setEngineData(data, originAttributes) {
    let name = data.name.trim();
    if (Services.search.getEngineByName(name)) {
      throw Components.Exception(
@@ -258,7 +265,8 @@ export class OpenSearchEngine extends SearchEngine {
    }

    for (let image of data.images) {
      this._setIcon(image.url, image.size).catch(e =>
      this._setIcon(image.url, { size: image.size, originAttributes }).catch(
        e =>
          lazy.logConsole.error(
            `Error while setting icon for search engine ${data.name}:`,
            e.message
+25 −8
Original line number Diff line number Diff line
@@ -585,15 +585,19 @@ export class SearchEngine {
   * @param {string} iconURL
   *   A URI string pointing to the engine's icon.
   *   Must have http[s], data, or moz-extension protocol.
   * @param {number} [size]
   * @param {object} options
   *   The options object
   * @param {number} [options.size]
   *   Width and height of the icon (determined automatically if not provided).
   * @param {boolean} [override]
   * @param {boolean} [options.override]
   * Whether the new URI should override an existing one.
   * @param {object} [options.originAttributes]
   *   The origin attributes to use to load the icon.
   * @returns {Promise<void>}
   *   Resolves when the icon was set.
   *   Rejects with an Error if there was an error.
   */
  async _setIcon(iconURL, size, override = true) {
  async _setIcon(iconURL, options = { override: true }) {
    lazy.logConsole.debug(
      "_setIcon: Setting icon url for",
      this.name,
@@ -601,8 +605,12 @@ export class SearchEngine {
      limitURILength(iconURL)
    );

    [iconURL, size] = await this._downloadAndRescaleIcon(iconURL, size);
    this._addIconToMap(iconURL, size, override);
    let size;
    [iconURL, size] = await this._downloadAndRescaleIcon(iconURL, {
      size: options.size,
      originAttributes: options.originAttributes,
    });
    this._addIconToMap(iconURL, size, options.override);

    if (this._engineAddedToStore) {
      lazy.SearchUtils.notifyAction(
@@ -620,18 +628,24 @@ export class SearchEngine {
   * @param {string} iconURL
   *   A URI string pointing to the engine's icon.
   *   Must have http[s], data, or moz-extension protocol.
   * @param {number} [size]
   * @param {object} options
   *   The options object
   * @param {number} [options.size]
   *   Width and height of the icon (determined automatically if not provided).
   * @param {object} [options.originAttributes]
   *   The origin attributes to use to load the icon.
   * @returns {Promise<[string, number]>}
   *   Resolves to [dataURL, size] if successful and rejects if there was an error.
   */
  async _downloadAndRescaleIcon(iconURL, size) {
  async _downloadAndRescaleIcon(iconURL, options = {}) {
    let uri = lazy.SearchUtils.makeURI(iconURL);

    if (!uri) {
      throw new Error(`Invalid URI`);
    }

    let size = options.size;

    switch (uri.scheme) {
      case "moz-extension": {
        if (!size) {
@@ -644,7 +658,10 @@ export class SearchEngine {
      case "data":
      case "http":
      case "https": {
        let [byteArray, contentType] = await lazy.SearchUtils.fetchIcon(uri);
        let [byteArray, contentType] = await lazy.SearchUtils.fetchIcon(
          uri,
          options.originAttributes
        );
        if (byteArray.length > lazy.SearchUtils.MAX_ICON_SIZE) {
          lazy.logConsole.debug(
            `Rescaling icon for search engine ${this.name}.`
+5 −1
Original line number Diff line number Diff line
@@ -769,7 +769,11 @@ export class SearchService {
        null,
        originAttributes
      );
      engine = new lazy.OpenSearchEngine({ engineData, faviconURL: iconURL });
      engine = new lazy.OpenSearchEngine({
        engineData,
        faviconURL: iconURL,
        originAttributes,
      });
    } catch (ex) {
      throw Components.Exception(
        "addEngine: Error adding engine:\n" + ex,
+8 −2
Original line number Diff line number Diff line
@@ -511,13 +511,19 @@ export var SearchUtils = {
   *
   * @param {string|nsIURI} uri
   *  The URI to the icon.
   * @param {object} [originAttributes]
   *   The origin attributes to download the icon.
   * @returns {Promise<[Uint8Array, string]>}
   *   Resolves to an array containing the data and the mime type.
   *   Rejects if the icon cannot be fetched.
   */
  async fetchIcon(uri) {
  async fetchIcon(uri, originAttributes = null) {
    return new Promise((resolve, reject) => {
      let chan = SearchUtils.makeChannel(uri, Ci.nsIContentPolicy.TYPE_IMAGE);
      let chan = SearchUtils.makeChannel(
        uri,
        Ci.nsIContentPolicy.TYPE_IMAGE,
        originAttributes
      );
      let listener = new SearchUtils.LoadListener(
        chan,
        /^image\//,