tor-browser

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

worker-component-data.js (2519B)


      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 "use strict";
      6 
      7 const {
      8  DEBUG_TARGETS,
      9  REQUEST_WORKERS_SUCCESS,
     10  SERVICE_WORKER_FETCH_STATES,
     11  SERVICE_WORKER_STATUSES,
     12 } = require("resource://devtools/client/aboutdebugging/src/constants.js");
     13 
     14 /**
     15 * This middleware converts workers object that get from DevToolsClient.listAllWorkers()
     16 * to data which is used in DebugTargetItem.
     17 */
     18 const workerComponentDataMiddleware = () => next => action => {
     19  switch (action.type) {
     20    case REQUEST_WORKERS_SUCCESS: {
     21      action.otherWorkers = toComponentData(action.otherWorkers);
     22      action.serviceWorkers = toComponentData(action.serviceWorkers, true);
     23      action.sharedWorkers = toComponentData(action.sharedWorkers);
     24      break;
     25    }
     26  }
     27 
     28  return next(action);
     29 };
     30 
     31 function getServiceWorkerStatus(worker) {
     32  const isActive = worker.state === Ci.nsIServiceWorkerInfo.STATE_ACTIVATED;
     33  const isRunning = !!worker.workerDescriptorFront;
     34 
     35  if (isActive && isRunning) {
     36    return SERVICE_WORKER_STATUSES.RUNNING;
     37  } else if (isActive) {
     38    return SERVICE_WORKER_STATUSES.STOPPED;
     39  }
     40 
     41  // We cannot get service worker registrations unless the registration is in
     42  // ACTIVE state. Unable to know the actual state ("installing", "waiting"), we
     43  // display a custom state "registering" for now. See Bug 1153292.
     44  return SERVICE_WORKER_STATUSES.REGISTERING;
     45 }
     46 
     47 function toComponentData(workers, isServiceWorker) {
     48  return workers.map(worker => {
     49    // Here `worker` is the worker object created by RootFront.listAllWorkers
     50    const type = DEBUG_TARGETS.WORKER;
     51    const icon = "chrome://devtools/skin/images/debugging-workers.svg";
     52    let { fetch } = worker;
     53    const { id, name, registrationFront, scope, subscription, origin } = worker;
     54 
     55    let pushServiceEndpoint = null;
     56    let status = null;
     57 
     58    if (isServiceWorker) {
     59      fetch = fetch
     60        ? SERVICE_WORKER_FETCH_STATES.LISTENING
     61        : SERVICE_WORKER_FETCH_STATES.NOT_LISTENING;
     62      status = getServiceWorkerStatus(worker);
     63      pushServiceEndpoint = subscription ? subscription.endpoint : null;
     64    }
     65 
     66    return {
     67      details: {
     68        fetch,
     69        pushServiceEndpoint,
     70        registrationFront,
     71        scope,
     72        status,
     73        origin,
     74      },
     75      icon,
     76      id,
     77      name,
     78      type,
     79    };
     80  });
     81 }
     82 
     83 module.exports = workerComponentDataMiddleware;