tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

QRCodeWorker.worker.mjs (2372B)


      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 // Now load the QRCode library with the full resource URI
      6 import { QR } from "moz-src:///toolkit/components/qrcode/encoder.mjs";
      7 import { PromiseWorker } from "resource://gre/modules/workers/PromiseWorker.mjs";
      8 
      9 /**
     10 * QRCode Worker Implementation
     11 *
     12 * This worker handles QR code generation off the main thread.
     13 */
     14 
     15 /**
     16 * The QR Code generator that runs in a worker thread
     17 */
     18 class QRCodeWorkerImpl {
     19  constructor() {
     20    this.#connectToPromiseWorker();
     21  }
     22 
     23  /**
     24   * Simple ping test for worker communication
     25   *
     26   * @returns {string} Returns "pong"
     27   */
     28  ping() {
     29    return "pong";
     30  }
     31 
     32  /**
     33   * Check if the QRCode library is available
     34   *
     35   * @returns {boolean} True if library is loaded
     36   */
     37  hasQRCodeLibrary() {
     38    return typeof QR !== "undefined" && QR !== null;
     39  }
     40 
     41  /**
     42   * Generate a QR code for the given URL
     43   *
     44   * @param {string} url - The URL to encode
     45   * @param {string} errorCorrectionLevel - Error correction level (L, M, Q, H)
     46   * @returns {object} Object with width, height, and src data URI
     47   */
     48  generateQRCode(url, errorCorrectionLevel = "H") {
     49    if (!QR || !QR.encodeToDataURI) {
     50      throw new Error("QRCode library not available in worker");
     51    }
     52 
     53    // Generate the QR code data URI
     54    const qrData = QR.encodeToDataURI(url, errorCorrectionLevel);
     55 
     56    return {
     57      width: qrData.width,
     58      height: qrData.height,
     59      src: qrData.src,
     60    };
     61  }
     62 
     63  /**
     64   * Glue code to connect the `QRCodeWorkerImpl` to the PromiseWorker interface.
     65   */
     66  #connectToPromiseWorker() {
     67    const worker = new PromiseWorker.AbstractWorker();
     68 
     69    worker.dispatch = (method, args = []) => {
     70      if (!this[method]) {
     71        throw new Error("Method does not exist: " + method);
     72      }
     73      return this[method](...args);
     74    };
     75 
     76    worker.close = () => self.close();
     77 
     78    worker.postMessage = (message, ...transfers) => {
     79      self.postMessage(message, ...transfers);
     80    };
     81 
     82    self.addEventListener("message", msg => worker.handleMessage(msg));
     83    self.addEventListener("unhandledrejection", function (error) {
     84      throw error.reason;
     85    });
     86  }
     87 }
     88 
     89 // Create the worker instance
     90 new QRCodeWorkerImpl();