firefoxcom.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import "../extensions/firefox/tools/l10n.js";
  16. import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
  17. import { isPdfFile, PDFDataRangeTransport, shadow } from "pdfjs-lib";
  18. import { BasePreferences } from "./preferences.js";
  19. import { DEFAULT_SCALE_VALUE } from "./ui_utils.js";
  20. import { getL10nFallback } from "./l10n_utils.js";
  21. if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
  22. throw new Error(
  23. 'Module "./firefoxcom.js" shall not be used outside MOZCENTRAL builds.'
  24. );
  25. }
  26. class FirefoxCom {
  27. /**
  28. * Creates an event that the extension is listening for and will
  29. * synchronously respond to.
  30. * NOTE: It is recommended to use requestAsync() instead since one day we may
  31. * not be able to synchronously reply.
  32. * @param {string} action - The action to trigger.
  33. * @param {Object|string} [data] - The data to send.
  34. * @returns {*} The response.
  35. */
  36. static requestSync(action, data) {
  37. const request = document.createTextNode("");
  38. document.documentElement.append(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. /**
  51. * Creates an event that the extension is listening for and will
  52. * asynchronously respond to.
  53. * @param {string} action - The action to trigger.
  54. * @param {Object|string} [data] - The data to send.
  55. * @returns {Promise<any>} A promise that is resolved with the response data.
  56. */
  57. static requestAsync(action, data) {
  58. return new Promise(resolve => {
  59. this.request(action, data, resolve);
  60. });
  61. }
  62. /**
  63. * Creates an event that the extension is listening for and will, optionally,
  64. * asynchronously respond to.
  65. * @param {string} action - The action to trigger.
  66. * @param {Object|string} [data] - The data to send.
  67. */
  68. static request(action, data, callback = null) {
  69. const request = document.createTextNode("");
  70. if (callback) {
  71. request.addEventListener(
  72. "pdf.js.response",
  73. event => {
  74. const response = event.detail.response;
  75. event.target.remove();
  76. callback(response);
  77. },
  78. { once: true }
  79. );
  80. }
  81. document.documentElement.append(request);
  82. const sender = document.createEvent("CustomEvent");
  83. sender.initCustomEvent("pdf.js.message", true, false, {
  84. action,
  85. data,
  86. sync: false,
  87. responseExpected: !!callback,
  88. });
  89. request.dispatchEvent(sender);
  90. }
  91. }
  92. class DownloadManager {
  93. #openBlobUrls = new WeakMap();
  94. downloadUrl(url, filename) {
  95. FirefoxCom.request("download", {
  96. originalUrl: url,
  97. filename,
  98. });
  99. }
  100. downloadData(data, filename, contentType) {
  101. const blobUrl = URL.createObjectURL(
  102. new Blob([data], { type: contentType })
  103. );
  104. FirefoxCom.request("download", {
  105. blobUrl,
  106. originalUrl: blobUrl,
  107. filename,
  108. isAttachment: true,
  109. });
  110. }
  111. /**
  112. * @returns {boolean} Indicating if the data was opened.
  113. */
  114. openOrDownloadData(element, data, filename) {
  115. const isPdfData = isPdfFile(filename);
  116. const contentType = isPdfData ? "application/pdf" : "";
  117. if (isPdfData) {
  118. let blobUrl = this.#openBlobUrls.get(element);
  119. if (!blobUrl) {
  120. blobUrl = URL.createObjectURL(new Blob([data], { type: contentType }));
  121. this.#openBlobUrls.set(element, blobUrl);
  122. }
  123. // Let Firefox's content handler catch the URL and display the PDF.
  124. const viewerUrl = blobUrl + "#filename=" + encodeURIComponent(filename);
  125. try {
  126. window.open(viewerUrl);
  127. return true;
  128. } catch (ex) {
  129. console.error(`openOrDownloadData: ${ex}`);
  130. // Release the `blobUrl`, since opening it failed, and fallback to
  131. // downloading the PDF file.
  132. URL.revokeObjectURL(blobUrl);
  133. this.#openBlobUrls.delete(element);
  134. }
  135. }
  136. this.downloadData(data, filename, contentType);
  137. return false;
  138. }
  139. download(blob, url, filename) {
  140. const blobUrl = URL.createObjectURL(blob);
  141. FirefoxCom.request("download", {
  142. blobUrl,
  143. originalUrl: url,
  144. filename,
  145. });
  146. }
  147. }
  148. class FirefoxPreferences extends BasePreferences {
  149. async _writeToStorage(prefObj) {
  150. return FirefoxCom.requestAsync("setPreferences", prefObj);
  151. }
  152. async _readFromStorage(prefObj) {
  153. const prefStr = await FirefoxCom.requestAsync("getPreferences", prefObj);
  154. return JSON.parse(prefStr);
  155. }
  156. }
  157. class MozL10n {
  158. constructor(mozL10n) {
  159. this.mozL10n = mozL10n;
  160. }
  161. async getLanguage() {
  162. return this.mozL10n.getLanguage();
  163. }
  164. async getDirection() {
  165. return this.mozL10n.getDirection();
  166. }
  167. async get(key, args = null, fallback = getL10nFallback(key, args)) {
  168. return this.mozL10n.get(key, args, fallback);
  169. }
  170. async translate(element) {
  171. this.mozL10n.translate(element);
  172. }
  173. }
  174. (function listenFindEvents() {
  175. const events = [
  176. "find",
  177. "findagain",
  178. "findhighlightallchange",
  179. "findcasesensitivitychange",
  180. "findentirewordchange",
  181. "findbarclose",
  182. "finddiacriticmatchingchange",
  183. ];
  184. const findLen = "find".length;
  185. const handleEvent = function ({ type, detail }) {
  186. if (!PDFViewerApplication.initialized) {
  187. return;
  188. }
  189. if (type === "findbarclose") {
  190. PDFViewerApplication.eventBus.dispatch(type, { source: window });
  191. return;
  192. }
  193. PDFViewerApplication.eventBus.dispatch("find", {
  194. source: window,
  195. type: type.substring(findLen),
  196. query: detail.query,
  197. phraseSearch: true,
  198. caseSensitive: !!detail.caseSensitive,
  199. entireWord: !!detail.entireWord,
  200. highlightAll: !!detail.highlightAll,
  201. findPrevious: !!detail.findPrevious,
  202. matchDiacritics: !!detail.matchDiacritics,
  203. });
  204. };
  205. for (const event of events) {
  206. window.addEventListener(event, handleEvent);
  207. }
  208. })();
  209. (function listenZoomEvents() {
  210. const events = ["zoomin", "zoomout", "zoomreset"];
  211. const handleEvent = function ({ type, detail }) {
  212. if (!PDFViewerApplication.initialized) {
  213. return;
  214. }
  215. // Avoid attempting to needlessly reset the zoom level *twice* in a row,
  216. // when using the `Ctrl + 0` keyboard shortcut.
  217. if (
  218. type === "zoomreset" &&
  219. PDFViewerApplication.pdfViewer.currentScaleValue === DEFAULT_SCALE_VALUE
  220. ) {
  221. return;
  222. }
  223. PDFViewerApplication.eventBus.dispatch(type, { source: window });
  224. };
  225. for (const event of events) {
  226. window.addEventListener(event, handleEvent);
  227. }
  228. })();
  229. (function listenSaveEvent() {
  230. const handleEvent = function ({ type, detail }) {
  231. if (!PDFViewerApplication.initialized) {
  232. return;
  233. }
  234. PDFViewerApplication.eventBus.dispatch("download", { source: window });
  235. };
  236. window.addEventListener("save", handleEvent);
  237. })();
  238. (function listenEditingEvent() {
  239. const handleEvent = function ({ detail }) {
  240. if (!PDFViewerApplication.initialized) {
  241. return;
  242. }
  243. PDFViewerApplication.eventBus.dispatch("editingaction", {
  244. source: window,
  245. name: detail.name,
  246. });
  247. };
  248. window.addEventListener("editingaction", handleEvent);
  249. })();
  250. class FirefoxComDataRangeTransport extends PDFDataRangeTransport {
  251. requestDataRange(begin, end) {
  252. FirefoxCom.request("requestDataRange", { begin, end });
  253. }
  254. abort() {
  255. // Sync call to ensure abort is really started.
  256. FirefoxCom.requestSync("abortLoading", null);
  257. }
  258. }
  259. class FirefoxScripting {
  260. static async createSandbox(data) {
  261. const success = await FirefoxCom.requestAsync("createSandbox", data);
  262. if (!success) {
  263. throw new Error("Cannot create sandbox.");
  264. }
  265. }
  266. static async dispatchEventInSandbox(event) {
  267. FirefoxCom.request("dispatchEventInSandbox", event);
  268. }
  269. static async destroySandbox() {
  270. FirefoxCom.request("destroySandbox", null);
  271. }
  272. }
  273. class FirefoxExternalServices extends DefaultExternalServices {
  274. static updateFindControlState(data) {
  275. FirefoxCom.request("updateFindControlState", data);
  276. }
  277. static updateFindMatchesCount(data) {
  278. FirefoxCom.request("updateFindMatchesCount", data);
  279. }
  280. static initPassiveLoading(callbacks) {
  281. let pdfDataRangeTransport;
  282. window.addEventListener("message", function windowMessage(e) {
  283. if (e.source !== null) {
  284. // The message MUST originate from Chrome code.
  285. console.warn("Rejected untrusted message from " + e.origin);
  286. return;
  287. }
  288. const args = e.data;
  289. if (typeof args !== "object" || !("pdfjsLoadAction" in args)) {
  290. return;
  291. }
  292. switch (args.pdfjsLoadAction) {
  293. case "supportsRangedLoading":
  294. if (args.done && !args.data) {
  295. callbacks.onError();
  296. break;
  297. }
  298. pdfDataRangeTransport = new FirefoxComDataRangeTransport(
  299. args.length,
  300. args.data,
  301. args.done,
  302. args.filename
  303. );
  304. callbacks.onOpenWithTransport(
  305. args.pdfUrl,
  306. args.length,
  307. pdfDataRangeTransport
  308. );
  309. break;
  310. case "range":
  311. pdfDataRangeTransport.onDataRange(args.begin, args.chunk);
  312. break;
  313. case "rangeProgress":
  314. pdfDataRangeTransport.onDataProgress(args.loaded);
  315. break;
  316. case "progressiveRead":
  317. pdfDataRangeTransport.onDataProgressiveRead(args.chunk);
  318. // Don't forget to report loading progress as well, since otherwise
  319. // the loadingBar won't update when `disableRange=true` is set.
  320. pdfDataRangeTransport.onDataProgress(args.loaded, args.total);
  321. break;
  322. case "progressiveDone":
  323. pdfDataRangeTransport?.onDataProgressiveDone();
  324. break;
  325. case "progress":
  326. callbacks.onProgress(args.loaded, args.total);
  327. break;
  328. case "complete":
  329. if (!args.data) {
  330. callbacks.onError(args.errorCode);
  331. break;
  332. }
  333. callbacks.onOpenWithData(args.data, args.filename);
  334. break;
  335. }
  336. });
  337. FirefoxCom.requestSync("initPassiveLoading", null);
  338. }
  339. static reportTelemetry(data) {
  340. FirefoxCom.request("reportTelemetry", JSON.stringify(data));
  341. }
  342. static createDownloadManager() {
  343. return new DownloadManager();
  344. }
  345. static createPreferences() {
  346. return new FirefoxPreferences();
  347. }
  348. static updateEditorStates(data) {
  349. FirefoxCom.request("updateEditorStates", data);
  350. }
  351. static createL10n(options) {
  352. const mozL10n = document.mozL10n;
  353. // TODO refactor mozL10n.setExternalLocalizerServices
  354. return new MozL10n(mozL10n);
  355. }
  356. static createScripting(options) {
  357. return FirefoxScripting;
  358. }
  359. static get supportsIntegratedFind() {
  360. const support = FirefoxCom.requestSync("supportsIntegratedFind");
  361. return shadow(this, "supportsIntegratedFind", support);
  362. }
  363. static get supportsDocumentFonts() {
  364. const support = FirefoxCom.requestSync("supportsDocumentFonts");
  365. return shadow(this, "supportsDocumentFonts", support);
  366. }
  367. static get supportedMouseWheelZoomModifierKeys() {
  368. const support = FirefoxCom.requestSync(
  369. "supportedMouseWheelZoomModifierKeys"
  370. );
  371. return shadow(this, "supportedMouseWheelZoomModifierKeys", support);
  372. }
  373. static get isInAutomation() {
  374. // Returns the value of `Cu.isInAutomation`, which is only `true` when e.g.
  375. // various test-suites are running in mozilla-central.
  376. const isInAutomation = FirefoxCom.requestSync("isInAutomation");
  377. return shadow(this, "isInAutomation", isInAutomation);
  378. }
  379. }
  380. PDFViewerApplication.externalServices = FirefoxExternalServices;
  381. // l10n.js for Firefox extension expects services to be set.
  382. document.mozL10n.setExternalLocalizerServices({
  383. getLocale() {
  384. return FirefoxCom.requestSync("getLocale", null);
  385. },
  386. getStrings(key) {
  387. return FirefoxCom.requestSync("getStrings", null);
  388. },
  389. });
  390. export { DownloadManager, FirefoxCom };