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