Bug 1886946: Remove incorrect assertion that buffer is not-pinned. r=sfink
[gecko.git] / dom / ipc / ManifestMessagesChild.sys.mjs
blob5a42fd32069ab1852a1dbf90f2c65e41640488e1
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  */
15 const lazy = {};
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 });
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   }
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   }
48   /**
49    * Asynchronously obtains a web manifest from this window by using the
50    * ManifestObtainer and returns the result.
51    * @param {Object} checkConformance True if spec conformance messages should be collected.
52    */
53   async obtainManifest(options) {
54     const { checkConformance } = options;
55     const response = makeMsgResponse();
56     try {
57       response.result = await lazy.ManifestObtainer.contentObtainManifest(
58         this.contentWindow,
59         { checkConformance }
60       );
61       response.success = true;
62     } catch (err) {
63       response.result = serializeError(err);
64     }
65     return response;
66   }
68   /**
69    * Given a manifest and an expected icon size, ask ManifestIcons
70    * to fetch the appropriate icon and send along result
71    */
72   async fetchIcon({ data: { manifest, iconSize } }) {
73     const response = makeMsgResponse();
74     try {
75       response.result = await lazy.ManifestIcons.contentFetchIcon(
76         this.contentWindow,
77         manifest,
78         iconSize
79       );
80       response.success = true;
81     } catch (err) {
82       response.result = serializeError(err);
83     }
84     return response;
85   }
88 /**
89  * Utility function to Serializes an JS Error, so it can be transferred over
90  * the message channel.
91  * FIX ME: https://bugzilla.mozilla.org/show_bug.cgi?id=1172586
92  * @param  {Error} aError The error to serialize.
93  * @return {Object} The serialized object.
94  */
95 function serializeError(aError) {
96   const clone = {
97     fileName: aError.fileName,
98     lineNumber: aError.lineNumber,
99     columnNumber: aError.columnNumber,
100     stack: aError.stack,
101     message: aError.message,
102     name: aError.name,
103   };
104   return clone;
107 function makeMsgResponse() {
108   return {
109     success: false,
110     result: undefined,
111   };