pdf_scripting_manager.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFScriptingManager = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. var _pdf = require("../pdf");
  29. class PDFScriptingManager {
  30. constructor({
  31. eventBus,
  32. sandboxBundleSrc = null,
  33. scriptingFactory = null,
  34. docPropertiesLookup = null
  35. }) {
  36. this._pdfDocument = null;
  37. this._pdfViewer = null;
  38. this._closeCapability = null;
  39. this._destroyCapability = null;
  40. this._scripting = null;
  41. this._ready = false;
  42. this._eventBus = eventBus;
  43. this._sandboxBundleSrc = sandboxBundleSrc;
  44. this._scriptingFactory = scriptingFactory;
  45. this._docPropertiesLookup = docPropertiesLookup;
  46. }
  47. setViewer(pdfViewer) {
  48. this._pdfViewer = pdfViewer;
  49. }
  50. async setDocument(pdfDocument) {
  51. if (this._pdfDocument) {
  52. await this._destroyScripting();
  53. }
  54. this._pdfDocument = pdfDocument;
  55. if (!pdfDocument) {
  56. return;
  57. }
  58. const [objects, calculationOrder, docActions] = await Promise.all([pdfDocument.getFieldObjects(), pdfDocument.getCalculationOrderIds(), pdfDocument.getJSActions()]);
  59. if (!objects && !docActions) {
  60. await this._destroyScripting();
  61. return;
  62. }
  63. if (pdfDocument !== this._pdfDocument) {
  64. return;
  65. }
  66. try {
  67. this._scripting = this._createScripting();
  68. } catch (error) {
  69. console.error(`PDFScriptingManager.setDocument: "${error?.message}".`);
  70. await this._destroyScripting();
  71. return;
  72. }
  73. this._internalEvents.set("updatefromsandbox", event => {
  74. if (event?.source !== window) {
  75. return;
  76. }
  77. this._updateFromSandbox(event.detail);
  78. });
  79. this._internalEvents.set("dispatcheventinsandbox", event => {
  80. this._scripting?.dispatchEventInSandbox(event.detail);
  81. });
  82. this._internalEvents.set("pagechanging", ({
  83. pageNumber,
  84. previous
  85. }) => {
  86. if (pageNumber === previous) {
  87. return;
  88. }
  89. this._dispatchPageClose(previous);
  90. this._dispatchPageOpen(pageNumber);
  91. });
  92. this._internalEvents.set("pagerendered", ({
  93. pageNumber
  94. }) => {
  95. if (!this._pageOpenPending.has(pageNumber)) {
  96. return;
  97. }
  98. if (pageNumber !== this._pdfViewer.currentPageNumber) {
  99. return;
  100. }
  101. this._dispatchPageOpen(pageNumber);
  102. });
  103. this._internalEvents.set("pagesdestroy", async event => {
  104. await this._dispatchPageClose(this._pdfViewer.currentPageNumber);
  105. await this._scripting?.dispatchEventInSandbox({
  106. id: "doc",
  107. name: "WillClose"
  108. });
  109. this._closeCapability?.resolve();
  110. });
  111. for (const [name, listener] of this._internalEvents) {
  112. this._eventBus._on(name, listener);
  113. }
  114. try {
  115. const docProperties = await this._getDocProperties();
  116. if (pdfDocument !== this._pdfDocument) {
  117. return;
  118. }
  119. await this._scripting.createSandbox({
  120. objects,
  121. calculationOrder,
  122. appInfo: {
  123. platform: navigator.platform,
  124. language: navigator.language
  125. },
  126. docInfo: {
  127. ...docProperties,
  128. actions: docActions
  129. }
  130. });
  131. this._eventBus.dispatch("sandboxcreated", {
  132. source: this
  133. });
  134. } catch (error) {
  135. console.error(`PDFScriptingManager.setDocument: "${error?.message}".`);
  136. await this._destroyScripting();
  137. return;
  138. }
  139. await this._scripting?.dispatchEventInSandbox({
  140. id: "doc",
  141. name: "Open"
  142. });
  143. await this._dispatchPageOpen(this._pdfViewer.currentPageNumber, true);
  144. Promise.resolve().then(() => {
  145. if (pdfDocument === this._pdfDocument) {
  146. this._ready = true;
  147. }
  148. });
  149. }
  150. async dispatchWillSave(detail) {
  151. return this._scripting?.dispatchEventInSandbox({
  152. id: "doc",
  153. name: "WillSave"
  154. });
  155. }
  156. async dispatchDidSave(detail) {
  157. return this._scripting?.dispatchEventInSandbox({
  158. id: "doc",
  159. name: "DidSave"
  160. });
  161. }
  162. async dispatchWillPrint(detail) {
  163. return this._scripting?.dispatchEventInSandbox({
  164. id: "doc",
  165. name: "WillPrint"
  166. });
  167. }
  168. async dispatchDidPrint(detail) {
  169. return this._scripting?.dispatchEventInSandbox({
  170. id: "doc",
  171. name: "DidPrint"
  172. });
  173. }
  174. get destroyPromise() {
  175. return this._destroyCapability?.promise || null;
  176. }
  177. get ready() {
  178. return this._ready;
  179. }
  180. get _internalEvents() {
  181. return (0, _pdf.shadow)(this, "_internalEvents", new Map());
  182. }
  183. get _pageOpenPending() {
  184. return (0, _pdf.shadow)(this, "_pageOpenPending", new Set());
  185. }
  186. get _visitedPages() {
  187. return (0, _pdf.shadow)(this, "_visitedPages", new Map());
  188. }
  189. async _updateFromSandbox(detail) {
  190. const isInPresentationMode = this._pdfViewer.isInPresentationMode || this._pdfViewer.isChangingPresentationMode;
  191. const {
  192. id,
  193. siblings,
  194. command,
  195. value
  196. } = detail;
  197. if (!id) {
  198. switch (command) {
  199. case "clear":
  200. console.clear();
  201. break;
  202. case "error":
  203. console.error(value);
  204. break;
  205. case "layout":
  206. {
  207. if (isInPresentationMode) {
  208. return;
  209. }
  210. const modes = (0, _ui_utils.apiPageLayoutToViewerModes)(value);
  211. this._pdfViewer.spreadMode = modes.spreadMode;
  212. break;
  213. }
  214. case "page-num":
  215. this._pdfViewer.currentPageNumber = value + 1;
  216. break;
  217. case "print":
  218. await this._pdfViewer.pagesPromise;
  219. this._eventBus.dispatch("print", {
  220. source: this
  221. });
  222. break;
  223. case "println":
  224. console.log(value);
  225. break;
  226. case "zoom":
  227. if (isInPresentationMode) {
  228. return;
  229. }
  230. this._pdfViewer.currentScaleValue = value;
  231. break;
  232. case "SaveAs":
  233. this._eventBus.dispatch("download", {
  234. source: this
  235. });
  236. break;
  237. case "FirstPage":
  238. this._pdfViewer.currentPageNumber = 1;
  239. break;
  240. case "LastPage":
  241. this._pdfViewer.currentPageNumber = this._pdfViewer.pagesCount;
  242. break;
  243. case "NextPage":
  244. this._pdfViewer.nextPage();
  245. break;
  246. case "PrevPage":
  247. this._pdfViewer.previousPage();
  248. break;
  249. case "ZoomViewIn":
  250. if (isInPresentationMode) {
  251. return;
  252. }
  253. this._pdfViewer.increaseScale();
  254. break;
  255. case "ZoomViewOut":
  256. if (isInPresentationMode) {
  257. return;
  258. }
  259. this._pdfViewer.decreaseScale();
  260. break;
  261. }
  262. return;
  263. }
  264. if (isInPresentationMode) {
  265. if (detail.focus) {
  266. return;
  267. }
  268. }
  269. delete detail.id;
  270. delete detail.siblings;
  271. const ids = siblings ? [id, ...siblings] : [id];
  272. for (const elementId of ids) {
  273. const element = document.querySelector(`[data-element-id="${elementId}"]`);
  274. if (element) {
  275. element.dispatchEvent(new CustomEvent("updatefromsandbox", {
  276. detail
  277. }));
  278. } else {
  279. this._pdfDocument?.annotationStorage.setValue(elementId, detail);
  280. }
  281. }
  282. }
  283. async _dispatchPageOpen(pageNumber, initialize = false) {
  284. const pdfDocument = this._pdfDocument,
  285. visitedPages = this._visitedPages;
  286. if (initialize) {
  287. this._closeCapability = (0, _pdf.createPromiseCapability)();
  288. }
  289. if (!this._closeCapability) {
  290. return;
  291. }
  292. const pageView = this._pdfViewer.getPageView(pageNumber - 1);
  293. if (pageView?.renderingState !== _ui_utils.RenderingStates.FINISHED) {
  294. this._pageOpenPending.add(pageNumber);
  295. return;
  296. }
  297. this._pageOpenPending.delete(pageNumber);
  298. const actionsPromise = (async () => {
  299. const actions = await (!visitedPages.has(pageNumber) ? pageView.pdfPage?.getJSActions() : null);
  300. if (pdfDocument !== this._pdfDocument) {
  301. return;
  302. }
  303. await this._scripting?.dispatchEventInSandbox({
  304. id: "page",
  305. name: "PageOpen",
  306. pageNumber,
  307. actions
  308. });
  309. })();
  310. visitedPages.set(pageNumber, actionsPromise);
  311. }
  312. async _dispatchPageClose(pageNumber) {
  313. const pdfDocument = this._pdfDocument,
  314. visitedPages = this._visitedPages;
  315. if (!this._closeCapability) {
  316. return;
  317. }
  318. if (this._pageOpenPending.has(pageNumber)) {
  319. return;
  320. }
  321. const actionsPromise = visitedPages.get(pageNumber);
  322. if (!actionsPromise) {
  323. return;
  324. }
  325. visitedPages.set(pageNumber, null);
  326. await actionsPromise;
  327. if (pdfDocument !== this._pdfDocument) {
  328. return;
  329. }
  330. await this._scripting?.dispatchEventInSandbox({
  331. id: "page",
  332. name: "PageClose",
  333. pageNumber
  334. });
  335. }
  336. async _getDocProperties() {
  337. if (this._docPropertiesLookup) {
  338. return this._docPropertiesLookup(this._pdfDocument);
  339. }
  340. throw new Error("_getDocProperties: Unable to lookup properties.");
  341. }
  342. _createScripting() {
  343. this._destroyCapability = (0, _pdf.createPromiseCapability)();
  344. if (this._scripting) {
  345. throw new Error("_createScripting: Scripting already exists.");
  346. }
  347. if (this._scriptingFactory) {
  348. return this._scriptingFactory.createScripting({
  349. sandboxBundleSrc: this._sandboxBundleSrc
  350. });
  351. }
  352. throw new Error("_createScripting: Cannot create scripting.");
  353. }
  354. async _destroyScripting() {
  355. if (!this._scripting) {
  356. this._pdfDocument = null;
  357. this._destroyCapability?.resolve();
  358. return;
  359. }
  360. if (this._closeCapability) {
  361. await Promise.race([this._closeCapability.promise, new Promise(resolve => {
  362. setTimeout(resolve, 1000);
  363. })]).catch(reason => {});
  364. this._closeCapability = null;
  365. }
  366. this._pdfDocument = null;
  367. try {
  368. await this._scripting.destroySandbox();
  369. } catch (ex) {}
  370. for (const [name, listener] of this._internalEvents) {
  371. this._eventBus._off(name, listener);
  372. }
  373. this._internalEvents.clear();
  374. this._pageOpenPending.clear();
  375. this._visitedPages.clear();
  376. this._scripting = null;
  377. this._ready = false;
  378. this._destroyCapability?.resolve();
  379. }
  380. }
  381. exports.PDFScriptingManager = PDFScriptingManager;