ManifestFinder.jsm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 https://mozilla.org/MPL/2.0/. */
  4. /* globals Components, Task, PromiseMessage */
  5. "use strict";
  6. const {
  7. utils: Cu
  8. } = Components;
  9. Cu.import("resource://gre/modules/PromiseMessage.jsm");
  10. Cu.import("resource://gre/modules/Task.jsm");
  11. this.ManifestFinder = {// jshint ignore:line
  12. /**
  13. * Check from content process if DOM Window has a conforming
  14. * manifest link relationship.
  15. * @param aContent DOM Window to check.
  16. * @return {Promise<Boolean>}
  17. */
  18. contentHasManifestLink(aContent) {
  19. if (!aContent || isXULBrowser(aContent)) {
  20. throw new TypeError("Invalid input.");
  21. }
  22. return checkForManifest(aContent);
  23. },
  24. /**
  25. * Check from a XUL browser (parent process) if it's content document has a
  26. * manifest link relationship.
  27. * @param aBrowser The XUL browser to check.
  28. * @return {Promise}
  29. */
  30. browserHasManifestLink: Task.async(
  31. function* (aBrowser) {
  32. if (!isXULBrowser(aBrowser)) {
  33. throw new TypeError("Invalid input.");
  34. }
  35. const msgKey = "DOM:WebManifest:hasManifestLink";
  36. const mm = aBrowser.messageManager;
  37. const reply = yield PromiseMessage.send(mm, msgKey);
  38. return reply.data.result;
  39. }
  40. )
  41. };
  42. function isXULBrowser(aBrowser) {
  43. if (!aBrowser || !aBrowser.namespaceURI || !aBrowser.localName) {
  44. return false;
  45. }
  46. const XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  47. return (aBrowser.namespaceURI === XUL && aBrowser.localName === "browser");
  48. }
  49. function checkForManifest(aWindow) {
  50. // Only top-level browsing contexts are valid.
  51. if (!aWindow || aWindow.top !== aWindow) {
  52. return false;
  53. }
  54. const elem = aWindow.document.querySelector("link[rel~='manifest']");
  55. // Only if we have an element and a non-empty href attribute.
  56. if (!elem || !elem.getAttribute("href")) {
  57. return false;
  58. }
  59. return true;
  60. }
  61. this.EXPORTED_SYMBOLS = [// jshint ignore:line
  62. "ManifestFinder"
  63. ];