QRCodeWorker.sys.mjs (1971B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * QRCodeWorker - Worker-based QR code generation 7 * 8 * This module provides a worker-based implementation for QR code generation 9 * to avoid blocking the main thread during QR code processing. 10 */ 11 12 import { BasePromiseWorker } from "resource://gre/modules/PromiseWorker.sys.mjs"; 13 14 const lazy = {}; 15 16 ChromeUtils.defineLazyGetter(lazy, "logConsole", function () { 17 return console.createInstance({ 18 prefix: "QRCodeWorker", 19 maxLogLevel: Services.prefs.getBoolPref("browser.qrcode.log", false) 20 ? "Debug" 21 : "Warn", 22 }); 23 }); 24 25 /** 26 * Worker wrapper for QR code generation 27 */ 28 export class QRCodeWorker extends BasePromiseWorker { 29 constructor() { 30 super("moz-src:///browser/components/qrcode/QRCodeWorker.worker.mjs", { 31 type: "module", 32 }); 33 34 // Set up logging 35 this.log = (...args) => lazy.logConsole.debug(...args); 36 } 37 38 /** 39 * Simple ping test for worker communication 40 * 41 * @returns {Promise<string>} Returns "pong" 42 */ 43 async ping() { 44 return this.post("ping", []); 45 } 46 47 /** 48 * Check if the QRCode library is available in the worker 49 * 50 * @returns {Promise<boolean>} True if library is available 51 */ 52 async hasQRCodeLibrary() { 53 return this.post("hasQRCodeLibrary", []); 54 } 55 56 /** 57 * Generate a QR code for the given URL 58 * 59 * @param {string} url - The URL to encode in the QR code 60 * @param {string} errorCorrectionLevel - Error correction level (L, M, Q, H) 61 * @returns {Promise<object>} Object with width, height, and src data URI 62 */ 63 async generateQRCode(url, errorCorrectionLevel = "H") { 64 return this.post("generateQRCode", [url, errorCorrectionLevel]); 65 } 66 67 /** 68 * Terminate the worker and clean up resources 69 */ 70 async terminate() { 71 super.terminate(); 72 } 73 }