summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pdfjs/content/build/pdf.sandbox.external.sys.mjs
blob: 37647adcdae3ad2ce8d1cf5e204b831fef445172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright 2023 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

export class SandboxSupportBase {
 constructor(win) {
  this.win = win;
  this.timeoutIds = new Map();
  this.commFun = null;
 }
 destroy() {
  this.commFun = null;
  for (const id of this.timeoutIds.values()) {
   this.win.clearTimeout(id);
  }
  this.timeoutIds = null;
 }
 exportValueToSandbox(val) {
  throw new Error("Not implemented");
 }
 importValueFromSandbox(val) {
  throw new Error("Not implemented");
 }
 createErrorForSandbox(errorMessage) {
  throw new Error("Not implemented");
 }
 callSandboxFunction(name, args) {
  try {
   args = this.exportValueToSandbox(args);
   this.commFun(name, args);
  } catch (e) {
   this.win.console.error(e);
  }
 }
 createSandboxExternals() {
  const externals = {
   setTimeout: (callbackId, nMilliseconds) => {
    if (typeof callbackId !== "number" || typeof nMilliseconds !== "number") {
     return;
    }
    if (callbackId === 0) {
     this.win.clearTimeout(this.timeoutIds.get(callbackId));
    }
    const id = this.win.setTimeout(() => {
     this.timeoutIds.delete(callbackId);
     this.callSandboxFunction("timeoutCb", {
      callbackId,
      interval: false
     });
    }, nMilliseconds);
    this.timeoutIds.set(callbackId, id);
   },
   clearTimeout: callbackId => {
    this.win.clearTimeout(this.timeoutIds.get(callbackId));
    this.timeoutIds.delete(callbackId);
   },
   setInterval: (callbackId, nMilliseconds) => {
    if (typeof callbackId !== "number" || typeof nMilliseconds !== "number") {
     return;
    }
    const id = this.win.setInterval(() => {
     this.callSandboxFunction("timeoutCb", {
      callbackId,
      interval: true
     });
    }, nMilliseconds);
    this.timeoutIds.set(callbackId, id);
   },
   clearInterval: callbackId => {
    this.win.clearInterval(this.timeoutIds.get(callbackId));
    this.timeoutIds.delete(callbackId);
   },
   alert: cMsg => {
    if (typeof cMsg !== "string") {
     return;
    }
    this.win.alert(cMsg);
   },
   confirm: cMsg => {
    if (typeof cMsg !== "string") {
     return false;
    }
    return this.win.confirm(cMsg);
   },
   prompt: (cQuestion, cDefault) => {
    if (typeof cQuestion !== "string" || typeof cDefault !== "string") {
     return null;
    }
    return this.win.prompt(cQuestion, cDefault);
   },
   parseURL: cUrl => {
    const url = new this.win.URL(cUrl);
    const props = [
     "hash",
     "host",
     "hostname",
     "href",
     "origin",
     "password",
     "pathname",
     "port",
     "protocol",
     "search",
     "searchParams",
     "username"
    ];
    return Object.fromEntries(props.map(name => [
     name,
     url[name].toString()
    ]));
   },
   send: data => {
    if (!data) {
     return;
    }
    const event = new this.win.CustomEvent("updatefromsandbox", { detail: this.importValueFromSandbox(data) });
    this.win.dispatchEvent(event);
   }
  };
  Object.setPrototypeOf(externals, null);
  return (name, args) => {
   try {
    const result = externals[name](...args);
    return this.exportValueToSandbox(result);
   } catch (error) {
    throw this.createErrorForSandbox(error?.toString() ?? "");
   }
  };
 }
}