dom/manifest/ManifestFinder.jsm
author J.C. Jones <jjones@mozilla.com>
Wed, 12 Jun 2019 23:22:22 +0000
changeset 478563 547109ab8d7bb037fa42996e1837f7d231b5d615
parent 455795 6b56696d713a7f7858f16235e37baa8307e73b49
child 481377 f3b422727b92f4933ca41fa7a4013d829a98240d
permissions -rw-r--r--
Bug 1558977 - Upgrade NSS 3.45 r=keeler Differential Revision: https://phabricator.services.mozilla.com/D34784

/* 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 https://mozilla.org/MPL/2.0/. */
/* globals Components, Task, PromiseMessage */
"use strict";

const {PromiseMessage} = ChromeUtils.import("resource://gre/modules/PromiseMessage.jsm");

var ManifestFinder = {// jshint ignore:line
  /**
  * Check from content process if DOM Window has a conforming
  * manifest link relationship.
  * @param aContent DOM Window to check.
  * @return {Promise<Boolean>}
  */
  contentHasManifestLink(aContent) {
    if (!aContent || isXULBrowser(aContent)) {
      throw new TypeError("Invalid input.");
    }
    return checkForManifest(aContent);
  },

  /**
  * Check from a XUL browser (parent process) if it's content document has a
  * manifest link relationship.
  * @param aBrowser The XUL browser to check.
  * @return {Promise}
  */
  async browserHasManifestLink(aBrowser) {
      if (!isXULBrowser(aBrowser)) {
        throw new TypeError("Invalid input.");
      }
      const msgKey = "DOM:WebManifest:hasManifestLink";
      const mm = aBrowser.messageManager;
      const reply = await PromiseMessage.send(mm, msgKey);
      return reply.data.result;
    },
};

function isXULBrowser(aBrowser) {
  if (!aBrowser || !aBrowser.namespaceURI || !aBrowser.localName) {
    return false;
  }
  const XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  return (aBrowser.namespaceURI === XUL && aBrowser.localName === "browser");
}

function checkForManifest(aWindow) {
  // Only top-level browsing contexts are valid.
  if (!aWindow || aWindow.top !== aWindow) {
    return false;
  }
  const elem = aWindow.document.querySelector("link[rel~='manifest']");
  // Only if we have an element and a non-empty href attribute.
  if (!elem || !elem.getAttribute("href")) {
    return false;
  }
  return true;
}

var EXPORTED_SYMBOLS = [// jshint ignore:line
  "ManifestFinder",
];