firefoxcom.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.FirefoxCom = exports.DownloadManager = void 0;
  27. require("../extensions/firefox/tools/l10n.js");
  28. var _app = require("./app.js");
  29. var _pdf = require("../pdf");
  30. var _preferences = require("./preferences.js");
  31. var _ui_utils = require("./ui_utils.js");
  32. {
  33. throw new Error('Module "./firefoxcom.js" shall not be used outside MOZCENTRAL builds.');
  34. }
  35. class FirefoxCom {
  36. static requestSync(action, data) {
  37. const request = document.createTextNode("");
  38. document.documentElement.appendChild(request);
  39. const sender = document.createEvent("CustomEvent");
  40. sender.initCustomEvent("pdf.js.message", true, false, {
  41. action,
  42. data,
  43. sync: true
  44. });
  45. request.dispatchEvent(sender);
  46. const response = sender.detail.response;
  47. request.remove();
  48. return response;
  49. }
  50. static requestAsync(action, data) {
  51. return new Promise(resolve => {
  52. this.request(action, data, resolve);
  53. });
  54. }
  55. static request(action, data, callback = null) {
  56. const request = document.createTextNode("");
  57. if (callback) {
  58. request.addEventListener("pdf.js.response", event => {
  59. const response = event.detail.response;
  60. event.target.remove();
  61. callback(response);
  62. }, {
  63. once: true
  64. });
  65. }
  66. document.documentElement.appendChild(request);
  67. const sender = document.createEvent("CustomEvent");
  68. sender.initCustomEvent("pdf.js.message", true, false, {
  69. action,
  70. data,
  71. sync: false,
  72. responseExpected: !!callback
  73. });
  74. request.dispatchEvent(sender);
  75. }
  76. }
  77. exports.FirefoxCom = FirefoxCom;
  78. class DownloadManager {
  79. downloadUrl(url, filename) {
  80. FirefoxCom.request("download", {
  81. originalUrl: url,
  82. filename
  83. });
  84. }
  85. downloadData(data, filename, contentType) {
  86. const blobUrl = URL.createObjectURL(new Blob([data], {
  87. type: contentType
  88. }));
  89. FirefoxCom.requestAsync("download", {
  90. blobUrl,
  91. originalUrl: blobUrl,
  92. filename,
  93. isAttachment: true
  94. }).then(error => {
  95. URL.revokeObjectURL(blobUrl);
  96. });
  97. }
  98. download(blob, url, filename, sourceEventType = "download") {
  99. const blobUrl = URL.createObjectURL(blob);
  100. FirefoxCom.requestAsync("download", {
  101. blobUrl,
  102. originalUrl: url,
  103. filename,
  104. sourceEventType
  105. }).then(error => {
  106. if (error) {
  107. console.error("`ChromeActions.download` failed.");
  108. }
  109. URL.revokeObjectURL(blobUrl);
  110. });
  111. }
  112. }
  113. exports.DownloadManager = DownloadManager;
  114. class FirefoxPreferences extends _preferences.BasePreferences {
  115. async _writeToStorage(prefObj) {
  116. return FirefoxCom.requestAsync("setPreferences", prefObj);
  117. }
  118. async _readFromStorage(prefObj) {
  119. const prefStr = await FirefoxCom.requestAsync("getPreferences", prefObj);
  120. return JSON.parse(prefStr);
  121. }
  122. }
  123. class MozL10n {
  124. constructor(mozL10n) {
  125. this.mozL10n = mozL10n;
  126. }
  127. async getLanguage() {
  128. return this.mozL10n.getLanguage();
  129. }
  130. async getDirection() {
  131. return this.mozL10n.getDirection();
  132. }
  133. async get(property, args, fallback) {
  134. return this.mozL10n.get(property, args, fallback);
  135. }
  136. async translate(element) {
  137. this.mozL10n.translate(element);
  138. }
  139. }
  140. (function listenFindEvents() {
  141. const events = ["find", "findagain", "findhighlightallchange", "findcasesensitivitychange", "findentirewordchange", "findbarclose"];
  142. const handleEvent = function ({
  143. type,
  144. detail
  145. }) {
  146. if (!_app.PDFViewerApplication.initialized) {
  147. return;
  148. }
  149. if (type === "findbarclose") {
  150. _app.PDFViewerApplication.eventBus.dispatch(type, {
  151. source: window
  152. });
  153. return;
  154. }
  155. _app.PDFViewerApplication.eventBus.dispatch("find", {
  156. source: window,
  157. type: type.substring("find".length),
  158. query: detail.query,
  159. phraseSearch: true,
  160. caseSensitive: !!detail.caseSensitive,
  161. entireWord: !!detail.entireWord,
  162. highlightAll: !!detail.highlightAll,
  163. findPrevious: !!detail.findPrevious
  164. });
  165. };
  166. for (const event of events) {
  167. window.addEventListener(event, handleEvent);
  168. }
  169. })();
  170. (function listenZoomEvents() {
  171. const events = ["zoomin", "zoomout", "zoomreset"];
  172. const handleEvent = function ({
  173. type,
  174. detail
  175. }) {
  176. if (!_app.PDFViewerApplication.initialized) {
  177. return;
  178. }
  179. if (type === "zoomreset" && _app.PDFViewerApplication.pdfViewer.currentScaleValue === _ui_utils.DEFAULT_SCALE_VALUE) {
  180. return;
  181. }
  182. _app.PDFViewerApplication.eventBus.dispatch(type, {
  183. source: window
  184. });
  185. };
  186. for (const event of events) {
  187. window.addEventListener(event, handleEvent);
  188. }
  189. })();
  190. (function listenSaveEvent() {
  191. const handleEvent = function ({
  192. type,
  193. detail
  194. }) {
  195. if (!_app.PDFViewerApplication.initialized) {
  196. return;
  197. }
  198. _app.PDFViewerApplication.eventBus.dispatch(type, {
  199. source: window
  200. });
  201. };
  202. window.addEventListener("save", handleEvent);
  203. })();
  204. class FirefoxComDataRangeTransport extends _pdf.PDFDataRangeTransport {
  205. requestDataRange(begin, end) {
  206. FirefoxCom.request("requestDataRange", {
  207. begin,
  208. end
  209. });
  210. }
  211. abort() {
  212. FirefoxCom.requestSync("abortLoading", null);
  213. }
  214. }
  215. class FirefoxScripting {
  216. static async createSandbox(data) {
  217. const success = await FirefoxCom.requestAsync("createSandbox", data);
  218. if (!success) {
  219. throw new Error("Cannot create sandbox.");
  220. }
  221. }
  222. static async dispatchEventInSandbox(event) {
  223. FirefoxCom.request("dispatchEventInSandbox", event);
  224. }
  225. static async destroySandbox() {
  226. FirefoxCom.request("destroySandbox", null);
  227. }
  228. }
  229. class FirefoxExternalServices extends _app.DefaultExternalServices {
  230. static updateFindControlState(data) {
  231. FirefoxCom.request("updateFindControlState", data);
  232. }
  233. static updateFindMatchesCount(data) {
  234. FirefoxCom.request("updateFindMatchesCount", data);
  235. }
  236. static initPassiveLoading(callbacks) {
  237. let pdfDataRangeTransport;
  238. window.addEventListener("message", function windowMessage(e) {
  239. if (e.source !== null) {
  240. console.warn("Rejected untrusted message from " + e.origin);
  241. return;
  242. }
  243. const args = e.data;
  244. if (typeof args !== "object" || !("pdfjsLoadAction" in args)) {
  245. return;
  246. }
  247. switch (args.pdfjsLoadAction) {
  248. case "supportsRangedLoading":
  249. pdfDataRangeTransport = new FirefoxComDataRangeTransport(args.length, args.data, args.done);
  250. callbacks.onOpenWithTransport(args.pdfUrl, args.length, pdfDataRangeTransport);
  251. break;
  252. case "range":
  253. pdfDataRangeTransport.onDataRange(args.begin, args.chunk);
  254. break;
  255. case "rangeProgress":
  256. pdfDataRangeTransport.onDataProgress(args.loaded);
  257. break;
  258. case "progressiveRead":
  259. pdfDataRangeTransport.onDataProgressiveRead(args.chunk);
  260. pdfDataRangeTransport.onDataProgress(args.loaded, args.total);
  261. break;
  262. case "progressiveDone":
  263. if (pdfDataRangeTransport) {
  264. pdfDataRangeTransport.onDataProgressiveDone();
  265. }
  266. break;
  267. case "progress":
  268. callbacks.onProgress(args.loaded, args.total);
  269. break;
  270. case "complete":
  271. if (!args.data) {
  272. callbacks.onError(args.errorCode);
  273. break;
  274. }
  275. callbacks.onOpenWithData(args.data);
  276. break;
  277. }
  278. });
  279. FirefoxCom.requestSync("initPassiveLoading", null);
  280. }
  281. static async fallback(data) {
  282. return FirefoxCom.requestAsync("fallback", data);
  283. }
  284. static reportTelemetry(data) {
  285. FirefoxCom.request("reportTelemetry", JSON.stringify(data));
  286. }
  287. static createDownloadManager(options) {
  288. return new DownloadManager();
  289. }
  290. static createPreferences() {
  291. return new FirefoxPreferences();
  292. }
  293. static createL10n(options) {
  294. const mozL10n = document.mozL10n;
  295. return new MozL10n(mozL10n);
  296. }
  297. static createScripting(options) {
  298. return FirefoxScripting;
  299. }
  300. static get supportsIntegratedFind() {
  301. const support = FirefoxCom.requestSync("supportsIntegratedFind");
  302. return (0, _pdf.shadow)(this, "supportsIntegratedFind", support);
  303. }
  304. static get supportsDocumentFonts() {
  305. const support = FirefoxCom.requestSync("supportsDocumentFonts");
  306. return (0, _pdf.shadow)(this, "supportsDocumentFonts", support);
  307. }
  308. static get supportedMouseWheelZoomModifierKeys() {
  309. const support = FirefoxCom.requestSync("supportedMouseWheelZoomModifierKeys");
  310. return (0, _pdf.shadow)(this, "supportedMouseWheelZoomModifierKeys", support);
  311. }
  312. static get isInAutomation() {
  313. const isInAutomation = FirefoxCom.requestSync("isInAutomation");
  314. return (0, _pdf.shadow)(this, "isInAutomation", isInAutomation);
  315. }
  316. }
  317. _app.PDFViewerApplication.externalServices = FirefoxExternalServices;
  318. document.mozL10n.setExternalLocalizerServices({
  319. getLocale() {
  320. return FirefoxCom.requestSync("getLocale", null);
  321. },
  322. getStrings(key) {
  323. return FirefoxCom.requestSync("getStrings", key);
  324. }
  325. });