ManifestFinder.sys.mjs (1806B)
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 5 export var ManifestFinder = { 6 /** 7 * Check from content process if DOM Window has a conforming 8 * manifest link relationship. 9 * 10 * @param aContent DOM Window to check. 11 * @return {Promise<boolean>} 12 */ 13 contentHasManifestLink(aContent) { 14 if (!aContent || isXULBrowser(aContent)) { 15 throw new TypeError("Invalid input."); 16 } 17 return checkForManifest(aContent); 18 }, 19 20 /** 21 * Check from a XUL browser (parent process) if it's content document has a 22 * manifest link relationship. 23 * 24 * @param aBrowser The XUL browser to check. 25 * @return {Promise} 26 */ 27 async browserHasManifestLink(aBrowser) { 28 if (!isXULBrowser(aBrowser)) { 29 throw new TypeError("Invalid input."); 30 } 31 32 const actor = 33 aBrowser.browsingContext.currentWindowGlobal.getActor("ManifestMessages"); 34 const reply = await actor.sendQuery("DOM:WebManifest:hasManifestLink"); 35 return reply.result; 36 }, 37 }; 38 39 function isXULBrowser(aBrowser) { 40 if (!aBrowser || !aBrowser.namespaceURI || !aBrowser.localName) { 41 return false; 42 } 43 const XUL_NS = 44 "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; 45 return aBrowser.namespaceURI === XUL_NS && aBrowser.localName === "browser"; 46 } 47 48 function checkForManifest(aWindow) { 49 // Only top-level browsing contexts are valid. 50 if (!aWindow || aWindow.top !== aWindow) { 51 return false; 52 } 53 const elem = aWindow.document.querySelector("link[rel~='manifest']"); 54 // Only if we have an element and a non-empty href attribute. 55 if (!elem || !elem.getAttribute("href")) { 56 return false; 57 } 58 return true; 59 }