ManifestMessagesChild.sys.mjs (3229B)
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 http://mozilla.org/MPL/2.0/.*/ 4 /* 5 * Manifest obtainer frame script implementation of: 6 * http://www.w3.org/TR/appmanifest/#obtaining 7 * 8 * It searches a top-level browsing context for 9 * a <link rel=manifest> element. Then fetches 10 * and processes the linked manifest. 11 * 12 * BUG: https://bugzilla.mozilla.org/show_bug.cgi?id=1083410 13 */ 14 15 const lazy = {}; 16 17 ChromeUtils.defineESModuleGetters(lazy, { 18 ManifestFinder: "resource://gre/modules/ManifestFinder.sys.mjs", 19 ManifestIcons: "resource://gre/modules/ManifestIcons.sys.mjs", 20 ManifestObtainer: "resource://gre/modules/ManifestObtainer.sys.mjs", 21 }); 22 23 export class ManifestMessagesChild extends JSWindowActorChild { 24 receiveMessage(message) { 25 switch (message.name) { 26 case "DOM:WebManifest:hasManifestLink": 27 return this.hasManifestLink(); 28 case "DOM:ManifestObtainer:Obtain": 29 return this.obtainManifest(message.data); 30 case "DOM:WebManifest:fetchIcon": 31 return this.fetchIcon(message); 32 } 33 return undefined; 34 } 35 36 /** 37 * Check if the document includes a link to a web manifest. 38 */ 39 hasManifestLink() { 40 const response = makeMsgResponse(); 41 response.result = lazy.ManifestFinder.contentHasManifestLink( 42 this.contentWindow 43 ); 44 response.success = true; 45 return response; 46 } 47 48 /** 49 * Asynchronously obtains a web manifest from this window by using the 50 * ManifestObtainer and returns the result. 51 * 52 * @param {object} checkConformance True if spec conformance messages should be collected. 53 */ 54 async obtainManifest(options) { 55 const { checkConformance } = options; 56 const response = makeMsgResponse(); 57 try { 58 response.result = await lazy.ManifestObtainer.contentObtainManifest( 59 this.contentWindow, 60 { checkConformance } 61 ); 62 response.success = true; 63 } catch (err) { 64 response.result = serializeError(err); 65 } 66 return response; 67 } 68 69 /** 70 * Given a manifest and an expected icon size, ask ManifestIcons 71 * to fetch the appropriate icon and send along result 72 */ 73 async fetchIcon({ data: { manifest, iconSize } }) { 74 const response = makeMsgResponse(); 75 try { 76 response.result = await lazy.ManifestIcons.contentFetchIcon( 77 this.contentWindow, 78 manifest, 79 iconSize 80 ); 81 response.success = true; 82 } catch (err) { 83 response.result = serializeError(err); 84 } 85 return response; 86 } 87 } 88 89 /** 90 * Utility function to Serializes an JS Error, so it can be transferred over 91 * the message channel. 92 * FIX ME: https://bugzilla.mozilla.org/show_bug.cgi?id=1172586 93 * 94 * @param {Error} aError The error to serialize. 95 * @return {object} The serialized object. 96 */ 97 function serializeError(aError) { 98 const clone = { 99 fileName: aError.fileName, 100 lineNumber: aError.lineNumber, 101 columnNumber: aError.columnNumber, 102 stack: aError.stack, 103 message: aError.message, 104 name: aError.name, 105 }; 106 return clone; 107 } 108 109 function makeMsgResponse() { 110 return { 111 success: false, 112 result: undefined, 113 }; 114 }