2
0

pdf_scripting_manager.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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 _pdf = require("../pdf");
  28. var _ui_utils = require("./ui_utils.js");
  29. var _pdf_rendering_queue = require("./pdf_rendering_queue.js");
  30. class PDFScriptingManager {
  31. constructor({
  32. eventBus,
  33. sandboxBundleSrc = null,
  34. scriptingFactory = null,
  35. docPropertiesLookup = null
  36. }) {
  37. this._pdfDocument = null;
  38. this._pdfViewer = null;
  39. this._closeCapability = null;
  40. this._destroyCapability = null;
  41. this._scripting = null;
  42. this._mouseState = Object.create(null);
  43. this._pageEventsReady = false;
  44. this._ready = false;
  45. this._eventBus = eventBus;
  46. this._sandboxBundleSrc = sandboxBundleSrc;
  47. this._scriptingFactory = scriptingFactory;
  48. this._docPropertiesLookup = docPropertiesLookup;
  49. }
  50. setViewer(pdfViewer) {
  51. this._pdfViewer = pdfViewer;
  52. }
  53. async setDocument(pdfDocument) {
  54. if (this._pdfDocument) {
  55. await this._destroyScripting();
  56. }
  57. this._pdfDocument = pdfDocument;
  58. if (!pdfDocument) {
  59. return;
  60. }
  61. const [objects, calculationOrder, docActions] = await Promise.all([pdfDocument.getFieldObjects(), pdfDocument.getCalculationOrderIds(), pdfDocument.getJSActions()]);
  62. if (!objects && !docActions) {
  63. await this._destroyScripting();
  64. return;
  65. }
  66. if (pdfDocument !== this._pdfDocument) {
  67. return;
  68. }
  69. this._scripting = this._createScripting();
  70. this._internalEvents.set("updatefromsandbox", event => {
  71. if (event?.source !== window) {
  72. return;
  73. }
  74. this._updateFromSandbox(event.detail);
  75. });
  76. this._internalEvents.set("dispatcheventinsandbox", event => {
  77. this._scripting?.dispatchEventInSandbox(event.detail);
  78. });
  79. this._internalEvents.set("pagechanging", ({
  80. pageNumber,
  81. previous
  82. }) => {
  83. if (pageNumber === previous) {
  84. return;
  85. }
  86. this._dispatchPageClose(previous);
  87. this._dispatchPageOpen(pageNumber);
  88. });
  89. this._internalEvents.set("pagerendered", ({
  90. pageNumber
  91. }) => {
  92. if (!this._pageOpenPending.has(pageNumber)) {
  93. return;
  94. }
  95. if (pageNumber !== this._pdfViewer.currentPageNumber) {
  96. return;
  97. }
  98. this._dispatchPageOpen(pageNumber);
  99. });
  100. this._internalEvents.set("pagesdestroy", async event => {
  101. await this._dispatchPageClose(this._pdfViewer.currentPageNumber);
  102. await this._scripting?.dispatchEventInSandbox({
  103. id: "doc",
  104. name: "WillClose"
  105. });
  106. this._closeCapability?.resolve();
  107. });
  108. this._domEvents.set("mousedown", event => {
  109. this._mouseState.isDown = true;
  110. });
  111. this._domEvents.set("mouseup", event => {
  112. this._mouseState.isDown = false;
  113. });
  114. for (const [name, listener] of this._internalEvents) {
  115. this._eventBus._on(name, listener);
  116. }
  117. for (const [name, listener] of this._domEvents) {
  118. window.addEventListener(name, listener);
  119. }
  120. try {
  121. const docProperties = await this._getDocProperties();
  122. if (pdfDocument !== this._pdfDocument) {
  123. return;
  124. }
  125. await this._scripting.createSandbox({
  126. objects,
  127. calculationOrder,
  128. appInfo: {
  129. platform: navigator.platform,
  130. language: navigator.language
  131. },
  132. docInfo: { ...docProperties,
  133. actions: docActions
  134. }
  135. });
  136. this._eventBus.dispatch("sandboxcreated", {
  137. source: this
  138. });
  139. } catch (error) {
  140. console.error(`PDFScriptingManager.setDocument: "${error?.message}".`);
  141. await this._destroyScripting();
  142. return;
  143. }
  144. await this._scripting?.dispatchEventInSandbox({
  145. id: "doc",
  146. name: "Open"
  147. });
  148. await this._dispatchPageOpen(this._pdfViewer.currentPageNumber, true);
  149. Promise.resolve().then(() => {
  150. if (pdfDocument === this._pdfDocument) {
  151. this._ready = true;
  152. }
  153. });
  154. }
  155. async dispatchWillSave(detail) {
  156. return this._scripting?.dispatchEventInSandbox({
  157. id: "doc",
  158. name: "WillSave"
  159. });
  160. }
  161. async dispatchDidSave(detail) {
  162. return this._scripting?.dispatchEventInSandbox({
  163. id: "doc",
  164. name: "DidSave"
  165. });
  166. }
  167. async dispatchWillPrint(detail) {
  168. return this._scripting?.dispatchEventInSandbox({
  169. id: "doc",
  170. name: "WillPrint"
  171. });
  172. }
  173. async dispatchDidPrint(detail) {
  174. return this._scripting?.dispatchEventInSandbox({
  175. id: "doc",
  176. name: "DidPrint"
  177. });
  178. }
  179. get mouseState() {
  180. return this._mouseState;
  181. }
  182. get destroyPromise() {
  183. return this._destroyCapability?.promise || null;
  184. }
  185. get ready() {
  186. return this._ready;
  187. }
  188. get _internalEvents() {
  189. return (0, _pdf.shadow)(this, "_internalEvents", new Map());
  190. }
  191. get _domEvents() {
  192. return (0, _pdf.shadow)(this, "_domEvents", new Map());
  193. }
  194. get _pageOpenPending() {
  195. return (0, _pdf.shadow)(this, "_pageOpenPending", new Set());
  196. }
  197. get _visitedPages() {
  198. return (0, _pdf.shadow)(this, "_visitedPages", new Map());
  199. }
  200. async _updateFromSandbox(detail) {
  201. const isInPresentationMode = this._pdfViewer.isInPresentationMode || this._pdfViewer.isChangingPresentationMode;
  202. const {
  203. id,
  204. command,
  205. value
  206. } = detail;
  207. if (!id) {
  208. switch (command) {
  209. case "clear":
  210. console.clear();
  211. break;
  212. case "error":
  213. console.error(value);
  214. break;
  215. case "layout":
  216. this._pdfViewer.spreadMode = (0, _ui_utils.apiPageLayoutToSpreadMode)(value);
  217. break;
  218. case "page-num":
  219. this._pdfViewer.currentPageNumber = value + 1;
  220. break;
  221. case "print":
  222. await this._pdfViewer.pagesPromise;
  223. this._eventBus.dispatch("print", {
  224. source: this
  225. });
  226. break;
  227. case "println":
  228. console.log(value);
  229. break;
  230. case "zoom":
  231. if (isInPresentationMode) {
  232. return;
  233. }
  234. this._pdfViewer.currentScaleValue = value;
  235. break;
  236. }
  237. return;
  238. }
  239. if (isInPresentationMode) {
  240. if (detail.focus) {
  241. return;
  242. }
  243. }
  244. const element = document.getElementById(id);
  245. if (element) {
  246. element.dispatchEvent(new CustomEvent("updatefromsandbox", {
  247. detail
  248. }));
  249. } else {
  250. delete detail.id;
  251. this._pdfDocument?.annotationStorage.setValue(id, detail);
  252. }
  253. }
  254. async _dispatchPageOpen(pageNumber, initialize = false) {
  255. const pdfDocument = this._pdfDocument,
  256. visitedPages = this._visitedPages;
  257. if (initialize) {
  258. this._closeCapability = (0, _pdf.createPromiseCapability)();
  259. this._pageEventsReady = true;
  260. }
  261. if (!this._pageEventsReady) {
  262. return;
  263. }
  264. const pageView = this._pdfViewer.getPageView(pageNumber - 1);
  265. if (pageView?.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  266. this._pageOpenPending.add(pageNumber);
  267. return;
  268. }
  269. this._pageOpenPending.delete(pageNumber);
  270. const actionsPromise = (async () => {
  271. const actions = await (!visitedPages.has(pageNumber) ? pageView.pdfPage?.getJSActions() : null);
  272. if (pdfDocument !== this._pdfDocument) {
  273. return;
  274. }
  275. await this._scripting?.dispatchEventInSandbox({
  276. id: "page",
  277. name: "PageOpen",
  278. pageNumber,
  279. actions
  280. });
  281. })();
  282. visitedPages.set(pageNumber, actionsPromise);
  283. }
  284. async _dispatchPageClose(pageNumber) {
  285. const pdfDocument = this._pdfDocument,
  286. visitedPages = this._visitedPages;
  287. if (!this._pageEventsReady) {
  288. return;
  289. }
  290. if (this._pageOpenPending.has(pageNumber)) {
  291. return;
  292. }
  293. const actionsPromise = visitedPages.get(pageNumber);
  294. if (!actionsPromise) {
  295. return;
  296. }
  297. visitedPages.set(pageNumber, null);
  298. await actionsPromise;
  299. if (pdfDocument !== this._pdfDocument) {
  300. return;
  301. }
  302. await this._scripting?.dispatchEventInSandbox({
  303. id: "page",
  304. name: "PageClose",
  305. pageNumber
  306. });
  307. }
  308. async _getDocProperties() {
  309. if (this._docPropertiesLookup) {
  310. return this._docPropertiesLookup(this._pdfDocument);
  311. }
  312. throw new Error("_getDocProperties: Unable to lookup properties.");
  313. }
  314. _createScripting() {
  315. this._destroyCapability = (0, _pdf.createPromiseCapability)();
  316. if (this._scripting) {
  317. throw new Error("_createScripting: Scripting already exists.");
  318. }
  319. if (this._scriptingFactory) {
  320. return this._scriptingFactory.createScripting({
  321. sandboxBundleSrc: this._sandboxBundleSrc
  322. });
  323. }
  324. throw new Error("_createScripting: Cannot create scripting.");
  325. }
  326. async _destroyScripting() {
  327. if (!this._scripting) {
  328. this._pdfDocument = null;
  329. this._destroyCapability?.resolve();
  330. return;
  331. }
  332. if (this._closeCapability) {
  333. await Promise.race([this._closeCapability.promise, new Promise(resolve => {
  334. setTimeout(resolve, 1000);
  335. })]).catch(reason => {});
  336. this._closeCapability = null;
  337. }
  338. this._pdfDocument = null;
  339. try {
  340. await this._scripting.destroySandbox();
  341. } catch (ex) {}
  342. for (const [name, listener] of this._internalEvents) {
  343. this._eventBus._off(name, listener);
  344. }
  345. this._internalEvents.clear();
  346. for (const [name, listener] of this._domEvents) {
  347. window.removeEventListener(name, listener);
  348. }
  349. this._domEvents.clear();
  350. this._pageOpenPending.clear();
  351. this._visitedPages.clear();
  352. this._scripting = null;
  353. delete this._mouseState.isDown;
  354. this._pageEventsReady = false;
  355. this._ready = false;
  356. this._destroyCapability?.resolve();
  357. }
  358. }
  359. exports.PDFScriptingManager = PDFScriptingManager;