Backed out 7 changesets (bug 1942424) for causing frequent crashes. a=backout
[gecko.git] / toolkit / components / pdfjs / content / build / pdf.sandbox.external.sys.mjs
blobb8f5ede978dac0a9f6aa087e188b3254a1531f48
1 /* Copyright 2024 Mozilla Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
16 export class SandboxSupportBase {
17   constructor(win) {
18     this.win = win;
19     this.timeoutIds = new Map();
20     this.commFun = null;
21   }
22   destroy() {
23     this.commFun = null;
24     for (const id of this.timeoutIds.values()) {
25       this.win.clearTimeout(id);
26     }
27     this.timeoutIds = null;
28   }
29   exportValueToSandbox(val) {
30     throw new Error("Not implemented");
31   }
32   importValueFromSandbox(val) {
33     throw new Error("Not implemented");
34   }
35   createErrorForSandbox(errorMessage) {
36     throw new Error("Not implemented");
37   }
38   callSandboxFunction(name, args) {
39     if (!this.commFun) {
40       return;
41     }
42     try {
43       args = this.exportValueToSandbox(args);
44       this.commFun(name, args);
45     } catch (e) {
46       this.win.console.error(e);
47     }
48   }
49   createSandboxExternals() {
50     const externals = {
51       setTimeout: (callbackId, nMilliseconds) => {
52         if (typeof callbackId !== "number" || typeof nMilliseconds !== "number") {
53           return;
54         }
55         if (callbackId === 0) {
56           this.win.clearTimeout(this.timeoutIds.get(callbackId));
57         }
58         const id = this.win.setTimeout(() => {
59           this.timeoutIds.delete(callbackId);
60           this.callSandboxFunction("timeoutCb", {
61             callbackId,
62             interval: false
63           });
64         }, nMilliseconds);
65         this.timeoutIds.set(callbackId, id);
66       },
67       clearTimeout: callbackId => {
68         this.win.clearTimeout(this.timeoutIds.get(callbackId));
69         this.timeoutIds.delete(callbackId);
70       },
71       setInterval: (callbackId, nMilliseconds) => {
72         if (typeof callbackId !== "number" || typeof nMilliseconds !== "number") {
73           return;
74         }
75         const id = this.win.setInterval(() => {
76           this.callSandboxFunction("timeoutCb", {
77             callbackId,
78             interval: true
79           });
80         }, nMilliseconds);
81         this.timeoutIds.set(callbackId, id);
82       },
83       clearInterval: callbackId => {
84         this.win.clearInterval(this.timeoutIds.get(callbackId));
85         this.timeoutIds.delete(callbackId);
86       },
87       alert: cMsg => {
88         if (typeof cMsg !== "string") {
89           return;
90         }
91         this.win.alert(cMsg);
92       },
93       confirm: cMsg => {
94         if (typeof cMsg !== "string") {
95           return false;
96         }
97         return this.win.confirm(cMsg);
98       },
99       prompt: (cQuestion, cDefault) => {
100         if (typeof cQuestion !== "string" || typeof cDefault !== "string") {
101           return null;
102         }
103         return this.win.prompt(cQuestion, cDefault);
104       },
105       parseURL: cUrl => {
106         const url = new this.win.URL(cUrl);
107         const props = ["hash", "host", "hostname", "href", "origin", "password", "pathname", "port", "protocol", "search", "searchParams", "username"];
108         return Object.fromEntries(props.map(name => [name, url[name].toString()]));
109       },
110       send: data => {
111         if (!data) {
112           return;
113         }
114         const event = new this.win.CustomEvent("updatefromsandbox", {
115           detail: this.importValueFromSandbox(data)
116         });
117         this.win.dispatchEvent(event);
118       }
119     };
120     Object.setPrototypeOf(externals, null);
121     return (name, args) => {
122       try {
123         const result = externals[name](...args);
124         return this.exportValueToSandbox(result);
125       } catch (error) {
126         throw this.createErrorForSandbox(error?.toString() ?? "");
127       }
128     };
129   }