dom/manifest/ManifestFinder.jsm
author Marcos Caceres <marcos@marcosc.com>
Thu, 30 Jul 2015 11:56:12 -0400
changeset 287130 7692b5a9d0c57bc9f761daf7780117cdd5c2e0a8
parent 287096 4b328a6f7448002a7f0e4980a0bbae56a0f0b019
child 414181 b0dd91a6a1fd347587ecaef9f81c04de937d7f31
permissions -rw-r--r--
Bug 1171200 - Add means of checking if a document links to a manifest. r=billm

/* 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 {
  utils: Cu
} = Components;
Cu.import("resource://gre/modules/PromiseMessage.jsm");
Cu.import("resource://gre/modules/Task.jsm");

this.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}
  */
  browserHasManifestLink: Task.async(
    function* (aBrowser) {
      if (!isXULBrowser(aBrowser)) {
        throw new TypeError("Invalid input.");
      }
      const msgKey = "DOM:WebManifest:hasManifestLink";
      const mm = aBrowser.messageManager;
      const reply = yield 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;
}

this.EXPORTED_SYMBOLS = [// jshint ignore:line
  "ManifestFinder"
];