2
0

firefoxcom.js 9.6 KB

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