firefoxcom.js 9.3 KB

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